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