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