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