2 * Copyright (c) 2003-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright 1998 Massachusetts Institute of Technology
31 * Permission to use, copy, modify, and distribute this software and
32 * its documentation for any purpose and without fee is hereby
33 * granted, provided that both the above copyright notice and this
34 * permission notice appear in all copies, that both the above
35 * copyright notice and this permission notice appear in all
36 * supporting documentation, and that the name of M.I.T. not be used
37 * in advertising or publicity pertaining to distribution of the
38 * software without specific, written prior permission. M.I.T. makes
39 * no representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied
43 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
44 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
45 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
46 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
47 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
50 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
51 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
53 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * $FreeBSD: src/sys/net/if_vlan.c,v 1.54 2003/10/31 18:32:08 brooks Exp $
60 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
61 * Might be extended some day to also handle IEEE 802.1p priority
62 * tagging. This is sort of sneaky in the implementation, since
63 * we need to pretend to be enough of an Ethernet implementation
64 * to make arp work. The way we do this is by telling everyone
65 * that we are an Ethernet, and then catch the packets that
66 * ether_output() left on our output queue when it calls
67 * if_start(), rewrite them for use by the real outgoing interface,
68 * and ask it to send them.
72 #include <sys/param.h>
73 #include <sys/kernel.h>
74 #include <sys/malloc.h>
76 #include <sys/queue.h>
77 #include <sys/socket.h>
78 #include <sys/sockio.h>
79 #include <sys/sysctl.h>
80 #include <sys/systm.h>
81 #include <sys/kern_event.h>
82 #include <sys/mcache.h>
85 #include <net/ethernet.h>
87 #include <net/if_arp.h>
88 #include <net/if_dl.h>
89 #include <net/if_ether.h>
90 #include <net/if_types.h>
91 #include <net/if_vlan_var.h>
92 #include <libkern/OSAtomic.h>
96 #include <net/kpi_interface.h>
97 #include <net/kpi_protocol.h>
99 #include <kern/locks.h>
102 #include <netinet/in.h>
103 #include <netinet/if_ether.h>
106 #include <net/if_media.h>
107 #include <net/multicast_list.h>
108 #include <net/ether_if_module.h>
110 #define VLANNAME "vlan"
115 static __inline__ lck_grp_t
*
116 my_lck_grp_alloc_init(const char * grp_name
)
119 lck_grp_attr_t
* grp_attrs
;
121 grp_attrs
= lck_grp_attr_alloc_init();
122 grp
= lck_grp_alloc_init(grp_name
, grp_attrs
);
123 lck_grp_attr_free(grp_attrs
);
127 static __inline__ lck_mtx_t
*
128 my_lck_mtx_alloc_init(lck_grp_t
* lck_grp
)
130 lck_attr_t
* lck_attrs
;
133 lck_attrs
= lck_attr_alloc_init();
134 lck_mtx
= lck_mtx_alloc_init(lck_grp
, lck_attrs
);
135 lck_attr_free(lck_attrs
);
139 static lck_mtx_t
* vlan_lck_mtx
;
141 static __inline__
void
144 lck_grp_t
* vlan_lck_grp
;
146 vlan_lck_grp
= my_lck_grp_alloc_init("if_vlan");
147 vlan_lck_mtx
= my_lck_mtx_alloc_init(vlan_lck_grp
);
150 static __inline__
void
151 vlan_assert_lock_held(void)
153 LCK_MTX_ASSERT(vlan_lck_mtx
, LCK_MTX_ASSERT_OWNED
);
157 static __inline__
void
158 vlan_assert_lock_not_held(void)
160 LCK_MTX_ASSERT(vlan_lck_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
164 static __inline__
void
167 lck_mtx_lock(vlan_lck_mtx
);
171 static __inline__
void
174 lck_mtx_unlock(vlan_lck_mtx
);
179 ** vlan structures, types
182 LIST_HEAD(vlan_parent_list
, vlan_parent
);
184 LIST_HEAD(ifvlan_list
, ifvlan
);
186 typedef LIST_ENTRY(vlan_parent
)
188 typedef LIST_ENTRY(ifvlan
)
191 #define VLP_SIGNATURE 0xfaceface
192 typedef struct vlan_parent
{
193 vlan_parent_entry vlp_parent_list
;/* list of parents */
194 struct ifnet
* vlp_ifp
; /* interface */
195 struct ifvlan_list vlp_vlan_list
; /* list of VLAN's */
196 #define VLPF_SUPPORTS_VLAN_MTU 0x00000001
197 #define VLPF_CHANGE_IN_PROGRESS 0x00000002
198 #define VLPF_DETACHING 0x00000004
199 #define VLPF_LINK_EVENT_REQUIRED 0x00000008
201 u_int32_t vlp_event_code
;
202 struct ifdevmtu vlp_devmtu
;
203 int32_t vlp_retain_count
;
204 u_int32_t vlp_signature
; /* VLP_SIGNATURE */
205 } vlan_parent
, * vlan_parent_ref
;
207 #define IFV_SIGNATURE 0xbeefbeef
209 ifvlan_entry ifv_vlan_list
;
210 char ifv_name
[IFNAMSIZ
]; /* our unique id */
211 struct ifnet
* ifv_ifp
; /* our interface */
212 vlan_parent_ref ifv_vlp
; /* parent information */
214 u_int16_t ifvm_encaplen
;/* encapsulation length */
215 u_int16_t ifvm_mtufudge
;/* MTU fudged by this much */
216 u_int16_t ifvm_proto
; /* encapsulation ethertype */
217 u_int16_t ifvm_tag
; /* tag to apply on packets leaving if */
219 struct multicast_list ifv_multicast
;
220 #define IFVF_PROMISC 0x1 /* promiscuous mode enabled */
221 #define IFVF_DETACHING 0x2 /* interface is detaching */
222 #define IFVF_READY 0x4 /* interface is ready */
224 int32_t ifv_retain_count
;
225 u_int32_t ifv_signature
; /* IFV_SIGNATURE */
228 typedef struct ifvlan
* ifvlan_ref
;
230 typedef struct vlan_globals_s
{
231 struct vlan_parent_list parent_list
;
233 } * vlan_globals_ref
;
235 static vlan_globals_ref g_vlan
;
237 #define ifv_tag ifv_mib.ifvm_tag
238 #define ifv_encaplen ifv_mib.ifvm_encaplen
239 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
242 vlan_parent_retain(vlan_parent_ref vlp
);
245 vlan_parent_release(vlan_parent_ref vlp
);
248 ** vlan_parent_ref vlp_flags in-lines
250 static __inline__
int
251 vlan_parent_flags_supports_vlan_mtu(vlan_parent_ref vlp
)
253 return ((vlp
->vlp_flags
& VLPF_SUPPORTS_VLAN_MTU
) != 0);
256 static __inline__
void
257 vlan_parent_flags_set_supports_vlan_mtu(vlan_parent_ref vlp
)
259 vlp
->vlp_flags
|= VLPF_SUPPORTS_VLAN_MTU
;
263 static __inline__
int
264 vlan_parent_flags_change_in_progress(vlan_parent_ref vlp
)
266 return ((vlp
->vlp_flags
& VLPF_CHANGE_IN_PROGRESS
) != 0);
269 static __inline__
void
270 vlan_parent_flags_set_change_in_progress(vlan_parent_ref vlp
)
272 vlp
->vlp_flags
|= VLPF_CHANGE_IN_PROGRESS
;
276 static __inline__
void
277 vlan_parent_flags_clear_change_in_progress(vlan_parent_ref vlp
)
279 vlp
->vlp_flags
&= ~VLPF_CHANGE_IN_PROGRESS
;
283 static __inline__
int
284 vlan_parent_flags_detaching(struct vlan_parent
* vlp
)
286 return ((vlp
->vlp_flags
& VLPF_DETACHING
) != 0);
289 static __inline__
void
290 vlan_parent_flags_set_detaching(struct vlan_parent
* vlp
)
292 vlp
->vlp_flags
|= VLPF_DETACHING
;
296 static __inline__
int
297 vlan_parent_flags_link_event_required(vlan_parent_ref vlp
)
299 return ((vlp
->vlp_flags
& VLPF_LINK_EVENT_REQUIRED
) != 0);
302 static __inline__
void
303 vlan_parent_flags_set_link_event_required(vlan_parent_ref vlp
)
305 vlp
->vlp_flags
|= VLPF_LINK_EVENT_REQUIRED
;
309 static __inline__
void
310 vlan_parent_flags_clear_link_event_required(vlan_parent_ref vlp
)
312 vlp
->vlp_flags
&= ~VLPF_LINK_EVENT_REQUIRED
;
318 ** ifvlan_flags in-lines routines
320 static __inline__
int
321 ifvlan_flags_promisc(ifvlan_ref ifv
)
323 return ((ifv
->ifv_flags
& IFVF_PROMISC
) != 0);
326 static __inline__
void
327 ifvlan_flags_set_promisc(ifvlan_ref ifv
)
329 ifv
->ifv_flags
|= IFVF_PROMISC
;
333 static __inline__
void
334 ifvlan_flags_clear_promisc(ifvlan_ref ifv
)
336 ifv
->ifv_flags
&= ~IFVF_PROMISC
;
340 static __inline__
int
341 ifvlan_flags_ready(ifvlan_ref ifv
)
343 return ((ifv
->ifv_flags
& IFVF_READY
) != 0);
346 static __inline__
void
347 ifvlan_flags_set_ready(ifvlan_ref ifv
)
349 ifv
->ifv_flags
|= IFVF_READY
;
353 static __inline__
int
354 ifvlan_flags_detaching(ifvlan_ref ifv
)
356 return ((ifv
->ifv_flags
& IFVF_DETACHING
) != 0);
359 static __inline__
void
360 ifvlan_flags_set_detaching(ifvlan_ref ifv
)
362 ifv
->ifv_flags
|= IFVF_DETACHING
;
367 SYSCTL_DECL(_net_link
);
368 SYSCTL_NODE(_net_link
, IFT_L2VLAN
, vlan
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "IEEE 802.1Q VLAN");
369 SYSCTL_NODE(_net_link_vlan
, PF_LINK
, link
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "for consistency");
372 #define M_VLAN M_DEVBUF
374 static int vlan_clone_create(struct if_clone
*, u_int32_t
, void *);
375 static int vlan_clone_destroy(struct ifnet
*);
376 static int vlan_input(ifnet_t ifp
, protocol_family_t protocol
,
377 mbuf_t m
, char *frame_header
);
378 static int vlan_output(struct ifnet
*ifp
, struct mbuf
*m
);
379 static int vlan_ioctl(ifnet_t ifp
, u_long cmd
, void * addr
);
380 static int vlan_attach_protocol(struct ifnet
*ifp
);
381 static int vlan_detach_protocol(struct ifnet
*ifp
);
382 static int vlan_setmulti(struct ifnet
*ifp
);
383 static int vlan_unconfig(ifvlan_ref ifv
, int need_to_wait
);
384 static int vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
);
385 static void vlan_if_free(struct ifnet
* ifp
);
386 static int vlan_remove(ifvlan_ref ifv
, int need_to_wait
);
388 static struct if_clone vlan_cloner
= IF_CLONE_INITIALIZER(VLANNAME
,
393 static void interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
);
394 static void vlan_parent_link_event(struct ifnet
* p
,
395 u_int32_t event_code
);
397 static int ifvlan_new_mtu(ifvlan_ref ifv
, int mtu
);
400 ** ifvlan_ref routines
403 ifvlan_retain(ifvlan_ref ifv
)
405 if (ifv
->ifv_signature
!= IFV_SIGNATURE
) {
406 panic("ifvlan_retain: bad signature\n");
408 if (ifv
->ifv_retain_count
== 0) {
409 panic("ifvlan_retain: retain count is 0\n");
411 OSIncrementAtomic(&ifv
->ifv_retain_count
);
415 ifvlan_release(ifvlan_ref ifv
)
417 u_int32_t old_retain_count
;
419 if (ifv
->ifv_signature
!= IFV_SIGNATURE
) {
420 panic("ifvlan_release: bad signature\n");
422 old_retain_count
= OSDecrementAtomic(&ifv
->ifv_retain_count
);
423 switch (old_retain_count
) {
425 panic("ifvlan_release: retain count is 0\n");
428 if (g_vlan
->verbose
) {
429 printf("ifvlan_release(%s)\n", ifv
->ifv_name
);
431 ifv
->ifv_signature
= 0;
440 static vlan_parent_ref
441 ifvlan_get_vlan_parent_retained(ifvlan_ref ifv
)
443 vlan_parent_ref vlp
= ifv
->ifv_vlp
;
445 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
448 vlan_parent_retain(vlp
);
457 ifnet_get_ifvlan(struct ifnet
* ifp
)
461 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
466 ifnet_get_ifvlan_retained(struct ifnet
* ifp
)
470 ifv
= ifnet_get_ifvlan(ifp
);
474 if (ifvlan_flags_detaching(ifv
)) {
482 ifnet_ifvlan_vlan_parent_ok(struct ifnet
* ifp
, ifvlan_ref ifv
,
485 ifvlan_ref check_ifv
;
487 check_ifv
= ifnet_get_ifvlan(ifp
);
488 if (check_ifv
!= ifv
|| ifvlan_flags_detaching(ifv
)) {
489 /* ifvlan_ref no longer valid */
492 if (ifv
->ifv_vlp
!= vlp
) {
493 /* vlan_parent no longer valid */
496 if (vlan_parent_flags_detaching(vlp
)) {
497 /* parent is detaching */
504 ** vlan, etc. routines
508 vlan_globals_init(void)
512 vlan_assert_lock_not_held();
514 if (g_vlan
!= NULL
) {
517 v
= _MALLOC(sizeof(*v
), M_VLAN
, M_WAITOK
);
519 LIST_INIT(&v
->parent_list
);
523 if (g_vlan
!= NULL
) {
539 siocgifdevmtu(struct ifnet
* ifp
, struct ifdevmtu
* ifdm_p
)
544 bzero(&ifr
, sizeof(ifr
));
545 error
= ifnet_ioctl(ifp
, 0,SIOCGIFDEVMTU
, &ifr
);
547 *ifdm_p
= ifr
.ifr_devmtu
;
553 siocsifaltmtu(struct ifnet
* ifp
, int mtu
)
557 bzero(&ifr
, sizeof(ifr
));
559 return (ifnet_ioctl(ifp
, 0, SIOCSIFALTMTU
, &ifr
));
563 ** vlan_parent synchronization routines
566 vlan_parent_retain(vlan_parent_ref vlp
)
568 if (vlp
->vlp_signature
!= VLP_SIGNATURE
) {
569 panic("vlan_parent_retain: signature is bad\n");
571 if (vlp
->vlp_retain_count
== 0) {
572 panic("vlan_parent_retain: retain count is 0\n");
574 OSIncrementAtomic(&vlp
->vlp_retain_count
);
578 vlan_parent_release(vlan_parent_ref vlp
)
580 u_int32_t old_retain_count
;
582 if (vlp
->vlp_signature
!= VLP_SIGNATURE
) {
583 panic("vlan_parent_release: signature is bad\n");
585 old_retain_count
= OSDecrementAtomic(&vlp
->vlp_retain_count
);
586 switch (old_retain_count
) {
588 panic("vlan_parent_release: retain count is 0\n");
591 if (g_vlan
->verbose
) {
592 struct ifnet
* ifp
= vlp
->vlp_ifp
;
593 printf("vlan_parent_release(%s%d)\n", ifnet_name(ifp
),
596 vlp
->vlp_signature
= 0;
606 * Function: vlan_parent_wait
608 * Allows a single thread to gain exclusive access to the vlan_parent
609 * data structure. Some operations take a long time to complete,
610 * and some have side-effects that we can't predict. Holding the
611 * vlan_lock() across such operations is not possible.
614 * Before calling, you must be holding the vlan_lock and have taken
615 * a reference on the vlan_parent_ref.
618 vlan_parent_wait(vlan_parent_ref vlp
, const char * msg
)
622 /* other add/remove/multicast-change in progress */
623 while (vlan_parent_flags_change_in_progress(vlp
)) {
624 if (g_vlan
->verbose
) {
625 struct ifnet
* ifp
= vlp
->vlp_ifp
;
627 printf("%s%d: %s msleep\n", ifnet_name(ifp
), ifnet_unit(ifp
), msg
);
630 (void)msleep(vlp
, vlan_lck_mtx
, PZERO
, msg
, 0);
632 /* prevent other vlan parent remove/add from taking place */
633 vlan_parent_flags_set_change_in_progress(vlp
);
634 if (g_vlan
->verbose
&& waited
) {
635 struct ifnet
* ifp
= vlp
->vlp_ifp
;
637 printf("%s%d: %s woke up\n", ifnet_name(ifp
), ifnet_unit(ifp
), msg
);
643 * Function: vlan_parent_signal
645 * Allows the thread that previously invoked vlan_parent_wait() to
646 * give up exclusive access to the vlan_parent data structure, and wake up
647 * any other threads waiting to access
649 * Before calling, you must be holding the vlan_lock and have taken
650 * a reference on the vlan_parent_ref.
653 vlan_parent_signal(vlan_parent_ref vlp
, const char * msg
)
655 struct ifnet
* vlp_ifp
= vlp
->vlp_ifp
;
657 if (vlan_parent_flags_link_event_required(vlp
)) {
658 vlan_parent_flags_clear_link_event_required(vlp
);
659 if (!vlan_parent_flags_detaching(vlp
)) {
660 u_int32_t event_code
= vlp
->vlp_event_code
;
665 /* we can safely walk the list unlocked */
666 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
667 struct ifnet
* ifp
= ifv
->ifv_ifp
;
669 interface_link_event(ifp
, event_code
);
671 if (g_vlan
->verbose
) {
672 printf("%s%d: propagated link event to vlans\n",
673 ifnet_name(vlp_ifp
), ifnet_unit(vlp_ifp
));
678 vlan_parent_flags_clear_change_in_progress(vlp
);
679 wakeup((caddr_t
)vlp
);
680 if (g_vlan
->verbose
) {
681 printf("%s%d: %s wakeup\n",
682 ifnet_name(vlp_ifp
), ifnet_unit(vlp_ifp
), msg
);
688 * Program our multicast filter. What we're actually doing is
689 * programming the multicast filter of the parent. This has the
690 * side effect of causing the parent interface to receive multicast
691 * traffic that it doesn't really want, which ends up being discarded
692 * later by the upper protocol layers. Unfortunately, there's no way
693 * to avoid this: there really is only one physical interface.
696 vlan_setmulti(struct ifnet
* ifp
)
701 vlan_parent_ref vlp
= NULL
;
704 ifv
= ifnet_get_ifvlan_retained(ifp
);
708 vlp
= ifvlan_get_vlan_parent_retained(ifv
);
710 /* no parent, no need to program the multicast filter */
713 vlan_parent_wait(vlp
, "vlan_setmulti");
715 /* check again, things could have changed */
716 if (ifnet_ifvlan_vlan_parent_ok(ifp
, ifv
, vlp
) == FALSE
) {
722 /* update parent interface with our multicast addresses */
723 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
728 vlan_parent_signal(vlp
, "vlan_setmulti");
736 vlan_parent_release(vlp
);
742 ** vlan_parent list manipulation/lookup routines
744 static vlan_parent_ref
745 parent_list_lookup(struct ifnet
* p
)
749 LIST_FOREACH(vlp
, &g_vlan
->parent_list
, vlp_parent_list
) {
750 if (vlp
->vlp_ifp
== p
) {
758 vlan_parent_lookup_tag(vlan_parent_ref vlp
, int tag
)
762 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
763 if (tag
== ifv
->ifv_tag
) {
771 vlan_lookup_parent_and_tag(struct ifnet
* p
, int tag
)
775 vlp
= parent_list_lookup(p
);
777 return (vlan_parent_lookup_tag(vlp
, tag
));
783 vlan_parent_find_max_mtu(vlan_parent_ref vlp
, ifvlan_ref exclude_ifv
)
788 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
791 if (exclude_ifv
== ifv
) {
794 req_mtu
= ifnet_mtu(ifv
->ifv_ifp
) + ifv
->ifv_mtufudge
;
795 if (req_mtu
> max_mtu
) {
803 * Function: vlan_parent_create
805 * Create a vlan_parent structure to hold the VLAN's for the given
806 * interface. Add it to the list of VLAN parents.
809 vlan_parent_create(struct ifnet
* p
, vlan_parent_ref
* ret_vlp
)
815 vlp
= _MALLOC(sizeof(*vlp
), M_VLAN
, M_WAITOK
| M_ZERO
);
819 error
= siocgifdevmtu(p
, &vlp
->vlp_devmtu
);
821 printf("vlan_parent_create (%s%d): siocgifdevmtu failed, %d\n",
822 ifnet_name(p
), ifnet_unit(p
), error
);
826 LIST_INIT(&vlp
->vlp_vlan_list
);
828 vlp
->vlp_retain_count
= 1;
829 vlp
->vlp_signature
= VLP_SIGNATURE
;
831 & (IF_HWASSIST_VLAN_MTU
| IF_HWASSIST_VLAN_TAGGING
)) {
832 vlan_parent_flags_set_supports_vlan_mtu(vlp
);
839 vlan_parent_remove_all_vlans(struct ifnet
* p
)
842 int need_vlp_release
= 0;
847 vlp
= parent_list_lookup(p
);
848 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
853 vlan_parent_flags_set_detaching(vlp
);
854 vlan_parent_retain(vlp
);
855 vlan_parent_wait(vlp
, "vlan_parent_remove_all_vlans");
859 if (parent_list_lookup(p
) != vlp
) {
863 for (ifv
= LIST_FIRST(&vlp
->vlp_vlan_list
); ifv
!= NULL
; ifv
= next
) {
864 struct ifnet
* ifp
= ifv
->ifv_ifp
;
867 next
= LIST_NEXT(ifv
, ifv_vlan_list
);
868 removed
= vlan_remove(ifv
, FALSE
);
876 /* the vlan parent has no more VLAN's */
877 ifnet_set_eflags(p
, 0, IFEF_VLAN
); /* clear IFEF_VLAN */
879 LIST_REMOVE(vlp
, vlp_parent_list
);
880 need_vlp_release
++; /* one for being in the list */
881 need_vlp_release
++; /* final reference */
884 vlan_parent_signal(vlp
, "vlan_parent_remove_all_vlans");
887 while (need_vlp_release
--) {
888 vlan_parent_release(vlp
);
893 static __inline__
int
894 vlan_parent_no_vlans(vlan_parent_ref vlp
)
896 return (LIST_EMPTY(&vlp
->vlp_vlan_list
));
900 vlan_parent_add_vlan(vlan_parent_ref vlp
, ifvlan_ref ifv
, int tag
)
902 LIST_INSERT_HEAD(&vlp
->vlp_vlan_list
, ifv
, ifv_vlan_list
);
909 vlan_parent_remove_vlan(__unused vlan_parent_ref vlp
, ifvlan_ref ifv
)
912 LIST_REMOVE(ifv
, ifv_vlan_list
);
917 vlan_clone_attach(void)
921 error
= if_clone_attach(&vlan_cloner
);
929 vlan_clone_create(struct if_clone
*ifc
, u_int32_t unit
, __unused
void *params
)
934 struct ifnet_init_eparams vlan_init
;
936 error
= vlan_globals_init();
940 ifv
= _MALLOC(sizeof(struct ifvlan
), M_VLAN
, M_WAITOK
| M_ZERO
);
943 ifv
->ifv_retain_count
= 1;
944 ifv
->ifv_signature
= IFV_SIGNATURE
;
945 multicast_list_init(&ifv
->ifv_multicast
);
947 /* use the interface name as the unique id for ifp recycle */
949 snprintf(ifv
->ifv_name
, sizeof(ifv
->ifv_name
), "%s%d",
950 ifc
->ifc_name
, unit
) >= sizeof(ifv
->ifv_name
)) {
955 bzero(&vlan_init
, sizeof(vlan_init
));
956 vlan_init
.ver
= IFNET_INIT_CURRENT_VERSION
;
957 vlan_init
.len
= sizeof (vlan_init
);
958 vlan_init
.flags
= IFNET_INIT_LEGACY
;
959 vlan_init
.uniqueid
= ifv
->ifv_name
;
960 vlan_init
.uniqueid_len
= strlen(ifv
->ifv_name
);
961 vlan_init
.name
= ifc
->ifc_name
;
962 vlan_init
.unit
= unit
;
963 vlan_init
.family
= IFNET_FAMILY_VLAN
;
964 vlan_init
.type
= IFT_L2VLAN
;
965 vlan_init
.output
= vlan_output
;
966 vlan_init
.demux
= ether_demux
;
967 vlan_init
.add_proto
= ether_add_proto
;
968 vlan_init
.del_proto
= ether_del_proto
;
969 vlan_init
.check_multi
= ether_check_multi
;
970 vlan_init
.framer_extended
= ether_frameout_extended
;
971 vlan_init
.softc
= ifv
;
972 vlan_init
.ioctl
= vlan_ioctl
;
973 vlan_init
.set_bpf_tap
= NULL
;
974 vlan_init
.detach
= vlan_if_free
;
975 vlan_init
.broadcast_addr
= etherbroadcastaddr
;
976 vlan_init
.broadcast_len
= ETHER_ADDR_LEN
;
977 error
= ifnet_allocate_extended(&vlan_init
, &ifp
);
984 ifnet_set_offload(ifp
, 0);
985 ifnet_set_addrlen(ifp
, ETHER_ADDR_LEN
); /* XXX ethernet specific */
986 ifnet_set_baudrate(ifp
, 0);
987 ifnet_set_hdrlen(ifp
, ETHER_VLAN_ENCAP_LEN
);
989 error
= ifnet_attach(ifp
, NULL
);
997 /* attach as ethernet */
998 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
1003 vlan_remove(ifvlan_ref ifv
, int need_to_wait
)
1005 vlan_assert_lock_held();
1006 if (ifvlan_flags_detaching(ifv
)) {
1009 ifvlan_flags_set_detaching(ifv
);
1010 vlan_unconfig(ifv
, need_to_wait
);
1016 vlan_clone_destroy(struct ifnet
*ifp
)
1021 ifv
= ifnet_get_ifvlan_retained(ifp
);
1026 if (vlan_remove(ifv
, TRUE
) == 0) {
1028 ifvlan_release(ifv
);
1032 ifvlan_release(ifv
);
1039 vlan_output(struct ifnet
* ifp
, struct mbuf
* m
)
1041 struct ether_vlan_header
* evl
;
1047 vlan_parent_ref vlp
= NULL
;
1049 struct flowadv adv
= { FADV_SUCCESS
};
1054 if ((m
->m_flags
& M_PKTHDR
) == 0) {
1059 ifv
= ifnet_get_ifvlan_retained(ifp
);
1060 if (ifv
== NULL
|| ifvlan_flags_ready(ifv
) == 0) {
1063 vlp
= ifvlan_get_vlan_parent_retained(ifv
);
1068 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
1069 soft_vlan
= (ifnet_offload(p
) & IF_HWASSIST_VLAN_TAGGING
) == 0;
1071 encaplen
= ifv
->ifv_encaplen
;
1074 ifvlan_release(ifv
);
1075 vlan_parent_release(vlp
);
1077 bpf_tap_out(ifp
, DLT_EN10MB
, m
, NULL
, 0);
1079 /* do not run parent's if_output() if the parent is not up */
1080 if ((ifnet_flags(p
) & (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
)) {
1082 atomic_add_64(&ifp
->if_collisions
, 1);
1086 * If underlying interface can do VLAN tag insertion itself,
1087 * just pass the packet along. However, we need some way to
1088 * tell the interface where the packet came from so that it
1089 * knows how to find the VLAN tag to use. We use a field in
1090 * the mbuf header to store the VLAN tag, and a bit in the
1091 * csum_flags field to mark the field as valid.
1093 if (soft_vlan
== 0) {
1094 m
->m_pkthdr
.csum_flags
|= CSUM_VLAN_TAG_VALID
;
1095 m
->m_pkthdr
.vlan_tag
= tag
;
1097 M_PREPEND(m
, encaplen
, M_DONTWAIT
, 1);
1099 printf("%s%d: unable to prepend VLAN header\n", ifnet_name(ifp
),
1101 atomic_add_64(&ifp
->if_oerrors
, 1);
1104 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1105 if (m
->m_len
< (int)sizeof(*evl
)) {
1106 m
= m_pullup(m
, sizeof(*evl
));
1108 printf("%s%d: unable to pullup VLAN header\n", ifnet_name(ifp
),
1110 atomic_add_64(&ifp
->if_oerrors
, 1);
1116 * Transform the Ethernet header into an Ethernet header
1117 * with 802.1Q encapsulation.
1119 bcopy(mtod(m
, char *) + encaplen
,
1120 mtod(m
, char *), ETHER_HDR_LEN
);
1121 evl
= mtod(m
, struct ether_vlan_header
*);
1122 evl
->evl_proto
= evl
->evl_encap_proto
;
1123 evl
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
1124 evl
->evl_tag
= htons(tag
);
1127 err
= dlil_output(p
, PF_VLAN
, m
, NULL
, NULL
, 1, &adv
);
1130 if (adv
.code
== FADV_FLOW_CONTROLLED
) {
1132 } else if (adv
.code
== FADV_SUSPENDED
) {
1142 ifvlan_release(ifv
);
1145 vlan_parent_release(vlp
);
1153 vlan_input(ifnet_t p
, __unused protocol_family_t protocol
,
1154 mbuf_t m
, char *frame_header
)
1156 struct ether_vlan_header
* evl
;
1157 struct ifnet
* ifp
= NULL
;
1161 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1163 * Packet is tagged, m contains a normal
1164 * Ethernet frame; the tag is stored out-of-band.
1166 m
->m_pkthdr
.csum_flags
&= ~CSUM_VLAN_TAG_VALID
;
1167 tag
= EVL_VLANOFTAG(m
->m_pkthdr
.vlan_tag
);
1168 m
->m_pkthdr
.vlan_tag
= 0;
1171 switch (ifnet_type(p
)) {
1173 if (m
->m_len
< ETHER_VLAN_ENCAP_LEN
) {
1177 evl
= (struct ether_vlan_header
*)(void *)frame_header
;
1178 if (ntohs(evl
->evl_proto
) == ETHERTYPE_VLAN
) {
1179 /* don't allow VLAN within VLAN */
1183 tag
= EVL_VLANOFTAG(ntohs(evl
->evl_tag
));
1186 * Restore the original ethertype. We'll remove
1187 * the encapsulation after we've found the vlan
1188 * interface corresponding to the tag.
1190 evl
->evl_encap_proto
= evl
->evl_proto
;
1193 printf("vlan_demux: unsupported if type %u",
1202 if ((ifnet_eflags(p
) & IFEF_VLAN
) == 0) {
1203 /* don't bother looking through the VLAN list */
1208 ifv
= vlan_lookup_parent_and_tag(p
, tag
);
1213 || ifvlan_flags_ready(ifv
) == 0
1214 || (ifnet_flags(ifp
) & IFF_UP
) == 0) {
1223 * Packet had an in-line encapsulation header;
1224 * remove it. The original header has already
1225 * been fixed up above.
1227 m
->m_len
-= ETHER_VLAN_ENCAP_LEN
;
1228 m
->m_data
+= ETHER_VLAN_ENCAP_LEN
;
1229 m
->m_pkthdr
.len
-= ETHER_VLAN_ENCAP_LEN
;
1230 m
->m_pkthdr
.csum_flags
= 0; /* can't trust hardware checksum */
1233 m
->m_pkthdr
.rcvif
= ifp
;
1234 m
->m_pkthdr
.pkt_hdr
= frame_header
;
1235 (void)ifnet_stat_increment_in(ifp
, 1,
1236 m
->m_pkthdr
.len
+ ETHER_HDR_LEN
, 0);
1237 bpf_tap_in(ifp
, DLT_EN10MB
, m
, frame_header
, ETHER_HDR_LEN
);
1238 /* We found a vlan interface, inject on that interface. */
1239 dlil_input_packet_list(ifp
, m
);
1241 m
->m_pkthdr
.pkt_hdr
= frame_header
;
1242 /* Send priority-tagged packet up through the parent */
1243 dlil_input_packet_list(p
, m
);
1249 vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
)
1252 int first_vlan
= FALSE
;
1253 ifvlan_ref ifv
= NULL
;
1254 int ifv_added
= FALSE
;
1255 int need_vlp_release
= 0;
1256 vlan_parent_ref new_vlp
= NULL
;
1257 ifnet_offload_t offload
;
1258 u_int16_t parent_flags
;
1259 vlan_parent_ref vlp
= NULL
;
1261 /* pre-allocate space for vlan_parent, in case we're first */
1262 error
= vlan_parent_create(p
, &new_vlp
);
1268 ifv
= ifnet_get_ifvlan_retained(ifp
);
1269 if (ifv
== NULL
|| ifv
->ifv_vlp
!= NULL
) {
1272 ifvlan_release(ifv
);
1274 vlan_parent_release(new_vlp
);
1277 vlp
= parent_list_lookup(p
);
1279 vlan_parent_retain(vlp
);
1281 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1282 /* already a VLAN with that tag on this interface */
1288 /* one for being in the list */
1289 vlan_parent_retain(new_vlp
);
1291 /* we're the first VLAN on this interface */
1292 LIST_INSERT_HEAD(&g_vlan
->parent_list
, new_vlp
, vlp_parent_list
);
1295 vlan_parent_retain(vlp
);
1299 /* need to wait to ensure no one else is trying to add/remove */
1300 vlan_parent_wait(vlp
, "vlan_config");
1302 if (ifnet_get_ifvlan(ifp
) != ifv
) {
1307 /* check again because someone might have gotten in */
1308 if (parent_list_lookup(p
) != vlp
) {
1313 if (vlan_parent_flags_detaching(vlp
)
1314 || ifvlan_flags_detaching(ifv
) || ifv
->ifv_vlp
!= NULL
) {
1319 /* check again because someone might have gotten the tag */
1320 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1321 /* already a VLAN with that tag on this interface */
1326 if (vlan_parent_no_vlans(vlp
)) {
1329 vlan_parent_add_vlan(vlp
, ifv
, tag
);
1330 ifvlan_retain(ifv
); /* parent references ifv */
1333 /* check whether bond interface is using parent interface */
1334 ifnet_lock_exclusive(p
);
1335 if ((ifnet_eflags(p
) & IFEF_BOND
) != 0) {
1337 /* don't allow VLAN over interface that's already part of a bond */
1341 /* prevent BOND interface from using it */
1342 /* Can't use ifnet_set_eflags because that would take the lock */
1343 p
->if_eflags
|= IFEF_VLAN
;
1348 /* attach our VLAN "protocol" to the interface */
1349 error
= vlan_attach_protocol(p
);
1356 /* configure parent to receive our multicast addresses */
1357 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
1360 (void)vlan_detach_protocol(p
);
1366 /* set our ethernet address to that of the parent */
1367 ifnet_set_lladdr_and_type(ifp
, IF_LLADDR(p
), ETHER_ADDR_LEN
, IFT_ETHER
);
1369 /* no failures past this point */
1372 ifv
->ifv_encaplen
= ETHER_VLAN_ENCAP_LEN
;
1374 if (vlan_parent_flags_supports_vlan_mtu(vlp
)) {
1375 ifv
->ifv_mtufudge
= 0;
1378 * Fudge the MTU by the encapsulation size. This
1379 * makes us incompatible with strictly compliant
1380 * 802.1Q implementations, but allows us to use
1381 * the feature with other NetBSD implementations,
1382 * which might still be useful.
1384 ifv
->ifv_mtufudge
= ifv
->ifv_encaplen
;
1386 ifnet_set_mtu(ifp
, ETHERMTU
- ifv
->ifv_mtufudge
);
1389 * Copy only a selected subset of flags from the parent.
1390 * Other flags are none of our business.
1392 parent_flags
= ifnet_flags(p
)
1393 & (IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
);
1394 ifnet_set_flags(ifp
, parent_flags
,
1395 IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
);
1397 /* use hwassist bits from parent interface, but exclude VLAN bits */
1398 offload
= ifnet_offload(p
) & ~(IFNET_VLAN_TAGGING
| IFNET_VLAN_MTU
);
1399 ifnet_set_offload(ifp
, offload
);
1401 ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
1402 ifvlan_flags_set_ready(ifv
);
1403 vlan_parent_signal(vlp
, "vlan_config");
1405 if (new_vlp
!= vlp
) {
1406 /* throw it away, it wasn't needed */
1407 vlan_parent_release(new_vlp
);
1410 ifvlan_release(ifv
);
1413 /* mark the parent interface up */
1414 ifnet_set_flags(p
, IFF_UP
, IFF_UP
);
1415 (void)ifnet_ioctl(p
, 0, SIOCSIFFLAGS
, (caddr_t
)NULL
);
1420 vlan_assert_lock_held();
1423 vlan_parent_remove_vlan(vlp
, ifv
);
1424 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1425 /* the vlan parent has no more VLAN's */
1426 ifnet_set_eflags(p
, 0, IFEF_VLAN
);
1427 LIST_REMOVE(vlp
, vlp_parent_list
);
1428 /* release outside of the lock below */
1431 /* one for being in the list */
1435 vlan_parent_signal(vlp
, "vlan_config");
1440 while (need_vlp_release
--) {
1441 vlan_parent_release(vlp
);
1443 if (new_vlp
!= vlp
) {
1444 vlan_parent_release(new_vlp
);
1448 ifvlan_release(ifv
);
1450 ifvlan_release(ifv
);
1456 vlan_link_event(struct ifnet
* ifp
, struct ifnet
* p
)
1458 struct ifmediareq ifmr
;
1460 /* generate a link event based on the state of the underlying interface */
1461 bzero(&ifmr
, sizeof(ifmr
));
1462 snprintf(ifmr
.ifm_name
, sizeof(ifmr
.ifm_name
),
1463 "%s%d", ifnet_name(p
), ifnet_unit(p
));
1464 if (ifnet_ioctl(p
, 0, SIOCGIFMEDIA
, &ifmr
) == 0
1465 && ifmr
.ifm_count
> 0 && ifmr
.ifm_status
& IFM_AVALID
) {
1468 event
= (ifmr
.ifm_status
& IFM_ACTIVE
)
1469 ? KEV_DL_LINK_ON
: KEV_DL_LINK_OFF
;
1470 interface_link_event(ifp
, event
);
1476 vlan_unconfig(ifvlan_ref ifv
, int need_to_wait
)
1478 struct ifnet
* ifp
= ifv
->ifv_ifp
;
1479 int last_vlan
= FALSE
;
1480 int need_ifv_release
= 0;
1481 int need_vlp_release
= 0;
1483 vlan_parent_ref vlp
;
1485 vlan_assert_lock_held();
1492 vlan_parent_retain(vlp
);
1493 vlan_parent_wait(vlp
, "vlan_unconfig");
1495 /* check again because another thread could be in vlan_unconfig */
1496 if (ifv
!= ifnet_get_ifvlan(ifp
)) {
1499 if (ifv
->ifv_vlp
!= vlp
) {
1500 /* vlan parent changed */
1505 /* ifv has a reference on vlp, need to remove it */
1509 /* remember whether we're the last VLAN on the parent */
1510 if (LIST_NEXT(LIST_FIRST(&vlp
->vlp_vlan_list
), ifv_vlan_list
) == NULL
) {
1511 if (g_vlan
->verbose
) {
1512 printf("vlan_unconfig: last vlan on %s%d\n",
1513 ifnet_name(p
), ifnet_unit(p
));
1518 /* back-out any effect our mtu might have had on the parent */
1519 (void)ifvlan_new_mtu(ifv
, ETHERMTU
- ifv
->ifv_mtufudge
);
1523 /* un-join multicast on parent interface */
1524 (void)multicast_list_remove(&ifv
->ifv_multicast
);
1526 /* Clear our MAC address. */
1527 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_L2VLAN
);
1529 /* detach VLAN "protocol" */
1531 (void)vlan_detach_protocol(p
);
1536 /* return to the state we were in before SIFVLAN */
1537 ifnet_set_mtu(ifp
, 0);
1538 ifnet_set_flags(ifp
, 0,
1539 IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
| IFF_RUNNING
);
1540 ifnet_set_offload(ifp
, 0);
1541 ifv
->ifv_mtufudge
= 0;
1543 /* Disconnect from parent. */
1544 vlan_parent_remove_vlan(vlp
, ifv
);
1547 /* vlan_parent has reference to ifv, remove it */
1550 /* from this point on, no more referencing ifv */
1551 if (last_vlan
&& !vlan_parent_flags_detaching(vlp
)) {
1552 /* the vlan parent has no more VLAN's */
1553 ifnet_set_eflags(p
, 0, IFEF_VLAN
);
1554 LIST_REMOVE(vlp
, vlp_parent_list
);
1556 /* one for being in the list */
1559 /* release outside of the lock below */
1565 vlan_parent_signal(vlp
, "vlan_unconfig");
1568 while (need_ifv_release
--) {
1569 ifvlan_release(ifv
);
1571 while (need_vlp_release
--) { /* references to vlp */
1572 vlan_parent_release(vlp
);
1579 vlan_set_promisc(struct ifnet
* ifp
)
1583 vlan_parent_ref vlp
;
1586 ifv
= ifnet_get_ifvlan_retained(ifp
);
1596 if ((ifnet_flags(ifp
) & IFF_PROMISC
) != 0) {
1597 if (!ifvlan_flags_promisc(ifv
)) {
1598 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 1);
1600 ifvlan_flags_set_promisc(ifv
);
1604 if (ifvlan_flags_promisc(ifv
)) {
1605 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 0);
1607 ifvlan_flags_clear_promisc(ifv
);
1614 ifvlan_release(ifv
);
1620 ifvlan_new_mtu(ifvlan_ref ifv
, int mtu
)
1622 struct ifdevmtu
* devmtu_p
;
1624 struct ifnet
* ifp
= ifv
->ifv_ifp
;
1628 vlan_parent_ref vlp
;
1630 vlan_assert_lock_held();
1632 devmtu_p
= &vlp
->vlp_devmtu
;
1633 req_mtu
= mtu
+ ifv
->ifv_mtufudge
;
1634 if (req_mtu
> devmtu_p
->ifdm_max
|| req_mtu
< devmtu_p
->ifdm_min
) {
1637 max_mtu
= vlan_parent_find_max_mtu(vlp
, ifv
);
1638 if (req_mtu
> max_mtu
) {
1641 else if (max_mtu
< devmtu_p
->ifdm_current
) {
1645 struct ifnet
* p
= vlp
->vlp_ifp
;
1647 error
= siocsifaltmtu(p
, new_mtu
);
1652 devmtu_p
->ifdm_current
= new_mtu
;
1654 ifnet_set_mtu(ifp
, mtu
);
1660 vlan_set_mtu(struct ifnet
* ifp
, int mtu
)
1664 vlan_parent_ref vlp
;
1666 if (mtu
< IF_MINMTU
) {
1670 ifv
= ifnet_get_ifvlan_retained(ifp
);
1675 vlp
= ifvlan_get_vlan_parent_retained(ifv
);
1678 ifvlan_release(ifv
);
1684 vlan_parent_wait(vlp
, "vlan_set_mtu");
1686 /* check again, something might have changed */
1687 if (ifnet_get_ifvlan(ifp
) != ifv
1688 || ifvlan_flags_detaching(ifv
)) {
1692 if (ifv
->ifv_vlp
!= vlp
) {
1693 /* vlan parent changed */
1696 if (vlan_parent_flags_detaching(vlp
)) {
1702 error
= ifvlan_new_mtu(ifv
, mtu
);
1705 vlan_parent_signal(vlp
, "vlan_set_mtu");
1707 vlan_parent_release(vlp
);
1708 ifvlan_release(ifv
);
1714 vlan_ioctl(ifnet_t ifp
, u_long cmd
, void * data
)
1716 struct ifdevmtu
* devmtu_p
;
1718 struct ifaddr
* ifa
;
1719 struct ifmediareq
*ifmr
;
1724 user_addr_t user_addr
;
1725 vlan_parent_ref vlp
;
1728 if (ifnet_type(ifp
) != IFT_L2VLAN
) {
1729 return (EOPNOTSUPP
);
1731 ifr
= (struct ifreq
*)data
;
1732 ifa
= (struct ifaddr
*)data
;
1736 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
1739 case SIOCGIFMEDIA32
:
1740 case SIOCGIFMEDIA64
:
1742 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1743 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1745 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1747 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1749 ifmr
= (struct ifmediareq
*)data
;
1750 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
1751 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
1752 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
1754 struct ifmediareq p_ifmr
;
1756 bzero(&p_ifmr
, sizeof(p_ifmr
));
1757 error
= ifnet_ioctl(p
, 0, SIOCGIFMEDIA
, &p_ifmr
);
1759 ifmr
->ifm_active
= p_ifmr
.ifm_active
;
1760 ifmr
->ifm_current
= p_ifmr
.ifm_current
;
1761 ifmr
->ifm_mask
= p_ifmr
.ifm_mask
;
1762 ifmr
->ifm_status
= p_ifmr
.ifm_status
;
1763 ifmr
->ifm_count
= p_ifmr
.ifm_count
;
1764 /* Limit the result to the parent's current config. */
1765 if (ifmr
->ifm_count
>= 1 && user_addr
!= USER_ADDR_NULL
) {
1766 ifmr
->ifm_count
= 1;
1767 error
= copyout(&ifmr
->ifm_current
, user_addr
,
1772 ifmr
->ifm_active
= ifmr
->ifm_current
= IFM_NONE
;
1774 ifmr
->ifm_status
= IFM_AVALID
;
1775 ifmr
->ifm_count
= 1;
1776 if (user_addr
!= USER_ADDR_NULL
) {
1777 error
= copyout(&ifmr
->ifm_current
, user_addr
, sizeof(int));
1788 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1789 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1791 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1795 int min_mtu
= vlp
->vlp_devmtu
.ifdm_min
- ifv
->ifv_mtufudge
;
1796 devmtu_p
= &ifr
->ifr_devmtu
;
1797 devmtu_p
->ifdm_current
= ifnet_mtu(ifp
);
1798 devmtu_p
->ifdm_min
= max(min_mtu
, IF_MINMTU
);
1799 devmtu_p
->ifdm_max
= vlp
->vlp_devmtu
.ifdm_max
- ifv
->ifv_mtufudge
;
1802 devmtu_p
= &ifr
->ifr_devmtu
;
1803 devmtu_p
->ifdm_current
= 0;
1804 devmtu_p
->ifdm_min
= 0;
1805 devmtu_p
->ifdm_max
= 0;
1811 error
= vlan_set_mtu(ifp
, ifr
->ifr_mtu
);
1815 user_addr
= proc_is64bit(current_proc())
1816 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1817 error
= copyin(user_addr
, &vlr
, sizeof(vlr
));
1822 /* ensure nul termination */
1823 vlr
.vlr_parent
[IFNAMSIZ
- 1] = '\0';
1824 if (vlr
.vlr_parent
[0] != '\0') {
1825 if (vlr
.vlr_tag
& ~EVL_VLID_MASK
) {
1827 * Don't let the caller set up a VLAN tag with
1828 * anything except VLID bits.
1833 p
= ifunit(vlr
.vlr_parent
);
1838 if (IFNET_IS_INTCOPROC(p
)) {
1843 /* can't do VLAN over anything but ethernet or ethernet aggregate */
1844 if (ifnet_type(p
) != IFT_ETHER
1845 && ifnet_type(p
) != IFT_IEEE8023ADLAG
) {
1846 error
= EPROTONOSUPPORT
;
1849 error
= vlan_config(ifp
, p
, vlr
.vlr_tag
);
1854 /* Update promiscuous mode, if necessary. */
1855 (void)vlan_set_promisc(ifp
);
1857 /* generate a link event based on the state of the parent */
1858 vlan_link_event(ifp
, p
);
1861 int need_link_event
= FALSE
;
1864 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1865 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1867 error
= (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1870 need_link_event
= vlan_remove(ifv
, TRUE
);
1872 if (need_link_event
) {
1873 interface_link_event(ifp
, KEV_DL_LINK_OFF
);
1879 bzero(&vlr
, sizeof vlr
);
1881 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1882 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1884 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1886 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1890 snprintf(vlr
.vlr_parent
, sizeof(vlr
.vlr_parent
),
1891 "%s%d", ifnet_name(p
), ifnet_unit(p
));
1894 user_addr
= proc_is64bit(current_proc())
1895 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1896 error
= copyout(&vlr
, user_addr
, sizeof(vlr
));
1901 * For promiscuous mode, we enable promiscuous mode on
1902 * the parent if we need promiscuous on the VLAN interface.
1904 error
= vlan_set_promisc(ifp
);
1909 error
= vlan_setmulti(ifp
);
1918 vlan_if_free(struct ifnet
* ifp
)
1925 ifv
= (ifvlan_ref
)ifnet_softc(ifp
);
1929 ifvlan_release(ifv
);
1935 vlan_event(struct ifnet
* p
, __unused protocol_family_t protocol
,
1936 const struct kev_msg
* event
)
1940 /* Check if the interface we are attached to is being detached */
1941 if (event
->vendor_code
!= KEV_VENDOR_APPLE
1942 || event
->kev_class
!= KEV_NETWORK_CLASS
1943 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
1946 event_code
= event
->event_code
;
1947 switch (event_code
) {
1948 case KEV_DL_LINK_OFF
:
1949 case KEV_DL_LINK_ON
:
1950 vlan_parent_link_event(p
, event_code
);
1959 vlan_detached(ifnet_t p
, __unused protocol_family_t protocol
)
1961 if (ifnet_is_attached(p
, 0) == 0) {
1962 /* if the parent isn't attached, remove all VLANs */
1963 vlan_parent_remove_all_vlans(p
);
1969 interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
)
1972 struct kern_event_msg header
;
1974 char if_name
[IFNAMSIZ
];
1977 bzero(&event
, sizeof(event
));
1978 event
.header
.total_size
= sizeof(event
);
1979 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
1980 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
1981 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
1982 event
.header
.event_code
= event_code
;
1983 event
.header
.event_data
[0] = ifnet_family(ifp
);
1984 event
.unit
= (u_int32_t
) ifnet_unit(ifp
);
1985 strlcpy(event
.if_name
, ifnet_name(ifp
), IFNAMSIZ
);
1986 ifnet_event(ifp
, &event
.header
);
1991 vlan_parent_link_event(struct ifnet
* p
, u_int32_t event_code
)
1993 vlan_parent_ref vlp
;
1996 if ((ifnet_eflags(p
) & IFEF_VLAN
) == 0) {
2001 vlp
= parent_list_lookup(p
);
2007 vlan_parent_flags_set_link_event_required(vlp
);
2008 vlp
->vlp_event_code
= event_code
;
2009 if (vlan_parent_flags_change_in_progress(vlp
)) {
2010 /* don't block waiting to generate an event */
2014 vlan_parent_retain(vlp
);
2015 vlan_parent_wait(vlp
, "vlan_parent_link_event");
2016 vlan_parent_signal(vlp
, "vlan_parent_link_event");
2018 vlan_parent_release(vlp
);
2024 * Function: vlan_attach_protocol
2026 * Attach a DLIL protocol to the interface, using the ETHERTYPE_VLAN
2029 * The ethernet demux actually special cases VLAN to support hardware.
2030 * The demux here isn't used. The demux will return PF_VLAN for the
2031 * appropriate packets and our vlan_input function will be called.
2034 vlan_attach_protocol(struct ifnet
*ifp
)
2037 struct ifnet_attach_proto_param reg
;
2039 bzero(®
, sizeof(reg
));
2040 reg
.input
= vlan_input
;
2041 reg
.event
= vlan_event
;
2042 reg
.detached
= vlan_detached
;
2043 error
= ifnet_attach_protocol(ifp
, PF_VLAN
, ®
);
2045 printf("vlan_proto_attach(%s%d) ifnet_attach_protocol failed, %d\n",
2046 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
2052 * Function: vlan_detach_protocol
2054 * Detach our DLIL protocol from an interface
2057 vlan_detach_protocol(struct ifnet
*ifp
)
2061 error
= ifnet_detach_protocol(ifp
, PF_VLAN
);
2063 printf("vlan_proto_detach(%s%d) ifnet_detach_protocol failed, %d\n",
2064 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
2071 * DLIL interface family functions
2072 * We use the ethernet plumb functions, since that's all we support.
2073 * If we wanted to handle multiple LAN types (tokenring, etc.), we'd
2074 * call the appropriate routines for that LAN type instead of hard-coding
2078 vlan_attach_inet(struct ifnet
*ifp
, protocol_family_t protocol_family
)
2080 return (ether_attach_inet(ifp
, protocol_family
));
2084 vlan_detach_inet(struct ifnet
*ifp
, protocol_family_t protocol_family
)
2086 ether_detach_inet(ifp
, protocol_family
);
2091 vlan_attach_inet6(struct ifnet
*ifp
, protocol_family_t protocol_family
)
2093 return (ether_attach_inet6(ifp
, protocol_family
));
2097 vlan_detach_inet6(struct ifnet
*ifp
, protocol_family_t protocol_family
)
2099 ether_detach_inet6(ifp
, protocol_family
);
2103 __private_extern__
int
2104 vlan_family_init(void)
2108 error
= proto_register_plumber(PF_INET
, IFNET_FAMILY_VLAN
,
2109 vlan_attach_inet
, vlan_detach_inet
);
2111 printf("proto_register_plumber failed for AF_INET error=%d\n",
2116 error
= proto_register_plumber(PF_INET6
, IFNET_FAMILY_VLAN
,
2117 vlan_attach_inet6
, vlan_detach_inet6
);
2119 printf("proto_register_plumber failed for AF_INET6 error=%d\n",
2124 error
= vlan_clone_attach();
2126 printf("proto_register_plumber failed vlan_clone_attach error=%d\n",