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