if_vlan.c revision 1.24.2.5 1 /* $NetBSD: if_vlan.c,v 1.24.2.5 2001/01/18 09:23:53 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
87 #include "opt_inet.h"
88 #include "bpfilter.h"
89
90 #include <sys/param.h>
91 #include <sys/kernel.h>
92 #include <sys/mbuf.h>
93 #include <sys/queue.h>
94 #include <sys/socket.h>
95 #include <sys/sockio.h>
96 #include <sys/systm.h>
97 #include <sys/proc.h>
98
99 #if NBPFILTER > 0
100 #include <net/bpf.h>
101 #endif
102 #include <net/if.h>
103 #include <net/if_dl.h>
104 #include <net/if_types.h>
105 #include <net/if_ether.h>
106 #include <net/if_vlanvar.h>
107
108 #ifdef INET
109 #include <netinet/in.h>
110 #include <netinet/if_inarp.h>
111 #endif
112
113 struct vlan_mc_entry {
114 LIST_ENTRY(vlan_mc_entry) mc_entries;
115 /*
116 * A key to identify this entry. The mc_addr below can't be
117 * used since multiple sockaddr may mapped into the same
118 * ether_multi (e.g., AF_UNSPEC).
119 */
120 union {
121 struct ether_multi *mcu_enm;
122 } mc_u;
123 struct sockaddr_storage mc_addr;
124 };
125
126 #define mc_enm mc_u.mcu_enm
127
128 struct ifvlan {
129 union {
130 struct ethercom ifvu_ec;
131 } ifv_u;
132 struct ifnet *ifv_p; /* parent interface of this vlan */
133 struct ifv_linkmib {
134 const struct vlan_multisw *ifvm_msw;
135 int ifvm_encaplen; /* encapsulation length */
136 int ifvm_mtufudge; /* MTU fudged by this much */
137 int ifvm_mintu; /* min transmission unit */
138 u_int16_t ifvm_proto; /* encapsulation ethertype */
139 u_int16_t ifvm_tag; /* tag to apply on packets */
140 } ifv_mib;
141 LIST_HEAD(__vlan_mchead, vlan_mc_entry) ifv_mc_listhead;
142 LIST_ENTRY(ifvlan) ifv_list;
143 int ifv_flags;
144 };
145
146 #define IFVF_PROMISC 0x01 /* promiscuous mode enabled */
147
148 #define ifv_ec ifv_u.ifvu_ec
149
150 #define ifv_if ifv_ec.ec_if
151
152 #define ifv_msw ifv_mib.ifvm_msw
153 #define ifv_encaplen ifv_mib.ifvm_encaplen
154 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
155 #define ifv_mintu ifv_mib.ifvm_mintu
156 #define ifv_tag ifv_mib.ifvm_tag
157
158 struct vlan_multisw {
159 int (*vmsw_addmulti)(struct ifvlan *, struct ifreq *);
160 int (*vmsw_delmulti)(struct ifvlan *, struct ifreq *);
161 void (*vmsw_purgemulti)(struct ifvlan *);
162 };
163
164 static int vlan_ether_addmulti(struct ifvlan *, struct ifreq *);
165 static int vlan_ether_delmulti(struct ifvlan *, struct ifreq *);
166 static void vlan_ether_purgemulti(struct ifvlan *);
167
168 const struct vlan_multisw vlan_ether_multisw = {
169 vlan_ether_addmulti,
170 vlan_ether_delmulti,
171 vlan_ether_purgemulti,
172 };
173
174 static int vlan_clone_create(struct if_clone *, int);
175 static void vlan_clone_destroy(struct ifnet *);
176 static int vlan_config(struct ifvlan *, struct ifnet *);
177 static int vlan_ioctl(struct ifnet *, u_long, caddr_t);
178 static void vlan_start(struct ifnet *);
179 static void vlan_unconfig(struct ifnet *);
180
181 void vlanattach(int);
182
183 /* XXX This should be a hash table with the tag as the basis of the key. */
184 static LIST_HEAD(, ifvlan) ifv_list;
185
186 struct if_clone vlan_cloner =
187 IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
188
189 void
190 vlanattach(int n)
191 {
192
193 LIST_INIT(&ifv_list);
194 if_clone_attach(&vlan_cloner);
195 }
196
197 static int
198 vlan_clone_create(struct if_clone *ifc, int unit)
199 {
200 struct ifvlan *ifv;
201 struct ifnet *ifp;
202 int s;
203
204 ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK);
205 memset(ifv, 0, sizeof(struct ifvlan));
206 ifp = &ifv->ifv_if;
207 LIST_INIT(&ifv->ifv_mc_listhead);
208
209 s = splnet();
210 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
211 splx(s);
212
213 sprintf(ifp->if_xname, "%s%d", ifc->ifc_name, unit);
214 ifp->if_softc = ifv;
215 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
216 ifp->if_start = vlan_start;
217 ifp->if_ioctl = vlan_ioctl;
218
219 if_attach(ifp);
220
221 return (0);
222 }
223
224 static void
225 vlan_clone_destroy(struct ifnet *ifp)
226 {
227 struct ifvlan *ifv = ifp->if_softc;
228 int s;
229
230 s = splnet();
231 LIST_REMOVE(ifv, ifv_list);
232 vlan_unconfig(ifp);
233 splx(s);
234
235 if_detach(ifp);
236 free(ifv, M_DEVBUF);
237 }
238
239 /*
240 * Configure a VLAN interface. Must be called at splnet().
241 */
242 static int
243 vlan_config(struct ifvlan *ifv, struct ifnet *p)
244 {
245 struct ifnet *ifp = &ifv->ifv_if;
246 int error;
247
248 if (ifv->ifv_p != NULL)
249 return (EBUSY);
250
251 switch (p->if_type) {
252 case IFT_ETHER:
253 {
254 struct ethercom *ec = (void *) p;
255
256 ifv->ifv_msw = &vlan_ether_multisw;
257 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
258 ifv->ifv_mintu = ETHERMIN;
259
260 /*
261 * If the parent supports the VLAN_MTU capability,
262 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
263 * enable it.
264 */
265 if (ec->ec_nvlans++ == 0 &&
266 (ec->ec_capabilities & ETHERCAP_VLAN_MTU) != 0) {
267 /*
268 * Enable Tx/Rx of VLAN-sized frames.
269 */
270 ec->ec_capenable |= ETHERCAP_VLAN_MTU;
271 if (p->if_flags & IFF_UP) {
272 struct ifreq ifr;
273
274 ifr.ifr_flags = p->if_flags;
275 error = (*p->if_ioctl)(p, SIOCSIFFLAGS,
276 (caddr_t) &ifr);
277 if (error) {
278 if (ec->ec_nvlans-- == 1)
279 ec->ec_capenable &=
280 ~ETHERCAP_VLAN_MTU;
281 return (error);
282 }
283 }
284 ifv->ifv_mtufudge = 0;
285 } else if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0) {
286 /*
287 * Fudge the MTU by the encapsulation size. This
288 * makes us incompatible with strictly compliant
289 * 802.1Q implementations, but allows us to use
290 * the feature with other NetBSD implementations,
291 * which might still be useful.
292 */
293 ifv->ifv_mtufudge = ifv->ifv_encaplen;
294 }
295
296 /*
297 * We inherit the parent's Ethernet address.
298 */
299 ether_ifattach(ifp, LLADDR(p->if_sadl));
300 ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
301 break;
302 }
303
304 default:
305 return (EPROTONOSUPPORT);
306 }
307
308 ifv->ifv_p = p;
309 ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge;
310 ifv->ifv_if.if_flags = p->if_flags &
311 (IFF_UP | IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
312
313 /*
314 * Inherit the if_type from the parent. This allows us
315 * to participate in bridges of that type.
316 */
317 ifv->ifv_if.if_type = p->if_type;
318
319 return (0);
320 }
321
322 /*
323 * Unconfigure a VLAN interface. Must be called at splnet().
324 */
325 static void
326 vlan_unconfig(struct ifnet *ifp)
327 {
328 struct ifvlan *ifv = ifp->if_softc;
329
330 if (ifv->ifv_p == NULL)
331 return;
332
333 /*
334 * Since the interface is being unconfigured, we need to empty the
335 * list of multicast groups that we may have joined while we were
336 * alive and remove them from the parent's list also.
337 */
338 (*ifv->ifv_msw->vmsw_purgemulti)(ifv);
339
340 /* Disconnect from parent. */
341 switch (ifv->ifv_p->if_type) {
342 case IFT_ETHER:
343 {
344 struct ethercom *ec = (void *) ifv->ifv_p;
345
346 if (ec->ec_nvlans-- == 1) {
347 /*
348 * Disable Tx/Rx of VLAN-sized frames.
349 */
350 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
351 if (ifv->ifv_p->if_flags & IFF_UP) {
352 struct ifreq ifr;
353
354 ifr.ifr_flags = ifv->ifv_p->if_flags;
355 (void) (*ifv->ifv_p->if_ioctl)(ifv->ifv_p,
356 SIOCSIFFLAGS, (caddr_t) &ifr);
357 }
358 }
359
360 ether_ifdetach(ifp);
361 break;
362 }
363
364 #ifdef DIAGNOSTIC
365 default:
366 panic("vlan_unconfig: impossible");
367 #endif
368 }
369
370 ifv->ifv_p = NULL;
371 ifv->ifv_if.if_mtu = 0;
372 ifv->ifv_flags = 0;
373
374 if_down(ifp);
375 ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
376 }
377
378 /*
379 * Called when a parent interface is detaching; destroy any VLAN
380 * configuration for the parent interface.
381 */
382 void
383 vlan_ifdetach(struct ifnet *p)
384 {
385 struct ifvlan *ifv;
386 int s;
387
388 s = splnet();
389
390 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
391 ifv = LIST_NEXT(ifv, ifv_list)) {
392 if (ifv->ifv_p == p)
393 vlan_unconfig(&ifv->ifv_if);
394 }
395
396 splx(s);
397 }
398
399 static int
400 vlan_set_promisc(struct ifnet *ifp)
401 {
402 struct ifvlan *ifv = ifp->if_softc;
403 int error = 0;
404
405 if ((ifp->if_flags & IFF_PROMISC) != 0) {
406 if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
407 error = ifpromisc(ifv->ifv_p, 1);
408 if (error == 0)
409 ifv->ifv_flags |= IFVF_PROMISC;
410 }
411 } else {
412 if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
413 error = ifpromisc(ifv->ifv_p, 0);
414 if (error == 0)
415 ifv->ifv_flags &= ~IFVF_PROMISC;
416 }
417 }
418
419 return (error);
420 }
421
422 static int
423 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
424 {
425 struct proc *p = curproc; /* XXX */
426 struct ifvlan *ifv = ifp->if_softc;
427 struct ifaddr *ifa = (struct ifaddr *) data;
428 struct ifreq *ifr = (struct ifreq *) data;
429 struct ifnet *pr;
430 struct vlanreq vlr;
431 struct sockaddr *sa;
432 int s, error = 0;
433
434 s = splnet();
435
436 switch (cmd) {
437 case SIOCSIFADDR:
438 if (ifv->ifv_p != NULL) {
439 ifp->if_flags |= IFF_UP;
440
441 switch (ifa->ifa_addr->sa_family) {
442 #ifdef INET
443 case AF_INET:
444 arp_ifinit(ifp, ifa);
445 break;
446 #endif
447 default:
448 break;
449 }
450 } else {
451 error = EINVAL;
452 }
453 break;
454
455 case SIOCGIFADDR:
456 sa = (struct sockaddr *)&ifr->ifr_data;
457 memcpy(sa->sa_data, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
458 break;
459
460 case SIOCSIFMTU:
461 if (ifv->ifv_p != NULL) {
462 if (ifr->ifr_mtu >
463 (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
464 ifr->ifr_mtu <
465 (ifv->ifv_mintu - ifv->ifv_mtufudge))
466 error = EINVAL;
467 else
468 ifp->if_mtu = ifr->ifr_mtu;
469 } else
470 error = EINVAL;
471 break;
472
473 case SIOCSETVLAN:
474 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
475 break;
476 if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
477 break;
478 if (vlr.vlr_parent[0] == '\0') {
479 vlan_unconfig(ifp);
480 break;
481 }
482 if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
483 error = EINVAL; /* check for valid tag */
484 break;
485 }
486 if ((pr = ifunit(vlr.vlr_parent)) == 0) {
487 error = ENOENT;
488 break;
489 }
490 if ((error = vlan_config(ifv, pr)) != 0)
491 break;
492 ifv->ifv_tag = vlr.vlr_tag;
493 ifp->if_flags |= IFF_RUNNING;
494
495 /* Update promiscuous mode, if necessary. */
496 vlan_set_promisc(ifp);
497 break;
498
499 case SIOCGETVLAN:
500 memset(&vlr, 0, sizeof(vlr));
501 if (ifv->ifv_p != NULL) {
502 snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
503 ifv->ifv_p->if_xname);
504 vlr.vlr_tag = ifv->ifv_tag;
505 }
506 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
507 break;
508
509 case SIOCSIFFLAGS:
510 /*
511 * For promiscuous mode, we enable promiscuous mode on
512 * the parent if we need promiscuous on the VLAN interface.
513 */
514 if (ifv->ifv_p != NULL)
515 error = vlan_set_promisc(ifp);
516 break;
517
518 case SIOCADDMULTI:
519 error = (ifv->ifv_p != NULL) ?
520 (*ifv->ifv_msw->vmsw_addmulti)(ifv, ifr) : EINVAL;
521 break;
522
523 case SIOCDELMULTI:
524 error = (ifv->ifv_p != NULL) ?
525 (*ifv->ifv_msw->vmsw_delmulti)(ifv, ifr) : EINVAL;
526 break;
527
528 default:
529 error = EINVAL;
530 }
531
532 splx(s);
533
534 return (error);
535 }
536
537 static int
538 vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
539 {
540 struct vlan_mc_entry *mc;
541 u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
542 int error;
543
544 if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr_storage))
545 return (EINVAL);
546
547 error = ether_addmulti(ifr, &ifv->ifv_ec);
548 if (error != ENETRESET)
549 return (error);
550
551 /*
552 * This is new multicast address. We have to tell parent
553 * about it. Also, remember this multicast address so that
554 * we can delete them on unconfigure.
555 */
556 MALLOC(mc, struct vlan_mc_entry *, sizeof(struct vlan_mc_entry),
557 M_DEVBUF, M_NOWAIT);
558 if (mc == NULL) {
559 error = ENOMEM;
560 goto alloc_failed;
561 }
562
563 /*
564 * As ether_addmulti() returns ENETRESET, following two
565 * statement shouldn't fail.
566 */
567 (void)ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
568 ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, mc->mc_enm);
569 memcpy(&mc->mc_addr, &ifr->ifr_addr, ifr->ifr_addr.sa_len);
570 LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
571
572 error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCADDMULTI,
573 (caddr_t)ifr);
574 if (error != 0)
575 goto ioctl_failed;
576 return (error);
577
578 ioctl_failed:
579 LIST_REMOVE(mc, mc_entries);
580 FREE(mc, M_DEVBUF);
581 alloc_failed:
582 (void)ether_delmulti(ifr, &ifv->ifv_ec);
583 return (error);
584 }
585
586 static int
587 vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
588 {
589 struct ether_multi *enm;
590 struct vlan_mc_entry *mc;
591 u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
592 int error;
593
594 /*
595 * Find a key to lookup vlan_mc_entry. We have to do this
596 * before calling ether_delmulti for obvious reason.
597 */
598 if ((error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi)) != 0)
599 return (error);
600 ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, enm);
601
602 error = ether_delmulti(ifr, &ifv->ifv_ec);
603 if (error != ENETRESET)
604 return (error);
605
606 /* We no longer use this multicast address. Tell parent so. */
607 error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCDELMULTI,
608 (caddr_t)ifr);
609 if (error == 0) {
610 /* And forget about this address. */
611 for (mc = LIST_FIRST(&ifv->ifv_mc_listhead); mc != NULL;
612 mc = LIST_NEXT(mc, mc_entries)) {
613 if (mc->mc_enm == enm) {
614 LIST_REMOVE(mc, mc_entries);
615 FREE(mc, M_DEVBUF);
616 break;
617 }
618 }
619 KASSERT(mc != NULL);
620 } else
621 (void)ether_addmulti(ifr, &ifv->ifv_ec);
622 return (error);
623 }
624
625 /*
626 * Delete any multicast address we have asked to add form parent
627 * interface. Called when the vlan is being unconfigured.
628 */
629 static void
630 vlan_ether_purgemulti(struct ifvlan *ifv)
631 {
632 struct ifnet *ifp = ifv->ifv_p; /* Parent. */
633 struct vlan_mc_entry *mc;
634 union {
635 struct ifreq ifreq;
636 struct {
637 char ifr_name[IFNAMSIZ];
638 struct sockaddr_storage ifr_ss;
639 } ifreq_storage;
640 } ifreq;
641 struct ifreq *ifr = &ifreq.ifreq;
642
643 memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
644 while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
645 memcpy(&ifr->ifr_addr, &mc->mc_addr, mc->mc_addr.ss_len);
646 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)ifr);
647 LIST_REMOVE(mc, mc_entries);
648 FREE(mc, M_DEVBUF);
649 }
650 }
651
652 static void
653 vlan_start(struct ifnet *ifp)
654 {
655 struct ifvlan *ifv = ifp->if_softc;
656 struct ifnet *p = ifv->ifv_p;
657 struct ethercom *ec = (void *) ifv->ifv_p;
658 struct mbuf *m;
659
660 ifp->if_flags |= IFF_OACTIVE;
661
662 for (;;) {
663 IF_DEQUEUE(&ifp->if_snd, m);
664 if (m == NULL)
665 break;
666
667 #if NBPFILTER > 0
668 if (ifp->if_bpf)
669 bpf_mtap(ifp->if_bpf, m);
670 #endif
671 /*
672 * If the parent can insert the tag itself, just mark
673 * the tag in the mbuf header.
674 */
675 if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
676 struct mbuf *n;
677 n = m_aux_add(m, AF_LINK, ETHERTYPE_VLAN);
678 if (n == NULL) {
679 ifp->if_oerrors++;
680 m_freem(m);
681 continue;
682 }
683 *mtod(n, int *) = ifv->ifv_tag;
684 n->m_len = sizeof(int);
685 } else {
686 /*
687 * insert the tag ourselve
688 */
689 M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
690 if (m == NULL) {
691 printf("%s: unable to prepend encap header",
692 ifv->ifv_p->if_xname);
693 ifp->if_oerrors++;
694 continue;
695 }
696
697 switch (p->if_type) {
698 case IFT_ETHER:
699 {
700 struct ether_vlan_header *evl;
701
702 if (m->m_len < sizeof(struct ether_vlan_header))
703 m = m_pullup(m,
704 sizeof(struct ether_vlan_header));
705 if (m == NULL) {
706 printf("%s: unable to pullup encap "
707 "header", ifv->ifv_p->if_xname);
708 ifp->if_oerrors++;
709 continue;
710 }
711
712 /*
713 * Transform the Ethernet header into an
714 * Ethernet header with 802.1Q encapsulation.
715 */
716 memmove(mtod(m, caddr_t),
717 mtod(m, caddr_t) + ifv->ifv_encaplen,
718 sizeof(struct ether_header));
719 evl = mtod(m, struct ether_vlan_header *);
720 evl->evl_proto = evl->evl_encap_proto;
721 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
722 evl->evl_tag = htons(ifv->ifv_tag);
723 break;
724 }
725
726 #ifdef DIAGNOSTIC
727 default:
728 panic("vlan_start: impossible");
729 #endif
730 }
731 }
732
733 /*
734 * Send it, precisely as the parent's output routine
735 * would have. We are already running at splimp.
736 */
737 if (IF_QFULL(&p->if_snd)) {
738 IF_DROP(&p->if_snd);
739 /* XXX stats */
740 ifp->if_oerrors++;
741 m_freem(m);
742 continue;
743 }
744
745 IF_ENQUEUE(&p->if_snd, m);
746 ifp->if_opackets++;
747 if ((p->if_flags & IFF_OACTIVE) == 0) {
748 (*p->if_start)(p);
749 }
750 }
751
752 ifp->if_flags &= ~IFF_OACTIVE;
753 }
754
755 /*
756 * Given an Ethernet frame, find a valid vlan interface corresponding to the
757 * given source interface and tag, then run the the real packet through
758 * the parent's input routine.
759 */
760 void
761 vlan_input(struct ifnet *ifp, struct mbuf *m)
762 {
763 struct ifvlan *ifv;
764 u_int tag;
765 struct mbuf *n;
766
767 n = m_aux_find(m, AF_LINK, ETHERTYPE_VLAN);
768 if (n) {
769 /* m contains a normal ethernet frame, the tag is in m_aux */
770 tag = *mtod(n, int *);
771 m_aux_delete(m, n);
772 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
773 ifv = LIST_NEXT(ifv, ifv_list))
774 if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
775 break;
776 } else {
777 switch (ifp->if_type) {
778 case IFT_ETHER:
779 {
780 struct ether_vlan_header *evl;
781
782 if (m->m_len < sizeof(struct ether_vlan_header) &&
783 (m = m_pullup(m,
784 sizeof(struct ether_vlan_header))) == NULL) {
785 printf("%s: no memory for VLAN header, "
786 "dropping packet.\n", ifp->if_xname);
787 return;
788 }
789 evl = mtod(m, struct ether_vlan_header *);
790 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
791
792 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
793
794 /*
795 * Restore the original ethertype. We'll remove
796 * the encapsulation after we've found the vlan
797 * interface corresponding to the tag.
798 */
799 evl->evl_encap_proto = evl->evl_proto;
800 break;
801 }
802
803 default:
804 tag = (u_int) -1; /* XXX GCC */
805 #ifdef DIAGNOSTIC
806 panic("vlan_input: impossible");
807 #endif
808 }
809
810 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
811 ifv = LIST_NEXT(ifv, ifv_list))
812 if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
813 break;
814
815
816 /*
817 * Now, remove the encapsulation header. The original
818 * header has already been fixed up above.
819 */
820 if (ifv) {
821 memmove(mtod(m, caddr_t) + ifv->ifv_encaplen,
822 mtod(m, caddr_t), sizeof(struct ether_header));
823 m_adj(m, ifv->ifv_encaplen);
824 }
825 }
826
827 if (ifv == NULL ||
828 (ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
829 (IFF_UP|IFF_RUNNING)) {
830 m_free(m);
831 ifp->if_noproto++;
832 return;
833 }
834 m->m_pkthdr.rcvif = &ifv->ifv_if;
835 ifv->ifv_if.if_ipackets++;
836
837 #if NBPFILTER > 0
838 if (ifv->ifv_if.if_bpf)
839 bpf_mtap(ifv->ifv_if.if_bpf, m);
840 #endif
841
842 /* Pass it back through the parent's input routine. */
843 (*ifp->if_input)(&ifv->ifv_if, m);
844 }
845