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