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