if_vlan.c revision 1.30.2.1 1 /* $NetBSD: if_vlan.c,v 1.30.2.1 2001/03/05 22:49:55 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran, and by Jason R. Thorpe of Zembu Labs, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright 1998 Massachusetts Institute of Technology
41 *
42 * Permission to use, copy, modify, and distribute this software and
43 * its documentation for any purpose and without fee is hereby
44 * granted, provided that both the above copyright notice and this
45 * permission notice appear in all copies, that both the above
46 * copyright notice and this permission notice appear in all
47 * supporting documentation, and that the name of M.I.T. not be used
48 * in advertising or publicity pertaining to distribution of the
49 * software without specific, written prior permission. M.I.T. makes
50 * no representations about the suitability of this software for any
51 * purpose. It is provided "as is" without express or implied
52 * warranty.
53 *
54 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
55 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
56 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
57 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
58 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
61 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
62 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
63 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
64 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * from FreeBSD: if_vlan.c,v 1.16 2000/03/26 15:21:40 charnier Exp
68 * via OpenBSD: if_vlan.c,v 1.4 2000/05/15 19:15:00 chris Exp
69 */
70
71 /*
72 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. Might be
73 * extended some day to also handle IEEE 802.1P priority tagging. This is
74 * sort of sneaky in the implementation, since we need to pretend to be
75 * enough of an Ethernet implementation to make ARP work. The way we do
76 * this is by telling everyone that we are an Ethernet interface, and then
77 * catch the packets that ether_output() left on our output queue when it
78 * calls if_start(), rewrite them for use by the real outgoing interface,
79 * and ask it to send them.
80 *
81 * TODO:
82 *
83 * - Need some way to notify vlan interfaces when the parent
84 * interface changes MTU.
85 */
86
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
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->l_proc; /* 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
680 ifp->if_flags |= IFF_OACTIVE;
681
682 for (;;) {
683 IF_DEQUEUE(&ifp->if_snd, m);
684 if (m == NULL)
685 break;
686
687 #if NBPFILTER > 0
688 if (ifp->if_bpf)
689 bpf_mtap(ifp->if_bpf, m);
690 #endif
691 /*
692 * If the parent can insert the tag itself, just mark
693 * the tag in the mbuf header.
694 */
695 if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
696 struct mbuf *n;
697 n = m_aux_add(m, AF_LINK, ETHERTYPE_VLAN);
698 if (n == NULL) {
699 ifp->if_oerrors++;
700 m_freem(m);
701 continue;
702 }
703 *mtod(n, int *) = ifv->ifv_tag;
704 n->m_len = sizeof(int);
705 } else {
706 /*
707 * insert the tag ourselve
708 */
709 M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
710 if (m == NULL) {
711 printf("%s: unable to prepend encap header",
712 ifv->ifv_p->if_xname);
713 ifp->if_oerrors++;
714 continue;
715 }
716
717 switch (p->if_type) {
718 case IFT_ETHER:
719 {
720 struct ether_vlan_header *evl;
721
722 if (m->m_len < sizeof(struct ether_vlan_header))
723 m = m_pullup(m,
724 sizeof(struct ether_vlan_header));
725 if (m == NULL) {
726 printf("%s: unable to pullup encap "
727 "header", ifv->ifv_p->if_xname);
728 ifp->if_oerrors++;
729 continue;
730 }
731
732 /*
733 * Transform the Ethernet header into an
734 * Ethernet header with 802.1Q encapsulation.
735 */
736 memmove(mtod(m, caddr_t),
737 mtod(m, caddr_t) + ifv->ifv_encaplen,
738 sizeof(struct ether_header));
739 evl = mtod(m, struct ether_vlan_header *);
740 evl->evl_proto = evl->evl_encap_proto;
741 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
742 evl->evl_tag = htons(ifv->ifv_tag);
743 break;
744 }
745
746 #ifdef DIAGNOSTIC
747 default:
748 panic("vlan_start: impossible");
749 #endif
750 }
751 }
752
753 /*
754 * Send it, precisely as the parent's output routine
755 * would have. We are already running at splimp.
756 */
757 if (IF_QFULL(&p->if_snd)) {
758 IF_DROP(&p->if_snd);
759 /* XXX stats */
760 ifp->if_oerrors++;
761 m_freem(m);
762 continue;
763 }
764
765 IF_ENQUEUE(&p->if_snd, m);
766 ifp->if_opackets++;
767 if ((p->if_flags & IFF_OACTIVE) == 0) {
768 (*p->if_start)(p);
769 }
770 }
771
772 ifp->if_flags &= ~IFF_OACTIVE;
773 }
774
775 /*
776 * Given an Ethernet frame, find a valid vlan interface corresponding to the
777 * given source interface and tag, then run the the real packet through
778 * the parent's input routine.
779 */
780 void
781 vlan_input(struct ifnet *ifp, struct mbuf *m)
782 {
783 struct ifvlan *ifv;
784 u_int tag;
785 struct mbuf *n;
786
787 n = m_aux_find(m, AF_LINK, ETHERTYPE_VLAN);
788 if (n) {
789 /* m contains a normal ethernet frame, the tag is in m_aux */
790 tag = *mtod(n, int *);
791 m_aux_delete(m, n);
792 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
793 ifv = LIST_NEXT(ifv, ifv_list))
794 if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
795 break;
796 } else {
797 switch (ifp->if_type) {
798 case IFT_ETHER:
799 {
800 struct ether_vlan_header *evl;
801
802 if (m->m_len < sizeof(struct ether_vlan_header) &&
803 (m = m_pullup(m,
804 sizeof(struct ether_vlan_header))) == NULL) {
805 printf("%s: no memory for VLAN header, "
806 "dropping packet.\n", ifp->if_xname);
807 return;
808 }
809 evl = mtod(m, struct ether_vlan_header *);
810 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
811
812 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
813
814 /*
815 * Restore the original ethertype. We'll remove
816 * the encapsulation after we've found the vlan
817 * interface corresponding to the tag.
818 */
819 evl->evl_encap_proto = evl->evl_proto;
820 break;
821 }
822
823 default:
824 tag = (u_int) -1; /* XXX GCC */
825 #ifdef DIAGNOSTIC
826 panic("vlan_input: impossible");
827 #endif
828 }
829
830 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
831 ifv = LIST_NEXT(ifv, ifv_list))
832 if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
833 break;
834
835
836 /*
837 * Now, remove the encapsulation header. The original
838 * header has already been fixed up above.
839 */
840 if (ifv) {
841 memmove(mtod(m, caddr_t) + ifv->ifv_encaplen,
842 mtod(m, caddr_t), sizeof(struct ether_header));
843 m_adj(m, ifv->ifv_encaplen);
844 }
845 }
846
847 if (ifv == NULL ||
848 (ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
849 (IFF_UP|IFF_RUNNING)) {
850 m_free(m);
851 ifp->if_noproto++;
852 return;
853 }
854 m->m_pkthdr.rcvif = &ifv->ifv_if;
855 ifv->ifv_if.if_ipackets++;
856
857 #if NBPFILTER > 0
858 if (ifv->ifv_if.if_bpf)
859 bpf_mtap(ifv->ifv_if.if_bpf, m);
860 #endif
861
862 /* Pass it back through the parent's input routine. */
863 (*ifp->if_input)(&ifv->ifv_if, m);
864 }
865