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