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