if_vlan.c revision 1.13 1 /* $NetBSD: if_vlan.c,v 1.13 2000/10/04 06:20:05 enami Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran, and by Jason R. Thorpe of Zembu Labs, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright 1998 Massachusetts Institute of Technology
41 *
42 * Permission to use, copy, modify, and distribute this software and
43 * its documentation for any purpose and without fee is hereby
44 * granted, provided that both the above copyright notice and this
45 * permission notice appear in all copies, that both the above
46 * copyright notice and this permission notice appear in all
47 * supporting documentation, and that the name of M.I.T. not be used
48 * in advertising or publicity pertaining to distribution of the
49 * software without specific, written prior permission. M.I.T. makes
50 * no representations about the suitability of this software for any
51 * purpose. It is provided "as is" without express or implied
52 * warranty.
53 *
54 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
55 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
56 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
57 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
58 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
61 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
62 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
63 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
64 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * from FreeBSD: if_vlan.c,v 1.16 2000/03/26 15:21:40 charnier Exp
68 * via OpenBSD: if_vlan.c,v 1.4 2000/05/15 19:15:00 chris Exp
69 */
70
71 /*
72 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. Might be
73 * extended some day to also handle IEEE 802.1P priority tagging. This is
74 * sort of sneaky in the implementation, since we need to pretend to be
75 * enough of an Ethernet implementation to make ARP work. The way we do
76 * this is by telling everyone that we are an Ethernet interface, and then
77 * catch the packets that ether_output() left on our output queue when it
78 * calls if_start(), rewrite them for use by the real outgoing interface,
79 * and ask it to send them.
80 *
81 * TODO:
82 *
83 * - Need some way to notify vlan interfaces when the parent
84 * interface changes MTU.
85 *
86 * - Need a way to facilitate parent interfaces that can do
87 * tag insertion and/or extraction in hardware.
88 *
89 * - Need to make promiscuous mode work.
90 */
91
92 #include "opt_inet.h"
93 #include "bpfilter.h"
94
95 #include <sys/param.h>
96 #include <sys/kernel.h>
97 #include <sys/mbuf.h>
98 #include <sys/queue.h>
99 #include <sys/socket.h>
100 #include <sys/sockio.h>
101 #include <sys/systm.h>
102 #include <sys/proc.h>
103
104 #if NBPFILTER > 0
105 #include <net/bpf.h>
106 #endif
107 #include <net/if.h>
108 #include <net/if_dl.h>
109 #include <net/if_types.h>
110 #include <net/if_ether.h>
111 #include <net/if_vlanvar.h>
112
113 #ifdef INET
114 #include <netinet/in.h>
115 #include <netinet/if_inarp.h>
116 #endif
117
118 extern struct ifaddr **ifnet_addrs; /* XXX if.c */
119
120 struct vlan_mc_entry {
121 LIST_ENTRY(vlan_mc_entry) mc_entries;
122 /*
123 * A key to identify this entry. The mc_addr below can't be
124 * used since multiple sockaddr may mapped into the same
125 * ether_multi (e.g., AF_UNSPEC).
126 */
127 union {
128 struct ether_multi *mcu_enm;
129 } mc_u;
130 struct sockaddr_storage mc_addr;
131 };
132
133 #define mc_enm mc_u.mcu_enm
134
135 struct ifvlan {
136 union {
137 struct ethercom ifvu_ec;
138 } ifv_u;
139 struct ifnet *ifv_p; /* parent interface of this vlan */
140 struct ifv_linkmib {
141 const struct vlan_multisw *ifvm_msw;
142 int ifvm_encaplen; /* encapsulation length */
143 int ifvm_mtufudge; /* MTU fudged by this much */
144 int ifvm_mintu; /* min transmission unit */
145 u_int16_t ifvm_proto; /* encapsulation ethertype */
146 u_int16_t ifvm_tag; /* tag to apply on packets */
147 } ifv_mib;
148 LIST_HEAD(__vlan_mchead, vlan_mc_entry) ifv_mc_listhead;
149 LIST_ENTRY(ifvlan) ifv_list;
150 };
151
152 #define ifv_ec ifv_u.ifvu_ec
153
154 #define ifv_if ifv_ec.ec_if
155
156 #define ifv_msw ifv_mib.ifvm_msw
157 #define ifv_encaplen ifv_mib.ifvm_encaplen
158 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
159 #define ifv_mintu ifv_mib.ifvm_mintu
160 #define ifv_tag ifv_mib.ifvm_tag
161
162 struct vlan_multisw {
163 int (*vmsw_addmulti)(struct ifvlan *, struct ifreq *);
164 int (*vmsw_delmulti)(struct ifvlan *, struct ifreq *);
165 void (*vmsw_purgemulti)(struct ifvlan *);
166 };
167
168 static int vlan_ether_addmulti(struct ifvlan *, struct ifreq *);
169 static int vlan_ether_delmulti(struct ifvlan *, struct ifreq *);
170 static void vlan_ether_purgemulti(struct ifvlan *);
171
172 const struct vlan_multisw vlan_ether_multisw = {
173 vlan_ether_addmulti,
174 vlan_ether_delmulti,
175 vlan_ether_purgemulti,
176 };
177
178 static int vlan_clone_create(struct if_clone *, int);
179 static void vlan_clone_destroy(struct ifnet *);
180 static int vlan_config(struct ifvlan *, struct ifnet *);
181 static int vlan_ioctl(struct ifnet *, u_long, caddr_t);
182 static void vlan_start(struct ifnet *);
183 static void vlan_unconfig(struct ifnet *);
184
185 void vlanattach(int);
186
187 /* XXX This should be a hash table with the tag as the basis of the key. */
188 static LIST_HEAD(, ifvlan) ifv_list;
189
190 struct if_clone vlan_cloner =
191 IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
192
193 void
194 vlanattach(int n)
195 {
196
197 LIST_INIT(&ifv_list);
198 if_clone_attach(&vlan_cloner);
199 }
200
201 static int
202 vlan_clone_create(struct if_clone *ifc, int unit)
203 {
204 struct ifvlan *ifv;
205 struct ifnet *ifp;
206 int s;
207
208 ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK);
209 memset(ifv, 0, sizeof(struct ifvlan));
210 ifp = &ifv->ifv_ec.ec_if;
211 LIST_INIT(&ifv->ifv_mc_listhead);
212
213 s = splnet();
214 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
215 splx(s);
216
217 sprintf(ifp->if_xname, "%s%d", ifc->ifc_name, unit);
218 ifp->if_softc = ifv;
219 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
220 ifp->if_start = vlan_start;
221 ifp->if_ioctl = vlan_ioctl;
222
223 if_attach(ifp);
224
225 return (0);
226 }
227
228 static void
229 vlan_clone_destroy(struct ifnet *ifp)
230 {
231 struct ifvlan *ifv = ifp->if_softc;
232 int s;
233
234 s = splnet();
235 LIST_REMOVE(ifv, ifv_list);
236 vlan_unconfig(ifp);
237 splx(s);
238
239 #if NBPFILTER > 0
240 bpfdetach(ifp);
241 #endif
242 ether_ifdetach(ifp);
243 if_detach(ifp);
244 free(ifv, M_DEVBUF);
245 }
246
247 /*
248 * Configure a VLAN interface. Must be called at splnet().
249 */
250 static int
251 vlan_config(struct ifvlan *ifv, struct ifnet *p)
252 {
253 struct ifnet *ifp = &ifv->ifv_if;
254 int error;
255
256 if (ifv->ifv_p != NULL)
257 return (EBUSY);
258
259 switch (p->if_type) {
260 case IFT_ETHER:
261 {
262 struct ethercom *ec = (void *) p;
263
264 ifv->ifv_msw = &vlan_ether_multisw;
265 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
266 ifv->ifv_mintu = ETHERMIN;
267
268 /*
269 * If the parent supports the VLAN_MTU capability,
270 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
271 * enable it.
272 */
273 if (ec->ec_nvlans++ == 0 &&
274 (ec->ec_capabilities & ETHERCAP_VLAN_MTU) != 0) {
275 /*
276 * Enable Tx/Rx of VLAN-sized frames.
277 */
278 ec->ec_capenable |= ETHERCAP_VLAN_MTU;
279 if (p->if_flags & IFF_UP) {
280 struct ifreq ifr;
281
282 ifr.ifr_flags = p->if_flags;
283 error = (*p->if_ioctl)(p, SIOCSIFFLAGS,
284 (caddr_t) &ifr);
285 if (error) {
286 if (ec->ec_nvlans-- == 1)
287 ec->ec_capenable &=
288 ~ETHERCAP_VLAN_MTU;
289 return (error);
290 }
291 }
292 ifv->ifv_mtufudge = 0;
293 } else if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0) {
294 /*
295 * Fudge the MTU by the encapsulation size. This
296 * makes us incompatible with strictly compliant
297 * 802.1Q implementations, but allows us to use
298 * the feature with other NetBSD implementations,
299 * which might still be useful.
300 */
301 ifv->ifv_mtufudge = ifv->ifv_encaplen;
302 }
303
304 /*
305 * We inherit the parent's Ethernet address.
306 */
307 ether_ifattach(ifp, LLADDR(p->if_sadl));
308 ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
309 #if NBPFILTER > 0
310 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
311 sizeof(struct ether_header));
312 #endif
313 break;
314 }
315
316 default:
317 return (EPROTONOSUPPORT);
318 }
319
320 ifv->ifv_p = p;
321 ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge;
322 ifv->ifv_if.if_flags = p->if_flags;
323
324 /*
325 * Inherit the if_type from the parent. This allows us
326 * to participate in bridges of that type.
327 */
328 ifv->ifv_if.if_type = p->if_type;
329
330 return (0);
331 }
332
333 /*
334 * Unconfigure a VLAN interface. Must be called at splnet().
335 */
336 static void
337 vlan_unconfig(struct ifnet *ifp)
338 {
339 struct ifvlan *ifv = ifp->if_softc;
340
341 if (ifv->ifv_p == NULL)
342 return;
343
344 /*
345 * Since the interface is being unconfigured, we need to empty the
346 * list of multicast groups that we may have joined while we were
347 * alive and remove them from the parent's list also.
348 */
349 (*ifv->ifv_msw->vmsw_purgemulti)(ifv);
350
351 /* Disconnect from parent. */
352 switch (ifv->ifv_p->if_type) {
353 case IFT_ETHER:
354 {
355 struct ethercom *ec = (void *) ifv->ifv_p;
356
357 if (ec->ec_nvlans-- == 1) {
358 /*
359 * Disable Tx/Rx of VLAN-sized frames.
360 */
361 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
362 if (ifv->ifv_p->if_flags & IFF_UP) {
363 struct ifreq ifr;
364
365 ifr.ifr_flags = ifv->ifv_p->if_flags;
366 (void) (*ifv->ifv_p->if_ioctl)(ifv->ifv_p,
367 SIOCSIFFLAGS, (caddr_t) &ifr);
368 }
369 }
370
371 #if NBPFILTER > 0
372 bpfdetach(ifp);
373 #endif
374 ether_ifdetach(ifp);
375 break;
376 }
377
378 #ifdef DIAGNOSTIC
379 default:
380 panic("vlan_unconfig: impossible");
381 #endif
382 }
383
384 ifv->ifv_p = NULL;
385 ifv->ifv_if.if_mtu = 0;
386
387 if_down(ifp);
388 ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
389 }
390
391 /*
392 * Called when a parent interface is detaching; destroy any VLAN
393 * configuration for the parent interface.
394 */
395 void
396 vlan_ifdetach(struct ifnet *p)
397 {
398 struct ifvlan *ifv;
399 int s;
400
401 s = splnet();
402
403 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
404 ifv = LIST_NEXT(ifv, ifv_list)) {
405 if (ifv->ifv_p == p)
406 vlan_unconfig(&ifv->ifv_if);
407 }
408
409 splx(s);
410 }
411
412 static int
413 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
414 {
415 struct proc *p = curproc; /* XXX */
416 struct ifvlan *ifv = ifp->if_softc;
417 struct ifaddr *ifa = (struct ifaddr *) data;
418 struct ifreq *ifr = (struct ifreq *) data;
419 struct ifnet *pr;
420 struct vlanreq vlr;
421 struct sockaddr *sa;
422 int s, error = 0;
423
424 s = splnet();
425
426 switch (cmd) {
427 case SIOCSIFADDR:
428 ifp->if_flags |= IFF_UP;
429
430 switch (ifa->ifa_addr->sa_family) {
431 #ifdef INET
432 case AF_INET:
433 arp_ifinit(ifp, ifa);
434 break;
435 #endif
436 default:
437 break;
438 }
439 break;
440
441 case SIOCGIFADDR:
442 sa = (struct sockaddr *)&ifr->ifr_data;
443 memcpy(sa->sa_data, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
444 break;
445
446 case SIOCSIFMTU:
447 if (ifv->ifv_p != NULL) {
448 if (ifr->ifr_mtu >
449 (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
450 ifr->ifr_mtu <
451 (ifv->ifv_mintu - ifv->ifv_mtufudge))
452 error = EINVAL;
453 else
454 ifp->if_mtu = ifr->ifr_mtu;
455 } else
456 error = EINVAL;
457 break;
458
459 case SIOCSETVLAN:
460 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
461 break;
462 if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
463 break;
464 if (vlr.vlr_parent[0] == '\0') {
465 vlan_unconfig(ifp);
466 break;
467 }
468 if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
469 error = EINVAL; /* check for valid tag */
470 break;
471 }
472 if ((pr = ifunit(vlr.vlr_parent)) == 0) {
473 error = ENOENT;
474 break;
475 }
476 if ((error = vlan_config(ifv, pr)) != 0)
477 break;
478 ifv->ifv_tag = vlr.vlr_tag;
479 ifp->if_flags |= IFF_RUNNING;
480 break;
481
482 case SIOCGETVLAN:
483 memset(&vlr, 0, sizeof(vlr));
484 if (ifv->ifv_p != NULL) {
485 snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
486 ifv->ifv_p->if_xname);
487 vlr.vlr_tag = ifv->ifv_tag;
488 }
489 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
490 break;
491
492 case SIOCSIFFLAGS:
493 /*
494 * XXX We don't support promiscuous mode right now because
495 * it would require help from the underlying drivers, which
496 * hasn't been implemented.
497 */
498 if ((ifr->ifr_flags & IFF_PROMISC) != 0) {
499 ifp->if_flags &= ~(IFF_PROMISC);
500 error = EINVAL;
501 }
502 break;
503
504 case SIOCADDMULTI:
505 error = (*ifv->ifv_msw->vmsw_addmulti)(ifv, ifr);
506 break;
507
508 case SIOCDELMULTI:
509 error = (*ifv->ifv_msw->vmsw_delmulti)(ifv, ifr);
510 break;
511
512 default:
513 error = EINVAL;
514 }
515
516 splx(s);
517
518 return (error);
519 }
520
521 static int
522 vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
523 {
524 struct vlan_mc_entry *mc;
525 u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
526 int error;
527
528 if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr_storage))
529 return (EINVAL);
530
531 error = ether_addmulti(ifr, &ifv->ifv_ec);
532 if (error != ENETRESET)
533 return (error);
534
535 /*
536 * This is new multicast address. We have to tell parent
537 * about it. Also, remember this multicast address so that
538 * we can delete them on unconfigure.
539 */
540 MALLOC(mc, struct vlan_mc_entry *, sizeof(struct vlan_mc_entry),
541 M_DEVBUF, M_NOWAIT);
542 if (mc == NULL) {
543 error = ENOMEM;
544 goto alloc_failed;
545 }
546
547 /*
548 * As ether_addmulti() returns ENETRESET, following two
549 * statement shouldn't fail.
550 */
551 (void)ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
552 ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, mc->mc_enm);
553 memcpy(&mc->mc_addr, &ifr->ifr_addr, ifr->ifr_addr.sa_len);
554 LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
555
556 error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCADDMULTI,
557 (caddr_t)ifr);
558 if (error != 0)
559 goto ioctl_failed;
560 return (error);
561
562 ioctl_failed:
563 LIST_REMOVE(mc, mc_entries);
564 FREE(mc, M_DEVBUF);
565 alloc_failed:
566 (void)ether_delmulti(ifr, &ifv->ifv_ec);
567 return (error);
568 }
569
570 static int
571 vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
572 {
573 struct ether_multi *enm;
574 struct vlan_mc_entry *mc;
575 u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
576 int error;
577
578 /*
579 * Find a key to lookup vlan_mc_entry. We have to do this
580 * before calling ether_delmulti for obvious reason.
581 */
582 if ((error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi)) != 0)
583 return (error);
584 ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, enm);
585
586 error = ether_delmulti(ifr, &ifv->ifv_ec);
587 if (error != ENETRESET)
588 return (error);
589
590 /* We no longer use this multicast address. Tell parent so. */
591 error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCDELMULTI,
592 (caddr_t)ifr);
593 if (error == 0) {
594 /* And forget about this address. */
595 for (mc = LIST_FIRST(&ifv->ifv_mc_listhead); mc != NULL;
596 mc = LIST_NEXT(mc, mc_entries)) {
597 if (mc->mc_enm == enm) {
598 LIST_REMOVE(mc, mc_entries);
599 FREE(mc, M_DEVBUF);
600 break;
601 }
602 }
603 KASSERT(mc != NULL);
604 } else
605 (void)ether_addmulti(ifr, &ifv->ifv_ec);
606 return (error);
607 }
608
609 /*
610 * Delete any multicast address we have asked to add form parent
611 * interface. Called when the vlan is being unconfigured.
612 */
613 static void
614 vlan_ether_purgemulti(struct ifvlan *ifv)
615 {
616 struct ifnet *ifp = ifv->ifv_p; /* Parent. */
617 struct vlan_mc_entry *mc;
618 union {
619 struct ifreq ifreq;
620 struct {
621 char ifr_name[IFNAMSIZ];
622 struct sockaddr_storage;
623 } ifreq_storage;
624 } ifreq;
625 struct ifreq *ifr = &ifreq.ifreq;
626
627 memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
628 while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
629 memcpy(&ifr->ifr_addr, &mc->mc_addr, mc->mc_addr.ss_len);
630 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)ifr);
631 LIST_REMOVE(mc->mc_enm, enm_list);
632 free(mc->mc_enm, M_IFMADDR);
633 LIST_REMOVE(mc, mc_entries);
634 FREE(mc, M_DEVBUF);
635 }
636
637 KASSERT(LIST_FIRST(&ifv->ifv_ec.ec_multiaddrs) == NULL);
638 }
639
640 static void
641 vlan_start(struct ifnet *ifp)
642 {
643 struct ifvlan *ifv;
644 struct ifnet *p;
645 struct mbuf *m;
646
647 ifv = ifp->if_softc;
648 p = ifv->ifv_p;
649 ifp->if_flags |= IFF_OACTIVE;
650
651 for (;;) {
652 IF_DEQUEUE(&ifp->if_snd, m);
653 if (m == NULL)
654 break;
655
656 #if NBPFILTER > 0
657 if (ifp->if_bpf)
658 bpf_mtap(ifp->if_bpf, m);
659 #endif
660
661 /*
662 * XXX Should handle the case where the underlying hardware
663 * interface can do VLAN tag insertion itself.
664 */
665 M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
666 if (m == NULL) {
667 printf("%s: unable to prepend encap header",
668 ifv->ifv_p->if_xname);
669 ifp->if_oerrors++;
670 continue;
671 }
672
673 switch (p->if_type) {
674 case IFT_ETHER:
675 {
676 struct ether_vlan_header *evl;
677
678 if (m->m_len < sizeof(struct ether_vlan_header) &&
679 (m = m_pullup(m,
680 sizeof(struct ether_vlan_header))) == NULL) {
681 printf("%s: unable to pullup encap header",
682 ifv->ifv_p->if_xname);
683 ifp->if_oerrors++;
684 continue;
685 }
686
687 /*
688 * Transform the Ethernet header into an Ethernet
689 * header with 802.1Q encapsulation.
690 */
691 memmove(mtod(m, caddr_t),
692 mtod(m, caddr_t) + ifv->ifv_encaplen,
693 sizeof(struct ether_header));
694 evl = mtod(m, struct ether_vlan_header *);
695 evl->evl_proto = evl->evl_encap_proto;
696 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
697 evl->evl_tag = htons(ifv->ifv_tag);
698 break;
699 }
700
701 #ifdef DIAGNOSTIC
702 default:
703 panic("vlan_start: impossible");
704 #endif
705 }
706
707 /*
708 * Send it, precisely as the parent's output routine
709 * would have. We are already running at splimp.
710 */
711 if (IF_QFULL(&p->if_snd)) {
712 IF_DROP(&p->if_snd);
713 /* XXX stats */
714 ifp->if_oerrors++;
715 m_freem(m);
716 continue;
717 }
718
719 IF_ENQUEUE(&p->if_snd, m);
720 if ((p->if_flags & IFF_OACTIVE) == 0) {
721 p->if_start(p);
722 ifp->if_opackets++;
723 }
724 }
725
726 ifp->if_flags &= ~IFF_OACTIVE;
727 }
728
729 /*
730 * Given an Ethernet frame, find a valid vlan interface corresponding to the
731 * given source interface and tag, then run the the real packet through
732 * the parent's input routine.
733 */
734 void
735 vlan_input(struct ifnet *ifp, struct mbuf *m)
736 {
737 struct ifvlan *ifv;
738 u_int tag;
739
740 switch (ifp->if_type) {
741 case IFT_ETHER:
742 {
743 struct ether_vlan_header *evl;
744
745 if (m->m_len < sizeof(struct ether_vlan_header) &&
746 (m = m_pullup(m,
747 sizeof(struct ether_vlan_header))) == NULL) {
748 printf("%s: no memory for VLAN header, "
749 "dropping packet.\n", ifp->if_xname);
750 return;
751 }
752 evl = mtod(m, struct ether_vlan_header *);
753 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
754
755 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
756
757 /*
758 * Restore the original ethertype. We'll remove
759 * the encapsulation after we've found the vlan
760 * interface corresponding to the tag.
761 */
762 evl->evl_encap_proto = evl->evl_proto;
763 break;
764 }
765
766 default:
767 tag = (u_int) -1; /* XXX GCC */
768 #ifdef DIAGNOSTIC
769 panic("vlan_input: impossible");
770 #endif
771 }
772
773 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
774 ifv = LIST_NEXT(ifv, ifv_list))
775 if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
776 break;
777
778 if (ifv == NULL ||
779 (ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
780 (IFF_UP|IFF_RUNNING)) {
781 m_free(m);
782 ifp->if_data.ifi_noproto++;
783 return;
784 }
785
786 /*
787 * Now, remove the encapsulation header. The original
788 * header has already been fixed up above.
789 */
790 memmove(mtod(m, caddr_t) + ifv->ifv_encaplen, mtod(m, caddr_t),
791 ifv->ifv_encaplen);
792 m_adj(m, ifv->ifv_encaplen);
793
794 m->m_pkthdr.rcvif = &ifv->ifv_if;
795 ifv->ifv_if.if_ipackets++;
796
797 #if NBPFILTER > 0
798 if (ifv->ifv_if.if_bpf)
799 bpf_mtap(ifv->ifv_if.if_bpf, m);
800 #endif
801
802 /* Pass it back through the parent's input routine. */
803 (*ifp->if_input)(&ifv->ifv_if, m);
804 }
805