2 * Copyright (c) 2003-2006 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 IF_MAXUNIT 0x7fff /* historical value */
111 #define VLANNAME "vlan"
113 typedef int (bpf_callback_func
)(struct ifnet
*, struct mbuf
*);
114 typedef int (if_set_bpf_tap_func
)(struct ifnet
*ifp
, int mode
, bpf_callback_func
* func
);
119 static __inline__ lck_grp_t
*
120 my_lck_grp_alloc_init(const char * grp_name
)
123 lck_grp_attr_t
* grp_attrs
;
125 grp_attrs
= lck_grp_attr_alloc_init();
126 grp
= lck_grp_alloc_init(grp_name
, grp_attrs
);
127 lck_grp_attr_free(grp_attrs
);
131 static __inline__ lck_mtx_t
*
132 my_lck_mtx_alloc_init(lck_grp_t
* lck_grp
)
134 lck_attr_t
* lck_attrs
;
137 lck_attrs
= lck_attr_alloc_init();
138 lck_mtx
= lck_mtx_alloc_init(lck_grp
, lck_attrs
);
139 lck_attr_free(lck_attrs
);
143 static lck_mtx_t
* vlan_lck_mtx
;
145 static __inline__
void
148 lck_grp_t
* vlan_lck_grp
;
150 vlan_lck_grp
= my_lck_grp_alloc_init("if_vlan");
151 vlan_lck_mtx
= my_lck_mtx_alloc_init(vlan_lck_grp
);
154 static __inline__
void
155 vlan_assert_lock_held(void)
157 lck_mtx_assert(vlan_lck_mtx
, LCK_MTX_ASSERT_OWNED
);
161 static __inline__
void
162 vlan_assert_lock_not_held(void)
164 lck_mtx_assert(vlan_lck_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
168 static __inline__
void
171 lck_mtx_lock(vlan_lck_mtx
);
175 static __inline__
void
178 lck_mtx_unlock(vlan_lck_mtx
);
183 ** vlan structures, types
186 LIST_HEAD(vlan_parent_list
, vlan_parent
);
188 LIST_HEAD(ifvlan_list
, ifvlan
);
190 typedef struct vlan_parent
{
191 LIST_ENTRY(vlan_parent
) vlp_parent_list
;/* list of parents */
192 struct ifnet
* vlp_ifp
; /* interface */
193 struct ifvlan_list vlp_vlan_list
; /* list of VLAN's */
194 #define VLPF_SUPPORTS_VLAN_MTU 0x1
195 #define VLPF_CHANGE_IN_PROGRESS 0x2
196 #define VLPF_DETACHING 0x4
198 struct ifdevmtu vlp_devmtu
;
199 SInt32 vlp_retain_count
;
200 } vlan_parent
, * vlan_parent_ref
;
203 LIST_ENTRY(ifvlan
) ifv_vlan_list
;
204 char ifv_name
[IFNAMSIZ
]; /* our unique id */
205 struct ifnet
* ifv_ifp
; /* our interface */
206 vlan_parent_ref ifv_vlp
; /* parent information */
208 u_int16_t ifvm_encaplen
;/* encapsulation length */
209 u_int16_t ifvm_mtufudge
;/* MTU fudged by this much */
210 u_int16_t ifvm_proto
; /* encapsulation ethertype */
211 u_int16_t ifvm_tag
; /* tag to apply on packets leaving if */
213 struct multicast_list ifv_multicast
;
214 #define IFVF_PROMISC 0x1 /* promiscuous mode enabled */
215 #define IFVF_DETACHING 0x2 /* interface is detaching */
216 #define IFVF_READY 0x4 /* interface is ready */
218 bpf_packet_func ifv_bpf_input
;
219 bpf_packet_func ifv_bpf_output
;
222 typedef struct ifvlan
* ifvlan_ref
;
224 typedef struct vlan_globals_s
{
225 struct vlan_parent_list parent_list
;
227 } * vlan_globals_ref
;
229 static vlan_globals_ref g_vlan
;
231 #define ifv_tag ifv_mib.ifvm_tag
232 #define ifv_encaplen ifv_mib.ifvm_encaplen
233 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
237 ** vlan_parent_ref vlp_flags in-lines
239 static __inline__
int
240 vlan_parent_flags_supports_vlan_mtu(vlan_parent_ref vlp
)
242 return ((vlp
->vlp_flags
& VLPF_SUPPORTS_VLAN_MTU
) != 0);
245 static __inline__
void
246 vlan_parent_flags_set_supports_vlan_mtu(vlan_parent_ref vlp
)
248 vlp
->vlp_flags
|= VLPF_SUPPORTS_VLAN_MTU
;
252 static __inline__
void
253 vlan_parent_flags_clear_supports_vlan_mtu(vlan_parent_ref vlp
)
255 vlp
->vlp_flags
&= ~VLPF_SUPPORTS_VLAN_MTU
;
259 static __inline__
int
260 vlan_parent_flags_change_in_progress(vlan_parent_ref vlp
)
262 return ((vlp
->vlp_flags
& VLPF_CHANGE_IN_PROGRESS
) != 0);
265 static __inline__
void
266 vlan_parent_flags_set_change_in_progress(vlan_parent_ref vlp
)
268 vlp
->vlp_flags
|= VLPF_CHANGE_IN_PROGRESS
;
272 static __inline__
void
273 vlan_parent_flags_clear_change_in_progress(vlan_parent_ref vlp
)
275 vlp
->vlp_flags
&= ~VLPF_CHANGE_IN_PROGRESS
;
279 static __inline__
int
280 vlan_parent_flags_detaching(struct vlan_parent
* vlp
)
282 return ((vlp
->vlp_flags
& VLPF_DETACHING
) != 0);
285 static __inline__
void
286 vlan_parent_flags_set_detaching(struct vlan_parent
* vlp
)
288 vlp
->vlp_flags
|= VLPF_DETACHING
;
294 ** ifvlan_flags in-lines routines
296 static __inline__
int
297 ifvlan_flags_promisc(ifvlan_ref ifv
)
299 return ((ifv
->ifv_flags
& IFVF_PROMISC
) != 0);
302 static __inline__
void
303 ifvlan_flags_set_promisc(ifvlan_ref ifv
)
305 ifv
->ifv_flags
|= IFVF_PROMISC
;
309 static __inline__
void
310 ifvlan_flags_clear_promisc(ifvlan_ref ifv
)
312 ifv
->ifv_flags
&= ~IFVF_PROMISC
;
316 static __inline__
int
317 ifvlan_flags_ready(ifvlan_ref ifv
)
319 return ((ifv
->ifv_flags
& IFVF_READY
) != 0);
322 static __inline__
void
323 ifvlan_flags_set_ready(ifvlan_ref ifv
)
325 ifv
->ifv_flags
|= IFVF_READY
;
329 static __inline__
void
330 ifvlan_flags_clear_ready(ifvlan_ref ifv
)
332 ifv
->ifv_flags
&= ~IFVF_READY
;
336 static __inline__
int
337 ifvlan_flags_detaching(ifvlan_ref ifv
)
339 return ((ifv
->ifv_flags
& IFVF_DETACHING
) != 0);
342 static __inline__
void
343 ifvlan_flags_set_detaching(ifvlan_ref ifv
)
345 ifv
->ifv_flags
|= IFVF_DETACHING
;
350 SYSCTL_DECL(_net_link
);
351 SYSCTL_NODE(_net_link
, IFT_L2VLAN
, vlan
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "IEEE 802.1Q VLAN");
352 SYSCTL_NODE(_net_link_vlan
, PF_LINK
, link
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "for consistency");
355 #define M_VLAN M_DEVBUF
357 static int vlan_clone_create(struct if_clone
*, int);
358 static void vlan_clone_destroy(struct ifnet
*);
359 static int vlan_input(ifnet_t ifp
, protocol_family_t protocol
,
360 mbuf_t m
, char *frame_header
);
361 static int vlan_output(struct ifnet
*ifp
, struct mbuf
*m
);
362 static int vlan_ioctl(ifnet_t ifp
, u_int32_t cmd
, void * addr
);
363 static int vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
,
364 bpf_packet_func func
);
365 static int vlan_attach_protocol(struct ifnet
*ifp
);
366 static int vlan_detach_protocol(struct ifnet
*ifp
);
367 static int vlan_setmulti(struct ifnet
*ifp
);
368 static int vlan_unconfig(struct ifnet
*ifp
);
369 static int vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
);
370 static void vlan_if_free(struct ifnet
* ifp
);
371 static void vlan_remove(ifvlan_ref ifv
);
372 static void vlan_if_detach(struct ifnet
* ifp
);
373 static int vlan_new_mtu(struct ifnet
* ifp
, int mtu
);
375 static struct if_clone vlan_cloner
= IF_CLONE_INITIALIZER(VLANNAME
,
380 static void interface_link_event(struct ifnet
* ifp
, u_long event_code
);
381 static void vlan_parent_link_event(vlan_parent_ref vlp
,
383 extern void dlil_input_packet_list(struct ifnet
*ifp
, struct mbuf
*m
);
386 vlan_globals_init(void)
390 vlan_assert_lock_not_held();
392 if (g_vlan
!= NULL
) {
395 v
= _MALLOC(sizeof(*v
), M_VLAN
, M_WAITOK
);
397 LIST_INIT(&v
->parent_list
);
401 if (g_vlan
!= NULL
) {
417 siocgifdevmtu(struct ifnet
* ifp
, struct ifdevmtu
* ifdm_p
)
422 bzero(&ifr
, sizeof(ifr
));
423 error
= ifnet_ioctl(ifp
, 0,SIOCGIFDEVMTU
, &ifr
);
425 *ifdm_p
= ifr
.ifr_devmtu
;
431 siocsifaltmtu(struct ifnet
* ifp
, int mtu
)
435 bzero(&ifr
, sizeof(ifr
));
437 return (ifnet_ioctl(ifp
, 0, SIOCSIFALTMTU
, &ifr
));
440 static __inline__
void
441 vlan_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
442 bpf_packet_func func
)
450 static __inline__
void
451 vlan_bpf_input(struct ifnet
* ifp
, struct mbuf
* m
,
452 bpf_packet_func func
, char * frame_header
,
453 int frame_header_len
, int encap_len
)
457 /* present the right header to bpf */
458 bcopy(frame_header
, frame_header
+ encap_len
, frame_header_len
);
460 m
->m_data
-= frame_header_len
;
461 m
->m_len
+= frame_header_len
;
463 m
->m_data
+= frame_header_len
;
464 m
->m_len
-= frame_header_len
;
466 /* restore the header */
467 bcopy(frame_header
+ encap_len
, frame_header
, frame_header_len
);
474 ** vlan_parent synchronization routines
476 static __inline__
void
477 vlan_parent_retain(vlan_parent_ref vlp
)
479 OSIncrementAtomic(&vlp
->vlp_retain_count
);
482 static __inline__
void
483 vlan_parent_release(vlan_parent_ref vlp
)
485 UInt32 old_retain_count
;
487 old_retain_count
= OSDecrementAtomic(&vlp
->vlp_retain_count
);
488 switch (old_retain_count
) {
490 panic("vlan_parent_release: retain count is 0\n");
493 if (g_vlan
->verbose
) {
494 struct ifnet
* ifp
= vlp
->vlp_ifp
;
495 printf("vlan_parent_release(%s%d)\n", ifnet_name(ifp
),
507 * Function: vlan_parent_wait
509 * Allows a single thread to gain exclusive access to the vlan_parent
510 * data structure. Some operations take a long time to complete,
511 * and some have side-effects that we can't predict. Holding the
512 * vlan_lock() across such operations is not possible.
515 * Before calling, you must be holding the vlan_lock and have taken
516 * a reference on the vlan_parent_ref.
519 vlan_parent_wait(vlan_parent_ref vlp
, const char * msg
)
523 /* other add/remove/multicast-change in progress */
524 while (vlan_parent_flags_change_in_progress(vlp
)) {
525 if (g_vlan
->verbose
) {
526 struct ifnet
* ifp
= vlp
->vlp_ifp
;
528 printf("%s%d: %s msleep\n", ifnet_name(ifp
), ifnet_unit(ifp
), msg
);
531 (void)msleep(vlp
, vlan_lck_mtx
, PZERO
, msg
, 0);
533 /* prevent other vlan parent remove/add from taking place */
534 vlan_parent_flags_set_change_in_progress(vlp
);
535 if (g_vlan
->verbose
&& waited
) {
536 struct ifnet
* ifp
= vlp
->vlp_ifp
;
538 printf("%s%d: %s woke up\n", ifnet_name(ifp
), ifnet_unit(ifp
), msg
);
544 * Function: vlan_parent_signal
546 * Allows the thread that previously invoked vlan_parent_wait() to
547 * give up exclusive access to the vlan_parent data structure, and wake up
548 * any other threads waiting to access
550 * Before calling, you must be holding the vlan_lock and have taken
551 * a reference on the vlan_parent_ref.
554 vlan_parent_signal(vlan_parent_ref vlp
, const char * msg
)
556 vlan_parent_flags_clear_change_in_progress(vlp
);
557 wakeup((caddr_t
)vlp
);
558 if (g_vlan
->verbose
) {
559 struct ifnet
* ifp
= vlp
->vlp_ifp
;
561 printf("%s%d: %s wakeup\n", ifnet_name(ifp
), ifnet_unit(ifp
), msg
);
568 * Program our multicast filter. What we're actually doing is
569 * programming the multicast filter of the parent. This has the
570 * side effect of causing the parent interface to receive multicast
571 * traffic that it doesn't really want, which ends up being discarded
572 * later by the upper protocol layers. Unfortunately, there's no way
573 * to avoid this: there really is only one physical interface.
576 vlan_setmulti(struct ifnet
* ifp
)
584 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
585 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
590 /* no parent, no need to program the multicast filter */
593 if (vlan_parent_flags_detaching(vlp
)) {
596 vlan_parent_retain(vlp
);
597 vlan_parent_wait(vlp
, "vlan_setmulti");
599 /* check again, things could have changed */
600 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
601 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
604 if (ifv
->ifv_vlp
!= vlp
) {
605 /* vlan parent changed */
609 /* no parent, no need to program the multicast filter */
615 /* update parent interface with our multicast addresses */
616 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
621 vlan_parent_signal(vlp
, "vlan_setmulti");
629 ** vlan_parent list manipulation/lookup routines
631 static vlan_parent_ref
632 parent_list_lookup(struct ifnet
* p
)
636 LIST_FOREACH(vlp
, &g_vlan
->parent_list
, vlp_parent_list
) {
637 if (vlp
->vlp_ifp
== p
) {
645 vlan_parent_lookup_tag(vlan_parent_ref vlp
, int tag
)
649 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
650 if (tag
== ifv
->ifv_tag
) {
658 vlan_lookup_parent_and_tag(struct ifnet
* p
, int tag
)
662 vlp
= parent_list_lookup(p
);
664 return (vlan_parent_lookup_tag(vlp
, tag
));
670 vlan_parent_find_max_mtu(vlan_parent_ref vlp
, ifvlan_ref exclude_ifv
)
675 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
678 if (exclude_ifv
== ifv
) {
681 req_mtu
= ifnet_mtu(ifv
->ifv_ifp
) + ifv
->ifv_mtufudge
;
682 if (req_mtu
> max_mtu
) {
690 * Function: vlan_parent_create
692 * Create a vlan_parent structure to hold the VLAN's for the given
693 * interface. Add it to the list of VLAN parents.
696 vlan_parent_create(struct ifnet
* p
, vlan_parent_ref
* ret_vlp
)
702 vlp
= _MALLOC(sizeof(*vlp
), M_VLAN
, M_WAITOK
);
706 bzero(vlp
, sizeof(*vlp
));
707 error
= siocgifdevmtu(p
, &vlp
->vlp_devmtu
);
709 printf("vlan_parent_create (%s%d): siocgifdevmtu failed, %d\n",
710 ifnet_name(p
), ifnet_unit(p
), error
);
714 LIST_INIT(&vlp
->vlp_vlan_list
);
716 vlan_parent_retain(vlp
);
718 & (IF_HWASSIST_VLAN_MTU
| IF_HWASSIST_VLAN_TAGGING
)) {
719 vlan_parent_flags_set_supports_vlan_mtu(vlp
);
726 vlan_parent_remove_all_vlans(vlan_parent_ref vlp
)
731 vlan_assert_lock_held();
733 while ((ifv
= LIST_FIRST(&vlp
->vlp_vlan_list
)) != NULL
) {
736 vlan_if_detach(ifv
->ifv_ifp
);
740 /* the vlan parent has no more VLAN's */
742 ifnet_set_eflags(p
, 0, IFEF_VLAN
); /* clear IFEF_VLAN */
743 LIST_REMOVE(vlp
, vlp_parent_list
);
745 vlan_parent_release(vlp
);
751 static __inline__
int
752 vlan_parent_no_vlans(vlan_parent_ref vlp
)
754 return (LIST_EMPTY(&vlp
->vlp_vlan_list
));
758 vlan_parent_add_vlan(vlan_parent_ref vlp
, ifvlan_ref ifv
, int tag
)
760 LIST_INSERT_HEAD(&vlp
->vlp_vlan_list
, ifv
, ifv_vlan_list
);
767 vlan_parent_remove_vlan(__unused vlan_parent_ref vlp
, ifvlan_ref ifv
)
770 LIST_REMOVE(ifv
, ifv_vlan_list
);
775 vlan_clone_attach(void)
777 if_clone_attach(&vlan_cloner
);
783 vlan_clone_create(struct if_clone
*ifc
, int unit
)
788 struct ifnet_init_params vlan_init
;
790 error
= vlan_globals_init();
794 ifv
= _MALLOC(sizeof(struct ifvlan
), M_VLAN
, M_WAITOK
);
795 bzero(ifv
, sizeof(struct ifvlan
));
796 multicast_list_init(&ifv
->ifv_multicast
);
798 /* use the interface name as the unique id for ifp recycle */
799 if ((unsigned int)snprintf(ifv
->ifv_name
, sizeof(ifv
->ifv_name
), "%s%d",
800 ifc
->ifc_name
, unit
) >= sizeof(ifv
->ifv_name
)) {
805 bzero(&vlan_init
, sizeof(vlan_init
));
806 vlan_init
.uniqueid
= ifv
->ifv_name
;
807 vlan_init
.uniqueid_len
= strlen(ifv
->ifv_name
);
808 vlan_init
.name
= ifc
->ifc_name
;
809 vlan_init
.unit
= unit
;
810 vlan_init
.family
= IFNET_FAMILY_VLAN
;
811 vlan_init
.type
= IFT_L2VLAN
;
812 vlan_init
.output
= vlan_output
;
813 vlan_init
.demux
= ether_demux
;
814 vlan_init
.add_proto
= ether_add_proto
;
815 vlan_init
.del_proto
= ether_del_proto
;
816 vlan_init
.check_multi
= ether_check_multi
;
817 vlan_init
.framer
= ether_frameout
;
818 vlan_init
.softc
= ifv
;
819 vlan_init
.ioctl
= vlan_ioctl
;
820 vlan_init
.set_bpf_tap
= vlan_set_bpf_tap
;
821 vlan_init
.detach
= vlan_if_free
;
822 vlan_init
.broadcast_addr
= etherbroadcastaddr
;
823 vlan_init
.broadcast_len
= ETHER_ADDR_LEN
;
824 error
= ifnet_allocate(&vlan_init
, &ifp
);
832 /* NB: flags are not set here */
833 ifnet_set_link_mib_data(ifp
, &ifv
->ifv_mib
, sizeof ifv
->ifv_mib
);
834 /* NB: mtu is not set here */
837 ifnet_set_offload(ifp
, 0);
838 ifnet_set_addrlen(ifp
, ETHER_ADDR_LEN
); /* XXX ethernet specific */
839 ifnet_set_baudrate(ifp
, 0);
840 ifnet_set_hdrlen(ifp
, ETHER_VLAN_ENCAP_LEN
);
842 error
= ifnet_attach(ifp
, NULL
);
850 /* attach as ethernet */
851 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
856 vlan_remove(ifvlan_ref ifv
)
858 vlan_assert_lock_held();
859 ifvlan_flags_set_detaching(ifv
);
860 vlan_unconfig(ifv
->ifv_ifp
);
865 vlan_if_detach(struct ifnet
* ifp
)
872 vlan_clone_destroy(struct ifnet
*ifp
)
877 ifv
= ifnet_softc(ifp
);
878 if (ifv
== NULL
|| ifnet_type(ifp
) != IFT_L2VLAN
) {
882 if (ifvlan_flags_detaching(ifv
)) {
893 vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
898 ifv
= ifnet_softc(ifp
);
899 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
904 case BPF_TAP_DISABLE
:
905 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= NULL
;
909 ifv
->ifv_bpf_input
= func
;
913 ifv
->ifv_bpf_output
= func
;
916 case BPF_TAP_INPUT_OUTPUT
:
917 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= func
;
927 vlan_output(struct ifnet
* ifp
, struct mbuf
* m
)
929 bpf_packet_func bpf_func
;
930 struct ether_vlan_header
* evl
;
941 if ((m
->m_flags
& M_PKTHDR
) == 0) {
946 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
947 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)
948 || ifvlan_flags_ready(ifv
) == 0) {
960 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
961 soft_vlan
= (ifnet_offload(p
) & IF_HWASSIST_VLAN_TAGGING
) == 0;
962 bpf_func
= ifv
->ifv_bpf_output
;
964 encaplen
= ifv
->ifv_encaplen
;
966 vlan_bpf_output(ifp
, m
, bpf_func
);
968 /* do not run parent's if_output() if the parent is not up */
969 if ((ifnet_flags(p
) & (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
)) {
971 ifp
->if_collisions
++;
975 * If underlying interface can do VLAN tag insertion itself,
976 * just pass the packet along. However, we need some way to
977 * tell the interface where the packet came from so that it
978 * knows how to find the VLAN tag to use. We use a field in
979 * the mbuf header to store the VLAN tag, and a bit in the
980 * csum_flags field to mark the field as valid.
982 if (soft_vlan
== 0) {
983 m
->m_pkthdr
.csum_flags
|= CSUM_VLAN_TAG_VALID
;
984 m
->m_pkthdr
.vlan_tag
= tag
;
986 M_PREPEND(m
, encaplen
, M_DONTWAIT
);
988 printf("%s%d: unable to prepend VLAN header\n", ifnet_name(ifp
),
993 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
994 if (m
->m_len
< (int)sizeof(*evl
)) {
995 m
= m_pullup(m
, sizeof(*evl
));
997 printf("%s%d: unable to pullup VLAN header\n", ifnet_name(ifp
),
1005 * Transform the Ethernet header into an Ethernet header
1006 * with 802.1Q encapsulation.
1008 bcopy(mtod(m
, char *) + encaplen
,
1009 mtod(m
, char *), ETHER_HDR_LEN
);
1010 evl
= mtod(m
, struct ether_vlan_header
*);
1011 evl
->evl_proto
= evl
->evl_encap_proto
;
1012 evl
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
1013 evl
->evl_tag
= htons(tag
);
1015 return ifnet_output_raw(p
, PF_VLAN
, m
);
1019 vlan_input(ifnet_t p
, __unused protocol_family_t protocol
,
1020 mbuf_t m
, char *frame_header
)
1022 bpf_packet_func bpf_func
= NULL
;
1023 struct ether_vlan_header
* evl
;
1024 struct ifnet
* ifp
= NULL
;
1028 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1030 * Packet is tagged, m contains a normal
1031 * Ethernet frame; the tag is stored out-of-band.
1033 m
->m_pkthdr
.csum_flags
&= ~CSUM_VLAN_TAG_VALID
;
1034 tag
= EVL_VLANOFTAG(m
->m_pkthdr
.vlan_tag
);
1035 m
->m_pkthdr
.vlan_tag
= 0;
1038 switch (ifnet_type(p
)) {
1040 if (m
->m_len
< ETHER_VLAN_ENCAP_LEN
) {
1044 evl
= (struct ether_vlan_header
*)frame_header
;
1045 if (ntohs(evl
->evl_proto
) == ETHERTYPE_VLAN
) {
1046 /* don't allow VLAN within VLAN */
1050 tag
= EVL_VLANOFTAG(ntohs(evl
->evl_tag
));
1053 * Restore the original ethertype. We'll remove
1054 * the encapsulation after we've found the vlan
1055 * interface corresponding to the tag.
1057 evl
->evl_encap_proto
= evl
->evl_proto
;
1060 printf("vlan_demux: unsupported if type %u",
1070 if ((ifnet_eflags(p
) & IFEF_VLAN
) == 0) {
1071 /* don't bother looking through the VLAN list */
1076 ifv
= vlan_lookup_parent_and_tag(p
, tag
);
1081 || ifvlan_flags_ready(ifv
) == 0
1082 || (ifnet_flags(ifp
) & IFF_UP
) == 0) {
1087 bpf_func
= ifv
->ifv_bpf_input
;
1092 * Packet had an in-line encapsulation header;
1093 * remove it. The original header has already
1094 * been fixed up above.
1096 m
->m_len
-= ETHER_VLAN_ENCAP_LEN
;
1097 m
->m_data
+= ETHER_VLAN_ENCAP_LEN
;
1098 m
->m_pkthdr
.len
-= ETHER_VLAN_ENCAP_LEN
;
1099 m
->m_pkthdr
.csum_flags
= 0; /* can't trust hardware checksum */
1102 m
->m_pkthdr
.rcvif
= ifp
;
1103 m
->m_pkthdr
.header
= frame_header
;
1104 (void)ifnet_stat_increment_in(ifp
, 1,
1105 m
->m_pkthdr
.len
+ ETHER_HDR_LEN
, 0);
1106 vlan_bpf_input(ifp
, m
, bpf_func
, frame_header
, ETHER_HDR_LEN
,
1107 soft_vlan
? ETHER_VLAN_ENCAP_LEN
: 0);
1108 /* We found a vlan interface, inject on that interface. */
1109 dlil_input_packet_list(ifp
, m
);
1111 /* Send priority-tagged packet up through the parent */
1112 dlil_input_packet_list(p
, m
);
1117 #define VLAN_CONFIG_PROGRESS_VLP_RETAINED 0x1
1118 #define VLAN_CONFIG_PROGRESS_IN_LIST 0x2
1121 vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
)
1125 ifvlan_ref ifv
= NULL
;
1126 vlan_parent_ref new_vlp
= NULL
;
1127 int need_vlp_release
= 0;
1128 u_int16_t parent_flags
;
1129 u_int32_t progress
= 0;
1130 vlan_parent_ref vlp
= NULL
;
1132 /* pre-allocate space for vlan_parent, in case we're first */
1133 error
= vlan_parent_create(p
, &new_vlp
);
1139 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1140 if (ifv
!= NULL
&& ifv
->ifv_vlp
!= NULL
) {
1142 vlan_parent_release(new_vlp
);
1145 vlp
= parent_list_lookup(p
);
1147 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1148 /* already a VLAN with that tag on this interface */
1154 /* we're the first VLAN on this interface */
1155 LIST_INSERT_HEAD(&g_vlan
->parent_list
, new_vlp
, vlp_parent_list
);
1159 /* need to wait to ensure no one else is trying to add/remove */
1160 vlan_parent_retain(vlp
);
1161 progress
|= VLAN_CONFIG_PROGRESS_VLP_RETAINED
;
1162 vlan_parent_wait(vlp
, "vlan_config");
1164 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1169 if (vlan_parent_flags_detaching(vlp
)
1170 || ifvlan_flags_detaching(ifv
) || ifv
->ifv_vlp
!= NULL
) {
1175 /* check again because someone might have gotten in */
1176 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1177 /* already a VLAN with that tag on this interface */
1182 if (vlan_parent_no_vlans(vlp
)) {
1185 vlan_parent_add_vlan(vlp
, ifv
, tag
);
1186 progress
|= VLAN_CONFIG_PROGRESS_IN_LIST
;
1188 /* check whether bond interface is using parent interface */
1189 ifnet_lock_exclusive(p
);
1190 if ((ifnet_eflags(p
) & IFEF_BOND
) != 0) {
1192 /* don't allow VLAN over interface that's already part of a bond */
1196 /* prevent BOND interface from using it */
1197 /* Can't use ifnet_set_eflags because that would take the lock */
1198 p
->if_eflags
|= IFEF_VLAN
;
1203 /* attach our VLAN "protocol" to the interface */
1204 error
= vlan_attach_protocol(p
);
1209 /* mark the parent interface up */
1210 ifnet_set_flags(p
, IFF_UP
, IFF_UP
);
1211 (void)ifnet_ioctl(p
, 0, SIOCSIFFLAGS
, (caddr_t
)NULL
);
1214 /* configure parent to receive our multicast addresses */
1215 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
1218 (void)vlan_detach_protocol(p
);
1224 /* set our ethernet address to that of the parent */
1225 ifnet_set_lladdr_and_type(ifp
, ifnet_lladdr(p
), ETHER_ADDR_LEN
, IFT_ETHER
);
1227 /* no failures past this point */
1230 ifv
->ifv_encaplen
= ETHER_VLAN_ENCAP_LEN
;
1232 if (vlan_parent_flags_supports_vlan_mtu(vlp
)) {
1233 ifv
->ifv_mtufudge
= 0;
1236 * Fudge the MTU by the encapsulation size. This
1237 * makes us incompatible with strictly compliant
1238 * 802.1Q implementations, but allows us to use
1239 * the feature with other NetBSD implementations,
1240 * which might still be useful.
1242 ifv
->ifv_mtufudge
= ifv
->ifv_encaplen
;
1244 ifnet_set_mtu(ifp
, ETHERMTU
- ifv
->ifv_mtufudge
);
1247 * Copy only a selected subset of flags from the parent.
1248 * Other flags are none of our business.
1250 parent_flags
= ifnet_flags(p
)
1251 & (IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
);
1252 ifnet_set_flags(ifp
, parent_flags
,
1253 IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
);
1256 * If the parent interface can do hardware-assisted
1257 * VLAN encapsulation, then propagate its hardware-
1258 * assisted checksumming flags.
1260 if (ifnet_offload(p
) & IF_HWASSIST_VLAN_TAGGING
) {
1261 ifnet_set_offload(ifp
, IF_HWASSIST_CSUM_FLAGS(ifnet_offload(p
)));
1264 ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
1265 ifvlan_flags_set_ready(ifv
);
1266 vlan_parent_signal(vlp
, "vlan_config");
1268 if (new_vlp
!= vlp
) {
1269 /* throw it away, it wasn't needed */
1270 vlan_parent_release(new_vlp
);
1275 vlan_assert_lock_held();
1276 vlan_parent_signal(vlp
, "vlan_config");
1279 if ((progress
& VLAN_CONFIG_PROGRESS_IN_LIST
) != 0) {
1280 vlan_parent_remove_vlan(vlp
, ifv
);
1282 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1283 /* the vlan parent has no more VLAN's */
1284 ifnet_set_eflags(p
, 0, IFEF_VLAN
);
1285 LIST_REMOVE(vlp
, vlp_parent_list
);
1286 /* release outside of the lock below */
1287 need_vlp_release
= 1;
1291 if ((progress
& VLAN_CONFIG_PROGRESS_VLP_RETAINED
) != 0) {
1292 vlan_parent_release(vlp
);
1294 if (need_vlp_release
) {
1295 vlan_parent_release(vlp
);
1297 if (new_vlp
!= vlp
) {
1298 vlan_parent_release(new_vlp
);
1304 vlan_link_event(struct ifnet
* ifp
, struct ifnet
* p
)
1306 struct ifmediareq ifmr
;
1308 /* generate a link event based on the state of the underlying interface */
1309 bzero(&ifmr
, sizeof(ifmr
));
1310 snprintf(ifmr
.ifm_name
, sizeof(ifmr
.ifm_name
),
1311 "%s%d", ifnet_name(p
), ifnet_unit(p
));
1312 if (ifnet_ioctl(p
, 0, SIOCGIFMEDIA
, &ifmr
) == 0
1313 && ifmr
.ifm_count
> 0 && ifmr
.ifm_status
& IFM_AVALID
) {
1316 event
= (ifmr
.ifm_status
& IFM_ACTIVE
)
1317 ? KEV_DL_LINK_ON
: KEV_DL_LINK_OFF
;
1318 interface_link_event(ifp
, event
);
1324 vlan_unconfig(struct ifnet
* ifp
)
1329 int need_vlp_release
= 0;
1331 vlan_parent_ref vlp
;
1333 vlan_assert_lock_held();
1334 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1342 vlan_parent_retain(vlp
);
1343 vlan_parent_wait(vlp
, "vlan_unconfig");
1345 /* check again because another thread could be in vlan_unconfig */
1346 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1350 if (ifv
->ifv_vlp
!= vlp
) {
1351 /* vlan parent changed */
1357 /* remember whether we're the last VLAN on the parent */
1358 if (LIST_NEXT(LIST_FIRST(&vlp
->vlp_vlan_list
), ifv_vlan_list
) == NULL
) {
1359 if (g_vlan
->verbose
) {
1360 printf("vlan_unconfig: last vlan on %s%d\n",
1361 ifnet_name(p
), ifnet_unit(p
));
1366 /* back-out any effect our mtu might have had on the parent */
1367 (void)vlan_new_mtu(ifp
, ETHERMTU
- ifv
->ifv_mtufudge
);
1371 /* detach VLAN "protocol" */
1373 (void)vlan_detach_protocol(p
);
1376 /* un-join multicast on parent interface */
1377 (void)multicast_list_remove(&ifv
->ifv_multicast
);
1379 /* Clear our MAC address. */
1380 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_L2VLAN
);
1384 /* Disconnect from parent. */
1385 vlan_parent_remove_vlan(vlp
, ifv
);
1387 /* return to the state we were in before SIFVLAN */
1388 ifnet_set_mtu(ifp
, 0);
1389 ifnet_set_flags(ifp
, 0,
1390 IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
| IFF_RUNNING
);
1391 ifnet_set_offload(ifp
, 0);
1393 ifv
->ifv_mtufudge
= 0;
1395 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1396 /* the vlan parent has no more VLAN's */
1397 ifnet_set_eflags(p
, 0, IFEF_VLAN
);
1398 LIST_REMOVE(vlp
, vlp_parent_list
);
1399 /* release outside of the lock below */
1404 vlan_parent_signal(vlp
, "vlan_unconfig");
1406 vlan_parent_release(vlp
); /* one because we waited */
1408 while (need_vlp_release
--) {
1409 vlan_parent_release(vlp
);
1416 vlan_set_promisc(struct ifnet
* ifp
)
1420 vlan_parent_ref vlp
;
1423 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1424 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1425 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1433 if ((ifnet_flags(ifp
) & IFF_PROMISC
) != 0) {
1434 if (!ifvlan_flags_promisc(ifv
)) {
1435 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 1);
1437 ifvlan_flags_set_promisc(ifv
);
1441 if (ifvlan_flags_promisc(ifv
)) {
1442 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 0);
1444 ifvlan_flags_clear_promisc(ifv
);
1454 vlan_new_mtu(struct ifnet
* ifp
, int mtu
)
1456 struct ifdevmtu
* devmtu_p
;
1462 vlan_parent_ref vlp
;
1464 vlan_assert_lock_held();
1465 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1467 devmtu_p
= &vlp
->vlp_devmtu
;
1468 req_mtu
= mtu
+ ifv
->ifv_mtufudge
;
1469 if (req_mtu
> devmtu_p
->ifdm_max
|| req_mtu
< devmtu_p
->ifdm_min
) {
1472 max_mtu
= vlan_parent_find_max_mtu(vlp
, ifv
);
1473 if (req_mtu
> max_mtu
) {
1476 else if (max_mtu
< devmtu_p
->ifdm_current
) {
1480 struct ifnet
* p
= vlp
->vlp_ifp
;
1482 error
= siocsifaltmtu(p
, new_mtu
);
1487 devmtu_p
->ifdm_current
= new_mtu
;
1489 ifnet_set_mtu(ifp
, mtu
);
1495 vlan_set_mtu(struct ifnet
* ifp
, int mtu
)
1499 vlan_parent_ref vlp
;
1501 if (mtu
< IF_MINMTU
) {
1505 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1506 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1508 return ((ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
);
1511 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1518 vlan_parent_retain(vlp
);
1519 vlan_parent_wait(vlp
, "vlan_set_mtu");
1521 /* check again, something might have changed */
1522 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1523 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1524 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1527 if (ifv
->ifv_vlp
!= vlp
) {
1528 /* vlan parent changed */
1531 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1537 error
= vlan_new_mtu(ifp
, mtu
);
1540 vlan_parent_signal(vlp
, "vlan_set_mtu");
1542 vlan_parent_release(vlp
);
1548 vlan_ioctl(ifnet_t ifp
, u_int32_t cmd
, void * data
)
1550 struct ifdevmtu
* devmtu_p
;
1552 struct ifaddr
* ifa
;
1553 struct ifmediareq64
* ifmr
;
1558 user_addr_t user_addr
;
1559 vlan_parent_ref vlp
;
1562 if (ifnet_type(ifp
) != IFT_L2VLAN
) {
1563 return (EOPNOTSUPP
);
1565 ifr
= (struct ifreq
*)data
;
1566 ifa
= (struct ifaddr
*)data
;
1570 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
1573 case SIOCGIFMEDIA64
:
1576 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1577 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1579 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1581 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1583 ifmr
= (struct ifmediareq64
*)data
;
1584 user_addr
= proc_is64bit(current_proc())
1585 ? ifmr
->ifm_ifmu
.ifmu_ulist64
1586 : CAST_USER_ADDR_T(ifmr
->ifm_ifmu
.ifmu_ulist32
);
1588 struct ifmediareq64 p_ifmr
;
1590 bzero(&p_ifmr
, sizeof(p_ifmr
));
1591 error
= ifnet_ioctl(p
, 0, SIOCGIFMEDIA
, &p_ifmr
);
1593 ifmr
->ifm_active
= p_ifmr
.ifm_active
;
1594 ifmr
->ifm_current
= p_ifmr
.ifm_current
;
1595 ifmr
->ifm_mask
= p_ifmr
.ifm_mask
;
1596 ifmr
->ifm_status
= p_ifmr
.ifm_status
;
1597 ifmr
->ifm_count
= p_ifmr
.ifm_count
;
1598 /* Limit the result to the parent's current config. */
1599 if (ifmr
->ifm_count
>= 1 && user_addr
!= USER_ADDR_NULL
) {
1600 ifmr
->ifm_count
= 1;
1601 error
= copyout(&ifmr
->ifm_current
, user_addr
,
1606 ifmr
->ifm_active
= ifmr
->ifm_current
= IFM_NONE
;
1608 ifmr
->ifm_status
= IFM_AVALID
;
1609 ifmr
->ifm_count
= 1;
1610 if (user_addr
!= USER_ADDR_NULL
) {
1611 error
= copyout(&ifmr
->ifm_current
, user_addr
, sizeof(int));
1622 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1623 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1625 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1629 int min_mtu
= vlp
->vlp_devmtu
.ifdm_min
- ifv
->ifv_mtufudge
;
1630 devmtu_p
= &ifr
->ifr_devmtu
;
1631 devmtu_p
->ifdm_current
= ifnet_mtu(ifp
);
1632 devmtu_p
->ifdm_min
= max(min_mtu
, IF_MINMTU
);
1633 devmtu_p
->ifdm_max
= vlp
->vlp_devmtu
.ifdm_max
- ifv
->ifv_mtufudge
;
1636 devmtu_p
= &ifr
->ifr_devmtu
;
1637 devmtu_p
->ifdm_current
= 0;
1638 devmtu_p
->ifdm_min
= 0;
1639 devmtu_p
->ifdm_max
= 0;
1645 error
= vlan_set_mtu(ifp
, ifr
->ifr_mtu
);
1649 user_addr
= proc_is64bit(current_proc())
1650 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1651 error
= copyin(user_addr
, &vlr
, sizeof(vlr
));
1656 if (vlr
.vlr_parent
[0] != '\0') {
1657 if (vlr
.vlr_tag
& ~EVL_VLID_MASK
) {
1659 * Don't let the caller set up a VLAN tag with
1660 * anything except VLID bits.
1665 p
= ifunit(vlr
.vlr_parent
);
1670 /* can't do VLAN over anything but ethernet or ethernet aggregate */
1671 if (ifnet_type(p
) != IFT_ETHER
1672 && ifnet_type(p
) != IFT_IEEE8023ADLAG
) {
1673 error
= EPROTONOSUPPORT
;
1676 error
= vlan_config(ifp
, p
, vlr
.vlr_tag
);
1681 /* Update promiscuous mode, if necessary. */
1682 (void)vlan_set_promisc(ifp
);
1684 /* generate a link event based on the state of the parent */
1685 vlan_link_event(ifp
, p
);
1688 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1689 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1691 error
= (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1694 error
= vlan_unconfig(ifp
);
1697 interface_link_event(ifp
, KEV_DL_LINK_OFF
);
1703 bzero(&vlr
, sizeof vlr
);
1705 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1706 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1708 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1710 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1714 snprintf(vlr
.vlr_parent
, sizeof(vlr
.vlr_parent
),
1715 "%s%d", ifnet_name(p
), ifnet_unit(p
));
1718 user_addr
= proc_is64bit(current_proc())
1719 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1720 error
= copyout(&vlr
, user_addr
, sizeof(vlr
));
1725 * For promiscuous mode, we enable promiscuous mode on
1726 * the parent if we need promiscuous on the VLAN interface.
1728 error
= vlan_set_promisc(ifp
);
1733 error
= vlan_setmulti(ifp
);
1742 vlan_if_free(struct ifnet
* ifp
)
1750 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1761 vlan_event(struct ifnet
* p
, __unused protocol_family_t protocol
,
1762 const struct kev_msg
* event
)
1764 vlan_parent_ref vlp
;
1766 /* Check if the interface we are attached to is being detached */
1767 if (event
->vendor_code
!= KEV_VENDOR_APPLE
1768 || event
->kev_class
!= KEV_NETWORK_CLASS
1769 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
1772 switch (event
->event_code
) {
1773 case KEV_DL_IF_DETACHING
:
1774 case KEV_DL_LINK_OFF
:
1775 case KEV_DL_LINK_ON
:
1781 if ((ifnet_eflags(p
) & IFEF_VLAN
) == 0) {
1786 vlp
= parent_list_lookup(p
);
1792 switch (event
->event_code
) {
1793 case KEV_DL_IF_DETACHING
:
1794 vlan_parent_flags_set_detaching(vlp
);
1795 vlan_parent_remove_all_vlans(vlp
);
1798 case KEV_DL_LINK_OFF
:
1799 case KEV_DL_LINK_ON
:
1800 vlan_parent_link_event(vlp
, event
->event_code
);
1810 interface_link_event(struct ifnet
* ifp
, u_long event_code
)
1813 struct kern_event_msg header
;
1815 char if_name
[IFNAMSIZ
];
1818 event
.header
.total_size
= sizeof(event
);
1819 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
1820 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
1821 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
1822 event
.header
.event_code
= event_code
;
1823 event
.header
.event_data
[0] = ifnet_family(ifp
);
1824 event
.unit
= (u_long
) ifnet_unit(ifp
);
1825 strncpy(event
.if_name
, ifnet_name(ifp
), IFNAMSIZ
);
1826 ifnet_event(ifp
, &event
.header
);
1831 vlan_parent_link_event(vlan_parent_ref vlp
, u_long event_code
)
1835 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
1836 interface_link_event(ifv
->ifv_ifp
, event_code
);
1843 * Function: vlan_attach_protocol
1845 * Attach a DLIL protocol to the interface, using the ETHERTYPE_VLAN
1848 * The ethernet demux actually special cases VLAN to support hardware.
1849 * The demux here isn't used. The demux will return PF_VLAN for the
1850 * appropriate packets and our vlan_input function will be called.
1853 vlan_attach_protocol(struct ifnet
*ifp
)
1856 struct ifnet_attach_proto_param reg
;
1858 bzero(®
, sizeof(reg
));
1859 reg
.input
= vlan_input
;
1860 reg
.event
= vlan_event
;
1861 error
= ifnet_attach_protocol(ifp
, PF_VLAN
, ®
);
1863 printf("vlan_proto_attach(%s%d) ifnet_attach_protocol failed, %d\n",
1864 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
1870 * Function: vlan_detach_protocol
1872 * Detach our DLIL protocol from an interface
1875 vlan_detach_protocol(struct ifnet
*ifp
)
1879 error
= ifnet_detach_protocol(ifp
, PF_VLAN
);
1881 printf("vlan_proto_detach(%s%d) ifnet_detach_protocol failed, %d\n",
1882 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
1889 * DLIL interface family functions
1890 * We use the ethernet plumb functions, since that's all we support.
1891 * If we wanted to handle multiple LAN types (tokenring, etc.), we'd
1892 * call the appropriate routines for that LAN type instead of hard-coding
1896 vlan_attach_inet(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1898 return (ether_attach_inet(ifp
, protocol_family
));
1902 vlan_detach_inet(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1904 ether_detach_inet(ifp
, protocol_family
);
1909 vlan_attach_inet6(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1911 return (ether_attach_inet6(ifp
, protocol_family
));
1915 vlan_detach_inet6(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1917 ether_detach_inet6(ifp
, protocol_family
);
1922 vlan_attach_at(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1924 return (ether_attach_at(ifp
, protocol_family
));
1928 vlan_detach_at(struct ifnet
*ifp
, protocol_family_t protocol_family
)
1930 ether_detach_at(ifp
, protocol_family
);
1933 __private_extern__
int
1934 vlan_family_init(void)
1938 error
= proto_register_plumber(PF_INET
, IFNET_FAMILY_VLAN
,
1939 vlan_attach_inet
, vlan_detach_inet
);
1941 printf("proto_register_plumber failed for AF_INET error=%d\n",
1946 error
= proto_register_plumber(PF_INET6
, IFNET_FAMILY_VLAN
,
1947 vlan_attach_inet6
, vlan_detach_inet6
);
1949 printf("proto_register_plumber failed for AF_INET6 error=%d\n",
1954 error
= proto_register_plumber(PF_APPLETALK
, IFNET_FAMILY_VLAN
,
1955 vlan_attach_at
, vlan_detach_at
);
1957 printf("proto_register_plumber failed for AF_APPLETALK error=%d\n",
1961 vlan_clone_attach();