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