2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright 1998 Massachusetts Institute of Technology
26 * Permission to use, copy, modify, and distribute this software and
27 * its documentation for any purpose and without fee is hereby
28 * granted, provided that both the above copyright notice and this
29 * permission notice appear in all copies, that both the above
30 * copyright notice and this permission notice appear in all
31 * supporting documentation, and that the name of M.I.T. not be used
32 * in advertising or publicity pertaining to distribution of the
33 * software without specific, written prior permission. M.I.T. makes
34 * no representations about the suitability of this software for any
35 * purpose. It is provided "as is" without express or implied
38 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
39 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
40 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
42 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * $FreeBSD: src/sys/net/if_vlan.c,v 1.54 2003/10/31 18:32:08 brooks Exp $
55 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
56 * Might be extended some day to also handle IEEE 802.1p priority
57 * tagging. This is sort of sneaky in the implementation, since
58 * we need to pretend to be enough of an Ethernet implementation
59 * to make arp work. The way we do this is by telling everyone
60 * that we are an Ethernet, and then catch the packets that
61 * ether_output() left on our output queue when it calls
62 * if_start(), rewrite them for use by the real outgoing interface,
63 * and ask it to send them.
67 #include <sys/param.h>
68 #include <sys/kernel.h>
69 #include <sys/malloc.h>
71 #include <sys/queue.h>
72 #include <sys/socket.h>
73 #include <sys/sockio.h>
74 #include <sys/sysctl.h>
75 #include <sys/systm.h>
76 #include <sys/kern_event.h>
79 #include <net/ethernet.h>
81 #include <net/if_arp.h>
82 #include <net/if_dl.h>
83 #include <net/if_ether.h>
84 #include <net/if_types.h>
85 #include <net/if_vlan_var.h>
86 #include <libkern/OSAtomic.h>
90 #include <kern/locks.h>
93 #include <netinet/in.h>
94 #include <netinet/if_ether.h>
97 #include <net/if_media.h>
98 #include <net/multicast_list.h>
100 #define IF_MAXUNIT 0x7fff /* historical value */
102 #define VLANNAME "vlan"
104 typedef int (bpf_callback_func
)(struct ifnet
*, struct mbuf
*);
105 typedef int (if_set_bpf_tap_func
)(struct ifnet
*ifp
, int mode
, bpf_callback_func
* func
);
110 static __inline__ lck_grp_t
*
111 my_lck_grp_alloc_init(const char * grp_name
)
114 lck_grp_attr_t
* grp_attrs
;
116 grp_attrs
= lck_grp_attr_alloc_init();
117 lck_grp_attr_setdefault(grp_attrs
);
118 grp
= lck_grp_alloc_init(grp_name
, grp_attrs
);
119 lck_grp_attr_free(grp_attrs
);
123 static __inline__ lck_mtx_t
*
124 my_lck_mtx_alloc_init(lck_grp_t
* lck_grp
)
126 lck_attr_t
* lck_attrs
;
129 lck_attrs
= lck_attr_alloc_init();
130 lck_attr_setdefault(lck_attrs
);
131 lck_mtx
= lck_mtx_alloc_init(lck_grp
, lck_attrs
);
132 lck_attr_free(lck_attrs
);
136 static lck_mtx_t
* vlan_lck_mtx
;
138 static __inline__
void
141 lck_grp_t
* vlan_lck_grp
;
143 vlan_lck_grp
= my_lck_grp_alloc_init("if_vlan");
144 vlan_lck_mtx
= my_lck_mtx_alloc_init(vlan_lck_grp
);
147 static __inline__
void
148 vlan_assert_lock_held(void)
150 lck_mtx_assert(vlan_lck_mtx
, LCK_MTX_ASSERT_OWNED
);
154 static __inline__
void
155 vlan_assert_lock_not_held(void)
157 lck_mtx_assert(vlan_lck_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
161 static __inline__
void
164 lck_mtx_lock(vlan_lck_mtx
);
168 static __inline__
void
171 lck_mtx_unlock(vlan_lck_mtx
);
176 ** vlan structures, types
179 LIST_HEAD(vlan_parent_list
, vlan_parent
);
181 LIST_HEAD(ifvlan_list
, ifvlan
);
183 typedef struct vlan_parent
{
184 LIST_ENTRY(vlan_parent
) vlp_parent_list
;/* list of parents */
185 struct ifnet
* vlp_ifp
; /* interface */
186 struct ifvlan_list vlp_vlan_list
; /* list of VLAN's */
187 #define VLPF_SUPPORTS_VLAN_MTU 0x1
188 #define VLPF_CHANGE_IN_PROGRESS 0x2
189 #define VLPF_DETACHING 0x4
191 struct ifdevmtu vlp_devmtu
;
192 UInt32 vlp_retain_count
;
193 } vlan_parent
, * vlan_parent_ref
;
196 LIST_ENTRY(ifvlan
) ifv_vlan_list
;
197 char ifv_name
[IFNAMSIZ
]; /* our unique id */
198 struct ifnet
* ifv_ifp
; /* our interface */
199 vlan_parent_ref ifv_vlp
; /* parent information */
201 u_int16_t ifvm_encaplen
;/* encapsulation length */
202 u_int16_t ifvm_mtufudge
;/* MTU fudged by this much */
203 u_int16_t ifvm_proto
; /* encapsulation ethertype */
204 u_int16_t ifvm_tag
; /* tag to apply on packets leaving if */
206 struct multicast_list ifv_multicast
;
207 #define IFVF_PROMISC 0x1 /* promiscuous mode enabled */
208 #define IFVF_DETACHING 0x2 /* interface is detaching */
209 #define IFVF_READY 0x4 /* interface is ready */
211 bpf_packet_func ifv_bpf_input
;
212 bpf_packet_func ifv_bpf_output
;
215 typedef struct ifvlan
* ifvlan_ref
;
217 typedef struct vlan_globals_s
{
218 struct vlan_parent_list parent_list
;
220 } * vlan_globals_ref
;
222 static vlan_globals_ref g_vlan
;
224 #define ifv_tag ifv_mib.ifvm_tag
225 #define ifv_encaplen ifv_mib.ifvm_encaplen
226 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
230 ** vlan_parent_ref vlp_flags in-lines
232 static __inline__
int
233 vlan_parent_flags_supports_vlan_mtu(vlan_parent_ref vlp
)
235 return ((vlp
->vlp_flags
& VLPF_SUPPORTS_VLAN_MTU
) != 0);
238 static __inline__
void
239 vlan_parent_flags_set_supports_vlan_mtu(vlan_parent_ref vlp
)
241 vlp
->vlp_flags
|= VLPF_SUPPORTS_VLAN_MTU
;
245 static __inline__
void
246 vlan_parent_flags_clear_supports_vlan_mtu(vlan_parent_ref vlp
)
248 vlp
->vlp_flags
&= ~VLPF_SUPPORTS_VLAN_MTU
;
252 static __inline__
int
253 vlan_parent_flags_change_in_progress(vlan_parent_ref vlp
)
255 return ((vlp
->vlp_flags
& VLPF_CHANGE_IN_PROGRESS
) != 0);
258 static __inline__
void
259 vlan_parent_flags_set_change_in_progress(vlan_parent_ref vlp
)
261 vlp
->vlp_flags
|= VLPF_CHANGE_IN_PROGRESS
;
265 static __inline__
void
266 vlan_parent_flags_clear_change_in_progress(vlan_parent_ref vlp
)
268 vlp
->vlp_flags
&= ~VLPF_CHANGE_IN_PROGRESS
;
272 static __inline__
int
273 vlan_parent_flags_detaching(struct vlan_parent
* vlp
)
275 return ((vlp
->vlp_flags
& VLPF_DETACHING
) != 0);
278 static __inline__
void
279 vlan_parent_flags_set_detaching(struct vlan_parent
* vlp
)
281 vlp
->vlp_flags
|= VLPF_DETACHING
;
287 ** ifvlan_flags in-lines routines
289 static __inline__
int
290 ifvlan_flags_promisc(ifvlan_ref ifv
)
292 return ((ifv
->ifv_flags
& IFVF_PROMISC
) != 0);
295 static __inline__
void
296 ifvlan_flags_set_promisc(ifvlan_ref ifv
)
298 ifv
->ifv_flags
|= IFVF_PROMISC
;
302 static __inline__
void
303 ifvlan_flags_clear_promisc(ifvlan_ref ifv
)
305 ifv
->ifv_flags
&= ~IFVF_PROMISC
;
309 static __inline__
int
310 ifvlan_flags_ready(ifvlan_ref ifv
)
312 return ((ifv
->ifv_flags
& IFVF_READY
) != 0);
315 static __inline__
void
316 ifvlan_flags_set_ready(ifvlan_ref ifv
)
318 ifv
->ifv_flags
|= IFVF_READY
;
322 static __inline__
void
323 ifvlan_flags_clear_ready(ifvlan_ref ifv
)
325 ifv
->ifv_flags
&= ~IFVF_READY
;
329 static __inline__
int
330 ifvlan_flags_detaching(ifvlan_ref ifv
)
332 return ((ifv
->ifv_flags
& IFVF_DETACHING
) != 0);
335 static __inline__
void
336 ifvlan_flags_set_detaching(ifvlan_ref ifv
)
338 ifv
->ifv_flags
|= IFVF_DETACHING
;
343 SYSCTL_DECL(_net_link
);
344 SYSCTL_NODE(_net_link
, IFT_L2VLAN
, vlan
, CTLFLAG_RW
, 0, "IEEE 802.1Q VLAN");
345 SYSCTL_NODE(_net_link_vlan
, PF_LINK
, link
, CTLFLAG_RW
, 0, "for consistency");
348 #define M_VLAN M_DEVBUF
350 static int vlan_clone_create(struct if_clone
*, int);
351 static void vlan_clone_destroy(struct ifnet
*);
352 static int vlan_input(struct mbuf
*m
, char *frame_header
, struct ifnet
*ifp
,
353 u_long protocol_family
, int sync_ok
);
354 static int vlan_output(struct ifnet
*ifp
, struct mbuf
*m
);
355 static int vlan_ioctl(ifnet_t ifp
, u_int32_t cmd
, void * addr
);
356 static int vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
,
357 bpf_packet_func func
);
358 static int vlan_attach_protocol(struct ifnet
*ifp
);
359 static int vlan_detach_protocol(struct ifnet
*ifp
);
360 static int vlan_setmulti(struct ifnet
*ifp
);
361 static int vlan_unconfig(struct ifnet
*ifp
);
362 static int vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
);
363 static void vlan_if_free(struct ifnet
* ifp
);
364 static void vlan_remove(ifvlan_ref ifv
);
365 static void vlan_if_detach(struct ifnet
* ifp
);
366 static int vlan_new_mtu(struct ifnet
* ifp
, int mtu
);
368 static struct if_clone vlan_cloner
= IF_CLONE_INITIALIZER(VLANNAME
,
373 static void interface_link_event(struct ifnet
* ifp
, u_long event_code
);
374 static void vlan_parent_link_event(vlan_parent_ref vlp
,
376 extern int dlil_input_packet(struct ifnet
*ifp
, struct mbuf
*m
, char *frame_header
);
379 vlan_globals_init(void)
383 vlan_assert_lock_not_held();
385 if (g_vlan
!= NULL
) {
388 v
= _MALLOC(sizeof(*v
), M_VLAN
, M_WAITOK
);
390 LIST_INIT(&v
->parent_list
);
394 if (g_vlan
!= NULL
) {
410 siocgifdevmtu(struct ifnet
* ifp
, struct ifdevmtu
* ifdm_p
)
415 bzero(&ifr
, sizeof(ifr
));
416 error
= dlil_ioctl(0, ifp
, SIOCGIFDEVMTU
, (caddr_t
)&ifr
);
418 *ifdm_p
= ifr
.ifr_devmtu
;
424 siocsifaltmtu(struct ifnet
* ifp
, int mtu
)
428 bzero(&ifr
, sizeof(ifr
));
430 return (dlil_ioctl(0, ifp
, SIOCSIFALTMTU
, (caddr_t
)&ifr
));
433 static __inline__
void
434 vlan_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
435 bpf_packet_func func
)
443 static __inline__
void
444 vlan_bpf_input(struct ifnet
* ifp
, struct mbuf
* m
,
445 bpf_packet_func func
, char * frame_header
,
446 int frame_header_len
, int encap_len
)
450 /* present the right header to bpf */
451 bcopy(frame_header
, frame_header
+ encap_len
, frame_header_len
);
453 m
->m_data
-= frame_header_len
;
454 m
->m_len
+= frame_header_len
;
456 m
->m_data
+= frame_header_len
;
457 m
->m_len
-= frame_header_len
;
459 /* restore the header */
460 bcopy(frame_header
+ encap_len
, frame_header
, frame_header_len
);
466 static struct ifaddr
*
467 ifaddr_byindex(int i
)
469 if (i
> if_index
|| i
== 0) {
472 return (ifnet_addrs
[i
- 1]);
476 ** vlan_parent synchronization routines
478 static __inline__
void
479 vlan_parent_retain(vlan_parent_ref vlp
)
481 OSIncrementAtomic(&vlp
->vlp_retain_count
);
484 static __inline__
void
485 vlan_parent_release(vlan_parent_ref vlp
)
487 UInt32 old_retain_count
;
489 old_retain_count
= OSDecrementAtomic(&vlp
->vlp_retain_count
);
490 switch (old_retain_count
) {
492 panic("vlan_parent_release: retain count is 0\n");
495 if (g_vlan
->verbose
) {
496 struct ifnet
* ifp
= vlp
->vlp_ifp
;
497 printf("vlan_parent_release(%s%d)\n", ifp
->if_name
,
509 * Function: vlan_parent_wait
511 * Allows a single thread to gain exclusive access to the vlan_parent
512 * data structure. Some operations take a long time to complete,
513 * and some have side-effects that we can't predict. Holding the
514 * vlan_lock() across such operations is not possible.
517 * Before calling, you must be holding the vlan_lock and have taken
518 * a reference on the vlan_parent_ref.
521 vlan_parent_wait(vlan_parent_ref vlp
, const char * msg
)
525 /* other add/remove/multicast-change in progress */
526 while (vlan_parent_flags_change_in_progress(vlp
)) {
527 if (g_vlan
->verbose
) {
528 struct ifnet
* ifp
= vlp
->vlp_ifp
;
530 printf("%s%d: %s msleep\n", ifp
->if_name
, ifp
->if_unit
, msg
);
533 (void)msleep(vlp
, vlan_lck_mtx
, PZERO
, msg
, 0);
535 /* prevent other vlan parent remove/add from taking place */
536 vlan_parent_flags_set_change_in_progress(vlp
);
537 if (g_vlan
->verbose
&& waited
) {
538 struct ifnet
* ifp
= vlp
->vlp_ifp
;
540 printf("%s: %s woke up\n", ifp
->if_name
, ifp
->if_unit
, msg
);
546 * Function: vlan_parent_signal
548 * Allows the thread that previously invoked vlan_parent_wait() to
549 * give up exclusive access to the vlan_parent data structure, and wake up
550 * any other threads waiting to access
552 * Before calling, you must be holding the vlan_lock and have taken
553 * a reference on the vlan_parent_ref.
556 vlan_parent_signal(vlan_parent_ref vlp
, const char * msg
)
558 vlan_parent_flags_clear_change_in_progress(vlp
);
559 wakeup((caddr_t
)vlp
);
560 if (g_vlan
->verbose
) {
561 struct ifnet
* ifp
= vlp
->vlp_ifp
;
563 printf("%s%d: %s wakeup\n", ifp
->if_name
, ifp
->if_unit
, msg
);
570 * Program our multicast filter. What we're actually doing is
571 * programming the multicast filter of the parent. This has the
572 * side effect of causing the parent interface to receive multicast
573 * traffic that it doesn't really want, which ends up being discarded
574 * later by the upper protocol layers. Unfortunately, there's no way
575 * to avoid this: there really is only one physical interface.
578 vlan_setmulti(struct ifnet
* ifp
)
586 ifv
= (ifvlan_ref
)ifp
->if_private
;
587 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
592 /* no parent, no need to program the multicast filter */
595 if (vlan_parent_flags_detaching(vlp
)) {
598 vlan_parent_retain(vlp
);
599 vlan_parent_wait(vlp
, "vlan_setmulti");
601 /* check again, things could have changed */
602 ifv
= (ifvlan_ref
)ifp
->if_private
;
603 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
606 if (ifv
->ifv_vlp
!= vlp
) {
607 /* vlan parent changed */
611 /* no parent, no need to program the multicast filter */
617 /* update parent interface with our multicast addresses */
618 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
623 vlan_parent_signal(vlp
, "vlan_setmulti");
631 ** vlan_parent list manipulation/lookup routines
633 static vlan_parent_ref
634 parent_list_lookup(struct ifnet
* p
)
638 LIST_FOREACH(vlp
, &g_vlan
->parent_list
, vlp_parent_list
) {
639 if (vlp
->vlp_ifp
== p
) {
647 vlan_parent_lookup_tag(vlan_parent_ref vlp
, int tag
)
651 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
652 if (tag
== ifv
->ifv_tag
) {
660 vlan_lookup_parent_and_tag(struct ifnet
* p
, int tag
)
664 vlp
= parent_list_lookup(p
);
666 return (vlan_parent_lookup_tag(vlp
, tag
));
672 vlan_parent_find_max_mtu(vlan_parent_ref vlp
, ifvlan_ref exclude_ifv
)
677 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
680 if (exclude_ifv
== ifv
) {
683 req_mtu
= ifv
->ifv_ifp
->if_mtu
+ ifv
->ifv_mtufudge
;
684 if (req_mtu
> max_mtu
) {
692 * Function: vlan_parent_create
694 * Create a vlan_parent structure to hold the VLAN's for the given
695 * interface. Add it to the list of VLAN parents.
698 vlan_parent_create(struct ifnet
* p
, vlan_parent_ref
* ret_vlp
)
704 vlp
= _MALLOC(sizeof(*vlp
), M_VLAN
, M_WAITOK
);
708 bzero(vlp
, sizeof(*vlp
));
709 error
= siocgifdevmtu(p
, &vlp
->vlp_devmtu
);
711 printf("vlan_parent_create (%s%d): siocgifdevmtu failed, %d\n",
712 p
->if_name
, p
->if_unit
, error
);
716 LIST_INIT(&vlp
->vlp_vlan_list
);
718 vlan_parent_retain(vlp
);
720 & (IF_HWASSIST_VLAN_MTU
| IF_HWASSIST_VLAN_TAGGING
)) {
721 vlan_parent_flags_set_supports_vlan_mtu(vlp
);
728 vlan_parent_remove_all_vlans(vlan_parent_ref vlp
)
733 vlan_assert_lock_held();
735 while ((ifv
= LIST_FIRST(&vlp
->vlp_vlan_list
)) != NULL
) {
738 vlan_if_detach(ifv
->ifv_ifp
);
742 /* the vlan parent has no more VLAN's */
744 ifnet_lock_exclusive(p
);
745 p
->if_eflags
&= ~IFEF_VLAN
;
747 LIST_REMOVE(vlp
, vlp_parent_list
);
749 vlan_parent_release(vlp
);
755 static __inline__
int
756 vlan_parent_no_vlans(vlan_parent_ref vlp
)
758 return (LIST_EMPTY(&vlp
->vlp_vlan_list
));
762 vlan_parent_add_vlan(vlan_parent_ref vlp
, ifvlan_ref ifv
, int tag
)
764 LIST_INSERT_HEAD(&vlp
->vlp_vlan_list
, ifv
, ifv_vlan_list
);
771 vlan_parent_remove_vlan(__unused vlan_parent_ref vlp
, ifvlan_ref ifv
)
774 LIST_REMOVE(ifv
, ifv_vlan_list
);
779 vlan_clone_attach(void)
781 if_clone_attach(&vlan_cloner
);
787 vlan_clone_create(struct if_clone
*ifc
, int unit
)
793 error
= vlan_globals_init();
797 ifv
= _MALLOC(sizeof(struct ifvlan
), M_VLAN
, M_WAITOK
);
798 bzero(ifv
, sizeof(struct ifvlan
));
799 multicast_list_init(&ifv
->ifv_multicast
);
801 /* use the interface name as the unique id for ifp recycle */
802 if ((unsigned int)snprintf(ifv
->ifv_name
, sizeof(ifv
->ifv_name
), "%s%d",
803 ifc
->ifc_name
, unit
) >= sizeof(ifv
->ifv_name
)) {
807 error
= dlil_if_acquire(APPLE_IF_FAM_VLAN
,
809 strlen(ifv
->ifv_name
),
815 ifp
->if_name
= ifc
->ifc_name
;
817 ifp
->if_family
= APPLE_IF_FAM_VLAN
;
820 /* NB: flags are not set here */
821 ifp
->if_linkmib
= &ifv
->ifv_mib
;
822 ifp
->if_linkmiblen
= sizeof ifv
->ifv_mib
;
823 /* NB: mtu is not set here */
826 ifp
->if_ioctl
= vlan_ioctl
;
827 ifp
->if_set_bpf_tap
= vlan_set_bpf_tap
;
828 ifp
->if_free
= vlan_if_free
;
829 ifp
->if_output
= vlan_output
;
830 ifp
->if_hwassist
= 0;
831 ifp
->if_addrlen
= ETHER_ADDR_LEN
; /* XXX ethernet specific */
832 ifp
->if_baudrate
= 0;
833 ifp
->if_type
= IFT_L2VLAN
;
834 ifp
->if_hdrlen
= ETHER_VLAN_ENCAP_LEN
;
836 /* XXX ethernet specific */
837 ifp
->if_broadcast
.length
= ETHER_ADDR_LEN
;
838 bcopy(etherbroadcastaddr
, ifp
->if_broadcast
.u
.buffer
, ETHER_ADDR_LEN
);
840 error
= dlil_if_attach(ifp
);
842 dlil_if_release(ifp
);
846 ifp
->if_private
= ifv
;
849 /* attach as ethernet */
850 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
855 vlan_remove(ifvlan_ref ifv
)
857 vlan_assert_lock_held();
858 ifvlan_flags_set_detaching(ifv
);
859 vlan_unconfig(ifv
->ifv_ifp
);
864 vlan_if_detach(struct ifnet
* ifp
)
866 if (dlil_if_detach(ifp
) != DLIL_WAIT_FOR_FREE
) {
873 vlan_clone_destroy(struct ifnet
*ifp
)
878 ifv
= ifp
->if_private
;
879 if (ifv
== NULL
|| ifp
->if_type
!= IFT_L2VLAN
) {
883 if (ifvlan_flags_detaching(ifv
)) {
894 vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
899 ifv
= ifp
->if_private
;
900 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
905 case BPF_TAP_DISABLE
:
906 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= NULL
;
910 ifv
->ifv_bpf_input
= func
;
914 ifv
->ifv_bpf_output
= func
;
917 case BPF_TAP_INPUT_OUTPUT
:
918 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= func
;
928 vlan_output(struct ifnet
* ifp
, struct mbuf
* m
)
930 bpf_packet_func bpf_func
;
931 struct ether_vlan_header
* evl
;
942 if ((m
->m_flags
& M_PKTHDR
) == 0) {
947 ifv
= (ifvlan_ref
)ifp
->if_private
;
948 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)
949 || ifvlan_flags_ready(ifv
) == 0) {
961 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
962 soft_vlan
= (p
->if_hwassist
& IF_HWASSIST_VLAN_TAGGING
) == 0;
963 bpf_func
= ifv
->ifv_bpf_output
;
965 encaplen
= ifv
->ifv_encaplen
;
967 vlan_bpf_output(ifp
, m
, bpf_func
);
969 /* do not run parent's if_output() if the parent is not up */
970 if ((p
->if_flags
& (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
)) {
972 ifp
->if_collisions
++;
976 * If underlying interface can do VLAN tag insertion itself,
977 * just pass the packet along. However, we need some way to
978 * tell the interface where the packet came from so that it
979 * knows how to find the VLAN tag to use. We use a field in
980 * the mbuf header to store the VLAN tag, and a bit in the
981 * csum_flags field to mark the field as valid.
983 if (soft_vlan
== 0) {
984 m
->m_pkthdr
.csum_flags
|= CSUM_VLAN_TAG_VALID
;
985 m
->m_pkthdr
.vlan_tag
= tag
;
987 M_PREPEND(m
, encaplen
, M_DONTWAIT
);
989 printf("%s%d: unable to prepend VLAN header\n", ifp
->if_name
,
994 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
995 if (m
->m_len
< (int)sizeof(*evl
)) {
996 m
= m_pullup(m
, sizeof(*evl
));
998 printf("%s%d: unable to pullup VLAN header\n", ifp
->if_name
,
1006 * Transform the Ethernet header into an Ethernet header
1007 * with 802.1Q encapsulation.
1009 bcopy(mtod(m
, char *) + encaplen
,
1010 mtod(m
, char *), ETHER_HDR_LEN
);
1011 evl
= mtod(m
, struct ether_vlan_header
*);
1012 evl
->evl_proto
= evl
->evl_encap_proto
;
1013 evl
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
1014 evl
->evl_tag
= htons(tag
);
1016 return dlil_output(p
, 0, m
, NULL
, NULL
, 1);
1020 vlan_input(struct mbuf
* m
, char * frame_header
, struct ifnet
* p
,
1021 __unused u_long protocol_family
, __unused
int sync_ok
)
1023 bpf_packet_func bpf_func
= NULL
;
1024 struct ether_vlan_header
* evl
;
1025 struct ifnet
* ifp
= NULL
;
1029 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1031 * Packet is tagged, m contains a normal
1032 * Ethernet frame; the tag is stored out-of-band.
1034 m
->m_pkthdr
.csum_flags
&= ~CSUM_VLAN_TAG_VALID
;
1035 tag
= EVL_VLANOFTAG(m
->m_pkthdr
.vlan_tag
);
1036 m
->m_pkthdr
.vlan_tag
= 0;
1039 switch (p
->if_type
) {
1041 if (m
->m_len
< ETHER_VLAN_ENCAP_LEN
) {
1045 evl
= (struct ether_vlan_header
*)frame_header
;
1046 if (ntohs(evl
->evl_proto
) == ETHERTYPE_VLAN
) {
1047 /* don't allow VLAN within VLAN */
1051 tag
= EVL_VLANOFTAG(ntohs(evl
->evl_tag
));
1054 * Restore the original ethertype. We'll remove
1055 * the encapsulation after we've found the vlan
1056 * interface corresponding to the tag.
1058 evl
->evl_encap_proto
= evl
->evl_proto
;
1061 printf("vlan_demux: unsupported if type %u",
1071 if ((p
->if_eflags
& IFEF_VLAN
) == 0) {
1072 /* don't bother looking through the VLAN list */
1077 ifv
= vlan_lookup_parent_and_tag(p
, tag
);
1082 || ifvlan_flags_ready(ifv
) == 0
1083 || (ifp
->if_flags
& IFF_UP
) == 0) {
1088 bpf_func
= ifv
->ifv_bpf_input
;
1093 * Packet had an in-line encapsulation header;
1094 * remove it. The original header has already
1095 * been fixed up above.
1097 m
->m_len
-= ETHER_VLAN_ENCAP_LEN
;
1098 m
->m_data
+= ETHER_VLAN_ENCAP_LEN
;
1099 m
->m_pkthdr
.len
-= ETHER_VLAN_ENCAP_LEN
;
1100 m
->m_pkthdr
.csum_flags
= 0; /* can't trust hardware checksum */
1103 m
->m_pkthdr
.rcvif
= ifp
;
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(ifp
, m
, frame_header
);
1111 /* Send priority-tagged packet up through the parent */
1112 dlil_input_packet(p
, m
, frame_header
);
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 struct ifaddr
* ifa1
;
1127 struct ifaddr
* ifa2
;
1128 vlan_parent_ref new_vlp
= NULL
;
1129 int need_vlp_release
= 0;
1130 u_int32_t progress
= 0;
1131 struct sockaddr_dl
*sdl1
;
1132 struct sockaddr_dl
*sdl2
;
1133 vlan_parent_ref vlp
= NULL
;
1135 /* pre-allocate space for vlan_parent, in case we're first */
1136 error
= vlan_parent_create(p
, &new_vlp
);
1142 ifv
= (ifvlan_ref
)ifp
->if_private
;
1143 if (ifv
!= NULL
&& ifv
->ifv_vlp
!= NULL
) {
1145 vlan_parent_release(new_vlp
);
1148 vlp
= parent_list_lookup(p
);
1150 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1151 /* already a VLAN with that tag on this interface */
1157 /* we're the first VLAN on this interface */
1158 LIST_INSERT_HEAD(&g_vlan
->parent_list
, new_vlp
, vlp_parent_list
);
1162 /* need to wait to ensure no one else is trying to add/remove */
1163 vlan_parent_retain(vlp
);
1164 progress
|= VLAN_CONFIG_PROGRESS_VLP_RETAINED
;
1165 vlan_parent_wait(vlp
, "vlan_config");
1167 ifv
= (ifvlan_ref
)ifp
->if_private
;
1172 if (vlan_parent_flags_detaching(vlp
)
1173 || ifvlan_flags_detaching(ifv
) || ifv
->ifv_vlp
!= NULL
) {
1178 /* check again because someone might have gotten in */
1179 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1180 /* already a VLAN with that tag on this interface */
1185 if (vlan_parent_no_vlans(vlp
)) {
1188 vlan_parent_add_vlan(vlp
, ifv
, tag
);
1189 progress
|= VLAN_CONFIG_PROGRESS_IN_LIST
;
1191 /* check whether bond interface is using parent interface */
1192 ifnet_lock_exclusive(p
);
1193 if ((p
->if_eflags
& IFEF_BOND
) != 0) {
1195 /* don't allow VLAN over interface that's already part of a bond */
1199 /* prevent BOND interface from using it */
1200 p
->if_eflags
|= IFEF_VLAN
;
1205 /* attach our VLAN "protocol" to the interface */
1206 error
= vlan_attach_protocol(p
);
1211 /* mark the parent interface up */
1212 ifnet_lock_exclusive(p
);
1213 p
->if_flags
|= IFF_UP
;
1215 (void)dlil_ioctl(0, p
, SIOCSIFFLAGS
, (caddr_t
)NULL
);
1218 /* configure parent to receive our multicast addresses */
1219 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
1222 (void)vlan_detach_protocol(p
);
1228 /* no failures past this point */
1231 ifv
->ifv_encaplen
= ETHER_VLAN_ENCAP_LEN
;
1233 if (vlan_parent_flags_supports_vlan_mtu(vlp
)) {
1234 ifv
->ifv_mtufudge
= 0;
1237 * Fudge the MTU by the encapsulation size. This
1238 * makes us incompatible with strictly compliant
1239 * 802.1Q implementations, but allows us to use
1240 * the feature with other NetBSD implementations,
1241 * which might still be useful.
1243 ifv
->ifv_mtufudge
= ifv
->ifv_encaplen
;
1245 ifp
->if_mtu
= ETHERMTU
- ifv
->ifv_mtufudge
;
1248 * Copy only a selected subset of flags from the parent.
1249 * Other flags are none of our business.
1251 ifp
->if_flags
|= (p
->if_flags
&
1252 (IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
));
1254 * If the parent interface can do hardware-assisted
1255 * VLAN encapsulation, then propagate its hardware-
1256 * assisted checksumming flags.
1258 if (p
->if_hwassist
& IF_HWASSIST_VLAN_TAGGING
) {
1259 ifp
->if_hwassist
|= IF_HWASSIST_CSUM_FLAGS(p
->if_hwassist
);
1262 /* set our ethernet address to that of the parent */
1263 ifa1
= ifaddr_byindex(ifp
->if_index
);
1264 ifa2
= ifaddr_byindex(p
->if_index
);
1265 sdl1
= (struct sockaddr_dl
*)ifa1
->ifa_addr
;
1266 sdl2
= (struct sockaddr_dl
*)ifa2
->ifa_addr
;
1267 sdl1
->sdl_type
= IFT_ETHER
;
1268 sdl1
->sdl_alen
= ETHER_ADDR_LEN
;
1269 bcopy(LLADDR(sdl2
), LLADDR(sdl1
), ETHER_ADDR_LEN
);
1271 ifp
->if_flags
|= IFF_RUNNING
;
1272 ifvlan_flags_set_ready(ifv
);
1273 vlan_parent_signal(vlp
, "vlan_config");
1275 if (new_vlp
!= vlp
) {
1276 /* throw it away, it wasn't needed */
1277 vlan_parent_release(new_vlp
);
1282 vlan_assert_lock_held();
1283 vlan_parent_signal(vlp
, "vlan_config");
1286 if ((progress
& VLAN_CONFIG_PROGRESS_IN_LIST
) != 0) {
1287 vlan_parent_remove_vlan(vlp
, ifv
);
1289 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1290 /* the vlan parent has no more VLAN's */
1291 ifnet_lock_exclusive(p
);
1292 p
->if_eflags
&= ~IFEF_VLAN
;
1294 LIST_REMOVE(vlp
, vlp_parent_list
);
1295 /* release outside of the lock below */
1296 need_vlp_release
= 1;
1300 if ((progress
& VLAN_CONFIG_PROGRESS_VLP_RETAINED
) != 0) {
1301 vlan_parent_release(vlp
);
1303 if (need_vlp_release
) {
1304 vlan_parent_release(vlp
);
1306 if (new_vlp
!= vlp
) {
1307 vlan_parent_release(new_vlp
);
1313 vlan_link_event(struct ifnet
* ifp
, struct ifnet
* p
)
1315 struct ifmediareq ifmr
;
1317 /* generate a link event based on the state of the underlying interface */
1318 bzero(&ifmr
, sizeof(ifmr
));
1319 snprintf(ifmr
.ifm_name
, sizeof(ifmr
.ifm_name
),
1320 "%s%d", p
->if_name
, p
->if_unit
);
1321 if ((*p
->if_ioctl
)(p
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) == 0
1322 && ifmr
.ifm_count
> 0 && ifmr
.ifm_status
& IFM_AVALID
) {
1325 event
= (ifmr
.ifm_status
& IFM_ACTIVE
)
1326 ? KEV_DL_LINK_ON
: KEV_DL_LINK_OFF
;
1327 interface_link_event(ifp
, event
);
1333 vlan_unconfig(struct ifnet
* ifp
)
1336 struct ifaddr
* ifa
;
1339 int need_vlp_release
= 0;
1341 struct sockaddr_dl
*sdl
;
1342 vlan_parent_ref vlp
;
1344 vlan_assert_lock_held();
1345 ifv
= (ifvlan_ref
)ifp
->if_private
;
1353 vlan_parent_retain(vlp
);
1354 vlan_parent_wait(vlp
, "vlan_unconfig");
1356 /* check again because another thread could be in vlan_unconfig */
1357 ifv
= (ifvlan_ref
)ifp
->if_private
;
1361 if (ifv
->ifv_vlp
!= vlp
) {
1362 /* vlan parent changed */
1368 /* remember whether we're the last VLAN on the parent */
1369 if (LIST_NEXT(LIST_FIRST(&vlp
->vlp_vlan_list
), ifv_vlan_list
) == NULL
) {
1370 if (g_vlan
->verbose
) {
1371 printf("vlan_unconfig: last vlan on %s%d\n",
1372 p
->if_name
, p
->if_unit
);
1377 /* back-out any effect our mtu might have had on the parent */
1378 (void)vlan_new_mtu(ifp
, ETHERMTU
- ifv
->ifv_mtufudge
);
1382 /* detach VLAN "protocol" */
1384 (void)vlan_detach_protocol(p
);
1387 /* un-join multicast on parent interface */
1388 (void)multicast_list_remove(&ifv
->ifv_multicast
);
1392 /* Disconnect from parent. */
1393 vlan_parent_remove_vlan(vlp
, ifv
);
1395 /* return to the state we were in before SIFVLAN */
1397 ifp
->if_flags
&= ~(IFF_BROADCAST
| IFF_MULTICAST
1398 | IFF_SIMPLEX
| IFF_RUNNING
);
1399 ifp
->if_hwassist
= 0;
1401 ifv
->ifv_mtufudge
= 0;
1403 /* Clear our MAC address. */
1404 ifa
= ifaddr_byindex(ifp
->if_index
);
1405 sdl
= (struct sockaddr_dl
*)(ifa
->ifa_addr
);
1406 sdl
->sdl_type
= IFT_L2VLAN
;
1408 bzero(LLADDR(sdl
), ETHER_ADDR_LEN
);
1410 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1411 /* the vlan parent has no more VLAN's */
1412 ifnet_lock_exclusive(p
);
1413 p
->if_eflags
&= ~IFEF_VLAN
;
1415 LIST_REMOVE(vlp
, vlp_parent_list
);
1416 /* release outside of the lock below */
1421 vlan_parent_signal(vlp
, "vlan_unconfig");
1423 vlan_parent_release(vlp
); /* one because we waited */
1425 while (need_vlp_release
--) {
1426 vlan_parent_release(vlp
);
1433 vlan_set_promisc(struct ifnet
* ifp
)
1437 vlan_parent_ref vlp
;
1440 ifv
= (ifvlan_ref
)ifp
->if_private
;
1441 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1442 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1450 if ((ifp
->if_flags
& IFF_PROMISC
) != 0) {
1451 if (!ifvlan_flags_promisc(ifv
)) {
1452 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 1);
1454 ifvlan_flags_set_promisc(ifv
);
1458 if (ifvlan_flags_promisc(ifv
)) {
1459 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 0);
1461 ifvlan_flags_clear_promisc(ifv
);
1471 vlan_new_mtu(struct ifnet
* ifp
, int mtu
)
1473 struct ifdevmtu
* devmtu_p
;
1479 vlan_parent_ref vlp
;
1481 vlan_assert_lock_held();
1482 ifv
= (ifvlan_ref
)ifp
->if_private
;
1484 devmtu_p
= &vlp
->vlp_devmtu
;
1485 req_mtu
= mtu
+ ifv
->ifv_mtufudge
;
1486 if (req_mtu
> devmtu_p
->ifdm_max
|| req_mtu
< devmtu_p
->ifdm_min
) {
1489 max_mtu
= vlan_parent_find_max_mtu(vlp
, ifv
);
1490 if (req_mtu
> max_mtu
) {
1493 else if (max_mtu
< devmtu_p
->ifdm_current
) {
1497 struct ifnet
* p
= vlp
->vlp_ifp
;
1499 error
= siocsifaltmtu(p
, new_mtu
);
1504 devmtu_p
->ifdm_current
= new_mtu
;
1512 vlan_set_mtu(struct ifnet
* ifp
, int mtu
)
1516 vlan_parent_ref vlp
;
1518 if (mtu
< IF_MINMTU
) {
1522 ifv
= (ifvlan_ref
)ifp
->if_private
;
1523 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1525 return ((ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
);
1528 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1535 vlan_parent_retain(vlp
);
1536 vlan_parent_wait(vlp
, "vlan_set_mtu");
1538 /* check again, something might have changed */
1539 ifv
= (ifvlan_ref
)ifp
->if_private
;
1540 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1541 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1544 if (ifv
->ifv_vlp
!= vlp
) {
1545 /* vlan parent changed */
1548 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1554 error
= vlan_new_mtu(ifp
, mtu
);
1557 vlan_parent_signal(vlp
, "vlan_set_mtu");
1559 vlan_parent_release(vlp
);
1565 vlan_ioctl(ifnet_t ifp
, u_int32_t cmd
, void * data
)
1567 struct ifdevmtu
* devmtu_p
;
1569 struct ifaddr
* ifa
;
1570 struct ifmediareq64
* ifmr
;
1575 user_addr_t user_addr
;
1576 vlan_parent_ref vlp
;
1579 if (ifp
->if_type
!= IFT_L2VLAN
) {
1580 return (EOPNOTSUPP
);
1582 ifr
= (struct ifreq
*)data
;
1583 ifa
= (struct ifaddr
*)data
;
1587 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
1590 case SIOCGIFMEDIA64
:
1593 ifv
= (ifvlan_ref
)ifp
->if_private
;
1594 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1596 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1598 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1600 ifmr
= (struct ifmediareq64
*)data
;
1601 user_addr
= (cmd
== SIOCGIFMEDIA64
)
1602 ? ifmr
->ifm_ifmu
.ifmu_ulist64
1603 : CAST_USER_ADDR_T(ifmr
->ifm_ifmu
.ifmu_ulist32
);
1605 struct ifmediareq64 p_ifmr
;
1607 bzero(&p_ifmr
, sizeof(p_ifmr
));
1608 error
= dlil_ioctl(0, p
, SIOCGIFMEDIA
, (caddr_t
)&p_ifmr
);
1610 ifmr
->ifm_active
= p_ifmr
.ifm_active
;
1611 ifmr
->ifm_current
= p_ifmr
.ifm_current
;
1612 ifmr
->ifm_mask
= p_ifmr
.ifm_mask
;
1613 ifmr
->ifm_status
= p_ifmr
.ifm_status
;
1614 ifmr
->ifm_count
= p_ifmr
.ifm_count
;
1615 /* Limit the result to the parent's current config. */
1616 if (ifmr
->ifm_count
>= 1 && user_addr
!= USER_ADDR_NULL
) {
1617 ifmr
->ifm_count
= 1;
1618 error
= copyout(&ifmr
->ifm_current
, user_addr
,
1623 ifmr
->ifm_active
= ifmr
->ifm_current
= IFM_NONE
;
1625 ifmr
->ifm_status
= IFM_AVALID
;
1626 ifmr
->ifm_count
= 1;
1627 if (user_addr
!= USER_ADDR_NULL
) {
1628 error
= copyout(&ifmr
->ifm_current
, user_addr
, sizeof(int));
1639 ifv
= (ifvlan_ref
)ifp
->if_private
;
1640 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1642 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1646 int min_mtu
= vlp
->vlp_devmtu
.ifdm_min
- ifv
->ifv_mtufudge
;
1647 devmtu_p
= &ifr
->ifr_devmtu
;
1648 devmtu_p
->ifdm_current
= ifp
->if_mtu
;
1649 devmtu_p
->ifdm_min
= max(min_mtu
, IF_MINMTU
);
1650 devmtu_p
->ifdm_max
= vlp
->vlp_devmtu
.ifdm_max
- ifv
->ifv_mtufudge
;
1653 devmtu_p
= &ifr
->ifr_devmtu
;
1654 devmtu_p
->ifdm_current
= 0;
1655 devmtu_p
->ifdm_min
= 0;
1656 devmtu_p
->ifdm_max
= 0;
1662 error
= vlan_set_mtu(ifp
, ifr
->ifr_mtu
);
1666 user_addr
= proc_is64bit(current_proc())
1667 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1668 error
= copyin(user_addr
, &vlr
, sizeof(vlr
));
1673 if (vlr
.vlr_parent
[0] != '\0') {
1674 if (vlr
.vlr_tag
& ~EVL_VLID_MASK
) {
1676 * Don't let the caller set up a VLAN tag with
1677 * anything except VLID bits.
1682 p
= ifunit(vlr
.vlr_parent
);
1687 /* can't do VLAN over anything but ethernet or ethernet aggregate */
1688 if (p
->if_type
!= IFT_ETHER
&& p
->if_type
!= IFT_IEEE8023ADLAG
) {
1689 error
= EPROTONOSUPPORT
;
1692 error
= vlan_config(ifp
, p
, vlr
.vlr_tag
);
1697 /* Update promiscuous mode, if necessary. */
1698 (void)vlan_set_promisc(ifp
);
1700 /* generate a link event based on the state of the parent */
1701 vlan_link_event(ifp
, p
);
1704 ifv
= (ifvlan_ref
)ifp
->if_private
;
1705 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1707 error
= (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1710 error
= vlan_unconfig(ifp
);
1713 interface_link_event(ifp
, KEV_DL_LINK_OFF
);
1719 bzero(&vlr
, sizeof vlr
);
1721 ifv
= (ifvlan_ref
)ifp
->if_private
;
1722 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1724 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1726 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1730 snprintf(vlr
.vlr_parent
, sizeof(vlr
.vlr_parent
),
1731 "%s%d", p
->if_name
, p
->if_unit
);
1734 user_addr
= proc_is64bit(current_proc())
1735 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1736 error
= copyout(&vlr
, user_addr
, sizeof(vlr
));
1741 * For promiscuous mode, we enable promiscuous mode on
1742 * the parent if we need promiscuous on the VLAN interface.
1744 error
= vlan_set_promisc(ifp
);
1749 error
= vlan_setmulti(ifp
);
1758 vlan_if_free(struct ifnet
* ifp
)
1766 ifv
= (ifvlan_ref
)ifp
->if_private
;
1771 ifp
->if_private
= NULL
;
1773 dlil_if_release(ifp
);
1778 vlan_event(struct ifnet
* p
, struct kev_msg
* event
)
1780 vlan_parent_ref vlp
;
1782 /* Check if the interface we are attached to is being detached */
1783 if (event
->vendor_code
!= KEV_VENDOR_APPLE
1784 || event
->kev_class
!= KEV_NETWORK_CLASS
1785 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
1788 switch (event
->event_code
) {
1789 case KEV_DL_IF_DETACHING
:
1790 case KEV_DL_LINK_OFF
:
1791 case KEV_DL_LINK_ON
:
1797 if ((p
->if_eflags
& IFEF_VLAN
) == 0) {
1802 vlp
= parent_list_lookup(p
);
1808 switch (event
->event_code
) {
1809 case KEV_DL_IF_DETACHING
:
1810 vlan_parent_flags_set_detaching(vlp
);
1811 vlan_parent_remove_all_vlans(vlp
);
1814 case KEV_DL_LINK_OFF
:
1815 case KEV_DL_LINK_ON
:
1816 vlan_parent_link_event(vlp
, event
->event_code
);
1826 interface_link_event(struct ifnet
* ifp
, u_long event_code
)
1829 struct kern_event_msg header
;
1831 char if_name
[IFNAMSIZ
];
1834 event
.header
.total_size
= sizeof(event
);
1835 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
1836 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
1837 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
1838 event
.header
.event_code
= event_code
;
1839 event
.header
.event_data
[0] = ifp
->if_family
;
1840 event
.unit
= (u_long
) ifp
->if_unit
;
1841 strncpy(event
.if_name
, ifp
->if_name
, IFNAMSIZ
);
1842 dlil_event(ifp
, &event
.header
);
1847 vlan_parent_link_event(vlan_parent_ref vlp
, u_long event_code
)
1851 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
1852 interface_link_event(ifv
->ifv_ifp
, event_code
);
1859 * Function: vlan_attach_protocol
1861 * Attach a DLIL protocol to the interface, using the ETHERTYPE_VLAN
1864 * The ethernet demux actually special cases VLAN to support hardware.
1865 * The demux here isn't used. The demux will return PF_VLAN for the
1866 * appropriate packets and our vlan_input function will be called.
1869 vlan_attach_protocol(struct ifnet
*ifp
)
1872 struct dlil_proto_reg_str reg
;
1874 bzero(®
, sizeof(reg
));
1875 TAILQ_INIT(®
.demux_desc_head
);
1876 reg
.interface_family
= ifp
->if_family
;
1877 reg
.unit_number
= ifp
->if_unit
;
1878 reg
.input
= vlan_input
;
1879 reg
.event
= vlan_event
;
1880 reg
.protocol_family
= PF_VLAN
;
1881 error
= dlil_attach_protocol(®
);
1883 printf("vlan_proto_attach(%s%d) dlil_attach_protocol failed, %d\n",
1884 ifp
->if_name
, ifp
->if_unit
, error
);
1890 * Function: vlan_detach_protocol
1892 * Detach our DLIL protocol from an interface
1895 vlan_detach_protocol(struct ifnet
*ifp
)
1899 error
= dlil_detach_protocol(ifp
, PF_VLAN
);
1901 printf("vlan_proto_detach(%s%d) dlil_detach_protocol failed, %d\n",
1902 ifp
->if_name
, ifp
->if_unit
, error
);
1909 * DLIL interface family functions
1910 * We use the ethernet dlil functions, since that's all we support.
1911 * If we wanted to handle multiple LAN types (tokenring, etc.), we'd
1912 * call the appropriate routines for that LAN type instead of hard-coding
1915 extern int ether_add_if(struct ifnet
*ifp
);
1916 extern int ether_del_if(struct ifnet
*ifp
);
1917 extern int ether_init_if(struct ifnet
*ifp
);
1918 extern int ether_add_proto_old(struct ifnet
*ifp
, u_long protocol_family
,
1919 struct ddesc_head_str
*desc_head
);
1921 extern int ether_attach_inet(struct ifnet
*ifp
, u_long protocol_family
);
1922 extern int ether_detach_inet(struct ifnet
*ifp
, u_long protocol_family
);
1923 extern int ether_attach_inet6(struct ifnet
*ifp
, u_long protocol_family
);
1924 extern int ether_detach_inet6(struct ifnet
*ifp
, u_long protocol_family
);
1927 vlan_attach_inet(struct ifnet
*ifp
, u_long protocol_family
)
1929 return (ether_attach_inet(ifp
, protocol_family
));
1933 vlan_detach_inet(struct ifnet
*ifp
, u_long protocol_family
)
1935 return (ether_detach_inet(ifp
, protocol_family
));
1939 vlan_attach_inet6(struct ifnet
*ifp
, u_long protocol_family
)
1941 return (ether_attach_inet6(ifp
, protocol_family
));
1945 vlan_detach_inet6(struct ifnet
*ifp
, u_long protocol_family
)
1947 return (ether_detach_inet6(ifp
, protocol_family
));
1951 vlan_add_if(struct ifnet
*ifp
)
1953 return (ether_add_if(ifp
));
1957 vlan_del_if(struct ifnet
*ifp
)
1959 return (ether_del_if(ifp
));
1963 __private_extern__
int
1964 vlan_family_init(void)
1967 struct dlil_ifmod_reg_str ifmod_reg
;
1969 bzero(&ifmod_reg
, sizeof(ifmod_reg
));
1970 ifmod_reg
.add_if
= vlan_add_if
;
1971 ifmod_reg
.del_if
= vlan_del_if
;
1972 ifmod_reg
.init_if
= NULL
;
1973 ifmod_reg
.add_proto
= ether_add_proto_old
;
1974 ifmod_reg
.del_proto
= ether_del_proto
;
1975 ifmod_reg
.ifmod_ioctl
= ether_ioctl
;
1976 ifmod_reg
.shutdown
= NULL
;
1978 if (dlil_reg_if_modules(APPLE_IF_FAM_VLAN
, &ifmod_reg
)) {
1979 printf("WARNING: vlan_family_init -- "
1980 "Can't register if family modules\n");
1985 error
= dlil_reg_proto_module(PF_INET
, APPLE_IF_FAM_VLAN
,
1986 vlan_attach_inet
, vlan_detach_inet
);
1988 printf("dlil_reg_proto_module failed for AF_INET error=%d\n",
1992 error
= dlil_reg_proto_module(PF_INET6
, APPLE_IF_FAM_VLAN
,
1993 vlan_attach_inet6
, vlan_detach_inet6
);
1995 printf("dlil_reg_proto_module failed for AF_INET6 error=%d\n",
1999 vlan_clone_attach();