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