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