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