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