2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright 1998 Massachusetts Institute of Technology
33 * Permission to use, copy, modify, and distribute this software and
34 * its documentation for any purpose and without fee is hereby
35 * granted, provided that both the above copyright notice and this
36 * permission notice appear in all copies, that both the above
37 * copyright notice and this permission notice appear in all
38 * supporting documentation, and that the name of M.I.T. not be used
39 * in advertising or publicity pertaining to distribution of the
40 * software without specific, written prior permission. M.I.T. makes
41 * no representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied
45 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
46 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
47 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
48 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
49 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * $FreeBSD: src/sys/net/if_vlan.c,v 1.54 2003/10/31 18:32:08 brooks Exp $
62 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
63 * Might be extended some day to also handle IEEE 802.1p priority
64 * tagging. This is sort of sneaky in the implementation, since
65 * we need to pretend to be enough of an Ethernet implementation
66 * to make arp work. The way we do this is by telling everyone
67 * that we are an Ethernet, and then catch the packets that
68 * ether_output() left on our output queue when it calls
69 * if_start(), rewrite them for use by the real outgoing interface,
70 * and ask it to send them.
74 #include <sys/param.h>
75 #include <sys/kernel.h>
76 #include <sys/malloc.h>
78 #include <sys/queue.h>
79 #include <sys/socket.h>
80 #include <sys/sockio.h>
81 #include <sys/sysctl.h>
82 #include <sys/systm.h>
83 #include <sys/kern_event.h>
86 #include <net/ethernet.h>
88 #include <net/if_arp.h>
89 #include <net/if_dl.h>
90 #include <net/if_ether.h>
91 #include <net/if_types.h>
92 #include <net/if_vlan_var.h>
93 #include <libkern/OSAtomic.h>
97 #include <kern/locks.h>
100 #include <netinet/in.h>
101 #include <netinet/if_ether.h>
104 #include <net/if_media.h>
105 #include <net/multicast_list.h>
107 #define IF_MAXUNIT 0x7fff /* historical value */
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 UInt32 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
, 0, "IEEE 802.1Q VLAN");
350 SYSCTL_NODE(_net_link_vlan
, PF_LINK
, link
, CTLFLAG_RW
, 0, "for consistency");
353 #define M_VLAN M_DEVBUF
355 static int vlan_clone_create(struct if_clone
*, int);
356 static void vlan_clone_destroy(struct ifnet
*);
357 static int vlan_input(struct mbuf
*m
, char *frame_header
, struct ifnet
*ifp
,
358 u_long protocol_family
, int sync_ok
);
359 static int vlan_output(struct ifnet
*ifp
, struct mbuf
*m
);
360 static int vlan_ioctl(ifnet_t ifp
, u_int32_t 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_long event_code
);
379 static void vlan_parent_link_event(vlan_parent_ref vlp
,
381 extern int dlil_input_packet(struct ifnet
*ifp
, struct mbuf
*m
, char *frame_header
);
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
= dlil_ioctl(0, ifp
, SIOCGIFDEVMTU
, (caddr_t
)&ifr
);
423 *ifdm_p
= ifr
.ifr_devmtu
;
429 siocsifaltmtu(struct ifnet
* ifp
, int mtu
)
433 bzero(&ifr
, sizeof(ifr
));
435 return (dlil_ioctl(0, ifp
, SIOCSIFALTMTU
, (caddr_t
)&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
);
471 static struct ifaddr
*
472 ifaddr_byindex(int i
)
474 if (i
> if_index
|| i
== 0) {
477 return (ifnet_addrs
[i
- 1]);
481 ** vlan_parent synchronization routines
483 static __inline__
void
484 vlan_parent_retain(vlan_parent_ref vlp
)
486 OSIncrementAtomic(&vlp
->vlp_retain_count
);
489 static __inline__
void
490 vlan_parent_release(vlan_parent_ref vlp
)
492 UInt32 old_retain_count
;
494 old_retain_count
= OSDecrementAtomic(&vlp
->vlp_retain_count
);
495 switch (old_retain_count
) {
497 panic("vlan_parent_release: retain count is 0\n");
500 if (g_vlan
->verbose
) {
501 struct ifnet
* ifp
= vlp
->vlp_ifp
;
502 printf("vlan_parent_release(%s%d)\n", ifp
->if_name
,
514 * Function: vlan_parent_wait
516 * Allows a single thread to gain exclusive access to the vlan_parent
517 * data structure. Some operations take a long time to complete,
518 * and some have side-effects that we can't predict. Holding the
519 * vlan_lock() across such operations is not possible.
522 * Before calling, you must be holding the vlan_lock and have taken
523 * a reference on the vlan_parent_ref.
526 vlan_parent_wait(vlan_parent_ref vlp
, const char * msg
)
530 /* other add/remove/multicast-change in progress */
531 while (vlan_parent_flags_change_in_progress(vlp
)) {
532 if (g_vlan
->verbose
) {
533 struct ifnet
* ifp
= vlp
->vlp_ifp
;
535 printf("%s%d: %s msleep\n", ifp
->if_name
, ifp
->if_unit
, msg
);
538 (void)msleep(vlp
, vlan_lck_mtx
, PZERO
, msg
, 0);
540 /* prevent other vlan parent remove/add from taking place */
541 vlan_parent_flags_set_change_in_progress(vlp
);
542 if (g_vlan
->verbose
&& waited
) {
543 struct ifnet
* ifp
= vlp
->vlp_ifp
;
545 printf("%s: %s woke up\n", ifp
->if_name
, ifp
->if_unit
, msg
);
551 * Function: vlan_parent_signal
553 * Allows the thread that previously invoked vlan_parent_wait() to
554 * give up exclusive access to the vlan_parent data structure, and wake up
555 * any other threads waiting to access
557 * Before calling, you must be holding the vlan_lock and have taken
558 * a reference on the vlan_parent_ref.
561 vlan_parent_signal(vlan_parent_ref vlp
, const char * msg
)
563 vlan_parent_flags_clear_change_in_progress(vlp
);
564 wakeup((caddr_t
)vlp
);
565 if (g_vlan
->verbose
) {
566 struct ifnet
* ifp
= vlp
->vlp_ifp
;
568 printf("%s%d: %s wakeup\n", ifp
->if_name
, ifp
->if_unit
, msg
);
575 * Program our multicast filter. What we're actually doing is
576 * programming the multicast filter of the parent. This has the
577 * side effect of causing the parent interface to receive multicast
578 * traffic that it doesn't really want, which ends up being discarded
579 * later by the upper protocol layers. Unfortunately, there's no way
580 * to avoid this: there really is only one physical interface.
583 vlan_setmulti(struct ifnet
* ifp
)
591 ifv
= (ifvlan_ref
)ifp
->if_private
;
592 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
597 /* no parent, no need to program the multicast filter */
600 if (vlan_parent_flags_detaching(vlp
)) {
603 vlan_parent_retain(vlp
);
604 vlan_parent_wait(vlp
, "vlan_setmulti");
606 /* check again, things could have changed */
607 ifv
= (ifvlan_ref
)ifp
->if_private
;
608 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
611 if (ifv
->ifv_vlp
!= vlp
) {
612 /* vlan parent changed */
616 /* no parent, no need to program the multicast filter */
622 /* update parent interface with our multicast addresses */
623 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
628 vlan_parent_signal(vlp
, "vlan_setmulti");
636 ** vlan_parent list manipulation/lookup routines
638 static vlan_parent_ref
639 parent_list_lookup(struct ifnet
* p
)
643 LIST_FOREACH(vlp
, &g_vlan
->parent_list
, vlp_parent_list
) {
644 if (vlp
->vlp_ifp
== p
) {
652 vlan_parent_lookup_tag(vlan_parent_ref vlp
, int tag
)
656 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
657 if (tag
== ifv
->ifv_tag
) {
665 vlan_lookup_parent_and_tag(struct ifnet
* p
, int tag
)
669 vlp
= parent_list_lookup(p
);
671 return (vlan_parent_lookup_tag(vlp
, tag
));
677 vlan_parent_find_max_mtu(vlan_parent_ref vlp
, ifvlan_ref exclude_ifv
)
682 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
685 if (exclude_ifv
== ifv
) {
688 req_mtu
= ifv
->ifv_ifp
->if_mtu
+ ifv
->ifv_mtufudge
;
689 if (req_mtu
> max_mtu
) {
697 * Function: vlan_parent_create
699 * Create a vlan_parent structure to hold the VLAN's for the given
700 * interface. Add it to the list of VLAN parents.
703 vlan_parent_create(struct ifnet
* p
, vlan_parent_ref
* ret_vlp
)
709 vlp
= _MALLOC(sizeof(*vlp
), M_VLAN
, M_WAITOK
);
713 bzero(vlp
, sizeof(*vlp
));
714 error
= siocgifdevmtu(p
, &vlp
->vlp_devmtu
);
716 printf("vlan_parent_create (%s%d): siocgifdevmtu failed, %d\n",
717 p
->if_name
, p
->if_unit
, error
);
721 LIST_INIT(&vlp
->vlp_vlan_list
);
723 vlan_parent_retain(vlp
);
725 & (IF_HWASSIST_VLAN_MTU
| IF_HWASSIST_VLAN_TAGGING
)) {
726 vlan_parent_flags_set_supports_vlan_mtu(vlp
);
733 vlan_parent_remove_all_vlans(vlan_parent_ref vlp
)
738 vlan_assert_lock_held();
740 while ((ifv
= LIST_FIRST(&vlp
->vlp_vlan_list
)) != NULL
) {
743 vlan_if_detach(ifv
->ifv_ifp
);
747 /* the vlan parent has no more VLAN's */
749 ifnet_lock_exclusive(p
);
750 p
->if_eflags
&= ~IFEF_VLAN
;
752 LIST_REMOVE(vlp
, vlp_parent_list
);
754 vlan_parent_release(vlp
);
760 static __inline__
int
761 vlan_parent_no_vlans(vlan_parent_ref vlp
)
763 return (LIST_EMPTY(&vlp
->vlp_vlan_list
));
767 vlan_parent_add_vlan(vlan_parent_ref vlp
, ifvlan_ref ifv
, int tag
)
769 LIST_INSERT_HEAD(&vlp
->vlp_vlan_list
, ifv
, ifv_vlan_list
);
776 vlan_parent_remove_vlan(__unused vlan_parent_ref vlp
, ifvlan_ref ifv
)
779 LIST_REMOVE(ifv
, ifv_vlan_list
);
784 vlan_clone_attach(void)
786 if_clone_attach(&vlan_cloner
);
792 vlan_clone_create(struct if_clone
*ifc
, int unit
)
798 error
= vlan_globals_init();
802 ifv
= _MALLOC(sizeof(struct ifvlan
), M_VLAN
, M_WAITOK
);
803 bzero(ifv
, sizeof(struct ifvlan
));
804 multicast_list_init(&ifv
->ifv_multicast
);
806 /* use the interface name as the unique id for ifp recycle */
807 if ((unsigned int)snprintf(ifv
->ifv_name
, sizeof(ifv
->ifv_name
), "%s%d",
808 ifc
->ifc_name
, unit
) >= sizeof(ifv
->ifv_name
)) {
812 error
= dlil_if_acquire(APPLE_IF_FAM_VLAN
,
814 strlen(ifv
->ifv_name
),
820 ifp
->if_name
= ifc
->ifc_name
;
822 ifp
->if_family
= APPLE_IF_FAM_VLAN
;
825 /* NB: flags are not set here */
826 ifp
->if_linkmib
= &ifv
->ifv_mib
;
827 ifp
->if_linkmiblen
= sizeof ifv
->ifv_mib
;
828 /* NB: mtu is not set here */
831 ifp
->if_ioctl
= vlan_ioctl
;
832 ifp
->if_set_bpf_tap
= vlan_set_bpf_tap
;
833 ifp
->if_free
= vlan_if_free
;
834 ifp
->if_output
= vlan_output
;
835 ifp
->if_hwassist
= 0;
836 ifp
->if_addrlen
= ETHER_ADDR_LEN
; /* XXX ethernet specific */
837 ifp
->if_baudrate
= 0;
838 ifp
->if_type
= IFT_L2VLAN
;
839 ifp
->if_hdrlen
= ETHER_VLAN_ENCAP_LEN
;
841 /* XXX ethernet specific */
842 ifp
->if_broadcast
.length
= ETHER_ADDR_LEN
;
843 bcopy(etherbroadcastaddr
, ifp
->if_broadcast
.u
.buffer
, ETHER_ADDR_LEN
);
845 error
= dlil_if_attach(ifp
);
847 dlil_if_release(ifp
);
851 ifp
->if_private
= ifv
;
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
)
871 if (dlil_if_detach(ifp
) != DLIL_WAIT_FOR_FREE
) {
878 vlan_clone_destroy(struct ifnet
*ifp
)
883 ifv
= ifp
->if_private
;
884 if (ifv
== NULL
|| ifp
->if_type
!= IFT_L2VLAN
) {
888 if (ifvlan_flags_detaching(ifv
)) {
899 vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
904 ifv
= ifp
->if_private
;
905 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
910 case BPF_TAP_DISABLE
:
911 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= NULL
;
915 ifv
->ifv_bpf_input
= func
;
919 ifv
->ifv_bpf_output
= func
;
922 case BPF_TAP_INPUT_OUTPUT
:
923 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= func
;
933 vlan_output(struct ifnet
* ifp
, struct mbuf
* m
)
935 bpf_packet_func bpf_func
;
936 struct ether_vlan_header
* evl
;
947 if ((m
->m_flags
& M_PKTHDR
) == 0) {
952 ifv
= (ifvlan_ref
)ifp
->if_private
;
953 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)
954 || ifvlan_flags_ready(ifv
) == 0) {
966 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
967 soft_vlan
= (p
->if_hwassist
& IF_HWASSIST_VLAN_TAGGING
) == 0;
968 bpf_func
= ifv
->ifv_bpf_output
;
970 encaplen
= ifv
->ifv_encaplen
;
972 vlan_bpf_output(ifp
, m
, bpf_func
);
974 /* do not run parent's if_output() if the parent is not up */
975 if ((p
->if_flags
& (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
)) {
977 ifp
->if_collisions
++;
981 * If underlying interface can do VLAN tag insertion itself,
982 * just pass the packet along. However, we need some way to
983 * tell the interface where the packet came from so that it
984 * knows how to find the VLAN tag to use. We use a field in
985 * the mbuf header to store the VLAN tag, and a bit in the
986 * csum_flags field to mark the field as valid.
988 if (soft_vlan
== 0) {
989 m
->m_pkthdr
.csum_flags
|= CSUM_VLAN_TAG_VALID
;
990 m
->m_pkthdr
.vlan_tag
= tag
;
992 M_PREPEND(m
, encaplen
, M_DONTWAIT
);
994 printf("%s%d: unable to prepend VLAN header\n", ifp
->if_name
,
999 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1000 if (m
->m_len
< (int)sizeof(*evl
)) {
1001 m
= m_pullup(m
, sizeof(*evl
));
1003 printf("%s%d: unable to pullup VLAN header\n", ifp
->if_name
,
1011 * Transform the Ethernet header into an Ethernet header
1012 * with 802.1Q encapsulation.
1014 bcopy(mtod(m
, char *) + encaplen
,
1015 mtod(m
, char *), ETHER_HDR_LEN
);
1016 evl
= mtod(m
, struct ether_vlan_header
*);
1017 evl
->evl_proto
= evl
->evl_encap_proto
;
1018 evl
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
1019 evl
->evl_tag
= htons(tag
);
1021 return dlil_output(p
, 0, m
, NULL
, NULL
, 1);
1025 vlan_input(struct mbuf
* m
, char * frame_header
, struct ifnet
* p
,
1026 __unused u_long protocol_family
, __unused
int sync_ok
)
1028 bpf_packet_func bpf_func
= NULL
;
1029 struct ether_vlan_header
* evl
;
1030 struct ifnet
* ifp
= NULL
;
1034 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1036 * Packet is tagged, m contains a normal
1037 * Ethernet frame; the tag is stored out-of-band.
1039 m
->m_pkthdr
.csum_flags
&= ~CSUM_VLAN_TAG_VALID
;
1040 tag
= EVL_VLANOFTAG(m
->m_pkthdr
.vlan_tag
);
1041 m
->m_pkthdr
.vlan_tag
= 0;
1044 switch (p
->if_type
) {
1046 if (m
->m_len
< ETHER_VLAN_ENCAP_LEN
) {
1050 evl
= (struct ether_vlan_header
*)frame_header
;
1051 if (ntohs(evl
->evl_proto
) == ETHERTYPE_VLAN
) {
1052 /* don't allow VLAN within VLAN */
1056 tag
= EVL_VLANOFTAG(ntohs(evl
->evl_tag
));
1059 * Restore the original ethertype. We'll remove
1060 * the encapsulation after we've found the vlan
1061 * interface corresponding to the tag.
1063 evl
->evl_encap_proto
= evl
->evl_proto
;
1066 printf("vlan_demux: unsupported if type %u",
1076 if ((p
->if_eflags
& IFEF_VLAN
) == 0) {
1077 /* don't bother looking through the VLAN list */
1082 ifv
= vlan_lookup_parent_and_tag(p
, tag
);
1087 || ifvlan_flags_ready(ifv
) == 0
1088 || (ifp
->if_flags
& IFF_UP
) == 0) {
1093 bpf_func
= ifv
->ifv_bpf_input
;
1098 * Packet had an in-line encapsulation header;
1099 * remove it. The original header has already
1100 * been fixed up above.
1102 m
->m_len
-= ETHER_VLAN_ENCAP_LEN
;
1103 m
->m_data
+= ETHER_VLAN_ENCAP_LEN
;
1104 m
->m_pkthdr
.len
-= ETHER_VLAN_ENCAP_LEN
;
1105 m
->m_pkthdr
.csum_flags
= 0; /* can't trust hardware checksum */
1108 m
->m_pkthdr
.rcvif
= ifp
;
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(ifp
, m
, frame_header
);
1116 /* Send priority-tagged packet up through the parent */
1117 dlil_input_packet(p
, m
, frame_header
);
1122 #define VLAN_CONFIG_PROGRESS_VLP_RETAINED 0x1
1123 #define VLAN_CONFIG_PROGRESS_IN_LIST 0x2
1126 vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
)
1130 ifvlan_ref ifv
= NULL
;
1131 struct ifaddr
* ifa1
;
1132 struct ifaddr
* ifa2
;
1133 vlan_parent_ref new_vlp
= NULL
;
1134 int need_vlp_release
= 0;
1135 u_int32_t progress
= 0;
1136 struct sockaddr_dl
*sdl1
;
1137 struct sockaddr_dl
*sdl2
;
1138 vlan_parent_ref vlp
= NULL
;
1140 /* pre-allocate space for vlan_parent, in case we're first */
1141 error
= vlan_parent_create(p
, &new_vlp
);
1147 ifv
= (ifvlan_ref
)ifp
->if_private
;
1148 if (ifv
!= NULL
&& ifv
->ifv_vlp
!= NULL
) {
1150 vlan_parent_release(new_vlp
);
1153 vlp
= parent_list_lookup(p
);
1155 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1156 /* already a VLAN with that tag on this interface */
1162 /* we're the first VLAN on this interface */
1163 LIST_INSERT_HEAD(&g_vlan
->parent_list
, new_vlp
, vlp_parent_list
);
1167 /* need to wait to ensure no one else is trying to add/remove */
1168 vlan_parent_retain(vlp
);
1169 progress
|= VLAN_CONFIG_PROGRESS_VLP_RETAINED
;
1170 vlan_parent_wait(vlp
, "vlan_config");
1172 ifv
= (ifvlan_ref
)ifp
->if_private
;
1177 if (vlan_parent_flags_detaching(vlp
)
1178 || ifvlan_flags_detaching(ifv
) || ifv
->ifv_vlp
!= NULL
) {
1183 /* check again because someone might have gotten in */
1184 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1185 /* already a VLAN with that tag on this interface */
1190 if (vlan_parent_no_vlans(vlp
)) {
1193 vlan_parent_add_vlan(vlp
, ifv
, tag
);
1194 progress
|= VLAN_CONFIG_PROGRESS_IN_LIST
;
1196 /* check whether bond interface is using parent interface */
1197 ifnet_lock_exclusive(p
);
1198 if ((p
->if_eflags
& IFEF_BOND
) != 0) {
1200 /* don't allow VLAN over interface that's already part of a bond */
1204 /* prevent BOND interface from using it */
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_lock_exclusive(p
);
1218 p
->if_flags
|= IFF_UP
;
1220 (void)dlil_ioctl(0, p
, SIOCSIFFLAGS
, (caddr_t
)NULL
);
1223 /* configure parent to receive our multicast addresses */
1224 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
1227 (void)vlan_detach_protocol(p
);
1233 /* no failures past this point */
1236 ifv
->ifv_encaplen
= ETHER_VLAN_ENCAP_LEN
;
1238 if (vlan_parent_flags_supports_vlan_mtu(vlp
)) {
1239 ifv
->ifv_mtufudge
= 0;
1242 * Fudge the MTU by the encapsulation size. This
1243 * makes us incompatible with strictly compliant
1244 * 802.1Q implementations, but allows us to use
1245 * the feature with other NetBSD implementations,
1246 * which might still be useful.
1248 ifv
->ifv_mtufudge
= ifv
->ifv_encaplen
;
1250 ifp
->if_mtu
= ETHERMTU
- ifv
->ifv_mtufudge
;
1253 * Copy only a selected subset of flags from the parent.
1254 * Other flags are none of our business.
1256 ifp
->if_flags
|= (p
->if_flags
&
1257 (IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
));
1259 * If the parent interface can do hardware-assisted
1260 * VLAN encapsulation, then propagate its hardware-
1261 * assisted checksumming flags.
1263 if (p
->if_hwassist
& IF_HWASSIST_VLAN_TAGGING
) {
1264 ifp
->if_hwassist
|= IF_HWASSIST_CSUM_FLAGS(p
->if_hwassist
);
1267 /* set our ethernet address to that of the parent */
1268 ifa1
= ifaddr_byindex(ifp
->if_index
);
1269 ifa2
= ifaddr_byindex(p
->if_index
);
1270 sdl1
= (struct sockaddr_dl
*)ifa1
->ifa_addr
;
1271 sdl2
= (struct sockaddr_dl
*)ifa2
->ifa_addr
;
1272 sdl1
->sdl_type
= IFT_ETHER
;
1273 sdl1
->sdl_alen
= ETHER_ADDR_LEN
;
1274 bcopy(LLADDR(sdl2
), LLADDR(sdl1
), ETHER_ADDR_LEN
);
1276 ifp
->if_flags
|= IFF_RUNNING
;
1277 ifvlan_flags_set_ready(ifv
);
1278 vlan_parent_signal(vlp
, "vlan_config");
1280 if (new_vlp
!= vlp
) {
1281 /* throw it away, it wasn't needed */
1282 vlan_parent_release(new_vlp
);
1287 vlan_assert_lock_held();
1288 vlan_parent_signal(vlp
, "vlan_config");
1291 if ((progress
& VLAN_CONFIG_PROGRESS_IN_LIST
) != 0) {
1292 vlan_parent_remove_vlan(vlp
, ifv
);
1294 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1295 /* the vlan parent has no more VLAN's */
1296 ifnet_lock_exclusive(p
);
1297 p
->if_eflags
&= ~IFEF_VLAN
;
1299 LIST_REMOVE(vlp
, vlp_parent_list
);
1300 /* release outside of the lock below */
1301 need_vlp_release
= 1;
1305 if ((progress
& VLAN_CONFIG_PROGRESS_VLP_RETAINED
) != 0) {
1306 vlan_parent_release(vlp
);
1308 if (need_vlp_release
) {
1309 vlan_parent_release(vlp
);
1311 if (new_vlp
!= vlp
) {
1312 vlan_parent_release(new_vlp
);
1318 vlan_link_event(struct ifnet
* ifp
, struct ifnet
* p
)
1320 struct ifmediareq ifmr
;
1322 /* generate a link event based on the state of the underlying interface */
1323 bzero(&ifmr
, sizeof(ifmr
));
1324 snprintf(ifmr
.ifm_name
, sizeof(ifmr
.ifm_name
),
1325 "%s%d", p
->if_name
, p
->if_unit
);
1326 if ((*p
->if_ioctl
)(p
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) == 0
1327 && ifmr
.ifm_count
> 0 && ifmr
.ifm_status
& IFM_AVALID
) {
1330 event
= (ifmr
.ifm_status
& IFM_ACTIVE
)
1331 ? KEV_DL_LINK_ON
: KEV_DL_LINK_OFF
;
1332 interface_link_event(ifp
, event
);
1338 vlan_unconfig(struct ifnet
* ifp
)
1341 struct ifaddr
* ifa
;
1344 int need_vlp_release
= 0;
1346 struct sockaddr_dl
*sdl
;
1347 vlan_parent_ref vlp
;
1349 vlan_assert_lock_held();
1350 ifv
= (ifvlan_ref
)ifp
->if_private
;
1358 vlan_parent_retain(vlp
);
1359 vlan_parent_wait(vlp
, "vlan_unconfig");
1361 /* check again because another thread could be in vlan_unconfig */
1362 ifv
= (ifvlan_ref
)ifp
->if_private
;
1366 if (ifv
->ifv_vlp
!= vlp
) {
1367 /* vlan parent changed */
1373 /* remember whether we're the last VLAN on the parent */
1374 if (LIST_NEXT(LIST_FIRST(&vlp
->vlp_vlan_list
), ifv_vlan_list
) == NULL
) {
1375 if (g_vlan
->verbose
) {
1376 printf("vlan_unconfig: last vlan on %s%d\n",
1377 p
->if_name
, p
->if_unit
);
1382 /* back-out any effect our mtu might have had on the parent */
1383 (void)vlan_new_mtu(ifp
, ETHERMTU
- ifv
->ifv_mtufudge
);
1387 /* detach VLAN "protocol" */
1389 (void)vlan_detach_protocol(p
);
1392 /* un-join multicast on parent interface */
1393 (void)multicast_list_remove(&ifv
->ifv_multicast
);
1397 /* Disconnect from parent. */
1398 vlan_parent_remove_vlan(vlp
, ifv
);
1400 /* return to the state we were in before SIFVLAN */
1402 ifp
->if_flags
&= ~(IFF_BROADCAST
| IFF_MULTICAST
1403 | IFF_SIMPLEX
| IFF_RUNNING
);
1404 ifp
->if_hwassist
= 0;
1406 ifv
->ifv_mtufudge
= 0;
1408 /* Clear our MAC address. */
1409 ifa
= ifaddr_byindex(ifp
->if_index
);
1410 sdl
= (struct sockaddr_dl
*)(ifa
->ifa_addr
);
1411 sdl
->sdl_type
= IFT_L2VLAN
;
1413 bzero(LLADDR(sdl
), ETHER_ADDR_LEN
);
1415 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1416 /* the vlan parent has no more VLAN's */
1417 ifnet_lock_exclusive(p
);
1418 p
->if_eflags
&= ~IFEF_VLAN
;
1420 LIST_REMOVE(vlp
, vlp_parent_list
);
1421 /* release outside of the lock below */
1426 vlan_parent_signal(vlp
, "vlan_unconfig");
1428 vlan_parent_release(vlp
); /* one because we waited */
1430 while (need_vlp_release
--) {
1431 vlan_parent_release(vlp
);
1438 vlan_set_promisc(struct ifnet
* ifp
)
1442 vlan_parent_ref vlp
;
1445 ifv
= (ifvlan_ref
)ifp
->if_private
;
1446 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1447 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1455 if ((ifp
->if_flags
& IFF_PROMISC
) != 0) {
1456 if (!ifvlan_flags_promisc(ifv
)) {
1457 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 1);
1459 ifvlan_flags_set_promisc(ifv
);
1463 if (ifvlan_flags_promisc(ifv
)) {
1464 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 0);
1466 ifvlan_flags_clear_promisc(ifv
);
1476 vlan_new_mtu(struct ifnet
* ifp
, int mtu
)
1478 struct ifdevmtu
* devmtu_p
;
1484 vlan_parent_ref vlp
;
1486 vlan_assert_lock_held();
1487 ifv
= (ifvlan_ref
)ifp
->if_private
;
1489 devmtu_p
= &vlp
->vlp_devmtu
;
1490 req_mtu
= mtu
+ ifv
->ifv_mtufudge
;
1491 if (req_mtu
> devmtu_p
->ifdm_max
|| req_mtu
< devmtu_p
->ifdm_min
) {
1494 max_mtu
= vlan_parent_find_max_mtu(vlp
, ifv
);
1495 if (req_mtu
> max_mtu
) {
1498 else if (max_mtu
< devmtu_p
->ifdm_current
) {
1502 struct ifnet
* p
= vlp
->vlp_ifp
;
1504 error
= siocsifaltmtu(p
, new_mtu
);
1509 devmtu_p
->ifdm_current
= new_mtu
;
1517 vlan_set_mtu(struct ifnet
* ifp
, int mtu
)
1521 vlan_parent_ref vlp
;
1523 if (mtu
< IF_MINMTU
) {
1527 ifv
= (ifvlan_ref
)ifp
->if_private
;
1528 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1530 return ((ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
);
1533 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1540 vlan_parent_retain(vlp
);
1541 vlan_parent_wait(vlp
, "vlan_set_mtu");
1543 /* check again, something might have changed */
1544 ifv
= (ifvlan_ref
)ifp
->if_private
;
1545 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1546 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1549 if (ifv
->ifv_vlp
!= vlp
) {
1550 /* vlan parent changed */
1553 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1559 error
= vlan_new_mtu(ifp
, mtu
);
1562 vlan_parent_signal(vlp
, "vlan_set_mtu");
1564 vlan_parent_release(vlp
);
1570 vlan_ioctl(ifnet_t ifp
, u_int32_t cmd
, void * data
)
1572 struct ifdevmtu
* devmtu_p
;
1574 struct ifaddr
* ifa
;
1575 struct ifmediareq64
* ifmr
;
1580 user_addr_t user_addr
;
1581 vlan_parent_ref vlp
;
1584 if (ifp
->if_type
!= IFT_L2VLAN
) {
1585 return (EOPNOTSUPP
);
1587 ifr
= (struct ifreq
*)data
;
1588 ifa
= (struct ifaddr
*)data
;
1592 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
1595 case SIOCGIFMEDIA64
:
1598 ifv
= (ifvlan_ref
)ifp
->if_private
;
1599 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1601 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1603 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1605 ifmr
= (struct ifmediareq64
*)data
;
1606 user_addr
= (cmd
== SIOCGIFMEDIA64
)
1607 ? ifmr
->ifm_ifmu
.ifmu_ulist64
1608 : CAST_USER_ADDR_T(ifmr
->ifm_ifmu
.ifmu_ulist32
);
1610 struct ifmediareq64 p_ifmr
;
1612 bzero(&p_ifmr
, sizeof(p_ifmr
));
1613 error
= dlil_ioctl(0, p
, SIOCGIFMEDIA
, (caddr_t
)&p_ifmr
);
1615 ifmr
->ifm_active
= p_ifmr
.ifm_active
;
1616 ifmr
->ifm_current
= p_ifmr
.ifm_current
;
1617 ifmr
->ifm_mask
= p_ifmr
.ifm_mask
;
1618 ifmr
->ifm_status
= p_ifmr
.ifm_status
;
1619 ifmr
->ifm_count
= p_ifmr
.ifm_count
;
1620 /* Limit the result to the parent's current config. */
1621 if (ifmr
->ifm_count
>= 1 && user_addr
!= USER_ADDR_NULL
) {
1622 ifmr
->ifm_count
= 1;
1623 error
= copyout(&ifmr
->ifm_current
, user_addr
,
1628 ifmr
->ifm_active
= ifmr
->ifm_current
= IFM_NONE
;
1630 ifmr
->ifm_status
= IFM_AVALID
;
1631 ifmr
->ifm_count
= 1;
1632 if (user_addr
!= USER_ADDR_NULL
) {
1633 error
= copyout(&ifmr
->ifm_current
, user_addr
, sizeof(int));
1644 ifv
= (ifvlan_ref
)ifp
->if_private
;
1645 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1647 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1651 int min_mtu
= vlp
->vlp_devmtu
.ifdm_min
- ifv
->ifv_mtufudge
;
1652 devmtu_p
= &ifr
->ifr_devmtu
;
1653 devmtu_p
->ifdm_current
= ifp
->if_mtu
;
1654 devmtu_p
->ifdm_min
= max(min_mtu
, IF_MINMTU
);
1655 devmtu_p
->ifdm_max
= vlp
->vlp_devmtu
.ifdm_max
- ifv
->ifv_mtufudge
;
1658 devmtu_p
= &ifr
->ifr_devmtu
;
1659 devmtu_p
->ifdm_current
= 0;
1660 devmtu_p
->ifdm_min
= 0;
1661 devmtu_p
->ifdm_max
= 0;
1667 error
= vlan_set_mtu(ifp
, ifr
->ifr_mtu
);
1671 user_addr
= proc_is64bit(current_proc())
1672 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1673 error
= copyin(user_addr
, &vlr
, sizeof(vlr
));
1678 if (vlr
.vlr_parent
[0] != '\0') {
1679 if (vlr
.vlr_tag
& ~EVL_VLID_MASK
) {
1681 * Don't let the caller set up a VLAN tag with
1682 * anything except VLID bits.
1687 p
= ifunit(vlr
.vlr_parent
);
1692 /* can't do VLAN over anything but ethernet or ethernet aggregate */
1693 if (p
->if_type
!= IFT_ETHER
&& p
->if_type
!= IFT_IEEE8023ADLAG
) {
1694 error
= EPROTONOSUPPORT
;
1697 error
= vlan_config(ifp
, p
, vlr
.vlr_tag
);
1702 /* Update promiscuous mode, if necessary. */
1703 (void)vlan_set_promisc(ifp
);
1705 /* generate a link event based on the state of the parent */
1706 vlan_link_event(ifp
, p
);
1709 ifv
= (ifvlan_ref
)ifp
->if_private
;
1710 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1712 error
= (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1715 error
= vlan_unconfig(ifp
);
1718 interface_link_event(ifp
, KEV_DL_LINK_OFF
);
1724 bzero(&vlr
, sizeof vlr
);
1726 ifv
= (ifvlan_ref
)ifp
->if_private
;
1727 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1729 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1731 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1735 snprintf(vlr
.vlr_parent
, sizeof(vlr
.vlr_parent
),
1736 "%s%d", p
->if_name
, p
->if_unit
);
1739 user_addr
= proc_is64bit(current_proc())
1740 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1741 error
= copyout(&vlr
, user_addr
, sizeof(vlr
));
1746 * For promiscuous mode, we enable promiscuous mode on
1747 * the parent if we need promiscuous on the VLAN interface.
1749 error
= vlan_set_promisc(ifp
);
1754 error
= vlan_setmulti(ifp
);
1763 vlan_if_free(struct ifnet
* ifp
)
1771 ifv
= (ifvlan_ref
)ifp
->if_private
;
1776 ifp
->if_private
= NULL
;
1778 dlil_if_release(ifp
);
1783 vlan_event(struct ifnet
* p
, struct kev_msg
* event
)
1785 vlan_parent_ref vlp
;
1787 /* Check if the interface we are attached to is being detached */
1788 if (event
->vendor_code
!= KEV_VENDOR_APPLE
1789 || event
->kev_class
!= KEV_NETWORK_CLASS
1790 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
1793 switch (event
->event_code
) {
1794 case KEV_DL_IF_DETACHING
:
1795 case KEV_DL_LINK_OFF
:
1796 case KEV_DL_LINK_ON
:
1802 if ((p
->if_eflags
& IFEF_VLAN
) == 0) {
1807 vlp
= parent_list_lookup(p
);
1813 switch (event
->event_code
) {
1814 case KEV_DL_IF_DETACHING
:
1815 vlan_parent_flags_set_detaching(vlp
);
1816 vlan_parent_remove_all_vlans(vlp
);
1819 case KEV_DL_LINK_OFF
:
1820 case KEV_DL_LINK_ON
:
1821 vlan_parent_link_event(vlp
, event
->event_code
);
1831 interface_link_event(struct ifnet
* ifp
, u_long event_code
)
1834 struct kern_event_msg header
;
1836 char if_name
[IFNAMSIZ
];
1839 event
.header
.total_size
= sizeof(event
);
1840 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
1841 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
1842 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
1843 event
.header
.event_code
= event_code
;
1844 event
.header
.event_data
[0] = ifp
->if_family
;
1845 event
.unit
= (u_long
) ifp
->if_unit
;
1846 strncpy(event
.if_name
, ifp
->if_name
, IFNAMSIZ
);
1847 dlil_event(ifp
, &event
.header
);
1852 vlan_parent_link_event(vlan_parent_ref vlp
, u_long event_code
)
1856 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
1857 interface_link_event(ifv
->ifv_ifp
, event_code
);
1864 * Function: vlan_attach_protocol
1866 * Attach a DLIL protocol to the interface, using the ETHERTYPE_VLAN
1869 * The ethernet demux actually special cases VLAN to support hardware.
1870 * The demux here isn't used. The demux will return PF_VLAN for the
1871 * appropriate packets and our vlan_input function will be called.
1874 vlan_attach_protocol(struct ifnet
*ifp
)
1877 struct dlil_proto_reg_str reg
;
1879 bzero(®
, sizeof(reg
));
1880 TAILQ_INIT(®
.demux_desc_head
);
1881 reg
.interface_family
= ifp
->if_family
;
1882 reg
.unit_number
= ifp
->if_unit
;
1883 reg
.input
= vlan_input
;
1884 reg
.event
= vlan_event
;
1885 reg
.protocol_family
= PF_VLAN
;
1886 error
= dlil_attach_protocol(®
);
1888 printf("vlan_proto_attach(%s%d) dlil_attach_protocol failed, %d\n",
1889 ifp
->if_name
, ifp
->if_unit
, error
);
1895 * Function: vlan_detach_protocol
1897 * Detach our DLIL protocol from an interface
1900 vlan_detach_protocol(struct ifnet
*ifp
)
1904 error
= dlil_detach_protocol(ifp
, PF_VLAN
);
1906 printf("vlan_proto_detach(%s%d) dlil_detach_protocol failed, %d\n",
1907 ifp
->if_name
, ifp
->if_unit
, error
);
1914 * DLIL interface family functions
1915 * We use the ethernet dlil functions, since that's all we support.
1916 * If we wanted to handle multiple LAN types (tokenring, etc.), we'd
1917 * call the appropriate routines for that LAN type instead of hard-coding
1920 extern int ether_add_if(struct ifnet
*ifp
);
1921 extern int ether_del_if(struct ifnet
*ifp
);
1922 extern int ether_init_if(struct ifnet
*ifp
);
1923 extern int ether_add_proto_old(struct ifnet
*ifp
, u_long protocol_family
,
1924 struct ddesc_head_str
*desc_head
);
1926 extern int ether_attach_inet(struct ifnet
*ifp
, u_long protocol_family
);
1927 extern int ether_detach_inet(struct ifnet
*ifp
, u_long protocol_family
);
1928 extern int ether_attach_inet6(struct ifnet
*ifp
, u_long protocol_family
);
1929 extern int ether_detach_inet6(struct ifnet
*ifp
, u_long protocol_family
);
1932 vlan_attach_inet(struct ifnet
*ifp
, u_long protocol_family
)
1934 return (ether_attach_inet(ifp
, protocol_family
));
1938 vlan_detach_inet(struct ifnet
*ifp
, u_long protocol_family
)
1940 return (ether_detach_inet(ifp
, protocol_family
));
1944 vlan_attach_inet6(struct ifnet
*ifp
, u_long protocol_family
)
1946 return (ether_attach_inet6(ifp
, protocol_family
));
1950 vlan_detach_inet6(struct ifnet
*ifp
, u_long protocol_family
)
1952 return (ether_detach_inet6(ifp
, protocol_family
));
1956 vlan_add_if(struct ifnet
*ifp
)
1958 return (ether_add_if(ifp
));
1962 vlan_del_if(struct ifnet
*ifp
)
1964 return (ether_del_if(ifp
));
1968 __private_extern__
int
1969 vlan_family_init(void)
1972 struct dlil_ifmod_reg_str ifmod_reg
;
1974 bzero(&ifmod_reg
, sizeof(ifmod_reg
));
1975 ifmod_reg
.add_if
= vlan_add_if
;
1976 ifmod_reg
.del_if
= vlan_del_if
;
1977 ifmod_reg
.init_if
= NULL
;
1978 ifmod_reg
.add_proto
= ether_add_proto_old
;
1979 ifmod_reg
.del_proto
= ether_del_proto
;
1980 ifmod_reg
.ifmod_ioctl
= ether_ioctl
;
1981 ifmod_reg
.shutdown
= NULL
;
1983 if (dlil_reg_if_modules(APPLE_IF_FAM_VLAN
, &ifmod_reg
)) {
1984 printf("WARNING: vlan_family_init -- "
1985 "Can't register if family modules\n");
1990 error
= dlil_reg_proto_module(PF_INET
, APPLE_IF_FAM_VLAN
,
1991 vlan_attach_inet
, vlan_detach_inet
);
1993 printf("dlil_reg_proto_module failed for AF_INET error=%d\n",
1997 error
= dlil_reg_proto_module(PF_INET6
, APPLE_IF_FAM_VLAN
,
1998 vlan_attach_inet6
, vlan_detach_inet6
);
2000 printf("dlil_reg_proto_module failed for AF_INET6 error=%d\n",
2004 vlan_clone_attach();