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