2 * Copyright (c) 2003 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 <kern/locks.h>
98 #include <netinet/in.h>
99 #include <netinet/if_ether.h>
102 #include <net/if_media.h>
103 #include <net/multicast_list.h>
105 #define IF_MAXUNIT 0x7fff /* historical value */
107 #define VLANNAME "vlan"
109 typedef int (bpf_callback_func
)(struct ifnet
*, struct mbuf
*);
110 typedef int (if_set_bpf_tap_func
)(struct ifnet
*ifp
, int mode
, bpf_callback_func
* func
);
115 static __inline__ lck_grp_t
*
116 my_lck_grp_alloc_init(const char * grp_name
)
119 lck_grp_attr_t
* grp_attrs
;
121 grp_attrs
= lck_grp_attr_alloc_init();
122 grp
= lck_grp_alloc_init(grp_name
, grp_attrs
);
123 lck_grp_attr_free(grp_attrs
);
127 static __inline__ lck_mtx_t
*
128 my_lck_mtx_alloc_init(lck_grp_t
* lck_grp
)
130 lck_attr_t
* lck_attrs
;
133 lck_attrs
= lck_attr_alloc_init();
134 lck_mtx
= lck_mtx_alloc_init(lck_grp
, lck_attrs
);
135 lck_attr_free(lck_attrs
);
139 static lck_mtx_t
* vlan_lck_mtx
;
141 static __inline__
void
144 lck_grp_t
* vlan_lck_grp
;
146 vlan_lck_grp
= my_lck_grp_alloc_init("if_vlan");
147 vlan_lck_mtx
= my_lck_mtx_alloc_init(vlan_lck_grp
);
150 static __inline__
void
151 vlan_assert_lock_held(void)
153 lck_mtx_assert(vlan_lck_mtx
, LCK_MTX_ASSERT_OWNED
);
157 static __inline__
void
158 vlan_assert_lock_not_held(void)
160 lck_mtx_assert(vlan_lck_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
164 static __inline__
void
167 lck_mtx_lock(vlan_lck_mtx
);
171 static __inline__
void
174 lck_mtx_unlock(vlan_lck_mtx
);
179 ** vlan structures, types
182 LIST_HEAD(vlan_parent_list
, vlan_parent
);
184 LIST_HEAD(ifvlan_list
, ifvlan
);
186 typedef struct vlan_parent
{
187 LIST_ENTRY(vlan_parent
) vlp_parent_list
;/* list of parents */
188 struct ifnet
* vlp_ifp
; /* interface */
189 struct ifvlan_list vlp_vlan_list
; /* list of VLAN's */
190 #define VLPF_SUPPORTS_VLAN_MTU 0x1
191 #define VLPF_CHANGE_IN_PROGRESS 0x2
192 #define VLPF_DETACHING 0x4
194 struct ifdevmtu vlp_devmtu
;
195 UInt32 vlp_retain_count
;
196 } vlan_parent
, * vlan_parent_ref
;
199 LIST_ENTRY(ifvlan
) ifv_vlan_list
;
200 char ifv_name
[IFNAMSIZ
]; /* our unique id */
201 struct ifnet
* ifv_ifp
; /* our interface */
202 vlan_parent_ref ifv_vlp
; /* parent information */
204 u_int16_t ifvm_encaplen
;/* encapsulation length */
205 u_int16_t ifvm_mtufudge
;/* MTU fudged by this much */
206 u_int16_t ifvm_proto
; /* encapsulation ethertype */
207 u_int16_t ifvm_tag
; /* tag to apply on packets leaving if */
209 struct multicast_list ifv_multicast
;
210 #define IFVF_PROMISC 0x1 /* promiscuous mode enabled */
211 #define IFVF_DETACHING 0x2 /* interface is detaching */
212 #define IFVF_READY 0x4 /* interface is ready */
214 bpf_packet_func ifv_bpf_input
;
215 bpf_packet_func ifv_bpf_output
;
218 typedef struct ifvlan
* ifvlan_ref
;
220 typedef struct vlan_globals_s
{
221 struct vlan_parent_list parent_list
;
223 } * vlan_globals_ref
;
225 static vlan_globals_ref g_vlan
;
227 #define ifv_tag ifv_mib.ifvm_tag
228 #define ifv_encaplen ifv_mib.ifvm_encaplen
229 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
233 ** vlan_parent_ref vlp_flags in-lines
235 static __inline__
int
236 vlan_parent_flags_supports_vlan_mtu(vlan_parent_ref vlp
)
238 return ((vlp
->vlp_flags
& VLPF_SUPPORTS_VLAN_MTU
) != 0);
241 static __inline__
void
242 vlan_parent_flags_set_supports_vlan_mtu(vlan_parent_ref vlp
)
244 vlp
->vlp_flags
|= VLPF_SUPPORTS_VLAN_MTU
;
248 static __inline__
void
249 vlan_parent_flags_clear_supports_vlan_mtu(vlan_parent_ref vlp
)
251 vlp
->vlp_flags
&= ~VLPF_SUPPORTS_VLAN_MTU
;
255 static __inline__
int
256 vlan_parent_flags_change_in_progress(vlan_parent_ref vlp
)
258 return ((vlp
->vlp_flags
& VLPF_CHANGE_IN_PROGRESS
) != 0);
261 static __inline__
void
262 vlan_parent_flags_set_change_in_progress(vlan_parent_ref vlp
)
264 vlp
->vlp_flags
|= VLPF_CHANGE_IN_PROGRESS
;
268 static __inline__
void
269 vlan_parent_flags_clear_change_in_progress(vlan_parent_ref vlp
)
271 vlp
->vlp_flags
&= ~VLPF_CHANGE_IN_PROGRESS
;
275 static __inline__
int
276 vlan_parent_flags_detaching(struct vlan_parent
* vlp
)
278 return ((vlp
->vlp_flags
& VLPF_DETACHING
) != 0);
281 static __inline__
void
282 vlan_parent_flags_set_detaching(struct vlan_parent
* vlp
)
284 vlp
->vlp_flags
|= VLPF_DETACHING
;
290 ** ifvlan_flags in-lines routines
292 static __inline__
int
293 ifvlan_flags_promisc(ifvlan_ref ifv
)
295 return ((ifv
->ifv_flags
& IFVF_PROMISC
) != 0);
298 static __inline__
void
299 ifvlan_flags_set_promisc(ifvlan_ref ifv
)
301 ifv
->ifv_flags
|= IFVF_PROMISC
;
305 static __inline__
void
306 ifvlan_flags_clear_promisc(ifvlan_ref ifv
)
308 ifv
->ifv_flags
&= ~IFVF_PROMISC
;
312 static __inline__
int
313 ifvlan_flags_ready(ifvlan_ref ifv
)
315 return ((ifv
->ifv_flags
& IFVF_READY
) != 0);
318 static __inline__
void
319 ifvlan_flags_set_ready(ifvlan_ref ifv
)
321 ifv
->ifv_flags
|= IFVF_READY
;
325 static __inline__
void
326 ifvlan_flags_clear_ready(ifvlan_ref ifv
)
328 ifv
->ifv_flags
&= ~IFVF_READY
;
332 static __inline__
int
333 ifvlan_flags_detaching(ifvlan_ref ifv
)
335 return ((ifv
->ifv_flags
& IFVF_DETACHING
) != 0);
338 static __inline__
void
339 ifvlan_flags_set_detaching(ifvlan_ref ifv
)
341 ifv
->ifv_flags
|= IFVF_DETACHING
;
346 SYSCTL_DECL(_net_link
);
347 SYSCTL_NODE(_net_link
, IFT_L2VLAN
, vlan
, CTLFLAG_RW
, 0, "IEEE 802.1Q VLAN");
348 SYSCTL_NODE(_net_link_vlan
, PF_LINK
, link
, CTLFLAG_RW
, 0, "for consistency");
351 #define M_VLAN M_DEVBUF
353 static int vlan_clone_create(struct if_clone
*, int);
354 static void vlan_clone_destroy(struct ifnet
*);
355 static int vlan_input(struct mbuf
*m
, char *frame_header
, struct ifnet
*ifp
,
356 u_long protocol_family
, int sync_ok
);
357 static int vlan_output(struct ifnet
*ifp
, struct mbuf
*m
);
358 static int vlan_ioctl(ifnet_t ifp
, u_int32_t cmd
, void * addr
);
359 static int vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
,
360 bpf_packet_func func
);
361 static int vlan_attach_protocol(struct ifnet
*ifp
);
362 static int vlan_detach_protocol(struct ifnet
*ifp
);
363 static int vlan_setmulti(struct ifnet
*ifp
);
364 static int vlan_unconfig(struct ifnet
*ifp
);
365 static int vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
);
366 static void vlan_if_free(struct ifnet
* ifp
);
367 static void vlan_remove(ifvlan_ref ifv
);
368 static void vlan_if_detach(struct ifnet
* ifp
);
369 static int vlan_new_mtu(struct ifnet
* ifp
, int mtu
);
371 static struct if_clone vlan_cloner
= IF_CLONE_INITIALIZER(VLANNAME
,
376 static void interface_link_event(struct ifnet
* ifp
, u_long event_code
);
377 static void vlan_parent_link_event(vlan_parent_ref vlp
,
379 extern int dlil_input_packet(struct ifnet
*ifp
, struct mbuf
*m
, char *frame_header
);
382 vlan_globals_init(void)
386 vlan_assert_lock_not_held();
388 if (g_vlan
!= NULL
) {
391 v
= _MALLOC(sizeof(*v
), M_VLAN
, M_WAITOK
);
393 LIST_INIT(&v
->parent_list
);
397 if (g_vlan
!= NULL
) {
413 siocgifdevmtu(struct ifnet
* ifp
, struct ifdevmtu
* ifdm_p
)
418 bzero(&ifr
, sizeof(ifr
));
419 error
= dlil_ioctl(0, ifp
, SIOCGIFDEVMTU
, (caddr_t
)&ifr
);
421 *ifdm_p
= ifr
.ifr_devmtu
;
427 siocsifaltmtu(struct ifnet
* ifp
, int mtu
)
431 bzero(&ifr
, sizeof(ifr
));
433 return (dlil_ioctl(0, ifp
, SIOCSIFALTMTU
, (caddr_t
)&ifr
));
436 static __inline__
void
437 vlan_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
438 bpf_packet_func func
)
446 static __inline__
void
447 vlan_bpf_input(struct ifnet
* ifp
, struct mbuf
* m
,
448 bpf_packet_func func
, char * frame_header
,
449 int frame_header_len
, int encap_len
)
453 /* present the right header to bpf */
454 bcopy(frame_header
, frame_header
+ encap_len
, frame_header_len
);
456 m
->m_data
-= frame_header_len
;
457 m
->m_len
+= frame_header_len
;
459 m
->m_data
+= frame_header_len
;
460 m
->m_len
-= frame_header_len
;
462 /* restore the header */
463 bcopy(frame_header
+ encap_len
, frame_header
, frame_header_len
);
469 static struct ifaddr
*
470 ifaddr_byindex(int i
)
472 if (i
> if_index
|| i
== 0) {
475 return (ifnet_addrs
[i
- 1]);
479 ** vlan_parent synchronization routines
481 static __inline__
void
482 vlan_parent_retain(vlan_parent_ref vlp
)
484 OSIncrementAtomic(&vlp
->vlp_retain_count
);
487 static __inline__
void
488 vlan_parent_release(vlan_parent_ref vlp
)
490 UInt32 old_retain_count
;
492 old_retain_count
= OSDecrementAtomic(&vlp
->vlp_retain_count
);
493 switch (old_retain_count
) {
495 panic("vlan_parent_release: retain count is 0\n");
498 if (g_vlan
->verbose
) {
499 struct ifnet
* ifp
= vlp
->vlp_ifp
;
500 printf("vlan_parent_release(%s%d)\n", ifp
->if_name
,
512 * Function: vlan_parent_wait
514 * Allows a single thread to gain exclusive access to the vlan_parent
515 * data structure. Some operations take a long time to complete,
516 * and some have side-effects that we can't predict. Holding the
517 * vlan_lock() across such operations is not possible.
520 * Before calling, you must be holding the vlan_lock and have taken
521 * a reference on the vlan_parent_ref.
524 vlan_parent_wait(vlan_parent_ref vlp
, const char * msg
)
528 /* other add/remove/multicast-change in progress */
529 while (vlan_parent_flags_change_in_progress(vlp
)) {
530 if (g_vlan
->verbose
) {
531 struct ifnet
* ifp
= vlp
->vlp_ifp
;
533 printf("%s%d: %s msleep\n", ifp
->if_name
, ifp
->if_unit
, msg
);
536 (void)msleep(vlp
, vlan_lck_mtx
, PZERO
, msg
, 0);
538 /* prevent other vlan parent remove/add from taking place */
539 vlan_parent_flags_set_change_in_progress(vlp
);
540 if (g_vlan
->verbose
&& waited
) {
541 struct ifnet
* ifp
= vlp
->vlp_ifp
;
543 printf("%s: %s woke up\n", ifp
->if_name
, ifp
->if_unit
, msg
);
549 * Function: vlan_parent_signal
551 * Allows the thread that previously invoked vlan_parent_wait() to
552 * give up exclusive access to the vlan_parent data structure, and wake up
553 * any other threads waiting to access
555 * Before calling, you must be holding the vlan_lock and have taken
556 * a reference on the vlan_parent_ref.
559 vlan_parent_signal(vlan_parent_ref vlp
, const char * msg
)
561 vlan_parent_flags_clear_change_in_progress(vlp
);
562 wakeup((caddr_t
)vlp
);
563 if (g_vlan
->verbose
) {
564 struct ifnet
* ifp
= vlp
->vlp_ifp
;
566 printf("%s%d: %s wakeup\n", ifp
->if_name
, ifp
->if_unit
, msg
);
573 * Program our multicast filter. What we're actually doing is
574 * programming the multicast filter of the parent. This has the
575 * side effect of causing the parent interface to receive multicast
576 * traffic that it doesn't really want, which ends up being discarded
577 * later by the upper protocol layers. Unfortunately, there's no way
578 * to avoid this: there really is only one physical interface.
581 vlan_setmulti(struct ifnet
* ifp
)
589 ifv
= (ifvlan_ref
)ifp
->if_private
;
590 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
595 /* no parent, no need to program the multicast filter */
598 if (vlan_parent_flags_detaching(vlp
)) {
601 vlan_parent_retain(vlp
);
602 vlan_parent_wait(vlp
, "vlan_setmulti");
604 /* check again, things could have changed */
605 ifv
= (ifvlan_ref
)ifp
->if_private
;
606 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
609 if (ifv
->ifv_vlp
!= vlp
) {
610 /* vlan parent changed */
614 /* no parent, no need to program the multicast filter */
620 /* update parent interface with our multicast addresses */
621 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
626 vlan_parent_signal(vlp
, "vlan_setmulti");
634 ** vlan_parent list manipulation/lookup routines
636 static vlan_parent_ref
637 parent_list_lookup(struct ifnet
* p
)
641 LIST_FOREACH(vlp
, &g_vlan
->parent_list
, vlp_parent_list
) {
642 if (vlp
->vlp_ifp
== p
) {
650 vlan_parent_lookup_tag(vlan_parent_ref vlp
, int tag
)
654 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
655 if (tag
== ifv
->ifv_tag
) {
663 vlan_lookup_parent_and_tag(struct ifnet
* p
, int tag
)
667 vlp
= parent_list_lookup(p
);
669 return (vlan_parent_lookup_tag(vlp
, tag
));
675 vlan_parent_find_max_mtu(vlan_parent_ref vlp
, ifvlan_ref exclude_ifv
)
680 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
683 if (exclude_ifv
== ifv
) {
686 req_mtu
= ifv
->ifv_ifp
->if_mtu
+ ifv
->ifv_mtufudge
;
687 if (req_mtu
> max_mtu
) {
695 * Function: vlan_parent_create
697 * Create a vlan_parent structure to hold the VLAN's for the given
698 * interface. Add it to the list of VLAN parents.
701 vlan_parent_create(struct ifnet
* p
, vlan_parent_ref
* ret_vlp
)
707 vlp
= _MALLOC(sizeof(*vlp
), M_VLAN
, M_WAITOK
);
711 bzero(vlp
, sizeof(*vlp
));
712 error
= siocgifdevmtu(p
, &vlp
->vlp_devmtu
);
714 printf("vlan_parent_create (%s%d): siocgifdevmtu failed, %d\n",
715 p
->if_name
, p
->if_unit
, error
);
719 LIST_INIT(&vlp
->vlp_vlan_list
);
721 vlan_parent_retain(vlp
);
723 & (IF_HWASSIST_VLAN_MTU
| IF_HWASSIST_VLAN_TAGGING
)) {
724 vlan_parent_flags_set_supports_vlan_mtu(vlp
);
731 vlan_parent_remove_all_vlans(vlan_parent_ref vlp
)
736 vlan_assert_lock_held();
738 while ((ifv
= LIST_FIRST(&vlp
->vlp_vlan_list
)) != NULL
) {
741 vlan_if_detach(ifv
->ifv_ifp
);
745 /* the vlan parent has no more VLAN's */
747 ifnet_lock_exclusive(p
);
748 p
->if_eflags
&= ~IFEF_VLAN
;
750 LIST_REMOVE(vlp
, vlp_parent_list
);
752 vlan_parent_release(vlp
);
758 static __inline__
int
759 vlan_parent_no_vlans(vlan_parent_ref vlp
)
761 return (LIST_EMPTY(&vlp
->vlp_vlan_list
));
765 vlan_parent_add_vlan(vlan_parent_ref vlp
, ifvlan_ref ifv
, int tag
)
767 LIST_INSERT_HEAD(&vlp
->vlp_vlan_list
, ifv
, ifv_vlan_list
);
774 vlan_parent_remove_vlan(__unused vlan_parent_ref vlp
, ifvlan_ref ifv
)
777 LIST_REMOVE(ifv
, ifv_vlan_list
);
782 vlan_clone_attach(void)
784 if_clone_attach(&vlan_cloner
);
790 vlan_clone_create(struct if_clone
*ifc
, int unit
)
796 error
= vlan_globals_init();
800 ifv
= _MALLOC(sizeof(struct ifvlan
), M_VLAN
, M_WAITOK
);
801 bzero(ifv
, sizeof(struct ifvlan
));
802 multicast_list_init(&ifv
->ifv_multicast
);
804 /* use the interface name as the unique id for ifp recycle */
805 if ((unsigned int)snprintf(ifv
->ifv_name
, sizeof(ifv
->ifv_name
), "%s%d",
806 ifc
->ifc_name
, unit
) >= sizeof(ifv
->ifv_name
)) {
810 error
= dlil_if_acquire(APPLE_IF_FAM_VLAN
,
812 strlen(ifv
->ifv_name
),
818 ifp
->if_name
= ifc
->ifc_name
;
820 ifp
->if_family
= APPLE_IF_FAM_VLAN
;
823 /* NB: flags are not set here */
824 ifp
->if_linkmib
= &ifv
->ifv_mib
;
825 ifp
->if_linkmiblen
= sizeof ifv
->ifv_mib
;
826 /* NB: mtu is not set here */
829 ifp
->if_ioctl
= vlan_ioctl
;
830 ifp
->if_set_bpf_tap
= vlan_set_bpf_tap
;
831 ifp
->if_free
= vlan_if_free
;
832 ifp
->if_output
= vlan_output
;
833 ifp
->if_hwassist
= 0;
834 ifp
->if_addrlen
= ETHER_ADDR_LEN
; /* XXX ethernet specific */
835 ifp
->if_baudrate
= 0;
836 ifp
->if_type
= IFT_L2VLAN
;
837 ifp
->if_hdrlen
= ETHER_VLAN_ENCAP_LEN
;
839 /* XXX ethernet specific */
840 ifp
->if_broadcast
.length
= ETHER_ADDR_LEN
;
841 bcopy(etherbroadcastaddr
, ifp
->if_broadcast
.u
.buffer
, ETHER_ADDR_LEN
);
843 error
= dlil_if_attach(ifp
);
845 dlil_if_release(ifp
);
849 ifp
->if_private
= ifv
;
852 /* attach as ethernet */
853 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
858 vlan_remove(ifvlan_ref ifv
)
860 vlan_assert_lock_held();
861 ifvlan_flags_set_detaching(ifv
);
862 vlan_unconfig(ifv
->ifv_ifp
);
867 vlan_if_detach(struct ifnet
* ifp
)
869 if (dlil_if_detach(ifp
) != DLIL_WAIT_FOR_FREE
) {
876 vlan_clone_destroy(struct ifnet
*ifp
)
881 ifv
= ifp
->if_private
;
882 if (ifv
== NULL
|| ifp
->if_type
!= 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
= ifp
->if_private
;
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
)ifp
->if_private
;
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
= (p
->if_hwassist
& 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 ((p
->if_flags
& (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", ifp
->if_name
,
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", ifp
->if_name
,
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 dlil_output(p
, 0, m
, NULL
, NULL
, 1);
1023 vlan_input(struct mbuf
* m
, char * frame_header
, struct ifnet
* p
,
1024 __unused u_long protocol_family
, __unused
int sync_ok
)
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 (p
->if_type
) {
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 ((p
->if_eflags
& 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 || (ifp
->if_flags
& 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 (void)ifnet_stat_increment_in(ifp
, 1,
1108 m
->m_pkthdr
.len
+ ETHER_HDR_LEN
, 0);
1109 vlan_bpf_input(ifp
, m
, bpf_func
, frame_header
, ETHER_HDR_LEN
,
1110 soft_vlan
? ETHER_VLAN_ENCAP_LEN
: 0);
1111 /* We found a vlan interface, inject on that interface. */
1112 dlil_input_packet(ifp
, m
, frame_header
);
1114 /* Send priority-tagged packet up through the parent */
1115 dlil_input_packet(p
, m
, frame_header
);
1120 #define VLAN_CONFIG_PROGRESS_VLP_RETAINED 0x1
1121 #define VLAN_CONFIG_PROGRESS_IN_LIST 0x2
1124 vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
)
1128 ifvlan_ref ifv
= NULL
;
1129 struct ifaddr
* ifa1
;
1130 struct ifaddr
* ifa2
;
1131 vlan_parent_ref new_vlp
= NULL
;
1132 int need_vlp_release
= 0;
1133 u_int32_t progress
= 0;
1134 struct sockaddr_dl
*sdl1
;
1135 struct sockaddr_dl
*sdl2
;
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
)ifp
->if_private
;
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
)ifp
->if_private
;
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 ((p
->if_eflags
& 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 p
->if_eflags
|= IFEF_VLAN
;
1208 /* attach our VLAN "protocol" to the interface */
1209 error
= vlan_attach_protocol(p
);
1214 /* mark the parent interface up */
1215 ifnet_lock_exclusive(p
);
1216 p
->if_flags
|= IFF_UP
;
1218 (void)dlil_ioctl(0, p
, 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 /* no failures past this point */
1234 ifv
->ifv_encaplen
= ETHER_VLAN_ENCAP_LEN
;
1236 if (vlan_parent_flags_supports_vlan_mtu(vlp
)) {
1237 ifv
->ifv_mtufudge
= 0;
1240 * Fudge the MTU by the encapsulation size. This
1241 * makes us incompatible with strictly compliant
1242 * 802.1Q implementations, but allows us to use
1243 * the feature with other NetBSD implementations,
1244 * which might still be useful.
1246 ifv
->ifv_mtufudge
= ifv
->ifv_encaplen
;
1248 ifp
->if_mtu
= ETHERMTU
- ifv
->ifv_mtufudge
;
1251 * Copy only a selected subset of flags from the parent.
1252 * Other flags are none of our business.
1254 ifp
->if_flags
|= (p
->if_flags
&
1255 (IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
));
1257 * If the parent interface can do hardware-assisted
1258 * VLAN encapsulation, then propagate its hardware-
1259 * assisted checksumming flags.
1261 if (p
->if_hwassist
& IF_HWASSIST_VLAN_TAGGING
) {
1262 ifp
->if_hwassist
|= IF_HWASSIST_CSUM_FLAGS(p
->if_hwassist
);
1265 /* set our ethernet address to that of the parent */
1266 ifa1
= ifaddr_byindex(ifp
->if_index
);
1267 ifa2
= ifaddr_byindex(p
->if_index
);
1268 sdl1
= (struct sockaddr_dl
*)ifa1
->ifa_addr
;
1269 sdl2
= (struct sockaddr_dl
*)ifa2
->ifa_addr
;
1270 sdl1
->sdl_type
= IFT_ETHER
;
1271 sdl1
->sdl_alen
= ETHER_ADDR_LEN
;
1272 bcopy(LLADDR(sdl2
), LLADDR(sdl1
), ETHER_ADDR_LEN
);
1274 ifp
->if_flags
|= IFF_RUNNING
;
1275 ifvlan_flags_set_ready(ifv
);
1276 vlan_parent_signal(vlp
, "vlan_config");
1278 if (new_vlp
!= vlp
) {
1279 /* throw it away, it wasn't needed */
1280 vlan_parent_release(new_vlp
);
1285 vlan_assert_lock_held();
1286 vlan_parent_signal(vlp
, "vlan_config");
1289 if ((progress
& VLAN_CONFIG_PROGRESS_IN_LIST
) != 0) {
1290 vlan_parent_remove_vlan(vlp
, ifv
);
1292 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1293 /* the vlan parent has no more VLAN's */
1294 ifnet_lock_exclusive(p
);
1295 p
->if_eflags
&= ~IFEF_VLAN
;
1297 LIST_REMOVE(vlp
, vlp_parent_list
);
1298 /* release outside of the lock below */
1299 need_vlp_release
= 1;
1303 if ((progress
& VLAN_CONFIG_PROGRESS_VLP_RETAINED
) != 0) {
1304 vlan_parent_release(vlp
);
1306 if (need_vlp_release
) {
1307 vlan_parent_release(vlp
);
1309 if (new_vlp
!= vlp
) {
1310 vlan_parent_release(new_vlp
);
1316 vlan_link_event(struct ifnet
* ifp
, struct ifnet
* p
)
1318 struct ifmediareq ifmr
;
1320 /* generate a link event based on the state of the underlying interface */
1321 bzero(&ifmr
, sizeof(ifmr
));
1322 snprintf(ifmr
.ifm_name
, sizeof(ifmr
.ifm_name
),
1323 "%s%d", p
->if_name
, p
->if_unit
);
1324 if ((*p
->if_ioctl
)(p
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) == 0
1325 && ifmr
.ifm_count
> 0 && ifmr
.ifm_status
& IFM_AVALID
) {
1328 event
= (ifmr
.ifm_status
& IFM_ACTIVE
)
1329 ? KEV_DL_LINK_ON
: KEV_DL_LINK_OFF
;
1330 interface_link_event(ifp
, event
);
1336 vlan_unconfig(struct ifnet
* ifp
)
1339 struct ifaddr
* ifa
;
1342 int need_vlp_release
= 0;
1344 struct sockaddr_dl
*sdl
;
1345 vlan_parent_ref vlp
;
1347 vlan_assert_lock_held();
1348 ifv
= (ifvlan_ref
)ifp
->if_private
;
1356 vlan_parent_retain(vlp
);
1357 vlan_parent_wait(vlp
, "vlan_unconfig");
1359 /* check again because another thread could be in vlan_unconfig */
1360 ifv
= (ifvlan_ref
)ifp
->if_private
;
1364 if (ifv
->ifv_vlp
!= vlp
) {
1365 /* vlan parent changed */
1371 /* remember whether we're the last VLAN on the parent */
1372 if (LIST_NEXT(LIST_FIRST(&vlp
->vlp_vlan_list
), ifv_vlan_list
) == NULL
) {
1373 if (g_vlan
->verbose
) {
1374 printf("vlan_unconfig: last vlan on %s%d\n",
1375 p
->if_name
, p
->if_unit
);
1380 /* back-out any effect our mtu might have had on the parent */
1381 (void)vlan_new_mtu(ifp
, ETHERMTU
- ifv
->ifv_mtufudge
);
1385 /* detach VLAN "protocol" */
1387 (void)vlan_detach_protocol(p
);
1390 /* un-join multicast on parent interface */
1391 (void)multicast_list_remove(&ifv
->ifv_multicast
);
1395 /* Disconnect from parent. */
1396 vlan_parent_remove_vlan(vlp
, ifv
);
1398 /* return to the state we were in before SIFVLAN */
1400 ifp
->if_flags
&= ~(IFF_BROADCAST
| IFF_MULTICAST
1401 | IFF_SIMPLEX
| IFF_RUNNING
);
1402 ifp
->if_hwassist
= 0;
1404 ifv
->ifv_mtufudge
= 0;
1406 /* Clear our MAC address. */
1407 ifa
= ifaddr_byindex(ifp
->if_index
);
1408 sdl
= (struct sockaddr_dl
*)(ifa
->ifa_addr
);
1409 sdl
->sdl_type
= IFT_L2VLAN
;
1411 bzero(LLADDR(sdl
), ETHER_ADDR_LEN
);
1413 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1414 /* the vlan parent has no more VLAN's */
1415 ifnet_lock_exclusive(p
);
1416 p
->if_eflags
&= ~IFEF_VLAN
;
1418 LIST_REMOVE(vlp
, vlp_parent_list
);
1419 /* release outside of the lock below */
1424 vlan_parent_signal(vlp
, "vlan_unconfig");
1426 vlan_parent_release(vlp
); /* one because we waited */
1428 while (need_vlp_release
--) {
1429 vlan_parent_release(vlp
);
1436 vlan_set_promisc(struct ifnet
* ifp
)
1440 vlan_parent_ref vlp
;
1443 ifv
= (ifvlan_ref
)ifp
->if_private
;
1444 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1445 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1453 if ((ifp
->if_flags
& IFF_PROMISC
) != 0) {
1454 if (!ifvlan_flags_promisc(ifv
)) {
1455 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 1);
1457 ifvlan_flags_set_promisc(ifv
);
1461 if (ifvlan_flags_promisc(ifv
)) {
1462 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 0);
1464 ifvlan_flags_clear_promisc(ifv
);
1474 vlan_new_mtu(struct ifnet
* ifp
, int mtu
)
1476 struct ifdevmtu
* devmtu_p
;
1482 vlan_parent_ref vlp
;
1484 vlan_assert_lock_held();
1485 ifv
= (ifvlan_ref
)ifp
->if_private
;
1487 devmtu_p
= &vlp
->vlp_devmtu
;
1488 req_mtu
= mtu
+ ifv
->ifv_mtufudge
;
1489 if (req_mtu
> devmtu_p
->ifdm_max
|| req_mtu
< devmtu_p
->ifdm_min
) {
1492 max_mtu
= vlan_parent_find_max_mtu(vlp
, ifv
);
1493 if (req_mtu
> max_mtu
) {
1496 else if (max_mtu
< devmtu_p
->ifdm_current
) {
1500 struct ifnet
* p
= vlp
->vlp_ifp
;
1502 error
= siocsifaltmtu(p
, new_mtu
);
1507 devmtu_p
->ifdm_current
= new_mtu
;
1515 vlan_set_mtu(struct ifnet
* ifp
, int mtu
)
1519 vlan_parent_ref vlp
;
1521 if (mtu
< IF_MINMTU
) {
1525 ifv
= (ifvlan_ref
)ifp
->if_private
;
1526 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1528 return ((ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
);
1531 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1538 vlan_parent_retain(vlp
);
1539 vlan_parent_wait(vlp
, "vlan_set_mtu");
1541 /* check again, something might have changed */
1542 ifv
= (ifvlan_ref
)ifp
->if_private
;
1543 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1544 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1547 if (ifv
->ifv_vlp
!= vlp
) {
1548 /* vlan parent changed */
1551 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1557 error
= vlan_new_mtu(ifp
, mtu
);
1560 vlan_parent_signal(vlp
, "vlan_set_mtu");
1562 vlan_parent_release(vlp
);
1568 vlan_ioctl(ifnet_t ifp
, u_int32_t cmd
, void * data
)
1570 struct ifdevmtu
* devmtu_p
;
1572 struct ifaddr
* ifa
;
1573 struct ifmediareq64
* ifmr
;
1578 user_addr_t user_addr
;
1579 vlan_parent_ref vlp
;
1582 if (ifp
->if_type
!= IFT_L2VLAN
) {
1583 return (EOPNOTSUPP
);
1585 ifr
= (struct ifreq
*)data
;
1586 ifa
= (struct ifaddr
*)data
;
1590 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
1593 case SIOCGIFMEDIA64
:
1596 ifv
= (ifvlan_ref
)ifp
->if_private
;
1597 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1599 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1601 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1603 ifmr
= (struct ifmediareq64
*)data
;
1604 user_addr
= (cmd
== SIOCGIFMEDIA64
)
1605 ? ifmr
->ifm_ifmu
.ifmu_ulist64
1606 : CAST_USER_ADDR_T(ifmr
->ifm_ifmu
.ifmu_ulist32
);
1608 struct ifmediareq64 p_ifmr
;
1610 bzero(&p_ifmr
, sizeof(p_ifmr
));
1611 error
= dlil_ioctl(0, p
, SIOCGIFMEDIA
, (caddr_t
)&p_ifmr
);
1613 ifmr
->ifm_active
= p_ifmr
.ifm_active
;
1614 ifmr
->ifm_current
= p_ifmr
.ifm_current
;
1615 ifmr
->ifm_mask
= p_ifmr
.ifm_mask
;
1616 ifmr
->ifm_status
= p_ifmr
.ifm_status
;
1617 ifmr
->ifm_count
= p_ifmr
.ifm_count
;
1618 /* Limit the result to the parent's current config. */
1619 if (ifmr
->ifm_count
>= 1 && user_addr
!= USER_ADDR_NULL
) {
1620 ifmr
->ifm_count
= 1;
1621 error
= copyout(&ifmr
->ifm_current
, user_addr
,
1626 ifmr
->ifm_active
= ifmr
->ifm_current
= IFM_NONE
;
1628 ifmr
->ifm_status
= IFM_AVALID
;
1629 ifmr
->ifm_count
= 1;
1630 if (user_addr
!= USER_ADDR_NULL
) {
1631 error
= copyout(&ifmr
->ifm_current
, user_addr
, sizeof(int));
1642 ifv
= (ifvlan_ref
)ifp
->if_private
;
1643 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1645 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1649 int min_mtu
= vlp
->vlp_devmtu
.ifdm_min
- ifv
->ifv_mtufudge
;
1650 devmtu_p
= &ifr
->ifr_devmtu
;
1651 devmtu_p
->ifdm_current
= ifp
->if_mtu
;
1652 devmtu_p
->ifdm_min
= max(min_mtu
, IF_MINMTU
);
1653 devmtu_p
->ifdm_max
= vlp
->vlp_devmtu
.ifdm_max
- ifv
->ifv_mtufudge
;
1656 devmtu_p
= &ifr
->ifr_devmtu
;
1657 devmtu_p
->ifdm_current
= 0;
1658 devmtu_p
->ifdm_min
= 0;
1659 devmtu_p
->ifdm_max
= 0;
1665 error
= vlan_set_mtu(ifp
, ifr
->ifr_mtu
);
1669 user_addr
= proc_is64bit(current_proc())
1670 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1671 error
= copyin(user_addr
, &vlr
, sizeof(vlr
));
1676 if (vlr
.vlr_parent
[0] != '\0') {
1677 if (vlr
.vlr_tag
& ~EVL_VLID_MASK
) {
1679 * Don't let the caller set up a VLAN tag with
1680 * anything except VLID bits.
1685 p
= ifunit(vlr
.vlr_parent
);
1690 /* can't do VLAN over anything but ethernet or ethernet aggregate */
1691 if (p
->if_type
!= IFT_ETHER
&& p
->if_type
!= IFT_IEEE8023ADLAG
) {
1692 error
= EPROTONOSUPPORT
;
1695 error
= vlan_config(ifp
, p
, vlr
.vlr_tag
);
1700 /* Update promiscuous mode, if necessary. */
1701 (void)vlan_set_promisc(ifp
);
1703 /* generate a link event based on the state of the parent */
1704 vlan_link_event(ifp
, p
);
1707 ifv
= (ifvlan_ref
)ifp
->if_private
;
1708 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1710 error
= (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1713 error
= vlan_unconfig(ifp
);
1716 interface_link_event(ifp
, KEV_DL_LINK_OFF
);
1722 bzero(&vlr
, sizeof vlr
);
1724 ifv
= (ifvlan_ref
)ifp
->if_private
;
1725 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1727 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1729 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1733 snprintf(vlr
.vlr_parent
, sizeof(vlr
.vlr_parent
),
1734 "%s%d", p
->if_name
, p
->if_unit
);
1737 user_addr
= proc_is64bit(current_proc())
1738 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1739 error
= copyout(&vlr
, user_addr
, sizeof(vlr
));
1744 * For promiscuous mode, we enable promiscuous mode on
1745 * the parent if we need promiscuous on the VLAN interface.
1747 error
= vlan_set_promisc(ifp
);
1752 error
= vlan_setmulti(ifp
);
1761 vlan_if_free(struct ifnet
* ifp
)
1769 ifv
= (ifvlan_ref
)ifp
->if_private
;
1774 ifp
->if_private
= NULL
;
1776 dlil_if_release(ifp
);
1781 vlan_event(struct ifnet
* p
, struct kev_msg
* event
)
1783 vlan_parent_ref vlp
;
1785 /* Check if the interface we are attached to is being detached */
1786 if (event
->vendor_code
!= KEV_VENDOR_APPLE
1787 || event
->kev_class
!= KEV_NETWORK_CLASS
1788 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
1791 switch (event
->event_code
) {
1792 case KEV_DL_IF_DETACHING
:
1793 case KEV_DL_LINK_OFF
:
1794 case KEV_DL_LINK_ON
:
1800 if ((p
->if_eflags
& IFEF_VLAN
) == 0) {
1805 vlp
= parent_list_lookup(p
);
1811 switch (event
->event_code
) {
1812 case KEV_DL_IF_DETACHING
:
1813 vlan_parent_flags_set_detaching(vlp
);
1814 vlan_parent_remove_all_vlans(vlp
);
1817 case KEV_DL_LINK_OFF
:
1818 case KEV_DL_LINK_ON
:
1819 vlan_parent_link_event(vlp
, event
->event_code
);
1829 interface_link_event(struct ifnet
* ifp
, u_long event_code
)
1832 struct kern_event_msg header
;
1834 char if_name
[IFNAMSIZ
];
1837 event
.header
.total_size
= sizeof(event
);
1838 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
1839 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
1840 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
1841 event
.header
.event_code
= event_code
;
1842 event
.header
.event_data
[0] = ifp
->if_family
;
1843 event
.unit
= (u_long
) ifp
->if_unit
;
1844 strncpy(event
.if_name
, ifp
->if_name
, IFNAMSIZ
);
1845 dlil_event(ifp
, &event
.header
);
1850 vlan_parent_link_event(vlan_parent_ref vlp
, u_long event_code
)
1854 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
1855 interface_link_event(ifv
->ifv_ifp
, event_code
);
1862 * Function: vlan_attach_protocol
1864 * Attach a DLIL protocol to the interface, using the ETHERTYPE_VLAN
1867 * The ethernet demux actually special cases VLAN to support hardware.
1868 * The demux here isn't used. The demux will return PF_VLAN for the
1869 * appropriate packets and our vlan_input function will be called.
1872 vlan_attach_protocol(struct ifnet
*ifp
)
1875 struct dlil_proto_reg_str reg
;
1877 bzero(®
, sizeof(reg
));
1878 TAILQ_INIT(®
.demux_desc_head
);
1879 reg
.interface_family
= ifp
->if_family
;
1880 reg
.unit_number
= ifp
->if_unit
;
1881 reg
.input
= vlan_input
;
1882 reg
.event
= vlan_event
;
1883 reg
.protocol_family
= PF_VLAN
;
1884 error
= dlil_attach_protocol(®
);
1886 printf("vlan_proto_attach(%s%d) dlil_attach_protocol failed, %d\n",
1887 ifp
->if_name
, ifp
->if_unit
, error
);
1893 * Function: vlan_detach_protocol
1895 * Detach our DLIL protocol from an interface
1898 vlan_detach_protocol(struct ifnet
*ifp
)
1902 error
= dlil_detach_protocol(ifp
, PF_VLAN
);
1904 printf("vlan_proto_detach(%s%d) dlil_detach_protocol failed, %d\n",
1905 ifp
->if_name
, ifp
->if_unit
, error
);
1912 * DLIL interface family functions
1913 * We use the ethernet dlil functions, since that's all we support.
1914 * If we wanted to handle multiple LAN types (tokenring, etc.), we'd
1915 * call the appropriate routines for that LAN type instead of hard-coding
1918 extern int ether_add_if(struct ifnet
*ifp
);
1919 extern int ether_del_if(struct ifnet
*ifp
);
1920 extern int ether_init_if(struct ifnet
*ifp
);
1921 extern int ether_add_proto_old(struct ifnet
*ifp
, u_long protocol_family
,
1922 struct ddesc_head_str
*desc_head
);
1924 extern int ether_attach_inet(struct ifnet
*ifp
, u_long protocol_family
);
1925 extern int ether_detach_inet(struct ifnet
*ifp
, u_long protocol_family
);
1926 extern int ether_attach_inet6(struct ifnet
*ifp
, u_long protocol_family
);
1927 extern int ether_detach_inet6(struct ifnet
*ifp
, u_long protocol_family
);
1930 vlan_attach_inet(struct ifnet
*ifp
, u_long protocol_family
)
1932 return (ether_attach_inet(ifp
, protocol_family
));
1936 vlan_detach_inet(struct ifnet
*ifp
, u_long protocol_family
)
1938 return (ether_detach_inet(ifp
, protocol_family
));
1942 vlan_attach_inet6(struct ifnet
*ifp
, u_long protocol_family
)
1944 return (ether_attach_inet6(ifp
, protocol_family
));
1948 vlan_detach_inet6(struct ifnet
*ifp
, u_long protocol_family
)
1950 return (ether_detach_inet6(ifp
, protocol_family
));
1954 vlan_add_if(struct ifnet
*ifp
)
1956 return (ether_add_if(ifp
));
1960 vlan_del_if(struct ifnet
*ifp
)
1962 return (ether_del_if(ifp
));
1966 __private_extern__
int
1967 vlan_family_init(void)
1970 struct dlil_ifmod_reg_str ifmod_reg
;
1972 bzero(&ifmod_reg
, sizeof(ifmod_reg
));
1973 ifmod_reg
.add_if
= vlan_add_if
;
1974 ifmod_reg
.del_if
= vlan_del_if
;
1975 ifmod_reg
.init_if
= NULL
;
1976 ifmod_reg
.add_proto
= ether_add_proto_old
;
1977 ifmod_reg
.del_proto
= ether_del_proto
;
1978 ifmod_reg
.ifmod_ioctl
= ether_ioctl
;
1979 ifmod_reg
.shutdown
= NULL
;
1981 if (dlil_reg_if_modules(APPLE_IF_FAM_VLAN
, &ifmod_reg
)) {
1982 printf("WARNING: vlan_family_init -- "
1983 "Can't register if family modules\n");
1988 error
= dlil_reg_proto_module(PF_INET
, APPLE_IF_FAM_VLAN
,
1989 vlan_attach_inet
, vlan_detach_inet
);
1991 printf("dlil_reg_proto_module failed for AF_INET error=%d\n",
1995 error
= dlil_reg_proto_module(PF_INET6
, APPLE_IF_FAM_VLAN
,
1996 vlan_attach_inet6
, vlan_detach_inet6
);
1998 printf("dlil_reg_proto_module failed for AF_INET6 error=%d\n",
2002 vlan_clone_attach();