if_vlan.c revision 1.100 1 /* $NetBSD: if_vlan.c,v 1.100 2017/09/26 07:42:06 knakahara 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright 1998 Massachusetts Institute of Technology
34 *
35 * Permission to use, copy, modify, and distribute this software and
36 * its documentation for any purpose and without fee is hereby
37 * granted, provided that both the above copyright notice and this
38 * permission notice appear in all copies, that both the above
39 * copyright notice and this permission notice appear in all
40 * supporting documentation, and that the name of M.I.T. not be used
41 * in advertising or publicity pertaining to distribution of the
42 * software without specific, written prior permission. M.I.T. makes
43 * no representations about the suitability of this software for any
44 * purpose. It is provided "as is" without express or implied
45 * warranty.
46 *
47 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
48 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
49 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
50 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
51 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * from FreeBSD: if_vlan.c,v 1.16 2000/03/26 15:21:40 charnier Exp
61 * via OpenBSD: if_vlan.c,v 1.4 2000/05/15 19:15:00 chris Exp
62 */
63
64 /*
65 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. Might be
66 * extended some day to also handle IEEE 802.1P priority tagging. This is
67 * sort of sneaky in the implementation, since we need to pretend to be
68 * enough of an Ethernet implementation to make ARP work. The way we do
69 * this is by telling everyone that we are an Ethernet interface, and then
70 * catch the packets that ether_output() left on our output queue when it
71 * calls if_start(), rewrite them for use by the real outgoing interface,
72 * and ask it to send them.
73 *
74 * TODO:
75 *
76 * - Need some way to notify vlan interfaces when the parent
77 * interface changes MTU.
78 */
79
80 #include <sys/cdefs.h>
81 __KERNEL_RCSID(0, "$NetBSD: if_vlan.c,v 1.100 2017/09/26 07:42:06 knakahara Exp $");
82
83 #ifdef _KERNEL_OPT
84 #include "opt_inet.h"
85 #include "opt_net_mpsafe.h"
86 #endif
87
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/kernel.h>
91 #include <sys/mbuf.h>
92 #include <sys/queue.h>
93 #include <sys/socket.h>
94 #include <sys/sockio.h>
95 #include <sys/systm.h>
96 #include <sys/proc.h>
97 #include <sys/kauth.h>
98 #include <sys/mutex.h>
99 #include <sys/kmem.h>
100 #include <sys/cpu.h>
101 #include <sys/pserialize.h>
102 #include <sys/psref.h>
103 #include <sys/pslist.h>
104 #include <sys/atomic.h>
105 #include <sys/device.h>
106 #include <sys/module.h>
107
108 #include <net/bpf.h>
109 #include <net/if.h>
110 #include <net/if_dl.h>
111 #include <net/if_types.h>
112 #include <net/if_ether.h>
113 #include <net/if_vlanvar.h>
114
115 #ifdef INET
116 #include <netinet/in.h>
117 #include <netinet/if_inarp.h>
118 #endif
119 #ifdef INET6
120 #include <netinet6/in6_ifattach.h>
121 #include <netinet6/in6_var.h>
122 #endif
123
124 #include "ioconf.h"
125
126 #ifdef NET_MPSAFE
127 #define VLAN_MPSAFE 1
128 #endif
129
130 struct vlan_mc_entry {
131 LIST_ENTRY(vlan_mc_entry) mc_entries;
132 /*
133 * A key to identify this entry. The mc_addr below can't be
134 * used since multiple sockaddr may mapped into the same
135 * ether_multi (e.g., AF_UNSPEC).
136 */
137 union {
138 struct ether_multi *mcu_enm;
139 } mc_u;
140 struct sockaddr_storage mc_addr;
141 };
142
143 #define mc_enm mc_u.mcu_enm
144
145
146 struct ifvlan_linkmib {
147 struct ifvlan *ifvm_ifvlan;
148 const struct vlan_multisw *ifvm_msw;
149 int ifvm_encaplen; /* encapsulation length */
150 int ifvm_mtufudge; /* MTU fudged by this much */
151 int ifvm_mintu; /* min transmission unit */
152 uint16_t ifvm_proto; /* encapsulation ethertype */
153 uint16_t ifvm_tag; /* tag to apply on packets */
154 struct ifnet *ifvm_p; /* parent interface of this vlan */
155
156 struct psref_target ifvm_psref;
157 };
158
159 struct ifvlan {
160 union {
161 struct ethercom ifvu_ec;
162 } ifv_u;
163 struct ifvlan_linkmib *ifv_mib; /*
164 * reader must use vlan_getref_linkmib()
165 * instead of direct dereference
166 */
167 kmutex_t ifv_lock; /* writer lock for ifv_mib */
168
169 LIST_HEAD(__vlan_mchead, vlan_mc_entry) ifv_mc_listhead;
170 LIST_ENTRY(ifvlan) ifv_list;
171 struct pslist_entry ifv_hash;
172 int ifv_flags;
173 };
174
175 #define IFVF_PROMISC 0x01 /* promiscuous mode enabled */
176
177 #define ifv_ec ifv_u.ifvu_ec
178
179 #define ifv_if ifv_ec.ec_if
180
181 #define ifv_msw ifv_mib.ifvm_msw
182 #define ifv_encaplen ifv_mib.ifvm_encaplen
183 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
184 #define ifv_mintu ifv_mib.ifvm_mintu
185 #define ifv_tag ifv_mib.ifvm_tag
186
187 struct vlan_multisw {
188 int (*vmsw_addmulti)(struct ifvlan *, struct ifreq *);
189 int (*vmsw_delmulti)(struct ifvlan *, struct ifreq *);
190 void (*vmsw_purgemulti)(struct ifvlan *);
191 };
192
193 static int vlan_ether_addmulti(struct ifvlan *, struct ifreq *);
194 static int vlan_ether_delmulti(struct ifvlan *, struct ifreq *);
195 static void vlan_ether_purgemulti(struct ifvlan *);
196
197 const struct vlan_multisw vlan_ether_multisw = {
198 vlan_ether_addmulti,
199 vlan_ether_delmulti,
200 vlan_ether_purgemulti,
201 };
202
203 static int vlan_clone_create(struct if_clone *, int);
204 static int vlan_clone_destroy(struct ifnet *);
205 static int vlan_config(struct ifvlan *, struct ifnet *,
206 uint16_t);
207 static int vlan_ioctl(struct ifnet *, u_long, void *);
208 static void vlan_start(struct ifnet *);
209 static int vlan_transmit(struct ifnet *, struct mbuf *);
210 static void vlan_unconfig(struct ifnet *);
211 static int vlan_unconfig_locked(struct ifvlan *,
212 struct ifvlan_linkmib *);
213 static void vlan_hash_init(void);
214 static int vlan_hash_fini(void);
215 static struct ifvlan_linkmib* vlan_getref_linkmib(struct ifvlan *,
216 struct psref *);
217 static void vlan_putref_linkmib(struct ifvlan_linkmib *,
218 struct psref *);
219 static void vlan_linkmib_update(struct ifvlan *,
220 struct ifvlan_linkmib *);
221 static struct ifvlan_linkmib* vlan_lookup_tag_psref(struct ifnet *,
222 uint16_t, struct psref *);
223 static int tag_hash_func(uint16_t, u_long);
224
225 LIST_HEAD(vlan_ifvlist, ifvlan);
226 static struct {
227 kmutex_t lock;
228 struct vlan_ifvlist list;
229 } ifv_list __cacheline_aligned;
230
231
232 #if !defined(VLAN_TAG_HASH_SIZE)
233 #define VLAN_TAG_HASH_SIZE 32
234 #endif
235 static struct {
236 kmutex_t lock;
237 struct pslist_head *lists;
238 u_long mask;
239 } ifv_hash __cacheline_aligned = {
240 .lists = NULL,
241 .mask = 0,
242 };
243
244 pserialize_t vlan_psz __read_mostly;
245 static struct psref_class *ifvm_psref_class __read_mostly;
246
247 struct if_clone vlan_cloner =
248 IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
249
250 /* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */
251 static char vlan_zero_pad_buff[ETHER_MIN_LEN];
252
253 void
254 vlanattach(int n)
255 {
256
257 /*
258 * Nothing to do here, initialization is handled by the
259 * module initialization code in vlaninit() below).
260 */
261 }
262
263 static void
264 vlaninit(void)
265 {
266 mutex_init(&ifv_list.lock, MUTEX_DEFAULT, IPL_NONE);
267 LIST_INIT(&ifv_list.list);
268
269 mutex_init(&ifv_hash.lock, MUTEX_DEFAULT, IPL_NONE);
270 vlan_psz = pserialize_create();
271 ifvm_psref_class = psref_class_create("vlanlinkmib", IPL_SOFTNET);
272 if_clone_attach(&vlan_cloner);
273
274 vlan_hash_init();
275 }
276
277 static int
278 vlandetach(void)
279 {
280 int error = 0;
281
282 mutex_enter(&ifv_list.lock);
283 if (!LIST_EMPTY(&ifv_list.list)) {
284 mutex_exit(&ifv_list.lock);
285 return EBUSY;
286 }
287 mutex_exit(&ifv_list.lock);
288
289 error = vlan_hash_fini();
290 if (error != 0)
291 return error;
292
293 if_clone_detach(&vlan_cloner);
294 psref_class_destroy(ifvm_psref_class);
295 pserialize_destroy(vlan_psz);
296 mutex_destroy(&ifv_hash.lock);
297 mutex_destroy(&ifv_list.lock);
298
299 return 0;
300 }
301
302 static void
303 vlan_reset_linkname(struct ifnet *ifp)
304 {
305
306 /*
307 * We start out with a "802.1Q VLAN" type and zero-length
308 * addresses. When we attach to a parent interface, we
309 * inherit its type, address length, address, and data link
310 * type.
311 */
312
313 ifp->if_type = IFT_L2VLAN;
314 ifp->if_addrlen = 0;
315 ifp->if_dlt = DLT_NULL;
316 if_alloc_sadl(ifp);
317 }
318
319 static int
320 vlan_clone_create(struct if_clone *ifc, int unit)
321 {
322 struct ifvlan *ifv;
323 struct ifnet *ifp;
324 struct ifvlan_linkmib *mib;
325
326 ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK|M_ZERO);
327 mib = kmem_zalloc(sizeof(struct ifvlan_linkmib), KM_SLEEP);
328 ifp = &ifv->ifv_if;
329 LIST_INIT(&ifv->ifv_mc_listhead);
330
331 mib->ifvm_ifvlan = ifv;
332 mib->ifvm_p = NULL;
333 psref_target_init(&mib->ifvm_psref, ifvm_psref_class);
334
335 mutex_init(&ifv->ifv_lock, MUTEX_DEFAULT, IPL_NONE);
336 ifv->ifv_mib = mib;
337
338 mutex_enter(&ifv_list.lock);
339 LIST_INSERT_HEAD(&ifv_list.list, ifv, ifv_list);
340 mutex_exit(&ifv_list.lock);
341
342 if_initname(ifp, ifc->ifc_name, unit);
343 ifp->if_softc = ifv;
344 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
345 #ifdef VLAN_MPSAFE
346 ifp->if_extflags = IFEF_START_MPSAFE;
347 #endif
348 ifp->if_start = vlan_start;
349 ifp->if_transmit = vlan_transmit;
350 ifp->if_ioctl = vlan_ioctl;
351 IFQ_SET_READY(&ifp->if_snd);
352
353 if_initialize(ifp);
354 vlan_reset_linkname(ifp);
355 if_register(ifp);
356
357 return (0);
358 }
359
360 static int
361 vlan_clone_destroy(struct ifnet *ifp)
362 {
363 struct ifvlan *ifv = ifp->if_softc;
364
365 mutex_enter(&ifv_list.lock);
366 LIST_REMOVE(ifv, ifv_list);
367 mutex_exit(&ifv_list.lock);
368
369 vlan_unconfig(ifp);
370 if_detach(ifp);
371
372 psref_target_destroy(&ifv->ifv_mib->ifvm_psref, ifvm_psref_class);
373 kmem_free(ifv->ifv_mib, sizeof(struct ifvlan_linkmib));
374 mutex_destroy(&ifv->ifv_lock);
375 free(ifv, M_DEVBUF);
376
377 return (0);
378 }
379
380 /*
381 * Configure a VLAN interface.
382 */
383 static int
384 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
385 {
386 struct ifnet *ifp = &ifv->ifv_if;
387 struct ifvlan_linkmib *nmib = NULL;
388 struct ifvlan_linkmib *omib = NULL;
389 struct psref_target *nmib_psref = NULL;
390 int error = 0;
391 int idx;
392 bool omib_cleanup = false;
393
394 nmib = kmem_alloc(sizeof(*nmib), KM_SLEEP);
395
396 mutex_enter(&ifv->ifv_lock);
397 omib = ifv->ifv_mib;
398
399 if (omib->ifvm_p != NULL) {
400 error = EBUSY;
401 goto done;
402 }
403
404 *nmib = *omib;
405 nmib_psref = &nmib->ifvm_psref;
406
407 psref_target_init(nmib_psref, ifvm_psref_class);
408
409 switch (p->if_type) {
410 case IFT_ETHER:
411 {
412 struct ethercom *ec = (void *) p;
413 nmib->ifvm_msw = &vlan_ether_multisw;
414 nmib->ifvm_encaplen = ETHER_VLAN_ENCAP_LEN;
415 nmib->ifvm_mintu = ETHERMIN;
416
417 if (ec->ec_nvlans++ == 0) {
418 if ((error = ether_enable_vlan_mtu(p)) >= 0) {
419 if (error) {
420 ec->ec_nvlans--;
421 goto done;
422 }
423 nmib->ifvm_mtufudge = 0;
424 } else {
425 /*
426 * Fudge the MTU by the encapsulation size. This
427 * makes us incompatible with strictly compliant
428 * 802.1Q implementations, but allows us to use
429 * the feature with other NetBSD
430 * implementations, which might still be useful.
431 */
432 nmib->ifvm_mtufudge = nmib->ifvm_encaplen;
433 }
434 error = 0;
435 }
436
437 /*
438 * If the parent interface can do hardware-assisted
439 * VLAN encapsulation, then propagate its hardware-
440 * assisted checksumming flags and tcp segmentation
441 * offload.
442 */
443 if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
444 ec->ec_capenable |= ETHERCAP_VLAN_HWTAGGING;
445 ifp->if_capabilities = p->if_capabilities &
446 (IFCAP_TSOv4 | IFCAP_TSOv6 |
447 IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx|
448 IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx|
449 IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx|
450 IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx|
451 IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx);
452 }
453 /*
454 * We inherit the parent's Ethernet address.
455 */
456 ether_ifattach(ifp, CLLADDR(p->if_sadl));
457 ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
458 break;
459 }
460
461 default:
462 error = EPROTONOSUPPORT;
463 goto done;
464 }
465
466 nmib->ifvm_p = p;
467 nmib->ifvm_tag = tag;
468 ifv->ifv_if.if_mtu = p->if_mtu - nmib->ifvm_mtufudge;
469 ifv->ifv_if.if_flags = p->if_flags &
470 (IFF_UP | IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
471
472 /*
473 * Inherit the if_type from the parent. This allows us
474 * to participate in bridges of that type.
475 */
476 ifv->ifv_if.if_type = p->if_type;
477
478 idx = tag_hash_func(tag, ifv_hash.mask);
479
480 mutex_enter(&ifv_hash.lock);
481 PSLIST_WRITER_INSERT_HEAD(&ifv_hash.lists[idx], ifv, ifv_hash);
482 mutex_exit(&ifv_hash.lock);
483
484 vlan_linkmib_update(ifv, nmib);
485 nmib = NULL;
486 nmib_psref = NULL;
487 omib_cleanup = true;
488
489 done:
490 mutex_exit(&ifv->ifv_lock);
491
492 if (nmib_psref)
493 psref_target_destroy(nmib_psref, ifvm_psref_class);
494
495 if (nmib)
496 kmem_free(nmib, sizeof(*nmib));
497
498 if (omib_cleanup)
499 kmem_free(omib, sizeof(*omib));
500
501 return error;
502 }
503
504 /*
505 * Unconfigure a VLAN interface.
506 */
507 static void
508 vlan_unconfig(struct ifnet *ifp)
509 {
510 struct ifvlan *ifv = ifp->if_softc;
511 struct ifvlan_linkmib *nmib = NULL;
512 int error;
513
514 nmib = kmem_alloc(sizeof(*nmib), KM_SLEEP);
515
516 mutex_enter(&ifv->ifv_lock);
517 error = vlan_unconfig_locked(ifv, nmib);
518 mutex_exit(&ifv->ifv_lock);
519
520 if (error)
521 kmem_free(nmib, sizeof(*nmib));
522 }
523 static int
524 vlan_unconfig_locked(struct ifvlan *ifv, struct ifvlan_linkmib *nmib)
525 {
526 struct ifnet *p;
527 struct ifnet *ifp = &ifv->ifv_if;
528 struct psref_target *nmib_psref = NULL;
529 struct ifvlan_linkmib *omib;
530 int error = 0;
531
532 KASSERT(mutex_owned(&ifv->ifv_lock));
533
534 omib = ifv->ifv_mib;
535 p = omib->ifvm_p;
536
537 if (p == NULL) {
538 error = -1;
539 goto done;
540 }
541
542 *nmib = *omib;
543 nmib_psref = &nmib->ifvm_psref;
544 psref_target_init(nmib_psref, ifvm_psref_class);
545
546 /*
547 * Since the interface is being unconfigured, we need to empty the
548 * list of multicast groups that we may have joined while we were
549 * alive and remove them from the parent's list also.
550 */
551 (*nmib->ifvm_msw->vmsw_purgemulti)(ifv);
552
553 /* Disconnect from parent. */
554 switch (p->if_type) {
555 case IFT_ETHER:
556 {
557 struct ethercom *ec = (void *)p;
558 if (--ec->ec_nvlans == 0)
559 (void)ether_disable_vlan_mtu(p);
560
561 ether_ifdetach(ifp);
562 /* Restore vlan_ioctl overwritten by ether_ifdetach */
563 ifp->if_ioctl = vlan_ioctl;
564 vlan_reset_linkname(ifp);
565 break;
566 }
567
568 #ifdef DIAGNOSTIC
569 default:
570 panic("vlan_unconfig: impossible");
571 #endif
572 }
573
574 nmib->ifvm_p = NULL;
575 ifv->ifv_if.if_mtu = 0;
576 ifv->ifv_flags = 0;
577
578 mutex_enter(&ifv_hash.lock);
579 PSLIST_WRITER_REMOVE(ifv, ifv_hash);
580 pserialize_perform(vlan_psz);
581 mutex_exit(&ifv_hash.lock);
582
583 vlan_linkmib_update(ifv, nmib);
584
585 mutex_exit(&ifv->ifv_lock);
586
587 nmib_psref = NULL;
588 kmem_free(omib, sizeof(*omib));
589
590 #ifdef INET6
591 /* To delete v6 link local addresses */
592 if (in6_present)
593 in6_ifdetach(ifp);
594 #endif
595
596 if ((ifp->if_flags & IFF_PROMISC) != 0)
597 ifpromisc(ifp, 0);
598 if_down(ifp);
599 ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
600 ifp->if_capabilities = 0;
601 mutex_enter(&ifv->ifv_lock);
602 done:
603
604 if (nmib_psref)
605 psref_target_destroy(nmib_psref, ifvm_psref_class);
606
607 return error;
608 }
609
610 static void
611 vlan_hash_init(void)
612 {
613
614 ifv_hash.lists = hashinit(VLAN_TAG_HASH_SIZE, HASH_PSLIST, true,
615 &ifv_hash.mask);
616 }
617
618 static int
619 vlan_hash_fini(void)
620 {
621 int i;
622
623 mutex_enter(&ifv_hash.lock);
624
625 for (i = 0; i < ifv_hash.mask + 1; i++) {
626 if (PSLIST_WRITER_FIRST(&ifv_hash.lists[i], struct ifvlan,
627 ifv_hash) != NULL) {
628 mutex_exit(&ifv_hash.lock);
629 return EBUSY;
630 }
631 }
632
633 for (i = 0; i < ifv_hash.mask + 1; i++)
634 PSLIST_DESTROY(&ifv_hash.lists[i]);
635
636 mutex_exit(&ifv_hash.lock);
637
638 hashdone(ifv_hash.lists, HASH_PSLIST, ifv_hash.mask);
639
640 ifv_hash.lists = NULL;
641 ifv_hash.mask = 0;
642
643 return 0;
644 }
645
646 static int
647 tag_hash_func(uint16_t tag, u_long mask)
648 {
649 uint32_t hash;
650
651 hash = (tag >> 8) ^ tag;
652 hash = (hash >> 2) ^ hash;
653
654 return hash & mask;
655 }
656
657 static struct ifvlan_linkmib *
658 vlan_getref_linkmib(struct ifvlan *sc, struct psref *psref)
659 {
660 struct ifvlan_linkmib *mib;
661 int s;
662
663 s = pserialize_read_enter();
664 mib = sc->ifv_mib;
665 if (mib == NULL) {
666 pserialize_read_exit(s);
667 return NULL;
668 }
669 membar_datadep_consumer();
670 psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class);
671 pserialize_read_exit(s);
672
673 return mib;
674 }
675
676 static void
677 vlan_putref_linkmib(struct ifvlan_linkmib *mib, struct psref *psref)
678 {
679 if (mib == NULL)
680 return;
681 psref_release(psref, &mib->ifvm_psref, ifvm_psref_class);
682 }
683
684 static struct ifvlan_linkmib *
685 vlan_lookup_tag_psref(struct ifnet *ifp, uint16_t tag, struct psref *psref)
686 {
687 int idx;
688 int s;
689 struct ifvlan *sc;
690
691 idx = tag_hash_func(tag, ifv_hash.mask);
692
693 s = pserialize_read_enter();
694 PSLIST_READER_FOREACH(sc, &ifv_hash.lists[idx], struct ifvlan,
695 ifv_hash) {
696 struct ifvlan_linkmib *mib = sc->ifv_mib;
697 if (mib == NULL)
698 continue;
699 if (mib->ifvm_tag != tag)
700 continue;
701 if (mib->ifvm_p != ifp)
702 continue;
703
704 psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class);
705 pserialize_read_exit(s);
706 return mib;
707 }
708 pserialize_read_exit(s);
709 return NULL;
710 }
711
712 static void
713 vlan_linkmib_update(struct ifvlan *ifv, struct ifvlan_linkmib *nmib)
714 {
715 struct ifvlan_linkmib *omib = ifv->ifv_mib;
716
717 KASSERT(mutex_owned(&ifv->ifv_lock));
718
719 membar_producer();
720 ifv->ifv_mib = nmib;
721
722 pserialize_perform(vlan_psz);
723 psref_target_destroy(&omib->ifvm_psref, ifvm_psref_class);
724 }
725
726 /*
727 * Called when a parent interface is detaching; destroy any VLAN
728 * configuration for the parent interface.
729 */
730 void
731 vlan_ifdetach(struct ifnet *p)
732 {
733 struct ifvlan *ifv;
734 struct ifvlan_linkmib *mib, **nmibs;
735 struct psref psref;
736 int error;
737 int bound;
738 int i, cnt = 0;
739
740 bound = curlwp_bind();
741 mutex_enter(&ifv_list.lock);
742 LIST_FOREACH(ifv, &ifv_list.list, ifv_list) {
743 mib = vlan_getref_linkmib(ifv, &psref);
744 if (mib == NULL)
745 continue;
746
747 if (mib->ifvm_p == p)
748 cnt++;
749
750 vlan_putref_linkmib(mib, &psref);
751 }
752 mutex_exit(&ifv_list.lock);
753
754 /*
755 * The value of "cnt" does not increase while ifv_list.lock
756 * and ifv->ifv_lock are released here, because the parent
757 * interface is detaching.
758 */
759 nmibs = kmem_alloc(sizeof(*nmibs) * cnt, KM_SLEEP);
760 for (i=0; i < cnt; i++) {
761 nmibs[i] = kmem_alloc(sizeof(*nmibs[i]), KM_SLEEP);
762 }
763
764 mutex_enter(&ifv_list.lock);
765
766 i = 0;
767 LIST_FOREACH(ifv, &ifv_list.list, ifv_list) {
768 mutex_enter(&ifv->ifv_lock);
769 if (ifv->ifv_mib->ifvm_p == p) {
770 KASSERTMSG(i < cnt, "no memory for unconfig, parent=%s",
771 p->if_xname);
772 error = vlan_unconfig_locked(ifv, nmibs[i]);
773 if (!error) {
774 nmibs[i] = NULL;
775 i++;
776 }
777
778 }
779 mutex_exit(&ifv->ifv_lock);
780 }
781
782 mutex_exit(&ifv_list.lock);
783 curlwp_bindx(bound);
784
785 for (i=0; i < cnt; i++) {
786 if (nmibs[i])
787 kmem_free(nmibs[i], sizeof(*nmibs[i]));
788 }
789
790 kmem_free(nmibs, sizeof(*nmibs) * cnt);
791
792 return;
793 }
794
795 static int
796 vlan_set_promisc(struct ifnet *ifp)
797 {
798 struct ifvlan *ifv = ifp->if_softc;
799 struct ifvlan_linkmib *mib;
800 struct psref psref;
801 int error = 0;
802 int bound;
803
804 bound = curlwp_bind();
805 mib = vlan_getref_linkmib(ifv, &psref);
806 if (mib == NULL) {
807 curlwp_bindx(bound);
808 return EBUSY;
809 }
810
811 if ((ifp->if_flags & IFF_PROMISC) != 0) {
812 if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
813 error = ifpromisc(mib->ifvm_p, 1);
814 if (error == 0)
815 ifv->ifv_flags |= IFVF_PROMISC;
816 }
817 } else {
818 if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
819 error = ifpromisc(mib->ifvm_p, 0);
820 if (error == 0)
821 ifv->ifv_flags &= ~IFVF_PROMISC;
822 }
823 }
824 vlan_putref_linkmib(mib, &psref);
825 curlwp_bindx(bound);
826
827 return (error);
828 }
829
830 static int
831 vlan_ioctl(struct ifnet *ifp, u_long cmd, void *data)
832 {
833 struct lwp *l = curlwp; /* XXX */
834 struct ifvlan *ifv = ifp->if_softc;
835 struct ifaddr *ifa = (struct ifaddr *) data;
836 struct ifreq *ifr = (struct ifreq *) data;
837 struct ifnet *pr;
838 struct ifcapreq *ifcr;
839 struct vlanreq vlr;
840 struct ifvlan_linkmib *mib;
841 struct psref psref;
842 int error = 0;
843 int bound;
844
845 switch (cmd) {
846 case SIOCSIFMTU:
847 bound = curlwp_bind();
848 mib = vlan_getref_linkmib(ifv, &psref);
849 if (mib == NULL) {
850 curlwp_bindx(bound);
851 error = EBUSY;
852 break;
853 }
854
855 if (mib->ifvm_p == NULL) {
856 vlan_putref_linkmib(mib, &psref);
857 curlwp_bindx(bound);
858 error = EINVAL;
859 } else if (
860 ifr->ifr_mtu > (mib->ifvm_p->if_mtu - mib->ifvm_mtufudge) ||
861 ifr->ifr_mtu < (mib->ifvm_mintu - mib->ifvm_mtufudge)) {
862 vlan_putref_linkmib(mib, &psref);
863 curlwp_bindx(bound);
864 error = EINVAL;
865 } else {
866 vlan_putref_linkmib(mib, &psref);
867 curlwp_bindx(bound);
868
869 error = ifioctl_common(ifp, cmd, data);
870 if (error == ENETRESET)
871 error = 0;
872 }
873
874 break;
875
876 case SIOCSETVLAN:
877 if ((error = kauth_authorize_network(l->l_cred,
878 KAUTH_NETWORK_INTERFACE,
879 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
880 NULL)) != 0)
881 break;
882 if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
883 break;
884
885 if (vlr.vlr_parent[0] == '\0') {
886 bound = curlwp_bind();
887 mib = vlan_getref_linkmib(ifv, &psref);
888 if (mib == NULL) {
889 curlwp_bindx(bound);
890 error = EBUSY;
891 break;
892 }
893
894 if (mib->ifvm_p != NULL &&
895 (ifp->if_flags & IFF_PROMISC) != 0)
896 error = ifpromisc(mib->ifvm_p, 0);
897
898 vlan_putref_linkmib(mib, &psref);
899 curlwp_bindx(bound);
900
901 vlan_unconfig(ifp);
902 break;
903 }
904 if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
905 error = EINVAL; /* check for valid tag */
906 break;
907 }
908 if ((pr = ifunit(vlr.vlr_parent)) == 0) {
909 error = ENOENT;
910 break;
911 }
912 error = vlan_config(ifv, pr, vlr.vlr_tag);
913 if (error != 0) {
914 break;
915 }
916 ifp->if_flags |= IFF_RUNNING;
917
918 /* Update promiscuous mode, if necessary. */
919 vlan_set_promisc(ifp);
920 break;
921
922 case SIOCGETVLAN:
923 memset(&vlr, 0, sizeof(vlr));
924 bound = curlwp_bind();
925 mib = vlan_getref_linkmib(ifv, &psref);
926 if (mib == NULL) {
927 curlwp_bindx(bound);
928 error = EBUSY;
929 break;
930 }
931 if (mib->ifvm_p != NULL) {
932 snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
933 mib->ifvm_p->if_xname);
934 vlr.vlr_tag = mib->ifvm_tag;
935 }
936 vlan_putref_linkmib(mib, &psref);
937 curlwp_bindx(bound);
938 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
939 break;
940
941 case SIOCSIFFLAGS:
942 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
943 break;
944 /*
945 * For promiscuous mode, we enable promiscuous mode on
946 * the parent if we need promiscuous on the VLAN interface.
947 */
948 bound = curlwp_bind();
949 mib = vlan_getref_linkmib(ifv, &psref);
950 if (mib == NULL) {
951 curlwp_bindx(bound);
952 error = EBUSY;
953 break;
954 }
955
956 if (mib->ifvm_p != NULL)
957 error = vlan_set_promisc(ifp);
958 vlan_putref_linkmib(mib, &psref);
959 curlwp_bindx(bound);
960 break;
961
962 case SIOCADDMULTI:
963 mutex_enter(&ifv->ifv_lock);
964 mib = ifv->ifv_mib;
965 if (mib == NULL) {
966 error = EBUSY;
967 mutex_exit(&ifv->ifv_lock);
968 break;
969 }
970
971 error = (mib->ifvm_p != NULL) ?
972 (*mib->ifvm_msw->vmsw_addmulti)(ifv, ifr) : EINVAL;
973 mib = NULL;
974 mutex_exit(&ifv->ifv_lock);
975 break;
976
977 case SIOCDELMULTI:
978 mutex_enter(&ifv->ifv_lock);
979 mib = ifv->ifv_mib;
980 if (mib == NULL) {
981 error = EBUSY;
982 mutex_exit(&ifv->ifv_lock);
983 break;
984 }
985 error = (mib->ifvm_p != NULL) ?
986 (*mib->ifvm_msw->vmsw_delmulti)(ifv, ifr) : EINVAL;
987 mib = NULL;
988 mutex_exit(&ifv->ifv_lock);
989 break;
990
991 case SIOCSIFCAP:
992 ifcr = data;
993 /* make sure caps are enabled on parent */
994 bound = curlwp_bind();
995 mib = vlan_getref_linkmib(ifv, &psref);
996 if (mib == NULL) {
997 curlwp_bindx(bound);
998 error = EBUSY;
999 break;
1000 }
1001
1002 if (mib->ifvm_p == NULL) {
1003 vlan_putref_linkmib(mib, &psref);
1004 curlwp_bindx(bound);
1005 error = EINVAL;
1006 break;
1007 }
1008 if ((mib->ifvm_p->if_capenable & ifcr->ifcr_capenable) !=
1009 ifcr->ifcr_capenable) {
1010 vlan_putref_linkmib(mib, &psref);
1011 curlwp_bindx(bound);
1012 error = EINVAL;
1013 break;
1014 }
1015
1016 vlan_putref_linkmib(mib, &psref);
1017 curlwp_bindx(bound);
1018
1019 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
1020 error = 0;
1021 break;
1022 case SIOCINITIFADDR:
1023 bound = curlwp_bind();
1024 mib = vlan_getref_linkmib(ifv, &psref);
1025 if (mib == NULL) {
1026 curlwp_bindx(bound);
1027 error = EBUSY;
1028 break;
1029 }
1030
1031 if (mib->ifvm_p == NULL) {
1032 error = EINVAL;
1033 vlan_putref_linkmib(mib, &psref);
1034 curlwp_bindx(bound);
1035 break;
1036 }
1037 vlan_putref_linkmib(mib, &psref);
1038 curlwp_bindx(bound);
1039
1040 ifp->if_flags |= IFF_UP;
1041 #ifdef INET
1042 if (ifa->ifa_addr->sa_family == AF_INET)
1043 arp_ifinit(ifp, ifa);
1044 #endif
1045 break;
1046
1047 default:
1048 error = ether_ioctl(ifp, cmd, data);
1049 }
1050
1051 return (error);
1052 }
1053
1054 static int
1055 vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
1056 {
1057 const struct sockaddr *sa = ifreq_getaddr(SIOCADDMULTI, ifr);
1058 struct vlan_mc_entry *mc;
1059 uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
1060 struct ifvlan_linkmib *mib;
1061 int error;
1062
1063 KASSERT(mutex_owned(&ifv->ifv_lock));
1064
1065 if (sa->sa_len > sizeof(struct sockaddr_storage))
1066 return (EINVAL);
1067
1068 error = ether_addmulti(sa, &ifv->ifv_ec);
1069 if (error != ENETRESET)
1070 return (error);
1071
1072 /*
1073 * This is new multicast address. We have to tell parent
1074 * about it. Also, remember this multicast address so that
1075 * we can delete them on unconfigure.
1076 */
1077 mc = malloc(sizeof(struct vlan_mc_entry), M_DEVBUF, M_NOWAIT);
1078 if (mc == NULL) {
1079 error = ENOMEM;
1080 goto alloc_failed;
1081 }
1082
1083 /*
1084 * As ether_addmulti() returns ENETRESET, following two
1085 * statement shouldn't fail.
1086 */
1087 (void)ether_multiaddr(sa, addrlo, addrhi);
1088 ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, mc->mc_enm);
1089 memcpy(&mc->mc_addr, sa, sa->sa_len);
1090 LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
1091
1092 mib = ifv->ifv_mib;
1093 error = if_mcast_op(mib->ifvm_p, SIOCADDMULTI, sa);
1094
1095 if (error != 0)
1096 goto ioctl_failed;
1097 return (error);
1098
1099 ioctl_failed:
1100 LIST_REMOVE(mc, mc_entries);
1101 free(mc, M_DEVBUF);
1102 alloc_failed:
1103 (void)ether_delmulti(sa, &ifv->ifv_ec);
1104 return (error);
1105 }
1106
1107 static int
1108 vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
1109 {
1110 const struct sockaddr *sa = ifreq_getaddr(SIOCDELMULTI, ifr);
1111 struct ether_multi *enm;
1112 struct vlan_mc_entry *mc;
1113 struct ifvlan_linkmib *mib;
1114 uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
1115 int error;
1116
1117 KASSERT(mutex_owned(&ifv->ifv_lock));
1118
1119 /*
1120 * Find a key to lookup vlan_mc_entry. We have to do this
1121 * before calling ether_delmulti for obvious reason.
1122 */
1123 if ((error = ether_multiaddr(sa, addrlo, addrhi)) != 0)
1124 return (error);
1125 ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, enm);
1126
1127 error = ether_delmulti(sa, &ifv->ifv_ec);
1128 if (error != ENETRESET)
1129 return (error);
1130
1131 /* We no longer use this multicast address. Tell parent so. */
1132 mib = ifv->ifv_mib;
1133 error = if_mcast_op(mib->ifvm_p, SIOCDELMULTI, sa);
1134
1135 if (error == 0) {
1136 /* And forget about this address. */
1137 for (mc = LIST_FIRST(&ifv->ifv_mc_listhead); mc != NULL;
1138 mc = LIST_NEXT(mc, mc_entries)) {
1139 if (mc->mc_enm == enm) {
1140 LIST_REMOVE(mc, mc_entries);
1141 free(mc, M_DEVBUF);
1142 break;
1143 }
1144 }
1145 KASSERT(mc != NULL);
1146 } else
1147 (void)ether_addmulti(sa, &ifv->ifv_ec);
1148 return (error);
1149 }
1150
1151 /*
1152 * Delete any multicast address we have asked to add from parent
1153 * interface. Called when the vlan is being unconfigured.
1154 */
1155 static void
1156 vlan_ether_purgemulti(struct ifvlan *ifv)
1157 {
1158 struct vlan_mc_entry *mc;
1159 struct ifvlan_linkmib *mib;
1160
1161 KASSERT(mutex_owned(&ifv->ifv_lock));
1162 mib = ifv->ifv_mib;
1163 if (mib == NULL) {
1164 return;
1165 }
1166
1167 while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
1168 (void)if_mcast_op(mib->ifvm_p, SIOCDELMULTI,
1169 (const struct sockaddr *)&mc->mc_addr);
1170 LIST_REMOVE(mc, mc_entries);
1171 free(mc, M_DEVBUF);
1172 }
1173 }
1174
1175 static void
1176 vlan_start(struct ifnet *ifp)
1177 {
1178 struct ifvlan *ifv = ifp->if_softc;
1179 struct ifnet *p;
1180 struct ethercom *ec;
1181 struct mbuf *m;
1182 struct ifvlan_linkmib *mib;
1183 struct psref psref;
1184 int error;
1185
1186 #ifndef NET_MPSAFE
1187 KASSERT(KERNEL_LOCKED_P());
1188 #endif
1189
1190 mib = vlan_getref_linkmib(ifv, &psref);
1191 if (mib == NULL)
1192 return;
1193 p = mib->ifvm_p;
1194 ec = (void *)mib->ifvm_p;
1195
1196 ifp->if_flags |= IFF_OACTIVE;
1197
1198 for (;;) {
1199 IFQ_DEQUEUE(&ifp->if_snd, m);
1200 if (m == NULL)
1201 break;
1202
1203 #ifdef ALTQ
1204 /*
1205 * KERNEL_LOCK is required for ALTQ even if NET_MPSAFE is defined.
1206 */
1207 KERNEL_LOCK(1, NULL);
1208 /*
1209 * If ALTQ is enabled on the parent interface, do
1210 * classification; the queueing discipline might
1211 * not require classification, but might require
1212 * the address family/header pointer in the pktattr.
1213 */
1214 if (ALTQ_IS_ENABLED(&p->if_snd)) {
1215 switch (p->if_type) {
1216 case IFT_ETHER:
1217 altq_etherclassify(&p->if_snd, m);
1218 break;
1219 #ifdef DIAGNOSTIC
1220 default:
1221 panic("vlan_start: impossible (altq)");
1222 #endif
1223 }
1224 }
1225 KERNEL_UNLOCK_ONE(NULL);
1226 #endif /* ALTQ */
1227
1228 bpf_mtap(ifp, m);
1229 /*
1230 * If the parent can insert the tag itself, just mark
1231 * the tag in the mbuf header.
1232 */
1233 if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
1234 vlan_set_tag(m, mib->ifvm_tag);
1235 } else {
1236 /*
1237 * insert the tag ourselves
1238 */
1239 M_PREPEND(m, mib->ifvm_encaplen, M_DONTWAIT);
1240 if (m == NULL) {
1241 printf("%s: unable to prepend encap header",
1242 p->if_xname);
1243 ifp->if_oerrors++;
1244 continue;
1245 }
1246
1247 switch (p->if_type) {
1248 case IFT_ETHER:
1249 {
1250 struct ether_vlan_header *evl;
1251
1252 if (m->m_len < sizeof(struct ether_vlan_header))
1253 m = m_pullup(m,
1254 sizeof(struct ether_vlan_header));
1255 if (m == NULL) {
1256 printf("%s: unable to pullup encap "
1257 "header", p->if_xname);
1258 ifp->if_oerrors++;
1259 continue;
1260 }
1261
1262 /*
1263 * Transform the Ethernet header into an
1264 * Ethernet header with 802.1Q encapsulation.
1265 */
1266 memmove(mtod(m, void *),
1267 mtod(m, char *) + mib->ifvm_encaplen,
1268 sizeof(struct ether_header));
1269 evl = mtod(m, struct ether_vlan_header *);
1270 evl->evl_proto = evl->evl_encap_proto;
1271 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1272 evl->evl_tag = htons(mib->ifvm_tag);
1273
1274 /*
1275 * To cater for VLAN-aware layer 2 ethernet
1276 * switches which may need to strip the tag
1277 * before forwarding the packet, make sure
1278 * the packet+tag is at least 68 bytes long.
1279 * This is necessary because our parent will
1280 * only pad to 64 bytes (ETHER_MIN_LEN) and
1281 * some switches will not pad by themselves
1282 * after deleting a tag.
1283 */
1284 if (m->m_pkthdr.len <
1285 (ETHER_MIN_LEN - ETHER_CRC_LEN +
1286 ETHER_VLAN_ENCAP_LEN)) {
1287 m_copyback(m, m->m_pkthdr.len,
1288 (ETHER_MIN_LEN - ETHER_CRC_LEN +
1289 ETHER_VLAN_ENCAP_LEN) -
1290 m->m_pkthdr.len,
1291 vlan_zero_pad_buff);
1292 }
1293 break;
1294 }
1295
1296 #ifdef DIAGNOSTIC
1297 default:
1298 panic("vlan_start: impossible");
1299 #endif
1300 }
1301 }
1302
1303 if ((p->if_flags & IFF_RUNNING) == 0) {
1304 m_freem(m);
1305 continue;
1306 }
1307
1308 error = if_transmit_lock(p, m);
1309 if (error) {
1310 /* mbuf is already freed */
1311 ifp->if_oerrors++;
1312 continue;
1313 }
1314 ifp->if_opackets++;
1315 }
1316
1317 ifp->if_flags &= ~IFF_OACTIVE;
1318
1319 /* Remove reference to mib before release */
1320 p = NULL;
1321 ec = NULL;
1322
1323 vlan_putref_linkmib(mib, &psref);
1324 }
1325
1326 static int
1327 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1328 {
1329 struct ifvlan *ifv = ifp->if_softc;
1330 struct ifnet *p;
1331 struct ethercom *ec;
1332 struct ifvlan_linkmib *mib;
1333 struct psref psref;
1334 int error;
1335 size_t pktlen = m->m_pkthdr.len;
1336 bool mcast = (m->m_flags & M_MCAST) != 0;
1337
1338 mib = vlan_getref_linkmib(ifv, &psref);
1339 if (mib == NULL) {
1340 m_freem(m);
1341 return ENETDOWN;
1342 }
1343
1344 p = mib->ifvm_p;
1345 ec = (void *)mib->ifvm_p;
1346
1347 bpf_mtap(ifp, m);
1348 /*
1349 * If the parent can insert the tag itself, just mark
1350 * the tag in the mbuf header.
1351 */
1352 if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
1353 vlan_set_tag(m, mib->ifvm_tag);
1354 } else {
1355 /*
1356 * insert the tag ourselves
1357 */
1358 M_PREPEND(m, mib->ifvm_encaplen, M_DONTWAIT);
1359 if (m == NULL) {
1360 printf("%s: unable to prepend encap header",
1361 p->if_xname);
1362 ifp->if_oerrors++;
1363 error = ENOBUFS;
1364 goto out;
1365 }
1366
1367 switch (p->if_type) {
1368 case IFT_ETHER:
1369 {
1370 struct ether_vlan_header *evl;
1371
1372 if (m->m_len < sizeof(struct ether_vlan_header))
1373 m = m_pullup(m,
1374 sizeof(struct ether_vlan_header));
1375 if (m == NULL) {
1376 printf("%s: unable to pullup encap "
1377 "header", p->if_xname);
1378 ifp->if_oerrors++;
1379 error = ENOBUFS;
1380 goto out;
1381 }
1382
1383 /*
1384 * Transform the Ethernet header into an
1385 * Ethernet header with 802.1Q encapsulation.
1386 */
1387 memmove(mtod(m, void *),
1388 mtod(m, char *) + mib->ifvm_encaplen,
1389 sizeof(struct ether_header));
1390 evl = mtod(m, struct ether_vlan_header *);
1391 evl->evl_proto = evl->evl_encap_proto;
1392 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1393 evl->evl_tag = htons(mib->ifvm_tag);
1394
1395 /*
1396 * To cater for VLAN-aware layer 2 ethernet
1397 * switches which may need to strip the tag
1398 * before forwarding the packet, make sure
1399 * the packet+tag is at least 68 bytes long.
1400 * This is necessary because our parent will
1401 * only pad to 64 bytes (ETHER_MIN_LEN) and
1402 * some switches will not pad by themselves
1403 * after deleting a tag.
1404 */
1405 if (m->m_pkthdr.len <
1406 (ETHER_MIN_LEN - ETHER_CRC_LEN +
1407 ETHER_VLAN_ENCAP_LEN)) {
1408 m_copyback(m, m->m_pkthdr.len,
1409 (ETHER_MIN_LEN - ETHER_CRC_LEN +
1410 ETHER_VLAN_ENCAP_LEN) -
1411 m->m_pkthdr.len,
1412 vlan_zero_pad_buff);
1413 }
1414 break;
1415 }
1416
1417 #ifdef DIAGNOSTIC
1418 default:
1419 panic("vlan_transmit: impossible");
1420 #endif
1421 }
1422 }
1423
1424 if ((p->if_flags & IFF_RUNNING) == 0) {
1425 m_freem(m);
1426 error = ENETDOWN;
1427 goto out;
1428 }
1429
1430 error = if_transmit_lock(p, m);
1431 if (error) {
1432 /* mbuf is already freed */
1433 ifp->if_oerrors++;
1434 } else {
1435
1436 ifp->if_opackets++;
1437 ifp->if_obytes += pktlen;
1438 if (mcast)
1439 ifp->if_omcasts++;
1440 }
1441
1442 out:
1443 /* Remove reference to mib before release */
1444 p = NULL;
1445 ec = NULL;
1446
1447 vlan_putref_linkmib(mib, &psref);
1448 return error;
1449 }
1450
1451 /*
1452 * Given an Ethernet frame, find a valid vlan interface corresponding to the
1453 * given source interface and tag, then run the real packet through the
1454 * parent's input routine.
1455 */
1456 void
1457 vlan_input(struct ifnet *ifp, struct mbuf *m)
1458 {
1459 struct ifvlan *ifv;
1460 u_int tag;
1461 struct ifvlan_linkmib *mib;
1462 struct psref psref;
1463 bool have_vtag;
1464
1465 have_vtag = vlan_has_tag(m);
1466 if (have_vtag) {
1467 tag = EVL_VLANOFTAG(vlan_get_tag(m));
1468 m->m_flags &= ~M_VLANTAG;
1469 } else {
1470 switch (ifp->if_type) {
1471 case IFT_ETHER:
1472 {
1473 struct ether_vlan_header *evl;
1474
1475 if (m->m_len < sizeof(struct ether_vlan_header) &&
1476 (m = m_pullup(m,
1477 sizeof(struct ether_vlan_header))) == NULL) {
1478 printf("%s: no memory for VLAN header, "
1479 "dropping packet.\n", ifp->if_xname);
1480 return;
1481 }
1482 evl = mtod(m, struct ether_vlan_header *);
1483 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
1484
1485 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
1486
1487 /*
1488 * Restore the original ethertype. We'll remove
1489 * the encapsulation after we've found the vlan
1490 * interface corresponding to the tag.
1491 */
1492 evl->evl_encap_proto = evl->evl_proto;
1493 break;
1494 }
1495
1496 default:
1497 tag = (u_int) -1; /* XXX GCC */
1498 #ifdef DIAGNOSTIC
1499 panic("vlan_input: impossible");
1500 #endif
1501 }
1502 }
1503
1504 mib = vlan_lookup_tag_psref(ifp, tag, &psref);
1505 if (mib == NULL) {
1506 m_freem(m);
1507 ifp->if_noproto++;
1508 return;
1509 }
1510
1511 ifv = mib->ifvm_ifvlan;
1512 if ((ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
1513 (IFF_UP|IFF_RUNNING)) {
1514 m_freem(m);
1515 ifp->if_noproto++;
1516 goto out;
1517 }
1518
1519 /*
1520 * Now, remove the encapsulation header. The original
1521 * header has already been fixed up above.
1522 */
1523 if (!have_vtag) {
1524 memmove(mtod(m, char *) + mib->ifvm_encaplen,
1525 mtod(m, void *), sizeof(struct ether_header));
1526 m_adj(m, mib->ifvm_encaplen);
1527 }
1528
1529 m_set_rcvif(m, &ifv->ifv_if);
1530 ifv->ifv_if.if_ipackets++;
1531
1532 m->m_flags &= ~M_PROMISC;
1533 if_input(&ifv->ifv_if, m);
1534 out:
1535 vlan_putref_linkmib(mib, &psref);
1536 }
1537
1538 /*
1539 * Module infrastructure
1540 */
1541 #include "if_module.h"
1542
1543 IF_MODULE(MODULE_CLASS_DRIVER, vlan, "")
1544