75d164cdac19f4d0fbd9422c4e033d8f3b883420
[dragonfly.git] / sys / net / vlan / if_vlan.c
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 $
30  */
31
32 /*
33  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
34  * Might be extended some day to also handle IEEE 802.1p priority
35  * tagging.  This is sort of sneaky in the implementation, since
36  * we need to pretend to be enough of an Ethernet implementation
37  * to make arp work.  The way we do this is by telling everyone
38  * that we are an Ethernet, and then catch the packets that
39  * ether_output() left on our output queue queue when it calls
40  * if_start(), rewrite them for use by the real outgoing interface,
41  * and ask it to send them.
42  *
43  *
44  * Note about vlan's MP safe approach:
45  *
46  * - All configuration operation, e.g. config, unconfig and change flags,
47  *   is serialized by netisr0; not by vlan's serializer
48  *
49  * - Parent interface's trunk and vlans are linked in the following
50  *   fashion:
51  *                     CPU0     CPU1     CPU2     CPU3
52  *   +--------------+--------+--------+--------+--------+
53  *   | parent ifnet |trunk[0]|trunk[1]|trunk[2]|trunk[3]|
54  *   +--------------+--------+--------+--------+--------+
55  *                       |        |        |        |
56  *                       V        V        V        V
57  *   +--------------+--------+--------+--------+--------+
58  *   |   vlan ifnet |entry[0]|entry[1]|entry[2]|entry[3]|
59  *   +--------------+--------+--------+--------+--------+
60  *                       |        |        |        |
61  *                       V        V        V        V
62  *   +--------------+--------+--------+--------+--------+
63  *   |   vlan ifnet |entry[0]|entry[1]|entry[2]|entry[3]|
64  *   +--------------+--------+--------+--------+--------+
65  *
66  * - Vlan is linked/unlinked onto parent interface's trunk using following
67  *   way:
68  *
69  *       CPU0             CPU1             CPU2             CPU3
70  *
71  *      netisr0 <---------------------------------------------+
72  *  (config/unconfig)                                         |
73  *         |                                                  |
74  *         | domsg                                            | replymsg
75  *         |                                                  |
76  *         V     fwdmsg           fwdmsg           fwdmsg     |
77  *       ifnet0 --------> ifnet1 --------> ifnet2 --------> ifnet3
78  *    (link/unlink)    (link/unlink)    (link/unlink)    (link/unlink)
79  *
80  * - Parent interface's trunk is destroyed in the following lockless way:
81  *
82  *     old_trunk = ifp->if_vlantrunks;
83  *     ifp->if_vlantrunks = NULL;
84  *     netmsg_service_sync();
85  *     (*)
86  *     free(old_trunk);
87  *
88  *   Since all of the accessing of if_vlantrunks only happens in network
89  *   threads (percpu netisr and ifnet threads), after netmsg_service_sync()
90  *   the network threads are promised to see only NULL if_vlantrunks; we
91  *   are safe to free the "to be destroyed" parent interface's trunk
92  *   afterwards.
93  */
94
95 #ifndef NVLAN
96 #include "use_vlan.h"
97 #endif
98 #include "opt_inet.h"
99
100 #include <sys/param.h>
101 #include <sys/systm.h>
102 #include <sys/kernel.h>
103 #include <sys/malloc.h>
104 #include <sys/mbuf.h>
105 #include <sys/module.h>
106 #include <sys/queue.h>
107 #include <sys/socket.h>
108 #include <sys/sockio.h>
109 #include <sys/sysctl.h>
110 #include <sys/bus.h>
111 #include <sys/thread2.h>
112
113 #include <net/bpf.h>
114 #include <net/ethernet.h>
115 #include <net/if.h>
116 #include <net/if_arp.h>
117 #include <net/if_dl.h>
118 #include <net/if_types.h>
119 #include <net/ifq_var.h>
120 #include <net/if_clone.h>
121 #include <net/netmsg2.h>
122 #include <net/netisr2.h>
123
124 #ifdef INET
125 #include <netinet/in.h>
126 #include <netinet/if_ether.h>
127 #endif
128
129 #include <net/vlan/if_vlan_var.h>
130 #include <net/vlan/if_vlan_ether.h>
131
132 struct ifvlan;
133
134 struct vlan_mc_entry {
135         struct ether_addr               mc_addr;
136         SLIST_ENTRY(vlan_mc_entry)      mc_entries;
137 };
138
139 struct vlan_entry {
140         struct ifvlan           *ifv;
141         LIST_ENTRY(vlan_entry)  ifv_link;
142 };
143
144 struct  ifvlan {
145         struct  arpcom ifv_ac;  /* make this an interface */
146         struct  ifnet *ifv_p;   /* parent inteface of this vlan */
147         int ifv_pflags;         /* special flags we have set on parent */
148         struct  ifv_linkmib {
149                 int     ifvm_parent;
150                 uint16_t ifvm_proto; /* encapsulation ethertype */
151                 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
152         }       ifv_mib;
153         SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
154         LIST_ENTRY(ifvlan) ifv_list;
155         struct vlan_entry ifv_entries[1];
156 };
157 #define ifv_if  ifv_ac.ac_if
158 #define ifv_tag ifv_mib.ifvm_tag
159
160 struct vlan_trunk {
161         LIST_HEAD(, vlan_entry) vlan_list;
162 };
163
164 struct netmsg_vlan {
165         struct netmsg_base base;
166         struct ifvlan   *nv_ifv;
167         struct ifnet    *nv_ifp_p;
168         const char      *nv_parent_name;
169         uint16_t        nv_vlantag;
170 };
171
172 #define VLANNAME        "vlan"
173
174 SYSCTL_DECL(_net_link);
175 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
176 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
177
178 static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface");
179 static LIST_HEAD(, ifvlan) ifv_list;
180
181 static int      vlan_clone_create(struct if_clone *, int, caddr_t);
182 static int      vlan_clone_destroy(struct ifnet *);
183 static void     vlan_ifdetach(void *, struct ifnet *);
184
185 static void     vlan_init(void *);
186 static void     vlan_start(struct ifnet *, struct ifaltq_subque *);
187 static int      vlan_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
188 static void     vlan_input(struct mbuf *);
189
190 static int      vlan_setflags(struct ifvlan *, struct ifnet *, int);
191 static int      vlan_setflag(struct ifvlan *, struct ifnet *, int, int,
192                              int (*)(struct ifnet *, int));
193 static int      vlan_config_flags(struct ifvlan *ifv);
194 static void     vlan_clrmulti(struct ifvlan *, struct ifnet *);
195 static int      vlan_setmulti(struct ifvlan *, struct ifnet *);
196 static int      vlan_config_multi(struct ifvlan *);
197 static int      vlan_config(struct ifvlan *, const char *, uint16_t);
198 static int      vlan_unconfig(struct ifvlan *);
199 static void     vlan_link(struct ifvlan *, struct ifnet *);
200 static void     vlan_unlink(struct ifvlan *, struct ifnet *);
201
202 static void     vlan_config_dispatch(netmsg_t);
203 static void     vlan_unconfig_dispatch(netmsg_t);
204 static void     vlan_link_dispatch(netmsg_t);
205 static void     vlan_unlink_dispatch(netmsg_t);
206 static void     vlan_multi_dispatch(netmsg_t);
207 static void     vlan_flags_dispatch(netmsg_t);
208 static void     vlan_ifdetach_dispatch(netmsg_t);
209
210 /* Special flags we should propagate to parent */
211 static struct {
212         int flag;
213         int (*func)(struct ifnet *, int);
214 } vlan_pflags[] = {
215         { IFF_PROMISC, ifpromisc },
216         { IFF_ALLMULTI, if_allmulti },
217         { 0, NULL }
218 };
219
220 static eventhandler_tag vlan_ifdetach_cookie;
221 static struct if_clone vlan_cloner =
222         IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy,
223                              NVLAN, IF_MAXUNIT);
224
225 /*
226  * Handle IFF_* flags that require certain changes on the parent:
227  * if "set" is true, update parent's flags respective to our if_flags;
228  * if "set" is false, forcedly clear the flags set on parent.
229  */
230 static int
231 vlan_setflags(struct ifvlan *ifv, struct ifnet *ifp_p, int set)
232 {
233         int error, i;
234
235         ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
236
237         for (i = 0; vlan_pflags[i].func != NULL; i++) {
238                 error = vlan_setflag(ifv, ifp_p, vlan_pflags[i].flag,
239                                      set, vlan_pflags[i].func);
240                 if (error)
241                         return error;
242         }
243         return 0;
244 }
245
246 /* Handle a reference counted flag that should be set on the parent as well */
247 static int
248 vlan_setflag(struct ifvlan *ifv, struct ifnet *ifp_p, int flag, int set,
249              int (*func)(struct ifnet *, int))
250 {
251         struct ifnet *ifp = &ifv->ifv_if;
252         int error, ifv_flag;
253
254         ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
255
256         ifv_flag = set ? (ifp->if_flags & flag) : 0;
257
258         /*
259          * See if recorded parent's status is different from what
260          * we want it to be.  If it is, flip it.  We record parent's
261          * status in ifv_pflags so that we won't clear parent's flag
262          * we haven't set.  In fact, we don't clear or set parent's
263          * flags directly, but get or release references to them.
264          * That's why we can be sure that recorded flags still are
265          * in accord with actual parent's flags.
266          */
267         if (ifv_flag != (ifv->ifv_pflags & flag)) {
268                 error = func(ifp_p, ifv_flag);
269                 if (error)
270                         return error;
271                 ifv->ifv_pflags &= ~flag;
272                 ifv->ifv_pflags |= ifv_flag;
273         }
274         return 0;
275 }
276
277 /*
278  * Program our multicast filter. What we're actually doing is
279  * programming the multicast filter of the parent. This has the
280  * side effect of causing the parent interface to receive multicast
281  * traffic that it doesn't really want, which ends up being discarded
282  * later by the upper protocol layers. Unfortunately, there's no way
283  * to avoid this: there really is only one physical interface.
284  */
285 static int
286 vlan_setmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
287 {
288         struct ifmultiaddr *ifma;
289         struct vlan_mc_entry *mc = NULL;
290         struct sockaddr_dl sdl;
291         struct ifnet *ifp = &ifv->ifv_if;
292
293         ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
294
295         /*
296          * First, remove any existing filter entries.
297          */
298         vlan_clrmulti(ifv, ifp_p);
299
300         /*
301          * Save the filter entries to be added to parent.
302          *
303          * TODO: need ifnet_serialize_main
304          */
305         ifnet_serialize_all(ifp);
306         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
307                 if (ifma->ifma_addr->sa_family != AF_LINK)
308                         continue;
309
310                 /* Save a copy */
311                 mc = kmalloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK);
312                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
313                       &mc->mc_addr, ETHER_ADDR_LEN);
314                 SLIST_INSERT_HEAD(&ifv->vlan_mc_listhead, mc, mc_entries);
315         }
316         ifnet_deserialize_all(ifp);
317
318         /*
319          * Now program new ones.
320          */
321         bzero(&sdl, sizeof(sdl));
322         sdl.sdl_len = sizeof(sdl);
323         sdl.sdl_family = AF_LINK;
324         sdl.sdl_index = ifp_p->if_index;
325         sdl.sdl_type = IFT_ETHER;
326         sdl.sdl_alen = ETHER_ADDR_LEN;
327
328         /*
329          * Program the parent multicast filter
330          */
331         SLIST_FOREACH(mc, &ifv->vlan_mc_listhead, mc_entries) {
332                 int error;
333
334                 bcopy(&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
335                 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, NULL);
336                 if (error) {
337                         /* XXX probably should keep going */
338                         return error;
339                 }
340         }
341         return 0;
342 }
343
344 static void
345 vlan_clrmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
346 {
347         struct vlan_mc_entry *mc;
348         struct sockaddr_dl sdl;
349
350         ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
351
352         bzero(&sdl, sizeof(sdl));
353         sdl.sdl_len = sizeof(sdl);
354         sdl.sdl_family = AF_LINK;
355         sdl.sdl_index = ifp_p->if_index;
356         sdl.sdl_type = IFT_ETHER;
357         sdl.sdl_alen = ETHER_ADDR_LEN;
358
359         while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
360                 bcopy(&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
361                 if_delmulti(ifp_p, (struct sockaddr *)&sdl); /* ignore error */
362
363                 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
364                 kfree(mc, M_VLAN);
365         }
366 }
367
368 static int
369 vlan_modevent(module_t mod, int type, void *data)
370 {
371         switch (type) {
372         case MOD_LOAD:
373                 LIST_INIT(&ifv_list);
374                 vlan_input_p = vlan_input;
375                 vlan_ifdetach_cookie =
376                 EVENTHANDLER_REGISTER(ifnet_detach_event,
377                                       vlan_ifdetach, NULL,
378                                       EVENTHANDLER_PRI_ANY);
379                 if_clone_attach(&vlan_cloner);
380                 break;
381
382         case MOD_UNLOAD:
383                 if_clone_detach(&vlan_cloner);
384
385                 vlan_input_p = NULL;
386                 /*
387                  * Make sure that all protocol threads see vlan_input_p change.
388                  */
389                 netmsg_service_sync();
390
391                 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
392                                         vlan_ifdetach_cookie);
393                 while (!LIST_EMPTY(&ifv_list))
394                         vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if);
395                 break;
396         }
397         return 0;
398 }
399
400 static moduledata_t vlan_mod = {
401         "if_vlan",
402         vlan_modevent,
403         0
404 };
405
406 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
407
408 static void
409 vlan_ifdetach_dispatch(netmsg_t msg)
410 {
411         struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
412         struct ifnet *ifp_p = vmsg->nv_ifp_p;
413         struct vlan_trunk *vlantrunks, *trunk;
414         struct vlan_entry *ifve;
415
416         vlantrunks = ifp_p->if_vlantrunks;
417         if (vlantrunks == NULL)
418                 goto reply;
419         trunk = &vlantrunks[mycpuid];
420
421         while (ifp_p->if_vlantrunks &&
422                (ifve = LIST_FIRST(&trunk->vlan_list)) != NULL)
423                 vlan_unconfig(ifve->ifv);
424 reply:
425         lwkt_replymsg(&vmsg->base.lmsg, 0);
426 }
427
428 static void
429 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
430 {
431         struct netmsg_vlan vmsg;
432
433         ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
434
435         bzero(&vmsg, sizeof(vmsg));
436
437         netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
438                     0, vlan_ifdetach_dispatch);
439         vmsg.nv_ifp_p = ifp;
440
441         lwkt_domsg(netisr_cpuport(0), &vmsg.base.lmsg, 0);
442 }
443
444 static int
445 vlan_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
446 {
447         struct ifvlan *ifv;
448         struct ifnet *ifp;
449         int vlan_size, i;
450
451         vlan_size = sizeof(struct ifvlan)
452                   + ((ncpus - 1) * sizeof(struct vlan_entry));
453         ifv = kmalloc(vlan_size, M_VLAN, M_WAITOK | M_ZERO);
454         SLIST_INIT(&ifv->vlan_mc_listhead);
455         for (i = 0; i < ncpus; ++i)
456                 ifv->ifv_entries[i].ifv = ifv;
457
458         crit_enter();   /* XXX not MP safe */
459         LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
460         crit_exit();
461
462         ifp = &ifv->ifv_if;
463         ifp->if_softc = ifv;
464         if_initname(ifp, "vlan", unit);
465         /* NB: flags are not set here */
466         ifp->if_linkmib = &ifv->ifv_mib;
467         ifp->if_linkmiblen = sizeof ifv->ifv_mib;
468         /* NB: mtu is not set here */
469
470         ifp->if_init = vlan_init;
471         ifp->if_start = vlan_start;
472         ifp->if_ioctl = vlan_ioctl;
473         ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
474         ifq_set_ready(&ifp->if_snd);
475         ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr, NULL);
476         /* Now undo some of the damage... */
477         ifp->if_data.ifi_type = IFT_L2VLAN;
478         ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN;
479
480         return (0);
481 }
482
483 static int
484 vlan_clone_destroy(struct ifnet *ifp)
485 {
486         struct ifvlan *ifv = ifp->if_softc;
487
488         crit_enter();   /* XXX not MP safe */
489         LIST_REMOVE(ifv, ifv_list);
490         crit_exit();
491
492         vlan_unconfig(ifv);
493         ether_ifdetach(ifp);
494
495         kfree(ifv, M_VLAN);
496
497         return 0;
498 }
499
500 static void
501 vlan_init(void *xsc)
502 {
503         struct ifvlan *ifv = xsc;
504         struct ifnet *ifp = &ifv->ifv_if;
505
506         ASSERT_IFNET_SERIALIZED_ALL(ifp);
507
508         if (ifv->ifv_p != NULL)
509                 ifp->if_flags |= IFF_RUNNING;
510 }
511
512 static void
513 vlan_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
514 {
515         struct ifvlan *ifv = ifp->if_softc;
516         struct ifnet *ifp_p = ifv->ifv_p;
517         struct mbuf *m;
518         lwkt_port_t p_port;
519
520         ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
521         ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
522
523         if (ifp_p == NULL) {
524                 ifsq_purge(ifsq);
525                 return;
526         }
527
528         if ((ifp->if_flags & IFF_RUNNING) == 0)
529                 return;
530
531         p_port = netisr_cpuport(
532             ifsq_get_cpuid(ifq_get_subq_default(&ifp_p->if_snd)));
533         for (;;) {
534                 struct netmsg_packet *nmp;
535
536                 m = ifsq_dequeue(ifsq);
537                 if (m == NULL)
538                         break;
539                 BPF_MTAP(ifp, m);
540
541                 /*
542                  * Do not run parent's if_start() if the parent is not up,
543                  * or parent's driver will cause a system crash.
544                  */
545                 if ((ifp_p->if_flags & (IFF_UP | IFF_RUNNING)) !=
546                     (IFF_UP | IFF_RUNNING)) {
547                         m_freem(m);
548                         IFNET_STAT_INC(ifp, collisions, 1);
549                         continue;
550                 }
551
552                 /*
553                  * We need some way to tell the interface where the packet
554                  * came from so that it knows how to find the VLAN tag to
555                  * use, so we set the ether_vlantag in the mbuf packet header
556                  * to our vlan tag.  We also set the M_VLANTAG flag in the
557                  * mbuf to let the parent driver know that the ether_vlantag
558                  * is really valid.
559                  */
560                 m->m_pkthdr.ether_vlantag = ifv->ifv_tag;
561                 m->m_flags |= M_VLANTAG;
562
563                 nmp = &m->m_hdr.mh_netmsg;
564
565                 netmsg_init(&nmp->base, NULL, &netisr_apanic_rport,
566                             0, vlan_start_dispatch);
567                 nmp->nm_packet = m;
568                 nmp->base.lmsg.u.ms_resultp = ifp_p;
569
570                 lwkt_sendmsg(p_port, &nmp->base.lmsg);
571                 IFNET_STAT_INC(ifp, opackets, 1);
572         }
573 }
574
575 static void
576 vlan_input(struct mbuf *m)
577 {
578         struct ifvlan *ifv = NULL;
579         struct ifnet *rcvif;
580         struct vlan_trunk *vlantrunks;
581         struct vlan_entry *entry;
582
583         rcvif = m->m_pkthdr.rcvif;
584         KKASSERT(m->m_flags & M_VLANTAG);
585
586         vlantrunks = rcvif->if_vlantrunks;
587         if (vlantrunks == NULL) {
588                 IFNET_STAT_INC(rcvif, noproto, 1);
589                 m_freem(m);
590                 return;
591         }
592
593         crit_enter();   /* XXX Necessary? */
594         LIST_FOREACH(entry, &vlantrunks[mycpuid].vlan_list, ifv_link) {
595                 if (entry->ifv->ifv_tag ==
596                     EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag)) {
597                         ifv = entry->ifv;
598                         break;
599                 }
600         }
601         crit_exit();
602
603         /*
604          * Packet is discarded if:
605          * - no corresponding vlan(4) interface
606          * - vlan(4) interface has not been completely set up yet,
607          *   or is being destroyed (ifv->ifv_p != rcvif)
608          */
609         if (ifv == NULL || ifv->ifv_p != rcvif) {
610                 IFNET_STAT_INC(rcvif, noproto, 1);
611                 m_freem(m);
612                 return;
613         }
614
615         /*
616          * Clear M_VLANTAG, before the packet is handed to
617          * vlan(4) interface
618          */
619         m->m_flags &= ~M_VLANTAG;
620
621         ether_reinput_oncpu(&ifv->ifv_if, m, REINPUT_RUNBPF);
622 }
623
624 static void
625 vlan_link_dispatch(netmsg_t msg)
626 {
627         struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
628         struct ifvlan *ifv = vmsg->nv_ifv;
629         struct ifnet *ifp_p = vmsg->nv_ifp_p;
630         struct vlan_entry *entry;
631         struct vlan_trunk *vlantrunks, *trunk;
632         int cpu = mycpuid;
633
634         vlantrunks = ifp_p->if_vlantrunks;
635         KASSERT(vlantrunks != NULL,
636                 ("vlan trunk has not been initialized yet"));
637
638         entry = &ifv->ifv_entries[cpu];
639         trunk = &vlantrunks[cpu];
640
641         crit_enter();
642         LIST_INSERT_HEAD(&trunk->vlan_list, entry, ifv_link);
643         crit_exit();
644
645         ifnet_forwardmsg(&vmsg->base.lmsg, cpu + 1);
646 }
647
648 static void
649 vlan_link(struct ifvlan *ifv, struct ifnet *ifp_p)
650 {
651         struct netmsg_vlan vmsg;
652
653         /* Assert in netisr0 */
654         ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
655
656         if (ifp_p->if_vlantrunks == NULL) {
657                 struct vlan_trunk *vlantrunks;
658                 int i;
659
660                 vlantrunks = kmalloc(sizeof(*vlantrunks) * ncpus, M_VLAN,
661                                      M_WAITOK | M_ZERO);
662                 for (i = 0; i < ncpus; ++i)
663                         LIST_INIT(&vlantrunks[i].vlan_list);
664
665                 ifp_p->if_vlantrunks = vlantrunks;
666         }
667
668         bzero(&vmsg, sizeof(vmsg));
669
670         netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
671                     0, vlan_link_dispatch);
672         vmsg.nv_ifv = ifv;
673         vmsg.nv_ifp_p = ifp_p;
674
675         ifnet_domsg(&vmsg.base.lmsg, 0);
676 }
677
678 static void
679 vlan_config_dispatch(netmsg_t msg)
680 {
681         struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
682         struct ifvlan *ifv;
683         struct ifnet *ifp_p, *ifp;
684         struct sockaddr_dl *sdl1, *sdl2;
685         int error;
686
687         /* Assert in netisr0 */
688
689         ifp_p = ifunit(vmsg->nv_parent_name);
690         if (ifp_p == NULL) {
691                 error = ENOENT;
692                 goto reply;
693         }
694
695         if (ifp_p->if_data.ifi_type != IFT_ETHER) {
696                 error = EPROTONOSUPPORT;
697                 goto reply;
698         }
699
700         ifv = vmsg->nv_ifv;
701         ifp = &ifv->ifv_if;
702
703         if (ifv->ifv_p) {
704                 error = EBUSY;
705                 goto reply;
706         }
707
708         /* Link vlan into parent's vlantrunk */
709         vlan_link(ifv, ifp_p);
710
711         ifnet_serialize_all(ifp);
712
713         ifv->ifv_tag = vmsg->nv_vlantag;
714         if (ifp_p->if_capenable & IFCAP_VLAN_MTU)
715                 ifp->if_mtu = ifp_p->if_mtu;
716         else
717                 ifp->if_mtu = ifp_p->if_data.ifi_mtu - EVL_ENCAPLEN;
718
719         /*
720          * Copy only a selected subset of flags from the parent.
721          * Other flags are none of our business.
722          */
723 #define VLAN_INHERIT_FLAGS      (IFF_BROADCAST | IFF_MULTICAST | \
724                                  IFF_SIMPLEX | IFF_POINTOPOINT)
725
726         ifp->if_flags &= ~VLAN_INHERIT_FLAGS;
727         ifp->if_flags |= (ifp_p->if_flags & VLAN_INHERIT_FLAGS);
728
729 #undef VLAN_INHERIT_FLAGS
730
731         /*
732          * Set up our ``Ethernet address'' to reflect the underlying
733          * physical interface's.
734          */
735         sdl1 = IF_LLSOCKADDR(ifp);
736         sdl2 = IF_LLSOCKADDR(ifp_p);
737         sdl1->sdl_type = IFT_ETHER;
738         sdl1->sdl_alen = ETHER_ADDR_LEN;
739         bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
740         bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
741
742         /*
743          * Release vlan's serializer before reprogramming parent's
744          * multicast filter to avoid possible dead lock.
745          */
746         ifnet_deserialize_all(ifp);
747
748         /*
749          * Configure multicast addresses that may already be
750          * joined on the vlan device.
751          */
752         vlan_setmulti(ifv, ifp_p);
753
754         /*
755          * Set flags on the parent, if necessary.
756          */
757         vlan_setflags(ifv, ifp_p, 1);
758
759         /*
760          * Connect to parent after everything have been set up,
761          * so input/output could know that vlan is ready to go
762          */
763         ifv->ifv_p = ifp_p;
764         error = 0;
765 reply:
766         lwkt_replymsg(&vmsg->base.lmsg, error);
767 }
768
769 static int
770 vlan_config(struct ifvlan *ifv, const char *parent_name, uint16_t vlantag)
771 {
772         struct netmsg_vlan vmsg;
773
774         ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
775
776         bzero(&vmsg, sizeof(vmsg));
777
778         netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
779                     0, vlan_config_dispatch);
780         vmsg.nv_ifv = ifv;
781         vmsg.nv_parent_name = parent_name;
782         vmsg.nv_vlantag = vlantag;
783
784         return lwkt_domsg(netisr_cpuport(0), &vmsg.base.lmsg, 0);
785 }
786
787 static void
788 vlan_unlink_dispatch(netmsg_t msg)
789 {
790         struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
791         struct ifvlan *ifv = vmsg->nv_ifv;
792         struct vlan_entry *entry;
793         int cpu = mycpuid;
794
795         KASSERT(vmsg->nv_ifp_p->if_vlantrunks != NULL,
796                 ("vlan trunk has not been initialized yet"));
797         entry = &ifv->ifv_entries[cpu];
798
799         crit_enter();
800         LIST_REMOVE(entry, ifv_link);
801         crit_exit();
802
803         ifnet_forwardmsg(&vmsg->base.lmsg, cpu + 1);
804 }
805
806 static void
807 vlan_unlink(struct ifvlan *ifv, struct ifnet *ifp_p)
808 {
809         struct vlan_trunk *vlantrunks = ifp_p->if_vlantrunks;
810         struct netmsg_vlan vmsg;
811
812         /* Assert in netisr0 */
813         ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
814
815         KASSERT(ifp_p->if_vlantrunks != NULL,
816                 ("vlan trunk has not been initialized yet"));
817
818         bzero(&vmsg, sizeof(vmsg));
819
820         netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
821                     0, vlan_unlink_dispatch);
822         vmsg.nv_ifv = ifv;
823         vmsg.nv_ifp_p = ifp_p;
824
825         ifnet_domsg(&vmsg.base.lmsg, 0);
826
827         crit_enter();
828         if (LIST_EMPTY(&vlantrunks[mycpuid].vlan_list)) {
829                 ifp_p->if_vlantrunks = NULL;
830
831                 /*
832                  * Make sure that all protocol threads see if_vlantrunks change.
833                  */
834                 netmsg_service_sync();
835                 kfree(vlantrunks, M_VLAN);
836         }
837         crit_exit();
838 }
839
840 static void
841 vlan_unconfig_dispatch(netmsg_t msg)
842 {
843         struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
844         struct sockaddr_dl *sdl;
845         struct ifvlan *ifv;
846         struct ifnet *ifp_p, *ifp;
847         int error;
848
849         /* Assert in netisr0 */
850
851         ifv = vmsg->nv_ifv;
852         ifp = &ifv->ifv_if;
853
854         if (ifp->if_flags & IFF_UP)
855                 if_down(ifp);
856
857         ifnet_serialize_all(ifp);
858
859         ifp->if_flags &= ~IFF_RUNNING;
860
861         /*
862          * Save parent ifnet pointer and disconnect from parent.
863          *
864          * This is done early in this function, so input/output could
865          * know that we are disconnecting.
866          */
867         ifp_p = ifv->ifv_p;
868         ifv->ifv_p = NULL;
869
870         /*
871          * Release vlan's serializer before reprogramming parent's
872          * multicast filter to avoid possible dead lock.
873          */
874         ifnet_deserialize_all(ifp);
875
876         if (ifp_p) {
877                 /*
878                  * Since the interface is being unconfigured, we need to
879                  * empty the list of multicast groups that we may have joined
880                  * while we were alive from the parent's list.
881                  */
882                 vlan_clrmulti(ifv, ifp_p);
883
884                 /* Clear parent's flags which was set by us. */
885                 vlan_setflags(ifv, ifp_p, 0);
886         }
887
888         ifnet_serialize_all(ifp);
889
890         ifp->if_mtu = ETHERMTU;
891
892         /* Clear our MAC address. */
893         sdl = IF_LLSOCKADDR(ifp);
894         sdl->sdl_type = IFT_ETHER;
895         sdl->sdl_alen = ETHER_ADDR_LEN;
896         bzero(LLADDR(sdl), ETHER_ADDR_LEN);
897         bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
898
899         ifnet_deserialize_all(ifp);
900
901         /* Unlink vlan from parent's vlantrunk */
902         if (ifp_p != NULL && ifp_p->if_vlantrunks != NULL)
903                 vlan_unlink(ifv, ifp_p);
904
905         error = 0;
906         lwkt_replymsg(&vmsg->base.lmsg, error);
907 }
908
909 static int
910 vlan_unconfig(struct ifvlan *ifv)
911 {
912         struct netmsg_vlan vmsg;
913
914         ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
915
916         bzero(&vmsg, sizeof(vmsg));
917
918         netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
919                     0, vlan_unconfig_dispatch);
920         vmsg.nv_ifv = ifv;
921
922         return lwkt_domsg(netisr_cpuport(0), &vmsg.base.lmsg, 0);
923 }
924
925 static int
926 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
927 {
928         struct ifvlan *ifv = ifp->if_softc;
929         struct ifreq *ifr = (struct ifreq *)data;
930         struct ifnet *ifp_p;
931         struct vlanreq vlr;
932         int error = 0;
933
934         ASSERT_IFNET_SERIALIZED_ALL(ifp);
935
936         switch (cmd) {
937         case SIOCGIFMEDIA:
938                 ifp_p = ifv->ifv_p;
939                 if (ifp_p != NULL) {
940                         /*
941                          * Release vlan interface's serializer to void
942                          * possible dead lock.
943                          */
944                         ifnet_deserialize_all(ifp);
945
946                         ifnet_serialize_all(ifp_p);
947                         error = ifp_p->if_ioctl(ifp_p, SIOCGIFMEDIA, data, cr);
948                         ifnet_deserialize_all(ifp_p);
949
950                         ifnet_serialize_all(ifp);
951
952                         if (ifv->ifv_p == NULL || ifv->ifv_p != ifp_p) {
953                                 /*
954                                  * We are disconnected from the original
955                                  * parent interface or the parent interface
956                                  * is changed, after vlan interface's
957                                  * serializer is released.
958                                  */
959                                 error = EINVAL;
960                         }
961
962                         /* Limit the result to the parent's current config. */
963                         if (error == 0) {
964                                 struct ifmediareq *ifmr;
965
966                                 ifmr = (struct ifmediareq *) data;
967                                 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
968                                         ifmr->ifm_count = 1;
969                                         error = copyout(&ifmr->ifm_current,
970                                                 ifmr->ifm_ulist, 
971                                                 sizeof(int));
972                                 }
973                         }
974                 } else {
975                         error = EINVAL;
976                 }
977                 break;
978
979         case SIOCSIFMEDIA:
980                 error = EINVAL;
981                 break;
982
983         case SIOCSETVLAN:
984                 error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
985                 if (error)
986                         break;
987
988                 ifnet_deserialize_all(ifp);
989                 if (vlr.vlr_parent[0] == '\0')
990                         error = vlan_unconfig(ifv);
991                 else
992                         error = vlan_config(ifv, vlr.vlr_parent, vlr.vlr_tag);
993                 ifnet_serialize_all(ifp);
994                 break;
995
996         case SIOCGETVLAN:
997                 bzero(&vlr, sizeof(vlr));
998                 if (ifv->ifv_p) {
999                         strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
1000                             sizeof(vlr.vlr_parent));
1001                         vlr.vlr_tag = ifv->ifv_tag;
1002                 }
1003                 error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
1004                 break;
1005
1006         case SIOCSIFFLAGS:
1007                 if (ifp->if_flags & IFF_UP)
1008                         ifp->if_init(ifp);
1009                 else
1010                         ifp->if_flags &= ~IFF_RUNNING;
1011
1012                 /*
1013                  * We should propagate selected flags to the parent,
1014                  * e.g., promiscuous mode.
1015                  */
1016                 ifnet_deserialize_all(ifp);
1017                 error = vlan_config_flags(ifv);
1018                 ifnet_serialize_all(ifp);
1019                 break;
1020
1021         case SIOCADDMULTI:
1022         case SIOCDELMULTI:
1023                 ifnet_deserialize_all(ifp);
1024                 error = vlan_config_multi(ifv);
1025                 ifnet_serialize_all(ifp);
1026                 break;
1027
1028         default:
1029                 error = ether_ioctl(ifp, cmd, data);
1030                 break;
1031         }
1032         return error;
1033 }
1034
1035 static void
1036 vlan_multi_dispatch(netmsg_t msg)
1037 {
1038         struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
1039         struct ifvlan *ifv = vmsg->nv_ifv;
1040         int error = 0;
1041
1042         /*
1043          * If we don't have a parent, just remember the membership for
1044          * when we do.
1045          */
1046         if (ifv->ifv_p != NULL)
1047                 error = vlan_setmulti(ifv, ifv->ifv_p);
1048         lwkt_replymsg(&vmsg->base.lmsg, error);
1049 }
1050
1051 static int
1052 vlan_config_multi(struct ifvlan *ifv)
1053 {
1054         struct netmsg_vlan vmsg;
1055
1056         ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
1057
1058         bzero(&vmsg, sizeof(vmsg));
1059
1060         netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
1061                     0, vlan_multi_dispatch);
1062         vmsg.nv_ifv = ifv;
1063
1064         return lwkt_domsg(netisr_cpuport(0), &vmsg.base.lmsg, 0);
1065 }
1066
1067 static void
1068 vlan_flags_dispatch(netmsg_t msg)
1069 {
1070         struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
1071         struct ifvlan *ifv = vmsg->nv_ifv;
1072         int error = 0;
1073
1074         /*
1075          * If we don't have a parent, just remember the flags for
1076          * when we do.
1077          */
1078         if (ifv->ifv_p != NULL)
1079                 error = vlan_setflags(ifv, ifv->ifv_p, 1);
1080         lwkt_replymsg(&vmsg->base.lmsg, error);
1081 }
1082
1083 static int
1084 vlan_config_flags(struct ifvlan *ifv)
1085 {
1086         struct netmsg_vlan vmsg;
1087
1088         ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);
1089
1090         bzero(&vmsg, sizeof(vmsg));
1091
1092         netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
1093                     0, vlan_flags_dispatch);
1094         vmsg.nv_ifv = ifv;
1095
1096         return lwkt_domsg(netisr_cpuport(0), &vmsg.base.lmsg, 0);
1097 }