2 * Copyright (c) 2003-2010 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
*, u_int32_t
, void *);
356 static int 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
, u_int32_t unit
, __unused
void *params
)
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
)) {
898 vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
903 ifv
= ifnet_softc(ifp
);
904 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
909 case BPF_TAP_DISABLE
:
910 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= NULL
;
914 ifv
->ifv_bpf_input
= func
;
918 ifv
->ifv_bpf_output
= func
;
921 case BPF_TAP_INPUT_OUTPUT
:
922 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= func
;
932 vlan_output(struct ifnet
* ifp
, struct mbuf
* m
)
934 bpf_packet_func bpf_func
;
935 struct ether_vlan_header
* evl
;
946 if ((m
->m_flags
& M_PKTHDR
) == 0) {
951 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
952 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)
953 || ifvlan_flags_ready(ifv
) == 0) {
965 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
966 soft_vlan
= (ifnet_offload(p
) & IF_HWASSIST_VLAN_TAGGING
) == 0;
967 bpf_func
= ifv
->ifv_bpf_output
;
969 encaplen
= ifv
->ifv_encaplen
;
971 vlan_bpf_output(ifp
, m
, bpf_func
);
973 /* do not run parent's if_output() if the parent is not up */
974 if ((ifnet_flags(p
) & (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
)) {
976 ifp
->if_collisions
++;
980 * If underlying interface can do VLAN tag insertion itself,
981 * just pass the packet along. However, we need some way to
982 * tell the interface where the packet came from so that it
983 * knows how to find the VLAN tag to use. We use a field in
984 * the mbuf header to store the VLAN tag, and a bit in the
985 * csum_flags field to mark the field as valid.
987 if (soft_vlan
== 0) {
988 m
->m_pkthdr
.csum_flags
|= CSUM_VLAN_TAG_VALID
;
989 m
->m_pkthdr
.vlan_tag
= tag
;
991 M_PREPEND(m
, encaplen
, M_DONTWAIT
);
993 printf("%s%d: unable to prepend VLAN header\n", ifnet_name(ifp
),
998 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
999 if (m
->m_len
< (int)sizeof(*evl
)) {
1000 m
= m_pullup(m
, sizeof(*evl
));
1002 printf("%s%d: unable to pullup VLAN header\n", ifnet_name(ifp
),
1010 * Transform the Ethernet header into an Ethernet header
1011 * with 802.1Q encapsulation.
1013 bcopy(mtod(m
, char *) + encaplen
,
1014 mtod(m
, char *), ETHER_HDR_LEN
);
1015 evl
= mtod(m
, struct ether_vlan_header
*);
1016 evl
->evl_proto
= evl
->evl_encap_proto
;
1017 evl
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
1018 evl
->evl_tag
= htons(tag
);
1020 return ifnet_output_raw(p
, PF_VLAN
, m
);
1024 vlan_input(ifnet_t p
, __unused protocol_family_t protocol
,
1025 mbuf_t m
, char *frame_header
)
1027 bpf_packet_func bpf_func
= NULL
;
1028 struct ether_vlan_header
* evl
;
1029 struct ifnet
* ifp
= NULL
;
1033 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1035 * Packet is tagged, m contains a normal
1036 * Ethernet frame; the tag is stored out-of-band.
1038 m
->m_pkthdr
.csum_flags
&= ~CSUM_VLAN_TAG_VALID
;
1039 tag
= EVL_VLANOFTAG(m
->m_pkthdr
.vlan_tag
);
1040 m
->m_pkthdr
.vlan_tag
= 0;
1043 switch (ifnet_type(p
)) {
1045 if (m
->m_len
< ETHER_VLAN_ENCAP_LEN
) {
1049 evl
= (struct ether_vlan_header
*)frame_header
;
1050 if (ntohs(evl
->evl_proto
) == ETHERTYPE_VLAN
) {
1051 /* don't allow VLAN within VLAN */
1055 tag
= EVL_VLANOFTAG(ntohs(evl
->evl_tag
));
1058 * Restore the original ethertype. We'll remove
1059 * the encapsulation after we've found the vlan
1060 * interface corresponding to the tag.
1062 evl
->evl_encap_proto
= evl
->evl_proto
;
1065 printf("vlan_demux: unsupported if type %u",
1075 if ((ifnet_eflags(p
) & IFEF_VLAN
) == 0) {
1076 /* don't bother looking through the VLAN list */
1081 ifv
= vlan_lookup_parent_and_tag(p
, tag
);
1086 || ifvlan_flags_ready(ifv
) == 0
1087 || (ifnet_flags(ifp
) & IFF_UP
) == 0) {
1092 bpf_func
= ifv
->ifv_bpf_input
;
1097 * Packet had an in-line encapsulation header;
1098 * remove it. The original header has already
1099 * been fixed up above.
1101 m
->m_len
-= ETHER_VLAN_ENCAP_LEN
;
1102 m
->m_data
+= ETHER_VLAN_ENCAP_LEN
;
1103 m
->m_pkthdr
.len
-= ETHER_VLAN_ENCAP_LEN
;
1104 m
->m_pkthdr
.csum_flags
= 0; /* can't trust hardware checksum */
1107 m
->m_pkthdr
.rcvif
= ifp
;
1108 m
->m_pkthdr
.header
= frame_header
;
1109 (void)ifnet_stat_increment_in(ifp
, 1,
1110 m
->m_pkthdr
.len
+ ETHER_HDR_LEN
, 0);
1111 vlan_bpf_input(ifp
, m
, bpf_func
, frame_header
, ETHER_HDR_LEN
,
1112 soft_vlan
? ETHER_VLAN_ENCAP_LEN
: 0);
1113 /* We found a vlan interface, inject on that interface. */
1114 dlil_input_packet_list(ifp
, m
);
1116 m
->m_pkthdr
.header
= frame_header
;
1117 /* Send priority-tagged packet up through the parent */
1118 dlil_input_packet_list(p
, m
);
1123 #define VLAN_CONFIG_PROGRESS_VLP_RETAINED 0x1
1124 #define VLAN_CONFIG_PROGRESS_IN_LIST 0x2
1127 vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
)
1131 ifvlan_ref ifv
= NULL
;
1132 vlan_parent_ref new_vlp
= NULL
;
1133 int need_vlp_release
= 0;
1134 ifnet_offload_t offload
;
1135 u_int16_t parent_flags
;
1136 u_int32_t progress
= 0;
1137 vlan_parent_ref vlp
= NULL
;
1139 /* pre-allocate space for vlan_parent, in case we're first */
1140 error
= vlan_parent_create(p
, &new_vlp
);
1146 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1147 if (ifv
!= NULL
&& ifv
->ifv_vlp
!= NULL
) {
1149 vlan_parent_release(new_vlp
);
1152 vlp
= parent_list_lookup(p
);
1154 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1155 /* already a VLAN with that tag on this interface */
1161 /* we're the first VLAN on this interface */
1162 LIST_INSERT_HEAD(&g_vlan
->parent_list
, new_vlp
, vlp_parent_list
);
1166 /* need to wait to ensure no one else is trying to add/remove */
1167 vlan_parent_retain(vlp
);
1168 progress
|= VLAN_CONFIG_PROGRESS_VLP_RETAINED
;
1169 vlan_parent_wait(vlp
, "vlan_config");
1171 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1176 if (vlan_parent_flags_detaching(vlp
)
1177 || ifvlan_flags_detaching(ifv
) || ifv
->ifv_vlp
!= NULL
) {
1182 /* check again because someone might have gotten in */
1183 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1184 /* already a VLAN with that tag on this interface */
1189 if (vlan_parent_no_vlans(vlp
)) {
1192 vlan_parent_add_vlan(vlp
, ifv
, tag
);
1193 progress
|= VLAN_CONFIG_PROGRESS_IN_LIST
;
1195 /* check whether bond interface is using parent interface */
1196 ifnet_lock_exclusive(p
);
1197 if ((ifnet_eflags(p
) & IFEF_BOND
) != 0) {
1199 /* don't allow VLAN over interface that's already part of a bond */
1203 /* prevent BOND interface from using it */
1204 /* Can't use ifnet_set_eflags because that would take the lock */
1205 p
->if_eflags
|= IFEF_VLAN
;
1210 /* attach our VLAN "protocol" to the interface */
1211 error
= vlan_attach_protocol(p
);
1216 /* mark the parent interface up */
1217 ifnet_set_flags(p
, IFF_UP
, IFF_UP
);
1218 (void)ifnet_ioctl(p
, 0, SIOCSIFFLAGS
, (caddr_t
)NULL
);
1221 /* configure parent to receive our multicast addresses */
1222 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
1225 (void)vlan_detach_protocol(p
);
1231 /* set our ethernet address to that of the parent */
1232 ifnet_set_lladdr_and_type(ifp
, ifnet_lladdr(p
), ETHER_ADDR_LEN
, IFT_ETHER
);
1234 /* no failures past this point */
1237 ifv
->ifv_encaplen
= ETHER_VLAN_ENCAP_LEN
;
1239 if (vlan_parent_flags_supports_vlan_mtu(vlp
)) {
1240 ifv
->ifv_mtufudge
= 0;
1243 * Fudge the MTU by the encapsulation size. This
1244 * makes us incompatible with strictly compliant
1245 * 802.1Q implementations, but allows us to use
1246 * the feature with other NetBSD implementations,
1247 * which might still be useful.
1249 ifv
->ifv_mtufudge
= ifv
->ifv_encaplen
;
1251 ifnet_set_mtu(ifp
, ETHERMTU
- ifv
->ifv_mtufudge
);
1254 * Copy only a selected subset of flags from the parent.
1255 * Other flags are none of our business.
1257 parent_flags
= ifnet_flags(p
)
1258 & (IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
);
1259 ifnet_set_flags(ifp
, parent_flags
,
1260 IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
);
1262 /* use hwassist bits from parent interface, but exclude VLAN bits */
1263 offload
= ifnet_offload(p
) & ~(IFNET_VLAN_TAGGING
| IFNET_VLAN_MTU
);
1264 ifnet_set_offload(ifp
, offload
);
1266 ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
1267 ifvlan_flags_set_ready(ifv
);
1268 vlan_parent_signal(vlp
, "vlan_config");
1270 if (new_vlp
!= vlp
) {
1271 /* throw it away, it wasn't needed */
1272 vlan_parent_release(new_vlp
);
1277 vlan_assert_lock_held();
1278 vlan_parent_signal(vlp
, "vlan_config");
1281 if ((progress
& VLAN_CONFIG_PROGRESS_IN_LIST
) != 0) {
1282 vlan_parent_remove_vlan(vlp
, ifv
);
1284 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1285 /* the vlan parent has no more VLAN's */
1286 ifnet_set_eflags(p
, 0, IFEF_VLAN
);
1287 LIST_REMOVE(vlp
, vlp_parent_list
);
1288 /* release outside of the lock below */
1289 need_vlp_release
= 1;
1293 if ((progress
& VLAN_CONFIG_PROGRESS_VLP_RETAINED
) != 0) {
1294 vlan_parent_release(vlp
);
1296 if (need_vlp_release
) {
1297 vlan_parent_release(vlp
);
1299 if (new_vlp
!= vlp
) {
1300 vlan_parent_release(new_vlp
);
1306 vlan_link_event(struct ifnet
* ifp
, struct ifnet
* p
)
1308 struct ifmediareq ifmr
;
1310 /* generate a link event based on the state of the underlying interface */
1311 bzero(&ifmr
, sizeof(ifmr
));
1312 snprintf(ifmr
.ifm_name
, sizeof(ifmr
.ifm_name
),
1313 "%s%d", ifnet_name(p
), ifnet_unit(p
));
1314 if (ifnet_ioctl(p
, 0, SIOCGIFMEDIA
, &ifmr
) == 0
1315 && ifmr
.ifm_count
> 0 && ifmr
.ifm_status
& IFM_AVALID
) {
1318 event
= (ifmr
.ifm_status
& IFM_ACTIVE
)
1319 ? KEV_DL_LINK_ON
: KEV_DL_LINK_OFF
;
1320 interface_link_event(ifp
, event
);
1326 vlan_unconfig(struct ifnet
* ifp
)
1331 int need_vlp_release
= 0;
1333 vlan_parent_ref vlp
;
1335 vlan_assert_lock_held();
1336 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1344 vlan_parent_retain(vlp
);
1345 vlan_parent_wait(vlp
, "vlan_unconfig");
1347 /* check again because another thread could be in vlan_unconfig */
1348 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1352 if (ifv
->ifv_vlp
!= vlp
) {
1353 /* vlan parent changed */
1359 /* remember whether we're the last VLAN on the parent */
1360 if (LIST_NEXT(LIST_FIRST(&vlp
->vlp_vlan_list
), ifv_vlan_list
) == NULL
) {
1361 if (g_vlan
->verbose
) {
1362 printf("vlan_unconfig: last vlan on %s%d\n",
1363 ifnet_name(p
), ifnet_unit(p
));
1368 /* back-out any effect our mtu might have had on the parent */
1369 (void)vlan_new_mtu(ifp
, ETHERMTU
- ifv
->ifv_mtufudge
);
1373 /* detach VLAN "protocol" */
1375 (void)vlan_detach_protocol(p
);
1378 /* un-join multicast on parent interface */
1379 (void)multicast_list_remove(&ifv
->ifv_multicast
);
1381 /* Clear our MAC address. */
1382 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_L2VLAN
);
1386 /* Disconnect from parent. */
1387 vlan_parent_remove_vlan(vlp
, ifv
);
1389 /* return to the state we were in before SIFVLAN */
1390 ifnet_set_mtu(ifp
, 0);
1391 ifnet_set_flags(ifp
, 0,
1392 IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
| IFF_RUNNING
);
1393 ifnet_set_offload(ifp
, 0);
1395 ifv
->ifv_mtufudge
= 0;
1397 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1398 /* the vlan parent has no more VLAN's */
1399 ifnet_set_eflags(p
, 0, IFEF_VLAN
);
1400 LIST_REMOVE(vlp
, vlp_parent_list
);
1401 /* release outside of the lock below */
1406 vlan_parent_signal(vlp
, "vlan_unconfig");
1408 vlan_parent_release(vlp
); /* one because we waited */
1410 while (need_vlp_release
--) {
1411 vlan_parent_release(vlp
);
1418 vlan_set_promisc(struct ifnet
* ifp
)
1422 vlan_parent_ref vlp
;
1425 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1426 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1427 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1435 if ((ifnet_flags(ifp
) & IFF_PROMISC
) != 0) {
1436 if (!ifvlan_flags_promisc(ifv
)) {
1437 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 1);
1439 ifvlan_flags_set_promisc(ifv
);
1443 if (ifvlan_flags_promisc(ifv
)) {
1444 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 0);
1446 ifvlan_flags_clear_promisc(ifv
);
1456 vlan_new_mtu(struct ifnet
* ifp
, int mtu
)
1458 struct ifdevmtu
* devmtu_p
;
1464 vlan_parent_ref vlp
;
1466 vlan_assert_lock_held();
1467 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1469 devmtu_p
= &vlp
->vlp_devmtu
;
1470 req_mtu
= mtu
+ ifv
->ifv_mtufudge
;
1471 if (req_mtu
> devmtu_p
->ifdm_max
|| req_mtu
< devmtu_p
->ifdm_min
) {
1474 max_mtu
= vlan_parent_find_max_mtu(vlp
, ifv
);
1475 if (req_mtu
> max_mtu
) {
1478 else if (max_mtu
< devmtu_p
->ifdm_current
) {
1482 struct ifnet
* p
= vlp
->vlp_ifp
;
1484 error
= siocsifaltmtu(p
, new_mtu
);
1489 devmtu_p
->ifdm_current
= new_mtu
;
1491 ifnet_set_mtu(ifp
, mtu
);
1497 vlan_set_mtu(struct ifnet
* ifp
, int mtu
)
1501 vlan_parent_ref vlp
;
1503 if (mtu
< IF_MINMTU
) {
1507 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1508 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1510 return ((ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
);
1513 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1520 vlan_parent_retain(vlp
);
1521 vlan_parent_wait(vlp
, "vlan_set_mtu");
1523 /* check again, something might have changed */
1524 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1525 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1526 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1529 if (ifv
->ifv_vlp
!= vlp
) {
1530 /* vlan parent changed */
1533 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1539 error
= vlan_new_mtu(ifp
, mtu
);
1542 vlan_parent_signal(vlp
, "vlan_set_mtu");
1544 vlan_parent_release(vlp
);
1550 vlan_ioctl(ifnet_t ifp
, u_long cmd
, void * data
)
1552 struct ifdevmtu
* devmtu_p
;
1554 struct ifaddr
* ifa
;
1555 struct ifmediareq
*ifmr
;
1560 user_addr_t user_addr
;
1561 vlan_parent_ref vlp
;
1564 if (ifnet_type(ifp
) != IFT_L2VLAN
) {
1565 return (EOPNOTSUPP
);
1567 ifr
= (struct ifreq
*)data
;
1568 ifa
= (struct ifaddr
*)data
;
1572 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
1575 case SIOCGIFMEDIA32
:
1576 case SIOCGIFMEDIA64
:
1578 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1579 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1581 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1583 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1585 ifmr
= (struct ifmediareq
*)data
;
1586 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
1587 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
1588 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
1590 struct ifmediareq p_ifmr
;
1592 bzero(&p_ifmr
, sizeof(p_ifmr
));
1593 error
= ifnet_ioctl(p
, 0, SIOCGIFMEDIA
, &p_ifmr
);
1595 ifmr
->ifm_active
= p_ifmr
.ifm_active
;
1596 ifmr
->ifm_current
= p_ifmr
.ifm_current
;
1597 ifmr
->ifm_mask
= p_ifmr
.ifm_mask
;
1598 ifmr
->ifm_status
= p_ifmr
.ifm_status
;
1599 ifmr
->ifm_count
= p_ifmr
.ifm_count
;
1600 /* Limit the result to the parent's current config. */
1601 if (ifmr
->ifm_count
>= 1 && user_addr
!= USER_ADDR_NULL
) {
1602 ifmr
->ifm_count
= 1;
1603 error
= copyout(&ifmr
->ifm_current
, user_addr
,
1608 ifmr
->ifm_active
= ifmr
->ifm_current
= IFM_NONE
;
1610 ifmr
->ifm_status
= IFM_AVALID
;
1611 ifmr
->ifm_count
= 1;
1612 if (user_addr
!= USER_ADDR_NULL
) {
1613 error
= copyout(&ifmr
->ifm_current
, user_addr
, sizeof(int));
1624 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1625 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1627 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1631 int min_mtu
= vlp
->vlp_devmtu
.ifdm_min
- ifv
->ifv_mtufudge
;
1632 devmtu_p
= &ifr
->ifr_devmtu
;
1633 devmtu_p
->ifdm_current
= ifnet_mtu(ifp
);
1634 devmtu_p
->ifdm_min
= max(min_mtu
, IF_MINMTU
);
1635 devmtu_p
->ifdm_max
= vlp
->vlp_devmtu
.ifdm_max
- ifv
->ifv_mtufudge
;
1638 devmtu_p
= &ifr
->ifr_devmtu
;
1639 devmtu_p
->ifdm_current
= 0;
1640 devmtu_p
->ifdm_min
= 0;
1641 devmtu_p
->ifdm_max
= 0;
1647 error
= vlan_set_mtu(ifp
, ifr
->ifr_mtu
);
1651 user_addr
= proc_is64bit(current_proc())
1652 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1653 error
= copyin(user_addr
, &vlr
, sizeof(vlr
));
1658 if (vlr
.vlr_parent
[0] != '\0') {
1659 if (vlr
.vlr_tag
& ~EVL_VLID_MASK
) {
1661 * Don't let the caller set up a VLAN tag with
1662 * anything except VLID bits.
1667 p
= ifunit(vlr
.vlr_parent
);
1672 /* can't do VLAN over anything but ethernet or ethernet aggregate */
1673 if (ifnet_type(p
) != IFT_ETHER
1674 && ifnet_type(p
) != IFT_IEEE8023ADLAG
) {
1675 error
= EPROTONOSUPPORT
;
1678 error
= vlan_config(ifp
, p
, vlr
.vlr_tag
);
1683 /* Update promiscuous mode, if necessary. */
1684 (void)vlan_set_promisc(ifp
);
1686 /* generate a link event based on the state of the parent */
1687 vlan_link_event(ifp
, p
);
1690 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1691 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1693 error
= (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1696 error
= vlan_unconfig(ifp
);
1699 interface_link_event(ifp
, KEV_DL_LINK_OFF
);
1705 bzero(&vlr
, sizeof vlr
);
1707 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1708 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1710 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1712 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1716 snprintf(vlr
.vlr_parent
, sizeof(vlr
.vlr_parent
),
1717 "%s%d", ifnet_name(p
), ifnet_unit(p
));
1720 user_addr
= proc_is64bit(current_proc())
1721 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1722 error
= copyout(&vlr
, user_addr
, sizeof(vlr
));
1727 * For promiscuous mode, we enable promiscuous mode on
1728 * the parent if we need promiscuous on the VLAN interface.
1730 error
= vlan_set_promisc(ifp
);
1735 error
= vlan_setmulti(ifp
);
1744 vlan_if_free(struct ifnet
* ifp
)
1752 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1763 vlan_event(struct ifnet
* p
, __unused protocol_family_t protocol
,
1764 const struct kev_msg
* event
)
1766 vlan_parent_ref vlp
;
1768 /* Check if the interface we are attached to is being detached */
1769 if (event
->vendor_code
!= KEV_VENDOR_APPLE
1770 || event
->kev_class
!= KEV_NETWORK_CLASS
1771 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
1774 switch (event
->event_code
) {
1775 case KEV_DL_IF_DETACHING
:
1776 case KEV_DL_LINK_OFF
:
1777 case KEV_DL_LINK_ON
:
1783 if ((ifnet_eflags(p
) & IFEF_VLAN
) == 0) {
1788 vlp
= parent_list_lookup(p
);
1794 switch (event
->event_code
) {
1795 case KEV_DL_IF_DETACHING
:
1796 vlan_parent_flags_set_detaching(vlp
);
1797 vlan_parent_remove_all_vlans(vlp
);
1800 case KEV_DL_LINK_OFF
:
1801 case KEV_DL_LINK_ON
:
1802 vlan_parent_link_event(vlp
, event
->event_code
);
1812 interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
)
1815 struct kern_event_msg header
;
1817 char if_name
[IFNAMSIZ
];
1820 event
.header
.total_size
= sizeof(event
);
1821 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
1822 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
1823 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
1824 event
.header
.event_code
= event_code
;
1825 event
.header
.event_data
[0] = ifnet_family(ifp
);
1826 event
.unit
= (u_int32_t
) ifnet_unit(ifp
);
1827 strncpy(event
.if_name
, ifnet_name(ifp
), IFNAMSIZ
);
1828 ifnet_event(ifp
, &event
.header
);
1833 vlan_parent_link_event(vlan_parent_ref vlp
, u_int32_t event_code
)
1837 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
1838 interface_link_event(ifv
->ifv_ifp
, event_code
);
1845 * Function: vlan_attach_protocol
1847 * Attach a DLIL protocol to the interface, using the ETHERTYPE_VLAN
1850 * The ethernet demux actually special cases VLAN to support hardware.
1851 * The demux here isn't used. The demux will return PF_VLAN for the
1852 * appropriate packets and our vlan_input function will be called.
1855 vlan_attach_protocol(struct ifnet
*ifp
)
1858 struct ifnet_attach_proto_param reg
;
1860 bzero(®
, sizeof(reg
));
1861 reg
.input
= vlan_input
;
1862 reg
.event
= vlan_event
;
1863 error
= ifnet_attach_protocol(ifp
, PF_VLAN
, ®
);
1865 printf("vlan_proto_attach(%s%d) ifnet_attach_protocol failed, %d\n",
1866 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
1872 * Function: vlan_detach_protocol
1874 * Detach our DLIL protocol from an interface
1877 vlan_detach_protocol(struct ifnet
*ifp
)
1881 error
= ifnet_detach_protocol(ifp
, PF_VLAN
);
1883 printf("vlan_proto_detach(%s%d) ifnet_detach_protocol failed, %d\n",
1884 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
1891 * DLIL interface family functions
1892 * We use the ethernet plumb functions, since that's all we support.
1893 * If we wanted to handle multiple LAN types (tokenring, etc.), we'd
1894 * call the appropriate routines for that LAN type instead of hard-coding
1898 vlan_attach_inet(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1900 return (ether_attach_inet(ifp
, protocol_family
));
1904 vlan_detach_inet(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1906 ether_detach_inet(ifp
, protocol_family
);
1911 vlan_attach_inet6(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1913 return (ether_attach_inet6(ifp
, protocol_family
));
1917 vlan_detach_inet6(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1919 ether_detach_inet6(ifp
, protocol_family
);
1925 vlan_attach_at(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1927 return (ether_attach_at(ifp
, protocol_family
));
1931 vlan_detach_at(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1933 ether_detach_at(ifp
, protocol_family
);
1937 __private_extern__
int
1938 vlan_family_init(void)
1942 error
= proto_register_plumber(PF_INET
, IFNET_FAMILY_VLAN
,
1943 vlan_attach_inet
, vlan_detach_inet
);
1945 printf("proto_register_plumber failed for AF_INET error=%d\n",
1950 error
= proto_register_plumber(PF_INET6
, IFNET_FAMILY_VLAN
,
1951 vlan_attach_inet6
, vlan_detach_inet6
);
1953 printf("proto_register_plumber failed for AF_INET6 error=%d\n",
1959 error
= proto_register_plumber(PF_APPLETALK
, IFNET_FAMILY_VLAN
,
1960 vlan_attach_at
, vlan_detach_at
);
1962 printf("proto_register_plumber failed for AF_APPLETALK error=%d\n",
1967 error
= vlan_clone_attach();
1969 printf("proto_register_plumber failed vlan_clone_attach error=%d\n",