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