2 * Copyright (c) 2006 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 lck_grp_attr_setdefault(grp_attrs
);
125 grp
= lck_grp_alloc_init(grp_name
, grp_attrs
);
126 lck_grp_attr_free(grp_attrs
);
130 static __inline__ lck_mtx_t
*
131 my_lck_mtx_alloc_init(lck_grp_t
* lck_grp
)
133 lck_attr_t
* lck_attrs
;
136 lck_attrs
= lck_attr_alloc_init();
137 lck_attr_setdefault(lck_attrs
);
138 lck_mtx
= lck_mtx_alloc_init(lck_grp
, lck_attrs
);
139 lck_attr_free(lck_attrs
);
143 static lck_mtx_t
* vlan_lck_mtx
;
145 static __inline__
void
148 lck_grp_t
* vlan_lck_grp
;
150 vlan_lck_grp
= my_lck_grp_alloc_init("if_vlan");
151 vlan_lck_mtx
= my_lck_mtx_alloc_init(vlan_lck_grp
);
154 static __inline__
void
155 vlan_assert_lock_held(void)
157 lck_mtx_assert(vlan_lck_mtx
, LCK_MTX_ASSERT_OWNED
);
161 static __inline__
void
162 vlan_assert_lock_not_held(void)
164 lck_mtx_assert(vlan_lck_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
168 static __inline__
void
171 lck_mtx_lock(vlan_lck_mtx
);
175 static __inline__
void
178 lck_mtx_unlock(vlan_lck_mtx
);
183 ** vlan structures, types
186 LIST_HEAD(vlan_parent_list
, vlan_parent
);
188 LIST_HEAD(ifvlan_list
, ifvlan
);
190 typedef struct vlan_parent
{
191 LIST_ENTRY(vlan_parent
) vlp_parent_list
;/* list of parents */
192 struct ifnet
* vlp_ifp
; /* interface */
193 struct ifvlan_list vlp_vlan_list
; /* list of VLAN's */
194 #define VLPF_SUPPORTS_VLAN_MTU 0x1
195 #define VLPF_CHANGE_IN_PROGRESS 0x2
196 #define VLPF_DETACHING 0x4
198 struct ifdevmtu vlp_devmtu
;
199 UInt32 vlp_retain_count
;
200 } vlan_parent
, * vlan_parent_ref
;
203 LIST_ENTRY(ifvlan
) ifv_vlan_list
;
204 char ifv_name
[IFNAMSIZ
]; /* our unique id */
205 struct ifnet
* ifv_ifp
; /* our interface */
206 vlan_parent_ref ifv_vlp
; /* parent information */
208 u_int16_t ifvm_encaplen
;/* encapsulation length */
209 u_int16_t ifvm_mtufudge
;/* MTU fudged by this much */
210 u_int16_t ifvm_proto
; /* encapsulation ethertype */
211 u_int16_t ifvm_tag
; /* tag to apply on packets leaving if */
213 struct multicast_list ifv_multicast
;
214 #define IFVF_PROMISC 0x1 /* promiscuous mode enabled */
215 #define IFVF_DETACHING 0x2 /* interface is detaching */
216 #define IFVF_READY 0x4 /* interface is ready */
218 bpf_packet_func ifv_bpf_input
;
219 bpf_packet_func ifv_bpf_output
;
222 typedef struct ifvlan
* ifvlan_ref
;
224 typedef struct vlan_globals_s
{
225 struct vlan_parent_list parent_list
;
227 } * vlan_globals_ref
;
229 static vlan_globals_ref g_vlan
;
231 #define ifv_tag ifv_mib.ifvm_tag
232 #define ifv_encaplen ifv_mib.ifvm_encaplen
233 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
237 ** vlan_parent_ref vlp_flags in-lines
239 static __inline__
int
240 vlan_parent_flags_supports_vlan_mtu(vlan_parent_ref vlp
)
242 return ((vlp
->vlp_flags
& VLPF_SUPPORTS_VLAN_MTU
) != 0);
245 static __inline__
void
246 vlan_parent_flags_set_supports_vlan_mtu(vlan_parent_ref vlp
)
248 vlp
->vlp_flags
|= VLPF_SUPPORTS_VLAN_MTU
;
252 static __inline__
void
253 vlan_parent_flags_clear_supports_vlan_mtu(vlan_parent_ref vlp
)
255 vlp
->vlp_flags
&= ~VLPF_SUPPORTS_VLAN_MTU
;
259 static __inline__
int
260 vlan_parent_flags_change_in_progress(vlan_parent_ref vlp
)
262 return ((vlp
->vlp_flags
& VLPF_CHANGE_IN_PROGRESS
) != 0);
265 static __inline__
void
266 vlan_parent_flags_set_change_in_progress(vlan_parent_ref vlp
)
268 vlp
->vlp_flags
|= VLPF_CHANGE_IN_PROGRESS
;
272 static __inline__
void
273 vlan_parent_flags_clear_change_in_progress(vlan_parent_ref vlp
)
275 vlp
->vlp_flags
&= ~VLPF_CHANGE_IN_PROGRESS
;
279 static __inline__
int
280 vlan_parent_flags_detaching(struct vlan_parent
* vlp
)
282 return ((vlp
->vlp_flags
& VLPF_DETACHING
) != 0);
285 static __inline__
void
286 vlan_parent_flags_set_detaching(struct vlan_parent
* vlp
)
288 vlp
->vlp_flags
|= VLPF_DETACHING
;
294 ** ifvlan_flags in-lines routines
296 static __inline__
int
297 ifvlan_flags_promisc(ifvlan_ref ifv
)
299 return ((ifv
->ifv_flags
& IFVF_PROMISC
) != 0);
302 static __inline__
void
303 ifvlan_flags_set_promisc(ifvlan_ref ifv
)
305 ifv
->ifv_flags
|= IFVF_PROMISC
;
309 static __inline__
void
310 ifvlan_flags_clear_promisc(ifvlan_ref ifv
)
312 ifv
->ifv_flags
&= ~IFVF_PROMISC
;
316 static __inline__
int
317 ifvlan_flags_ready(ifvlan_ref ifv
)
319 return ((ifv
->ifv_flags
& IFVF_READY
) != 0);
322 static __inline__
void
323 ifvlan_flags_set_ready(ifvlan_ref ifv
)
325 ifv
->ifv_flags
|= IFVF_READY
;
329 static __inline__
void
330 ifvlan_flags_clear_ready(ifvlan_ref ifv
)
332 ifv
->ifv_flags
&= ~IFVF_READY
;
336 static __inline__
int
337 ifvlan_flags_detaching(ifvlan_ref ifv
)
339 return ((ifv
->ifv_flags
& IFVF_DETACHING
) != 0);
342 static __inline__
void
343 ifvlan_flags_set_detaching(ifvlan_ref ifv
)
345 ifv
->ifv_flags
|= IFVF_DETACHING
;
350 SYSCTL_DECL(_net_link
);
351 SYSCTL_NODE(_net_link
, IFT_L2VLAN
, vlan
, CTLFLAG_RW
, 0, "IEEE 802.1Q VLAN");
352 SYSCTL_NODE(_net_link_vlan
, PF_LINK
, link
, CTLFLAG_RW
, 0, "for consistency");
355 #define M_VLAN M_DEVBUF
357 static int vlan_clone_create(struct if_clone
*, int);
358 static void vlan_clone_destroy(struct ifnet
*);
359 static int vlan_input(struct mbuf
*m
, char *frame_header
, struct ifnet
*ifp
,
360 u_long protocol_family
, int sync_ok
);
361 static int vlan_output(struct ifnet
*ifp
, struct mbuf
*m
);
362 static int vlan_ioctl(ifnet_t ifp
, u_int32_t cmd
, void * addr
);
363 static int vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
,
364 bpf_packet_func func
);
365 static int vlan_attach_protocol(struct ifnet
*ifp
);
366 static int vlan_detach_protocol(struct ifnet
*ifp
);
367 static int vlan_setmulti(struct ifnet
*ifp
);
368 static int vlan_unconfig(struct ifnet
*ifp
);
369 static int vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
);
370 static void vlan_if_free(struct ifnet
* ifp
);
371 static void vlan_remove(ifvlan_ref ifv
);
372 static void vlan_if_detach(struct ifnet
* ifp
);
373 static int vlan_new_mtu(struct ifnet
* ifp
, int mtu
);
375 static struct if_clone vlan_cloner
= IF_CLONE_INITIALIZER(VLANNAME
,
380 static void interface_link_event(struct ifnet
* ifp
, u_long event_code
);
381 static void vlan_parent_link_event(vlan_parent_ref vlp
,
383 extern int dlil_input_packet(struct ifnet
*ifp
, struct mbuf
*m
, char *frame_header
);
386 vlan_globals_init(void)
390 vlan_assert_lock_not_held();
392 if (g_vlan
!= NULL
) {
395 v
= _MALLOC(sizeof(*v
), M_VLAN
, M_WAITOK
);
397 LIST_INIT(&v
->parent_list
);
401 if (g_vlan
!= NULL
) {
417 siocgifdevmtu(struct ifnet
* ifp
, struct ifdevmtu
* ifdm_p
)
422 bzero(&ifr
, sizeof(ifr
));
423 error
= dlil_ioctl(0, ifp
, SIOCGIFDEVMTU
, (caddr_t
)&ifr
);
425 *ifdm_p
= ifr
.ifr_devmtu
;
431 siocsifaltmtu(struct ifnet
* ifp
, int mtu
)
435 bzero(&ifr
, sizeof(ifr
));
437 return (dlil_ioctl(0, ifp
, SIOCSIFALTMTU
, (caddr_t
)&ifr
));
440 static __inline__
void
441 vlan_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
442 bpf_packet_func func
)
450 static __inline__
void
451 vlan_bpf_input(struct ifnet
* ifp
, struct mbuf
* m
,
452 bpf_packet_func func
, char * frame_header
,
453 int frame_header_len
, int encap_len
)
457 /* present the right header to bpf */
458 bcopy(frame_header
, frame_header
+ encap_len
, frame_header_len
);
460 m
->m_data
-= frame_header_len
;
461 m
->m_len
+= frame_header_len
;
463 m
->m_data
+= frame_header_len
;
464 m
->m_len
-= frame_header_len
;
466 /* restore the header */
467 bcopy(frame_header
+ encap_len
, frame_header
, frame_header_len
);
473 static struct ifaddr
*
474 ifaddr_byindex(int i
)
476 if (i
> if_index
|| i
== 0) {
479 return (ifnet_addrs
[i
- 1]);
483 ** vlan_parent synchronization routines
485 static __inline__
void
486 vlan_parent_retain(vlan_parent_ref vlp
)
488 OSIncrementAtomic(&vlp
->vlp_retain_count
);
491 static __inline__
void
492 vlan_parent_release(vlan_parent_ref vlp
)
494 UInt32 old_retain_count
;
496 old_retain_count
= OSDecrementAtomic(&vlp
->vlp_retain_count
);
497 switch (old_retain_count
) {
499 panic("vlan_parent_release: retain count is 0\n");
502 if (g_vlan
->verbose
) {
503 struct ifnet
* ifp
= vlp
->vlp_ifp
;
504 printf("vlan_parent_release(%s%d)\n", ifp
->if_name
,
516 * Function: vlan_parent_wait
518 * Allows a single thread to gain exclusive access to the vlan_parent
519 * data structure. Some operations take a long time to complete,
520 * and some have side-effects that we can't predict. Holding the
521 * vlan_lock() across such operations is not possible.
524 * Before calling, you must be holding the vlan_lock and have taken
525 * a reference on the vlan_parent_ref.
528 vlan_parent_wait(vlan_parent_ref vlp
, const char * msg
)
532 /* other add/remove/multicast-change in progress */
533 while (vlan_parent_flags_change_in_progress(vlp
)) {
534 if (g_vlan
->verbose
) {
535 struct ifnet
* ifp
= vlp
->vlp_ifp
;
537 printf("%s%d: %s msleep\n", ifp
->if_name
, ifp
->if_unit
, msg
);
540 (void)msleep(vlp
, vlan_lck_mtx
, PZERO
, msg
, 0);
542 /* prevent other vlan parent remove/add from taking place */
543 vlan_parent_flags_set_change_in_progress(vlp
);
544 if (g_vlan
->verbose
&& waited
) {
545 struct ifnet
* ifp
= vlp
->vlp_ifp
;
547 printf("%s: %s woke up\n", ifp
->if_name
, ifp
->if_unit
, msg
);
553 * Function: vlan_parent_signal
555 * Allows the thread that previously invoked vlan_parent_wait() to
556 * give up exclusive access to the vlan_parent data structure, and wake up
557 * any other threads waiting to access
559 * Before calling, you must be holding the vlan_lock and have taken
560 * a reference on the vlan_parent_ref.
563 vlan_parent_signal(vlan_parent_ref vlp
, const char * msg
)
565 vlan_parent_flags_clear_change_in_progress(vlp
);
566 wakeup((caddr_t
)vlp
);
567 if (g_vlan
->verbose
) {
568 struct ifnet
* ifp
= vlp
->vlp_ifp
;
570 printf("%s%d: %s wakeup\n", ifp
->if_name
, ifp
->if_unit
, msg
);
577 * Program our multicast filter. What we're actually doing is
578 * programming the multicast filter of the parent. This has the
579 * side effect of causing the parent interface to receive multicast
580 * traffic that it doesn't really want, which ends up being discarded
581 * later by the upper protocol layers. Unfortunately, there's no way
582 * to avoid this: there really is only one physical interface.
585 vlan_setmulti(struct ifnet
* ifp
)
593 ifv
= (ifvlan_ref
)ifp
->if_private
;
594 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
599 /* no parent, no need to program the multicast filter */
602 if (vlan_parent_flags_detaching(vlp
)) {
605 vlan_parent_retain(vlp
);
606 vlan_parent_wait(vlp
, "vlan_setmulti");
608 /* check again, things could have changed */
609 ifv
= (ifvlan_ref
)ifp
->if_private
;
610 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
613 if (ifv
->ifv_vlp
!= vlp
) {
614 /* vlan parent changed */
618 /* no parent, no need to program the multicast filter */
624 /* update parent interface with our multicast addresses */
625 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
630 vlan_parent_signal(vlp
, "vlan_setmulti");
638 ** vlan_parent list manipulation/lookup routines
640 static vlan_parent_ref
641 parent_list_lookup(struct ifnet
* p
)
645 LIST_FOREACH(vlp
, &g_vlan
->parent_list
, vlp_parent_list
) {
646 if (vlp
->vlp_ifp
== p
) {
654 vlan_parent_lookup_tag(vlan_parent_ref vlp
, int tag
)
658 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
659 if (tag
== ifv
->ifv_tag
) {
667 vlan_lookup_parent_and_tag(struct ifnet
* p
, int tag
)
671 vlp
= parent_list_lookup(p
);
673 return (vlan_parent_lookup_tag(vlp
, tag
));
679 vlan_parent_find_max_mtu(vlan_parent_ref vlp
, ifvlan_ref exclude_ifv
)
684 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
687 if (exclude_ifv
== ifv
) {
690 req_mtu
= ifv
->ifv_ifp
->if_mtu
+ ifv
->ifv_mtufudge
;
691 if (req_mtu
> max_mtu
) {
699 * Function: vlan_parent_create
701 * Create a vlan_parent structure to hold the VLAN's for the given
702 * interface. Add it to the list of VLAN parents.
705 vlan_parent_create(struct ifnet
* p
, vlan_parent_ref
* ret_vlp
)
711 vlp
= _MALLOC(sizeof(*vlp
), M_VLAN
, M_WAITOK
);
715 bzero(vlp
, sizeof(*vlp
));
716 error
= siocgifdevmtu(p
, &vlp
->vlp_devmtu
);
718 printf("vlan_parent_create (%s%d): siocgifdevmtu failed, %d\n",
719 p
->if_name
, p
->if_unit
, error
);
723 LIST_INIT(&vlp
->vlp_vlan_list
);
725 vlan_parent_retain(vlp
);
727 & (IF_HWASSIST_VLAN_MTU
| IF_HWASSIST_VLAN_TAGGING
)) {
728 vlan_parent_flags_set_supports_vlan_mtu(vlp
);
735 vlan_parent_remove_all_vlans(vlan_parent_ref vlp
)
740 vlan_assert_lock_held();
742 while ((ifv
= LIST_FIRST(&vlp
->vlp_vlan_list
)) != NULL
) {
745 vlan_if_detach(ifv
->ifv_ifp
);
749 /* the vlan parent has no more VLAN's */
751 ifnet_lock_exclusive(p
);
752 p
->if_eflags
&= ~IFEF_VLAN
;
754 LIST_REMOVE(vlp
, vlp_parent_list
);
756 vlan_parent_release(vlp
);
762 static __inline__
int
763 vlan_parent_no_vlans(vlan_parent_ref vlp
)
765 return (LIST_EMPTY(&vlp
->vlp_vlan_list
));
769 vlan_parent_add_vlan(vlan_parent_ref vlp
, ifvlan_ref ifv
, int tag
)
771 LIST_INSERT_HEAD(&vlp
->vlp_vlan_list
, ifv
, ifv_vlan_list
);
778 vlan_parent_remove_vlan(__unused vlan_parent_ref vlp
, ifvlan_ref ifv
)
781 LIST_REMOVE(ifv
, ifv_vlan_list
);
786 vlan_clone_attach(void)
788 if_clone_attach(&vlan_cloner
);
794 vlan_clone_create(struct if_clone
*ifc
, int unit
)
800 error
= vlan_globals_init();
804 ifv
= _MALLOC(sizeof(struct ifvlan
), M_VLAN
, M_WAITOK
);
805 bzero(ifv
, sizeof(struct ifvlan
));
806 multicast_list_init(&ifv
->ifv_multicast
);
808 /* use the interface name as the unique id for ifp recycle */
809 if ((unsigned int)snprintf(ifv
->ifv_name
, sizeof(ifv
->ifv_name
), "%s%d",
810 ifc
->ifc_name
, unit
) >= sizeof(ifv
->ifv_name
)) {
814 error
= dlil_if_acquire(APPLE_IF_FAM_VLAN
,
816 strlen(ifv
->ifv_name
),
822 ifp
->if_name
= ifc
->ifc_name
;
824 ifp
->if_family
= APPLE_IF_FAM_VLAN
;
827 /* NB: flags are not set here */
828 ifp
->if_linkmib
= &ifv
->ifv_mib
;
829 ifp
->if_linkmiblen
= sizeof ifv
->ifv_mib
;
830 /* NB: mtu is not set here */
833 ifp
->if_ioctl
= vlan_ioctl
;
834 ifp
->if_set_bpf_tap
= vlan_set_bpf_tap
;
835 ifp
->if_free
= vlan_if_free
;
836 ifp
->if_output
= vlan_output
;
837 ifp
->if_hwassist
= 0;
838 ifp
->if_addrlen
= ETHER_ADDR_LEN
; /* XXX ethernet specific */
839 ifp
->if_baudrate
= 0;
840 ifp
->if_type
= IFT_L2VLAN
;
841 ifp
->if_hdrlen
= ETHER_VLAN_ENCAP_LEN
;
843 /* XXX ethernet specific */
844 ifp
->if_broadcast
.length
= ETHER_ADDR_LEN
;
845 bcopy(etherbroadcastaddr
, ifp
->if_broadcast
.u
.buffer
, ETHER_ADDR_LEN
);
847 error
= dlil_if_attach(ifp
);
849 dlil_if_release(ifp
);
853 ifp
->if_private
= ifv
;
856 /* attach as ethernet */
857 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
862 vlan_remove(ifvlan_ref ifv
)
864 vlan_assert_lock_held();
865 ifvlan_flags_set_detaching(ifv
);
866 vlan_unconfig(ifv
->ifv_ifp
);
871 vlan_if_detach(struct ifnet
* ifp
)
873 if (dlil_if_detach(ifp
) != DLIL_WAIT_FOR_FREE
) {
880 vlan_clone_destroy(struct ifnet
*ifp
)
885 ifv
= ifp
->if_private
;
886 if (ifv
== NULL
|| ifp
->if_type
!= IFT_L2VLAN
) {
890 if (ifvlan_flags_detaching(ifv
)) {
901 vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
906 ifv
= ifp
->if_private
;
907 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
912 case BPF_TAP_DISABLE
:
913 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= NULL
;
917 ifv
->ifv_bpf_input
= func
;
921 ifv
->ifv_bpf_output
= func
;
924 case BPF_TAP_INPUT_OUTPUT
:
925 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= func
;
935 vlan_output(struct ifnet
* ifp
, struct mbuf
* m
)
937 bpf_packet_func bpf_func
;
938 struct ether_vlan_header
* evl
;
949 if ((m
->m_flags
& M_PKTHDR
) == 0) {
954 ifv
= (ifvlan_ref
)ifp
->if_private
;
955 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)
956 || ifvlan_flags_ready(ifv
) == 0) {
968 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
969 soft_vlan
= (p
->if_hwassist
& IF_HWASSIST_VLAN_TAGGING
) == 0;
970 bpf_func
= ifv
->ifv_bpf_output
;
972 encaplen
= ifv
->ifv_encaplen
;
974 vlan_bpf_output(ifp
, m
, bpf_func
);
976 /* do not run parent's if_output() if the parent is not up */
977 if ((p
->if_flags
& (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
)) {
979 ifp
->if_collisions
++;
983 * If underlying interface can do VLAN tag insertion itself,
984 * just pass the packet along. However, we need some way to
985 * tell the interface where the packet came from so that it
986 * knows how to find the VLAN tag to use. We use a field in
987 * the mbuf header to store the VLAN tag, and a bit in the
988 * csum_flags field to mark the field as valid.
990 if (soft_vlan
== 0) {
991 m
->m_pkthdr
.csum_flags
|= CSUM_VLAN_TAG_VALID
;
992 m
->m_pkthdr
.vlan_tag
= tag
;
994 M_PREPEND(m
, encaplen
, M_DONTWAIT
);
996 printf("%s%d: unable to prepend VLAN header\n", ifp
->if_name
,
1001 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1002 if (m
->m_len
< (int)sizeof(*evl
)) {
1003 m
= m_pullup(m
, sizeof(*evl
));
1005 printf("%s%d: unable to pullup VLAN header\n", ifp
->if_name
,
1013 * Transform the Ethernet header into an Ethernet header
1014 * with 802.1Q encapsulation.
1016 bcopy(mtod(m
, char *) + encaplen
,
1017 mtod(m
, char *), ETHER_HDR_LEN
);
1018 evl
= mtod(m
, struct ether_vlan_header
*);
1019 evl
->evl_proto
= evl
->evl_encap_proto
;
1020 evl
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
1021 evl
->evl_tag
= htons(tag
);
1023 return dlil_output(p
, 0, m
, NULL
, NULL
, 1);
1027 vlan_input(struct mbuf
* m
, char * frame_header
, struct ifnet
* p
,
1028 __unused u_long protocol_family
, __unused
int sync_ok
)
1030 bpf_packet_func bpf_func
= NULL
;
1031 struct ether_vlan_header
* evl
;
1032 struct ifnet
* ifp
= NULL
;
1036 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1038 * Packet is tagged, m contains a normal
1039 * Ethernet frame; the tag is stored out-of-band.
1041 m
->m_pkthdr
.csum_flags
&= ~CSUM_VLAN_TAG_VALID
;
1042 tag
= EVL_VLANOFTAG(m
->m_pkthdr
.vlan_tag
);
1043 m
->m_pkthdr
.vlan_tag
= 0;
1046 switch (p
->if_type
) {
1048 if (m
->m_len
< ETHER_VLAN_ENCAP_LEN
) {
1052 evl
= (struct ether_vlan_header
*)frame_header
;
1053 if (ntohs(evl
->evl_proto
) == ETHERTYPE_VLAN
) {
1054 /* don't allow VLAN within VLAN */
1058 tag
= EVL_VLANOFTAG(ntohs(evl
->evl_tag
));
1061 * Restore the original ethertype. We'll remove
1062 * the encapsulation after we've found the vlan
1063 * interface corresponding to the tag.
1065 evl
->evl_encap_proto
= evl
->evl_proto
;
1068 printf("vlan_demux: unsupported if type %u",
1078 if ((p
->if_eflags
& IFEF_VLAN
) == 0) {
1079 /* don't bother looking through the VLAN list */
1084 ifv
= vlan_lookup_parent_and_tag(p
, tag
);
1089 || ifvlan_flags_ready(ifv
) == 0
1090 || (ifp
->if_flags
& IFF_UP
) == 0) {
1095 bpf_func
= ifv
->ifv_bpf_input
;
1100 * Packet had an in-line encapsulation header;
1101 * remove it. The original header has already
1102 * been fixed up above.
1104 m
->m_len
-= ETHER_VLAN_ENCAP_LEN
;
1105 m
->m_data
+= ETHER_VLAN_ENCAP_LEN
;
1106 m
->m_pkthdr
.len
-= ETHER_VLAN_ENCAP_LEN
;
1107 m
->m_pkthdr
.csum_flags
= 0; /* can't trust hardware checksum */
1110 m
->m_pkthdr
.rcvif
= ifp
;
1111 (void)ifnet_stat_increment_in(ifp
, 1,
1112 m
->m_pkthdr
.len
+ ETHER_HDR_LEN
, 0);
1113 vlan_bpf_input(ifp
, m
, bpf_func
, frame_header
, ETHER_HDR_LEN
,
1114 soft_vlan
? ETHER_VLAN_ENCAP_LEN
: 0);
1115 /* We found a vlan interface, inject on that interface. */
1116 dlil_input_packet(ifp
, m
, frame_header
);
1118 /* Send priority-tagged packet up through the parent */
1119 dlil_input_packet(p
, m
, frame_header
);
1124 #define VLAN_CONFIG_PROGRESS_VLP_RETAINED 0x1
1125 #define VLAN_CONFIG_PROGRESS_IN_LIST 0x2
1128 vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
)
1132 ifvlan_ref ifv
= NULL
;
1133 struct ifaddr
* ifa1
;
1134 struct ifaddr
* ifa2
;
1135 vlan_parent_ref new_vlp
= NULL
;
1136 int need_vlp_release
= 0;
1137 u_int32_t progress
= 0;
1138 struct sockaddr_dl
*sdl1
;
1139 struct sockaddr_dl
*sdl2
;
1140 vlan_parent_ref vlp
= NULL
;
1142 /* pre-allocate space for vlan_parent, in case we're first */
1143 error
= vlan_parent_create(p
, &new_vlp
);
1149 ifv
= (ifvlan_ref
)ifp
->if_private
;
1150 if (ifv
!= NULL
&& ifv
->ifv_vlp
!= NULL
) {
1152 vlan_parent_release(new_vlp
);
1155 vlp
= parent_list_lookup(p
);
1157 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1158 /* already a VLAN with that tag on this interface */
1164 /* we're the first VLAN on this interface */
1165 LIST_INSERT_HEAD(&g_vlan
->parent_list
, new_vlp
, vlp_parent_list
);
1169 /* need to wait to ensure no one else is trying to add/remove */
1170 vlan_parent_retain(vlp
);
1171 progress
|= VLAN_CONFIG_PROGRESS_VLP_RETAINED
;
1172 vlan_parent_wait(vlp
, "vlan_config");
1174 ifv
= (ifvlan_ref
)ifp
->if_private
;
1179 if (vlan_parent_flags_detaching(vlp
)
1180 || ifvlan_flags_detaching(ifv
) || ifv
->ifv_vlp
!= NULL
) {
1185 /* check again because someone might have gotten in */
1186 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1187 /* already a VLAN with that tag on this interface */
1192 if (vlan_parent_no_vlans(vlp
)) {
1195 vlan_parent_add_vlan(vlp
, ifv
, tag
);
1196 progress
|= VLAN_CONFIG_PROGRESS_IN_LIST
;
1198 /* check whether bond interface is using parent interface */
1199 ifnet_lock_exclusive(p
);
1200 if ((p
->if_eflags
& IFEF_BOND
) != 0) {
1202 /* don't allow VLAN over interface that's already part of a bond */
1206 /* prevent BOND interface from using it */
1207 p
->if_eflags
|= IFEF_VLAN
;
1212 /* attach our VLAN "protocol" to the interface */
1213 error
= vlan_attach_protocol(p
);
1218 /* mark the parent interface up */
1219 ifnet_lock_exclusive(p
);
1220 p
->if_flags
|= IFF_UP
;
1222 (void)dlil_ioctl(0, p
, SIOCSIFFLAGS
, (caddr_t
)NULL
);
1225 /* configure parent to receive our multicast addresses */
1226 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
1229 (void)vlan_detach_protocol(p
);
1235 /* no failures past this point */
1238 ifv
->ifv_encaplen
= ETHER_VLAN_ENCAP_LEN
;
1240 if (vlan_parent_flags_supports_vlan_mtu(vlp
)) {
1241 ifv
->ifv_mtufudge
= 0;
1244 * Fudge the MTU by the encapsulation size. This
1245 * makes us incompatible with strictly compliant
1246 * 802.1Q implementations, but allows us to use
1247 * the feature with other NetBSD implementations,
1248 * which might still be useful.
1250 ifv
->ifv_mtufudge
= ifv
->ifv_encaplen
;
1252 ifp
->if_mtu
= ETHERMTU
- ifv
->ifv_mtufudge
;
1255 * Copy only a selected subset of flags from the parent.
1256 * Other flags are none of our business.
1258 ifp
->if_flags
|= (p
->if_flags
&
1259 (IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
));
1261 * If the parent interface can do hardware-assisted
1262 * VLAN encapsulation, then propagate its hardware-
1263 * assisted checksumming flags.
1265 if (p
->if_hwassist
& IF_HWASSIST_VLAN_TAGGING
) {
1266 ifp
->if_hwassist
|= IF_HWASSIST_CSUM_FLAGS(p
->if_hwassist
);
1269 /* set our ethernet address to that of the parent */
1270 ifa1
= ifaddr_byindex(ifp
->if_index
);
1271 ifa2
= ifaddr_byindex(p
->if_index
);
1272 sdl1
= (struct sockaddr_dl
*)ifa1
->ifa_addr
;
1273 sdl2
= (struct sockaddr_dl
*)ifa2
->ifa_addr
;
1274 sdl1
->sdl_type
= IFT_ETHER
;
1275 sdl1
->sdl_alen
= ETHER_ADDR_LEN
;
1276 bcopy(LLADDR(sdl2
), LLADDR(sdl1
), ETHER_ADDR_LEN
);
1278 ifp
->if_flags
|= IFF_RUNNING
;
1279 ifvlan_flags_set_ready(ifv
);
1280 vlan_parent_signal(vlp
, "vlan_config");
1282 if (new_vlp
!= vlp
) {
1283 /* throw it away, it wasn't needed */
1284 vlan_parent_release(new_vlp
);
1289 vlan_assert_lock_held();
1290 vlan_parent_signal(vlp
, "vlan_config");
1293 if ((progress
& VLAN_CONFIG_PROGRESS_IN_LIST
) != 0) {
1294 vlan_parent_remove_vlan(vlp
, ifv
);
1296 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1297 /* the vlan parent has no more VLAN's */
1298 ifnet_lock_exclusive(p
);
1299 p
->if_eflags
&= ~IFEF_VLAN
;
1301 LIST_REMOVE(vlp
, vlp_parent_list
);
1302 /* release outside of the lock below */
1303 need_vlp_release
= 1;
1307 if ((progress
& VLAN_CONFIG_PROGRESS_VLP_RETAINED
) != 0) {
1308 vlan_parent_release(vlp
);
1310 if (need_vlp_release
) {
1311 vlan_parent_release(vlp
);
1313 if (new_vlp
!= vlp
) {
1314 vlan_parent_release(new_vlp
);
1320 vlan_link_event(struct ifnet
* ifp
, struct ifnet
* p
)
1322 struct ifmediareq ifmr
;
1324 /* generate a link event based on the state of the underlying interface */
1325 bzero(&ifmr
, sizeof(ifmr
));
1326 snprintf(ifmr
.ifm_name
, sizeof(ifmr
.ifm_name
),
1327 "%s%d", p
->if_name
, p
->if_unit
);
1328 if ((*p
->if_ioctl
)(p
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) == 0
1329 && ifmr
.ifm_count
> 0 && ifmr
.ifm_status
& IFM_AVALID
) {
1332 event
= (ifmr
.ifm_status
& IFM_ACTIVE
)
1333 ? KEV_DL_LINK_ON
: KEV_DL_LINK_OFF
;
1334 interface_link_event(ifp
, event
);
1340 vlan_unconfig(struct ifnet
* ifp
)
1343 struct ifaddr
* ifa
;
1346 int need_vlp_release
= 0;
1348 struct sockaddr_dl
*sdl
;
1349 vlan_parent_ref vlp
;
1351 vlan_assert_lock_held();
1352 ifv
= (ifvlan_ref
)ifp
->if_private
;
1360 vlan_parent_retain(vlp
);
1361 vlan_parent_wait(vlp
, "vlan_unconfig");
1363 /* check again because another thread could be in vlan_unconfig */
1364 ifv
= (ifvlan_ref
)ifp
->if_private
;
1368 if (ifv
->ifv_vlp
!= vlp
) {
1369 /* vlan parent changed */
1375 /* remember whether we're the last VLAN on the parent */
1376 if (LIST_NEXT(LIST_FIRST(&vlp
->vlp_vlan_list
), ifv_vlan_list
) == NULL
) {
1377 if (g_vlan
->verbose
) {
1378 printf("vlan_unconfig: last vlan on %s%d\n",
1379 p
->if_name
, p
->if_unit
);
1384 /* back-out any effect our mtu might have had on the parent */
1385 (void)vlan_new_mtu(ifp
, ETHERMTU
- ifv
->ifv_mtufudge
);
1389 /* detach VLAN "protocol" */
1391 (void)vlan_detach_protocol(p
);
1394 /* un-join multicast on parent interface */
1395 (void)multicast_list_remove(&ifv
->ifv_multicast
);
1399 /* Disconnect from parent. */
1400 vlan_parent_remove_vlan(vlp
, ifv
);
1402 /* return to the state we were in before SIFVLAN */
1404 ifp
->if_flags
&= ~(IFF_BROADCAST
| IFF_MULTICAST
1405 | IFF_SIMPLEX
| IFF_RUNNING
);
1406 ifp
->if_hwassist
= 0;
1408 ifv
->ifv_mtufudge
= 0;
1410 /* Clear our MAC address. */
1411 ifa
= ifaddr_byindex(ifp
->if_index
);
1412 sdl
= (struct sockaddr_dl
*)(ifa
->ifa_addr
);
1413 sdl
->sdl_type
= IFT_L2VLAN
;
1415 bzero(LLADDR(sdl
), ETHER_ADDR_LEN
);
1417 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1418 /* the vlan parent has no more VLAN's */
1419 ifnet_lock_exclusive(p
);
1420 p
->if_eflags
&= ~IFEF_VLAN
;
1422 LIST_REMOVE(vlp
, vlp_parent_list
);
1423 /* release outside of the lock below */
1428 vlan_parent_signal(vlp
, "vlan_unconfig");
1430 vlan_parent_release(vlp
); /* one because we waited */
1432 while (need_vlp_release
--) {
1433 vlan_parent_release(vlp
);
1440 vlan_set_promisc(struct ifnet
* ifp
)
1444 vlan_parent_ref vlp
;
1447 ifv
= (ifvlan_ref
)ifp
->if_private
;
1448 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1449 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1457 if ((ifp
->if_flags
& IFF_PROMISC
) != 0) {
1458 if (!ifvlan_flags_promisc(ifv
)) {
1459 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 1);
1461 ifvlan_flags_set_promisc(ifv
);
1465 if (ifvlan_flags_promisc(ifv
)) {
1466 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 0);
1468 ifvlan_flags_clear_promisc(ifv
);
1478 vlan_new_mtu(struct ifnet
* ifp
, int mtu
)
1480 struct ifdevmtu
* devmtu_p
;
1486 vlan_parent_ref vlp
;
1488 vlan_assert_lock_held();
1489 ifv
= (ifvlan_ref
)ifp
->if_private
;
1491 devmtu_p
= &vlp
->vlp_devmtu
;
1492 req_mtu
= mtu
+ ifv
->ifv_mtufudge
;
1493 if (req_mtu
> devmtu_p
->ifdm_max
|| req_mtu
< devmtu_p
->ifdm_min
) {
1496 max_mtu
= vlan_parent_find_max_mtu(vlp
, ifv
);
1497 if (req_mtu
> max_mtu
) {
1500 else if (max_mtu
< devmtu_p
->ifdm_current
) {
1504 struct ifnet
* p
= vlp
->vlp_ifp
;
1506 error
= siocsifaltmtu(p
, new_mtu
);
1511 devmtu_p
->ifdm_current
= new_mtu
;
1519 vlan_set_mtu(struct ifnet
* ifp
, int mtu
)
1523 vlan_parent_ref vlp
;
1525 if (mtu
< IF_MINMTU
) {
1529 ifv
= (ifvlan_ref
)ifp
->if_private
;
1530 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1532 return ((ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
);
1535 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1542 vlan_parent_retain(vlp
);
1543 vlan_parent_wait(vlp
, "vlan_set_mtu");
1545 /* check again, something might have changed */
1546 ifv
= (ifvlan_ref
)ifp
->if_private
;
1547 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1548 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1551 if (ifv
->ifv_vlp
!= vlp
) {
1552 /* vlan parent changed */
1555 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1561 error
= vlan_new_mtu(ifp
, mtu
);
1564 vlan_parent_signal(vlp
, "vlan_set_mtu");
1566 vlan_parent_release(vlp
);
1572 vlan_ioctl(ifnet_t ifp
, u_int32_t cmd
, void * data
)
1574 struct ifdevmtu
* devmtu_p
;
1576 struct ifaddr
* ifa
;
1577 struct ifmediareq64
* ifmr
;
1582 user_addr_t user_addr
;
1583 vlan_parent_ref vlp
;
1586 if (ifp
->if_type
!= IFT_L2VLAN
) {
1587 return (EOPNOTSUPP
);
1589 ifr
= (struct ifreq
*)data
;
1590 ifa
= (struct ifaddr
*)data
;
1594 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
1597 case SIOCGIFMEDIA64
:
1600 ifv
= (ifvlan_ref
)ifp
->if_private
;
1601 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1603 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1605 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1607 ifmr
= (struct ifmediareq64
*)data
;
1608 user_addr
= (cmd
== SIOCGIFMEDIA64
)
1609 ? ifmr
->ifm_ifmu
.ifmu_ulist64
1610 : CAST_USER_ADDR_T(ifmr
->ifm_ifmu
.ifmu_ulist32
);
1612 struct ifmediareq64 p_ifmr
;
1614 bzero(&p_ifmr
, sizeof(p_ifmr
));
1615 error
= dlil_ioctl(0, p
, SIOCGIFMEDIA
, (caddr_t
)&p_ifmr
);
1617 ifmr
->ifm_active
= p_ifmr
.ifm_active
;
1618 ifmr
->ifm_current
= p_ifmr
.ifm_current
;
1619 ifmr
->ifm_mask
= p_ifmr
.ifm_mask
;
1620 ifmr
->ifm_status
= p_ifmr
.ifm_status
;
1621 ifmr
->ifm_count
= p_ifmr
.ifm_count
;
1622 /* Limit the result to the parent's current config. */
1623 if (ifmr
->ifm_count
>= 1 && user_addr
!= USER_ADDR_NULL
) {
1624 ifmr
->ifm_count
= 1;
1625 error
= copyout(&ifmr
->ifm_current
, user_addr
,
1630 ifmr
->ifm_active
= ifmr
->ifm_current
= IFM_NONE
;
1632 ifmr
->ifm_status
= IFM_AVALID
;
1633 ifmr
->ifm_count
= 1;
1634 if (user_addr
!= USER_ADDR_NULL
) {
1635 error
= copyout(&ifmr
->ifm_current
, user_addr
, sizeof(int));
1646 ifv
= (ifvlan_ref
)ifp
->if_private
;
1647 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1649 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1653 int min_mtu
= vlp
->vlp_devmtu
.ifdm_min
- ifv
->ifv_mtufudge
;
1654 devmtu_p
= &ifr
->ifr_devmtu
;
1655 devmtu_p
->ifdm_current
= ifp
->if_mtu
;
1656 devmtu_p
->ifdm_min
= max(min_mtu
, IF_MINMTU
);
1657 devmtu_p
->ifdm_max
= vlp
->vlp_devmtu
.ifdm_max
- ifv
->ifv_mtufudge
;
1660 devmtu_p
= &ifr
->ifr_devmtu
;
1661 devmtu_p
->ifdm_current
= 0;
1662 devmtu_p
->ifdm_min
= 0;
1663 devmtu_p
->ifdm_max
= 0;
1669 error
= vlan_set_mtu(ifp
, ifr
->ifr_mtu
);
1673 user_addr
= proc_is64bit(current_proc())
1674 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1675 error
= copyin(user_addr
, &vlr
, sizeof(vlr
));
1680 if (vlr
.vlr_parent
[0] != '\0') {
1681 if (vlr
.vlr_tag
& ~EVL_VLID_MASK
) {
1683 * Don't let the caller set up a VLAN tag with
1684 * anything except VLID bits.
1689 p
= ifunit(vlr
.vlr_parent
);
1694 /* can't do VLAN over anything but ethernet or ethernet aggregate */
1695 if (p
->if_type
!= IFT_ETHER
&& p
->if_type
!= IFT_IEEE8023ADLAG
) {
1696 error
= EPROTONOSUPPORT
;
1699 error
= vlan_config(ifp
, p
, vlr
.vlr_tag
);
1704 /* Update promiscuous mode, if necessary. */
1705 (void)vlan_set_promisc(ifp
);
1707 /* generate a link event based on the state of the parent */
1708 vlan_link_event(ifp
, p
);
1711 ifv
= (ifvlan_ref
)ifp
->if_private
;
1712 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1714 error
= (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1717 error
= vlan_unconfig(ifp
);
1720 interface_link_event(ifp
, KEV_DL_LINK_OFF
);
1726 bzero(&vlr
, sizeof vlr
);
1728 ifv
= (ifvlan_ref
)ifp
->if_private
;
1729 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1731 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1733 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1737 snprintf(vlr
.vlr_parent
, sizeof(vlr
.vlr_parent
),
1738 "%s%d", p
->if_name
, p
->if_unit
);
1741 user_addr
= proc_is64bit(current_proc())
1742 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1743 error
= copyout(&vlr
, user_addr
, sizeof(vlr
));
1748 * For promiscuous mode, we enable promiscuous mode on
1749 * the parent if we need promiscuous on the VLAN interface.
1751 error
= vlan_set_promisc(ifp
);
1756 error
= vlan_setmulti(ifp
);
1765 vlan_if_free(struct ifnet
* ifp
)
1773 ifv
= (ifvlan_ref
)ifp
->if_private
;
1778 ifp
->if_private
= NULL
;
1780 dlil_if_release(ifp
);
1785 vlan_event(struct ifnet
* p
, struct kev_msg
* event
)
1787 vlan_parent_ref vlp
;
1789 /* Check if the interface we are attached to is being detached */
1790 if (event
->vendor_code
!= KEV_VENDOR_APPLE
1791 || event
->kev_class
!= KEV_NETWORK_CLASS
1792 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
1795 switch (event
->event_code
) {
1796 case KEV_DL_IF_DETACHING
:
1797 case KEV_DL_LINK_OFF
:
1798 case KEV_DL_LINK_ON
:
1804 if ((p
->if_eflags
& IFEF_VLAN
) == 0) {
1809 vlp
= parent_list_lookup(p
);
1815 switch (event
->event_code
) {
1816 case KEV_DL_IF_DETACHING
:
1817 vlan_parent_flags_set_detaching(vlp
);
1818 vlan_parent_remove_all_vlans(vlp
);
1821 case KEV_DL_LINK_OFF
:
1822 case KEV_DL_LINK_ON
:
1823 vlan_parent_link_event(vlp
, event
->event_code
);
1833 interface_link_event(struct ifnet
* ifp
, u_long event_code
)
1836 struct kern_event_msg header
;
1838 char if_name
[IFNAMSIZ
];
1841 event
.header
.total_size
= sizeof(event
);
1842 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
1843 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
1844 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
1845 event
.header
.event_code
= event_code
;
1846 event
.header
.event_data
[0] = ifp
->if_family
;
1847 event
.unit
= (u_long
) ifp
->if_unit
;
1848 strncpy(event
.if_name
, ifp
->if_name
, IFNAMSIZ
);
1849 dlil_event(ifp
, &event
.header
);
1854 vlan_parent_link_event(vlan_parent_ref vlp
, u_long event_code
)
1858 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
1859 interface_link_event(ifv
->ifv_ifp
, event_code
);
1866 * Function: vlan_attach_protocol
1868 * Attach a DLIL protocol to the interface, using the ETHERTYPE_VLAN
1871 * The ethernet demux actually special cases VLAN to support hardware.
1872 * The demux here isn't used. The demux will return PF_VLAN for the
1873 * appropriate packets and our vlan_input function will be called.
1876 vlan_attach_protocol(struct ifnet
*ifp
)
1879 struct dlil_proto_reg_str reg
;
1881 bzero(®
, sizeof(reg
));
1882 TAILQ_INIT(®
.demux_desc_head
);
1883 reg
.interface_family
= ifp
->if_family
;
1884 reg
.unit_number
= ifp
->if_unit
;
1885 reg
.input
= vlan_input
;
1886 reg
.event
= vlan_event
;
1887 reg
.protocol_family
= PF_VLAN
;
1888 error
= dlil_attach_protocol(®
);
1890 printf("vlan_proto_attach(%s%d) dlil_attach_protocol failed, %d\n",
1891 ifp
->if_name
, ifp
->if_unit
, error
);
1897 * Function: vlan_detach_protocol
1899 * Detach our DLIL protocol from an interface
1902 vlan_detach_protocol(struct ifnet
*ifp
)
1906 error
= dlil_detach_protocol(ifp
, PF_VLAN
);
1908 printf("vlan_proto_detach(%s%d) dlil_detach_protocol failed, %d\n",
1909 ifp
->if_name
, ifp
->if_unit
, error
);
1916 * DLIL interface family functions
1917 * We use the ethernet dlil functions, since that's all we support.
1918 * If we wanted to handle multiple LAN types (tokenring, etc.), we'd
1919 * call the appropriate routines for that LAN type instead of hard-coding
1922 extern int ether_add_if(struct ifnet
*ifp
);
1923 extern int ether_del_if(struct ifnet
*ifp
);
1924 extern int ether_init_if(struct ifnet
*ifp
);
1925 extern int ether_add_proto_old(struct ifnet
*ifp
, u_long protocol_family
,
1926 struct ddesc_head_str
*desc_head
);
1928 extern int ether_attach_inet(struct ifnet
*ifp
, u_long protocol_family
);
1929 extern int ether_detach_inet(struct ifnet
*ifp
, u_long protocol_family
);
1930 extern int ether_attach_inet6(struct ifnet
*ifp
, u_long protocol_family
);
1931 extern int ether_detach_inet6(struct ifnet
*ifp
, u_long protocol_family
);
1934 vlan_attach_inet(struct ifnet
*ifp
, u_long protocol_family
)
1936 return (ether_attach_inet(ifp
, protocol_family
));
1940 vlan_detach_inet(struct ifnet
*ifp
, u_long protocol_family
)
1942 return (ether_detach_inet(ifp
, protocol_family
));
1946 vlan_attach_inet6(struct ifnet
*ifp
, u_long protocol_family
)
1948 return (ether_attach_inet6(ifp
, protocol_family
));
1952 vlan_detach_inet6(struct ifnet
*ifp
, u_long protocol_family
)
1954 return (ether_detach_inet6(ifp
, protocol_family
));
1958 vlan_add_if(struct ifnet
*ifp
)
1960 return (ether_add_if(ifp
));
1964 vlan_del_if(struct ifnet
*ifp
)
1966 return (ether_del_if(ifp
));
1970 __private_extern__
int
1971 vlan_family_init(void)
1974 struct dlil_ifmod_reg_str ifmod_reg
;
1976 bzero(&ifmod_reg
, sizeof(ifmod_reg
));
1977 ifmod_reg
.add_if
= vlan_add_if
;
1978 ifmod_reg
.del_if
= vlan_del_if
;
1979 ifmod_reg
.init_if
= NULL
;
1980 ifmod_reg
.add_proto
= ether_add_proto_old
;
1981 ifmod_reg
.del_proto
= ether_del_proto
;
1982 ifmod_reg
.ifmod_ioctl
= ether_ioctl
;
1983 ifmod_reg
.shutdown
= NULL
;
1985 if (dlil_reg_if_modules(APPLE_IF_FAM_VLAN
, &ifmod_reg
)) {
1986 printf("WARNING: vlan_family_init -- "
1987 "Can't register if family modules\n");
1992 error
= dlil_reg_proto_module(PF_INET
, APPLE_IF_FAM_VLAN
,
1993 vlan_attach_inet
, vlan_detach_inet
);
1995 printf("dlil_reg_proto_module failed for AF_INET error=%d\n",
1999 error
= dlil_reg_proto_module(PF_INET6
, APPLE_IF_FAM_VLAN
,
2000 vlan_attach_inet6
, vlan_detach_inet6
);
2002 printf("dlil_reg_proto_module failed for AF_INET6 error=%d\n",
2006 vlan_clone_attach();