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