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