2 * Copyright (c) 2003-2014 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright 1998 Massachusetts Institute of Technology
31 * Permission to use, copy, modify, and distribute this software and
32 * its documentation for any purpose and without fee is hereby
33 * granted, provided that both the above copyright notice and this
34 * permission notice appear in all copies, that both the above
35 * copyright notice and this permission notice appear in all
36 * supporting documentation, and that the name of M.I.T. not be used
37 * in advertising or publicity pertaining to distribution of the
38 * software without specific, written prior permission. M.I.T. makes
39 * no representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied
43 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
44 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
45 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
46 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
47 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
50 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
51 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
53 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * $FreeBSD: src/sys/net/if_vlan.c,v 1.54 2003/10/31 18:32:08 brooks Exp $
60 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
61 * Might be extended some day to also handle IEEE 802.1p priority
62 * tagging. This is sort of sneaky in the implementation, since
63 * we need to pretend to be enough of an Ethernet implementation
64 * to make arp work. The way we do this is by telling everyone
65 * that we are an Ethernet, and then catch the packets that
66 * ether_output() left on our output queue when it calls
67 * if_start(), rewrite them for use by the real outgoing interface,
68 * and ask it to send them.
72 #include <sys/param.h>
73 #include <sys/kernel.h>
74 #include <sys/malloc.h>
76 #include <sys/queue.h>
77 #include <sys/socket.h>
78 #include <sys/sockio.h>
79 #include <sys/sysctl.h>
80 #include <sys/systm.h>
81 #include <sys/kern_event.h>
82 #include <sys/mcache.h>
85 #include <net/ethernet.h>
87 #include <net/if_arp.h>
88 #include <net/if_dl.h>
89 #include <net/if_ether.h>
90 #include <net/if_types.h>
91 #include <net/if_vlan_var.h>
92 #include <libkern/OSAtomic.h>
96 #include <net/kpi_interface.h>
97 #include <net/kpi_protocol.h>
99 #include <kern/locks.h>
102 #include <netinet/in.h>
103 #include <netinet/if_ether.h>
106 #include <net/if_media.h>
107 #include <net/multicast_list.h>
108 #include <net/ether_if_module.h>
110 #define VLANNAME "vlan"
112 typedef int (bpf_callback_func
)(struct ifnet
*, struct mbuf
*);
113 typedef int (if_set_bpf_tap_func
)(struct ifnet
*ifp
, int mode
, bpf_callback_func
* func
);
118 static __inline__ lck_grp_t
*
119 my_lck_grp_alloc_init(const char * grp_name
)
122 lck_grp_attr_t
* grp_attrs
;
124 grp_attrs
= lck_grp_attr_alloc_init();
125 grp
= lck_grp_alloc_init(grp_name
, grp_attrs
);
126 lck_grp_attr_free(grp_attrs
);
130 static __inline__ lck_mtx_t
*
131 my_lck_mtx_alloc_init(lck_grp_t
* lck_grp
)
133 lck_attr_t
* lck_attrs
;
136 lck_attrs
= lck_attr_alloc_init();
137 lck_mtx
= lck_mtx_alloc_init(lck_grp
, lck_attrs
);
138 lck_attr_free(lck_attrs
);
142 static lck_mtx_t
* vlan_lck_mtx
;
144 static __inline__
void
147 lck_grp_t
* vlan_lck_grp
;
149 vlan_lck_grp
= my_lck_grp_alloc_init("if_vlan");
150 vlan_lck_mtx
= my_lck_mtx_alloc_init(vlan_lck_grp
);
153 static __inline__
void
154 vlan_assert_lock_held(void)
156 lck_mtx_assert(vlan_lck_mtx
, LCK_MTX_ASSERT_OWNED
);
160 static __inline__
void
161 vlan_assert_lock_not_held(void)
163 lck_mtx_assert(vlan_lck_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
167 static __inline__
void
170 lck_mtx_lock(vlan_lck_mtx
);
174 static __inline__
void
177 lck_mtx_unlock(vlan_lck_mtx
);
182 ** vlan structures, types
185 LIST_HEAD(vlan_parent_list
, vlan_parent
);
187 LIST_HEAD(ifvlan_list
, ifvlan
);
189 typedef LIST_ENTRY(vlan_parent
)
191 typedef LIST_ENTRY(ifvlan
)
194 #define VLP_SIGNATURE 0xfaceface
195 typedef struct vlan_parent
{
196 vlan_parent_entry vlp_parent_list
;/* list of parents */
197 struct ifnet
* vlp_ifp
; /* interface */
198 struct ifvlan_list vlp_vlan_list
; /* list of VLAN's */
199 #define VLPF_SUPPORTS_VLAN_MTU 0x00000001
200 #define VLPF_CHANGE_IN_PROGRESS 0x00000002
201 #define VLPF_DETACHING 0x00000004
202 #define VLPF_LINK_EVENT_REQUIRED 0x00000008
204 u_int32_t vlp_event_code
;
205 struct ifdevmtu vlp_devmtu
;
206 int32_t vlp_retain_count
;
207 u_int32_t vlp_signature
; /* VLP_SIGNATURE */
208 } vlan_parent
, * vlan_parent_ref
;
210 #define IFV_SIGNATURE 0xbeefbeef
212 ifvlan_entry ifv_vlan_list
;
213 char ifv_name
[IFNAMSIZ
]; /* our unique id */
214 struct ifnet
* ifv_ifp
; /* our interface */
215 vlan_parent_ref ifv_vlp
; /* parent information */
217 u_int16_t ifvm_encaplen
;/* encapsulation length */
218 u_int16_t ifvm_mtufudge
;/* MTU fudged by this much */
219 u_int16_t ifvm_proto
; /* encapsulation ethertype */
220 u_int16_t ifvm_tag
; /* tag to apply on packets leaving if */
222 struct multicast_list ifv_multicast
;
223 #define IFVF_PROMISC 0x1 /* promiscuous mode enabled */
224 #define IFVF_DETACHING 0x2 /* interface is detaching */
225 #define IFVF_READY 0x4 /* interface is ready */
227 bpf_packet_func ifv_bpf_input
;
228 bpf_packet_func ifv_bpf_output
;
229 int32_t ifv_retain_count
;
230 u_int32_t ifv_signature
; /* IFV_SIGNATURE */
233 typedef struct ifvlan
* ifvlan_ref
;
235 typedef struct vlan_globals_s
{
236 struct vlan_parent_list parent_list
;
238 } * vlan_globals_ref
;
240 static vlan_globals_ref g_vlan
;
242 #define ifv_tag ifv_mib.ifvm_tag
243 #define ifv_encaplen ifv_mib.ifvm_encaplen
244 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
247 vlan_parent_retain(vlan_parent_ref vlp
);
250 vlan_parent_release(vlan_parent_ref vlp
);
253 ** vlan_parent_ref vlp_flags in-lines
255 static __inline__
int
256 vlan_parent_flags_supports_vlan_mtu(vlan_parent_ref vlp
)
258 return ((vlp
->vlp_flags
& VLPF_SUPPORTS_VLAN_MTU
) != 0);
261 static __inline__
void
262 vlan_parent_flags_set_supports_vlan_mtu(vlan_parent_ref vlp
)
264 vlp
->vlp_flags
|= VLPF_SUPPORTS_VLAN_MTU
;
268 static __inline__
int
269 vlan_parent_flags_change_in_progress(vlan_parent_ref vlp
)
271 return ((vlp
->vlp_flags
& VLPF_CHANGE_IN_PROGRESS
) != 0);
274 static __inline__
void
275 vlan_parent_flags_set_change_in_progress(vlan_parent_ref vlp
)
277 vlp
->vlp_flags
|= VLPF_CHANGE_IN_PROGRESS
;
281 static __inline__
void
282 vlan_parent_flags_clear_change_in_progress(vlan_parent_ref vlp
)
284 vlp
->vlp_flags
&= ~VLPF_CHANGE_IN_PROGRESS
;
288 static __inline__
int
289 vlan_parent_flags_detaching(struct vlan_parent
* vlp
)
291 return ((vlp
->vlp_flags
& VLPF_DETACHING
) != 0);
294 static __inline__
void
295 vlan_parent_flags_set_detaching(struct vlan_parent
* vlp
)
297 vlp
->vlp_flags
|= VLPF_DETACHING
;
301 static __inline__
int
302 vlan_parent_flags_link_event_required(vlan_parent_ref vlp
)
304 return ((vlp
->vlp_flags
& VLPF_LINK_EVENT_REQUIRED
) != 0);
307 static __inline__
void
308 vlan_parent_flags_set_link_event_required(vlan_parent_ref vlp
)
310 vlp
->vlp_flags
|= VLPF_LINK_EVENT_REQUIRED
;
314 static __inline__
void
315 vlan_parent_flags_clear_link_event_required(vlan_parent_ref vlp
)
317 vlp
->vlp_flags
&= ~VLPF_LINK_EVENT_REQUIRED
;
323 ** ifvlan_flags in-lines routines
325 static __inline__
int
326 ifvlan_flags_promisc(ifvlan_ref ifv
)
328 return ((ifv
->ifv_flags
& IFVF_PROMISC
) != 0);
331 static __inline__
void
332 ifvlan_flags_set_promisc(ifvlan_ref ifv
)
334 ifv
->ifv_flags
|= IFVF_PROMISC
;
338 static __inline__
void
339 ifvlan_flags_clear_promisc(ifvlan_ref ifv
)
341 ifv
->ifv_flags
&= ~IFVF_PROMISC
;
345 static __inline__
int
346 ifvlan_flags_ready(ifvlan_ref ifv
)
348 return ((ifv
->ifv_flags
& IFVF_READY
) != 0);
351 static __inline__
void
352 ifvlan_flags_set_ready(ifvlan_ref ifv
)
354 ifv
->ifv_flags
|= IFVF_READY
;
358 static __inline__
int
359 ifvlan_flags_detaching(ifvlan_ref ifv
)
361 return ((ifv
->ifv_flags
& IFVF_DETACHING
) != 0);
364 static __inline__
void
365 ifvlan_flags_set_detaching(ifvlan_ref ifv
)
367 ifv
->ifv_flags
|= IFVF_DETACHING
;
372 SYSCTL_DECL(_net_link
);
373 SYSCTL_NODE(_net_link
, IFT_L2VLAN
, vlan
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "IEEE 802.1Q VLAN");
374 SYSCTL_NODE(_net_link_vlan
, PF_LINK
, link
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "for consistency");
377 #define M_VLAN M_DEVBUF
379 static int vlan_clone_create(struct if_clone
*, u_int32_t
, void *);
380 static int vlan_clone_destroy(struct ifnet
*);
381 static int vlan_input(ifnet_t ifp
, protocol_family_t protocol
,
382 mbuf_t m
, char *frame_header
);
383 static int vlan_output(struct ifnet
*ifp
, struct mbuf
*m
);
384 static int vlan_ioctl(ifnet_t ifp
, u_long cmd
, void * addr
);
385 static int vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
,
386 bpf_packet_func func
);
387 static int vlan_attach_protocol(struct ifnet
*ifp
);
388 static int vlan_detach_protocol(struct ifnet
*ifp
);
389 static int vlan_setmulti(struct ifnet
*ifp
);
390 static int vlan_unconfig(ifvlan_ref ifv
, int need_to_wait
);
391 static int vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
);
392 static void vlan_if_free(struct ifnet
* ifp
);
393 static int vlan_remove(ifvlan_ref ifv
, int need_to_wait
);
395 static struct if_clone vlan_cloner
= IF_CLONE_INITIALIZER(VLANNAME
,
400 static void interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
);
401 static void vlan_parent_link_event(struct ifnet
* p
,
402 u_int32_t event_code
);
404 static int ifvlan_new_mtu(ifvlan_ref ifv
, int mtu
);
407 ** ifvlan_ref routines
410 ifvlan_retain(ifvlan_ref ifv
)
412 if (ifv
->ifv_signature
!= IFV_SIGNATURE
) {
413 panic("ifvlan_retain: bad signature\n");
415 if (ifv
->ifv_retain_count
== 0) {
416 panic("ifvlan_retain: retain count is 0\n");
418 OSIncrementAtomic(&ifv
->ifv_retain_count
);
422 ifvlan_release(ifvlan_ref ifv
)
424 u_int32_t old_retain_count
;
426 if (ifv
->ifv_signature
!= IFV_SIGNATURE
) {
427 panic("ifvlan_release: bad signature\n");
429 old_retain_count
= OSDecrementAtomic(&ifv
->ifv_retain_count
);
430 switch (old_retain_count
) {
432 panic("ifvlan_release: retain count is 0\n");
435 if (g_vlan
->verbose
) {
436 printf("ifvlan_release(%s)\n", ifv
->ifv_name
);
438 ifv
->ifv_signature
= 0;
447 static vlan_parent_ref
448 ifvlan_get_vlan_parent_retained(ifvlan_ref ifv
)
450 vlan_parent_ref vlp
= ifv
->ifv_vlp
;
452 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
455 vlan_parent_retain(vlp
);
464 ifnet_get_ifvlan(struct ifnet
* ifp
)
468 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
473 ifnet_get_ifvlan_retained(struct ifnet
* ifp
)
477 ifv
= ifnet_get_ifvlan(ifp
);
481 if (ifvlan_flags_detaching(ifv
)) {
489 ifnet_ifvlan_vlan_parent_ok(struct ifnet
* ifp
, ifvlan_ref ifv
,
492 ifvlan_ref check_ifv
;
494 check_ifv
= ifnet_get_ifvlan(ifp
);
495 if (check_ifv
!= ifv
|| ifvlan_flags_detaching(ifv
)) {
496 /* ifvlan_ref no longer valid */
499 if (ifv
->ifv_vlp
!= vlp
) {
500 /* vlan_parent no longer valid */
503 if (vlan_parent_flags_detaching(vlp
)) {
504 /* parent is detaching */
511 ** vlan, etc. routines
515 vlan_globals_init(void)
519 vlan_assert_lock_not_held();
521 if (g_vlan
!= NULL
) {
524 v
= _MALLOC(sizeof(*v
), M_VLAN
, M_WAITOK
);
526 LIST_INIT(&v
->parent_list
);
530 if (g_vlan
!= NULL
) {
546 siocgifdevmtu(struct ifnet
* ifp
, struct ifdevmtu
* ifdm_p
)
551 bzero(&ifr
, sizeof(ifr
));
552 error
= ifnet_ioctl(ifp
, 0,SIOCGIFDEVMTU
, &ifr
);
554 *ifdm_p
= ifr
.ifr_devmtu
;
560 siocsifaltmtu(struct ifnet
* ifp
, int mtu
)
564 bzero(&ifr
, sizeof(ifr
));
566 return (ifnet_ioctl(ifp
, 0, SIOCSIFALTMTU
, &ifr
));
569 static __inline__
void
570 vlan_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
571 bpf_packet_func func
)
579 static __inline__
void
580 vlan_bpf_input(struct ifnet
* ifp
, struct mbuf
* m
,
581 bpf_packet_func func
, char * frame_header
,
582 int frame_header_len
, int encap_len
)
586 /* present the right header to bpf */
587 bcopy(frame_header
, frame_header
+ encap_len
, frame_header_len
);
589 m
->m_data
-= frame_header_len
;
590 m
->m_len
+= frame_header_len
;
592 m
->m_data
+= frame_header_len
;
593 m
->m_len
-= frame_header_len
;
595 /* restore the header */
596 bcopy(frame_header
+ encap_len
, frame_header
, frame_header_len
);
603 ** vlan_parent synchronization routines
606 vlan_parent_retain(vlan_parent_ref vlp
)
608 if (vlp
->vlp_signature
!= VLP_SIGNATURE
) {
609 panic("vlan_parent_retain: signature is bad\n");
611 if (vlp
->vlp_retain_count
== 0) {
612 panic("vlan_parent_retain: retain count is 0\n");
614 OSIncrementAtomic(&vlp
->vlp_retain_count
);
618 vlan_parent_release(vlan_parent_ref vlp
)
620 u_int32_t old_retain_count
;
622 if (vlp
->vlp_signature
!= VLP_SIGNATURE
) {
623 panic("vlan_parent_release: signature is bad\n");
625 old_retain_count
= OSDecrementAtomic(&vlp
->vlp_retain_count
);
626 switch (old_retain_count
) {
628 panic("vlan_parent_release: retain count is 0\n");
631 if (g_vlan
->verbose
) {
632 struct ifnet
* ifp
= vlp
->vlp_ifp
;
633 printf("vlan_parent_release(%s%d)\n", ifnet_name(ifp
),
636 vlp
->vlp_signature
= 0;
646 * Function: vlan_parent_wait
648 * Allows a single thread to gain exclusive access to the vlan_parent
649 * data structure. Some operations take a long time to complete,
650 * and some have side-effects that we can't predict. Holding the
651 * vlan_lock() across such operations is not possible.
654 * Before calling, you must be holding the vlan_lock and have taken
655 * a reference on the vlan_parent_ref.
658 vlan_parent_wait(vlan_parent_ref vlp
, const char * msg
)
662 /* other add/remove/multicast-change in progress */
663 while (vlan_parent_flags_change_in_progress(vlp
)) {
664 if (g_vlan
->verbose
) {
665 struct ifnet
* ifp
= vlp
->vlp_ifp
;
667 printf("%s%d: %s msleep\n", ifnet_name(ifp
), ifnet_unit(ifp
), msg
);
670 (void)msleep(vlp
, vlan_lck_mtx
, PZERO
, msg
, 0);
672 /* prevent other vlan parent remove/add from taking place */
673 vlan_parent_flags_set_change_in_progress(vlp
);
674 if (g_vlan
->verbose
&& waited
) {
675 struct ifnet
* ifp
= vlp
->vlp_ifp
;
677 printf("%s%d: %s woke up\n", ifnet_name(ifp
), ifnet_unit(ifp
), msg
);
683 * Function: vlan_parent_signal
685 * Allows the thread that previously invoked vlan_parent_wait() to
686 * give up exclusive access to the vlan_parent data structure, and wake up
687 * any other threads waiting to access
689 * Before calling, you must be holding the vlan_lock and have taken
690 * a reference on the vlan_parent_ref.
693 vlan_parent_signal(vlan_parent_ref vlp
, const char * msg
)
695 struct ifnet
* vlp_ifp
= vlp
->vlp_ifp
;
697 if (vlan_parent_flags_link_event_required(vlp
)) {
698 vlan_parent_flags_clear_link_event_required(vlp
);
699 if (!vlan_parent_flags_detaching(vlp
)) {
700 u_int32_t event_code
= vlp
->vlp_event_code
;
705 /* we can safely walk the list unlocked */
706 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
707 struct ifnet
* ifp
= ifv
->ifv_ifp
;
709 interface_link_event(ifp
, event_code
);
711 if (g_vlan
->verbose
) {
712 printf("%s%d: propagated link event to vlans\n",
713 ifnet_name(vlp_ifp
), ifnet_unit(vlp_ifp
));
718 vlan_parent_flags_clear_change_in_progress(vlp
);
719 wakeup((caddr_t
)vlp
);
720 if (g_vlan
->verbose
) {
721 printf("%s%d: %s wakeup\n",
722 ifnet_name(vlp_ifp
), ifnet_unit(vlp_ifp
), msg
);
728 * Program our multicast filter. What we're actually doing is
729 * programming the multicast filter of the parent. This has the
730 * side effect of causing the parent interface to receive multicast
731 * traffic that it doesn't really want, which ends up being discarded
732 * later by the upper protocol layers. Unfortunately, there's no way
733 * to avoid this: there really is only one physical interface.
736 vlan_setmulti(struct ifnet
* ifp
)
741 vlan_parent_ref vlp
= NULL
;
744 ifv
= ifnet_get_ifvlan_retained(ifp
);
748 vlp
= ifvlan_get_vlan_parent_retained(ifv
);
750 /* no parent, no need to program the multicast filter */
753 vlan_parent_wait(vlp
, "vlan_setmulti");
755 /* check again, things could have changed */
756 if (ifnet_ifvlan_vlan_parent_ok(ifp
, ifv
, vlp
) == FALSE
) {
762 /* update parent interface with our multicast addresses */
763 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
768 vlan_parent_signal(vlp
, "vlan_setmulti");
776 vlan_parent_release(vlp
);
782 ** vlan_parent list manipulation/lookup routines
784 static vlan_parent_ref
785 parent_list_lookup(struct ifnet
* p
)
789 LIST_FOREACH(vlp
, &g_vlan
->parent_list
, vlp_parent_list
) {
790 if (vlp
->vlp_ifp
== p
) {
798 vlan_parent_lookup_tag(vlan_parent_ref vlp
, int tag
)
802 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
803 if (tag
== ifv
->ifv_tag
) {
811 vlan_lookup_parent_and_tag(struct ifnet
* p
, int tag
)
815 vlp
= parent_list_lookup(p
);
817 return (vlan_parent_lookup_tag(vlp
, tag
));
823 vlan_parent_find_max_mtu(vlan_parent_ref vlp
, ifvlan_ref exclude_ifv
)
828 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
831 if (exclude_ifv
== ifv
) {
834 req_mtu
= ifnet_mtu(ifv
->ifv_ifp
) + ifv
->ifv_mtufudge
;
835 if (req_mtu
> max_mtu
) {
843 * Function: vlan_parent_create
845 * Create a vlan_parent structure to hold the VLAN's for the given
846 * interface. Add it to the list of VLAN parents.
849 vlan_parent_create(struct ifnet
* p
, vlan_parent_ref
* ret_vlp
)
855 vlp
= _MALLOC(sizeof(*vlp
), M_VLAN
, M_WAITOK
);
859 bzero(vlp
, sizeof(*vlp
));
860 error
= siocgifdevmtu(p
, &vlp
->vlp_devmtu
);
862 printf("vlan_parent_create (%s%d): siocgifdevmtu failed, %d\n",
863 ifnet_name(p
), ifnet_unit(p
), error
);
867 LIST_INIT(&vlp
->vlp_vlan_list
);
869 vlp
->vlp_retain_count
= 1;
870 vlp
->vlp_signature
= VLP_SIGNATURE
;
872 & (IF_HWASSIST_VLAN_MTU
| IF_HWASSIST_VLAN_TAGGING
)) {
873 vlan_parent_flags_set_supports_vlan_mtu(vlp
);
880 vlan_parent_remove_all_vlans(struct ifnet
* p
)
883 int need_vlp_release
= 0;
888 vlp
= parent_list_lookup(p
);
889 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
894 vlan_parent_flags_set_detaching(vlp
);
895 vlan_parent_retain(vlp
);
896 vlan_parent_wait(vlp
, "vlan_parent_remove_all_vlans");
898 vlp
= parent_list_lookup(p
);
904 for (ifv
= LIST_FIRST(&vlp
->vlp_vlan_list
); ifv
!= NULL
; ifv
= next
) {
905 struct ifnet
* ifp
= ifv
->ifv_ifp
;
908 next
= LIST_NEXT(ifv
, ifv_vlan_list
);
909 removed
= vlan_remove(ifv
, FALSE
);
917 /* the vlan parent has no more VLAN's */
918 ifnet_set_eflags(p
, 0, IFEF_VLAN
); /* clear IFEF_VLAN */
920 LIST_REMOVE(vlp
, vlp_parent_list
);
921 need_vlp_release
++; /* one for being in the list */
922 need_vlp_release
++; /* final reference */
925 vlan_parent_signal(vlp
, "vlan_parent_remove_all_vlans");
928 while (need_vlp_release
--) {
929 vlan_parent_release(vlp
);
934 static __inline__
int
935 vlan_parent_no_vlans(vlan_parent_ref vlp
)
937 return (LIST_EMPTY(&vlp
->vlp_vlan_list
));
941 vlan_parent_add_vlan(vlan_parent_ref vlp
, ifvlan_ref ifv
, int tag
)
943 LIST_INSERT_HEAD(&vlp
->vlp_vlan_list
, ifv
, ifv_vlan_list
);
950 vlan_parent_remove_vlan(__unused vlan_parent_ref vlp
, ifvlan_ref ifv
)
953 LIST_REMOVE(ifv
, ifv_vlan_list
);
958 vlan_clone_attach(void)
962 error
= if_clone_attach(&vlan_cloner
);
970 vlan_clone_create(struct if_clone
*ifc
, u_int32_t unit
, __unused
void *params
)
975 struct ifnet_init_eparams vlan_init
;
977 error
= vlan_globals_init();
981 ifv
= _MALLOC(sizeof(struct ifvlan
), M_VLAN
, M_WAITOK
);
984 bzero(ifv
, sizeof(struct ifvlan
));
985 ifv
->ifv_retain_count
= 1;
986 ifv
->ifv_signature
= IFV_SIGNATURE
;
987 multicast_list_init(&ifv
->ifv_multicast
);
989 /* use the interface name as the unique id for ifp recycle */
991 snprintf(ifv
->ifv_name
, sizeof(ifv
->ifv_name
), "%s%d",
992 ifc
->ifc_name
, unit
) >= sizeof(ifv
->ifv_name
)) {
997 bzero(&vlan_init
, sizeof(vlan_init
));
998 vlan_init
.ver
= IFNET_INIT_CURRENT_VERSION
;
999 vlan_init
.len
= sizeof (vlan_init
);
1000 vlan_init
.flags
= IFNET_INIT_LEGACY
;
1001 vlan_init
.uniqueid
= ifv
->ifv_name
;
1002 vlan_init
.uniqueid_len
= strlen(ifv
->ifv_name
);
1003 vlan_init
.name
= ifc
->ifc_name
;
1004 vlan_init
.unit
= unit
;
1005 vlan_init
.family
= IFNET_FAMILY_VLAN
;
1006 vlan_init
.type
= IFT_L2VLAN
;
1007 vlan_init
.output
= vlan_output
;
1008 vlan_init
.demux
= ether_demux
;
1009 vlan_init
.add_proto
= ether_add_proto
;
1010 vlan_init
.del_proto
= ether_del_proto
;
1011 vlan_init
.check_multi
= ether_check_multi
;
1012 vlan_init
.framer_extended
= ether_frameout_extended
;
1013 vlan_init
.softc
= ifv
;
1014 vlan_init
.ioctl
= vlan_ioctl
;
1015 vlan_init
.set_bpf_tap
= vlan_set_bpf_tap
;
1016 vlan_init
.detach
= vlan_if_free
;
1017 vlan_init
.broadcast_addr
= etherbroadcastaddr
;
1018 vlan_init
.broadcast_len
= ETHER_ADDR_LEN
;
1019 error
= ifnet_allocate_extended(&vlan_init
, &ifp
);
1022 ifvlan_release(ifv
);
1026 ifnet_set_offload(ifp
, 0);
1027 ifnet_set_addrlen(ifp
, ETHER_ADDR_LEN
); /* XXX ethernet specific */
1028 ifnet_set_baudrate(ifp
, 0);
1029 ifnet_set_hdrlen(ifp
, ETHER_VLAN_ENCAP_LEN
);
1031 error
= ifnet_attach(ifp
, NULL
);
1034 ifvlan_release(ifv
);
1039 /* attach as ethernet */
1040 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
1045 vlan_remove(ifvlan_ref ifv
, int need_to_wait
)
1047 vlan_assert_lock_held();
1048 if (ifvlan_flags_detaching(ifv
)) {
1051 ifvlan_flags_set_detaching(ifv
);
1052 vlan_unconfig(ifv
, need_to_wait
);
1058 vlan_clone_destroy(struct ifnet
*ifp
)
1063 ifv
= ifnet_get_ifvlan_retained(ifp
);
1068 if (vlan_remove(ifv
, TRUE
) == 0) {
1070 ifvlan_release(ifv
);
1074 ifvlan_release(ifv
);
1081 vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
1086 ifv
= ifnet_get_ifvlan_retained(ifp
);
1092 case BPF_TAP_DISABLE
:
1093 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= NULL
;
1097 ifv
->ifv_bpf_input
= func
;
1100 case BPF_TAP_OUTPUT
:
1101 ifv
->ifv_bpf_output
= func
;
1104 case BPF_TAP_INPUT_OUTPUT
:
1105 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= func
;
1111 ifvlan_release(ifv
);
1116 vlan_output(struct ifnet
* ifp
, struct mbuf
* m
)
1118 bpf_packet_func bpf_func
;
1119 struct ether_vlan_header
* evl
;
1125 vlan_parent_ref vlp
= NULL
;
1127 struct flowadv adv
= { FADV_SUCCESS
};
1132 if ((m
->m_flags
& M_PKTHDR
) == 0) {
1137 ifv
= ifnet_get_ifvlan_retained(ifp
);
1138 if (ifv
== NULL
|| ifvlan_flags_ready(ifv
) == 0) {
1141 vlp
= ifvlan_get_vlan_parent_retained(ifv
);
1146 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
1147 soft_vlan
= (ifnet_offload(p
) & IF_HWASSIST_VLAN_TAGGING
) == 0;
1148 bpf_func
= ifv
->ifv_bpf_output
;
1150 encaplen
= ifv
->ifv_encaplen
;
1153 ifvlan_release(ifv
);
1154 vlan_parent_release(vlp
);
1156 vlan_bpf_output(ifp
, m
, bpf_func
);
1158 /* do not run parent's if_output() if the parent is not up */
1159 if ((ifnet_flags(p
) & (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
)) {
1161 atomic_add_64(&ifp
->if_collisions
, 1);
1165 * If underlying interface can do VLAN tag insertion itself,
1166 * just pass the packet along. However, we need some way to
1167 * tell the interface where the packet came from so that it
1168 * knows how to find the VLAN tag to use. We use a field in
1169 * the mbuf header to store the VLAN tag, and a bit in the
1170 * csum_flags field to mark the field as valid.
1172 if (soft_vlan
== 0) {
1173 m
->m_pkthdr
.csum_flags
|= CSUM_VLAN_TAG_VALID
;
1174 m
->m_pkthdr
.vlan_tag
= tag
;
1176 M_PREPEND(m
, encaplen
, M_DONTWAIT
);
1178 printf("%s%d: unable to prepend VLAN header\n", ifnet_name(ifp
),
1180 atomic_add_64(&ifp
->if_oerrors
, 1);
1183 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1184 if (m
->m_len
< (int)sizeof(*evl
)) {
1185 m
= m_pullup(m
, sizeof(*evl
));
1187 printf("%s%d: unable to pullup VLAN header\n", ifnet_name(ifp
),
1189 atomic_add_64(&ifp
->if_oerrors
, 1);
1195 * Transform the Ethernet header into an Ethernet header
1196 * with 802.1Q encapsulation.
1198 bcopy(mtod(m
, char *) + encaplen
,
1199 mtod(m
, char *), ETHER_HDR_LEN
);
1200 evl
= mtod(m
, struct ether_vlan_header
*);
1201 evl
->evl_proto
= evl
->evl_encap_proto
;
1202 evl
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
1203 evl
->evl_tag
= htons(tag
);
1206 err
= dlil_output(p
, PF_VLAN
, m
, NULL
, NULL
, 1, &adv
);
1209 if (adv
.code
== FADV_FLOW_CONTROLLED
) {
1211 } else if (adv
.code
== FADV_SUSPENDED
) {
1221 ifvlan_release(ifv
);
1224 vlan_parent_release(vlp
);
1232 vlan_input(ifnet_t p
, __unused protocol_family_t protocol
,
1233 mbuf_t m
, char *frame_header
)
1235 bpf_packet_func bpf_func
= NULL
;
1236 struct ether_vlan_header
* evl
;
1237 struct ifnet
* ifp
= NULL
;
1241 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1243 * Packet is tagged, m contains a normal
1244 * Ethernet frame; the tag is stored out-of-band.
1246 m
->m_pkthdr
.csum_flags
&= ~CSUM_VLAN_TAG_VALID
;
1247 tag
= EVL_VLANOFTAG(m
->m_pkthdr
.vlan_tag
);
1248 m
->m_pkthdr
.vlan_tag
= 0;
1251 switch (ifnet_type(p
)) {
1253 if (m
->m_len
< ETHER_VLAN_ENCAP_LEN
) {
1257 evl
= (struct ether_vlan_header
*)(void *)frame_header
;
1258 if (ntohs(evl
->evl_proto
) == ETHERTYPE_VLAN
) {
1259 /* don't allow VLAN within VLAN */
1263 tag
= EVL_VLANOFTAG(ntohs(evl
->evl_tag
));
1266 * Restore the original ethertype. We'll remove
1267 * the encapsulation after we've found the vlan
1268 * interface corresponding to the tag.
1270 evl
->evl_encap_proto
= evl
->evl_proto
;
1273 printf("vlan_demux: unsupported if type %u",
1283 if ((ifnet_eflags(p
) & IFEF_VLAN
) == 0) {
1284 /* don't bother looking through the VLAN list */
1289 ifv
= vlan_lookup_parent_and_tag(p
, tag
);
1294 || ifvlan_flags_ready(ifv
) == 0
1295 || (ifnet_flags(ifp
) & IFF_UP
) == 0) {
1300 bpf_func
= ifv
->ifv_bpf_input
;
1305 * Packet had an in-line encapsulation header;
1306 * remove it. The original header has already
1307 * been fixed up above.
1309 m
->m_len
-= ETHER_VLAN_ENCAP_LEN
;
1310 m
->m_data
+= ETHER_VLAN_ENCAP_LEN
;
1311 m
->m_pkthdr
.len
-= ETHER_VLAN_ENCAP_LEN
;
1312 m
->m_pkthdr
.csum_flags
= 0; /* can't trust hardware checksum */
1315 m
->m_pkthdr
.rcvif
= ifp
;
1316 m
->m_pkthdr
.pkt_hdr
= frame_header
;
1317 (void)ifnet_stat_increment_in(ifp
, 1,
1318 m
->m_pkthdr
.len
+ ETHER_HDR_LEN
, 0);
1319 vlan_bpf_input(ifp
, m
, bpf_func
, frame_header
, ETHER_HDR_LEN
,
1320 soft_vlan
? ETHER_VLAN_ENCAP_LEN
: 0);
1321 /* We found a vlan interface, inject on that interface. */
1322 dlil_input_packet_list(ifp
, m
);
1324 m
->m_pkthdr
.pkt_hdr
= frame_header
;
1325 /* Send priority-tagged packet up through the parent */
1326 dlil_input_packet_list(p
, m
);
1332 vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
)
1335 int first_vlan
= FALSE
;
1336 ifvlan_ref ifv
= NULL
;
1337 int ifv_added
= FALSE
;
1338 int need_vlp_release
= 0;
1339 vlan_parent_ref new_vlp
= NULL
;
1340 ifnet_offload_t offload
;
1341 u_int16_t parent_flags
;
1342 vlan_parent_ref vlp
= NULL
;
1344 /* pre-allocate space for vlan_parent, in case we're first */
1345 error
= vlan_parent_create(p
, &new_vlp
);
1351 ifv
= ifnet_get_ifvlan_retained(ifp
);
1352 if (ifv
== NULL
|| ifv
->ifv_vlp
!= NULL
) {
1355 ifvlan_release(ifv
);
1357 vlan_parent_release(new_vlp
);
1360 vlp
= parent_list_lookup(p
);
1362 vlan_parent_retain(vlp
);
1364 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1365 /* already a VLAN with that tag on this interface */
1371 /* one for being in the list */
1372 vlan_parent_retain(new_vlp
);
1374 /* we're the first VLAN on this interface */
1375 LIST_INSERT_HEAD(&g_vlan
->parent_list
, new_vlp
, vlp_parent_list
);
1378 vlan_parent_retain(vlp
);
1382 /* need to wait to ensure no one else is trying to add/remove */
1383 vlan_parent_wait(vlp
, "vlan_config");
1385 if (ifnet_get_ifvlan(ifp
) != ifv
) {
1390 /* check again because someone might have gotten in */
1391 if (parent_list_lookup(p
) != vlp
) {
1396 if (vlan_parent_flags_detaching(vlp
)
1397 || ifvlan_flags_detaching(ifv
) || ifv
->ifv_vlp
!= NULL
) {
1402 /* check again because someone might have gotten the tag */
1403 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1404 /* already a VLAN with that tag on this interface */
1409 if (vlan_parent_no_vlans(vlp
)) {
1412 vlan_parent_add_vlan(vlp
, ifv
, tag
);
1413 ifvlan_retain(ifv
); /* parent references ifv */
1416 /* check whether bond interface is using parent interface */
1417 ifnet_lock_exclusive(p
);
1418 if ((ifnet_eflags(p
) & IFEF_BOND
) != 0) {
1420 /* don't allow VLAN over interface that's already part of a bond */
1424 /* prevent BOND interface from using it */
1425 /* Can't use ifnet_set_eflags because that would take the lock */
1426 p
->if_eflags
|= IFEF_VLAN
;
1431 /* attach our VLAN "protocol" to the interface */
1432 error
= vlan_attach_protocol(p
);
1439 /* configure parent to receive our multicast addresses */
1440 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
1443 (void)vlan_detach_protocol(p
);
1449 /* set our ethernet address to that of the parent */
1450 ifnet_set_lladdr_and_type(ifp
, IF_LLADDR(p
), ETHER_ADDR_LEN
, IFT_ETHER
);
1452 /* no failures past this point */
1455 ifv
->ifv_encaplen
= ETHER_VLAN_ENCAP_LEN
;
1457 if (vlan_parent_flags_supports_vlan_mtu(vlp
)) {
1458 ifv
->ifv_mtufudge
= 0;
1461 * Fudge the MTU by the encapsulation size. This
1462 * makes us incompatible with strictly compliant
1463 * 802.1Q implementations, but allows us to use
1464 * the feature with other NetBSD implementations,
1465 * which might still be useful.
1467 ifv
->ifv_mtufudge
= ifv
->ifv_encaplen
;
1469 ifnet_set_mtu(ifp
, ETHERMTU
- ifv
->ifv_mtufudge
);
1472 * Copy only a selected subset of flags from the parent.
1473 * Other flags are none of our business.
1475 parent_flags
= ifnet_flags(p
)
1476 & (IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
);
1477 ifnet_set_flags(ifp
, parent_flags
,
1478 IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
);
1480 /* use hwassist bits from parent interface, but exclude VLAN bits */
1481 offload
= ifnet_offload(p
) & ~(IFNET_VLAN_TAGGING
| IFNET_VLAN_MTU
);
1482 ifnet_set_offload(ifp
, offload
);
1484 ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
1485 ifvlan_flags_set_ready(ifv
);
1486 vlan_parent_signal(vlp
, "vlan_config");
1488 if (new_vlp
!= vlp
) {
1489 /* throw it away, it wasn't needed */
1490 vlan_parent_release(new_vlp
);
1493 ifvlan_release(ifv
);
1496 /* mark the parent interface up */
1497 ifnet_set_flags(p
, IFF_UP
, IFF_UP
);
1498 (void)ifnet_ioctl(p
, 0, SIOCSIFFLAGS
, (caddr_t
)NULL
);
1503 vlan_assert_lock_held();
1506 vlan_parent_remove_vlan(vlp
, ifv
);
1507 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1508 /* the vlan parent has no more VLAN's */
1509 ifnet_set_eflags(p
, 0, IFEF_VLAN
);
1510 LIST_REMOVE(vlp
, vlp_parent_list
);
1511 /* release outside of the lock below */
1514 /* one for being in the list */
1518 vlan_parent_signal(vlp
, "vlan_config");
1523 while (need_vlp_release
--) {
1524 vlan_parent_release(vlp
);
1526 if (new_vlp
!= vlp
) {
1527 vlan_parent_release(new_vlp
);
1531 ifvlan_release(ifv
);
1533 ifvlan_release(ifv
);
1539 vlan_link_event(struct ifnet
* ifp
, struct ifnet
* p
)
1541 struct ifmediareq ifmr
;
1543 /* generate a link event based on the state of the underlying interface */
1544 bzero(&ifmr
, sizeof(ifmr
));
1545 snprintf(ifmr
.ifm_name
, sizeof(ifmr
.ifm_name
),
1546 "%s%d", ifnet_name(p
), ifnet_unit(p
));
1547 if (ifnet_ioctl(p
, 0, SIOCGIFMEDIA
, &ifmr
) == 0
1548 && ifmr
.ifm_count
> 0 && ifmr
.ifm_status
& IFM_AVALID
) {
1551 event
= (ifmr
.ifm_status
& IFM_ACTIVE
)
1552 ? KEV_DL_LINK_ON
: KEV_DL_LINK_OFF
;
1553 interface_link_event(ifp
, event
);
1559 vlan_unconfig(ifvlan_ref ifv
, int need_to_wait
)
1561 struct ifnet
* ifp
= ifv
->ifv_ifp
;
1562 int last_vlan
= FALSE
;
1563 int need_ifv_release
= 0;
1564 int need_vlp_release
= 0;
1566 vlan_parent_ref vlp
;
1568 vlan_assert_lock_held();
1575 vlan_parent_retain(vlp
);
1576 vlan_parent_wait(vlp
, "vlan_unconfig");
1578 /* check again because another thread could be in vlan_unconfig */
1579 if (ifv
!= ifnet_get_ifvlan(ifp
)) {
1582 if (ifv
->ifv_vlp
!= vlp
) {
1583 /* vlan parent changed */
1588 /* ifv has a reference on vlp, need to remove it */
1592 /* remember whether we're the last VLAN on the parent */
1593 if (LIST_NEXT(LIST_FIRST(&vlp
->vlp_vlan_list
), ifv_vlan_list
) == NULL
) {
1594 if (g_vlan
->verbose
) {
1595 printf("vlan_unconfig: last vlan on %s%d\n",
1596 ifnet_name(p
), ifnet_unit(p
));
1601 /* back-out any effect our mtu might have had on the parent */
1602 (void)ifvlan_new_mtu(ifv
, ETHERMTU
- ifv
->ifv_mtufudge
);
1606 /* un-join multicast on parent interface */
1607 (void)multicast_list_remove(&ifv
->ifv_multicast
);
1609 /* Clear our MAC address. */
1610 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_L2VLAN
);
1612 /* detach VLAN "protocol" */
1614 (void)vlan_detach_protocol(p
);
1619 /* return to the state we were in before SIFVLAN */
1620 ifnet_set_mtu(ifp
, 0);
1621 ifnet_set_flags(ifp
, 0,
1622 IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
| IFF_RUNNING
);
1623 ifnet_set_offload(ifp
, 0);
1624 ifv
->ifv_mtufudge
= 0;
1626 /* Disconnect from parent. */
1627 vlan_parent_remove_vlan(vlp
, ifv
);
1630 /* vlan_parent has reference to ifv, remove it */
1633 /* from this point on, no more referencing ifv */
1634 if (last_vlan
&& !vlan_parent_flags_detaching(vlp
)) {
1635 /* the vlan parent has no more VLAN's */
1636 ifnet_set_eflags(p
, 0, IFEF_VLAN
);
1637 LIST_REMOVE(vlp
, vlp_parent_list
);
1639 /* one for being in the list */
1642 /* release outside of the lock below */
1648 vlan_parent_signal(vlp
, "vlan_unconfig");
1651 while (need_ifv_release
--) {
1652 ifvlan_release(ifv
);
1654 while (need_vlp_release
--) { /* references to vlp */
1655 vlan_parent_release(vlp
);
1662 vlan_set_promisc(struct ifnet
* ifp
)
1666 vlan_parent_ref vlp
;
1669 ifv
= ifnet_get_ifvlan_retained(ifp
);
1679 if ((ifnet_flags(ifp
) & IFF_PROMISC
) != 0) {
1680 if (!ifvlan_flags_promisc(ifv
)) {
1681 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 1);
1683 ifvlan_flags_set_promisc(ifv
);
1687 if (ifvlan_flags_promisc(ifv
)) {
1688 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 0);
1690 ifvlan_flags_clear_promisc(ifv
);
1697 ifvlan_release(ifv
);
1703 ifvlan_new_mtu(ifvlan_ref ifv
, int mtu
)
1705 struct ifdevmtu
* devmtu_p
;
1707 struct ifnet
* ifp
= ifv
->ifv_ifp
;
1711 vlan_parent_ref vlp
;
1713 vlan_assert_lock_held();
1715 devmtu_p
= &vlp
->vlp_devmtu
;
1716 req_mtu
= mtu
+ ifv
->ifv_mtufudge
;
1717 if (req_mtu
> devmtu_p
->ifdm_max
|| req_mtu
< devmtu_p
->ifdm_min
) {
1720 max_mtu
= vlan_parent_find_max_mtu(vlp
, ifv
);
1721 if (req_mtu
> max_mtu
) {
1724 else if (max_mtu
< devmtu_p
->ifdm_current
) {
1728 struct ifnet
* p
= vlp
->vlp_ifp
;
1730 error
= siocsifaltmtu(p
, new_mtu
);
1735 devmtu_p
->ifdm_current
= new_mtu
;
1737 ifnet_set_mtu(ifp
, mtu
);
1743 vlan_set_mtu(struct ifnet
* ifp
, int mtu
)
1747 vlan_parent_ref vlp
;
1749 if (mtu
< IF_MINMTU
) {
1753 ifv
= ifnet_get_ifvlan_retained(ifp
);
1758 vlp
= ifvlan_get_vlan_parent_retained(ifv
);
1761 ifvlan_release(ifv
);
1767 vlan_parent_wait(vlp
, "vlan_set_mtu");
1769 /* check again, something might have changed */
1770 if (ifnet_get_ifvlan(ifp
) != ifv
1771 || ifvlan_flags_detaching(ifv
)) {
1775 if (ifv
->ifv_vlp
!= vlp
) {
1776 /* vlan parent changed */
1779 if (vlan_parent_flags_detaching(vlp
)) {
1785 error
= ifvlan_new_mtu(ifv
, mtu
);
1788 vlan_parent_signal(vlp
, "vlan_set_mtu");
1790 vlan_parent_release(vlp
);
1791 ifvlan_release(ifv
);
1797 vlan_ioctl(ifnet_t ifp
, u_long cmd
, void * data
)
1799 struct ifdevmtu
* devmtu_p
;
1801 struct ifaddr
* ifa
;
1802 struct ifmediareq
*ifmr
;
1807 user_addr_t user_addr
;
1808 vlan_parent_ref vlp
;
1811 if (ifnet_type(ifp
) != IFT_L2VLAN
) {
1812 return (EOPNOTSUPP
);
1814 ifr
= (struct ifreq
*)data
;
1815 ifa
= (struct ifaddr
*)data
;
1819 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
1822 case SIOCGIFMEDIA32
:
1823 case SIOCGIFMEDIA64
:
1825 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1826 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1828 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1830 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1832 ifmr
= (struct ifmediareq
*)data
;
1833 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
1834 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
1835 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
1837 struct ifmediareq p_ifmr
;
1839 bzero(&p_ifmr
, sizeof(p_ifmr
));
1840 error
= ifnet_ioctl(p
, 0, SIOCGIFMEDIA
, &p_ifmr
);
1842 ifmr
->ifm_active
= p_ifmr
.ifm_active
;
1843 ifmr
->ifm_current
= p_ifmr
.ifm_current
;
1844 ifmr
->ifm_mask
= p_ifmr
.ifm_mask
;
1845 ifmr
->ifm_status
= p_ifmr
.ifm_status
;
1846 ifmr
->ifm_count
= p_ifmr
.ifm_count
;
1847 /* Limit the result to the parent's current config. */
1848 if (ifmr
->ifm_count
>= 1 && user_addr
!= USER_ADDR_NULL
) {
1849 ifmr
->ifm_count
= 1;
1850 error
= copyout(&ifmr
->ifm_current
, user_addr
,
1855 ifmr
->ifm_active
= ifmr
->ifm_current
= IFM_NONE
;
1857 ifmr
->ifm_status
= IFM_AVALID
;
1858 ifmr
->ifm_count
= 1;
1859 if (user_addr
!= USER_ADDR_NULL
) {
1860 error
= copyout(&ifmr
->ifm_current
, user_addr
, sizeof(int));
1871 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1872 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1874 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1878 int min_mtu
= vlp
->vlp_devmtu
.ifdm_min
- ifv
->ifv_mtufudge
;
1879 devmtu_p
= &ifr
->ifr_devmtu
;
1880 devmtu_p
->ifdm_current
= ifnet_mtu(ifp
);
1881 devmtu_p
->ifdm_min
= max(min_mtu
, IF_MINMTU
);
1882 devmtu_p
->ifdm_max
= vlp
->vlp_devmtu
.ifdm_max
- ifv
->ifv_mtufudge
;
1885 devmtu_p
= &ifr
->ifr_devmtu
;
1886 devmtu_p
->ifdm_current
= 0;
1887 devmtu_p
->ifdm_min
= 0;
1888 devmtu_p
->ifdm_max
= 0;
1894 error
= vlan_set_mtu(ifp
, ifr
->ifr_mtu
);
1898 user_addr
= proc_is64bit(current_proc())
1899 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1900 error
= copyin(user_addr
, &vlr
, sizeof(vlr
));
1905 if (vlr
.vlr_parent
[0] != '\0') {
1906 if (vlr
.vlr_tag
& ~EVL_VLID_MASK
) {
1908 * Don't let the caller set up a VLAN tag with
1909 * anything except VLID bits.
1914 p
= ifunit(vlr
.vlr_parent
);
1919 /* can't do VLAN over anything but ethernet or ethernet aggregate */
1920 if (ifnet_type(p
) != IFT_ETHER
1921 && ifnet_type(p
) != IFT_IEEE8023ADLAG
) {
1922 error
= EPROTONOSUPPORT
;
1925 error
= vlan_config(ifp
, p
, vlr
.vlr_tag
);
1930 /* Update promiscuous mode, if necessary. */
1931 (void)vlan_set_promisc(ifp
);
1933 /* generate a link event based on the state of the parent */
1934 vlan_link_event(ifp
, p
);
1937 int need_link_event
= FALSE
;
1940 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1941 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1943 error
= (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1946 need_link_event
= vlan_remove(ifv
, TRUE
);
1948 if (need_link_event
) {
1949 interface_link_event(ifp
, KEV_DL_LINK_OFF
);
1955 bzero(&vlr
, sizeof vlr
);
1957 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1958 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1960 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1962 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1966 snprintf(vlr
.vlr_parent
, sizeof(vlr
.vlr_parent
),
1967 "%s%d", ifnet_name(p
), ifnet_unit(p
));
1970 user_addr
= proc_is64bit(current_proc())
1971 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1972 error
= copyout(&vlr
, user_addr
, sizeof(vlr
));
1977 * For promiscuous mode, we enable promiscuous mode on
1978 * the parent if we need promiscuous on the VLAN interface.
1980 error
= vlan_set_promisc(ifp
);
1985 error
= vlan_setmulti(ifp
);
1994 vlan_if_free(struct ifnet
* ifp
)
2001 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
2005 ifvlan_release(ifv
);
2011 vlan_event(struct ifnet
* p
, __unused protocol_family_t protocol
,
2012 const struct kev_msg
* event
)
2016 /* Check if the interface we are attached to is being detached */
2017 if (event
->vendor_code
!= KEV_VENDOR_APPLE
2018 || event
->kev_class
!= KEV_NETWORK_CLASS
2019 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
2022 event_code
= event
->event_code
;
2023 switch (event_code
) {
2024 case KEV_DL_LINK_OFF
:
2025 case KEV_DL_LINK_ON
:
2026 vlan_parent_link_event(p
, event_code
);
2035 vlan_detached(ifnet_t p
, __unused protocol_family_t protocol
)
2037 if (ifnet_is_attached(p
, 0) == 0) {
2038 /* if the parent isn't attached, remove all VLANs */
2039 vlan_parent_remove_all_vlans(p
);
2045 interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
)
2048 struct kern_event_msg header
;
2050 char if_name
[IFNAMSIZ
];
2053 bzero(&event
, sizeof(event
));
2054 event
.header
.total_size
= sizeof(event
);
2055 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
2056 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
2057 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
2058 event
.header
.event_code
= event_code
;
2059 event
.header
.event_data
[0] = ifnet_family(ifp
);
2060 event
.unit
= (u_int32_t
) ifnet_unit(ifp
);
2061 strlcpy(event
.if_name
, ifnet_name(ifp
), IFNAMSIZ
);
2062 ifnet_event(ifp
, &event
.header
);
2067 vlan_parent_link_event(struct ifnet
* p
, u_int32_t event_code
)
2069 vlan_parent_ref vlp
;
2072 if ((ifnet_eflags(p
) & IFEF_VLAN
) == 0) {
2077 vlp
= parent_list_lookup(p
);
2083 vlan_parent_flags_set_link_event_required(vlp
);
2084 vlp
->vlp_event_code
= event_code
;
2085 if (vlan_parent_flags_change_in_progress(vlp
)) {
2086 /* don't block waiting to generate an event */
2090 vlan_parent_retain(vlp
);
2091 vlan_parent_wait(vlp
, "vlan_parent_link_event");
2092 vlan_parent_signal(vlp
, "vlan_parent_link_event");
2094 vlan_parent_release(vlp
);
2100 * Function: vlan_attach_protocol
2102 * Attach a DLIL protocol to the interface, using the ETHERTYPE_VLAN
2105 * The ethernet demux actually special cases VLAN to support hardware.
2106 * The demux here isn't used. The demux will return PF_VLAN for the
2107 * appropriate packets and our vlan_input function will be called.
2110 vlan_attach_protocol(struct ifnet
*ifp
)
2113 struct ifnet_attach_proto_param reg
;
2115 bzero(®
, sizeof(reg
));
2116 reg
.input
= vlan_input
;
2117 reg
.event
= vlan_event
;
2118 reg
.detached
= vlan_detached
;
2119 error
= ifnet_attach_protocol(ifp
, PF_VLAN
, ®
);
2121 printf("vlan_proto_attach(%s%d) ifnet_attach_protocol failed, %d\n",
2122 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
2128 * Function: vlan_detach_protocol
2130 * Detach our DLIL protocol from an interface
2133 vlan_detach_protocol(struct ifnet
*ifp
)
2137 error
= ifnet_detach_protocol(ifp
, PF_VLAN
);
2139 printf("vlan_proto_detach(%s%d) ifnet_detach_protocol failed, %d\n",
2140 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
2147 * DLIL interface family functions
2148 * We use the ethernet plumb functions, since that's all we support.
2149 * If we wanted to handle multiple LAN types (tokenring, etc.), we'd
2150 * call the appropriate routines for that LAN type instead of hard-coding
2154 vlan_attach_inet(struct ifnet
*ifp
, protocol_family_t protocol_family
)
2156 return (ether_attach_inet(ifp
, protocol_family
));
2160 vlan_detach_inet(struct ifnet
*ifp
, protocol_family_t protocol_family
)
2162 ether_detach_inet(ifp
, protocol_family
);
2167 vlan_attach_inet6(struct ifnet
*ifp
, protocol_family_t protocol_family
)
2169 return (ether_attach_inet6(ifp
, protocol_family
));
2173 vlan_detach_inet6(struct ifnet
*ifp
, protocol_family_t protocol_family
)
2175 ether_detach_inet6(ifp
, protocol_family
);
2179 __private_extern__
int
2180 vlan_family_init(void)
2184 error
= proto_register_plumber(PF_INET
, IFNET_FAMILY_VLAN
,
2185 vlan_attach_inet
, vlan_detach_inet
);
2187 printf("proto_register_plumber failed for AF_INET error=%d\n",
2192 error
= proto_register_plumber(PF_INET6
, IFNET_FAMILY_VLAN
,
2193 vlan_attach_inet6
, vlan_detach_inet6
);
2195 printf("proto_register_plumber failed for AF_INET6 error=%d\n",
2200 error
= vlan_clone_attach();
2202 printf("proto_register_plumber failed vlan_clone_attach error=%d\n",