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