if_vlan.c revision 1.16 1 /* $NetBSD: if_vlan.c,v 1.16 2000/10/15 11:58:26 bouyer 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_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_detach(ifp);
240 free(ifv, M_DEVBUF);
241 }
242
243 /*
244 * Configure a VLAN interface. Must be called at splnet().
245 */
246 static int
247 vlan_config(struct ifvlan *ifv, struct ifnet *p)
248 {
249 struct ifnet *ifp = &ifv->ifv_if;
250 int error;
251
252 if (ifv->ifv_p != NULL)
253 return (EBUSY);
254
255 switch (p->if_type) {
256 case IFT_ETHER:
257 {
258 struct ethercom *ec = (void *) p;
259
260 ifv->ifv_msw = &vlan_ether_multisw;
261 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
262 ifv->ifv_mintu = ETHERMIN;
263
264 /*
265 * If the parent supports the VLAN_MTU capability,
266 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
267 * enable it.
268 */
269 if (ec->ec_nvlans++ == 0 &&
270 (ec->ec_capabilities & ETHERCAP_VLAN_MTU) != 0) {
271 /*
272 * Enable Tx/Rx of VLAN-sized frames.
273 */
274 ec->ec_capenable |= ETHERCAP_VLAN_MTU;
275 if (p->if_flags & IFF_UP) {
276 struct ifreq ifr;
277
278 ifr.ifr_flags = p->if_flags;
279 error = (*p->if_ioctl)(p, SIOCSIFFLAGS,
280 (caddr_t) &ifr);
281 if (error) {
282 if (ec->ec_nvlans-- == 1)
283 ec->ec_capenable &=
284 ~ETHERCAP_VLAN_MTU;
285 return (error);
286 }
287 }
288 ifv->ifv_mtufudge = 0;
289 } else if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0) {
290 /*
291 * Fudge the MTU by the encapsulation size. This
292 * makes us incompatible with strictly compliant
293 * 802.1Q implementations, but allows us to use
294 * the feature with other NetBSD implementations,
295 * which might still be useful.
296 */
297 ifv->ifv_mtufudge = ifv->ifv_encaplen;
298 }
299
300 /*
301 * We inherit the parent's Ethernet address.
302 */
303 ether_ifattach(ifp, LLADDR(p->if_sadl));
304 ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
305 #if NBPFILTER > 0
306 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
307 sizeof(struct ether_header));
308 #endif
309 break;
310 }
311
312 default:
313 return (EPROTONOSUPPORT);
314 }
315
316 ifv->ifv_p = p;
317 ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge;
318 ifv->ifv_if.if_flags = p->if_flags;
319
320 /*
321 * Inherit the if_type from the parent. This allows us
322 * to participate in bridges of that type.
323 */
324 ifv->ifv_if.if_type = p->if_type;
325
326 return (0);
327 }
328
329 /*
330 * Unconfigure a VLAN interface. Must be called at splnet().
331 */
332 static void
333 vlan_unconfig(struct ifnet *ifp)
334 {
335 struct ifvlan *ifv = ifp->if_softc;
336
337 if (ifv->ifv_p == NULL)
338 return;
339
340 /*
341 * Since the interface is being unconfigured, we need to empty the
342 * list of multicast groups that we may have joined while we were
343 * alive and remove them from the parent's list also.
344 */
345 (*ifv->ifv_msw->vmsw_purgemulti)(ifv);
346
347 /* Disconnect from parent. */
348 switch (ifv->ifv_p->if_type) {
349 case IFT_ETHER:
350 {
351 struct ethercom *ec = (void *) ifv->ifv_p;
352
353 if (ec->ec_nvlans-- == 1) {
354 /*
355 * Disable Tx/Rx of VLAN-sized frames.
356 */
357 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
358 if (ifv->ifv_p->if_flags & IFF_UP) {
359 struct ifreq ifr;
360
361 ifr.ifr_flags = ifv->ifv_p->if_flags;
362 (void) (*ifv->ifv_p->if_ioctl)(ifv->ifv_p,
363 SIOCSIFFLAGS, (caddr_t) &ifr);
364 }
365 }
366
367 #if NBPFILTER > 0
368 bpfdetach(ifp);
369 #endif
370 ether_ifdetach(ifp);
371 break;
372 }
373
374 #ifdef DIAGNOSTIC
375 default:
376 panic("vlan_unconfig: impossible");
377 #endif
378 }
379
380 ifv->ifv_p = NULL;
381 ifv->ifv_if.if_mtu = 0;
382
383 if_down(ifp);
384 ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
385 }
386
387 /*
388 * Called when a parent interface is detaching; destroy any VLAN
389 * configuration for the parent interface.
390 */
391 void
392 vlan_ifdetach(struct ifnet *p)
393 {
394 struct ifvlan *ifv;
395 int s;
396
397 s = splnet();
398
399 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
400 ifv = LIST_NEXT(ifv, ifv_list)) {
401 if (ifv->ifv_p == p)
402 vlan_unconfig(&ifv->ifv_if);
403 }
404
405 splx(s);
406 }
407
408 static int
409 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
410 {
411 struct proc *p = curproc; /* XXX */
412 struct ifvlan *ifv = ifp->if_softc;
413 struct ifaddr *ifa = (struct ifaddr *) data;
414 struct ifreq *ifr = (struct ifreq *) data;
415 struct ifnet *pr;
416 struct vlanreq vlr;
417 struct sockaddr *sa;
418 int s, error = 0;
419
420 s = splnet();
421
422 switch (cmd) {
423 case SIOCSIFADDR:
424 if (ifv->ifv_p != NULL) {
425 ifp->if_flags |= IFF_UP;
426
427 switch (ifa->ifa_addr->sa_family) {
428 #ifdef INET
429 case AF_INET:
430 arp_ifinit(ifp, ifa);
431 break;
432 #endif
433 default:
434 break;
435 }
436 } else {
437 error = EINVAL;
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 if (ifv->ifv_p != NULL) {
506 error = (*ifv->ifv_msw->vmsw_addmulti)(ifv, ifr);
507 } else {
508 error = EINVAL;
509 }
510 break;
511
512 case SIOCDELMULTI:
513 if (ifv->ifv_p != NULL) {
514 error = (*ifv->ifv_msw->vmsw_delmulti)(ifv, ifr);
515 } else {
516 error = EINVAL;
517 }
518 break;
519
520 default:
521 error = EINVAL;
522 }
523
524 splx(s);
525
526 return (error);
527 }
528
529 static int
530 vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
531 {
532 struct vlan_mc_entry *mc;
533 u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
534 int error;
535
536 if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr_storage))
537 return (EINVAL);
538
539 error = ether_addmulti(ifr, &ifv->ifv_ec);
540 if (error != ENETRESET)
541 return (error);
542
543 /*
544 * This is new multicast address. We have to tell parent
545 * about it. Also, remember this multicast address so that
546 * we can delete them on unconfigure.
547 */
548 MALLOC(mc, struct vlan_mc_entry *, sizeof(struct vlan_mc_entry),
549 M_DEVBUF, M_NOWAIT);
550 if (mc == NULL) {
551 error = ENOMEM;
552 goto alloc_failed;
553 }
554
555 /*
556 * As ether_addmulti() returns ENETRESET, following two
557 * statement shouldn't fail.
558 */
559 (void)ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
560 ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, mc->mc_enm);
561 memcpy(&mc->mc_addr, &ifr->ifr_addr, ifr->ifr_addr.sa_len);
562 LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
563
564 error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCADDMULTI,
565 (caddr_t)ifr);
566 if (error != 0)
567 goto ioctl_failed;
568 return (error);
569
570 ioctl_failed:
571 LIST_REMOVE(mc, mc_entries);
572 FREE(mc, M_DEVBUF);
573 alloc_failed:
574 (void)ether_delmulti(ifr, &ifv->ifv_ec);
575 return (error);
576 }
577
578 static int
579 vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
580 {
581 struct ether_multi *enm;
582 struct vlan_mc_entry *mc;
583 u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
584 int error;
585
586 /*
587 * Find a key to lookup vlan_mc_entry. We have to do this
588 * before calling ether_delmulti for obvious reason.
589 */
590 if ((error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi)) != 0)
591 return (error);
592 ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, enm);
593
594 error = ether_delmulti(ifr, &ifv->ifv_ec);
595 if (error != ENETRESET)
596 return (error);
597
598 /* We no longer use this multicast address. Tell parent so. */
599 error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCDELMULTI,
600 (caddr_t)ifr);
601 if (error == 0) {
602 /* And forget about this address. */
603 for (mc = LIST_FIRST(&ifv->ifv_mc_listhead); mc != NULL;
604 mc = LIST_NEXT(mc, mc_entries)) {
605 if (mc->mc_enm == enm) {
606 LIST_REMOVE(mc, mc_entries);
607 FREE(mc, M_DEVBUF);
608 break;
609 }
610 }
611 KASSERT(mc != NULL);
612 } else
613 (void)ether_addmulti(ifr, &ifv->ifv_ec);
614 return (error);
615 }
616
617 /*
618 * Delete any multicast address we have asked to add form parent
619 * interface. Called when the vlan is being unconfigured.
620 */
621 static void
622 vlan_ether_purgemulti(struct ifvlan *ifv)
623 {
624 struct ifnet *ifp = ifv->ifv_p; /* Parent. */
625 struct vlan_mc_entry *mc;
626 union {
627 struct ifreq ifreq;
628 struct {
629 char ifr_name[IFNAMSIZ];
630 struct sockaddr_storage;
631 } ifreq_storage;
632 } ifreq;
633 struct ifreq *ifr = &ifreq.ifreq;
634
635 memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
636 while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
637 memcpy(&ifr->ifr_addr, &mc->mc_addr, mc->mc_addr.ss_len);
638 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)ifr);
639 LIST_REMOVE(mc->mc_enm, enm_list);
640 free(mc->mc_enm, M_IFMADDR);
641 LIST_REMOVE(mc, mc_entries);
642 FREE(mc, M_DEVBUF);
643 }
644
645 KASSERT(LIST_FIRST(&ifv->ifv_ec.ec_multiaddrs) == NULL);
646 }
647
648 static void
649 vlan_start(struct ifnet *ifp)
650 {
651 struct ifvlan *ifv = ifp->if_softc;
652 struct ifnet *p = ifv->ifv_p;
653 struct mbuf *m;
654
655 ifp->if_flags |= IFF_OACTIVE;
656
657 for (;;) {
658 IF_DEQUEUE(&ifp->if_snd, m);
659 if (m == NULL)
660 break;
661
662 #if NBPFILTER > 0
663 if (ifp->if_bpf)
664 bpf_mtap(ifp->if_bpf, m);
665 #endif
666
667 /*
668 * XXX Should handle the case where the underlying hardware
669 * interface can do VLAN tag insertion itself.
670 */
671 M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
672 if (m == NULL) {
673 printf("%s: unable to prepend encap header",
674 ifv->ifv_p->if_xname);
675 ifp->if_oerrors++;
676 continue;
677 }
678
679 switch (p->if_type) {
680 case IFT_ETHER:
681 {
682 struct ether_vlan_header *evl;
683
684 if (m->m_len < sizeof(struct ether_vlan_header) &&
685 (m = m_pullup(m,
686 sizeof(struct ether_vlan_header))) == NULL) {
687 printf("%s: unable to pullup encap header",
688 ifv->ifv_p->if_xname);
689 ifp->if_oerrors++;
690 continue;
691 }
692
693 /*
694 * Transform the Ethernet header into an Ethernet
695 * header with 802.1Q encapsulation.
696 */
697 memmove(mtod(m, caddr_t),
698 mtod(m, caddr_t) + ifv->ifv_encaplen,
699 sizeof(struct ether_header));
700 evl = mtod(m, struct ether_vlan_header *);
701 evl->evl_proto = evl->evl_encap_proto;
702 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
703 evl->evl_tag = htons(ifv->ifv_tag);
704 break;
705 }
706
707 #ifdef DIAGNOSTIC
708 default:
709 panic("vlan_start: impossible");
710 #endif
711 }
712
713 /*
714 * Send it, precisely as the parent's output routine
715 * would have. We are already running at splimp.
716 */
717 if (IF_QFULL(&p->if_snd)) {
718 IF_DROP(&p->if_snd);
719 /* XXX stats */
720 ifp->if_oerrors++;
721 m_freem(m);
722 continue;
723 }
724
725 IF_ENQUEUE(&p->if_snd, m);
726 if ((p->if_flags & IFF_OACTIVE) == 0) {
727 (*p->if_start)(p);
728 ifp->if_opackets++;
729 }
730 }
731
732 ifp->if_flags &= ~IFF_OACTIVE;
733 }
734
735 /*
736 * Given an Ethernet frame, find a valid vlan interface corresponding to the
737 * given source interface and tag, then run the the real packet through
738 * the parent's input routine.
739 */
740 void
741 vlan_input(struct ifnet *ifp, struct mbuf *m)
742 {
743 struct ifvlan *ifv;
744 u_int tag;
745
746 switch (ifp->if_type) {
747 case IFT_ETHER:
748 {
749 struct ether_vlan_header *evl;
750
751 if (m->m_len < sizeof(struct ether_vlan_header) &&
752 (m = m_pullup(m,
753 sizeof(struct ether_vlan_header))) == NULL) {
754 printf("%s: no memory for VLAN header, "
755 "dropping packet.\n", ifp->if_xname);
756 return;
757 }
758 evl = mtod(m, struct ether_vlan_header *);
759 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
760
761 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
762
763 /*
764 * Restore the original ethertype. We'll remove
765 * the encapsulation after we've found the vlan
766 * interface corresponding to the tag.
767 */
768 evl->evl_encap_proto = evl->evl_proto;
769 break;
770 }
771
772 default:
773 tag = (u_int) -1; /* XXX GCC */
774 #ifdef DIAGNOSTIC
775 panic("vlan_input: impossible");
776 #endif
777 }
778
779 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
780 ifv = LIST_NEXT(ifv, ifv_list))
781 if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
782 break;
783
784 if (ifv == NULL ||
785 (ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
786 (IFF_UP|IFF_RUNNING)) {
787 m_free(m);
788 ifp->if_noproto++;
789 return;
790 }
791
792 /*
793 * Now, remove the encapsulation header. The original
794 * header has already been fixed up above.
795 */
796 memmove(mtod(m, caddr_t) + ifv->ifv_encaplen, mtod(m, caddr_t),
797 ifv->ifv_encaplen);
798 m_adj(m, ifv->ifv_encaplen);
799
800 m->m_pkthdr.rcvif = &ifv->ifv_if;
801 ifv->ifv_if.if_ipackets++;
802
803 #if NBPFILTER > 0
804 if (ifv->ifv_if.if_bpf)
805 bpf_mtap(ifv->ifv_if.if_bpf, m);
806 #endif
807
808 /* Pass it back through the parent's input routine. */
809 (*ifp->if_input)(&ifv->ifv_if, m);
810 }
811