2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright 1998 Massachusetts Institute of Technology
25 * Permission to use, copy, modify, and distribute this software and
26 * its documentation for any purpose and without fee is hereby
27 * granted, provided that both the above copyright notice and this
28 * permission notice appear in all copies, that both the above
29 * copyright notice and this permission notice appear in all
30 * supporting documentation, and that the name of M.I.T. not be used
31 * in advertising or publicity pertaining to distribution of the
32 * software without specific, written prior permission. M.I.T. makes
33 * no representations about the suitability of this software for any
34 * purpose. It is provided "as is" without express or implied
37 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
38 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
39 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
41 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * $FreeBSD: src/sys/net/if_vlan.c,v 1.54 2003/10/31 18:32:08 brooks Exp $
54 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
55 * Might be extended some day to also handle IEEE 802.1p priority
56 * tagging. This is sort of sneaky in the implementation, since
57 * we need to pretend to be enough of an Ethernet implementation
58 * to make arp work. The way we do this is by telling everyone
59 * that we are an Ethernet, and then catch the packets that
60 * ether_output() left on our output queue when it calls
61 * if_start(), rewrite them for use by the real outgoing interface,
62 * and ask it to send them.
66 #include <sys/param.h>
67 #include <sys/kernel.h>
68 #include <sys/malloc.h>
70 #include <sys/queue.h>
71 #include <sys/socket.h>
72 #include <sys/sockio.h>
73 #include <sys/sysctl.h>
74 #include <sys/systm.h>
75 #include <sys/kern_event.h>
78 #include <net/ethernet.h>
80 #include <net/if_arp.h>
81 #include <net/if_dl.h>
82 #include <net/if_ether.h>
83 #include <net/if_types.h>
84 #include <net/if_vlan_var.h>
85 #include <libkern/OSAtomic.h>
89 #include <kern/locks.h>
92 #include <netinet/in.h>
93 #include <netinet/if_ether.h>
96 #include <net/if_media.h>
97 #include <net/multicast_list.h>
99 #define IF_MAXUNIT 0x7fff /* historical value */
101 #define VLANNAME "vlan"
103 typedef int (bpf_callback_func
)(struct ifnet
*, struct mbuf
*);
104 typedef int (if_set_bpf_tap_func
)(struct ifnet
*ifp
, int mode
, bpf_callback_func
* func
);
109 static __inline__ lck_grp_t
*
110 my_lck_grp_alloc_init(const char * grp_name
)
113 lck_grp_attr_t
* grp_attrs
;
115 grp_attrs
= lck_grp_attr_alloc_init();
116 grp
= lck_grp_alloc_init(grp_name
, grp_attrs
);
117 lck_grp_attr_free(grp_attrs
);
121 static __inline__ lck_mtx_t
*
122 my_lck_mtx_alloc_init(lck_grp_t
* lck_grp
)
124 lck_attr_t
* lck_attrs
;
127 lck_attrs
= lck_attr_alloc_init();
128 lck_mtx
= lck_mtx_alloc_init(lck_grp
, lck_attrs
);
129 lck_attr_free(lck_attrs
);
133 static lck_mtx_t
* vlan_lck_mtx
;
135 static __inline__
void
138 lck_grp_t
* vlan_lck_grp
;
140 vlan_lck_grp
= my_lck_grp_alloc_init("if_vlan");
141 vlan_lck_mtx
= my_lck_mtx_alloc_init(vlan_lck_grp
);
144 static __inline__
void
145 vlan_assert_lock_held(void)
147 lck_mtx_assert(vlan_lck_mtx
, LCK_MTX_ASSERT_OWNED
);
151 static __inline__
void
152 vlan_assert_lock_not_held(void)
154 lck_mtx_assert(vlan_lck_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
158 static __inline__
void
161 lck_mtx_lock(vlan_lck_mtx
);
165 static __inline__
void
168 lck_mtx_unlock(vlan_lck_mtx
);
173 ** vlan structures, types
176 LIST_HEAD(vlan_parent_list
, vlan_parent
);
178 LIST_HEAD(ifvlan_list
, ifvlan
);
180 typedef struct vlan_parent
{
181 LIST_ENTRY(vlan_parent
) vlp_parent_list
;/* list of parents */
182 struct ifnet
* vlp_ifp
; /* interface */
183 struct ifvlan_list vlp_vlan_list
; /* list of VLAN's */
184 #define VLPF_SUPPORTS_VLAN_MTU 0x1
185 #define VLPF_CHANGE_IN_PROGRESS 0x2
186 #define VLPF_DETACHING 0x4
188 struct ifdevmtu vlp_devmtu
;
189 UInt32 vlp_retain_count
;
190 } vlan_parent
, * vlan_parent_ref
;
193 LIST_ENTRY(ifvlan
) ifv_vlan_list
;
194 char ifv_name
[IFNAMSIZ
]; /* our unique id */
195 struct ifnet
* ifv_ifp
; /* our interface */
196 vlan_parent_ref ifv_vlp
; /* parent information */
198 u_int16_t ifvm_encaplen
;/* encapsulation length */
199 u_int16_t ifvm_mtufudge
;/* MTU fudged by this much */
200 u_int16_t ifvm_proto
; /* encapsulation ethertype */
201 u_int16_t ifvm_tag
; /* tag to apply on packets leaving if */
203 struct multicast_list ifv_multicast
;
204 #define IFVF_PROMISC 0x1 /* promiscuous mode enabled */
205 #define IFVF_DETACHING 0x2 /* interface is detaching */
206 #define IFVF_READY 0x4 /* interface is ready */
208 bpf_packet_func ifv_bpf_input
;
209 bpf_packet_func ifv_bpf_output
;
212 typedef struct ifvlan
* ifvlan_ref
;
214 typedef struct vlan_globals_s
{
215 struct vlan_parent_list parent_list
;
217 } * vlan_globals_ref
;
219 static vlan_globals_ref g_vlan
;
221 #define ifv_tag ifv_mib.ifvm_tag
222 #define ifv_encaplen ifv_mib.ifvm_encaplen
223 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
227 ** vlan_parent_ref vlp_flags in-lines
229 static __inline__
int
230 vlan_parent_flags_supports_vlan_mtu(vlan_parent_ref vlp
)
232 return ((vlp
->vlp_flags
& VLPF_SUPPORTS_VLAN_MTU
) != 0);
235 static __inline__
void
236 vlan_parent_flags_set_supports_vlan_mtu(vlan_parent_ref vlp
)
238 vlp
->vlp_flags
|= VLPF_SUPPORTS_VLAN_MTU
;
242 static __inline__
void
243 vlan_parent_flags_clear_supports_vlan_mtu(vlan_parent_ref vlp
)
245 vlp
->vlp_flags
&= ~VLPF_SUPPORTS_VLAN_MTU
;
249 static __inline__
int
250 vlan_parent_flags_change_in_progress(vlan_parent_ref vlp
)
252 return ((vlp
->vlp_flags
& VLPF_CHANGE_IN_PROGRESS
) != 0);
255 static __inline__
void
256 vlan_parent_flags_set_change_in_progress(vlan_parent_ref vlp
)
258 vlp
->vlp_flags
|= VLPF_CHANGE_IN_PROGRESS
;
262 static __inline__
void
263 vlan_parent_flags_clear_change_in_progress(vlan_parent_ref vlp
)
265 vlp
->vlp_flags
&= ~VLPF_CHANGE_IN_PROGRESS
;
269 static __inline__
int
270 vlan_parent_flags_detaching(struct vlan_parent
* vlp
)
272 return ((vlp
->vlp_flags
& VLPF_DETACHING
) != 0);
275 static __inline__
void
276 vlan_parent_flags_set_detaching(struct vlan_parent
* vlp
)
278 vlp
->vlp_flags
|= VLPF_DETACHING
;
284 ** ifvlan_flags in-lines routines
286 static __inline__
int
287 ifvlan_flags_promisc(ifvlan_ref ifv
)
289 return ((ifv
->ifv_flags
& IFVF_PROMISC
) != 0);
292 static __inline__
void
293 ifvlan_flags_set_promisc(ifvlan_ref ifv
)
295 ifv
->ifv_flags
|= IFVF_PROMISC
;
299 static __inline__
void
300 ifvlan_flags_clear_promisc(ifvlan_ref ifv
)
302 ifv
->ifv_flags
&= ~IFVF_PROMISC
;
306 static __inline__
int
307 ifvlan_flags_ready(ifvlan_ref ifv
)
309 return ((ifv
->ifv_flags
& IFVF_READY
) != 0);
312 static __inline__
void
313 ifvlan_flags_set_ready(ifvlan_ref ifv
)
315 ifv
->ifv_flags
|= IFVF_READY
;
319 static __inline__
void
320 ifvlan_flags_clear_ready(ifvlan_ref ifv
)
322 ifv
->ifv_flags
&= ~IFVF_READY
;
326 static __inline__
int
327 ifvlan_flags_detaching(ifvlan_ref ifv
)
329 return ((ifv
->ifv_flags
& IFVF_DETACHING
) != 0);
332 static __inline__
void
333 ifvlan_flags_set_detaching(ifvlan_ref ifv
)
335 ifv
->ifv_flags
|= IFVF_DETACHING
;
340 SYSCTL_DECL(_net_link
);
341 SYSCTL_NODE(_net_link
, IFT_L2VLAN
, vlan
, CTLFLAG_RW
, 0, "IEEE 802.1Q VLAN");
342 SYSCTL_NODE(_net_link_vlan
, PF_LINK
, link
, CTLFLAG_RW
, 0, "for consistency");
345 #define M_VLAN M_DEVBUF
347 static int vlan_clone_create(struct if_clone
*, int);
348 static void vlan_clone_destroy(struct ifnet
*);
349 static int vlan_input(struct mbuf
*m
, char *frame_header
, struct ifnet
*ifp
,
350 u_long protocol_family
, int sync_ok
);
351 static int vlan_output(struct ifnet
*ifp
, struct mbuf
*m
);
352 static int vlan_ioctl(ifnet_t ifp
, u_int32_t cmd
, void * addr
);
353 static int vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
,
354 bpf_packet_func func
);
355 static int vlan_attach_protocol(struct ifnet
*ifp
);
356 static int vlan_detach_protocol(struct ifnet
*ifp
);
357 static int vlan_setmulti(struct ifnet
*ifp
);
358 static int vlan_unconfig(struct ifnet
*ifp
);
359 static int vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
);
360 static void vlan_if_free(struct ifnet
* ifp
);
361 static void vlan_remove(ifvlan_ref ifv
);
362 static void vlan_if_detach(struct ifnet
* ifp
);
363 static int vlan_new_mtu(struct ifnet
* ifp
, int mtu
);
365 static struct if_clone vlan_cloner
= IF_CLONE_INITIALIZER(VLANNAME
,
370 static void interface_link_event(struct ifnet
* ifp
, u_long event_code
);
371 static void vlan_parent_link_event(vlan_parent_ref vlp
,
373 extern int dlil_input_packet(struct ifnet
*ifp
, struct mbuf
*m
, char *frame_header
);
376 vlan_globals_init(void)
380 vlan_assert_lock_not_held();
382 if (g_vlan
!= NULL
) {
385 v
= _MALLOC(sizeof(*v
), M_VLAN
, M_WAITOK
);
387 LIST_INIT(&v
->parent_list
);
391 if (g_vlan
!= NULL
) {
407 siocgifdevmtu(struct ifnet
* ifp
, struct ifdevmtu
* ifdm_p
)
412 bzero(&ifr
, sizeof(ifr
));
413 error
= dlil_ioctl(0, ifp
, SIOCGIFDEVMTU
, (caddr_t
)&ifr
);
415 *ifdm_p
= ifr
.ifr_devmtu
;
421 siocsifaltmtu(struct ifnet
* ifp
, int mtu
)
425 bzero(&ifr
, sizeof(ifr
));
427 return (dlil_ioctl(0, ifp
, SIOCSIFALTMTU
, (caddr_t
)&ifr
));
430 static __inline__
void
431 vlan_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
432 bpf_packet_func func
)
440 static __inline__
void
441 vlan_bpf_input(struct ifnet
* ifp
, struct mbuf
* m
,
442 bpf_packet_func func
, char * frame_header
,
443 int frame_header_len
, int encap_len
)
447 /* present the right header to bpf */
448 bcopy(frame_header
, frame_header
+ encap_len
, frame_header_len
);
450 m
->m_data
-= frame_header_len
;
451 m
->m_len
+= frame_header_len
;
453 m
->m_data
+= frame_header_len
;
454 m
->m_len
-= frame_header_len
;
456 /* restore the header */
457 bcopy(frame_header
+ encap_len
, frame_header
, frame_header_len
);
463 static struct ifaddr
*
464 ifaddr_byindex(int i
)
466 if (i
> if_index
|| i
== 0) {
469 return (ifnet_addrs
[i
- 1]);
473 ** vlan_parent synchronization routines
475 static __inline__
void
476 vlan_parent_retain(vlan_parent_ref vlp
)
478 OSIncrementAtomic(&vlp
->vlp_retain_count
);
481 static __inline__
void
482 vlan_parent_release(vlan_parent_ref vlp
)
484 UInt32 old_retain_count
;
486 old_retain_count
= OSDecrementAtomic(&vlp
->vlp_retain_count
);
487 switch (old_retain_count
) {
489 panic("vlan_parent_release: retain count is 0\n");
492 if (g_vlan
->verbose
) {
493 struct ifnet
* ifp
= vlp
->vlp_ifp
;
494 printf("vlan_parent_release(%s%d)\n", ifp
->if_name
,
506 * Function: vlan_parent_wait
508 * Allows a single thread to gain exclusive access to the vlan_parent
509 * data structure. Some operations take a long time to complete,
510 * and some have side-effects that we can't predict. Holding the
511 * vlan_lock() across such operations is not possible.
514 * Before calling, you must be holding the vlan_lock and have taken
515 * a reference on the vlan_parent_ref.
518 vlan_parent_wait(vlan_parent_ref vlp
, const char * msg
)
522 /* other add/remove/multicast-change in progress */
523 while (vlan_parent_flags_change_in_progress(vlp
)) {
524 if (g_vlan
->verbose
) {
525 struct ifnet
* ifp
= vlp
->vlp_ifp
;
527 printf("%s%d: %s msleep\n", ifp
->if_name
, ifp
->if_unit
, msg
);
530 (void)msleep(vlp
, vlan_lck_mtx
, PZERO
, msg
, 0);
532 /* prevent other vlan parent remove/add from taking place */
533 vlan_parent_flags_set_change_in_progress(vlp
);
534 if (g_vlan
->verbose
&& waited
) {
535 struct ifnet
* ifp
= vlp
->vlp_ifp
;
537 printf("%s: %s woke up\n", ifp
->if_name
, ifp
->if_unit
, msg
);
543 * Function: vlan_parent_signal
545 * Allows the thread that previously invoked vlan_parent_wait() to
546 * give up exclusive access to the vlan_parent data structure, and wake up
547 * any other threads waiting to access
549 * Before calling, you must be holding the vlan_lock and have taken
550 * a reference on the vlan_parent_ref.
553 vlan_parent_signal(vlan_parent_ref vlp
, const char * msg
)
555 vlan_parent_flags_clear_change_in_progress(vlp
);
556 wakeup((caddr_t
)vlp
);
557 if (g_vlan
->verbose
) {
558 struct ifnet
* ifp
= vlp
->vlp_ifp
;
560 printf("%s%d: %s wakeup\n", ifp
->if_name
, ifp
->if_unit
, msg
);
567 * Program our multicast filter. What we're actually doing is
568 * programming the multicast filter of the parent. This has the
569 * side effect of causing the parent interface to receive multicast
570 * traffic that it doesn't really want, which ends up being discarded
571 * later by the upper protocol layers. Unfortunately, there's no way
572 * to avoid this: there really is only one physical interface.
575 vlan_setmulti(struct ifnet
* ifp
)
583 ifv
= (ifvlan_ref
)ifp
->if_private
;
584 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
589 /* no parent, no need to program the multicast filter */
592 if (vlan_parent_flags_detaching(vlp
)) {
595 vlan_parent_retain(vlp
);
596 vlan_parent_wait(vlp
, "vlan_setmulti");
598 /* check again, things could have changed */
599 ifv
= (ifvlan_ref
)ifp
->if_private
;
600 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
603 if (ifv
->ifv_vlp
!= vlp
) {
604 /* vlan parent changed */
608 /* no parent, no need to program the multicast filter */
614 /* update parent interface with our multicast addresses */
615 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
620 vlan_parent_signal(vlp
, "vlan_setmulti");
628 ** vlan_parent list manipulation/lookup routines
630 static vlan_parent_ref
631 parent_list_lookup(struct ifnet
* p
)
635 LIST_FOREACH(vlp
, &g_vlan
->parent_list
, vlp_parent_list
) {
636 if (vlp
->vlp_ifp
== p
) {
644 vlan_parent_lookup_tag(vlan_parent_ref vlp
, int tag
)
648 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
649 if (tag
== ifv
->ifv_tag
) {
657 vlan_lookup_parent_and_tag(struct ifnet
* p
, int tag
)
661 vlp
= parent_list_lookup(p
);
663 return (vlan_parent_lookup_tag(vlp
, tag
));
669 vlan_parent_find_max_mtu(vlan_parent_ref vlp
, ifvlan_ref exclude_ifv
)
674 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
677 if (exclude_ifv
== ifv
) {
680 req_mtu
= ifv
->ifv_ifp
->if_mtu
+ ifv
->ifv_mtufudge
;
681 if (req_mtu
> max_mtu
) {
689 * Function: vlan_parent_create
691 * Create a vlan_parent structure to hold the VLAN's for the given
692 * interface. Add it to the list of VLAN parents.
695 vlan_parent_create(struct ifnet
* p
, vlan_parent_ref
* ret_vlp
)
701 vlp
= _MALLOC(sizeof(*vlp
), M_VLAN
, M_WAITOK
);
705 bzero(vlp
, sizeof(*vlp
));
706 error
= siocgifdevmtu(p
, &vlp
->vlp_devmtu
);
708 printf("vlan_parent_create (%s%d): siocgifdevmtu failed, %d\n",
709 p
->if_name
, p
->if_unit
, error
);
713 LIST_INIT(&vlp
->vlp_vlan_list
);
715 vlan_parent_retain(vlp
);
717 & (IF_HWASSIST_VLAN_MTU
| IF_HWASSIST_VLAN_TAGGING
)) {
718 vlan_parent_flags_set_supports_vlan_mtu(vlp
);
725 vlan_parent_remove_all_vlans(vlan_parent_ref vlp
)
730 vlan_assert_lock_held();
732 while ((ifv
= LIST_FIRST(&vlp
->vlp_vlan_list
)) != NULL
) {
735 vlan_if_detach(ifv
->ifv_ifp
);
739 /* the vlan parent has no more VLAN's */
741 ifnet_lock_exclusive(p
);
742 p
->if_eflags
&= ~IFEF_VLAN
;
744 LIST_REMOVE(vlp
, vlp_parent_list
);
746 vlan_parent_release(vlp
);
752 static __inline__
int
753 vlan_parent_no_vlans(vlan_parent_ref vlp
)
755 return (LIST_EMPTY(&vlp
->vlp_vlan_list
));
759 vlan_parent_add_vlan(vlan_parent_ref vlp
, ifvlan_ref ifv
, int tag
)
761 LIST_INSERT_HEAD(&vlp
->vlp_vlan_list
, ifv
, ifv_vlan_list
);
768 vlan_parent_remove_vlan(__unused vlan_parent_ref vlp
, ifvlan_ref ifv
)
771 LIST_REMOVE(ifv
, ifv_vlan_list
);
776 vlan_clone_attach(void)
778 if_clone_attach(&vlan_cloner
);
784 vlan_clone_create(struct if_clone
*ifc
, int unit
)
790 error
= vlan_globals_init();
794 ifv
= _MALLOC(sizeof(struct ifvlan
), M_VLAN
, M_WAITOK
);
795 bzero(ifv
, sizeof(struct ifvlan
));
796 multicast_list_init(&ifv
->ifv_multicast
);
798 /* use the interface name as the unique id for ifp recycle */
799 if ((unsigned int)snprintf(ifv
->ifv_name
, sizeof(ifv
->ifv_name
), "%s%d",
800 ifc
->ifc_name
, unit
) >= sizeof(ifv
->ifv_name
)) {
804 error
= dlil_if_acquire(APPLE_IF_FAM_VLAN
,
806 strlen(ifv
->ifv_name
),
812 ifp
->if_name
= ifc
->ifc_name
;
814 ifp
->if_family
= APPLE_IF_FAM_VLAN
;
817 /* NB: flags are not set here */
818 ifp
->if_linkmib
= &ifv
->ifv_mib
;
819 ifp
->if_linkmiblen
= sizeof ifv
->ifv_mib
;
820 /* NB: mtu is not set here */
823 ifp
->if_ioctl
= vlan_ioctl
;
824 ifp
->if_set_bpf_tap
= vlan_set_bpf_tap
;
825 ifp
->if_free
= vlan_if_free
;
826 ifp
->if_output
= vlan_output
;
827 ifp
->if_hwassist
= 0;
828 ifp
->if_addrlen
= ETHER_ADDR_LEN
; /* XXX ethernet specific */
829 ifp
->if_baudrate
= 0;
830 ifp
->if_type
= IFT_L2VLAN
;
831 ifp
->if_hdrlen
= ETHER_VLAN_ENCAP_LEN
;
833 /* XXX ethernet specific */
834 ifp
->if_broadcast
.length
= ETHER_ADDR_LEN
;
835 bcopy(etherbroadcastaddr
, ifp
->if_broadcast
.u
.buffer
, ETHER_ADDR_LEN
);
837 error
= dlil_if_attach(ifp
);
839 dlil_if_release(ifp
);
843 ifp
->if_private
= ifv
;
846 /* attach as ethernet */
847 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
852 vlan_remove(ifvlan_ref ifv
)
854 vlan_assert_lock_held();
855 ifvlan_flags_set_detaching(ifv
);
856 vlan_unconfig(ifv
->ifv_ifp
);
861 vlan_if_detach(struct ifnet
* ifp
)
863 if (dlil_if_detach(ifp
) != DLIL_WAIT_FOR_FREE
) {
870 vlan_clone_destroy(struct ifnet
*ifp
)
875 ifv
= ifp
->if_private
;
876 if (ifv
== NULL
|| ifp
->if_type
!= IFT_L2VLAN
) {
880 if (ifvlan_flags_detaching(ifv
)) {
891 vlan_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
896 ifv
= ifp
->if_private
;
897 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
902 case BPF_TAP_DISABLE
:
903 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= NULL
;
907 ifv
->ifv_bpf_input
= func
;
911 ifv
->ifv_bpf_output
= func
;
914 case BPF_TAP_INPUT_OUTPUT
:
915 ifv
->ifv_bpf_input
= ifv
->ifv_bpf_output
= func
;
925 vlan_output(struct ifnet
* ifp
, struct mbuf
* m
)
927 bpf_packet_func bpf_func
;
928 struct ether_vlan_header
* evl
;
939 if ((m
->m_flags
& M_PKTHDR
) == 0) {
944 ifv
= (ifvlan_ref
)ifp
->if_private
;
945 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)
946 || ifvlan_flags_ready(ifv
) == 0) {
958 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
959 soft_vlan
= (p
->if_hwassist
& IF_HWASSIST_VLAN_TAGGING
) == 0;
960 bpf_func
= ifv
->ifv_bpf_output
;
962 encaplen
= ifv
->ifv_encaplen
;
964 vlan_bpf_output(ifp
, m
, bpf_func
);
966 /* do not run parent's if_output() if the parent is not up */
967 if ((p
->if_flags
& (IFF_UP
| IFF_RUNNING
)) != (IFF_UP
| IFF_RUNNING
)) {
969 ifp
->if_collisions
++;
973 * If underlying interface can do VLAN tag insertion itself,
974 * just pass the packet along. However, we need some way to
975 * tell the interface where the packet came from so that it
976 * knows how to find the VLAN tag to use. We use a field in
977 * the mbuf header to store the VLAN tag, and a bit in the
978 * csum_flags field to mark the field as valid.
980 if (soft_vlan
== 0) {
981 m
->m_pkthdr
.csum_flags
|= CSUM_VLAN_TAG_VALID
;
982 m
->m_pkthdr
.vlan_tag
= tag
;
984 M_PREPEND(m
, encaplen
, M_DONTWAIT
);
986 printf("%s%d: unable to prepend VLAN header\n", ifp
->if_name
,
991 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
992 if (m
->m_len
< (int)sizeof(*evl
)) {
993 m
= m_pullup(m
, sizeof(*evl
));
995 printf("%s%d: unable to pullup VLAN header\n", ifp
->if_name
,
1003 * Transform the Ethernet header into an Ethernet header
1004 * with 802.1Q encapsulation.
1006 bcopy(mtod(m
, char *) + encaplen
,
1007 mtod(m
, char *), ETHER_HDR_LEN
);
1008 evl
= mtod(m
, struct ether_vlan_header
*);
1009 evl
->evl_proto
= evl
->evl_encap_proto
;
1010 evl
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
1011 evl
->evl_tag
= htons(tag
);
1013 return dlil_output(p
, 0, m
, NULL
, NULL
, 1);
1017 vlan_input(struct mbuf
* m
, char * frame_header
, struct ifnet
* p
,
1018 __unused u_long protocol_family
, __unused
int sync_ok
)
1020 bpf_packet_func bpf_func
= NULL
;
1021 struct ether_vlan_header
* evl
;
1022 struct ifnet
* ifp
= NULL
;
1026 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1028 * Packet is tagged, m contains a normal
1029 * Ethernet frame; the tag is stored out-of-band.
1031 m
->m_pkthdr
.csum_flags
&= ~CSUM_VLAN_TAG_VALID
;
1032 tag
= EVL_VLANOFTAG(m
->m_pkthdr
.vlan_tag
);
1033 m
->m_pkthdr
.vlan_tag
= 0;
1036 switch (p
->if_type
) {
1038 if (m
->m_len
< ETHER_VLAN_ENCAP_LEN
) {
1042 evl
= (struct ether_vlan_header
*)frame_header
;
1043 if (ntohs(evl
->evl_proto
) == ETHERTYPE_VLAN
) {
1044 /* don't allow VLAN within VLAN */
1048 tag
= EVL_VLANOFTAG(ntohs(evl
->evl_tag
));
1051 * Restore the original ethertype. We'll remove
1052 * the encapsulation after we've found the vlan
1053 * interface corresponding to the tag.
1055 evl
->evl_encap_proto
= evl
->evl_proto
;
1058 printf("vlan_demux: unsupported if type %u",
1068 if ((p
->if_eflags
& IFEF_VLAN
) == 0) {
1069 /* don't bother looking through the VLAN list */
1074 ifv
= vlan_lookup_parent_and_tag(p
, tag
);
1079 || ifvlan_flags_ready(ifv
) == 0
1080 || (ifp
->if_flags
& IFF_UP
) == 0) {
1085 bpf_func
= ifv
->ifv_bpf_input
;
1090 * Packet had an in-line encapsulation header;
1091 * remove it. The original header has already
1092 * been fixed up above.
1094 m
->m_len
-= ETHER_VLAN_ENCAP_LEN
;
1095 m
->m_data
+= ETHER_VLAN_ENCAP_LEN
;
1096 m
->m_pkthdr
.len
-= ETHER_VLAN_ENCAP_LEN
;
1097 m
->m_pkthdr
.csum_flags
= 0; /* can't trust hardware checksum */
1100 m
->m_pkthdr
.rcvif
= ifp
;
1101 (void)ifnet_stat_increment_in(ifp
, 1,
1102 m
->m_pkthdr
.len
+ ETHER_HDR_LEN
, 0);
1103 vlan_bpf_input(ifp
, m
, bpf_func
, frame_header
, ETHER_HDR_LEN
,
1104 soft_vlan
? ETHER_VLAN_ENCAP_LEN
: 0);
1105 /* We found a vlan interface, inject on that interface. */
1106 dlil_input_packet(ifp
, m
, frame_header
);
1108 /* Send priority-tagged packet up through the parent */
1109 dlil_input_packet(p
, m
, frame_header
);
1114 #define VLAN_CONFIG_PROGRESS_VLP_RETAINED 0x1
1115 #define VLAN_CONFIG_PROGRESS_IN_LIST 0x2
1118 vlan_config(struct ifnet
* ifp
, struct ifnet
* p
, int tag
)
1122 ifvlan_ref ifv
= NULL
;
1123 struct ifaddr
* ifa1
;
1124 struct ifaddr
* ifa2
;
1125 vlan_parent_ref new_vlp
= NULL
;
1126 int need_vlp_release
= 0;
1127 u_int32_t progress
= 0;
1128 struct sockaddr_dl
*sdl1
;
1129 struct sockaddr_dl
*sdl2
;
1130 vlan_parent_ref vlp
= NULL
;
1132 /* pre-allocate space for vlan_parent, in case we're first */
1133 error
= vlan_parent_create(p
, &new_vlp
);
1139 ifv
= (ifvlan_ref
)ifp
->if_private
;
1140 if (ifv
!= NULL
&& ifv
->ifv_vlp
!= NULL
) {
1142 vlan_parent_release(new_vlp
);
1145 vlp
= parent_list_lookup(p
);
1147 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1148 /* already a VLAN with that tag on this interface */
1154 /* we're the first VLAN on this interface */
1155 LIST_INSERT_HEAD(&g_vlan
->parent_list
, new_vlp
, vlp_parent_list
);
1159 /* need to wait to ensure no one else is trying to add/remove */
1160 vlan_parent_retain(vlp
);
1161 progress
|= VLAN_CONFIG_PROGRESS_VLP_RETAINED
;
1162 vlan_parent_wait(vlp
, "vlan_config");
1164 ifv
= (ifvlan_ref
)ifp
->if_private
;
1169 if (vlan_parent_flags_detaching(vlp
)
1170 || ifvlan_flags_detaching(ifv
) || ifv
->ifv_vlp
!= NULL
) {
1175 /* check again because someone might have gotten in */
1176 if (vlan_parent_lookup_tag(vlp
, tag
) != NULL
) {
1177 /* already a VLAN with that tag on this interface */
1182 if (vlan_parent_no_vlans(vlp
)) {
1185 vlan_parent_add_vlan(vlp
, ifv
, tag
);
1186 progress
|= VLAN_CONFIG_PROGRESS_IN_LIST
;
1188 /* check whether bond interface is using parent interface */
1189 ifnet_lock_exclusive(p
);
1190 if ((p
->if_eflags
& IFEF_BOND
) != 0) {
1192 /* don't allow VLAN over interface that's already part of a bond */
1196 /* prevent BOND interface from using it */
1197 p
->if_eflags
|= IFEF_VLAN
;
1202 /* attach our VLAN "protocol" to the interface */
1203 error
= vlan_attach_protocol(p
);
1208 /* mark the parent interface up */
1209 ifnet_lock_exclusive(p
);
1210 p
->if_flags
|= IFF_UP
;
1212 (void)dlil_ioctl(0, p
, SIOCSIFFLAGS
, (caddr_t
)NULL
);
1215 /* configure parent to receive our multicast addresses */
1216 error
= multicast_list_program(&ifv
->ifv_multicast
, ifp
, p
);
1219 (void)vlan_detach_protocol(p
);
1225 /* no failures past this point */
1228 ifv
->ifv_encaplen
= ETHER_VLAN_ENCAP_LEN
;
1230 if (vlan_parent_flags_supports_vlan_mtu(vlp
)) {
1231 ifv
->ifv_mtufudge
= 0;
1234 * Fudge the MTU by the encapsulation size. This
1235 * makes us incompatible with strictly compliant
1236 * 802.1Q implementations, but allows us to use
1237 * the feature with other NetBSD implementations,
1238 * which might still be useful.
1240 ifv
->ifv_mtufudge
= ifv
->ifv_encaplen
;
1242 ifp
->if_mtu
= ETHERMTU
- ifv
->ifv_mtufudge
;
1245 * Copy only a selected subset of flags from the parent.
1246 * Other flags are none of our business.
1248 ifp
->if_flags
|= (p
->if_flags
&
1249 (IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
));
1251 * If the parent interface can do hardware-assisted
1252 * VLAN encapsulation, then propagate its hardware-
1253 * assisted checksumming flags.
1255 if (p
->if_hwassist
& IF_HWASSIST_VLAN_TAGGING
) {
1256 ifp
->if_hwassist
|= IF_HWASSIST_CSUM_FLAGS(p
->if_hwassist
);
1259 /* set our ethernet address to that of the parent */
1260 ifa1
= ifaddr_byindex(ifp
->if_index
);
1261 ifa2
= ifaddr_byindex(p
->if_index
);
1262 sdl1
= (struct sockaddr_dl
*)ifa1
->ifa_addr
;
1263 sdl2
= (struct sockaddr_dl
*)ifa2
->ifa_addr
;
1264 sdl1
->sdl_type
= IFT_ETHER
;
1265 sdl1
->sdl_alen
= ETHER_ADDR_LEN
;
1266 bcopy(LLADDR(sdl2
), LLADDR(sdl1
), ETHER_ADDR_LEN
);
1268 ifp
->if_flags
|= IFF_RUNNING
;
1269 ifvlan_flags_set_ready(ifv
);
1270 vlan_parent_signal(vlp
, "vlan_config");
1272 if (new_vlp
!= vlp
) {
1273 /* throw it away, it wasn't needed */
1274 vlan_parent_release(new_vlp
);
1279 vlan_assert_lock_held();
1280 vlan_parent_signal(vlp
, "vlan_config");
1283 if ((progress
& VLAN_CONFIG_PROGRESS_IN_LIST
) != 0) {
1284 vlan_parent_remove_vlan(vlp
, ifv
);
1286 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1287 /* the vlan parent has no more VLAN's */
1288 ifnet_lock_exclusive(p
);
1289 p
->if_eflags
&= ~IFEF_VLAN
;
1291 LIST_REMOVE(vlp
, vlp_parent_list
);
1292 /* release outside of the lock below */
1293 need_vlp_release
= 1;
1297 if ((progress
& VLAN_CONFIG_PROGRESS_VLP_RETAINED
) != 0) {
1298 vlan_parent_release(vlp
);
1300 if (need_vlp_release
) {
1301 vlan_parent_release(vlp
);
1303 if (new_vlp
!= vlp
) {
1304 vlan_parent_release(new_vlp
);
1310 vlan_link_event(struct ifnet
* ifp
, struct ifnet
* p
)
1312 struct ifmediareq ifmr
;
1314 /* generate a link event based on the state of the underlying interface */
1315 bzero(&ifmr
, sizeof(ifmr
));
1316 snprintf(ifmr
.ifm_name
, sizeof(ifmr
.ifm_name
),
1317 "%s%d", p
->if_name
, p
->if_unit
);
1318 if ((*p
->if_ioctl
)(p
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) == 0
1319 && ifmr
.ifm_count
> 0 && ifmr
.ifm_status
& IFM_AVALID
) {
1322 event
= (ifmr
.ifm_status
& IFM_ACTIVE
)
1323 ? KEV_DL_LINK_ON
: KEV_DL_LINK_OFF
;
1324 interface_link_event(ifp
, event
);
1330 vlan_unconfig(struct ifnet
* ifp
)
1333 struct ifaddr
* ifa
;
1336 int need_vlp_release
= 0;
1338 struct sockaddr_dl
*sdl
;
1339 vlan_parent_ref vlp
;
1341 vlan_assert_lock_held();
1342 ifv
= (ifvlan_ref
)ifp
->if_private
;
1350 vlan_parent_retain(vlp
);
1351 vlan_parent_wait(vlp
, "vlan_unconfig");
1353 /* check again because another thread could be in vlan_unconfig */
1354 ifv
= (ifvlan_ref
)ifp
->if_private
;
1358 if (ifv
->ifv_vlp
!= vlp
) {
1359 /* vlan parent changed */
1365 /* remember whether we're the last VLAN on the parent */
1366 if (LIST_NEXT(LIST_FIRST(&vlp
->vlp_vlan_list
), ifv_vlan_list
) == NULL
) {
1367 if (g_vlan
->verbose
) {
1368 printf("vlan_unconfig: last vlan on %s%d\n",
1369 p
->if_name
, p
->if_unit
);
1374 /* back-out any effect our mtu might have had on the parent */
1375 (void)vlan_new_mtu(ifp
, ETHERMTU
- ifv
->ifv_mtufudge
);
1379 /* detach VLAN "protocol" */
1381 (void)vlan_detach_protocol(p
);
1384 /* un-join multicast on parent interface */
1385 (void)multicast_list_remove(&ifv
->ifv_multicast
);
1389 /* Disconnect from parent. */
1390 vlan_parent_remove_vlan(vlp
, ifv
);
1392 /* return to the state we were in before SIFVLAN */
1394 ifp
->if_flags
&= ~(IFF_BROADCAST
| IFF_MULTICAST
1395 | IFF_SIMPLEX
| IFF_RUNNING
);
1396 ifp
->if_hwassist
= 0;
1398 ifv
->ifv_mtufudge
= 0;
1400 /* Clear our MAC address. */
1401 ifa
= ifaddr_byindex(ifp
->if_index
);
1402 sdl
= (struct sockaddr_dl
*)(ifa
->ifa_addr
);
1403 sdl
->sdl_type
= IFT_L2VLAN
;
1405 bzero(LLADDR(sdl
), ETHER_ADDR_LEN
);
1407 if (!vlan_parent_flags_detaching(vlp
) && vlan_parent_no_vlans(vlp
)) {
1408 /* the vlan parent has no more VLAN's */
1409 ifnet_lock_exclusive(p
);
1410 p
->if_eflags
&= ~IFEF_VLAN
;
1412 LIST_REMOVE(vlp
, vlp_parent_list
);
1413 /* release outside of the lock below */
1418 vlan_parent_signal(vlp
, "vlan_unconfig");
1420 vlan_parent_release(vlp
); /* one because we waited */
1422 while (need_vlp_release
--) {
1423 vlan_parent_release(vlp
);
1430 vlan_set_promisc(struct ifnet
* ifp
)
1434 vlan_parent_ref vlp
;
1437 ifv
= (ifvlan_ref
)ifp
->if_private
;
1438 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1439 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1447 if ((ifp
->if_flags
& IFF_PROMISC
) != 0) {
1448 if (!ifvlan_flags_promisc(ifv
)) {
1449 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 1);
1451 ifvlan_flags_set_promisc(ifv
);
1455 if (ifvlan_flags_promisc(ifv
)) {
1456 error
= ifnet_set_promiscuous(vlp
->vlp_ifp
, 0);
1458 ifvlan_flags_clear_promisc(ifv
);
1468 vlan_new_mtu(struct ifnet
* ifp
, int mtu
)
1470 struct ifdevmtu
* devmtu_p
;
1476 vlan_parent_ref vlp
;
1478 vlan_assert_lock_held();
1479 ifv
= (ifvlan_ref
)ifp
->if_private
;
1481 devmtu_p
= &vlp
->vlp_devmtu
;
1482 req_mtu
= mtu
+ ifv
->ifv_mtufudge
;
1483 if (req_mtu
> devmtu_p
->ifdm_max
|| req_mtu
< devmtu_p
->ifdm_min
) {
1486 max_mtu
= vlan_parent_find_max_mtu(vlp
, ifv
);
1487 if (req_mtu
> max_mtu
) {
1490 else if (max_mtu
< devmtu_p
->ifdm_current
) {
1494 struct ifnet
* p
= vlp
->vlp_ifp
;
1496 error
= siocsifaltmtu(p
, new_mtu
);
1501 devmtu_p
->ifdm_current
= new_mtu
;
1509 vlan_set_mtu(struct ifnet
* ifp
, int mtu
)
1513 vlan_parent_ref vlp
;
1515 if (mtu
< IF_MINMTU
) {
1519 ifv
= (ifvlan_ref
)ifp
->if_private
;
1520 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1522 return ((ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
);
1525 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1532 vlan_parent_retain(vlp
);
1533 vlan_parent_wait(vlp
, "vlan_set_mtu");
1535 /* check again, something might have changed */
1536 ifv
= (ifvlan_ref
)ifp
->if_private
;
1537 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1538 error
= (ifv
== NULL
) ? EOPNOTSUPP
: EBUSY
;
1541 if (ifv
->ifv_vlp
!= vlp
) {
1542 /* vlan parent changed */
1545 if (vlp
== NULL
|| vlan_parent_flags_detaching(vlp
)) {
1551 error
= vlan_new_mtu(ifp
, mtu
);
1554 vlan_parent_signal(vlp
, "vlan_set_mtu");
1556 vlan_parent_release(vlp
);
1562 vlan_ioctl(ifnet_t ifp
, u_int32_t cmd
, void * data
)
1564 struct ifdevmtu
* devmtu_p
;
1566 struct ifaddr
* ifa
;
1567 struct ifmediareq64
* ifmr
;
1572 user_addr_t user_addr
;
1573 vlan_parent_ref vlp
;
1576 if (ifp
->if_type
!= IFT_L2VLAN
) {
1577 return (EOPNOTSUPP
);
1579 ifr
= (struct ifreq
*)data
;
1580 ifa
= (struct ifaddr
*)data
;
1584 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
1587 case SIOCGIFMEDIA64
:
1590 ifv
= (ifvlan_ref
)ifp
->if_private
;
1591 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1593 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1595 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1597 ifmr
= (struct ifmediareq64
*)data
;
1598 user_addr
= (cmd
== SIOCGIFMEDIA64
)
1599 ? ifmr
->ifm_ifmu
.ifmu_ulist64
1600 : CAST_USER_ADDR_T(ifmr
->ifm_ifmu
.ifmu_ulist32
);
1602 struct ifmediareq64 p_ifmr
;
1604 bzero(&p_ifmr
, sizeof(p_ifmr
));
1605 error
= dlil_ioctl(0, p
, SIOCGIFMEDIA
, (caddr_t
)&p_ifmr
);
1607 ifmr
->ifm_active
= p_ifmr
.ifm_active
;
1608 ifmr
->ifm_current
= p_ifmr
.ifm_current
;
1609 ifmr
->ifm_mask
= p_ifmr
.ifm_mask
;
1610 ifmr
->ifm_status
= p_ifmr
.ifm_status
;
1611 ifmr
->ifm_count
= p_ifmr
.ifm_count
;
1612 /* Limit the result to the parent's current config. */
1613 if (ifmr
->ifm_count
>= 1 && user_addr
!= USER_ADDR_NULL
) {
1614 ifmr
->ifm_count
= 1;
1615 error
= copyout(&ifmr
->ifm_current
, user_addr
,
1620 ifmr
->ifm_active
= ifmr
->ifm_current
= IFM_NONE
;
1622 ifmr
->ifm_status
= IFM_AVALID
;
1623 ifmr
->ifm_count
= 1;
1624 if (user_addr
!= USER_ADDR_NULL
) {
1625 error
= copyout(&ifmr
->ifm_current
, user_addr
, sizeof(int));
1636 ifv
= (ifvlan_ref
)ifp
->if_private
;
1637 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1639 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1643 int min_mtu
= vlp
->vlp_devmtu
.ifdm_min
- ifv
->ifv_mtufudge
;
1644 devmtu_p
= &ifr
->ifr_devmtu
;
1645 devmtu_p
->ifdm_current
= ifp
->if_mtu
;
1646 devmtu_p
->ifdm_min
= max(min_mtu
, IF_MINMTU
);
1647 devmtu_p
->ifdm_max
= vlp
->vlp_devmtu
.ifdm_max
- ifv
->ifv_mtufudge
;
1650 devmtu_p
= &ifr
->ifr_devmtu
;
1651 devmtu_p
->ifdm_current
= 0;
1652 devmtu_p
->ifdm_min
= 0;
1653 devmtu_p
->ifdm_max
= 0;
1659 error
= vlan_set_mtu(ifp
, ifr
->ifr_mtu
);
1663 user_addr
= proc_is64bit(current_proc())
1664 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1665 error
= copyin(user_addr
, &vlr
, sizeof(vlr
));
1670 if (vlr
.vlr_parent
[0] != '\0') {
1671 if (vlr
.vlr_tag
& ~EVL_VLID_MASK
) {
1673 * Don't let the caller set up a VLAN tag with
1674 * anything except VLID bits.
1679 p
= ifunit(vlr
.vlr_parent
);
1684 /* can't do VLAN over anything but ethernet or ethernet aggregate */
1685 if (p
->if_type
!= IFT_ETHER
&& p
->if_type
!= IFT_IEEE8023ADLAG
) {
1686 error
= EPROTONOSUPPORT
;
1689 error
= vlan_config(ifp
, p
, vlr
.vlr_tag
);
1694 /* Update promiscuous mode, if necessary. */
1695 (void)vlan_set_promisc(ifp
);
1697 /* generate a link event based on the state of the parent */
1698 vlan_link_event(ifp
, p
);
1701 ifv
= (ifvlan_ref
)ifp
->if_private
;
1702 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1704 error
= (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1707 error
= vlan_unconfig(ifp
);
1710 interface_link_event(ifp
, KEV_DL_LINK_OFF
);
1716 bzero(&vlr
, sizeof vlr
);
1718 ifv
= (ifvlan_ref
)ifp
->if_private
;
1719 if (ifv
== NULL
|| ifvlan_flags_detaching(ifv
)) {
1721 return (ifv
== NULL
? EOPNOTSUPP
: EBUSY
);
1723 p
= (ifv
->ifv_vlp
== NULL
) ? NULL
: ifv
->ifv_vlp
->vlp_ifp
;
1727 snprintf(vlr
.vlr_parent
, sizeof(vlr
.vlr_parent
),
1728 "%s%d", p
->if_name
, p
->if_unit
);
1731 user_addr
= proc_is64bit(current_proc())
1732 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
1733 error
= copyout(&vlr
, user_addr
, sizeof(vlr
));
1738 * For promiscuous mode, we enable promiscuous mode on
1739 * the parent if we need promiscuous on the VLAN interface.
1741 error
= vlan_set_promisc(ifp
);
1746 error
= vlan_setmulti(ifp
);
1755 vlan_if_free(struct ifnet
* ifp
)
1763 ifv
= (ifvlan_ref
)ifp
->if_private
;
1768 ifp
->if_private
= NULL
;
1770 dlil_if_release(ifp
);
1775 vlan_event(struct ifnet
* p
, struct kev_msg
* event
)
1777 vlan_parent_ref vlp
;
1779 /* Check if the interface we are attached to is being detached */
1780 if (event
->vendor_code
!= KEV_VENDOR_APPLE
1781 || event
->kev_class
!= KEV_NETWORK_CLASS
1782 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
1785 switch (event
->event_code
) {
1786 case KEV_DL_IF_DETACHING
:
1787 case KEV_DL_LINK_OFF
:
1788 case KEV_DL_LINK_ON
:
1794 if ((p
->if_eflags
& IFEF_VLAN
) == 0) {
1799 vlp
= parent_list_lookup(p
);
1805 switch (event
->event_code
) {
1806 case KEV_DL_IF_DETACHING
:
1807 vlan_parent_flags_set_detaching(vlp
);
1808 vlan_parent_remove_all_vlans(vlp
);
1811 case KEV_DL_LINK_OFF
:
1812 case KEV_DL_LINK_ON
:
1813 vlan_parent_link_event(vlp
, event
->event_code
);
1823 interface_link_event(struct ifnet
* ifp
, u_long event_code
)
1826 struct kern_event_msg header
;
1828 char if_name
[IFNAMSIZ
];
1831 event
.header
.total_size
= sizeof(event
);
1832 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
1833 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
1834 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
1835 event
.header
.event_code
= event_code
;
1836 event
.header
.event_data
[0] = ifp
->if_family
;
1837 event
.unit
= (u_long
) ifp
->if_unit
;
1838 strncpy(event
.if_name
, ifp
->if_name
, IFNAMSIZ
);
1839 dlil_event(ifp
, &event
.header
);
1844 vlan_parent_link_event(vlan_parent_ref vlp
, u_long event_code
)
1848 LIST_FOREACH(ifv
, &vlp
->vlp_vlan_list
, ifv_vlan_list
) {
1849 interface_link_event(ifv
->ifv_ifp
, event_code
);
1856 * Function: vlan_attach_protocol
1858 * Attach a DLIL protocol to the interface, using the ETHERTYPE_VLAN
1861 * The ethernet demux actually special cases VLAN to support hardware.
1862 * The demux here isn't used. The demux will return PF_VLAN for the
1863 * appropriate packets and our vlan_input function will be called.
1866 vlan_attach_protocol(struct ifnet
*ifp
)
1869 struct dlil_proto_reg_str reg
;
1871 bzero(®
, sizeof(reg
));
1872 TAILQ_INIT(®
.demux_desc_head
);
1873 reg
.interface_family
= ifp
->if_family
;
1874 reg
.unit_number
= ifp
->if_unit
;
1875 reg
.input
= vlan_input
;
1876 reg
.event
= vlan_event
;
1877 reg
.protocol_family
= PF_VLAN
;
1878 error
= dlil_attach_protocol(®
);
1880 printf("vlan_proto_attach(%s%d) dlil_attach_protocol failed, %d\n",
1881 ifp
->if_name
, ifp
->if_unit
, error
);
1887 * Function: vlan_detach_protocol
1889 * Detach our DLIL protocol from an interface
1892 vlan_detach_protocol(struct ifnet
*ifp
)
1896 error
= dlil_detach_protocol(ifp
, PF_VLAN
);
1898 printf("vlan_proto_detach(%s%d) dlil_detach_protocol failed, %d\n",
1899 ifp
->if_name
, ifp
->if_unit
, error
);
1906 * DLIL interface family functions
1907 * We use the ethernet dlil functions, since that's all we support.
1908 * If we wanted to handle multiple LAN types (tokenring, etc.), we'd
1909 * call the appropriate routines for that LAN type instead of hard-coding
1912 extern int ether_add_if(struct ifnet
*ifp
);
1913 extern int ether_del_if(struct ifnet
*ifp
);
1914 extern int ether_init_if(struct ifnet
*ifp
);
1915 extern int ether_add_proto_old(struct ifnet
*ifp
, u_long protocol_family
,
1916 struct ddesc_head_str
*desc_head
);
1918 extern int ether_attach_inet(struct ifnet
*ifp
, u_long protocol_family
);
1919 extern int ether_detach_inet(struct ifnet
*ifp
, u_long protocol_family
);
1920 extern int ether_attach_inet6(struct ifnet
*ifp
, u_long protocol_family
);
1921 extern int ether_detach_inet6(struct ifnet
*ifp
, u_long protocol_family
);
1924 vlan_attach_inet(struct ifnet
*ifp
, u_long protocol_family
)
1926 return (ether_attach_inet(ifp
, protocol_family
));
1930 vlan_detach_inet(struct ifnet
*ifp
, u_long protocol_family
)
1932 return (ether_detach_inet(ifp
, protocol_family
));
1936 vlan_attach_inet6(struct ifnet
*ifp
, u_long protocol_family
)
1938 return (ether_attach_inet6(ifp
, protocol_family
));
1942 vlan_detach_inet6(struct ifnet
*ifp
, u_long protocol_family
)
1944 return (ether_detach_inet6(ifp
, protocol_family
));
1948 vlan_add_if(struct ifnet
*ifp
)
1950 return (ether_add_if(ifp
));
1954 vlan_del_if(struct ifnet
*ifp
)
1956 return (ether_del_if(ifp
));
1960 __private_extern__
int
1961 vlan_family_init(void)
1964 struct dlil_ifmod_reg_str ifmod_reg
;
1966 bzero(&ifmod_reg
, sizeof(ifmod_reg
));
1967 ifmod_reg
.add_if
= vlan_add_if
;
1968 ifmod_reg
.del_if
= vlan_del_if
;
1969 ifmod_reg
.init_if
= NULL
;
1970 ifmod_reg
.add_proto
= ether_add_proto_old
;
1971 ifmod_reg
.del_proto
= ether_del_proto
;
1972 ifmod_reg
.ifmod_ioctl
= ether_ioctl
;
1973 ifmod_reg
.shutdown
= NULL
;
1975 if (dlil_reg_if_modules(APPLE_IF_FAM_VLAN
, &ifmod_reg
)) {
1976 printf("WARNING: vlan_family_init -- "
1977 "Can't register if family modules\n");
1982 error
= dlil_reg_proto_module(PF_INET
, APPLE_IF_FAM_VLAN
,
1983 vlan_attach_inet
, vlan_detach_inet
);
1985 printf("dlil_reg_proto_module failed for AF_INET error=%d\n",
1989 error
= dlil_reg_proto_module(PF_INET6
, APPLE_IF_FAM_VLAN
,
1990 vlan_attach_inet6
, vlan_detach_inet6
);
1992 printf("dlil_reg_proto_module failed for AF_INET6 error=%d\n",
1996 vlan_clone_attach();