if_vlan.c revision 1.18 1 /* $NetBSD: if_vlan.c,v 1.18 2000/11/10 02:27:19 enami Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran, and by Jason R. Thorpe of Zembu Labs, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright 1998 Massachusetts Institute of Technology
41 *
42 * Permission to use, copy, modify, and distribute this software and
43 * its documentation for any purpose and without fee is hereby
44 * granted, provided that both the above copyright notice and this
45 * permission notice appear in all copies, that both the above
46 * copyright notice and this permission notice appear in all
47 * supporting documentation, and that the name of M.I.T. not be used
48 * in advertising or publicity pertaining to distribution of the
49 * software without specific, written prior permission. M.I.T. makes
50 * no representations about the suitability of this software for any
51 * purpose. It is provided "as is" without express or implied
52 * warranty.
53 *
54 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
55 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
56 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
57 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
58 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
61 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
62 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
63 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
64 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * from FreeBSD: if_vlan.c,v 1.16 2000/03/26 15:21:40 charnier Exp
68 * via OpenBSD: if_vlan.c,v 1.4 2000/05/15 19:15:00 chris Exp
69 */
70
71 /*
72 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. Might be
73 * extended some day to also handle IEEE 802.1P priority tagging. This is
74 * sort of sneaky in the implementation, since we need to pretend to be
75 * enough of an Ethernet implementation to make ARP work. The way we do
76 * this is by telling everyone that we are an Ethernet interface, and then
77 * catch the packets that ether_output() left on our output queue when it
78 * calls if_start(), rewrite them for use by the real outgoing interface,
79 * and ask it to send them.
80 *
81 * TODO:
82 *
83 * - Need some way to notify vlan interfaces when the parent
84 * interface changes MTU.
85 *
86 * - Need a way to facilitate parent interfaces that can do
87 * tag insertion and/or extraction in hardware.
88 */
89
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 #if NBPFILTER > 0
307 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
308 sizeof(struct ether_header));
309 #endif
310 break;
311 }
312
313 default:
314 return (EPROTONOSUPPORT);
315 }
316
317 ifv->ifv_p = p;
318 ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge;
319 ifv->ifv_if.if_flags = p->if_flags;
320
321 /*
322 * Inherit the if_type from the parent. This allows us
323 * to participate in bridges of that type.
324 */
325 ifv->ifv_if.if_type = p->if_type;
326
327 return (0);
328 }
329
330 /*
331 * Unconfigure a VLAN interface. Must be called at splnet().
332 */
333 static void
334 vlan_unconfig(struct ifnet *ifp)
335 {
336 struct ifvlan *ifv = ifp->if_softc;
337
338 if (ifv->ifv_p == NULL)
339 return;
340
341 /*
342 * Since the interface is being unconfigured, we need to empty the
343 * list of multicast groups that we may have joined while we were
344 * alive and remove them from the parent's list also.
345 */
346 (*ifv->ifv_msw->vmsw_purgemulti)(ifv);
347
348 /* Disconnect from parent. */
349 switch (ifv->ifv_p->if_type) {
350 case IFT_ETHER:
351 {
352 struct ethercom *ec = (void *) ifv->ifv_p;
353
354 if (ec->ec_nvlans-- == 1) {
355 /*
356 * Disable Tx/Rx of VLAN-sized frames.
357 */
358 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
359 if (ifv->ifv_p->if_flags & IFF_UP) {
360 struct ifreq ifr;
361
362 ifr.ifr_flags = ifv->ifv_p->if_flags;
363 (void) (*ifv->ifv_p->if_ioctl)(ifv->ifv_p,
364 SIOCSIFFLAGS, (caddr_t) &ifr);
365 }
366 }
367
368 #if NBPFILTER > 0
369 bpfdetach(ifp);
370 #endif
371 ether_ifdetach(ifp);
372 break;
373 }
374
375 #ifdef DIAGNOSTIC
376 default:
377 panic("vlan_unconfig: impossible");
378 #endif
379 }
380
381 ifv->ifv_p = NULL;
382 ifv->ifv_if.if_mtu = 0;
383 ifv->ifv_flags = 0;
384
385 if_down(ifp);
386 ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
387 }
388
389 /*
390 * Called when a parent interface is detaching; destroy any VLAN
391 * configuration for the parent interface.
392 */
393 void
394 vlan_ifdetach(struct ifnet *p)
395 {
396 struct ifvlan *ifv;
397 int s;
398
399 s = splnet();
400
401 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
402 ifv = LIST_NEXT(ifv, ifv_list)) {
403 if (ifv->ifv_p == p)
404 vlan_unconfig(&ifv->ifv_if);
405 }
406
407 splx(s);
408 }
409
410 static int
411 vlan_set_promisc(struct ifnet *ifp)
412 {
413 struct ifvlan *ifv = ifp->if_softc;
414 int error;
415
416 if ((ifp->if_flags & IFF_PROMISC) != 0) {
417 if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
418 error = ifpromisc(ifv->ifv_p, 1);
419 if (error == 0)
420 ifv->ifv_flags |= IFVF_PROMISC;
421 }
422 } else {
423 if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
424 error = ifpromisc(ifv->ifv_p, 0);
425 if (error == 0)
426 ifv->ifv_flags &= ~IFVF_PROMISC;
427 }
428 }
429
430 return (error);
431 }
432
433 static int
434 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
435 {
436 struct proc *p = curproc; /* XXX */
437 struct ifvlan *ifv = ifp->if_softc;
438 struct ifaddr *ifa = (struct ifaddr *) data;
439 struct ifreq *ifr = (struct ifreq *) data;
440 struct ifnet *pr;
441 struct vlanreq vlr;
442 struct sockaddr *sa;
443 int s, error = 0;
444
445 s = splnet();
446
447 switch (cmd) {
448 case SIOCSIFADDR:
449 if (ifv->ifv_p != NULL) {
450 ifp->if_flags |= IFF_UP;
451
452 switch (ifa->ifa_addr->sa_family) {
453 #ifdef INET
454 case AF_INET:
455 arp_ifinit(ifp, ifa);
456 break;
457 #endif
458 default:
459 break;
460 }
461 } else {
462 error = EINVAL;
463 }
464 break;
465
466 case SIOCGIFADDR:
467 sa = (struct sockaddr *)&ifr->ifr_data;
468 memcpy(sa->sa_data, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
469 break;
470
471 case SIOCSIFMTU:
472 if (ifv->ifv_p != NULL) {
473 if (ifr->ifr_mtu >
474 (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
475 ifr->ifr_mtu <
476 (ifv->ifv_mintu - ifv->ifv_mtufudge))
477 error = EINVAL;
478 else
479 ifp->if_mtu = ifr->ifr_mtu;
480 } else
481 error = EINVAL;
482 break;
483
484 case SIOCSETVLAN:
485 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
486 break;
487 if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
488 break;
489 if (vlr.vlr_parent[0] == '\0') {
490 vlan_unconfig(ifp);
491 break;
492 }
493 if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
494 error = EINVAL; /* check for valid tag */
495 break;
496 }
497 if ((pr = ifunit(vlr.vlr_parent)) == 0) {
498 error = ENOENT;
499 break;
500 }
501 if ((error = vlan_config(ifv, pr)) != 0)
502 break;
503 ifv->ifv_tag = vlr.vlr_tag;
504 ifp->if_flags |= IFF_RUNNING;
505
506 /* Update promiscuous mode, if necessary. */
507 vlan_set_promisc(ifp);
508 break;
509
510 case SIOCGETVLAN:
511 memset(&vlr, 0, sizeof(vlr));
512 if (ifv->ifv_p != NULL) {
513 snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
514 ifv->ifv_p->if_xname);
515 vlr.vlr_tag = ifv->ifv_tag;
516 }
517 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
518 break;
519
520 case SIOCSIFFLAGS:
521 /*
522 * For promiscuous mode, we enable promiscuous mode on
523 * the parent if we need promiscuous on the VLAN interface.
524 */
525 if (ifv->ifv_p != NULL)
526 error = vlan_set_promisc(ifp);
527 break;
528
529 case SIOCADDMULTI:
530 if (ifv->ifv_p != NULL) {
531 error = (*ifv->ifv_msw->vmsw_addmulti)(ifv, ifr);
532 } else {
533 error = EINVAL;
534 }
535 break;
536
537 case SIOCDELMULTI:
538 if (ifv->ifv_p != NULL) {
539 error = (*ifv->ifv_msw->vmsw_delmulti)(ifv, ifr);
540 } else {
541 error = EINVAL;
542 }
543 break;
544
545 default:
546 error = EINVAL;
547 }
548
549 splx(s);
550
551 return (error);
552 }
553
554 static int
555 vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
556 {
557 struct vlan_mc_entry *mc;
558 u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
559 int error;
560
561 if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr_storage))
562 return (EINVAL);
563
564 error = ether_addmulti(ifr, &ifv->ifv_ec);
565 if (error != ENETRESET)
566 return (error);
567
568 /*
569 * This is new multicast address. We have to tell parent
570 * about it. Also, remember this multicast address so that
571 * we can delete them on unconfigure.
572 */
573 MALLOC(mc, struct vlan_mc_entry *, sizeof(struct vlan_mc_entry),
574 M_DEVBUF, M_NOWAIT);
575 if (mc == NULL) {
576 error = ENOMEM;
577 goto alloc_failed;
578 }
579
580 /*
581 * As ether_addmulti() returns ENETRESET, following two
582 * statement shouldn't fail.
583 */
584 (void)ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
585 ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, mc->mc_enm);
586 memcpy(&mc->mc_addr, &ifr->ifr_addr, ifr->ifr_addr.sa_len);
587 LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
588
589 error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCADDMULTI,
590 (caddr_t)ifr);
591 if (error != 0)
592 goto ioctl_failed;
593 return (error);
594
595 ioctl_failed:
596 LIST_REMOVE(mc, mc_entries);
597 FREE(mc, M_DEVBUF);
598 alloc_failed:
599 (void)ether_delmulti(ifr, &ifv->ifv_ec);
600 return (error);
601 }
602
603 static int
604 vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
605 {
606 struct ether_multi *enm;
607 struct vlan_mc_entry *mc;
608 u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
609 int error;
610
611 /*
612 * Find a key to lookup vlan_mc_entry. We have to do this
613 * before calling ether_delmulti for obvious reason.
614 */
615 if ((error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi)) != 0)
616 return (error);
617 ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, enm);
618
619 error = ether_delmulti(ifr, &ifv->ifv_ec);
620 if (error != ENETRESET)
621 return (error);
622
623 /* We no longer use this multicast address. Tell parent so. */
624 error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCDELMULTI,
625 (caddr_t)ifr);
626 if (error == 0) {
627 /* And forget about this address. */
628 for (mc = LIST_FIRST(&ifv->ifv_mc_listhead); mc != NULL;
629 mc = LIST_NEXT(mc, mc_entries)) {
630 if (mc->mc_enm == enm) {
631 LIST_REMOVE(mc, mc_entries);
632 FREE(mc, M_DEVBUF);
633 break;
634 }
635 }
636 KASSERT(mc != NULL);
637 } else
638 (void)ether_addmulti(ifr, &ifv->ifv_ec);
639 return (error);
640 }
641
642 /*
643 * Delete any multicast address we have asked to add form parent
644 * interface. Called when the vlan is being unconfigured.
645 */
646 static void
647 vlan_ether_purgemulti(struct ifvlan *ifv)
648 {
649 struct ifnet *ifp = ifv->ifv_p; /* Parent. */
650 struct vlan_mc_entry *mc;
651 union {
652 struct ifreq ifreq;
653 struct {
654 char ifr_name[IFNAMSIZ];
655 struct sockaddr_storage ifr_ss;
656 } ifreq_storage;
657 } ifreq;
658 struct ifreq *ifr = &ifreq.ifreq;
659
660 memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
661 while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
662 memcpy(&ifr->ifr_addr, &mc->mc_addr, mc->mc_addr.ss_len);
663 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)ifr);
664 LIST_REMOVE(mc->mc_enm, enm_list);
665 free(mc->mc_enm, M_IFMADDR);
666 LIST_REMOVE(mc, mc_entries);
667 FREE(mc, M_DEVBUF);
668 }
669
670 KASSERT(LIST_FIRST(&ifv->ifv_ec.ec_multiaddrs) == NULL);
671 }
672
673 static void
674 vlan_start(struct ifnet *ifp)
675 {
676 struct ifvlan *ifv = ifp->if_softc;
677 struct ifnet *p = ifv->ifv_p;
678 struct mbuf *m;
679
680 ifp->if_flags |= IFF_OACTIVE;
681
682 for (;;) {
683 IF_DEQUEUE(&ifp->if_snd, m);
684 if (m == NULL)
685 break;
686
687 #if NBPFILTER > 0
688 if (ifp->if_bpf)
689 bpf_mtap(ifp->if_bpf, m);
690 #endif
691
692 /*
693 * XXX Should handle the case where the underlying hardware
694 * interface can do VLAN tag insertion itself.
695 */
696 M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
697 if (m == NULL) {
698 printf("%s: unable to prepend encap header",
699 ifv->ifv_p->if_xname);
700 ifp->if_oerrors++;
701 continue;
702 }
703
704 switch (p->if_type) {
705 case IFT_ETHER:
706 {
707 struct ether_vlan_header *evl;
708
709 if (m->m_len < sizeof(struct ether_vlan_header) &&
710 (m = m_pullup(m,
711 sizeof(struct ether_vlan_header))) == NULL) {
712 printf("%s: unable to pullup encap header",
713 ifv->ifv_p->if_xname);
714 ifp->if_oerrors++;
715 continue;
716 }
717
718 /*
719 * Transform the Ethernet header into an Ethernet
720 * header with 802.1Q encapsulation.
721 */
722 memmove(mtod(m, caddr_t),
723 mtod(m, caddr_t) + ifv->ifv_encaplen,
724 sizeof(struct ether_header));
725 evl = mtod(m, struct ether_vlan_header *);
726 evl->evl_proto = evl->evl_encap_proto;
727 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
728 evl->evl_tag = htons(ifv->ifv_tag);
729 break;
730 }
731
732 #ifdef DIAGNOSTIC
733 default:
734 panic("vlan_start: impossible");
735 #endif
736 }
737
738 /*
739 * Send it, precisely as the parent's output routine
740 * would have. We are already running at splimp.
741 */
742 if (IF_QFULL(&p->if_snd)) {
743 IF_DROP(&p->if_snd);
744 /* XXX stats */
745 ifp->if_oerrors++;
746 m_freem(m);
747 continue;
748 }
749
750 IF_ENQUEUE(&p->if_snd, m);
751 if ((p->if_flags & IFF_OACTIVE) == 0) {
752 (*p->if_start)(p);
753 ifp->if_opackets++;
754 }
755 }
756
757 ifp->if_flags &= ~IFF_OACTIVE;
758 }
759
760 /*
761 * Given an Ethernet frame, find a valid vlan interface corresponding to the
762 * given source interface and tag, then run the the real packet through
763 * the parent's input routine.
764 */
765 void
766 vlan_input(struct ifnet *ifp, struct mbuf *m)
767 {
768 struct ifvlan *ifv;
769 u_int tag;
770
771 switch (ifp->if_type) {
772 case IFT_ETHER:
773 {
774 struct ether_vlan_header *evl;
775
776 if (m->m_len < sizeof(struct ether_vlan_header) &&
777 (m = m_pullup(m,
778 sizeof(struct ether_vlan_header))) == NULL) {
779 printf("%s: no memory for VLAN header, "
780 "dropping packet.\n", ifp->if_xname);
781 return;
782 }
783 evl = mtod(m, struct ether_vlan_header *);
784 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
785
786 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
787
788 /*
789 * Restore the original ethertype. We'll remove
790 * the encapsulation after we've found the vlan
791 * interface corresponding to the tag.
792 */
793 evl->evl_encap_proto = evl->evl_proto;
794 break;
795 }
796
797 default:
798 tag = (u_int) -1; /* XXX GCC */
799 #ifdef DIAGNOSTIC
800 panic("vlan_input: impossible");
801 #endif
802 }
803
804 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
805 ifv = LIST_NEXT(ifv, ifv_list))
806 if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
807 break;
808
809 if (ifv == NULL ||
810 (ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
811 (IFF_UP|IFF_RUNNING)) {
812 m_free(m);
813 ifp->if_noproto++;
814 return;
815 }
816
817 /*
818 * Now, remove the encapsulation header. The original
819 * header has already been fixed up above.
820 */
821 memmove(mtod(m, caddr_t) + ifv->ifv_encaplen, mtod(m, caddr_t),
822 ifv->ifv_encaplen);
823 m_adj(m, ifv->ifv_encaplen);
824
825 m->m_pkthdr.rcvif = &ifv->ifv_if;
826 ifv->ifv_if.if_ipackets++;
827
828 #if NBPFILTER > 0
829 if (ifv->ifv_if.if_bpf)
830 bpf_mtap(ifv->ifv_if.if_bpf, m);
831 #endif
832
833 /* Pass it back through the parent's input routine. */
834 (*ifp->if_input)(&ifv->ifv_if, m);
835 }
836