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