2 * Copyright (c) 2003-2009 Apple Computer, 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>
84 #include <net/ethernet.h>
86 #include <net/if_arp.h>
87 #include <net/if_dl.h>
88 #include <net/if_ether.h>
89 #include <net/if_types.h>
90 #include <net/if_vlan_var.h>
91 #include <libkern/OSAtomic.h>
95 #include <net/kpi_interface.h>
96 #include <net/kpi_protocol.h>
98 #include <kern/locks.h>
101 #include <netinet/in.h>
102 #include <netinet/if_ether.h>
105 #include <net/if_media.h>
106 #include <net/multicast_list.h>
107 #include <net/ether_if_module.h>
109 #define VLANNAME "vlan"
111 typedef int (bpf_callback_func
)(struct ifnet
*, struct mbuf
*);
112 typedef int (if_set_bpf_tap_func
)(struct ifnet
*ifp
, int mode
, bpf_callback_func
* func
);
117 static __inline__ lck_grp_t
*
118 my_lck_grp_alloc_init(const char * grp_name
)
121 lck_grp_attr_t
* grp_attrs
;
123 grp_attrs
= lck_grp_attr_alloc_init();
124 grp
= lck_grp_alloc_init(grp_name
, grp_attrs
);
125 lck_grp_attr_free(grp_attrs
);
129 static __inline__ lck_mtx_t
*
130 my_lck_mtx_alloc_init(lck_grp_t
* lck_grp
)
132 lck_attr_t
* lck_attrs
;
135 lck_attrs
= lck_attr_alloc_init();
136 lck_mtx
= lck_mtx_alloc_init(lck_grp
, lck_attrs
);
137 lck_attr_free(lck_attrs
);
141 static lck_mtx_t
* vlan_lck_mtx
;
143 static __inline__
void
146 lck_grp_t
* vlan_lck_grp
;
148 vlan_lck_grp
= my_lck_grp_alloc_init("if_vlan");
149 vlan_lck_mtx
= my_lck_mtx_alloc_init(vlan_lck_grp
);
152 static __inline__
void
153 vlan_assert_lock_held(void)
155 lck_mtx_assert(vlan_lck_mtx
, LCK_MTX_ASSERT_OWNED
);
159 static __inline__
void
160 vlan_assert_lock_not_held(void)
162 lck_mtx_assert(vlan_lck_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
166 static __inline__
void
169 lck_mtx_lock(vlan_lck_mtx
);
173 static __inline__
void
176 lck_mtx_unlock(vlan_lck_mtx
);
181 ** vlan structures, types
184 LIST_HEAD(vlan_parent_list
, vlan_parent
);
186 LIST_HEAD(ifvlan_list
, ifvlan
);
188 typedef struct vlan_parent
{
189 LIST_ENTRY(vlan_parent
) vlp_parent_list
;/* list of parents */
190 struct ifnet
* vlp_ifp
; /* interface */
191 struct ifvlan_list vlp_vlan_list
; /* list of VLAN's */
192 #define VLPF_SUPPORTS_VLAN_MTU 0x1
193 #define VLPF_CHANGE_IN_PROGRESS 0x2
194 #define VLPF_DETACHING 0x4
196 struct ifdevmtu vlp_devmtu
;
197 SInt32 vlp_retain_count
;
198 } vlan_parent
, * vlan_parent_ref
;
201 LIST_ENTRY(ifvlan
) ifv_vlan_list
;
202 char ifv_name
[IFNAMSIZ
]; /* our unique id */
203 struct ifnet
* ifv_ifp
; /* our interface */
204 vlan_parent_ref ifv_vlp
; /* parent information */
206 u_int16_t ifvm_encaplen
;/* encapsulation length */
207 u_int16_t ifvm_mtufudge
;/* MTU fudged by this much */
208 u_int16_t ifvm_proto
; /* encapsulation ethertype */
209 u_int16_t ifvm_tag
; /* tag to apply on packets leaving if */
211 struct multicast_list ifv_multicast
;
212 #define IFVF_PROMISC 0x1 /* promiscuous mode enabled */
213 #define IFVF_DETACHING 0x2 /* interface is detaching */
214 #define IFVF_READY 0x4 /* interface is ready */
216 bpf_packet_func ifv_bpf_input
;
217 bpf_packet_func ifv_bpf_output
;
220 typedef struct ifvlan
* ifvlan_ref
;
222 typedef struct vlan_globals_s
{
223 struct vlan_parent_list parent_list
;
225 } * vlan_globals_ref
;
227 static vlan_globals_ref g_vlan
;
229 #define ifv_tag ifv_mib.ifvm_tag
230 #define ifv_encaplen ifv_mib.ifvm_encaplen
231 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
235 ** vlan_parent_ref vlp_flags in-lines
237 static __inline__
int
238 vlan_parent_flags_supports_vlan_mtu(vlan_parent_ref vlp
)
240 return ((vlp
->vlp_flags
& VLPF_SUPPORTS_VLAN_MTU
) != 0);
243 static __inline__
void
244 vlan_parent_flags_set_supports_vlan_mtu(vlan_parent_ref vlp
)
246 vlp
->vlp_flags
|= VLPF_SUPPORTS_VLAN_MTU
;
250 static __inline__
void
251 vlan_parent_flags_clear_supports_vlan_mtu(vlan_parent_ref vlp
)
253 vlp
->vlp_flags
&= ~VLPF_SUPPORTS_VLAN_MTU
;
257 static __inline__
int
258 vlan_parent_flags_change_in_progress(vlan_parent_ref vlp
)
260 return ((vlp
->vlp_flags
& VLPF_CHANGE_IN_PROGRESS
) != 0);
263 static __inline__
void
264 vlan_parent_flags_set_change_in_progress(vlan_parent_ref vlp
)
266 vlp
->vlp_flags
|= VLPF_CHANGE_IN_PROGRESS
;
270 static __inline__
void
271 vlan_parent_flags_clear_change_in_progress(vlan_parent_ref vlp
)
273 vlp
->vlp_flags
&= ~VLPF_CHANGE_IN_PROGRESS
;
277 static __inline__
int
278 vlan_parent_flags_detaching(struct vlan_parent
* vlp
)
280 return ((vlp
->vlp_flags
& VLPF_DETACHING
) != 0);
283 static __inline__
void
284 vlan_parent_flags_set_detaching(struct vlan_parent
* vlp
)
286 vlp
->vlp_flags
|= VLPF_DETACHING
;
292 ** ifvlan_flags in-lines routines
294 static __inline__
int
295 ifvlan_flags_promisc(ifvlan_ref ifv
)
297 return ((ifv
->ifv_flags
& IFVF_PROMISC
) != 0);
300 static __inline__
void
301 ifvlan_flags_set_promisc(ifvlan_ref ifv
)
303 ifv
->ifv_flags
|= IFVF_PROMISC
;
307 static __inline__
void
308 ifvlan_flags_clear_promisc(ifvlan_ref ifv
)
310 ifv
->ifv_flags
&= ~IFVF_PROMISC
;
314 static __inline__
int
315 ifvlan_flags_ready(ifvlan_ref ifv
)
317 return ((ifv
->ifv_flags
& IFVF_READY
) != 0);
320 static __inline__
void
321 ifvlan_flags_set_ready(ifvlan_ref ifv
)
323 ifv
->ifv_flags
|= IFVF_READY
;
327 static __inline__
void
328 ifvlan_flags_clear_ready(ifvlan_ref ifv
)
330 ifv
->ifv_flags
&= ~IFVF_READY
;
334 static __inline__
int
335 ifvlan_flags_detaching(ifvlan_ref ifv
)
337 return ((ifv
->ifv_flags
& IFVF_DETACHING
) != 0);
340 static __inline__
void
341 ifvlan_flags_set_detaching(ifvlan_ref ifv
)
343 ifv
->ifv_flags
|= IFVF_DETACHING
;
348 SYSCTL_DECL(_net_link
);
349 SYSCTL_NODE(_net_link
, IFT_L2VLAN
, vlan
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "IEEE 802.1Q VLAN");
350 SYSCTL_NODE(_net_link_vlan
, PF_LINK
, link
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "for consistency");
353 #define M_VLAN M_DEVBUF
355 static int vlan_clone_create(struct if_clone
*, int);
356 static void vlan_clone_destroy(struct ifnet
*);
357 static int vlan_input(ifnet_t ifp
, protocol_family_t protocol
,
358 mbuf_t m
, char *frame_header
);
359 static int vlan_output(struct ifnet
*ifp
, struct mbuf
*m
);
360 static int vlan_ioctl(ifnet_t ifp
, u_long cmd
, void * addr
);
361 static int vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
,
362 bpf_packet_func func
);
363 static int vlan_attach_protocol(struct ifnet
*ifp
);
364 static int vlan_detach_protocol(struct ifnet
*ifp
);
365 static int vlan_setmulti(struct ifnet
*ifp
);
366 static int vlan_unconfig(struct ifnet
*ifp
);
367 static int vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
);
368 static void vlan_if_free(struct ifnet
* ifp
);
369 static void vlan_remove(ifvlan_ref ifv
);
370 static void vlan_if_detach(struct ifnet
* ifp
);
371 static int vlan_new_mtu(struct ifnet
* ifp
, int mtu
);
373 static struct if_clone vlan_cloner
= IF_CLONE_INITIALIZER(VLANNAME
,
378 static void interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
);
379 static void vlan_parent_link_event(vlan_parent_ref vlp
,
380 u_int32_t event_code
);
381 extern void dlil_input_packet_list(struct ifnet
*ifp
, struct mbuf
*m
);
384 vlan_globals_init(void)
388 vlan_assert_lock_not_held();
390 if (g_vlan
!= NULL
) {
393 v
= _MALLOC(sizeof(*v
), M_VLAN
, M_WAITOK
);
395 LIST_INIT(&v
->parent_list
);
399 if (g_vlan
!= NULL
) {
415 siocgifdevmtu(struct ifnet
* ifp
, struct ifdevmtu
* ifdm_p
)
420 bzero(&ifr
, sizeof(ifr
));
421 error
= ifnet_ioctl(ifp
, 0,SIOCGIFDEVMTU
, &ifr
);
423 *ifdm_p
= ifr
.ifr_devmtu
;
429 siocsifaltmtu(struct ifnet
* ifp
, int mtu
)
433 bzero(&ifr
, sizeof(ifr
));
435 return (ifnet_ioctl(ifp
, 0, SIOCSIFALTMTU
, &ifr
));
438 static __inline__
void
439 vlan_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
440 bpf_packet_func func
)
448 static __inline__
void
449 vlan_bpf_input(struct ifnet
* ifp
, struct mbuf
* m
,
450 bpf_packet_func func
, char * frame_header
,
451 int frame_header_len
, int encap_len
)
455 /* present the right header to bpf */
456 bcopy(frame_header
, frame_header
+ encap_len
, frame_header_len
);
458 m
->m_data
-= frame_header_len
;
459 m
->m_len
+= frame_header_len
;
461 m
->m_data
+= frame_header_len
;
462 m
->m_len
-= frame_header_len
;
464 /* restore the header */
465 bcopy(frame_header
+ encap_len
, frame_header
, frame_header_len
);
472 ** vlan_parent synchronization routines
474 static __inline__
void
475 vlan_parent_retain(vlan_parent_ref vlp
)
477 OSIncrementAtomic(&vlp
->vlp_retain_count
);
480 static __inline__
void
481 vlan_parent_release(vlan_parent_ref vlp
)
483 UInt32 old_retain_count
;
485 old_retain_count
= OSDecrementAtomic(&vlp
->vlp_retain_count
);
486 switch (old_retain_count
) {
488 panic("vlan_parent_release: retain count is 0\n");
491 if (g_vlan
->verbose
) {
492 struct ifnet
* ifp
= vlp
->vlp_ifp
;
493 printf("vlan_parent_release(%s%d)\n", ifnet_name(ifp
),
505 * Function: vlan_parent_wait
507 * Allows a single thread to gain exclusive access to the vlan_parent
508 * data structure. Some operations take a long time to complete,
509 * and some have side-effects that we can't predict. Holding the
510 * vlan_lock() across such operations is not possible.
513 * Before calling, you must be holding the vlan_lock and have taken
514 * a reference on the vlan_parent_ref.
517 vlan_parent_wait(vlan_parent_ref vlp
, const char * msg
)
521 /* other add/remove/multicast-change in progress */
522 while (vlan_parent_flags_change_in_progress(vlp
)) {
523 if (g_vlan
->verbose
) {
524 struct ifnet
* ifp
= vlp
->vlp_ifp
;
526 printf("%s%d: %s msleep\n", ifnet_name(ifp
), ifnet_unit(ifp
), msg
);
529 (void)msleep(vlp
, vlan_lck_mtx
, PZERO
, msg
, 0);
531 /* prevent other vlan parent remove/add from taking place */
532 vlan_parent_flags_set_change_in_progress(vlp
);
533 if (g_vlan
->verbose
&& waited
) {
534 struct ifnet
* ifp
= vlp
->vlp_ifp
;
536 printf("%s%d: %s woke up\n", ifnet_name(ifp
), ifnet_unit(ifp
), msg
);
542 * Function: vlan_parent_signal
544 * Allows the thread that previously invoked vlan_parent_wait() to
545 * give up exclusive access to the vlan_parent data structure, and wake up
546 * any other threads waiting to access
548 * Before calling, you must be holding the vlan_lock and have taken
549 * a reference on the vlan_parent_ref.
552 vlan_parent_signal(vlan_parent_ref vlp
, const char * msg
)
554 vlan_parent_flags_clear_change_in_progress(vlp
);
555 wakeup((caddr_t
)vlp
);
556 if (g_vlan
->verbose
) {
557 struct ifnet
* ifp
= vlp
->vlp_ifp
;
559 printf("%s%d: %s wakeup\n", ifnet_name(ifp
), ifnet_unit(ifp
), msg
);
566 * Program our multicast filter. What we're actually doing is
567 * programming the multicast filter of the parent. This has the
568 * side effect of causing the parent interface to receive multicast
569 * traffic that it doesn't really want, which ends up being discarded
570 * later by the upper protocol layers. Unfortunately, there's no way
571 * to avoid this: there really is only one physical interface.
574 vlan_setmulti(struct ifnet
* ifp
)
582 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
583 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
588 /* no parent, no need to program the multicast filter */
591 if (vlan_parent_flags_detaching(vlp
)) {
594 vlan_parent_retain(vlp
);
595 vlan_parent_wait(vlp
, "vlan_setmulti");
597 /* check again, things could have changed */
598 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
599 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
602 if (ifv
->ifv_vlp
!= vlp
) {
603 /* vlan parent changed */
607 /* no parent, no need to program the multicast filter */
613 /* update parent interface with our multicast addresses */
614 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
619 vlan_parent_signal(vlp
, "vlan_setmulti");
627 ** vlan_parent list manipulation/lookup routines
629 static vlan_parent_ref
630 parent_list_lookup(struct ifnet
* p
)
634 LIST_FOREACH(vlp
, &g_vlan
->parent_list
, vlp_parent_list
) {
635 if (vlp
->vlp_ifp
== p
) {
643 vlan_parent_lookup_tag(vlan_parent_ref vlp
, int tag
)
647 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
648 if (tag
== ifv
->ifv_tag
) {
656 vlan_lookup_parent_and_tag(struct ifnet
* p
, int tag
)
660 vlp
= parent_list_lookup(p
);
662 return (vlan_parent_lookup_tag(vlp
, tag
));
668 vlan_parent_find_max_mtu(vlan_parent_ref vlp
, ifvlan_ref exclude_ifv
)
673 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
676 if (exclude_ifv
== ifv
) {
679 req_mtu
= ifnet_mtu(ifv
->ifv_ifp
) + ifv
->ifv_mtufudge
;
680 if (req_mtu
> max_mtu
) {
688 * Function: vlan_parent_create
690 * Create a vlan_parent structure to hold the VLAN's for the given
691 * interface. Add it to the list of VLAN parents.
694 vlan_parent_create(struct ifnet
* p
, vlan_parent_ref
* ret_vlp
)
700 vlp
= _MALLOC(sizeof(*vlp
), M_VLAN
, M_WAITOK
);
704 bzero(vlp
, sizeof(*vlp
));
705 error
= siocgifdevmtu(p
, &vlp
->vlp_devmtu
);
707 printf("vlan_parent_create (%s%d): siocgifdevmtu failed, %d\n",
708 ifnet_name(p
), ifnet_unit(p
), error
);
712 LIST_INIT(&vlp
->vlp_vlan_list
);
714 vlan_parent_retain(vlp
);
716 & (IF_HWASSIST_VLAN_MTU
| IF_HWASSIST_VLAN_TAGGING
)) {
717 vlan_parent_flags_set_supports_vlan_mtu(vlp
);
724 vlan_parent_remove_all_vlans(vlan_parent_ref vlp
)
729 vlan_assert_lock_held();
731 while ((ifv
= LIST_FIRST(&vlp
->vlp_vlan_list
)) != NULL
) {
734 vlan_if_detach(ifv
->ifv_ifp
);
738 /* the vlan parent has no more VLAN's */
740 ifnet_set_eflags(p
, 0, IFEF_VLAN
); /* clear IFEF_VLAN */
741 LIST_REMOVE(vlp
, vlp_parent_list
);
743 vlan_parent_release(vlp
);
749 static __inline__
int
750 vlan_parent_no_vlans(vlan_parent_ref vlp
)
752 return (LIST_EMPTY(&vlp
->vlp_vlan_list
));
756 vlan_parent_add_vlan(vlan_parent_ref vlp
, ifvlan_ref ifv
, int tag
)
758 LIST_INSERT_HEAD(&vlp
->vlp_vlan_list
, ifv
, ifv_vlan_list
);
765 vlan_parent_remove_vlan(__unused vlan_parent_ref vlp
, ifvlan_ref ifv
)
768 LIST_REMOVE(ifv
, ifv_vlan_list
);
773 vlan_clone_attach(void)
777 error
= if_clone_attach(&vlan_cloner
);
785 vlan_clone_create(struct if_clone
*ifc
, int unit
)
790 struct ifnet_init_params vlan_init
;
792 error
= vlan_globals_init();
796 ifv
= _MALLOC(sizeof(struct ifvlan
), M_VLAN
, M_WAITOK
);
799 bzero(ifv
, sizeof(struct ifvlan
));
800 multicast_list_init(&ifv
->ifv_multicast
);
802 /* use the interface name as the unique id for ifp recycle */
803 if ((unsigned int)snprintf(ifv
->ifv_name
, sizeof(ifv
->ifv_name
), "%s%d",
804 ifc
->ifc_name
, unit
) >= sizeof(ifv
->ifv_name
)) {
809 bzero(&vlan_init
, sizeof(vlan_init
));
810 vlan_init
.uniqueid
= ifv
->ifv_name
;
811 vlan_init
.uniqueid_len
= strlen(ifv
->ifv_name
);
812 vlan_init
.name
= ifc
->ifc_name
;
813 vlan_init
.unit
= unit
;
814 vlan_init
.family
= IFNET_FAMILY_VLAN
;
815 vlan_init
.type
= IFT_L2VLAN
;
816 vlan_init
.output
= vlan_output
;
817 vlan_init
.demux
= ether_demux
;
818 vlan_init
.add_proto
= ether_add_proto
;
819 vlan_init
.del_proto
= ether_del_proto
;
820 vlan_init
.check_multi
= ether_check_multi
;
821 vlan_init
.framer
= ether_frameout
;
822 vlan_init
.softc
= ifv
;
823 vlan_init
.ioctl
= vlan_ioctl
;
824 vlan_init
.set_bpf_tap
= vlan_set_bpf_tap
;
825 vlan_init
.detach
= vlan_if_free
;
826 vlan_init
.broadcast_addr
= etherbroadcastaddr
;
827 vlan_init
.broadcast_len
= ETHER_ADDR_LEN
;
828 error
= ifnet_allocate(&vlan_init
, &ifp
);
836 /* NB: flags are not set here */
837 ifnet_set_link_mib_data(ifp
, &ifv
->ifv_mib
, sizeof ifv
->ifv_mib
);
838 /* NB: mtu is not set here */
841 ifnet_set_offload(ifp
, 0);
842 ifnet_set_addrlen(ifp
, ETHER_ADDR_LEN
); /* XXX ethernet specific */
843 ifnet_set_baudrate(ifp
, 0);
844 ifnet_set_hdrlen(ifp
, ETHER_VLAN_ENCAP_LEN
);
846 error
= ifnet_attach(ifp
, NULL
);
854 /* attach as ethernet */
855 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
860 vlan_remove(ifvlan_ref ifv
)
862 vlan_assert_lock_held();
863 ifvlan_flags_set_detaching(ifv
);
864 vlan_unconfig(ifv
->ifv_ifp
);
869 vlan_if_detach(struct ifnet
* ifp
)
876 vlan_clone_destroy(struct ifnet
*ifp
)
881 ifv
= ifnet_softc(ifp
);
882 if (ifv
== NULL
|| ifnet_type(ifp
) != IFT_L2VLAN
) {
886 if (ifvlan_flags_detaching(ifv
)) {
897 vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
902 ifv
= ifnet_softc(ifp
);
903 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
908 case BPF_TAP_DISABLE
:
909 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= NULL
;
913 ifv
->ifv_bpf_input
= func
;
917 ifv
->ifv_bpf_output
= func
;
920 case BPF_TAP_INPUT_OUTPUT
:
921 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= func
;
931 vlan_output(struct ifnet
* ifp
, struct mbuf
* m
)
933 bpf_packet_func bpf_func
;
934 struct ether_vlan_header
* evl
;
945 if ((m
->m_flags
& M_PKTHDR
) == 0) {
950 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
951 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)
952 || ifvlan_flags_ready(ifv
) == 0) {
964 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
965 soft_vlan
= (ifnet_offload(p
) & IF_HWASSIST_VLAN_TAGGING
) == 0;
966 bpf_func
= ifv
->ifv_bpf_output
;
968 encaplen
= ifv
->ifv_encaplen
;
970 vlan_bpf_output(ifp
, m
, bpf_func
);
972 /* do not run parent's if_output() if the parent is not up */
973 if ((ifnet_flags(p
) & (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
)) {
975 ifp
->if_collisions
++;
979 * If underlying interface can do VLAN tag insertion itself,
980 * just pass the packet along. However, we need some way to
981 * tell the interface where the packet came from so that it
982 * knows how to find the VLAN tag to use. We use a field in
983 * the mbuf header to store the VLAN tag, and a bit in the
984 * csum_flags field to mark the field as valid.
986 if (soft_vlan
== 0) {
987 m
->m_pkthdr
.csum_flags
|= CSUM_VLAN_TAG_VALID
;
988 m
->m_pkthdr
.vlan_tag
= tag
;
990 M_PREPEND(m
, encaplen
, M_DONTWAIT
);
992 printf("%s%d: unable to prepend VLAN header\n", ifnet_name(ifp
),
997 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
998 if (m
->m_len
< (int)sizeof(*evl
)) {
999 m
= m_pullup(m
, sizeof(*evl
));
1001 printf("%s%d: unable to pullup VLAN header\n", ifnet_name(ifp
),
1009 * Transform the Ethernet header into an Ethernet header
1010 * with 802.1Q encapsulation.
1012 bcopy(mtod(m
, char *) + encaplen
,
1013 mtod(m
, char *), ETHER_HDR_LEN
);
1014 evl
= mtod(m
, struct ether_vlan_header
*);
1015 evl
->evl_proto
= evl
->evl_encap_proto
;
1016 evl
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
1017 evl
->evl_tag
= htons(tag
);
1019 return ifnet_output_raw(p
, PF_VLAN
, m
);
1023 vlan_input(ifnet_t p
, __unused protocol_family_t protocol
,
1024 mbuf_t m
, char *frame_header
)
1026 bpf_packet_func bpf_func
= NULL
;
1027 struct ether_vlan_header
* evl
;
1028 struct ifnet
* ifp
= NULL
;
1032 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1034 * Packet is tagged, m contains a normal
1035 * Ethernet frame; the tag is stored out-of-band.
1037 m
->m_pkthdr
.csum_flags
&= ~CSUM_VLAN_TAG_VALID
;
1038 tag
= EVL_VLANOFTAG(m
->m_pkthdr
.vlan_tag
);
1039 m
->m_pkthdr
.vlan_tag
= 0;
1042 switch (ifnet_type(p
)) {
1044 if (m
->m_len
< ETHER_VLAN_ENCAP_LEN
) {
1048 evl
= (struct ether_vlan_header
*)frame_header
;
1049 if (ntohs(evl
->evl_proto
) == ETHERTYPE_VLAN
) {
1050 /* don't allow VLAN within VLAN */
1054 tag
= EVL_VLANOFTAG(ntohs(evl
->evl_tag
));
1057 * Restore the original ethertype. We'll remove
1058 * the encapsulation after we've found the vlan
1059 * interface corresponding to the tag.
1061 evl
->evl_encap_proto
= evl
->evl_proto
;
1064 printf("vlan_demux: unsupported if type %u",
1074 if ((ifnet_eflags(p
) & IFEF_VLAN
) == 0) {
1075 /* don't bother looking through the VLAN list */
1080 ifv
= vlan_lookup_parent_and_tag(p
, tag
);
1085 || ifvlan_flags_ready(ifv
) == 0
1086 || (ifnet_flags(ifp
) & IFF_UP
) == 0) {
1091 bpf_func
= ifv
->ifv_bpf_input
;
1096 * Packet had an in-line encapsulation header;
1097 * remove it. The original header has already
1098 * been fixed up above.
1100 m
->m_len
-= ETHER_VLAN_ENCAP_LEN
;
1101 m
->m_data
+= ETHER_VLAN_ENCAP_LEN
;
1102 m
->m_pkthdr
.len
-= ETHER_VLAN_ENCAP_LEN
;
1103 m
->m_pkthdr
.csum_flags
= 0; /* can't trust hardware checksum */
1106 m
->m_pkthdr
.rcvif
= ifp
;
1107 m
->m_pkthdr
.header
= frame_header
;
1108 (void)ifnet_stat_increment_in(ifp
, 1,
1109 m
->m_pkthdr
.len
+ ETHER_HDR_LEN
, 0);
1110 vlan_bpf_input(ifp
, m
, bpf_func
, frame_header
, ETHER_HDR_LEN
,
1111 soft_vlan
? ETHER_VLAN_ENCAP_LEN
: 0);
1112 /* We found a vlan interface, inject on that interface. */
1113 dlil_input_packet_list(ifp
, m
);
1115 m
->m_pkthdr
.header
= frame_header
;
1116 /* Send priority-tagged packet up through the parent */
1117 dlil_input_packet_list(p
, m
);
1122 #define VLAN_CONFIG_PROGRESS_VLP_RETAINED 0x1
1123 #define VLAN_CONFIG_PROGRESS_IN_LIST 0x2
1126 vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
)
1130 ifvlan_ref ifv
= NULL
;
1131 vlan_parent_ref new_vlp
= NULL
;
1132 int need_vlp_release
= 0;
1133 ifnet_offload_t offload
;
1134 u_int16_t parent_flags
;
1135 u_int32_t progress
= 0;
1136 vlan_parent_ref vlp
= NULL
;
1138 /* pre-allocate space for vlan_parent, in case we're first */
1139 error
= vlan_parent_create(p
, &new_vlp
);
1145 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1146 if (ifv
!= NULL
&& ifv
->ifv_vlp
!= NULL
) {
1148 vlan_parent_release(new_vlp
);
1151 vlp
= parent_list_lookup(p
);
1153 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1154 /* already a VLAN with that tag on this interface */
1160 /* we're the first VLAN on this interface */
1161 LIST_INSERT_HEAD(&g_vlan
->parent_list
, new_vlp
, vlp_parent_list
);
1165 /* need to wait to ensure no one else is trying to add/remove */
1166 vlan_parent_retain(vlp
);
1167 progress
|= VLAN_CONFIG_PROGRESS_VLP_RETAINED
;
1168 vlan_parent_wait(vlp
, "vlan_config");
1170 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1175 if (vlan_parent_flags_detaching(vlp
)
1176 || ifvlan_flags_detaching(ifv
) || ifv
->ifv_vlp
!= NULL
) {
1181 /* check again because someone might have gotten in */
1182 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1183 /* already a VLAN with that tag on this interface */
1188 if (vlan_parent_no_vlans(vlp
)) {
1191 vlan_parent_add_vlan(vlp
, ifv
, tag
);
1192 progress
|= VLAN_CONFIG_PROGRESS_IN_LIST
;
1194 /* check whether bond interface is using parent interface */
1195 ifnet_lock_exclusive(p
);
1196 if ((ifnet_eflags(p
) & IFEF_BOND
) != 0) {
1198 /* don't allow VLAN over interface that's already part of a bond */
1202 /* prevent BOND interface from using it */
1203 /* Can't use ifnet_set_eflags because that would take the lock */
1204 p
->if_eflags
|= IFEF_VLAN
;
1209 /* attach our VLAN "protocol" to the interface */
1210 error
= vlan_attach_protocol(p
);
1215 /* mark the parent interface up */
1216 ifnet_set_flags(p
, IFF_UP
, IFF_UP
);
1217 (void)ifnet_ioctl(p
, 0, SIOCSIFFLAGS
, (caddr_t
)NULL
);
1220 /* configure parent to receive our multicast addresses */
1221 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
1224 (void)vlan_detach_protocol(p
);
1230 /* set our ethernet address to that of the parent */
1231 ifnet_set_lladdr_and_type(ifp
, ifnet_lladdr(p
), ETHER_ADDR_LEN
, IFT_ETHER
);
1233 /* no failures past this point */
1236 ifv
->ifv_encaplen
= ETHER_VLAN_ENCAP_LEN
;
1238 if (vlan_parent_flags_supports_vlan_mtu(vlp
)) {
1239 ifv
->ifv_mtufudge
= 0;
1242 * Fudge the MTU by the encapsulation size. This
1243 * makes us incompatible with strictly compliant
1244 * 802.1Q implementations, but allows us to use
1245 * the feature with other NetBSD implementations,
1246 * which might still be useful.
1248 ifv
->ifv_mtufudge
= ifv
->ifv_encaplen
;
1250 ifnet_set_mtu(ifp
, ETHERMTU
- ifv
->ifv_mtufudge
);
1253 * Copy only a selected subset of flags from the parent.
1254 * Other flags are none of our business.
1256 parent_flags
= ifnet_flags(p
)
1257 & (IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
);
1258 ifnet_set_flags(ifp
, parent_flags
,
1259 IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
);
1261 /* use hwassist bits from parent interface, but exclude VLAN bits */
1262 offload
= ifnet_offload(p
) & ~(IFNET_VLAN_TAGGING
| IFNET_VLAN_MTU
);
1263 ifnet_set_offload(ifp
, offload
);
1265 ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
1266 ifvlan_flags_set_ready(ifv
);
1267 vlan_parent_signal(vlp
, "vlan_config");
1269 if (new_vlp
!= vlp
) {
1270 /* throw it away, it wasn't needed */
1271 vlan_parent_release(new_vlp
);
1276 vlan_assert_lock_held();
1277 vlan_parent_signal(vlp
, "vlan_config");
1280 if ((progress
& VLAN_CONFIG_PROGRESS_IN_LIST
) != 0) {
1281 vlan_parent_remove_vlan(vlp
, ifv
);
1283 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1284 /* the vlan parent has no more VLAN's */
1285 ifnet_set_eflags(p
, 0, IFEF_VLAN
);
1286 LIST_REMOVE(vlp
, vlp_parent_list
);
1287 /* release outside of the lock below */
1288 need_vlp_release
= 1;
1292 if ((progress
& VLAN_CONFIG_PROGRESS_VLP_RETAINED
) != 0) {
1293 vlan_parent_release(vlp
);
1295 if (need_vlp_release
) {
1296 vlan_parent_release(vlp
);
1298 if (new_vlp
!= vlp
) {
1299 vlan_parent_release(new_vlp
);
1305 vlan_link_event(struct ifnet
* ifp
, struct ifnet
* p
)
1307 struct ifmediareq ifmr
;
1309 /* generate a link event based on the state of the underlying interface */
1310 bzero(&ifmr
, sizeof(ifmr
));
1311 snprintf(ifmr
.ifm_name
, sizeof(ifmr
.ifm_name
),
1312 "%s%d", ifnet_name(p
), ifnet_unit(p
));
1313 if (ifnet_ioctl(p
, 0, SIOCGIFMEDIA
, &ifmr
) == 0
1314 && ifmr
.ifm_count
> 0 && ifmr
.ifm_status
& IFM_AVALID
) {
1317 event
= (ifmr
.ifm_status
& IFM_ACTIVE
)
1318 ? KEV_DL_LINK_ON
: KEV_DL_LINK_OFF
;
1319 interface_link_event(ifp
, event
);
1325 vlan_unconfig(struct ifnet
* ifp
)
1330 int need_vlp_release
= 0;
1332 vlan_parent_ref vlp
;
1334 vlan_assert_lock_held();
1335 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1343 vlan_parent_retain(vlp
);
1344 vlan_parent_wait(vlp
, "vlan_unconfig");
1346 /* check again because another thread could be in vlan_unconfig */
1347 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1351 if (ifv
->ifv_vlp
!= vlp
) {
1352 /* vlan parent changed */
1358 /* remember whether we're the last VLAN on the parent */
1359 if (LIST_NEXT(LIST_FIRST(&vlp
->vlp_vlan_list
), ifv_vlan_list
) == NULL
) {
1360 if (g_vlan
->verbose
) {
1361 printf("vlan_unconfig: last vlan on %s%d\n",
1362 ifnet_name(p
), ifnet_unit(p
));
1367 /* back-out any effect our mtu might have had on the parent */
1368 (void)vlan_new_mtu(ifp
, ETHERMTU
- ifv
->ifv_mtufudge
);
1372 /* detach VLAN "protocol" */
1374 (void)vlan_detach_protocol(p
);
1377 /* un-join multicast on parent interface */
1378 (void)multicast_list_remove(&ifv
->ifv_multicast
);
1380 /* Clear our MAC address. */
1381 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_L2VLAN
);
1385 /* Disconnect from parent. */
1386 vlan_parent_remove_vlan(vlp
, ifv
);
1388 /* return to the state we were in before SIFVLAN */
1389 ifnet_set_mtu(ifp
, 0);
1390 ifnet_set_flags(ifp
, 0,
1391 IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
| IFF_RUNNING
);
1392 ifnet_set_offload(ifp
, 0);
1394 ifv
->ifv_mtufudge
= 0;
1396 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1397 /* the vlan parent has no more VLAN's */
1398 ifnet_set_eflags(p
, 0, IFEF_VLAN
);
1399 LIST_REMOVE(vlp
, vlp_parent_list
);
1400 /* release outside of the lock below */
1405 vlan_parent_signal(vlp
, "vlan_unconfig");
1407 vlan_parent_release(vlp
); /* one because we waited */
1409 while (need_vlp_release
--) {
1410 vlan_parent_release(vlp
);
1417 vlan_set_promisc(struct ifnet
* ifp
)
1421 vlan_parent_ref vlp
;
1424 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1425 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1426 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1434 if ((ifnet_flags(ifp
) & IFF_PROMISC
) != 0) {
1435 if (!ifvlan_flags_promisc(ifv
)) {
1436 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 1);
1438 ifvlan_flags_set_promisc(ifv
);
1442 if (ifvlan_flags_promisc(ifv
)) {
1443 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 0);
1445 ifvlan_flags_clear_promisc(ifv
);
1455 vlan_new_mtu(struct ifnet
* ifp
, int mtu
)
1457 struct ifdevmtu
* devmtu_p
;
1463 vlan_parent_ref vlp
;
1465 vlan_assert_lock_held();
1466 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1468 devmtu_p
= &vlp
->vlp_devmtu
;
1469 req_mtu
= mtu
+ ifv
->ifv_mtufudge
;
1470 if (req_mtu
> devmtu_p
->ifdm_max
|| req_mtu
< devmtu_p
->ifdm_min
) {
1473 max_mtu
= vlan_parent_find_max_mtu(vlp
, ifv
);
1474 if (req_mtu
> max_mtu
) {
1477 else if (max_mtu
< devmtu_p
->ifdm_current
) {
1481 struct ifnet
* p
= vlp
->vlp_ifp
;
1483 error
= siocsifaltmtu(p
, new_mtu
);
1488 devmtu_p
->ifdm_current
= new_mtu
;
1490 ifnet_set_mtu(ifp
, mtu
);
1496 vlan_set_mtu(struct ifnet
* ifp
, int mtu
)
1500 vlan_parent_ref vlp
;
1502 if (mtu
< IF_MINMTU
) {
1506 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1507 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1509 return ((ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
);
1512 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1519 vlan_parent_retain(vlp
);
1520 vlan_parent_wait(vlp
, "vlan_set_mtu");
1522 /* check again, something might have changed */
1523 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1524 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1525 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1528 if (ifv
->ifv_vlp
!= vlp
) {
1529 /* vlan parent changed */
1532 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1538 error
= vlan_new_mtu(ifp
, mtu
);
1541 vlan_parent_signal(vlp
, "vlan_set_mtu");
1543 vlan_parent_release(vlp
);
1549 vlan_ioctl(ifnet_t ifp
, u_long cmd
, void * data
)
1551 struct ifdevmtu
* devmtu_p
;
1553 struct ifaddr
* ifa
;
1554 struct ifmediareq
*ifmr
;
1559 user_addr_t user_addr
;
1560 vlan_parent_ref vlp
;
1563 if (ifnet_type(ifp
) != IFT_L2VLAN
) {
1564 return (EOPNOTSUPP
);
1566 ifr
= (struct ifreq
*)data
;
1567 ifa
= (struct ifaddr
*)data
;
1571 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
1574 case SIOCGIFMEDIA32
:
1575 case SIOCGIFMEDIA64
:
1577 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1578 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1580 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1582 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1584 ifmr
= (struct ifmediareq
*)data
;
1585 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
1586 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
1587 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
1589 struct ifmediareq p_ifmr
;
1591 bzero(&p_ifmr
, sizeof(p_ifmr
));
1592 error
= ifnet_ioctl(p
, 0, SIOCGIFMEDIA
, &p_ifmr
);
1594 ifmr
->ifm_active
= p_ifmr
.ifm_active
;
1595 ifmr
->ifm_current
= p_ifmr
.ifm_current
;
1596 ifmr
->ifm_mask
= p_ifmr
.ifm_mask
;
1597 ifmr
->ifm_status
= p_ifmr
.ifm_status
;
1598 ifmr
->ifm_count
= p_ifmr
.ifm_count
;
1599 /* Limit the result to the parent's current config. */
1600 if (ifmr
->ifm_count
>= 1 && user_addr
!= USER_ADDR_NULL
) {
1601 ifmr
->ifm_count
= 1;
1602 error
= copyout(&ifmr
->ifm_current
, user_addr
,
1607 ifmr
->ifm_active
= ifmr
->ifm_current
= IFM_NONE
;
1609 ifmr
->ifm_status
= IFM_AVALID
;
1610 ifmr
->ifm_count
= 1;
1611 if (user_addr
!= USER_ADDR_NULL
) {
1612 error
= copyout(&ifmr
->ifm_current
, user_addr
, sizeof(int));
1623 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1624 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1626 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1630 int min_mtu
= vlp
->vlp_devmtu
.ifdm_min
- ifv
->ifv_mtufudge
;
1631 devmtu_p
= &ifr
->ifr_devmtu
;
1632 devmtu_p
->ifdm_current
= ifnet_mtu(ifp
);
1633 devmtu_p
->ifdm_min
= max(min_mtu
, IF_MINMTU
);
1634 devmtu_p
->ifdm_max
= vlp
->vlp_devmtu
.ifdm_max
- ifv
->ifv_mtufudge
;
1637 devmtu_p
= &ifr
->ifr_devmtu
;
1638 devmtu_p
->ifdm_current
= 0;
1639 devmtu_p
->ifdm_min
= 0;
1640 devmtu_p
->ifdm_max
= 0;
1646 error
= vlan_set_mtu(ifp
, ifr
->ifr_mtu
);
1650 user_addr
= proc_is64bit(current_proc())
1651 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1652 error
= copyin(user_addr
, &vlr
, sizeof(vlr
));
1657 if (vlr
.vlr_parent
[0] != '\0') {
1658 if (vlr
.vlr_tag
& ~EVL_VLID_MASK
) {
1660 * Don't let the caller set up a VLAN tag with
1661 * anything except VLID bits.
1666 p
= ifunit(vlr
.vlr_parent
);
1671 /* can't do VLAN over anything but ethernet or ethernet aggregate */
1672 if (ifnet_type(p
) != IFT_ETHER
1673 && ifnet_type(p
) != IFT_IEEE8023ADLAG
) {
1674 error
= EPROTONOSUPPORT
;
1677 error
= vlan_config(ifp
, p
, vlr
.vlr_tag
);
1682 /* Update promiscuous mode, if necessary. */
1683 (void)vlan_set_promisc(ifp
);
1685 /* generate a link event based on the state of the parent */
1686 vlan_link_event(ifp
, p
);
1689 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1690 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1692 error
= (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1695 error
= vlan_unconfig(ifp
);
1698 interface_link_event(ifp
, KEV_DL_LINK_OFF
);
1704 bzero(&vlr
, sizeof vlr
);
1706 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1707 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1709 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1711 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1715 snprintf(vlr
.vlr_parent
, sizeof(vlr
.vlr_parent
),
1716 "%s%d", ifnet_name(p
), ifnet_unit(p
));
1719 user_addr
= proc_is64bit(current_proc())
1720 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1721 error
= copyout(&vlr
, user_addr
, sizeof(vlr
));
1726 * For promiscuous mode, we enable promiscuous mode on
1727 * the parent if we need promiscuous on the VLAN interface.
1729 error
= vlan_set_promisc(ifp
);
1734 error
= vlan_setmulti(ifp
);
1743 vlan_if_free(struct ifnet
* ifp
)
1751 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1762 vlan_event(struct ifnet
* p
, __unused protocol_family_t protocol
,
1763 const struct kev_msg
* event
)
1765 vlan_parent_ref vlp
;
1767 /* Check if the interface we are attached to is being detached */
1768 if (event
->vendor_code
!= KEV_VENDOR_APPLE
1769 || event
->kev_class
!= KEV_NETWORK_CLASS
1770 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
1773 switch (event
->event_code
) {
1774 case KEV_DL_IF_DETACHING
:
1775 case KEV_DL_LINK_OFF
:
1776 case KEV_DL_LINK_ON
:
1782 if ((ifnet_eflags(p
) & IFEF_VLAN
) == 0) {
1787 vlp
= parent_list_lookup(p
);
1793 switch (event
->event_code
) {
1794 case KEV_DL_IF_DETACHING
:
1795 vlan_parent_flags_set_detaching(vlp
);
1796 vlan_parent_remove_all_vlans(vlp
);
1799 case KEV_DL_LINK_OFF
:
1800 case KEV_DL_LINK_ON
:
1801 vlan_parent_link_event(vlp
, event
->event_code
);
1811 interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
)
1814 struct kern_event_msg header
;
1816 char if_name
[IFNAMSIZ
];
1819 event
.header
.total_size
= sizeof(event
);
1820 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
1821 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
1822 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
1823 event
.header
.event_code
= event_code
;
1824 event
.header
.event_data
[0] = ifnet_family(ifp
);
1825 event
.unit
= (u_int32_t
) ifnet_unit(ifp
);
1826 strncpy(event
.if_name
, ifnet_name(ifp
), IFNAMSIZ
);
1827 ifnet_event(ifp
, &event
.header
);
1832 vlan_parent_link_event(vlan_parent_ref vlp
, u_int32_t event_code
)
1836 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
1837 interface_link_event(ifv
->ifv_ifp
, event_code
);
1844 * Function: vlan_attach_protocol
1846 * Attach a DLIL protocol to the interface, using the ETHERTYPE_VLAN
1849 * The ethernet demux actually special cases VLAN to support hardware.
1850 * The demux here isn't used. The demux will return PF_VLAN for the
1851 * appropriate packets and our vlan_input function will be called.
1854 vlan_attach_protocol(struct ifnet
*ifp
)
1857 struct ifnet_attach_proto_param reg
;
1859 bzero(®
, sizeof(reg
));
1860 reg
.input
= vlan_input
;
1861 reg
.event
= vlan_event
;
1862 error
= ifnet_attach_protocol(ifp
, PF_VLAN
, ®
);
1864 printf("vlan_proto_attach(%s%d) ifnet_attach_protocol failed, %d\n",
1865 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
1871 * Function: vlan_detach_protocol
1873 * Detach our DLIL protocol from an interface
1876 vlan_detach_protocol(struct ifnet
*ifp
)
1880 error
= ifnet_detach_protocol(ifp
, PF_VLAN
);
1882 printf("vlan_proto_detach(%s%d) ifnet_detach_protocol failed, %d\n",
1883 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
1890 * DLIL interface family functions
1891 * We use the ethernet plumb functions, since that's all we support.
1892 * If we wanted to handle multiple LAN types (tokenring, etc.), we'd
1893 * call the appropriate routines for that LAN type instead of hard-coding
1897 vlan_attach_inet(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1899 return (ether_attach_inet(ifp
, protocol_family
));
1903 vlan_detach_inet(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1905 ether_detach_inet(ifp
, protocol_family
);
1910 vlan_attach_inet6(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1912 return (ether_attach_inet6(ifp
, protocol_family
));
1916 vlan_detach_inet6(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1918 ether_detach_inet6(ifp
, protocol_family
);
1924 vlan_attach_at(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1926 return (ether_attach_at(ifp
, protocol_family
));
1930 vlan_detach_at(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1932 ether_detach_at(ifp
, protocol_family
);
1936 __private_extern__
int
1937 vlan_family_init(void)
1941 error
= proto_register_plumber(PF_INET
, IFNET_FAMILY_VLAN
,
1942 vlan_attach_inet
, vlan_detach_inet
);
1944 printf("proto_register_plumber failed for AF_INET error=%d\n",
1949 error
= proto_register_plumber(PF_INET6
, IFNET_FAMILY_VLAN
,
1950 vlan_attach_inet6
, vlan_detach_inet6
);
1952 printf("proto_register_plumber failed for AF_INET6 error=%d\n",
1958 error
= proto_register_plumber(PF_APPLETALK
, IFNET_FAMILY_VLAN
,
1959 vlan_attach_at
, vlan_detach_at
);
1961 printf("proto_register_plumber failed for AF_APPLETALK error=%d\n",
1966 error
= vlan_clone_attach();
1968 printf("proto_register_plumber failed vlan_clone_attach error=%d\n",