2 * Copyright (c) 2003-2012 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 0x1
200 #define VLPF_CHANGE_IN_PROGRESS 0x2
201 #define VLPF_DETACHING 0x4
203 struct ifdevmtu vlp_devmtu
;
204 SInt32 vlp_retain_count
;
205 UInt32 vlp_signature
; /* VLP_SIGNATURE */
206 } vlan_parent
, * vlan_parent_ref
;
208 #define IFV_SIGNATURE 0xbeefbeef
210 ifvlan_entry ifv_vlan_list
;
211 char ifv_name
[IFNAMSIZ
]; /* our unique id */
212 struct ifnet
* ifv_ifp
; /* our interface */
213 vlan_parent_ref ifv_vlp
; /* parent information */
215 u_int16_t ifvm_encaplen
;/* encapsulation length */
216 u_int16_t ifvm_mtufudge
;/* MTU fudged by this much */
217 u_int16_t ifvm_proto
; /* encapsulation ethertype */
218 u_int16_t ifvm_tag
; /* tag to apply on packets leaving if */
220 struct multicast_list ifv_multicast
;
221 #define IFVF_PROMISC 0x1 /* promiscuous mode enabled */
222 #define IFVF_DETACHING 0x2 /* interface is detaching */
223 #define IFVF_READY 0x4 /* interface is ready */
225 bpf_packet_func ifv_bpf_input
;
226 bpf_packet_func ifv_bpf_output
;
227 SInt32 ifv_retain_count
;
228 UInt32 ifv_signature
; /* IFV_SIGNATURE */
231 typedef struct ifvlan
* ifvlan_ref
;
233 typedef struct vlan_globals_s
{
234 struct vlan_parent_list parent_list
;
236 } * vlan_globals_ref
;
238 static vlan_globals_ref g_vlan
;
240 #define ifv_tag ifv_mib.ifvm_tag
241 #define ifv_encaplen ifv_mib.ifvm_encaplen
242 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
245 vlan_parent_retain(vlan_parent_ref vlp
);
248 vlan_parent_release(vlan_parent_ref vlp
);
251 ** vlan_parent_ref vlp_flags in-lines
253 static __inline__
int
254 vlan_parent_flags_supports_vlan_mtu(vlan_parent_ref vlp
)
256 return ((vlp
->vlp_flags
& VLPF_SUPPORTS_VLAN_MTU
) != 0);
259 static __inline__
void
260 vlan_parent_flags_set_supports_vlan_mtu(vlan_parent_ref vlp
)
262 vlp
->vlp_flags
|= VLPF_SUPPORTS_VLAN_MTU
;
266 static __inline__
void
267 vlan_parent_flags_clear_supports_vlan_mtu(vlan_parent_ref vlp
)
269 vlp
->vlp_flags
&= ~VLPF_SUPPORTS_VLAN_MTU
;
273 static __inline__
int
274 vlan_parent_flags_change_in_progress(vlan_parent_ref vlp
)
276 return ((vlp
->vlp_flags
& VLPF_CHANGE_IN_PROGRESS
) != 0);
279 static __inline__
void
280 vlan_parent_flags_set_change_in_progress(vlan_parent_ref vlp
)
282 vlp
->vlp_flags
|= VLPF_CHANGE_IN_PROGRESS
;
286 static __inline__
void
287 vlan_parent_flags_clear_change_in_progress(vlan_parent_ref vlp
)
289 vlp
->vlp_flags
&= ~VLPF_CHANGE_IN_PROGRESS
;
293 static __inline__
int
294 vlan_parent_flags_detaching(struct vlan_parent
* vlp
)
296 return ((vlp
->vlp_flags
& VLPF_DETACHING
) != 0);
299 static __inline__
void
300 vlan_parent_flags_set_detaching(struct vlan_parent
* vlp
)
302 vlp
->vlp_flags
|= VLPF_DETACHING
;
308 ** ifvlan_flags in-lines routines
310 static __inline__
int
311 ifvlan_flags_promisc(ifvlan_ref ifv
)
313 return ((ifv
->ifv_flags
& IFVF_PROMISC
) != 0);
316 static __inline__
void
317 ifvlan_flags_set_promisc(ifvlan_ref ifv
)
319 ifv
->ifv_flags
|= IFVF_PROMISC
;
323 static __inline__
void
324 ifvlan_flags_clear_promisc(ifvlan_ref ifv
)
326 ifv
->ifv_flags
&= ~IFVF_PROMISC
;
330 static __inline__
int
331 ifvlan_flags_ready(ifvlan_ref ifv
)
333 return ((ifv
->ifv_flags
& IFVF_READY
) != 0);
336 static __inline__
void
337 ifvlan_flags_set_ready(ifvlan_ref ifv
)
339 ifv
->ifv_flags
|= IFVF_READY
;
343 static __inline__
void
344 ifvlan_flags_clear_ready(ifvlan_ref ifv
)
346 ifv
->ifv_flags
&= ~IFVF_READY
;
350 static __inline__
int
351 ifvlan_flags_detaching(ifvlan_ref ifv
)
353 return ((ifv
->ifv_flags
& IFVF_DETACHING
) != 0);
356 static __inline__
void
357 ifvlan_flags_set_detaching(ifvlan_ref ifv
)
359 ifv
->ifv_flags
|= IFVF_DETACHING
;
364 SYSCTL_DECL(_net_link
);
365 SYSCTL_NODE(_net_link
, IFT_L2VLAN
, vlan
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "IEEE 802.1Q VLAN");
366 SYSCTL_NODE(_net_link_vlan
, PF_LINK
, link
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "for consistency");
369 #define M_VLAN M_DEVBUF
371 static int vlan_clone_create(struct if_clone
*, u_int32_t
, void *);
372 static int vlan_clone_destroy(struct ifnet
*);
373 static int vlan_input(ifnet_t ifp
, protocol_family_t protocol
,
374 mbuf_t m
, char *frame_header
);
375 static int vlan_output(struct ifnet
*ifp
, struct mbuf
*m
);
376 static int vlan_ioctl(ifnet_t ifp
, u_long cmd
, void * addr
);
377 static int vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
,
378 bpf_packet_func func
);
379 static int vlan_attach_protocol(struct ifnet
*ifp
);
380 static int vlan_detach_protocol(struct ifnet
*ifp
);
381 static int vlan_setmulti(struct ifnet
*ifp
);
382 static int vlan_unconfig(ifvlan_ref ifv
, int need_to_wait
);
383 static int vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
);
384 static void vlan_if_free(struct ifnet
* ifp
);
385 static int vlan_remove(ifvlan_ref ifv
, int need_to_wait
);
387 static struct if_clone vlan_cloner
= IF_CLONE_INITIALIZER(VLANNAME
,
392 static void interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
);
393 static void vlan_parent_link_event(struct ifnet
* p
,
394 u_int32_t event_code
);
396 static int ifvlan_new_mtu(ifvlan_ref ifv
, int mtu
);
399 ** ifvlan_ref routines
402 ifvlan_retain(ifvlan_ref ifv
)
404 if (ifv
->ifv_signature
!= IFV_SIGNATURE
) {
405 panic("ifvlan_retain: bad signature\n");
407 if (ifv
->ifv_retain_count
== 0) {
408 panic("ifvlan_retain: retain count is 0\n");
410 OSIncrementAtomic(&ifv
->ifv_retain_count
);
414 ifvlan_release(ifvlan_ref ifv
)
416 UInt32 old_retain_count
;
418 if (ifv
->ifv_signature
!= IFV_SIGNATURE
) {
419 panic("ifvlan_release: bad signature\n");
421 old_retain_count
= OSDecrementAtomic(&ifv
->ifv_retain_count
);
422 switch (old_retain_count
) {
424 panic("ifvlan_release: retain count is 0\n");
427 if (g_vlan
->verbose
) {
428 printf("ifvlan_release(%s)\n", ifv
->ifv_name
);
430 ifv
->ifv_signature
= 0;
439 static vlan_parent_ref
440 ifvlan_get_vlan_parent_retained(ifvlan_ref ifv
)
442 vlan_parent_ref vlp
= ifv
->ifv_vlp
;
444 if (vlan_parent_flags_detaching(vlp
)) {
447 vlan_parent_retain(vlp
);
456 ifnet_get_ifvlan(struct ifnet
* ifp
)
460 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
465 ifnet_get_ifvlan_retained(struct ifnet
* ifp
)
469 ifv
= ifnet_get_ifvlan(ifp
);
473 if (ifvlan_flags_detaching(ifv
)) {
481 ifnet_ifvlan_vlan_parent_ok(struct ifnet
* ifp
, ifvlan_ref ifv
,
484 ifvlan_ref check_ifv
;
486 check_ifv
= ifnet_get_ifvlan(ifp
);
487 if (check_ifv
!= ifv
|| ifvlan_flags_detaching(ifv
)) {
488 /* ifvlan_ref no longer valid */
491 if (ifv
->ifv_vlp
!= vlp
) {
492 /* vlan_parent no longer valid */
495 if (vlan_parent_flags_detaching(vlp
)) {
496 /* parent is detaching */
503 ** vlan, etc. routines
507 vlan_globals_init(void)
511 vlan_assert_lock_not_held();
513 if (g_vlan
!= NULL
) {
516 v
= _MALLOC(sizeof(*v
), M_VLAN
, M_WAITOK
);
518 LIST_INIT(&v
->parent_list
);
522 if (g_vlan
!= NULL
) {
538 siocgifdevmtu(struct ifnet
* ifp
, struct ifdevmtu
* ifdm_p
)
543 bzero(&ifr
, sizeof(ifr
));
544 error
= ifnet_ioctl(ifp
, 0,SIOCGIFDEVMTU
, &ifr
);
546 *ifdm_p
= ifr
.ifr_devmtu
;
552 siocsifaltmtu(struct ifnet
* ifp
, int mtu
)
556 bzero(&ifr
, sizeof(ifr
));
558 return (ifnet_ioctl(ifp
, 0, SIOCSIFALTMTU
, &ifr
));
561 static __inline__
void
562 vlan_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
563 bpf_packet_func func
)
571 static __inline__
void
572 vlan_bpf_input(struct ifnet
* ifp
, struct mbuf
* m
,
573 bpf_packet_func func
, char * frame_header
,
574 int frame_header_len
, int encap_len
)
578 /* present the right header to bpf */
579 bcopy(frame_header
, frame_header
+ encap_len
, frame_header_len
);
581 m
->m_data
-= frame_header_len
;
582 m
->m_len
+= frame_header_len
;
584 m
->m_data
+= frame_header_len
;
585 m
->m_len
-= frame_header_len
;
587 /* restore the header */
588 bcopy(frame_header
+ encap_len
, frame_header
, frame_header_len
);
595 ** vlan_parent synchronization routines
598 vlan_parent_retain(vlan_parent_ref vlp
)
600 if (vlp
->vlp_signature
!= VLP_SIGNATURE
) {
601 panic("vlan_parent_retain: signature is bad\n");
603 if (vlp
->vlp_retain_count
== 0) {
604 panic("vlan_parent_retain: retain count is 0\n");
606 OSIncrementAtomic(&vlp
->vlp_retain_count
);
610 vlan_parent_release(vlan_parent_ref vlp
)
612 UInt32 old_retain_count
;
614 if (vlp
->vlp_signature
!= VLP_SIGNATURE
) {
615 panic("vlan_parent_release: signature is bad\n");
617 old_retain_count
= OSDecrementAtomic(&vlp
->vlp_retain_count
);
618 switch (old_retain_count
) {
620 panic("vlan_parent_release: retain count is 0\n");
623 if (g_vlan
->verbose
) {
624 struct ifnet
* ifp
= vlp
->vlp_ifp
;
625 printf("vlan_parent_release(%s%d)\n", ifnet_name(ifp
),
628 vlp
->vlp_signature
= 0;
638 * Function: vlan_parent_wait
640 * Allows a single thread to gain exclusive access to the vlan_parent
641 * data structure. Some operations take a long time to complete,
642 * and some have side-effects that we can't predict. Holding the
643 * vlan_lock() across such operations is not possible.
646 * Before calling, you must be holding the vlan_lock and have taken
647 * a reference on the vlan_parent_ref.
650 vlan_parent_wait(vlan_parent_ref vlp
, const char * msg
)
654 /* other add/remove/multicast-change in progress */
655 while (vlan_parent_flags_change_in_progress(vlp
)) {
656 if (g_vlan
->verbose
) {
657 struct ifnet
* ifp
= vlp
->vlp_ifp
;
659 printf("%s%d: %s msleep\n", ifnet_name(ifp
), ifnet_unit(ifp
), msg
);
662 (void)msleep(vlp
, vlan_lck_mtx
, PZERO
, msg
, 0);
664 /* prevent other vlan parent remove/add from taking place */
665 vlan_parent_flags_set_change_in_progress(vlp
);
666 if (g_vlan
->verbose
&& waited
) {
667 struct ifnet
* ifp
= vlp
->vlp_ifp
;
669 printf("%s%d: %s woke up\n", ifnet_name(ifp
), ifnet_unit(ifp
), msg
);
675 * Function: vlan_parent_signal
677 * Allows the thread that previously invoked vlan_parent_wait() to
678 * give up exclusive access to the vlan_parent data structure, and wake up
679 * any other threads waiting to access
681 * Before calling, you must be holding the vlan_lock and have taken
682 * a reference on the vlan_parent_ref.
685 vlan_parent_signal(vlan_parent_ref vlp
, const char * msg
)
687 vlan_parent_flags_clear_change_in_progress(vlp
);
688 wakeup((caddr_t
)vlp
);
689 if (g_vlan
->verbose
) {
690 struct ifnet
* ifp
= vlp
->vlp_ifp
;
692 printf("%s%d: %s wakeup\n", ifnet_name(ifp
), ifnet_unit(ifp
), msg
);
698 * Program our multicast filter. What we're actually doing is
699 * programming the multicast filter of the parent. This has the
700 * side effect of causing the parent interface to receive multicast
701 * traffic that it doesn't really want, which ends up being discarded
702 * later by the upper protocol layers. Unfortunately, there's no way
703 * to avoid this: there really is only one physical interface.
706 vlan_setmulti(struct ifnet
* ifp
)
711 vlan_parent_ref vlp
= NULL
;
714 ifv
= ifnet_get_ifvlan_retained(ifp
);
718 vlp
= ifvlan_get_vlan_parent_retained(ifv
);
720 /* no parent, no need to program the multicast filter */
723 vlan_parent_wait(vlp
, "vlan_setmulti");
725 /* check again, things could have changed */
726 if (ifnet_ifvlan_vlan_parent_ok(ifp
, ifv
, vlp
) == FALSE
) {
732 /* update parent interface with our multicast addresses */
733 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
738 vlan_parent_signal(vlp
, "vlan_setmulti");
746 vlan_parent_release(vlp
);
752 ** vlan_parent list manipulation/lookup routines
754 static vlan_parent_ref
755 parent_list_lookup(struct ifnet
* p
)
759 LIST_FOREACH(vlp
, &g_vlan
->parent_list
, vlp_parent_list
) {
760 if (vlp
->vlp_ifp
== p
) {
768 vlan_parent_lookup_tag(vlan_parent_ref vlp
, int tag
)
772 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
773 if (tag
== ifv
->ifv_tag
) {
781 vlan_lookup_parent_and_tag(struct ifnet
* p
, int tag
)
785 vlp
= parent_list_lookup(p
);
787 return (vlan_parent_lookup_tag(vlp
, tag
));
793 vlan_parent_find_max_mtu(vlan_parent_ref vlp
, ifvlan_ref exclude_ifv
)
798 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
801 if (exclude_ifv
== ifv
) {
804 req_mtu
= ifnet_mtu(ifv
->ifv_ifp
) + ifv
->ifv_mtufudge
;
805 if (req_mtu
> max_mtu
) {
813 * Function: vlan_parent_create
815 * Create a vlan_parent structure to hold the VLAN's for the given
816 * interface. Add it to the list of VLAN parents.
819 vlan_parent_create(struct ifnet
* p
, vlan_parent_ref
* ret_vlp
)
825 vlp
= _MALLOC(sizeof(*vlp
), M_VLAN
, M_WAITOK
);
829 bzero(vlp
, sizeof(*vlp
));
830 error
= siocgifdevmtu(p
, &vlp
->vlp_devmtu
);
832 printf("vlan_parent_create (%s%d): siocgifdevmtu failed, %d\n",
833 ifnet_name(p
), ifnet_unit(p
), error
);
837 LIST_INIT(&vlp
->vlp_vlan_list
);
839 vlp
->vlp_retain_count
= 1;
840 vlp
->vlp_signature
= VLP_SIGNATURE
;
842 & (IF_HWASSIST_VLAN_MTU
| IF_HWASSIST_VLAN_TAGGING
)) {
843 vlan_parent_flags_set_supports_vlan_mtu(vlp
);
850 vlan_parent_remove_all_vlans(struct ifnet
* p
)
853 int need_vlp_release
= 0;
858 vlp
= parent_list_lookup(p
);
859 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
864 vlan_parent_flags_set_detaching(vlp
);
865 vlan_parent_retain(vlp
);
866 vlan_parent_wait(vlp
, "vlan_parent_remove_all_vlans");
868 vlp
= parent_list_lookup(p
);
874 for (ifv
= LIST_FIRST(&vlp
->vlp_vlan_list
); ifv
!= NULL
; ifv
= next
) {
875 struct ifnet
* ifp
= ifv
->ifv_ifp
;
878 next
= LIST_NEXT(ifv
, ifv_vlan_list
);
879 removed
= vlan_remove(ifv
, FALSE
);
887 /* the vlan parent has no more VLAN's */
888 ifnet_set_eflags(p
, 0, IFEF_VLAN
); /* clear IFEF_VLAN */
890 LIST_REMOVE(vlp
, vlp_parent_list
);
891 need_vlp_release
++; /* one for being in the list */
892 need_vlp_release
++; /* final reference */
895 vlan_parent_signal(vlp
, "vlan_parent_remove_all_vlans");
898 while (need_vlp_release
--) {
899 vlan_parent_release(vlp
);
904 static __inline__
int
905 vlan_parent_no_vlans(vlan_parent_ref vlp
)
907 return (LIST_EMPTY(&vlp
->vlp_vlan_list
));
911 vlan_parent_add_vlan(vlan_parent_ref vlp
, ifvlan_ref ifv
, int tag
)
913 LIST_INSERT_HEAD(&vlp
->vlp_vlan_list
, ifv
, ifv_vlan_list
);
920 vlan_parent_remove_vlan(__unused vlan_parent_ref vlp
, ifvlan_ref ifv
)
923 LIST_REMOVE(ifv
, ifv_vlan_list
);
928 vlan_clone_attach(void)
932 error
= if_clone_attach(&vlan_cloner
);
940 vlan_clone_create(struct if_clone
*ifc
, u_int32_t unit
, __unused
void *params
)
945 struct ifnet_init_params vlan_init
;
947 error
= vlan_globals_init();
951 ifv
= _MALLOC(sizeof(struct ifvlan
), M_VLAN
, M_WAITOK
);
954 bzero(ifv
, sizeof(struct ifvlan
));
955 ifv
->ifv_retain_count
= 1;
956 ifv
->ifv_signature
= IFV_SIGNATURE
;
957 multicast_list_init(&ifv
->ifv_multicast
);
959 /* use the interface name as the unique id for ifp recycle */
961 snprintf(ifv
->ifv_name
, sizeof(ifv
->ifv_name
), "%s%d",
962 ifc
->ifc_name
, unit
) >= sizeof(ifv
->ifv_name
)) {
967 bzero(&vlan_init
, sizeof(vlan_init
));
968 vlan_init
.uniqueid
= ifv
->ifv_name
;
969 vlan_init
.uniqueid_len
= strlen(ifv
->ifv_name
);
970 vlan_init
.name
= ifc
->ifc_name
;
971 vlan_init
.unit
= unit
;
972 vlan_init
.family
= IFNET_FAMILY_VLAN
;
973 vlan_init
.type
= IFT_L2VLAN
;
974 vlan_init
.output
= vlan_output
;
975 vlan_init
.demux
= ether_demux
;
976 vlan_init
.add_proto
= ether_add_proto
;
977 vlan_init
.del_proto
= ether_del_proto
;
978 vlan_init
.check_multi
= ether_check_multi
;
979 vlan_init
.framer
= ether_frameout
;
980 vlan_init
.softc
= ifv
;
981 vlan_init
.ioctl
= vlan_ioctl
;
982 vlan_init
.set_bpf_tap
= vlan_set_bpf_tap
;
983 vlan_init
.detach
= vlan_if_free
;
984 vlan_init
.broadcast_addr
= etherbroadcastaddr
;
985 vlan_init
.broadcast_len
= ETHER_ADDR_LEN
;
986 error
= ifnet_allocate(&vlan_init
, &ifp
);
993 ifnet_set_offload(ifp
, 0);
994 ifnet_set_addrlen(ifp
, ETHER_ADDR_LEN
); /* XXX ethernet specific */
995 ifnet_set_baudrate(ifp
, 0);
996 ifnet_set_hdrlen(ifp
, ETHER_VLAN_ENCAP_LEN
);
998 error
= ifnet_attach(ifp
, NULL
);
1001 ifvlan_release(ifv
);
1006 /* attach as ethernet */
1007 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
1012 vlan_remove(ifvlan_ref ifv
, int need_to_wait
)
1014 vlan_assert_lock_held();
1015 if (ifvlan_flags_detaching(ifv
)) {
1018 ifvlan_flags_set_detaching(ifv
);
1019 vlan_unconfig(ifv
, need_to_wait
);
1025 vlan_clone_destroy(struct ifnet
*ifp
)
1030 ifv
= ifnet_get_ifvlan_retained(ifp
);
1035 if (vlan_remove(ifv
, TRUE
) == 0) {
1037 ifvlan_release(ifv
);
1041 ifvlan_release(ifv
);
1048 vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
1053 ifv
= ifnet_get_ifvlan_retained(ifp
);
1059 case BPF_TAP_DISABLE
:
1060 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= NULL
;
1064 ifv
->ifv_bpf_input
= func
;
1067 case BPF_TAP_OUTPUT
:
1068 ifv
->ifv_bpf_output
= func
;
1071 case BPF_TAP_INPUT_OUTPUT
:
1072 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= func
;
1078 ifvlan_release(ifv
);
1083 vlan_output(struct ifnet
* ifp
, struct mbuf
* m
)
1085 bpf_packet_func bpf_func
;
1086 struct ether_vlan_header
* evl
;
1092 vlan_parent_ref vlp
= NULL
;
1094 struct flowadv adv
= { FADV_SUCCESS
};
1099 if ((m
->m_flags
& M_PKTHDR
) == 0) {
1104 ifv
= ifnet_get_ifvlan_retained(ifp
);
1105 if (ifv
== NULL
|| ifvlan_flags_ready(ifv
) == 0) {
1108 vlp
= ifvlan_get_vlan_parent_retained(ifv
);
1113 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
1114 soft_vlan
= (ifnet_offload(p
) & IF_HWASSIST_VLAN_TAGGING
) == 0;
1115 bpf_func
= ifv
->ifv_bpf_output
;
1117 encaplen
= ifv
->ifv_encaplen
;
1120 ifvlan_release(ifv
);
1121 vlan_parent_release(vlp
);
1123 vlan_bpf_output(ifp
, m
, bpf_func
);
1125 /* do not run parent's if_output() if the parent is not up */
1126 if ((ifnet_flags(p
) & (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
)) {
1128 atomic_add_64(&ifp
->if_collisions
, 1);
1132 * If underlying interface can do VLAN tag insertion itself,
1133 * just pass the packet along. However, we need some way to
1134 * tell the interface where the packet came from so that it
1135 * knows how to find the VLAN tag to use. We use a field in
1136 * the mbuf header to store the VLAN tag, and a bit in the
1137 * csum_flags field to mark the field as valid.
1139 if (soft_vlan
== 0) {
1140 m
->m_pkthdr
.csum_flags
|= CSUM_VLAN_TAG_VALID
;
1141 m
->m_pkthdr
.vlan_tag
= tag
;
1143 M_PREPEND(m
, encaplen
, M_DONTWAIT
);
1145 printf("%s%d: unable to prepend VLAN header\n", ifnet_name(ifp
),
1147 atomic_add_64(&ifp
->if_oerrors
, 1);
1150 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1151 if (m
->m_len
< (int)sizeof(*evl
)) {
1152 m
= m_pullup(m
, sizeof(*evl
));
1154 printf("%s%d: unable to pullup VLAN header\n", ifnet_name(ifp
),
1156 atomic_add_64(&ifp
->if_oerrors
, 1);
1162 * Transform the Ethernet header into an Ethernet header
1163 * with 802.1Q encapsulation.
1165 bcopy(mtod(m
, char *) + encaplen
,
1166 mtod(m
, char *), ETHER_HDR_LEN
);
1167 evl
= mtod(m
, struct ether_vlan_header
*);
1168 evl
->evl_proto
= evl
->evl_encap_proto
;
1169 evl
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
1170 evl
->evl_tag
= htons(tag
);
1173 err
= dlil_output(p
, PF_VLAN
, m
, NULL
, NULL
, 1, &adv
);
1176 if (adv
.code
== FADV_FLOW_CONTROLLED
) {
1178 } else if (adv
.code
== FADV_SUSPENDED
) {
1188 ifvlan_release(ifv
);
1191 vlan_parent_release(vlp
);
1199 vlan_input(ifnet_t p
, __unused protocol_family_t protocol
,
1200 mbuf_t m
, char *frame_header
)
1202 bpf_packet_func bpf_func
= NULL
;
1203 struct ether_vlan_header
* evl
;
1204 struct ifnet
* ifp
= NULL
;
1208 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1210 * Packet is tagged, m contains a normal
1211 * Ethernet frame; the tag is stored out-of-band.
1213 m
->m_pkthdr
.csum_flags
&= ~CSUM_VLAN_TAG_VALID
;
1214 tag
= EVL_VLANOFTAG(m
->m_pkthdr
.vlan_tag
);
1215 m
->m_pkthdr
.vlan_tag
= 0;
1218 switch (ifnet_type(p
)) {
1220 if (m
->m_len
< ETHER_VLAN_ENCAP_LEN
) {
1224 evl
= (struct ether_vlan_header
*)(void *)frame_header
;
1225 if (ntohs(evl
->evl_proto
) == ETHERTYPE_VLAN
) {
1226 /* don't allow VLAN within VLAN */
1230 tag
= EVL_VLANOFTAG(ntohs(evl
->evl_tag
));
1233 * Restore the original ethertype. We'll remove
1234 * the encapsulation after we've found the vlan
1235 * interface corresponding to the tag.
1237 evl
->evl_encap_proto
= evl
->evl_proto
;
1240 printf("vlan_demux: unsupported if type %u",
1250 if ((ifnet_eflags(p
) & IFEF_VLAN
) == 0) {
1251 /* don't bother looking through the VLAN list */
1256 ifv
= vlan_lookup_parent_and_tag(p
, tag
);
1261 || ifvlan_flags_ready(ifv
) == 0
1262 || (ifnet_flags(ifp
) & IFF_UP
) == 0) {
1267 bpf_func
= ifv
->ifv_bpf_input
;
1272 * Packet had an in-line encapsulation header;
1273 * remove it. The original header has already
1274 * been fixed up above.
1276 m
->m_len
-= ETHER_VLAN_ENCAP_LEN
;
1277 m
->m_data
+= ETHER_VLAN_ENCAP_LEN
;
1278 m
->m_pkthdr
.len
-= ETHER_VLAN_ENCAP_LEN
;
1279 m
->m_pkthdr
.csum_flags
= 0; /* can't trust hardware checksum */
1282 m
->m_pkthdr
.rcvif
= ifp
;
1283 m
->m_pkthdr
.header
= frame_header
;
1284 (void)ifnet_stat_increment_in(ifp
, 1,
1285 m
->m_pkthdr
.len
+ ETHER_HDR_LEN
, 0);
1286 vlan_bpf_input(ifp
, m
, bpf_func
, frame_header
, ETHER_HDR_LEN
,
1287 soft_vlan
? ETHER_VLAN_ENCAP_LEN
: 0);
1288 /* We found a vlan interface, inject on that interface. */
1289 dlil_input_packet_list(ifp
, m
);
1291 m
->m_pkthdr
.header
= frame_header
;
1292 /* Send priority-tagged packet up through the parent */
1293 dlil_input_packet_list(p
, m
);
1299 vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
)
1302 int first_vlan
= FALSE
;
1303 ifvlan_ref ifv
= NULL
;
1304 int ifv_added
= FALSE
;
1305 int need_vlp_release
= 0;
1306 vlan_parent_ref new_vlp
= NULL
;
1307 ifnet_offload_t offload
;
1308 u_int16_t parent_flags
;
1309 vlan_parent_ref vlp
= NULL
;
1311 /* pre-allocate space for vlan_parent, in case we're first */
1312 error
= vlan_parent_create(p
, &new_vlp
);
1318 ifv
= ifnet_get_ifvlan_retained(ifp
);
1319 if (ifv
== NULL
|| ifv
->ifv_vlp
!= NULL
) {
1322 ifvlan_release(ifv
);
1324 vlan_parent_release(new_vlp
);
1327 vlp
= parent_list_lookup(p
);
1329 vlan_parent_retain(vlp
);
1331 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1332 /* already a VLAN with that tag on this interface */
1338 /* one for being in the list */
1339 vlan_parent_retain(new_vlp
);
1341 /* we're the first VLAN on this interface */
1342 LIST_INSERT_HEAD(&g_vlan
->parent_list
, new_vlp
, vlp_parent_list
);
1345 vlan_parent_retain(vlp
);
1349 /* need to wait to ensure no one else is trying to add/remove */
1350 vlan_parent_wait(vlp
, "vlan_config");
1352 if (ifnet_get_ifvlan(ifp
) != ifv
) {
1357 /* check again because someone might have gotten in */
1358 if (parent_list_lookup(p
) != vlp
) {
1363 if (vlan_parent_flags_detaching(vlp
)
1364 || ifvlan_flags_detaching(ifv
) || ifv
->ifv_vlp
!= NULL
) {
1369 /* check again because someone might have gotten the tag */
1370 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1371 /* already a VLAN with that tag on this interface */
1376 if (vlan_parent_no_vlans(vlp
)) {
1379 vlan_parent_add_vlan(vlp
, ifv
, tag
);
1380 ifvlan_retain(ifv
); /* parent references ifv */
1383 /* check whether bond interface is using parent interface */
1384 ifnet_lock_exclusive(p
);
1385 if ((ifnet_eflags(p
) & IFEF_BOND
) != 0) {
1387 /* don't allow VLAN over interface that's already part of a bond */
1391 /* prevent BOND interface from using it */
1392 /* Can't use ifnet_set_eflags because that would take the lock */
1393 p
->if_eflags
|= IFEF_VLAN
;
1398 /* attach our VLAN "protocol" to the interface */
1399 error
= vlan_attach_protocol(p
);
1404 /* mark the parent interface up */
1405 ifnet_set_flags(p
, IFF_UP
, IFF_UP
);
1406 (void)ifnet_ioctl(p
, 0, SIOCSIFFLAGS
, (caddr_t
)NULL
);
1409 /* configure parent to receive our multicast addresses */
1410 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
1413 (void)vlan_detach_protocol(p
);
1419 /* set our ethernet address to that of the parent */
1420 ifnet_set_lladdr_and_type(ifp
, ifnet_lladdr(p
), ETHER_ADDR_LEN
, IFT_ETHER
);
1422 /* no failures past this point */
1425 ifv
->ifv_encaplen
= ETHER_VLAN_ENCAP_LEN
;
1427 if (vlan_parent_flags_supports_vlan_mtu(vlp
)) {
1428 ifv
->ifv_mtufudge
= 0;
1431 * Fudge the MTU by the encapsulation size. This
1432 * makes us incompatible with strictly compliant
1433 * 802.1Q implementations, but allows us to use
1434 * the feature with other NetBSD implementations,
1435 * which might still be useful.
1437 ifv
->ifv_mtufudge
= ifv
->ifv_encaplen
;
1439 ifnet_set_mtu(ifp
, ETHERMTU
- ifv
->ifv_mtufudge
);
1442 * Copy only a selected subset of flags from the parent.
1443 * Other flags are none of our business.
1445 parent_flags
= ifnet_flags(p
)
1446 & (IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
);
1447 ifnet_set_flags(ifp
, parent_flags
,
1448 IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
);
1450 /* use hwassist bits from parent interface, but exclude VLAN bits */
1451 offload
= ifnet_offload(p
) & ~(IFNET_VLAN_TAGGING
| IFNET_VLAN_MTU
);
1452 ifnet_set_offload(ifp
, offload
);
1454 ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
1455 ifvlan_flags_set_ready(ifv
);
1456 vlan_parent_signal(vlp
, "vlan_config");
1458 if (new_vlp
!= vlp
) {
1459 /* throw it away, it wasn't needed */
1460 vlan_parent_release(new_vlp
);
1463 ifvlan_release(ifv
);
1468 vlan_assert_lock_held();
1471 vlan_parent_remove_vlan(vlp
, ifv
);
1472 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1473 /* the vlan parent has no more VLAN's */
1474 ifnet_set_eflags(p
, 0, IFEF_VLAN
);
1475 LIST_REMOVE(vlp
, vlp_parent_list
);
1476 /* release outside of the lock below */
1479 /* one for being in the list */
1483 vlan_parent_signal(vlp
, "vlan_config");
1488 while (need_vlp_release
--) {
1489 vlan_parent_release(vlp
);
1491 if (new_vlp
!= vlp
) {
1492 vlan_parent_release(new_vlp
);
1496 ifvlan_release(ifv
);
1498 ifvlan_release(ifv
);
1504 vlan_link_event(struct ifnet
* ifp
, struct ifnet
* p
)
1506 struct ifmediareq ifmr
;
1508 /* generate a link event based on the state of the underlying interface */
1509 bzero(&ifmr
, sizeof(ifmr
));
1510 snprintf(ifmr
.ifm_name
, sizeof(ifmr
.ifm_name
),
1511 "%s%d", ifnet_name(p
), ifnet_unit(p
));
1512 if (ifnet_ioctl(p
, 0, SIOCGIFMEDIA
, &ifmr
) == 0
1513 && ifmr
.ifm_count
> 0 && ifmr
.ifm_status
& IFM_AVALID
) {
1516 event
= (ifmr
.ifm_status
& IFM_ACTIVE
)
1517 ? KEV_DL_LINK_ON
: KEV_DL_LINK_OFF
;
1518 interface_link_event(ifp
, event
);
1524 vlan_unconfig(ifvlan_ref ifv
, int need_to_wait
)
1526 struct ifnet
* ifp
= ifv
->ifv_ifp
;
1527 int last_vlan
= FALSE
;
1528 int need_ifv_release
= 0;
1529 int need_vlp_release
= 0;
1531 vlan_parent_ref vlp
;
1533 vlan_assert_lock_held();
1540 vlan_parent_retain(vlp
);
1541 vlan_parent_wait(vlp
, "vlan_unconfig");
1543 /* check again because another thread could be in vlan_unconfig */
1544 if (ifv
!= ifnet_get_ifvlan(ifp
)) {
1547 if (ifv
->ifv_vlp
!= vlp
) {
1548 /* vlan parent changed */
1553 /* ifv has a reference on vlp, need to remove it */
1557 /* remember whether we're the last VLAN on the parent */
1558 if (LIST_NEXT(LIST_FIRST(&vlp
->vlp_vlan_list
), ifv_vlan_list
) == NULL
) {
1559 if (g_vlan
->verbose
) {
1560 printf("vlan_unconfig: last vlan on %s%d\n",
1561 ifnet_name(p
), ifnet_unit(p
));
1566 /* back-out any effect our mtu might have had on the parent */
1567 (void)ifvlan_new_mtu(ifv
, ETHERMTU
- ifv
->ifv_mtufudge
);
1571 /* un-join multicast on parent interface */
1572 (void)multicast_list_remove(&ifv
->ifv_multicast
);
1574 /* Clear our MAC address. */
1575 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_L2VLAN
);
1577 /* detach VLAN "protocol" */
1579 (void)vlan_detach_protocol(p
);
1584 /* return to the state we were in before SIFVLAN */
1585 ifnet_set_mtu(ifp
, 0);
1586 ifnet_set_flags(ifp
, 0,
1587 IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
| IFF_RUNNING
);
1588 ifnet_set_offload(ifp
, 0);
1589 ifv
->ifv_mtufudge
= 0;
1591 /* Disconnect from parent. */
1592 vlan_parent_remove_vlan(vlp
, ifv
);
1595 /* vlan_parent has reference to ifv, remove it */
1598 /* from this point on, no more referencing ifv */
1599 if (last_vlan
&& !vlan_parent_flags_detaching(vlp
)) {
1600 /* the vlan parent has no more VLAN's */
1601 ifnet_set_eflags(p
, 0, IFEF_VLAN
);
1602 LIST_REMOVE(vlp
, vlp_parent_list
);
1604 /* one for being in the list */
1607 /* release outside of the lock below */
1613 vlan_parent_signal(vlp
, "vlan_unconfig");
1616 while (need_ifv_release
--) {
1617 ifvlan_release(ifv
);
1619 while (need_vlp_release
--) { /* references to vlp */
1620 vlan_parent_release(vlp
);
1627 vlan_set_promisc(struct ifnet
* ifp
)
1631 vlan_parent_ref vlp
;
1634 ifv
= ifnet_get_ifvlan_retained(ifp
);
1644 if ((ifnet_flags(ifp
) & IFF_PROMISC
) != 0) {
1645 if (!ifvlan_flags_promisc(ifv
)) {
1646 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 1);
1648 ifvlan_flags_set_promisc(ifv
);
1652 if (ifvlan_flags_promisc(ifv
)) {
1653 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 0);
1655 ifvlan_flags_clear_promisc(ifv
);
1662 ifvlan_release(ifv
);
1668 ifvlan_new_mtu(ifvlan_ref ifv
, int mtu
)
1670 struct ifdevmtu
* devmtu_p
;
1672 struct ifnet
* ifp
= ifv
->ifv_ifp
;
1676 vlan_parent_ref vlp
;
1678 vlan_assert_lock_held();
1680 devmtu_p
= &vlp
->vlp_devmtu
;
1681 req_mtu
= mtu
+ ifv
->ifv_mtufudge
;
1682 if (req_mtu
> devmtu_p
->ifdm_max
|| req_mtu
< devmtu_p
->ifdm_min
) {
1685 max_mtu
= vlan_parent_find_max_mtu(vlp
, ifv
);
1686 if (req_mtu
> max_mtu
) {
1689 else if (max_mtu
< devmtu_p
->ifdm_current
) {
1693 struct ifnet
* p
= vlp
->vlp_ifp
;
1695 error
= siocsifaltmtu(p
, new_mtu
);
1700 devmtu_p
->ifdm_current
= new_mtu
;
1702 ifnet_set_mtu(ifp
, mtu
);
1708 vlan_set_mtu(struct ifnet
* ifp
, int mtu
)
1712 vlan_parent_ref vlp
;
1714 if (mtu
< IF_MINMTU
) {
1718 ifv
= ifnet_get_ifvlan_retained(ifp
);
1723 vlp
= ifvlan_get_vlan_parent_retained(ifv
);
1726 ifvlan_release(ifv
);
1732 vlan_parent_wait(vlp
, "vlan_set_mtu");
1734 /* check again, something might have changed */
1735 if (ifnet_get_ifvlan(ifp
) != ifv
1736 || ifvlan_flags_detaching(ifv
)) {
1740 if (ifv
->ifv_vlp
!= vlp
) {
1741 /* vlan parent changed */
1744 if (vlan_parent_flags_detaching(vlp
)) {
1750 error
= ifvlan_new_mtu(ifv
, mtu
);
1753 vlan_parent_signal(vlp
, "vlan_set_mtu");
1755 vlan_parent_release(vlp
);
1756 ifvlan_release(ifv
);
1762 vlan_ioctl(ifnet_t ifp
, u_long cmd
, void * data
)
1764 struct ifdevmtu
* devmtu_p
;
1766 struct ifaddr
* ifa
;
1767 struct ifmediareq
*ifmr
;
1772 user_addr_t user_addr
;
1773 vlan_parent_ref vlp
;
1776 if (ifnet_type(ifp
) != IFT_L2VLAN
) {
1777 return (EOPNOTSUPP
);
1779 ifr
= (struct ifreq
*)data
;
1780 ifa
= (struct ifaddr
*)data
;
1784 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
1787 case SIOCGIFMEDIA32
:
1788 case SIOCGIFMEDIA64
:
1790 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1791 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1793 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1795 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1797 ifmr
= (struct ifmediareq
*)data
;
1798 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
1799 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
1800 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
1802 struct ifmediareq p_ifmr
;
1804 bzero(&p_ifmr
, sizeof(p_ifmr
));
1805 error
= ifnet_ioctl(p
, 0, SIOCGIFMEDIA
, &p_ifmr
);
1807 ifmr
->ifm_active
= p_ifmr
.ifm_active
;
1808 ifmr
->ifm_current
= p_ifmr
.ifm_current
;
1809 ifmr
->ifm_mask
= p_ifmr
.ifm_mask
;
1810 ifmr
->ifm_status
= p_ifmr
.ifm_status
;
1811 ifmr
->ifm_count
= p_ifmr
.ifm_count
;
1812 /* Limit the result to the parent's current config. */
1813 if (ifmr
->ifm_count
>= 1 && user_addr
!= USER_ADDR_NULL
) {
1814 ifmr
->ifm_count
= 1;
1815 error
= copyout(&ifmr
->ifm_current
, user_addr
,
1820 ifmr
->ifm_active
= ifmr
->ifm_current
= IFM_NONE
;
1822 ifmr
->ifm_status
= IFM_AVALID
;
1823 ifmr
->ifm_count
= 1;
1824 if (user_addr
!= USER_ADDR_NULL
) {
1825 error
= copyout(&ifmr
->ifm_current
, user_addr
, sizeof(int));
1836 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1837 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1839 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1843 int min_mtu
= vlp
->vlp_devmtu
.ifdm_min
- ifv
->ifv_mtufudge
;
1844 devmtu_p
= &ifr
->ifr_devmtu
;
1845 devmtu_p
->ifdm_current
= ifnet_mtu(ifp
);
1846 devmtu_p
->ifdm_min
= max(min_mtu
, IF_MINMTU
);
1847 devmtu_p
->ifdm_max
= vlp
->vlp_devmtu
.ifdm_max
- ifv
->ifv_mtufudge
;
1850 devmtu_p
= &ifr
->ifr_devmtu
;
1851 devmtu_p
->ifdm_current
= 0;
1852 devmtu_p
->ifdm_min
= 0;
1853 devmtu_p
->ifdm_max
= 0;
1859 error
= vlan_set_mtu(ifp
, ifr
->ifr_mtu
);
1863 user_addr
= proc_is64bit(current_proc())
1864 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1865 error
= copyin(user_addr
, &vlr
, sizeof(vlr
));
1870 if (vlr
.vlr_parent
[0] != '\0') {
1871 if (vlr
.vlr_tag
& ~EVL_VLID_MASK
) {
1873 * Don't let the caller set up a VLAN tag with
1874 * anything except VLID bits.
1879 p
= ifunit(vlr
.vlr_parent
);
1884 /* can't do VLAN over anything but ethernet or ethernet aggregate */
1885 if (ifnet_type(p
) != IFT_ETHER
1886 && ifnet_type(p
) != IFT_IEEE8023ADLAG
) {
1887 error
= EPROTONOSUPPORT
;
1890 error
= vlan_config(ifp
, p
, vlr
.vlr_tag
);
1895 /* Update promiscuous mode, if necessary. */
1896 (void)vlan_set_promisc(ifp
);
1898 /* generate a link event based on the state of the parent */
1899 vlan_link_event(ifp
, p
);
1902 int need_link_event
= FALSE
;
1905 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1906 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1908 error
= (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1911 need_link_event
= vlan_remove(ifv
, TRUE
);
1913 if (need_link_event
) {
1914 interface_link_event(ifp
, KEV_DL_LINK_OFF
);
1920 bzero(&vlr
, sizeof vlr
);
1922 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1923 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1925 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1927 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1931 snprintf(vlr
.vlr_parent
, sizeof(vlr
.vlr_parent
),
1932 "%s%d", ifnet_name(p
), ifnet_unit(p
));
1935 user_addr
= proc_is64bit(current_proc())
1936 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1937 error
= copyout(&vlr
, user_addr
, sizeof(vlr
));
1942 * For promiscuous mode, we enable promiscuous mode on
1943 * the parent if we need promiscuous on the VLAN interface.
1945 error
= vlan_set_promisc(ifp
);
1950 error
= vlan_setmulti(ifp
);
1959 vlan_if_free(struct ifnet
* ifp
)
1966 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1970 ifvlan_release(ifv
);
1976 vlan_event(struct ifnet
* p
, __unused protocol_family_t protocol
,
1977 const struct kev_msg
* event
)
1981 /* Check if the interface we are attached to is being detached */
1982 if (event
->vendor_code
!= KEV_VENDOR_APPLE
1983 || event
->kev_class
!= KEV_NETWORK_CLASS
1984 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
1987 event_code
= event
->event_code
;
1988 switch (event_code
) {
1989 case KEV_DL_LINK_OFF
:
1990 case KEV_DL_LINK_ON
:
1991 vlan_parent_link_event(p
, event_code
);
2000 vlan_detached(ifnet_t p
, __unused protocol_family_t protocol
)
2002 if (ifnet_is_attached(p
, 0) == 0) {
2003 /* if the parent isn't attached, remove all VLANs */
2004 vlan_parent_remove_all_vlans(p
);
2010 interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
)
2013 struct kern_event_msg header
;
2015 char if_name
[IFNAMSIZ
];
2018 bzero(&event
, sizeof(event
));
2019 event
.header
.total_size
= sizeof(event
);
2020 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
2021 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
2022 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
2023 event
.header
.event_code
= event_code
;
2024 event
.header
.event_data
[0] = ifnet_family(ifp
);
2025 event
.unit
= (u_int32_t
) ifnet_unit(ifp
);
2026 strncpy(event
.if_name
, ifnet_name(ifp
), IFNAMSIZ
);
2027 ifnet_event(ifp
, &event
.header
);
2032 vlan_parent_link_event(struct ifnet
* p
, u_int32_t event_code
)
2035 vlan_parent_ref vlp
;
2038 if ((ifnet_eflags(p
) & IFEF_VLAN
) == 0) {
2043 vlp
= parent_list_lookup(p
);
2050 vlan_parent_retain(vlp
);
2051 vlan_parent_wait(vlp
, "vlan_parent_link_event");
2052 if (vlan_parent_flags_detaching(vlp
)) {
2058 /* vlan_parent_wait() gives us exclusive access to the list */
2059 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
2060 struct ifnet
* ifp
= ifv
->ifv_ifp
;
2062 interface_link_event(ifp
, event_code
);
2068 vlan_parent_signal(vlp
, "vlan_parent_link_event");
2070 vlan_parent_release(vlp
);
2076 * Function: vlan_attach_protocol
2078 * Attach a DLIL protocol to the interface, using the ETHERTYPE_VLAN
2081 * The ethernet demux actually special cases VLAN to support hardware.
2082 * The demux here isn't used. The demux will return PF_VLAN for the
2083 * appropriate packets and our vlan_input function will be called.
2086 vlan_attach_protocol(struct ifnet
*ifp
)
2089 struct ifnet_attach_proto_param reg
;
2091 bzero(®
, sizeof(reg
));
2092 reg
.input
= vlan_input
;
2093 reg
.event
= vlan_event
;
2094 reg
.detached
= vlan_detached
;
2095 error
= ifnet_attach_protocol(ifp
, PF_VLAN
, ®
);
2097 printf("vlan_proto_attach(%s%d) ifnet_attach_protocol failed, %d\n",
2098 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
2104 * Function: vlan_detach_protocol
2106 * Detach our DLIL protocol from an interface
2109 vlan_detach_protocol(struct ifnet
*ifp
)
2113 error
= ifnet_detach_protocol(ifp
, PF_VLAN
);
2115 printf("vlan_proto_detach(%s%d) ifnet_detach_protocol failed, %d\n",
2116 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
2123 * DLIL interface family functions
2124 * We use the ethernet plumb functions, since that's all we support.
2125 * If we wanted to handle multiple LAN types (tokenring, etc.), we'd
2126 * call the appropriate routines for that LAN type instead of hard-coding
2130 vlan_attach_inet(struct ifnet
*ifp
, protocol_family_t protocol_family
)
2132 return (ether_attach_inet(ifp
, protocol_family
));
2136 vlan_detach_inet(struct ifnet
*ifp
, protocol_family_t protocol_family
)
2138 ether_detach_inet(ifp
, protocol_family
);
2143 vlan_attach_inet6(struct ifnet
*ifp
, protocol_family_t protocol_family
)
2145 return (ether_attach_inet6(ifp
, protocol_family
));
2149 vlan_detach_inet6(struct ifnet
*ifp
, protocol_family_t protocol_family
)
2151 ether_detach_inet6(ifp
, protocol_family
);
2157 vlan_attach_at(struct ifnet
*ifp
, protocol_family_t protocol_family
)
2159 return (ether_attach_at(ifp
, protocol_family
));
2163 vlan_detach_at(struct ifnet
*ifp
, protocol_family_t protocol_family
)
2165 ether_detach_at(ifp
, protocol_family
);
2169 __private_extern__
int
2170 vlan_family_init(void)
2174 error
= proto_register_plumber(PF_INET
, IFNET_FAMILY_VLAN
,
2175 vlan_attach_inet
, vlan_detach_inet
);
2177 printf("proto_register_plumber failed for AF_INET error=%d\n",
2182 error
= proto_register_plumber(PF_INET6
, IFNET_FAMILY_VLAN
,
2183 vlan_attach_inet6
, vlan_detach_inet6
);
2185 printf("proto_register_plumber failed for AF_INET6 error=%d\n",
2191 error
= proto_register_plumber(PF_APPLETALK
, IFNET_FAMILY_VLAN
,
2192 vlan_attach_at
, vlan_detach_at
);
2194 printf("proto_register_plumber failed for AF_APPLETALK error=%d\n",
2199 error
= vlan_clone_attach();
2201 printf("proto_register_plumber failed vlan_clone_attach error=%d\n",