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