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