]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
f427ee49 | 2 | * Copyright (c) 2003-2020 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright 1998 Massachusetts Institute of Technology | |
30 | * | |
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 | |
41 | * warranty. | |
0a7de745 | 42 | * |
1c79356b A |
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 | |
54 | * SUCH DAMAGE. | |
55 | * | |
4a249263 | 56 | * $FreeBSD: src/sys/net/if_vlan.c,v 1.54 2003/10/31 18:32:08 brooks Exp $ |
1c79356b A |
57 | */ |
58 | ||
59 | /* | |
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 | |
4a249263 | 66 | * ether_output() left on our output queue when it calls |
1c79356b A |
67 | * if_start(), rewrite them for use by the real outgoing interface, |
68 | * and ask it to send them. | |
1c79356b A |
69 | */ |
70 | ||
1c79356b A |
71 | |
72 | #include <sys/param.h> | |
73 | #include <sys/kernel.h> | |
4a249263 | 74 | #include <sys/malloc.h> |
1c79356b | 75 | #include <sys/mbuf.h> |
4a249263 | 76 | #include <sys/queue.h> |
1c79356b A |
77 | #include <sys/socket.h> |
78 | #include <sys/sockio.h> | |
79 | #include <sys/sysctl.h> | |
80 | #include <sys/systm.h> | |
4a249263 | 81 | #include <sys/kern_event.h> |
6d2010ae | 82 | #include <sys/mcache.h> |
1c79356b | 83 | |
1c79356b | 84 | #include <net/bpf.h> |
1c79356b A |
85 | #include <net/ethernet.h> |
86 | #include <net/if.h> | |
87 | #include <net/if_arp.h> | |
88 | #include <net/if_dl.h> | |
91447636 | 89 | #include <net/if_ether.h> |
1c79356b A |
90 | #include <net/if_types.h> |
91 | #include <net/if_vlan_var.h> | |
91447636 | 92 | #include <libkern/OSAtomic.h> |
1c79356b | 93 | |
4a249263 A |
94 | #include <net/dlil.h> |
95 | ||
2d21ac55 A |
96 | #include <net/kpi_interface.h> |
97 | #include <net/kpi_protocol.h> | |
98 | ||
91447636 | 99 | #include <kern/locks.h> |
d9a64523 | 100 | #include <kern/zalloc.h> |
91447636 | 101 | |
4a249263 | 102 | #ifdef INET |
1c79356b A |
103 | #include <netinet/in.h> |
104 | #include <netinet/if_ether.h> | |
105 | #endif | |
106 | ||
4a249263 | 107 | #include <net/if_media.h> |
91447636 | 108 | #include <net/multicast_list.h> |
2d21ac55 | 109 | #include <net/ether_if_module.h> |
4a249263 | 110 | |
0a7de745 | 111 | #define VLANNAME "vlan" |
4a249263 | 112 | |
91447636 | 113 | /** |
0a7de745 A |
114 | ** vlan locks |
115 | **/ | |
91447636 A |
116 | static __inline__ lck_grp_t * |
117 | my_lck_grp_alloc_init(const char * grp_name) | |
118 | { | |
0a7de745 A |
119 | lck_grp_t * grp; |
120 | lck_grp_attr_t * grp_attrs; | |
121 | ||
122 | grp_attrs = lck_grp_attr_alloc_init(); | |
123 | grp = lck_grp_alloc_init(grp_name, grp_attrs); | |
124 | lck_grp_attr_free(grp_attrs); | |
125 | return grp; | |
91447636 A |
126 | } |
127 | ||
128 | static __inline__ lck_mtx_t * | |
129 | my_lck_mtx_alloc_init(lck_grp_t * lck_grp) | |
130 | { | |
0a7de745 A |
131 | lck_attr_t * lck_attrs; |
132 | lck_mtx_t * lck_mtx; | |
91447636 | 133 | |
0a7de745 A |
134 | lck_attrs = lck_attr_alloc_init(); |
135 | lck_mtx = lck_mtx_alloc_init(lck_grp, lck_attrs); | |
136 | lck_attr_free(lck_attrs); | |
137 | return lck_mtx; | |
91447636 A |
138 | } |
139 | ||
0a7de745 | 140 | static lck_mtx_t * vlan_lck_mtx; |
91447636 A |
141 | |
142 | static __inline__ void | |
143 | vlan_lock_init(void) | |
144 | { | |
0a7de745 | 145 | lck_grp_t * vlan_lck_grp; |
91447636 | 146 | |
0a7de745 A |
147 | vlan_lck_grp = my_lck_grp_alloc_init("if_vlan"); |
148 | vlan_lck_mtx = my_lck_mtx_alloc_init(vlan_lck_grp); | |
91447636 A |
149 | } |
150 | ||
151 | static __inline__ void | |
152 | vlan_assert_lock_held(void) | |
153 | { | |
0a7de745 A |
154 | LCK_MTX_ASSERT(vlan_lck_mtx, LCK_MTX_ASSERT_OWNED); |
155 | return; | |
91447636 A |
156 | } |
157 | ||
158 | static __inline__ void | |
159 | vlan_assert_lock_not_held(void) | |
160 | { | |
0a7de745 A |
161 | LCK_MTX_ASSERT(vlan_lck_mtx, LCK_MTX_ASSERT_NOTOWNED); |
162 | return; | |
91447636 | 163 | } |
4a249263 | 164 | |
91447636 A |
165 | static __inline__ void |
166 | vlan_lock(void) | |
167 | { | |
0a7de745 A |
168 | lck_mtx_lock(vlan_lck_mtx); |
169 | return; | |
91447636 A |
170 | } |
171 | ||
172 | static __inline__ void | |
173 | vlan_unlock(void) | |
174 | { | |
0a7de745 A |
175 | lck_mtx_unlock(vlan_lck_mtx); |
176 | return; | |
91447636 A |
177 | } |
178 | ||
179 | /** | |
0a7de745 A |
180 | ** vlan structures, types |
181 | **/ | |
91447636 A |
182 | struct vlan_parent; |
183 | LIST_HEAD(vlan_parent_list, vlan_parent); | |
184 | struct ifvlan; | |
185 | LIST_HEAD(ifvlan_list, ifvlan); | |
186 | ||
0a7de745 | 187 | typedef LIST_ENTRY(vlan_parent) |
6d2010ae | 188 | vlan_parent_entry; |
0a7de745 | 189 | typedef LIST_ENTRY(ifvlan) |
6d2010ae A |
190 | ifvlan_entry; |
191 | ||
0a7de745 | 192 | #define VLP_SIGNATURE 0xfaceface |
91447636 | 193 | typedef struct vlan_parent { |
0a7de745 A |
194 | vlan_parent_entry vlp_parent_list;/* list of parents */ |
195 | struct ifnet * vlp_ifp; /* interface */ | |
196 | struct ifvlan_list vlp_vlan_list;/* list of VLAN's */ | |
197 | #define VLPF_SUPPORTS_VLAN_MTU 0x00000001 | |
198 | #define VLPF_CHANGE_IN_PROGRESS 0x00000002 | |
199 | #define VLPF_DETACHING 0x00000004 | |
200 | #define VLPF_LINK_EVENT_REQUIRED 0x00000008 | |
201 | u_int32_t vlp_flags; | |
202 | u_int32_t vlp_event_code; | |
203 | struct ifdevmtu vlp_devmtu; | |
204 | int32_t vlp_retain_count; | |
205 | u_int32_t vlp_signature;/* VLP_SIGNATURE */ | |
91447636 A |
206 | } vlan_parent, * vlan_parent_ref; |
207 | ||
0a7de745 | 208 | #define IFV_SIGNATURE 0xbeefbeef |
91447636 | 209 | struct ifvlan { |
0a7de745 A |
210 | ifvlan_entry ifv_vlan_list; |
211 | char ifv_name[IFNAMSIZ];/* our unique id */ | |
212 | struct ifnet * ifv_ifp; /* our interface */ | |
213 | vlan_parent_ref ifv_vlp; /* parent information */ | |
214 | struct ifv_linkmib { | |
215 | u_int16_t ifvm_encaplen;/* encapsulation length */ | |
216 | u_int16_t ifvm_mtufudge;/* MTU fudged by this much */ | |
217 | u_int16_t ifvm_proto; /* encapsulation ethertype */ | |
218 | u_int16_t ifvm_tag; /* tag to apply on packets leaving if */ | |
219 | } ifv_mib; | |
220 | struct multicast_list ifv_multicast; | |
221 | #define IFVF_PROMISC 0x1 /* promiscuous mode enabled */ | |
222 | #define IFVF_DETACHING 0x2 /* interface is detaching */ | |
223 | #define IFVF_READY 0x4 /* interface is ready */ | |
224 | u_int32_t ifv_flags; | |
225 | int32_t ifv_retain_count; | |
226 | u_int32_t ifv_signature;/* IFV_SIGNATURE */ | |
4a249263 A |
227 | }; |
228 | ||
91447636 A |
229 | typedef struct ifvlan * ifvlan_ref; |
230 | ||
231 | typedef struct vlan_globals_s { | |
0a7de745 A |
232 | struct vlan_parent_list parent_list; |
233 | int verbose; | |
91447636 | 234 | } * vlan_globals_ref; |
91447636 | 235 | |
0a7de745 A |
236 | static vlan_globals_ref g_vlan; |
237 | ||
238 | #define ifv_tag ifv_mib.ifvm_tag | |
239 | #define ifv_encaplen ifv_mib.ifvm_encaplen | |
240 | #define ifv_mtufudge ifv_mib.ifvm_mtufudge | |
4a249263 | 241 | |
6d2010ae A |
242 | static void |
243 | vlan_parent_retain(vlan_parent_ref vlp); | |
244 | ||
245 | static void | |
246 | vlan_parent_release(vlan_parent_ref vlp); | |
91447636 A |
247 | |
248 | /** | |
0a7de745 A |
249 | ** vlan_parent_ref vlp_flags in-lines |
250 | **/ | |
91447636 A |
251 | static __inline__ int |
252 | vlan_parent_flags_supports_vlan_mtu(vlan_parent_ref vlp) | |
253 | { | |
0a7de745 | 254 | return (vlp->vlp_flags & VLPF_SUPPORTS_VLAN_MTU) != 0; |
91447636 A |
255 | } |
256 | ||
257 | static __inline__ void | |
258 | vlan_parent_flags_set_supports_vlan_mtu(vlan_parent_ref vlp) | |
259 | { | |
0a7de745 A |
260 | vlp->vlp_flags |= VLPF_SUPPORTS_VLAN_MTU; |
261 | return; | |
91447636 A |
262 | } |
263 | ||
91447636 A |
264 | static __inline__ int |
265 | vlan_parent_flags_change_in_progress(vlan_parent_ref vlp) | |
266 | { | |
0a7de745 | 267 | return (vlp->vlp_flags & VLPF_CHANGE_IN_PROGRESS) != 0; |
91447636 A |
268 | } |
269 | ||
270 | static __inline__ void | |
271 | vlan_parent_flags_set_change_in_progress(vlan_parent_ref vlp) | |
272 | { | |
0a7de745 A |
273 | vlp->vlp_flags |= VLPF_CHANGE_IN_PROGRESS; |
274 | return; | |
91447636 A |
275 | } |
276 | ||
277 | static __inline__ void | |
278 | vlan_parent_flags_clear_change_in_progress(vlan_parent_ref vlp) | |
279 | { | |
0a7de745 A |
280 | vlp->vlp_flags &= ~VLPF_CHANGE_IN_PROGRESS; |
281 | return; | |
91447636 A |
282 | } |
283 | ||
284 | static __inline__ int | |
285 | vlan_parent_flags_detaching(struct vlan_parent * vlp) | |
286 | { | |
0a7de745 | 287 | return (vlp->vlp_flags & VLPF_DETACHING) != 0; |
91447636 A |
288 | } |
289 | ||
290 | static __inline__ void | |
291 | vlan_parent_flags_set_detaching(struct vlan_parent * vlp) | |
292 | { | |
0a7de745 A |
293 | vlp->vlp_flags |= VLPF_DETACHING; |
294 | return; | |
91447636 A |
295 | } |
296 | ||
fe8ab488 A |
297 | static __inline__ int |
298 | vlan_parent_flags_link_event_required(vlan_parent_ref vlp) | |
299 | { | |
0a7de745 | 300 | return (vlp->vlp_flags & VLPF_LINK_EVENT_REQUIRED) != 0; |
fe8ab488 A |
301 | } |
302 | ||
303 | static __inline__ void | |
304 | vlan_parent_flags_set_link_event_required(vlan_parent_ref vlp) | |
305 | { | |
0a7de745 A |
306 | vlp->vlp_flags |= VLPF_LINK_EVENT_REQUIRED; |
307 | return; | |
fe8ab488 A |
308 | } |
309 | ||
310 | static __inline__ void | |
311 | vlan_parent_flags_clear_link_event_required(vlan_parent_ref vlp) | |
312 | { | |
0a7de745 A |
313 | vlp->vlp_flags &= ~VLPF_LINK_EVENT_REQUIRED; |
314 | return; | |
fe8ab488 A |
315 | } |
316 | ||
91447636 A |
317 | |
318 | /** | |
0a7de745 A |
319 | ** ifvlan_flags in-lines routines |
320 | **/ | |
91447636 A |
321 | static __inline__ int |
322 | ifvlan_flags_promisc(ifvlan_ref ifv) | |
323 | { | |
0a7de745 | 324 | return (ifv->ifv_flags & IFVF_PROMISC) != 0; |
91447636 A |
325 | } |
326 | ||
327 | static __inline__ void | |
328 | ifvlan_flags_set_promisc(ifvlan_ref ifv) | |
329 | { | |
0a7de745 A |
330 | ifv->ifv_flags |= IFVF_PROMISC; |
331 | return; | |
91447636 A |
332 | } |
333 | ||
334 | static __inline__ void | |
335 | ifvlan_flags_clear_promisc(ifvlan_ref ifv) | |
336 | { | |
0a7de745 A |
337 | ifv->ifv_flags &= ~IFVF_PROMISC; |
338 | return; | |
91447636 A |
339 | } |
340 | ||
341 | static __inline__ int | |
342 | ifvlan_flags_ready(ifvlan_ref ifv) | |
343 | { | |
0a7de745 | 344 | return (ifv->ifv_flags & IFVF_READY) != 0; |
91447636 A |
345 | } |
346 | ||
347 | static __inline__ void | |
348 | ifvlan_flags_set_ready(ifvlan_ref ifv) | |
349 | { | |
0a7de745 A |
350 | ifv->ifv_flags |= IFVF_READY; |
351 | return; | |
91447636 A |
352 | } |
353 | ||
91447636 A |
354 | static __inline__ int |
355 | ifvlan_flags_detaching(ifvlan_ref ifv) | |
356 | { | |
0a7de745 | 357 | return (ifv->ifv_flags & IFVF_DETACHING) != 0; |
91447636 A |
358 | } |
359 | ||
360 | static __inline__ void | |
361 | ifvlan_flags_set_detaching(ifvlan_ref ifv) | |
362 | { | |
0a7de745 A |
363 | ifv->ifv_flags |= IFVF_DETACHING; |
364 | return; | |
91447636 | 365 | } |
4a249263 A |
366 | |
367 | #if 0 | |
1c79356b | 368 | SYSCTL_DECL(_net_link); |
0a7de745 A |
369 | SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "IEEE 802.1Q VLAN"); |
370 | SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "for consistency"); | |
b0d623f7 | 371 | #endif |
4a249263 | 372 | |
0a7de745 A |
373 | #define VLAN_UNITMAX IF_MAXUNIT |
374 | #define VLAN_ZONE_MAX_ELEM MIN(IFNETS_MAX, VLAN_UNITMAX) | |
375 | #define M_VLAN M_DEVBUF | |
376 | ||
377 | static int vlan_clone_create(struct if_clone *, u_int32_t, void *); | |
378 | static int vlan_clone_destroy(struct ifnet *); | |
379 | static int vlan_input(ifnet_t ifp, protocol_family_t protocol, | |
380 | mbuf_t m, char *frame_header); | |
381 | static int vlan_output(struct ifnet *ifp, struct mbuf *m); | |
382 | static int vlan_ioctl(ifnet_t ifp, u_long cmd, void * addr); | |
383 | static int vlan_attach_protocol(struct ifnet *ifp); | |
384 | static int vlan_detach_protocol(struct ifnet *ifp); | |
385 | static int vlan_setmulti(struct ifnet *ifp); | |
386 | static int vlan_unconfig(ifvlan_ref ifv, int need_to_wait); | |
387 | static int vlan_config(struct ifnet * ifp, struct ifnet * p, int tag); | |
388 | static void vlan_if_free(struct ifnet * ifp); | |
389 | static int vlan_remove(ifvlan_ref ifv, int need_to_wait); | |
4a249263 A |
390 | |
391 | static struct if_clone vlan_cloner = IF_CLONE_INITIALIZER(VLANNAME, | |
d9a64523 A |
392 | vlan_clone_create, |
393 | vlan_clone_destroy, | |
394 | 0, | |
395 | VLAN_UNITMAX, | |
396 | VLAN_ZONE_MAX_ELEM, | |
397 | sizeof(struct ifvlan)); | |
0a7de745 A |
398 | static void interface_link_event(struct ifnet * ifp, u_int32_t event_code); |
399 | static void vlan_parent_link_event(struct ifnet * p, | |
400 | u_int32_t event_code); | |
6d2010ae | 401 | |
0a7de745 | 402 | static int ifvlan_new_mtu(ifvlan_ref ifv, int mtu); |
6d2010ae A |
403 | |
404 | /** | |
0a7de745 A |
405 | ** ifvlan_ref routines |
406 | **/ | |
6d2010ae A |
407 | static void |
408 | ifvlan_retain(ifvlan_ref ifv) | |
409 | { | |
0a7de745 A |
410 | if (ifv->ifv_signature != IFV_SIGNATURE) { |
411 | panic("ifvlan_retain: bad signature\n"); | |
412 | } | |
413 | if (ifv->ifv_retain_count == 0) { | |
414 | panic("ifvlan_retain: retain count is 0\n"); | |
415 | } | |
416 | OSIncrementAtomic(&ifv->ifv_retain_count); | |
6d2010ae A |
417 | } |
418 | ||
419 | static void | |
420 | ifvlan_release(ifvlan_ref ifv) | |
421 | { | |
0a7de745 A |
422 | u_int32_t old_retain_count; |
423 | ||
424 | if (ifv->ifv_signature != IFV_SIGNATURE) { | |
425 | panic("ifvlan_release: bad signature\n"); | |
6d2010ae | 426 | } |
0a7de745 A |
427 | old_retain_count = OSDecrementAtomic(&ifv->ifv_retain_count); |
428 | switch (old_retain_count) { | |
429 | case 0: | |
430 | panic("ifvlan_release: retain count is 0\n"); | |
431 | break; | |
432 | case 1: | |
433 | if (g_vlan->verbose) { | |
434 | printf("ifvlan_release(%s)\n", ifv->ifv_name); | |
435 | } | |
436 | ifv->ifv_signature = 0; | |
437 | if_clone_softc_deallocate(&vlan_cloner, ifv); | |
438 | break; | |
439 | default: | |
440 | break; | |
441 | } | |
442 | return; | |
6d2010ae A |
443 | } |
444 | ||
445 | static vlan_parent_ref | |
446 | ifvlan_get_vlan_parent_retained(ifvlan_ref ifv) | |
447 | { | |
0a7de745 | 448 | vlan_parent_ref vlp = ifv->ifv_vlp; |
6d2010ae | 449 | |
0a7de745 A |
450 | if (vlp == NULL || vlan_parent_flags_detaching(vlp)) { |
451 | return NULL; | |
452 | } | |
453 | vlan_parent_retain(vlp); | |
454 | return vlp; | |
6d2010ae A |
455 | } |
456 | ||
457 | /** | |
0a7de745 A |
458 | ** ifnet_* routines |
459 | **/ | |
6d2010ae A |
460 | |
461 | static ifvlan_ref | |
462 | ifnet_get_ifvlan(struct ifnet * ifp) | |
463 | { | |
0a7de745 | 464 | ifvlan_ref ifv; |
6d2010ae | 465 | |
0a7de745 A |
466 | ifv = (ifvlan_ref)ifnet_softc(ifp); |
467 | return ifv; | |
6d2010ae A |
468 | } |
469 | ||
470 | static ifvlan_ref | |
471 | ifnet_get_ifvlan_retained(struct ifnet * ifp) | |
472 | { | |
0a7de745 | 473 | ifvlan_ref ifv; |
6d2010ae | 474 | |
0a7de745 A |
475 | ifv = ifnet_get_ifvlan(ifp); |
476 | if (ifv == NULL) { | |
477 | return NULL; | |
478 | } | |
479 | if (ifvlan_flags_detaching(ifv)) { | |
480 | return NULL; | |
481 | } | |
482 | ifvlan_retain(ifv); | |
483 | return ifv; | |
6d2010ae A |
484 | } |
485 | ||
486 | static int | |
487 | ifnet_ifvlan_vlan_parent_ok(struct ifnet * ifp, ifvlan_ref ifv, | |
0a7de745 A |
488 | vlan_parent_ref vlp) |
489 | { | |
490 | ifvlan_ref check_ifv; | |
491 | ||
492 | check_ifv = ifnet_get_ifvlan(ifp); | |
493 | if (check_ifv != ifv || ifvlan_flags_detaching(ifv)) { | |
494 | /* ifvlan_ref no longer valid */ | |
495 | return FALSE; | |
496 | } | |
497 | if (ifv->ifv_vlp != vlp) { | |
498 | /* vlan_parent no longer valid */ | |
499 | return FALSE; | |
500 | } | |
501 | if (vlan_parent_flags_detaching(vlp)) { | |
502 | /* parent is detaching */ | |
503 | return FALSE; | |
504 | } | |
505 | return TRUE; | |
6d2010ae A |
506 | } |
507 | ||
508 | /** | |
0a7de745 A |
509 | ** vlan, etc. routines |
510 | **/ | |
91447636 A |
511 | |
512 | static int | |
513 | vlan_globals_init(void) | |
514 | { | |
0a7de745 | 515 | vlan_globals_ref v; |
91447636 | 516 | |
0a7de745 | 517 | vlan_assert_lock_not_held(); |
91447636 | 518 | |
0a7de745 A |
519 | if (g_vlan != NULL) { |
520 | return 0; | |
521 | } | |
522 | v = _MALLOC(sizeof(*v), M_VLAN, M_WAITOK); | |
91447636 | 523 | if (v != NULL) { |
0a7de745 A |
524 | LIST_INIT(&v->parent_list); |
525 | v->verbose = 0; | |
526 | } | |
527 | vlan_lock(); | |
528 | if (g_vlan != NULL) { | |
529 | vlan_unlock(); | |
530 | if (v != NULL) { | |
531 | _FREE(v, M_VLAN); | |
532 | } | |
533 | return 0; | |
534 | } | |
535 | g_vlan = v; | |
536 | vlan_unlock(); | |
537 | if (v == NULL) { | |
538 | return ENOMEM; | |
91447636 | 539 | } |
0a7de745 | 540 | return 0; |
91447636 A |
541 | } |
542 | ||
543 | static int | |
544 | siocgifdevmtu(struct ifnet * ifp, struct ifdevmtu * ifdm_p) | |
545 | { | |
0a7de745 A |
546 | struct ifreq ifr; |
547 | int error; | |
91447636 | 548 | |
0a7de745 A |
549 | bzero(&ifr, sizeof(ifr)); |
550 | error = ifnet_ioctl(ifp, 0, SIOCGIFDEVMTU, &ifr); | |
551 | if (error == 0) { | |
552 | *ifdm_p = ifr.ifr_devmtu; | |
553 | } | |
554 | return error; | |
91447636 | 555 | } |
4a249263 | 556 | |
91447636 A |
557 | static int |
558 | siocsifaltmtu(struct ifnet * ifp, int mtu) | |
559 | { | |
0a7de745 | 560 | struct ifreq ifr; |
4a249263 | 561 | |
0a7de745 A |
562 | bzero(&ifr, sizeof(ifr)); |
563 | ifr.ifr_mtu = mtu; | |
564 | return ifnet_ioctl(ifp, 0, SIOCSIFALTMTU, &ifr); | |
91447636 | 565 | } |
4a249263 | 566 | |
91447636 | 567 | /** |
0a7de745 A |
568 | ** vlan_parent synchronization routines |
569 | **/ | |
6d2010ae | 570 | static void |
91447636 A |
571 | vlan_parent_retain(vlan_parent_ref vlp) |
572 | { | |
0a7de745 A |
573 | if (vlp->vlp_signature != VLP_SIGNATURE) { |
574 | panic("vlan_parent_retain: signature is bad\n"); | |
575 | } | |
576 | if (vlp->vlp_retain_count == 0) { | |
577 | panic("vlan_parent_retain: retain count is 0\n"); | |
578 | } | |
579 | OSIncrementAtomic(&vlp->vlp_retain_count); | |
91447636 A |
580 | } |
581 | ||
6d2010ae | 582 | static void |
91447636 A |
583 | vlan_parent_release(vlan_parent_ref vlp) |
584 | { | |
0a7de745 A |
585 | u_int32_t old_retain_count; |
586 | ||
587 | if (vlp->vlp_signature != VLP_SIGNATURE) { | |
588 | panic("vlan_parent_release: signature is bad\n"); | |
589 | } | |
590 | old_retain_count = OSDecrementAtomic(&vlp->vlp_retain_count); | |
591 | switch (old_retain_count) { | |
592 | case 0: | |
593 | panic("vlan_parent_release: retain count is 0\n"); | |
594 | break; | |
595 | case 1: | |
596 | if (g_vlan->verbose) { | |
597 | struct ifnet * ifp = vlp->vlp_ifp; | |
598 | printf("vlan_parent_release(%s%d)\n", ifnet_name(ifp), | |
599 | ifnet_unit(ifp)); | |
600 | } | |
601 | vlp->vlp_signature = 0; | |
602 | FREE(vlp, M_VLAN); | |
603 | break; | |
604 | default: | |
605 | break; | |
91447636 | 606 | } |
0a7de745 | 607 | return; |
91447636 A |
608 | } |
609 | ||
610 | /* | |
611 | * Function: vlan_parent_wait | |
612 | * Purpose: | |
613 | * Allows a single thread to gain exclusive access to the vlan_parent | |
0a7de745 | 614 | * data structure. Some operations take a long time to complete, |
91447636 A |
615 | * and some have side-effects that we can't predict. Holding the |
616 | * vlan_lock() across such operations is not possible. | |
617 | * | |
618 | * Notes: | |
619 | * Before calling, you must be holding the vlan_lock and have taken | |
620 | * a reference on the vlan_parent_ref. | |
621 | */ | |
622 | static void | |
623 | vlan_parent_wait(vlan_parent_ref vlp, const char * msg) | |
624 | { | |
0a7de745 | 625 | int waited = 0; |
91447636 | 626 | |
0a7de745 A |
627 | /* other add/remove/multicast-change in progress */ |
628 | while (vlan_parent_flags_change_in_progress(vlp)) { | |
629 | if (g_vlan->verbose) { | |
630 | struct ifnet * ifp = vlp->vlp_ifp; | |
91447636 | 631 | |
0a7de745 A |
632 | printf("%s%d: %s msleep\n", ifnet_name(ifp), ifnet_unit(ifp), msg); |
633 | } | |
634 | waited = 1; | |
635 | (void)msleep(vlp, vlan_lck_mtx, PZERO, msg, 0); | |
91447636 | 636 | } |
0a7de745 A |
637 | /* prevent other vlan parent remove/add from taking place */ |
638 | vlan_parent_flags_set_change_in_progress(vlp); | |
639 | if (g_vlan->verbose && waited) { | |
640 | struct ifnet * ifp = vlp->vlp_ifp; | |
91447636 | 641 | |
0a7de745 A |
642 | printf("%s%d: %s woke up\n", ifnet_name(ifp), ifnet_unit(ifp), msg); |
643 | } | |
644 | return; | |
91447636 A |
645 | } |
646 | ||
647 | /* | |
648 | * Function: vlan_parent_signal | |
649 | * Purpose: | |
0a7de745 | 650 | * Allows the thread that previously invoked vlan_parent_wait() to |
91447636 A |
651 | * give up exclusive access to the vlan_parent data structure, and wake up |
652 | * any other threads waiting to access | |
653 | * Notes: | |
654 | * Before calling, you must be holding the vlan_lock and have taken | |
655 | * a reference on the vlan_parent_ref. | |
656 | */ | |
657 | static void | |
658 | vlan_parent_signal(vlan_parent_ref vlp, const char * msg) | |
659 | { | |
0a7de745 A |
660 | struct ifnet * vlp_ifp = vlp->vlp_ifp; |
661 | ||
662 | if (vlan_parent_flags_link_event_required(vlp)) { | |
663 | vlan_parent_flags_clear_link_event_required(vlp); | |
664 | if (!vlan_parent_flags_detaching(vlp)) { | |
665 | u_int32_t event_code = vlp->vlp_event_code; | |
666 | ifvlan_ref ifv; | |
667 | ||
668 | vlan_unlock(); | |
669 | ||
670 | /* we can safely walk the list unlocked */ | |
671 | LIST_FOREACH(ifv, &vlp->vlp_vlan_list, ifv_vlan_list) { | |
672 | struct ifnet * ifp = ifv->ifv_ifp; | |
673 | ||
674 | interface_link_event(ifp, event_code); | |
675 | } | |
676 | if (g_vlan->verbose) { | |
677 | printf("%s%d: propagated link event to vlans\n", | |
678 | ifnet_name(vlp_ifp), ifnet_unit(vlp_ifp)); | |
679 | } | |
680 | vlan_lock(); | |
681 | } | |
682 | } | |
683 | vlan_parent_flags_clear_change_in_progress(vlp); | |
684 | wakeup((caddr_t)vlp); | |
685 | if (g_vlan->verbose) { | |
686 | printf("%s%d: %s wakeup\n", | |
687 | ifnet_name(vlp_ifp), ifnet_unit(vlp_ifp), msg); | |
688 | } | |
689 | return; | |
91447636 A |
690 | } |
691 | ||
1c79356b A |
692 | /* |
693 | * Program our multicast filter. What we're actually doing is | |
694 | * programming the multicast filter of the parent. This has the | |
695 | * side effect of causing the parent interface to receive multicast | |
696 | * traffic that it doesn't really want, which ends up being discarded | |
697 | * later by the upper protocol layers. Unfortunately, there's no way | |
698 | * to avoid this: there really is only one physical interface. | |
699 | */ | |
4a249263 | 700 | static int |
91447636 | 701 | vlan_setmulti(struct ifnet * ifp) |
1c79356b | 702 | { |
0a7de745 A |
703 | int error = 0; |
704 | ifvlan_ref ifv; | |
705 | struct ifnet * p; | |
706 | vlan_parent_ref vlp = NULL; | |
707 | ||
708 | vlan_lock(); | |
709 | ifv = ifnet_get_ifvlan_retained(ifp); | |
710 | if (ifv == NULL) { | |
711 | goto unlock_done; | |
712 | } | |
713 | vlp = ifvlan_get_vlan_parent_retained(ifv); | |
714 | if (vlp == NULL) { | |
715 | /* no parent, no need to program the multicast filter */ | |
716 | goto unlock_done; | |
717 | } | |
718 | vlan_parent_wait(vlp, "vlan_setmulti"); | |
719 | ||
720 | /* check again, things could have changed */ | |
721 | if (ifnet_ifvlan_vlan_parent_ok(ifp, ifv, vlp) == FALSE) { | |
722 | goto signal_done; | |
723 | } | |
724 | p = vlp->vlp_ifp; | |
725 | vlan_unlock(); | |
726 | ||
727 | /* update parent interface with our multicast addresses */ | |
728 | error = multicast_list_program(&ifv->ifv_multicast, ifp, p); | |
729 | ||
730 | vlan_lock(); | |
731 | ||
732 | signal_done: | |
733 | vlan_parent_signal(vlp, "vlan_setmulti"); | |
734 | ||
735 | unlock_done: | |
736 | vlan_unlock(); | |
737 | if (ifv != NULL) { | |
738 | ifvlan_release(ifv); | |
739 | } | |
740 | if (vlp != NULL) { | |
741 | vlan_parent_release(vlp); | |
742 | } | |
743 | return error; | |
91447636 | 744 | } |
4a249263 | 745 | |
91447636 | 746 | /** |
0a7de745 A |
747 | ** vlan_parent list manipulation/lookup routines |
748 | **/ | |
91447636 A |
749 | static vlan_parent_ref |
750 | parent_list_lookup(struct ifnet * p) | |
751 | { | |
0a7de745 | 752 | vlan_parent_ref vlp; |
4a249263 | 753 | |
0a7de745 A |
754 | LIST_FOREACH(vlp, &g_vlan->parent_list, vlp_parent_list) { |
755 | if (vlp->vlp_ifp == p) { | |
756 | return vlp; | |
757 | } | |
91447636 | 758 | } |
0a7de745 | 759 | return NULL; |
91447636 | 760 | } |
4a249263 | 761 | |
91447636 A |
762 | static ifvlan_ref |
763 | vlan_parent_lookup_tag(vlan_parent_ref vlp, int tag) | |
4a249263 | 764 | { |
0a7de745 | 765 | ifvlan_ref ifv; |
1c79356b | 766 | |
0a7de745 A |
767 | LIST_FOREACH(ifv, &vlp->vlp_vlan_list, ifv_vlan_list) { |
768 | if (tag == ifv->ifv_tag) { | |
769 | return ifv; | |
770 | } | |
1c79356b | 771 | } |
0a7de745 | 772 | return NULL; |
4a249263 | 773 | } |
1c79356b | 774 | |
0a7de745 | 775 | static ifvlan_ref |
91447636 | 776 | vlan_lookup_parent_and_tag(struct ifnet * p, int tag) |
4a249263 | 777 | { |
0a7de745 | 778 | vlan_parent_ref vlp; |
4a249263 | 779 | |
0a7de745 A |
780 | vlp = parent_list_lookup(p); |
781 | if (vlp != NULL) { | |
782 | return vlan_parent_lookup_tag(vlp, tag); | |
783 | } | |
784 | return NULL; | |
1c79356b A |
785 | } |
786 | ||
91447636 A |
787 | static int |
788 | vlan_parent_find_max_mtu(vlan_parent_ref vlp, ifvlan_ref exclude_ifv) | |
789 | { | |
0a7de745 A |
790 | int max_mtu = 0; |
791 | ifvlan_ref ifv; | |
91447636 | 792 | |
0a7de745 A |
793 | LIST_FOREACH(ifv, &vlp->vlp_vlan_list, ifv_vlan_list) { |
794 | int req_mtu; | |
91447636 | 795 | |
0a7de745 A |
796 | if (exclude_ifv == ifv) { |
797 | continue; | |
798 | } | |
799 | req_mtu = ifnet_mtu(ifv->ifv_ifp) + ifv->ifv_mtufudge; | |
800 | if (req_mtu > max_mtu) { | |
801 | max_mtu = req_mtu; | |
802 | } | |
91447636 | 803 | } |
0a7de745 | 804 | return max_mtu; |
91447636 A |
805 | } |
806 | ||
807 | /* | |
808 | * Function: vlan_parent_create | |
809 | * Purpose: | |
810 | * Create a vlan_parent structure to hold the VLAN's for the given | |
811 | * interface. Add it to the list of VLAN parents. | |
812 | */ | |
813 | static int | |
814 | vlan_parent_create(struct ifnet * p, vlan_parent_ref * ret_vlp) | |
815 | { | |
0a7de745 A |
816 | int error; |
817 | vlan_parent_ref vlp; | |
818 | ||
819 | *ret_vlp = NULL; | |
820 | vlp = _MALLOC(sizeof(*vlp), M_VLAN, M_WAITOK | M_ZERO); | |
821 | if (vlp == NULL) { | |
822 | return ENOMEM; | |
823 | } | |
824 | error = siocgifdevmtu(p, &vlp->vlp_devmtu); | |
825 | if (error != 0) { | |
826 | printf("vlan_parent_create (%s%d): siocgifdevmtu failed, %d\n", | |
827 | ifnet_name(p), ifnet_unit(p), error); | |
828 | FREE(vlp, M_VLAN); | |
829 | return error; | |
830 | } | |
831 | LIST_INIT(&vlp->vlp_vlan_list); | |
832 | vlp->vlp_ifp = p; | |
833 | vlp->vlp_retain_count = 1; | |
834 | vlp->vlp_signature = VLP_SIGNATURE; | |
835 | if (ifnet_offload(p) | |
836 | & (IF_HWASSIST_VLAN_MTU | IF_HWASSIST_VLAN_TAGGING)) { | |
837 | vlan_parent_flags_set_supports_vlan_mtu(vlp); | |
838 | } | |
839 | *ret_vlp = vlp; | |
840 | return 0; | |
91447636 A |
841 | } |
842 | ||
843 | static void | |
6d2010ae | 844 | vlan_parent_remove_all_vlans(struct ifnet * p) |
91447636 | 845 | { |
0a7de745 A |
846 | ifvlan_ref ifv; |
847 | int need_vlp_release = 0; | |
848 | ifvlan_ref next; | |
849 | vlan_parent_ref vlp; | |
91447636 | 850 | |
0a7de745 A |
851 | vlan_lock(); |
852 | vlp = parent_list_lookup(p); | |
853 | if (vlp == NULL || vlan_parent_flags_detaching(vlp)) { | |
854 | /* no VLAN's */ | |
855 | vlan_unlock(); | |
856 | return; | |
857 | } | |
858 | vlan_parent_flags_set_detaching(vlp); | |
859 | vlan_parent_retain(vlp); | |
860 | vlan_parent_wait(vlp, "vlan_parent_remove_all_vlans"); | |
861 | need_vlp_release++; | |
862 | ||
863 | /* check again */ | |
864 | if (parent_list_lookup(p) != vlp) { | |
865 | goto signal_done; | |
866 | } | |
867 | ||
868 | for (ifv = LIST_FIRST(&vlp->vlp_vlan_list); ifv != NULL; ifv = next) { | |
869 | struct ifnet * ifp = ifv->ifv_ifp; | |
870 | int removed; | |
871 | ||
872 | next = LIST_NEXT(ifv, ifv_vlan_list); | |
873 | removed = vlan_remove(ifv, FALSE); | |
874 | if (removed) { | |
875 | vlan_unlock(); | |
876 | ifnet_detach(ifp); | |
877 | vlan_lock(); | |
878 | } | |
879 | } | |
880 | ||
881 | /* the vlan parent has no more VLAN's */ | |
f427ee49 | 882 | if_clear_eflags(p, IFEF_VLAN); /* clear IFEF_VLAN */ |
0a7de745 A |
883 | |
884 | LIST_REMOVE(vlp, vlp_parent_list); | |
885 | need_vlp_release++; /* one for being in the list */ | |
886 | need_vlp_release++; /* final reference */ | |
887 | ||
888 | signal_done: | |
889 | vlan_parent_signal(vlp, "vlan_parent_remove_all_vlans"); | |
91447636 | 890 | vlan_unlock(); |
0a7de745 A |
891 | |
892 | while (need_vlp_release--) { | |
893 | vlan_parent_release(vlp); | |
894 | } | |
6d2010ae | 895 | return; |
91447636 A |
896 | } |
897 | ||
898 | static __inline__ int | |
899 | vlan_parent_no_vlans(vlan_parent_ref vlp) | |
900 | { | |
0a7de745 | 901 | return LIST_EMPTY(&vlp->vlp_vlan_list); |
91447636 A |
902 | } |
903 | ||
904 | static void | |
905 | vlan_parent_add_vlan(vlan_parent_ref vlp, ifvlan_ref ifv, int tag) | |
906 | { | |
0a7de745 A |
907 | LIST_INSERT_HEAD(&vlp->vlp_vlan_list, ifv, ifv_vlan_list); |
908 | ifv->ifv_vlp = vlp; | |
909 | ifv->ifv_tag = tag; | |
910 | return; | |
91447636 A |
911 | } |
912 | ||
913 | static void | |
914 | vlan_parent_remove_vlan(__unused vlan_parent_ref vlp, ifvlan_ref ifv) | |
915 | { | |
0a7de745 A |
916 | ifv->ifv_vlp = NULL; |
917 | LIST_REMOVE(ifv, ifv_vlan_list); | |
918 | return; | |
91447636 A |
919 | } |
920 | ||
b0d623f7 | 921 | static int |
4a249263 A |
922 | vlan_clone_attach(void) |
923 | { | |
0a7de745 | 924 | int error; |
b0d623f7 | 925 | |
0a7de745 A |
926 | error = if_clone_attach(&vlan_cloner); |
927 | if (error != 0) { | |
928 | return error; | |
929 | } | |
930 | vlan_lock_init(); | |
931 | return 0; | |
4a249263 A |
932 | } |
933 | ||
934 | static int | |
d1ecb069 | 935 | vlan_clone_create(struct if_clone *ifc, u_int32_t unit, __unused void *params) |
4a249263 | 936 | { |
0a7de745 A |
937 | int error; |
938 | ifvlan_ref ifv; | |
939 | ifnet_t ifp; | |
940 | struct ifnet_init_eparams vlan_init; | |
941 | ||
2d21ac55 A |
942 | error = vlan_globals_init(); |
943 | if (error != 0) { | |
0a7de745 | 944 | return error; |
2d21ac55 | 945 | } |
d9a64523 | 946 | ifv = if_clone_softc_allocate(&vlan_cloner); |
0a7de745 | 947 | if (ifv == NULL) { |
b0d623f7 | 948 | return ENOBUFS; |
0a7de745 | 949 | } |
6d2010ae A |
950 | ifv->ifv_retain_count = 1; |
951 | ifv->ifv_signature = IFV_SIGNATURE; | |
2d21ac55 | 952 | multicast_list_init(&ifv->ifv_multicast); |
0a7de745 | 953 | |
2d21ac55 | 954 | /* use the interface name as the unique id for ifp recycle */ |
6d2010ae A |
955 | if ((unsigned int) |
956 | snprintf(ifv->ifv_name, sizeof(ifv->ifv_name), "%s%d", | |
0a7de745 A |
957 | ifc->ifc_name, unit) >= sizeof(ifv->ifv_name)) { |
958 | ifvlan_release(ifv); | |
959 | return EINVAL; | |
2d21ac55 | 960 | } |
0a7de745 | 961 | |
2d21ac55 | 962 | bzero(&vlan_init, sizeof(vlan_init)); |
39236c6e | 963 | vlan_init.ver = IFNET_INIT_CURRENT_VERSION; |
0a7de745 | 964 | vlan_init.len = sizeof(vlan_init); |
39236c6e | 965 | vlan_init.flags = IFNET_INIT_LEGACY; |
2d21ac55 A |
966 | vlan_init.uniqueid = ifv->ifv_name; |
967 | vlan_init.uniqueid_len = strlen(ifv->ifv_name); | |
968 | vlan_init.name = ifc->ifc_name; | |
969 | vlan_init.unit = unit; | |
970 | vlan_init.family = IFNET_FAMILY_VLAN; | |
971 | vlan_init.type = IFT_L2VLAN; | |
972 | vlan_init.output = vlan_output; | |
973 | vlan_init.demux = ether_demux; | |
974 | vlan_init.add_proto = ether_add_proto; | |
975 | vlan_init.del_proto = ether_del_proto; | |
976 | vlan_init.check_multi = ether_check_multi; | |
39236c6e | 977 | vlan_init.framer_extended = ether_frameout_extended; |
2d21ac55 A |
978 | vlan_init.softc = ifv; |
979 | vlan_init.ioctl = vlan_ioctl; | |
5ba3f43e | 980 | vlan_init.set_bpf_tap = NULL; |
2d21ac55 A |
981 | vlan_init.detach = vlan_if_free; |
982 | vlan_init.broadcast_addr = etherbroadcastaddr; | |
983 | vlan_init.broadcast_len = ETHER_ADDR_LEN; | |
39236c6e | 984 | error = ifnet_allocate_extended(&vlan_init, &ifp); |
0a7de745 | 985 | |
2d21ac55 | 986 | if (error) { |
0a7de745 A |
987 | ifvlan_release(ifv); |
988 | return error; | |
2d21ac55 | 989 | } |
0a7de745 | 990 | |
2d21ac55 A |
991 | ifnet_set_offload(ifp, 0); |
992 | ifnet_set_addrlen(ifp, ETHER_ADDR_LEN); /* XXX ethernet specific */ | |
993 | ifnet_set_baudrate(ifp, 0); | |
994 | ifnet_set_hdrlen(ifp, ETHER_VLAN_ENCAP_LEN); | |
f427ee49 | 995 | ifnet_set_mtu(ifp, ETHERMTU); |
0a7de745 | 996 | |
2d21ac55 A |
997 | error = ifnet_attach(ifp, NULL); |
998 | if (error) { | |
0a7de745 A |
999 | ifnet_release(ifp); |
1000 | ifvlan_release(ifv); | |
1001 | return error; | |
2d21ac55 A |
1002 | } |
1003 | ifv->ifv_ifp = ifp; | |
0a7de745 | 1004 | |
2d21ac55 A |
1005 | /* attach as ethernet */ |
1006 | bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header)); | |
0a7de745 | 1007 | return 0; |
1c79356b | 1008 | } |
1c79356b | 1009 | |
6d2010ae A |
1010 | static int |
1011 | vlan_remove(ifvlan_ref ifv, int need_to_wait) | |
1c79356b | 1012 | { |
0a7de745 A |
1013 | vlan_assert_lock_held(); |
1014 | if (ifvlan_flags_detaching(ifv)) { | |
1015 | return 0; | |
1016 | } | |
1017 | ifvlan_flags_set_detaching(ifv); | |
1018 | vlan_unconfig(ifv, need_to_wait); | |
1019 | return 1; | |
1c79356b A |
1020 | } |
1021 | ||
1c79356b | 1022 | |
d1ecb069 | 1023 | static int |
4a249263 A |
1024 | vlan_clone_destroy(struct ifnet *ifp) |
1025 | { | |
0a7de745 | 1026 | ifvlan_ref ifv; |
1c79356b | 1027 | |
0a7de745 A |
1028 | vlan_lock(); |
1029 | ifv = ifnet_get_ifvlan_retained(ifp); | |
1030 | if (ifv == NULL) { | |
1031 | vlan_unlock(); | |
1032 | return 0; | |
1033 | } | |
1034 | if (vlan_remove(ifv, TRUE) == 0) { | |
1035 | vlan_unlock(); | |
1036 | ifvlan_release(ifv); | |
1037 | return 0; | |
1038 | } | |
91447636 | 1039 | vlan_unlock(); |
6d2010ae | 1040 | ifvlan_release(ifv); |
0a7de745 | 1041 | ifnet_detach(ifp); |
d1ecb069 | 1042 | |
0a7de745 | 1043 | return 0; |
1c79356b A |
1044 | } |
1045 | ||
4a249263 | 1046 | static int |
91447636 | 1047 | vlan_output(struct ifnet * ifp, struct mbuf * m) |
4a249263 | 1048 | { |
0a7de745 A |
1049 | struct ether_vlan_header * evl; |
1050 | int encaplen; | |
1051 | ifvlan_ref ifv; | |
1052 | struct ifnet * p; | |
1053 | int soft_vlan; | |
1054 | u_short tag; | |
1055 | vlan_parent_ref vlp = NULL; | |
1056 | int err; | |
cb323159 | 1057 | struct flowadv adv = { .code = FADV_SUCCESS }; |
0a7de745 A |
1058 | |
1059 | if (m == 0) { | |
1060 | return 0; | |
1061 | } | |
1062 | if ((m->m_flags & M_PKTHDR) == 0) { | |
1063 | m_freem_list(m); | |
1064 | return 0; | |
1065 | } | |
1066 | vlan_lock(); | |
1067 | ifv = ifnet_get_ifvlan_retained(ifp); | |
1068 | if (ifv == NULL || ifvlan_flags_ready(ifv) == 0) { | |
1069 | goto unlock_done; | |
1070 | } | |
1071 | vlp = ifvlan_get_vlan_parent_retained(ifv); | |
1072 | if (vlp == NULL) { | |
1073 | goto unlock_done; | |
1074 | } | |
1075 | p = vlp->vlp_ifp; | |
1076 | (void)ifnet_stat_increment_out(ifp, 1, m->m_pkthdr.len, 0); | |
1077 | soft_vlan = (ifnet_offload(p) & IF_HWASSIST_VLAN_TAGGING) == 0; | |
1078 | tag = ifv->ifv_tag; | |
1079 | encaplen = ifv->ifv_encaplen; | |
1080 | vlan_unlock(); | |
1081 | ||
1082 | ifvlan_release(ifv); | |
1083 | vlan_parent_release(vlp); | |
1084 | ||
1085 | bpf_tap_out(ifp, DLT_EN10MB, m, NULL, 0); | |
1086 | ||
1087 | /* do not run parent's if_output() if the parent is not up */ | |
1088 | if ((ifnet_flags(p) & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING)) { | |
1089 | m_freem(m); | |
1090 | atomic_add_64(&ifp->if_collisions, 1); | |
1091 | return 0; | |
1092 | } | |
1c79356b | 1093 | /* |
0a7de745 A |
1094 | * If underlying interface can do VLAN tag insertion itself, |
1095 | * just pass the packet along. However, we need some way to | |
1096 | * tell the interface where the packet came from so that it | |
1097 | * knows how to find the VLAN tag to use. We use a field in | |
1098 | * the mbuf header to store the VLAN tag, and a bit in the | |
1099 | * csum_flags field to mark the field as valid. | |
1c79356b | 1100 | */ |
0a7de745 A |
1101 | if (soft_vlan == 0) { |
1102 | m->m_pkthdr.csum_flags |= CSUM_VLAN_TAG_VALID; | |
1103 | m->m_pkthdr.vlan_tag = tag; | |
1104 | } else { | |
1105 | M_PREPEND(m, encaplen, M_DONTWAIT, 1); | |
1106 | if (m == NULL) { | |
1107 | printf("%s%d: unable to prepend VLAN header\n", ifnet_name(ifp), | |
1108 | ifnet_unit(ifp)); | |
1109 | atomic_add_64(&ifp->if_oerrors, 1); | |
1110 | return 0; | |
1111 | } | |
1112 | /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ | |
1113 | if (m->m_len < (int)sizeof(*evl)) { | |
1114 | m = m_pullup(m, sizeof(*evl)); | |
1115 | if (m == NULL) { | |
1116 | printf("%s%d: unable to pullup VLAN header\n", ifnet_name(ifp), | |
1117 | ifnet_unit(ifp)); | |
1118 | atomic_add_64(&ifp->if_oerrors, 1); | |
1119 | return 0; | |
1120 | } | |
1121 | } | |
316670eb | 1122 | |
0a7de745 A |
1123 | /* |
1124 | * Transform the Ethernet header into an Ethernet header | |
1125 | * with 802.1Q encapsulation. | |
1126 | */ | |
1127 | bcopy(mtod(m, char *) + encaplen, | |
1128 | mtod(m, char *), ETHER_HDR_LEN); | |
1129 | evl = mtod(m, struct ether_vlan_header *); | |
1130 | evl->evl_proto = evl->evl_encap_proto; | |
1131 | evl->evl_encap_proto = htons(ETHERTYPE_VLAN); | |
1132 | evl->evl_tag = htons(tag); | |
cb323159 A |
1133 | |
1134 | /* adjust partial checksum offload offsets */ | |
1135 | if ((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | | |
1136 | CSUM_PARTIAL)) == (CSUM_DATA_VALID | CSUM_PARTIAL)) { | |
1137 | m->m_pkthdr.csum_tx_start += ETHER_VLAN_ENCAP_LEN; | |
1138 | m->m_pkthdr.csum_tx_stuff += ETHER_VLAN_ENCAP_LEN; | |
1139 | } | |
316670eb | 1140 | } |
316670eb | 1141 | |
0a7de745 | 1142 | err = dlil_output(p, PF_VLAN, m, NULL, NULL, 1, &adv); |
6d2010ae | 1143 | |
0a7de745 A |
1144 | if (err == 0) { |
1145 | if (adv.code == FADV_FLOW_CONTROLLED) { | |
1146 | err = EQFULL; | |
1147 | } else if (adv.code == FADV_SUSPENDED) { | |
1148 | err = EQSUSPENDED; | |
1149 | } | |
1150 | } | |
6d2010ae | 1151 | |
0a7de745 A |
1152 | return err; |
1153 | ||
1154 | unlock_done: | |
1155 | vlan_unlock(); | |
1156 | if (ifv != NULL) { | |
1157 | ifvlan_release(ifv); | |
1158 | } | |
1159 | if (vlp != NULL) { | |
1160 | vlan_parent_release(vlp); | |
1161 | } | |
1162 | m_freem_list(m); | |
1163 | return 0; | |
1c79356b A |
1164 | } |
1165 | ||
91447636 | 1166 | static int |
2d21ac55 | 1167 | vlan_input(ifnet_t p, __unused protocol_family_t protocol, |
0a7de745 | 1168 | mbuf_t m, char *frame_header) |
1c79356b | 1169 | { |
0a7de745 A |
1170 | struct ether_vlan_header * evl; |
1171 | struct ifnet * ifp = NULL; | |
1172 | int soft_vlan = 0; | |
1173 | u_int tag = 0; | |
1c79356b | 1174 | |
0a7de745 A |
1175 | if (m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) { |
1176 | /* | |
1177 | * Packet is tagged, m contains a normal | |
1178 | * Ethernet frame; the tag is stored out-of-band. | |
1179 | */ | |
1180 | m->m_pkthdr.csum_flags &= ~CSUM_VLAN_TAG_VALID; | |
1181 | tag = EVL_VLANOFTAG(m->m_pkthdr.vlan_tag); | |
1182 | m->m_pkthdr.vlan_tag = 0; | |
1183 | } else { | |
1184 | soft_vlan = 1; | |
1185 | switch (ifnet_type(p)) { | |
1186 | case IFT_ETHER: | |
cb323159 | 1187 | case IFT_IEEE8023ADLAG: |
0a7de745 A |
1188 | if (m->m_len < ETHER_VLAN_ENCAP_LEN) { |
1189 | m_freem(m); | |
1190 | return 0; | |
1191 | } | |
1192 | evl = (struct ether_vlan_header *)(void *)frame_header; | |
1193 | if (ntohs(evl->evl_proto) == ETHERTYPE_VLAN) { | |
1194 | /* don't allow VLAN within VLAN */ | |
1195 | m_freem(m); | |
1196 | return 0; | |
1197 | } | |
1198 | tag = EVL_VLANOFTAG(ntohs(evl->evl_tag)); | |
1199 | ||
1200 | /* | |
1201 | * Restore the original ethertype. We'll remove | |
1202 | * the encapsulation after we've found the vlan | |
1203 | * interface corresponding to the tag. | |
1204 | */ | |
1205 | evl->evl_encap_proto = evl->evl_proto; | |
1206 | break; | |
1207 | default: | |
1208 | printf("vlan_demux: unsupported if type %u", | |
1209 | ifnet_type(p)); | |
1210 | m_freem(m); | |
1211 | return 0; | |
1212 | } | |
4a249263 | 1213 | } |
0a7de745 A |
1214 | if (tag != 0) { |
1215 | ifvlan_ref ifv; | |
91447636 | 1216 | |
0a7de745 A |
1217 | if ((ifnet_eflags(p) & IFEF_VLAN) == 0) { |
1218 | /* don't bother looking through the VLAN list */ | |
1219 | m_freem(m); | |
1220 | return 0; | |
1221 | } | |
1222 | vlan_lock(); | |
1223 | ifv = vlan_lookup_parent_and_tag(p, tag); | |
1224 | if (ifv != NULL) { | |
1225 | ifp = ifv->ifv_ifp; | |
1226 | } | |
1227 | if (ifv == NULL | |
1228 | || ifvlan_flags_ready(ifv) == 0 | |
1229 | || (ifnet_flags(ifp) & IFF_UP) == 0) { | |
1230 | vlan_unlock(); | |
1231 | m_freem(m); | |
1232 | return 0; | |
1233 | } | |
1234 | vlan_unlock(); | |
91447636 | 1235 | } |
0a7de745 A |
1236 | if (soft_vlan) { |
1237 | /* | |
1238 | * Packet had an in-line encapsulation header; | |
1239 | * remove it. The original header has already | |
1240 | * been fixed up above. | |
1241 | */ | |
1242 | m->m_len -= ETHER_VLAN_ENCAP_LEN; | |
1243 | m->m_data += ETHER_VLAN_ENCAP_LEN; | |
1244 | m->m_pkthdr.len -= ETHER_VLAN_ENCAP_LEN; | |
1245 | m->m_pkthdr.csum_flags = 0; /* can't trust hardware checksum */ | |
4a249263 | 1246 | } |
0a7de745 A |
1247 | if (tag != 0) { |
1248 | m->m_pkthdr.rcvif = ifp; | |
1249 | m->m_pkthdr.pkt_hdr = frame_header; | |
1250 | (void)ifnet_stat_increment_in(ifp, 1, | |
1251 | m->m_pkthdr.len + ETHER_HDR_LEN, 0); | |
1252 | bpf_tap_in(ifp, DLT_EN10MB, m, frame_header, ETHER_HDR_LEN); | |
1253 | /* We found a vlan interface, inject on that interface. */ | |
1254 | dlil_input_packet_list(ifp, m); | |
1255 | } else { | |
1256 | m->m_pkthdr.pkt_hdr = frame_header; | |
1257 | /* Send priority-tagged packet up through the parent */ | |
1258 | dlil_input_packet_list(p, m); | |
4a249263 | 1259 | } |
0a7de745 | 1260 | return 0; |
1c79356b A |
1261 | } |
1262 | ||
1263 | static int | |
91447636 | 1264 | vlan_config(struct ifnet * ifp, struct ifnet * p, int tag) |
1c79356b | 1265 | { |
f427ee49 | 1266 | u_int32_t eflags; |
0a7de745 A |
1267 | int error; |
1268 | int first_vlan = FALSE; | |
1269 | ifvlan_ref ifv = NULL; | |
1270 | int ifv_added = FALSE; | |
1271 | int need_vlp_release = 0; | |
1272 | vlan_parent_ref new_vlp = NULL; | |
1273 | ifnet_offload_t offload; | |
1274 | u_int16_t parent_flags; | |
1275 | vlan_parent_ref vlp = NULL; | |
1276 | ||
1277 | /* pre-allocate space for vlan_parent, in case we're first */ | |
1278 | error = vlan_parent_create(p, &new_vlp); | |
1279 | if (error != 0) { | |
1280 | return error; | |
6d2010ae | 1281 | } |
0a7de745 A |
1282 | |
1283 | vlan_lock(); | |
1284 | ifv = ifnet_get_ifvlan_retained(ifp); | |
1285 | if (ifv == NULL || ifv->ifv_vlp != NULL) { | |
1286 | vlan_unlock(); | |
1287 | if (ifv != NULL) { | |
1288 | ifvlan_release(ifv); | |
1289 | } | |
1290 | vlan_parent_release(new_vlp); | |
1291 | return EBUSY; | |
1292 | } | |
1293 | vlp = parent_list_lookup(p); | |
1294 | if (vlp != NULL) { | |
1295 | vlan_parent_retain(vlp); | |
1296 | need_vlp_release++; | |
1297 | if (vlan_parent_lookup_tag(vlp, tag) != NULL) { | |
1298 | /* already a VLAN with that tag on this interface */ | |
1299 | error = EADDRINUSE; | |
1300 | goto unlock_done; | |
1301 | } | |
1302 | } else { | |
1303 | /* one for being in the list */ | |
1304 | vlan_parent_retain(new_vlp); | |
1305 | ||
1306 | /* we're the first VLAN on this interface */ | |
1307 | LIST_INSERT_HEAD(&g_vlan->parent_list, new_vlp, vlp_parent_list); | |
1308 | vlp = new_vlp; | |
1309 | ||
1310 | vlan_parent_retain(vlp); | |
1311 | need_vlp_release++; | |
91447636 | 1312 | } |
6d2010ae | 1313 | |
0a7de745 A |
1314 | /* need to wait to ensure no one else is trying to add/remove */ |
1315 | vlan_parent_wait(vlp, "vlan_config"); | |
6d2010ae | 1316 | |
0a7de745 A |
1317 | if (ifnet_get_ifvlan(ifp) != ifv) { |
1318 | error = EINVAL; | |
1319 | goto signal_done; | |
1320 | } | |
1321 | ||
1322 | /* check again because someone might have gotten in */ | |
1323 | if (parent_list_lookup(p) != vlp) { | |
1324 | error = EBUSY; | |
1325 | goto signal_done; | |
1326 | } | |
1327 | ||
1328 | if (vlan_parent_flags_detaching(vlp) | |
1329 | || ifvlan_flags_detaching(ifv) || ifv->ifv_vlp != NULL) { | |
1330 | error = EBUSY; | |
1331 | goto signal_done; | |
1332 | } | |
1333 | ||
1334 | /* check again because someone might have gotten the tag */ | |
1335 | if (vlan_parent_lookup_tag(vlp, tag) != NULL) { | |
1336 | /* already a VLAN with that tag on this interface */ | |
1337 | error = EADDRINUSE; | |
1338 | goto signal_done; | |
4a249263 | 1339 | } |
1c79356b | 1340 | |
0a7de745 A |
1341 | if (vlan_parent_no_vlans(vlp)) { |
1342 | first_vlan = TRUE; | |
1343 | } | |
1344 | vlan_parent_add_vlan(vlp, ifv, tag); | |
1345 | ifvlan_retain(ifv); /* parent references ifv */ | |
1346 | ifv_added = TRUE; | |
1347 | ||
f427ee49 | 1348 | /* don't allow VLAN on interface that's part of a bond */ |
0a7de745 | 1349 | if ((ifnet_eflags(p) & IFEF_BOND) != 0) { |
0a7de745 A |
1350 | error = EBUSY; |
1351 | goto signal_done; | |
1352 | } | |
f427ee49 A |
1353 | /* mark it as in use by VLAN */ |
1354 | eflags = if_set_eflags(p, IFEF_VLAN); | |
1355 | if ((eflags & IFEF_BOND) != 0) { | |
1356 | /* bond got in ahead of us */ | |
1357 | if_clear_eflags(p, IFEF_VLAN); | |
1358 | error = EBUSY; | |
1359 | goto signal_done; | |
1360 | } | |
0a7de745 A |
1361 | vlan_unlock(); |
1362 | ||
91447636 | 1363 | if (first_vlan) { |
0a7de745 A |
1364 | /* attach our VLAN "protocol" to the interface */ |
1365 | error = vlan_attach_protocol(p); | |
1366 | if (error) { | |
1367 | vlan_lock(); | |
1368 | goto signal_done; | |
1369 | } | |
4a249263 | 1370 | } |
91447636 | 1371 | |
0a7de745 A |
1372 | /* configure parent to receive our multicast addresses */ |
1373 | error = multicast_list_program(&ifv->ifv_multicast, ifp, p); | |
1374 | if (error != 0) { | |
1375 | if (first_vlan) { | |
1376 | (void)vlan_detach_protocol(p); | |
1377 | } | |
1378 | vlan_lock(); | |
1379 | goto signal_done; | |
1380 | } | |
2d21ac55 | 1381 | |
0a7de745 A |
1382 | /* set our ethernet address to that of the parent */ |
1383 | ifnet_set_lladdr_and_type(ifp, IF_LLADDR(p), ETHER_ADDR_LEN, IFT_ETHER); | |
1384 | ||
1385 | /* no failures past this point */ | |
1386 | vlan_lock(); | |
1387 | ||
1388 | ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; | |
1389 | ifv->ifv_flags = 0; | |
1390 | if (vlan_parent_flags_supports_vlan_mtu(vlp)) { | |
1391 | ifv->ifv_mtufudge = 0; | |
1392 | } else { | |
1393 | /* | |
1394 | * Fudge the MTU by the encapsulation size. This | |
1395 | * makes us incompatible with strictly compliant | |
1396 | * 802.1Q implementations, but allows us to use | |
1397 | * the feature with other NetBSD implementations, | |
1398 | * which might still be useful. | |
1399 | */ | |
1400 | ifv->ifv_mtufudge = ifv->ifv_encaplen; | |
1401 | } | |
1402 | ifnet_set_mtu(ifp, ETHERMTU - ifv->ifv_mtufudge); | |
91447636 | 1403 | |
1c79356b | 1404 | /* |
0a7de745 A |
1405 | * Copy only a selected subset of flags from the parent. |
1406 | * Other flags are none of our business. | |
1c79356b | 1407 | */ |
0a7de745 A |
1408 | parent_flags = ifnet_flags(p) |
1409 | & (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX); | |
1410 | ifnet_set_flags(ifp, parent_flags, | |
1411 | IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX); | |
1412 | ||
1413 | /* use hwassist bits from parent interface, but exclude VLAN bits */ | |
1414 | offload = ifnet_offload(p) & ~(IFNET_VLAN_TAGGING | IFNET_VLAN_MTU); | |
1415 | ifnet_set_offload(ifp, offload); | |
1416 | ||
1417 | ifnet_set_flags(ifp, IFF_RUNNING, IFF_RUNNING); | |
1418 | ifvlan_flags_set_ready(ifv); | |
1419 | vlan_parent_signal(vlp, "vlan_config"); | |
1420 | vlan_unlock(); | |
1421 | if (new_vlp != vlp) { | |
1422 | /* throw it away, it wasn't needed */ | |
1423 | vlan_parent_release(new_vlp); | |
1424 | } | |
1425 | if (ifv != NULL) { | |
1426 | ifvlan_release(ifv); | |
6d2010ae | 1427 | } |
0a7de745 A |
1428 | if (first_vlan) { |
1429 | /* mark the parent interface up */ | |
1430 | ifnet_set_flags(p, IFF_UP, IFF_UP); | |
1431 | (void)ifnet_ioctl(p, 0, SIOCSIFFLAGS, (caddr_t)NULL); | |
1432 | } | |
1433 | return 0; | |
6d2010ae | 1434 | |
0a7de745 A |
1435 | signal_done: |
1436 | vlan_assert_lock_held(); | |
91447636 | 1437 | |
6d2010ae | 1438 | if (ifv_added) { |
0a7de745 A |
1439 | vlan_parent_remove_vlan(vlp, ifv); |
1440 | if (!vlan_parent_flags_detaching(vlp) && vlan_parent_no_vlans(vlp)) { | |
1441 | /* the vlan parent has no more VLAN's */ | |
f427ee49 | 1442 | if_clear_eflags(p, IFEF_VLAN); |
0a7de745 A |
1443 | LIST_REMOVE(vlp, vlp_parent_list); |
1444 | /* release outside of the lock below */ | |
1445 | need_vlp_release++; | |
1446 | ||
1447 | /* one for being in the list */ | |
1448 | need_vlp_release++; | |
1449 | } | |
6d2010ae | 1450 | } |
0a7de745 A |
1451 | vlan_parent_signal(vlp, "vlan_config"); |
1452 | ||
1453 | unlock_done: | |
1454 | vlan_unlock(); | |
1455 | ||
1456 | while (need_vlp_release--) { | |
1457 | vlan_parent_release(vlp); | |
1458 | } | |
1459 | if (new_vlp != vlp) { | |
1460 | vlan_parent_release(new_vlp); | |
1461 | } | |
1462 | if (ifv != NULL) { | |
1463 | if (ifv_added) { | |
1464 | ifvlan_release(ifv); | |
1465 | } | |
1466 | ifvlan_release(ifv); | |
1467 | } | |
1468 | return error; | |
4a249263 A |
1469 | } |
1470 | ||
1471 | static void | |
1472 | vlan_link_event(struct ifnet * ifp, struct ifnet * p) | |
1473 | { | |
0a7de745 | 1474 | struct ifmediareq ifmr; |
4a249263 | 1475 | |
0a7de745 A |
1476 | /* generate a link event based on the state of the underlying interface */ |
1477 | bzero(&ifmr, sizeof(ifmr)); | |
1478 | snprintf(ifmr.ifm_name, sizeof(ifmr.ifm_name), | |
1479 | "%s%d", ifnet_name(p), ifnet_unit(p)); | |
1480 | if (ifnet_ioctl(p, 0, SIOCGIFMEDIA, &ifmr) == 0 | |
1481 | && ifmr.ifm_count > 0 && ifmr.ifm_status & IFM_AVALID) { | |
1482 | u_int32_t event; | |
1483 | ||
1484 | event = (ifmr.ifm_status & IFM_ACTIVE) | |
1485 | ? KEV_DL_LINK_ON : KEV_DL_LINK_OFF; | |
1486 | interface_link_event(ifp, event); | |
1487 | } | |
1488 | return; | |
1c79356b A |
1489 | } |
1490 | ||
1491 | static int | |
6d2010ae | 1492 | vlan_unconfig(ifvlan_ref ifv, int need_to_wait) |
1c79356b | 1493 | { |
0a7de745 A |
1494 | struct ifnet * ifp = ifv->ifv_ifp; |
1495 | int last_vlan = FALSE; | |
1496 | int need_ifv_release = 0; | |
1497 | int need_vlp_release = 0; | |
1498 | struct ifnet * p; | |
1499 | vlan_parent_ref vlp; | |
91447636 | 1500 | |
0a7de745 A |
1501 | vlan_assert_lock_held(); |
1502 | vlp = ifv->ifv_vlp; | |
1503 | if (vlp == NULL) { | |
1504 | return 0; | |
6d2010ae | 1505 | } |
0a7de745 A |
1506 | if (need_to_wait) { |
1507 | need_vlp_release++; | |
1508 | vlan_parent_retain(vlp); | |
1509 | vlan_parent_wait(vlp, "vlan_unconfig"); | |
1510 | ||
1511 | /* check again because another thread could be in vlan_unconfig */ | |
1512 | if (ifv != ifnet_get_ifvlan(ifp)) { | |
1513 | goto signal_done; | |
1514 | } | |
1515 | if (ifv->ifv_vlp != vlp) { | |
1516 | /* vlan parent changed */ | |
1517 | goto signal_done; | |
1518 | } | |
6d2010ae | 1519 | } |
6d2010ae | 1520 | |
0a7de745 A |
1521 | /* ifv has a reference on vlp, need to remove it */ |
1522 | need_vlp_release++; | |
1523 | p = vlp->vlp_ifp; | |
91447636 | 1524 | |
0a7de745 A |
1525 | /* remember whether we're the last VLAN on the parent */ |
1526 | if (LIST_NEXT(LIST_FIRST(&vlp->vlp_vlan_list), ifv_vlan_list) == NULL) { | |
1527 | if (g_vlan->verbose) { | |
1528 | printf("vlan_unconfig: last vlan on %s%d\n", | |
1529 | ifnet_name(p), ifnet_unit(p)); | |
1530 | } | |
1531 | last_vlan = TRUE; | |
1c79356b | 1532 | } |
91447636 | 1533 | |
0a7de745 A |
1534 | /* back-out any effect our mtu might have had on the parent */ |
1535 | (void)ifvlan_new_mtu(ifv, ETHERMTU - ifv->ifv_mtufudge); | |
91447636 | 1536 | |
0a7de745 | 1537 | vlan_unlock(); |
91447636 | 1538 | |
0a7de745 A |
1539 | /* un-join multicast on parent interface */ |
1540 | (void)multicast_list_remove(&ifv->ifv_multicast); | |
91447636 | 1541 | |
0a7de745 A |
1542 | /* Clear our MAC address. */ |
1543 | ifnet_set_lladdr_and_type(ifp, NULL, 0, IFT_L2VLAN); | |
2d21ac55 | 1544 | |
0a7de745 A |
1545 | /* detach VLAN "protocol" */ |
1546 | if (last_vlan) { | |
1547 | (void)vlan_detach_protocol(p); | |
1548 | } | |
91447636 | 1549 | |
0a7de745 | 1550 | vlan_lock(); |
91447636 | 1551 | |
0a7de745 | 1552 | /* return to the state we were in before SIFVLAN */ |
f427ee49 | 1553 | ifnet_set_mtu(ifp, ETHERMTU); |
0a7de745 A |
1554 | ifnet_set_flags(ifp, 0, |
1555 | IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_RUNNING); | |
1556 | ifnet_set_offload(ifp, 0); | |
1557 | ifv->ifv_mtufudge = 0; | |
1c79356b | 1558 | |
0a7de745 A |
1559 | /* Disconnect from parent. */ |
1560 | vlan_parent_remove_vlan(vlp, ifv); | |
1561 | ifv->ifv_flags = 0; | |
6d2010ae | 1562 | |
0a7de745 A |
1563 | /* vlan_parent has reference to ifv, remove it */ |
1564 | need_ifv_release++; | |
6d2010ae | 1565 | |
0a7de745 A |
1566 | /* from this point on, no more referencing ifv */ |
1567 | if (last_vlan && !vlan_parent_flags_detaching(vlp)) { | |
1568 | /* the vlan parent has no more VLAN's */ | |
f427ee49 | 1569 | if_clear_eflags(p, IFEF_VLAN); |
0a7de745 | 1570 | LIST_REMOVE(vlp, vlp_parent_list); |
6d2010ae | 1571 | |
0a7de745 A |
1572 | /* one for being in the list */ |
1573 | need_vlp_release++; | |
6d2010ae | 1574 | |
0a7de745 A |
1575 | /* release outside of the lock below */ |
1576 | need_vlp_release++; | |
1577 | } | |
1578 | ||
1579 | signal_done: | |
1580 | if (need_to_wait) { | |
1581 | vlan_parent_signal(vlp, "vlan_unconfig"); | |
1582 | } | |
1583 | vlan_unlock(); | |
1584 | while (need_ifv_release--) { | |
1585 | ifvlan_release(ifv); | |
1586 | } | |
1587 | while (need_vlp_release--) { /* references to vlp */ | |
1588 | vlan_parent_release(vlp); | |
1589 | } | |
1590 | vlan_lock(); | |
1591 | return 0; | |
1c79356b A |
1592 | } |
1593 | ||
1594 | static int | |
91447636 | 1595 | vlan_set_promisc(struct ifnet * ifp) |
4a249263 | 1596 | { |
0a7de745 A |
1597 | int error = 0; |
1598 | ifvlan_ref ifv; | |
1599 | vlan_parent_ref vlp; | |
1600 | ||
1601 | vlan_lock(); | |
1602 | ifv = ifnet_get_ifvlan_retained(ifp); | |
1603 | if (ifv == NULL) { | |
1604 | error = EBUSY; | |
1605 | goto done; | |
1606 | } | |
1607 | ||
1608 | vlp = ifv->ifv_vlp; | |
1609 | if (vlp == NULL) { | |
1610 | goto done; | |
1611 | } | |
1612 | if ((ifnet_flags(ifp) & IFF_PROMISC) != 0) { | |
1613 | if (!ifvlan_flags_promisc(ifv)) { | |
1614 | error = ifnet_set_promiscuous(vlp->vlp_ifp, 1); | |
1615 | if (error == 0) { | |
1616 | ifvlan_flags_set_promisc(ifv); | |
1617 | } | |
1618 | } | |
1619 | } else { | |
1620 | if (ifvlan_flags_promisc(ifv)) { | |
1621 | error = ifnet_set_promiscuous(vlp->vlp_ifp, 0); | |
1622 | if (error == 0) { | |
1623 | ifvlan_flags_clear_promisc(ifv); | |
1624 | } | |
1625 | } | |
1626 | } | |
1627 | done: | |
1628 | vlan_unlock(); | |
1629 | if (ifv != NULL) { | |
1630 | ifvlan_release(ifv); | |
1631 | } | |
1632 | return error; | |
91447636 | 1633 | } |
1c79356b | 1634 | |
91447636 | 1635 | static int |
6d2010ae | 1636 | ifvlan_new_mtu(ifvlan_ref ifv, int mtu) |
91447636 | 1637 | { |
0a7de745 A |
1638 | struct ifdevmtu * devmtu_p; |
1639 | int error = 0; | |
1640 | struct ifnet * ifp = ifv->ifv_ifp; | |
1641 | int max_mtu; | |
1642 | int new_mtu = 0; | |
1643 | int req_mtu; | |
1644 | vlan_parent_ref vlp; | |
1645 | ||
1646 | vlan_assert_lock_held(); | |
1647 | vlp = ifv->ifv_vlp; | |
1648 | devmtu_p = &vlp->vlp_devmtu; | |
1649 | req_mtu = mtu + ifv->ifv_mtufudge; | |
1650 | if (req_mtu > devmtu_p->ifdm_max || req_mtu < devmtu_p->ifdm_min) { | |
1651 | return EINVAL; | |
1652 | } | |
1653 | max_mtu = vlan_parent_find_max_mtu(vlp, ifv); | |
1654 | if (req_mtu > max_mtu) { | |
1655 | new_mtu = req_mtu; | |
1656 | } else if (max_mtu < devmtu_p->ifdm_current) { | |
1657 | new_mtu = max_mtu; | |
1658 | } | |
91447636 | 1659 | if (new_mtu != 0) { |
0a7de745 A |
1660 | struct ifnet * p = vlp->vlp_ifp; |
1661 | vlan_unlock(); | |
1662 | error = siocsifaltmtu(p, new_mtu); | |
1663 | vlan_lock(); | |
91447636 | 1664 | } |
0a7de745 A |
1665 | if (error == 0) { |
1666 | if (new_mtu != 0) { | |
1667 | devmtu_p->ifdm_current = new_mtu; | |
1668 | } | |
1669 | ifnet_set_mtu(ifp, mtu); | |
1670 | } | |
1671 | return error; | |
4a249263 A |
1672 | } |
1673 | ||
1674 | static int | |
91447636 | 1675 | vlan_set_mtu(struct ifnet * ifp, int mtu) |
4a249263 | 1676 | { |
0a7de745 A |
1677 | int error = 0; |
1678 | ifvlan_ref ifv; | |
1679 | vlan_parent_ref vlp; | |
4a249263 | 1680 | |
0a7de745 A |
1681 | if (mtu < IF_MINMTU) { |
1682 | return EINVAL; | |
1683 | } | |
91447636 | 1684 | vlan_lock(); |
0a7de745 A |
1685 | ifv = ifnet_get_ifvlan_retained(ifp); |
1686 | if (ifv == NULL) { | |
1687 | vlan_unlock(); | |
1688 | return EBUSY; | |
91447636 | 1689 | } |
0a7de745 A |
1690 | vlp = ifvlan_get_vlan_parent_retained(ifv); |
1691 | if (vlp == NULL) { | |
1692 | vlan_unlock(); | |
1693 | ifvlan_release(ifv); | |
1694 | if (mtu != 0) { | |
1695 | return EINVAL; | |
1c79356b | 1696 | } |
0a7de745 | 1697 | return 0; |
4a249263 | 1698 | } |
0a7de745 | 1699 | vlan_parent_wait(vlp, "vlan_set_mtu"); |
4a249263 | 1700 | |
0a7de745 A |
1701 | /* check again, something might have changed */ |
1702 | if (ifnet_get_ifvlan(ifp) != ifv | |
1703 | || ifvlan_flags_detaching(ifv)) { | |
1704 | error = EBUSY; | |
1705 | goto signal_done; | |
4a249263 | 1706 | } |
0a7de745 A |
1707 | if (ifv->ifv_vlp != vlp) { |
1708 | /* vlan parent changed */ | |
1709 | goto signal_done; | |
1710 | } | |
1711 | if (vlan_parent_flags_detaching(vlp)) { | |
1712 | if (mtu != 0) { | |
1713 | error = EINVAL; | |
1714 | } | |
1715 | goto signal_done; | |
4a249263 | 1716 | } |
0a7de745 A |
1717 | error = ifvlan_new_mtu(ifv, mtu); |
1718 | ||
1719 | signal_done: | |
1720 | vlan_parent_signal(vlp, "vlan_set_mtu"); | |
91447636 | 1721 | vlan_unlock(); |
0a7de745 A |
1722 | vlan_parent_release(vlp); |
1723 | ifvlan_release(ifv); | |
91447636 | 1724 | |
0a7de745 A |
1725 | return error; |
1726 | } | |
91447636 | 1727 | |
0a7de745 A |
1728 | static int |
1729 | vlan_ioctl(ifnet_t ifp, u_long cmd, void * data) | |
1730 | { | |
1731 | struct ifdevmtu * devmtu_p; | |
1732 | int error = 0; | |
1733 | struct ifaddr * ifa; | |
1734 | struct ifmediareq *ifmr; | |
1735 | struct ifreq * ifr; | |
1736 | ifvlan_ref ifv; | |
1737 | struct ifnet * p; | |
1738 | u_short tag; | |
1739 | user_addr_t user_addr; | |
1740 | vlan_parent_ref vlp; | |
1741 | struct vlanreq vlr; | |
1742 | ||
1743 | if (ifnet_type(ifp) != IFT_L2VLAN) { | |
1744 | return EOPNOTSUPP; | |
4a249263 | 1745 | } |
0a7de745 A |
1746 | ifr = (struct ifreq *)data; |
1747 | ifa = (struct ifaddr *)data; | |
1748 | ||
1749 | switch (cmd) { | |
1750 | case SIOCSIFADDR: | |
1751 | ifnet_set_flags(ifp, IFF_UP, IFF_UP); | |
1752 | break; | |
1753 | ||
1754 | case SIOCGIFMEDIA32: | |
1755 | case SIOCGIFMEDIA64: | |
1756 | vlan_lock(); | |
1757 | ifv = (ifvlan_ref)ifnet_softc(ifp); | |
1758 | if (ifv == NULL || ifvlan_flags_detaching(ifv)) { | |
1759 | vlan_unlock(); | |
1760 | return ifv == NULL ? EOPNOTSUPP : EBUSY; | |
1761 | } | |
1762 | p = (ifv->ifv_vlp == NULL) ? NULL : ifv->ifv_vlp->vlp_ifp; | |
1763 | vlan_unlock(); | |
1764 | ifmr = (struct ifmediareq *)data; | |
1765 | user_addr = (cmd == SIOCGIFMEDIA64) ? | |
1766 | ((struct ifmediareq64 *)ifmr)->ifmu_ulist : | |
1767 | CAST_USER_ADDR_T(((struct ifmediareq32 *)ifmr)->ifmu_ulist); | |
1768 | if (p != NULL) { | |
1769 | struct ifmediareq p_ifmr; | |
1770 | ||
1771 | bzero(&p_ifmr, sizeof(p_ifmr)); | |
1772 | error = ifnet_ioctl(p, 0, SIOCGIFMEDIA, &p_ifmr); | |
1773 | if (error == 0) { | |
1774 | ifmr->ifm_active = p_ifmr.ifm_active; | |
1775 | ifmr->ifm_current = p_ifmr.ifm_current; | |
1776 | ifmr->ifm_mask = p_ifmr.ifm_mask; | |
1777 | ifmr->ifm_status = p_ifmr.ifm_status; | |
1778 | ifmr->ifm_count = p_ifmr.ifm_count; | |
1779 | /* Limit the result to the parent's current config. */ | |
1780 | if (ifmr->ifm_count >= 1 && user_addr != USER_ADDR_NULL) { | |
1781 | ifmr->ifm_count = 1; | |
1782 | error = copyout(&ifmr->ifm_current, user_addr, | |
1783 | sizeof(int)); | |
1784 | } | |
1785 | } | |
1786 | } else { | |
1787 | ifmr->ifm_active = ifmr->ifm_current = IFM_NONE; | |
1788 | ifmr->ifm_mask = 0; | |
1789 | ifmr->ifm_status = IFM_AVALID; | |
1790 | ifmr->ifm_count = 1; | |
1791 | if (user_addr != USER_ADDR_NULL) { | |
1792 | error = copyout(&ifmr->ifm_current, user_addr, sizeof(int)); | |
1793 | } | |
1794 | } | |
91447636 | 1795 | break; |
0a7de745 A |
1796 | |
1797 | case SIOCSIFMEDIA: | |
1798 | error = EOPNOTSUPP; | |
91447636 | 1799 | break; |
0a7de745 A |
1800 | |
1801 | case SIOCGIFDEVMTU: | |
1802 | vlan_lock(); | |
1803 | ifv = (ifvlan_ref)ifnet_softc(ifp); | |
1804 | if (ifv == NULL || ifvlan_flags_detaching(ifv)) { | |
1805 | vlan_unlock(); | |
1806 | return ifv == NULL ? EOPNOTSUPP : EBUSY; | |
1807 | } | |
1808 | vlp = ifv->ifv_vlp; | |
1809 | if (vlp != NULL) { | |
1810 | int min_mtu = vlp->vlp_devmtu.ifdm_min - ifv->ifv_mtufudge; | |
1811 | devmtu_p = &ifr->ifr_devmtu; | |
1812 | devmtu_p->ifdm_current = ifnet_mtu(ifp); | |
1813 | devmtu_p->ifdm_min = max(min_mtu, IF_MINMTU); | |
1814 | devmtu_p->ifdm_max = vlp->vlp_devmtu.ifdm_max - ifv->ifv_mtufudge; | |
1815 | } else { | |
1816 | devmtu_p = &ifr->ifr_devmtu; | |
1817 | devmtu_p->ifdm_current = 0; | |
1818 | devmtu_p->ifdm_min = 0; | |
1819 | devmtu_p->ifdm_max = 0; | |
1820 | } | |
1821 | vlan_unlock(); | |
5c9f4661 | 1822 | break; |
5c9f4661 | 1823 | |
0a7de745 A |
1824 | case SIOCSIFMTU: |
1825 | error = vlan_set_mtu(ifp, ifr->ifr_mtu); | |
91447636 | 1826 | break; |
0a7de745 A |
1827 | |
1828 | case SIOCSIFVLAN: | |
1829 | user_addr = proc_is64bit(current_proc()) | |
1830 | ? ifr->ifr_data64 : CAST_USER_ADDR_T(ifr->ifr_data); | |
1831 | error = copyin(user_addr, &vlr, sizeof(vlr)); | |
1832 | if (error) { | |
1833 | break; | |
1834 | } | |
1835 | p = NULL; | |
1836 | /* ensure nul termination */ | |
1837 | vlr.vlr_parent[IFNAMSIZ - 1] = '\0'; | |
1838 | if (vlr.vlr_parent[0] != '\0') { | |
1839 | if (vlr.vlr_tag & ~EVL_VLID_MASK) { | |
1840 | /* | |
1841 | * Don't let the caller set up a VLAN tag with | |
1842 | * anything except VLID bits. | |
1843 | */ | |
1844 | error = EINVAL; | |
1845 | break; | |
1846 | } | |
1847 | p = ifunit(vlr.vlr_parent); | |
1848 | if (p == NULL) { | |
1849 | error = ENXIO; | |
1850 | break; | |
1851 | } | |
1852 | if (IFNET_IS_INTCOPROC(p)) { | |
1853 | error = EINVAL; | |
1854 | break; | |
1855 | } | |
1856 | ||
1857 | /* can't do VLAN over anything but ethernet or ethernet aggregate */ | |
1858 | if (ifnet_type(p) != IFT_ETHER | |
1859 | && ifnet_type(p) != IFT_IEEE8023ADLAG) { | |
1860 | error = EPROTONOSUPPORT; | |
1861 | break; | |
1862 | } | |
1863 | error = vlan_config(ifp, p, vlr.vlr_tag); | |
1864 | if (error) { | |
1865 | break; | |
1866 | } | |
1867 | ||
1868 | /* Update promiscuous mode, if necessary. */ | |
1869 | (void)vlan_set_promisc(ifp); | |
1870 | ||
1871 | /* generate a link event based on the state of the parent */ | |
1872 | vlan_link_event(ifp, p); | |
1873 | } else { | |
1874 | int need_link_event = FALSE; | |
1875 | ||
1876 | vlan_lock(); | |
1877 | ifv = (ifvlan_ref)ifnet_softc(ifp); | |
1878 | if (ifv == NULL || ifvlan_flags_detaching(ifv)) { | |
1879 | vlan_unlock(); | |
1880 | error = (ifv == NULL ? EOPNOTSUPP : EBUSY); | |
1881 | break; | |
1882 | } | |
f427ee49 A |
1883 | need_link_event = (ifv->ifv_vlp != NULL); |
1884 | vlan_unconfig(ifv, TRUE); | |
0a7de745 A |
1885 | vlan_unlock(); |
1886 | if (need_link_event) { | |
1887 | interface_link_event(ifp, KEV_DL_LINK_OFF); | |
1888 | } | |
1889 | } | |
91447636 | 1890 | break; |
0a7de745 A |
1891 | |
1892 | case SIOCGIFVLAN: | |
1893 | bzero(&vlr, sizeof vlr); | |
1894 | vlan_lock(); | |
1895 | ifv = (ifvlan_ref)ifnet_softc(ifp); | |
1896 | if (ifv == NULL || ifvlan_flags_detaching(ifv)) { | |
1897 | vlan_unlock(); | |
1898 | return ifv == NULL ? EOPNOTSUPP : EBUSY; | |
1899 | } | |
1900 | p = (ifv->ifv_vlp == NULL) ? NULL : ifv->ifv_vlp->vlp_ifp; | |
1901 | tag = ifv->ifv_tag; | |
91447636 | 1902 | vlan_unlock(); |
0a7de745 A |
1903 | if (p != NULL) { |
1904 | snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), | |
1905 | "%s%d", ifnet_name(p), ifnet_unit(p)); | |
1906 | vlr.vlr_tag = tag; | |
1907 | } | |
1908 | user_addr = proc_is64bit(current_proc()) | |
1909 | ? ifr->ifr_data64 : CAST_USER_ADDR_T(ifr->ifr_data); | |
1910 | error = copyout(&vlr, user_addr, sizeof(vlr)); | |
91447636 | 1911 | break; |
4a249263 | 1912 | |
0a7de745 A |
1913 | case SIOCSIFFLAGS: |
1914 | /* | |
1915 | * For promiscuous mode, we enable promiscuous mode on | |
1916 | * the parent if we need promiscuous on the VLAN interface. | |
1917 | */ | |
1918 | error = vlan_set_promisc(ifp); | |
1919 | break; | |
1920 | ||
1921 | case SIOCADDMULTI: | |
1922 | case SIOCDELMULTI: | |
1923 | error = vlan_setmulti(ifp); | |
1924 | break; | |
1925 | default: | |
1926 | error = EOPNOTSUPP; | |
1927 | } | |
1928 | return error; | |
4a249263 A |
1929 | } |
1930 | ||
0a7de745 | 1931 | static void |
4a249263 A |
1932 | vlan_if_free(struct ifnet * ifp) |
1933 | { | |
0a7de745 | 1934 | ifvlan_ref ifv; |
4a249263 | 1935 | |
0a7de745 A |
1936 | if (ifp == NULL) { |
1937 | return; | |
1938 | } | |
1939 | ifv = (ifvlan_ref)ifnet_softc(ifp); | |
1940 | if (ifv == NULL) { | |
1941 | return; | |
1942 | } | |
1943 | ifvlan_release(ifv); | |
1944 | ifnet_release(ifp); | |
91447636 | 1945 | return; |
4a249263 A |
1946 | } |
1947 | ||
91447636 | 1948 | static void |
0a7de745 A |
1949 | vlan_event(struct ifnet * p, __unused protocol_family_t protocol, |
1950 | const struct kev_msg * event) | |
4a249263 | 1951 | { |
0a7de745 | 1952 | int event_code; |
4a249263 | 1953 | |
0a7de745 A |
1954 | /* Check if the interface we are attached to is being detached */ |
1955 | if (event->vendor_code != KEV_VENDOR_APPLE | |
1956 | || event->kev_class != KEV_NETWORK_CLASS | |
1957 | || event->kev_subclass != KEV_DL_SUBCLASS) { | |
1958 | return; | |
1959 | } | |
1960 | event_code = event->event_code; | |
1961 | switch (event_code) { | |
1962 | case KEV_DL_LINK_OFF: | |
1963 | case KEV_DL_LINK_ON: | |
1964 | vlan_parent_link_event(p, event_code); | |
1965 | break; | |
1966 | default: | |
1967 | return; | |
1968 | } | |
91447636 | 1969 | return; |
4a249263 A |
1970 | } |
1971 | ||
6d2010ae A |
1972 | static errno_t |
1973 | vlan_detached(ifnet_t p, __unused protocol_family_t protocol) | |
1974 | { | |
0a7de745 A |
1975 | if (ifnet_is_attached(p, 0) == 0) { |
1976 | /* if the parent isn't attached, remove all VLANs */ | |
1977 | vlan_parent_remove_all_vlans(p); | |
1978 | } | |
1979 | return 0; | |
6d2010ae A |
1980 | } |
1981 | ||
4a249263 | 1982 | static void |
b0d623f7 | 1983 | interface_link_event(struct ifnet * ifp, u_int32_t event_code) |
4a249263 | 1984 | { |
f427ee49 A |
1985 | struct event { |
1986 | u_int32_t ifnet_family; | |
1987 | u_int32_t unit; | |
1988 | char if_name[IFNAMSIZ]; | |
1989 | }; | |
1990 | _Alignas(struct kern_event_msg) char message[sizeof(struct kern_event_msg) + sizeof(struct event)] = { 0 }; | |
1991 | struct kern_event_msg *header = (struct kern_event_msg*)message; | |
1992 | struct event *data = (struct event *)(header + 1); | |
1993 | ||
1994 | header->total_size = sizeof(message); | |
1995 | header->vendor_code = KEV_VENDOR_APPLE; | |
1996 | header->kev_class = KEV_NETWORK_CLASS; | |
1997 | header->kev_subclass = KEV_DL_SUBCLASS; | |
1998 | header->event_code = event_code; | |
1999 | data->ifnet_family = ifnet_family(ifp); | |
2000 | data->unit = (u_int32_t)ifnet_unit(ifp); | |
2001 | strlcpy(data->if_name, ifnet_name(ifp), IFNAMSIZ); | |
2002 | ifnet_event(ifp, header); | |
4a249263 A |
2003 | } |
2004 | ||
2005 | static void | |
6d2010ae | 2006 | vlan_parent_link_event(struct ifnet * p, u_int32_t event_code) |
4a249263 | 2007 | { |
0a7de745 | 2008 | vlan_parent_ref vlp; |
4a249263 | 2009 | |
0a7de745 A |
2010 | vlan_lock(); |
2011 | if ((ifnet_eflags(p) & IFEF_VLAN) == 0) { | |
2012 | vlan_unlock(); | |
2013 | /* no VLAN's */ | |
2014 | return; | |
2015 | } | |
2016 | vlp = parent_list_lookup(p); | |
2017 | if (vlp == NULL) { | |
2018 | /* no VLAN's */ | |
2019 | vlan_unlock(); | |
2020 | return; | |
2021 | } | |
2022 | vlan_parent_flags_set_link_event_required(vlp); | |
2023 | vlp->vlp_event_code = event_code; | |
2024 | if (vlan_parent_flags_change_in_progress(vlp)) { | |
2025 | /* don't block waiting to generate an event */ | |
2026 | vlan_unlock(); | |
2027 | return; | |
2028 | } | |
2029 | vlan_parent_retain(vlp); | |
2030 | vlan_parent_wait(vlp, "vlan_parent_link_event"); | |
2031 | vlan_parent_signal(vlp, "vlan_parent_link_event"); | |
fe8ab488 | 2032 | vlan_unlock(); |
0a7de745 | 2033 | vlan_parent_release(vlp); |
fe8ab488 | 2034 | return; |
4a249263 A |
2035 | } |
2036 | ||
4a249263 A |
2037 | /* |
2038 | * Function: vlan_attach_protocol | |
2039 | * Purpose: | |
2040 | * Attach a DLIL protocol to the interface, using the ETHERTYPE_VLAN | |
91447636 | 2041 | * demux ether type. |
4a249263 | 2042 | * |
91447636 A |
2043 | * The ethernet demux actually special cases VLAN to support hardware. |
2044 | * The demux here isn't used. The demux will return PF_VLAN for the | |
2045 | * appropriate packets and our vlan_input function will be called. | |
4a249263 A |
2046 | */ |
2047 | static int | |
2048 | vlan_attach_protocol(struct ifnet *ifp) | |
2049 | { | |
0a7de745 A |
2050 | int error; |
2051 | struct ifnet_attach_proto_param reg; | |
2052 | ||
2053 | bzero(®, sizeof(reg)); | |
2054 | reg.input = vlan_input; | |
2055 | reg.event = vlan_event; | |
2056 | reg.detached = vlan_detached; | |
2057 | error = ifnet_attach_protocol(ifp, PF_VLAN, ®); | |
2058 | if (error) { | |
2059 | printf("vlan_proto_attach(%s%d) ifnet_attach_protocol failed, %d\n", | |
2060 | ifnet_name(ifp), ifnet_unit(ifp), error); | |
2061 | } | |
2062 | return error; | |
4a249263 A |
2063 | } |
2064 | ||
2065 | /* | |
2066 | * Function: vlan_detach_protocol | |
2067 | * Purpose: | |
2068 | * Detach our DLIL protocol from an interface | |
2069 | */ | |
2070 | static int | |
2071 | vlan_detach_protocol(struct ifnet *ifp) | |
2072 | { | |
0a7de745 A |
2073 | int error; |
2074 | ||
2075 | error = ifnet_detach_protocol(ifp, PF_VLAN); | |
2076 | if (error) { | |
2077 | printf("vlan_proto_detach(%s%d) ifnet_detach_protocol failed, %d\n", | |
2078 | ifnet_name(ifp), ifnet_unit(ifp), error); | |
2079 | } | |
4a249263 | 2080 | |
0a7de745 | 2081 | return error; |
4a249263 A |
2082 | } |
2083 | ||
2084 | /* | |
2085 | * DLIL interface family functions | |
2d21ac55 | 2086 | * We use the ethernet plumb functions, since that's all we support. |
4a249263 A |
2087 | * If we wanted to handle multiple LAN types (tokenring, etc.), we'd |
2088 | * call the appropriate routines for that LAN type instead of hard-coding | |
2089 | * ethernet. | |
2090 | */ | |
2d21ac55 A |
2091 | static errno_t |
2092 | vlan_attach_inet(struct ifnet *ifp, protocol_family_t protocol_family) | |
4a249263 | 2093 | { |
0a7de745 | 2094 | return ether_attach_inet(ifp, protocol_family); |
4a249263 A |
2095 | } |
2096 | ||
2d21ac55 A |
2097 | static void |
2098 | vlan_detach_inet(struct ifnet *ifp, protocol_family_t protocol_family) | |
4a249263 | 2099 | { |
0a7de745 | 2100 | ether_detach_inet(ifp, protocol_family); |
4a249263 A |
2101 | } |
2102 | ||
2d21ac55 A |
2103 | static errno_t |
2104 | vlan_attach_inet6(struct ifnet *ifp, protocol_family_t protocol_family) | |
4a249263 | 2105 | { |
0a7de745 | 2106 | return ether_attach_inet6(ifp, protocol_family); |
4a249263 A |
2107 | } |
2108 | ||
2d21ac55 A |
2109 | static void |
2110 | vlan_detach_inet6(struct ifnet *ifp, protocol_family_t protocol_family) | |
4a249263 | 2111 | { |
0a7de745 | 2112 | ether_detach_inet6(ifp, protocol_family); |
4a249263 A |
2113 | } |
2114 | ||
4a249263 | 2115 | __private_extern__ int |
91447636 | 2116 | vlan_family_init(void) |
4a249263 | 2117 | { |
0a7de745 | 2118 | int error = 0; |
2d21ac55 | 2119 | |
0a7de745 A |
2120 | error = proto_register_plumber(PF_INET, IFNET_FAMILY_VLAN, |
2121 | vlan_attach_inet, vlan_detach_inet); | |
2122 | if (error != 0) { | |
2123 | printf("proto_register_plumber failed for AF_INET error=%d\n", | |
2124 | error); | |
2125 | goto done; | |
2126 | } | |
0a7de745 A |
2127 | error = proto_register_plumber(PF_INET6, IFNET_FAMILY_VLAN, |
2128 | vlan_attach_inet6, vlan_detach_inet6); | |
2129 | if (error != 0) { | |
2130 | printf("proto_register_plumber failed for AF_INET6 error=%d\n", | |
2131 | error); | |
2132 | goto done; | |
2133 | } | |
0a7de745 A |
2134 | error = vlan_clone_attach(); |
2135 | if (error != 0) { | |
2136 | printf("proto_register_plumber failed vlan_clone_attach error=%d\n", | |
2137 | error); | |
2138 | goto done; | |
2139 | } | |
b0d623f7 | 2140 | |
4a249263 | 2141 | |
0a7de745 A |
2142 | done: |
2143 | return error; | |
4a249263 | 2144 | } |