]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if_vlan.c
xnu-1228.5.18.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
A
1110 } else {
1111 /* Send priority-tagged packet up through the parent */
2d21ac55 1112 dlil_input_packet_list(p, m);
4a249263 1113 }
91447636 1114 return 0;
1c79356b
A
1115}
1116
91447636
A
1117#define VLAN_CONFIG_PROGRESS_VLP_RETAINED 0x1
1118#define VLAN_CONFIG_PROGRESS_IN_LIST 0x2
1119
1c79356b 1120static int
91447636 1121vlan_config(struct ifnet * ifp, struct ifnet * p, int tag)
1c79356b 1122{
91447636
A
1123 int error;
1124 int first_vlan = 0;
1125 ifvlan_ref ifv = NULL;
91447636
A
1126 vlan_parent_ref new_vlp = NULL;
1127 int need_vlp_release = 0;
2d21ac55 1128 u_int16_t parent_flags;
91447636 1129 u_int32_t progress = 0;
91447636
A
1130 vlan_parent_ref vlp = NULL;
1131
1132 /* pre-allocate space for vlan_parent, in case we're first */
1133 error = vlan_parent_create(p, &new_vlp);
1134 if (error != 0) {
1135 return (error);
1136 }
1c79356b 1137
91447636 1138 vlan_lock();
2d21ac55 1139 ifv = (ifvlan_ref)ifnet_softc(ifp);
91447636
A
1140 if (ifv != NULL && ifv->ifv_vlp != NULL) {
1141 vlan_unlock();
1142 vlan_parent_release(new_vlp);
1143 return (EBUSY);
4a249263 1144 }
91447636
A
1145 vlp = parent_list_lookup(p);
1146 if (vlp != NULL) {
1147 if (vlan_parent_lookup_tag(vlp, tag) != NULL) {
1148 /* already a VLAN with that tag on this interface */
1149 error = EADDRINUSE;
1150 goto unlock_done;
1151 }
1152 }
1153 else {
1154 /* we're the first VLAN on this interface */
1155 LIST_INSERT_HEAD(&g_vlan->parent_list, new_vlp, vlp_parent_list);
1156 vlp = new_vlp;
4a249263 1157 }
1c79356b 1158
91447636
A
1159 /* need to wait to ensure no one else is trying to add/remove */
1160 vlan_parent_retain(vlp);
1161 progress |= VLAN_CONFIG_PROGRESS_VLP_RETAINED;
1162 vlan_parent_wait(vlp, "vlan_config");
1163
2d21ac55 1164 ifv = (ifvlan_ref)ifnet_softc(ifp);
91447636
A
1165 if (ifv == NULL) {
1166 error = EOPNOTSUPP;
1167 goto signal_done;
1168 }
1169 if (vlan_parent_flags_detaching(vlp)
1170 || ifvlan_flags_detaching(ifv) || ifv->ifv_vlp != NULL) {
1171 error = EBUSY;
1172 goto signal_done;
4a249263 1173 }
4a249263 1174
91447636
A
1175 /* check again because someone might have gotten in */
1176 if (vlan_parent_lookup_tag(vlp, tag) != NULL) {
1177 /* already a VLAN with that tag on this interface */
1178 error = EADDRINUSE;
1179 goto signal_done;
1180 }
4a249263 1181
91447636
A
1182 if (vlan_parent_no_vlans(vlp)) {
1183 first_vlan = 1;
1184 }
1185 vlan_parent_add_vlan(vlp, ifv, tag);
1186 progress |= VLAN_CONFIG_PROGRESS_IN_LIST;
1187
1188 /* check whether bond interface is using parent interface */
1189 ifnet_lock_exclusive(p);
2d21ac55 1190 if ((ifnet_eflags(p) & IFEF_BOND) != 0) {
91447636
A
1191 ifnet_lock_done(p);
1192 /* don't allow VLAN over interface that's already part of a bond */
1193 error = EBUSY;
1194 goto signal_done;
1195 }
1196 /* prevent BOND interface from using it */
2d21ac55 1197 /* Can't use ifnet_set_eflags because that would take the lock */
91447636
A
1198 p->if_eflags |= IFEF_VLAN;
1199 ifnet_lock_done(p);
1200 vlan_unlock();
1201
1202 if (first_vlan) {
4a249263
A
1203 /* attach our VLAN "protocol" to the interface */
1204 error = vlan_attach_protocol(p);
1205 if (error) {
91447636
A
1206 vlan_lock();
1207 goto signal_done;
4a249263 1208 }
91447636 1209 /* mark the parent interface up */
2d21ac55
A
1210 ifnet_set_flags(p, IFF_UP, IFF_UP);
1211 (void)ifnet_ioctl(p, 0, SIOCSIFFLAGS, (caddr_t)NULL);
91447636 1212 }
1c79356b 1213
91447636
A
1214 /* configure parent to receive our multicast addresses */
1215 error = multicast_list_program(&ifv->ifv_multicast, ifp, p);
1216 if (error != 0) {
1217 if (first_vlan) {
1218 (void)vlan_detach_protocol(p);
4a249263 1219 }
91447636
A
1220 vlan_lock();
1221 goto signal_done;
4a249263 1222 }
91447636 1223
2d21ac55
A
1224 /* set our ethernet address to that of the parent */
1225 ifnet_set_lladdr_and_type(ifp, ifnet_lladdr(p), ETHER_ADDR_LEN, IFT_ETHER);
1226
91447636
A
1227 /* no failures past this point */
1228 vlan_lock();
1229
1230 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1231 ifv->ifv_flags = 0;
1232 if (vlan_parent_flags_supports_vlan_mtu(vlp)) {
4a249263
A
1233 ifv->ifv_mtufudge = 0;
1234 } else {
1c79356b 1235 /*
4a249263
A
1236 * Fudge the MTU by the encapsulation size. This
1237 * makes us incompatible with strictly compliant
1238 * 802.1Q implementations, but allows us to use
1239 * the feature with other NetBSD implementations,
1240 * which might still be useful.
1c79356b 1241 */
4a249263
A
1242 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1243 }
2d21ac55 1244 ifnet_set_mtu(ifp, ETHERMTU - ifv->ifv_mtufudge);
4a249263 1245
4a249263
A
1246 /*
1247 * Copy only a selected subset of flags from the parent.
1248 * Other flags are none of our business.
1249 */
2d21ac55
A
1250 parent_flags = ifnet_flags(p)
1251 & (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX);
1252 ifnet_set_flags(ifp, parent_flags,
1253 IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX);
1254
4a249263
A
1255 /*
1256 * If the parent interface can do hardware-assisted
1257 * VLAN encapsulation, then propagate its hardware-
1258 * assisted checksumming flags.
1259 */
2d21ac55
A
1260 if (ifnet_offload(p) & IF_HWASSIST_VLAN_TAGGING) {
1261 ifnet_set_offload(ifp, IF_HWASSIST_CSUM_FLAGS(ifnet_offload(p)));
4a249263 1262 }
91447636 1263
2d21ac55 1264 ifnet_set_flags(ifp, IFF_RUNNING, IFF_RUNNING);
91447636
A
1265 ifvlan_flags_set_ready(ifv);
1266 vlan_parent_signal(vlp, "vlan_config");
1267 vlan_unlock();
1268 if (new_vlp != vlp) {
1269 /* throw it away, it wasn't needed */
1270 vlan_parent_release(new_vlp);
1271 }
4a249263 1272 return 0;
91447636
A
1273
1274 signal_done:
1275 vlan_assert_lock_held();
1276 vlan_parent_signal(vlp, "vlan_config");
1277
1278 unlock_done:
1279 if ((progress & VLAN_CONFIG_PROGRESS_IN_LIST) != 0) {
1280 vlan_parent_remove_vlan(vlp, ifv);
1281 }
1282 if (!vlan_parent_flags_detaching(vlp) && vlan_parent_no_vlans(vlp)) {
1283 /* the vlan parent has no more VLAN's */
2d21ac55 1284 ifnet_set_eflags(p, 0, IFEF_VLAN);
91447636
A
1285 LIST_REMOVE(vlp, vlp_parent_list);
1286 /* release outside of the lock below */
1287 need_vlp_release = 1;
1288 }
1289 vlan_unlock();
1290
1291 if ((progress & VLAN_CONFIG_PROGRESS_VLP_RETAINED) != 0) {
1292 vlan_parent_release(vlp);
1293 }
1294 if (need_vlp_release) {
1295 vlan_parent_release(vlp);
1296 }
1297 if (new_vlp != vlp) {
1298 vlan_parent_release(new_vlp);
1299 }
1300 return (error);
4a249263
A
1301}
1302
1303static void
1304vlan_link_event(struct ifnet * ifp, struct ifnet * p)
1305{
1306 struct ifmediareq ifmr;
1307
1308 /* generate a link event based on the state of the underlying interface */
1309 bzero(&ifmr, sizeof(ifmr));
1310 snprintf(ifmr.ifm_name, sizeof(ifmr.ifm_name),
2d21ac55
A
1311 "%s%d", ifnet_name(p), ifnet_unit(p));
1312 if (ifnet_ioctl(p, 0, SIOCGIFMEDIA, &ifmr) == 0
4a249263
A
1313 && ifmr.ifm_count > 0 && ifmr.ifm_status & IFM_AVALID) {
1314 u_long event;
1315
1316 event = (ifmr.ifm_status & IFM_ACTIVE)
1317 ? KEV_DL_LINK_ON : KEV_DL_LINK_OFF;
1318 interface_link_event(ifp, event);
1319 }
1320 return;
1c79356b
A
1321}
1322
1323static int
91447636 1324vlan_unconfig(struct ifnet * ifp)
1c79356b 1325{
91447636 1326 int error = 0;
91447636
A
1327 ifvlan_ref ifv;
1328 int last_vlan = 0;
1329 int need_vlp_release = 0;
1330 struct ifnet * p;
91447636 1331 vlan_parent_ref vlp;
1c79356b 1332
91447636 1333 vlan_assert_lock_held();
2d21ac55 1334 ifv = (ifvlan_ref)ifnet_softc(ifp);
91447636
A
1335 if (ifv == NULL) {
1336 return (0);
1337 }
1338 vlp = ifv->ifv_vlp;
1339 if (vlp == NULL) {
1340 return (0);
1341 }
1342 vlan_parent_retain(vlp);
1343 vlan_parent_wait(vlp, "vlan_unconfig");
1344
1345 /* check again because another thread could be in vlan_unconfig */
2d21ac55 1346 ifv = (ifvlan_ref)ifnet_softc(ifp);
91447636
A
1347 if (ifv == NULL) {
1348 goto signal_done;
1349 }
1350 if (ifv->ifv_vlp != vlp) {
1351 /* vlan parent changed */
1352 goto signal_done;
1353 }
1354 need_vlp_release++;
1355 p = vlp->vlp_ifp;
1356
1357 /* remember whether we're the last VLAN on the parent */
1358 if (LIST_NEXT(LIST_FIRST(&vlp->vlp_vlan_list), ifv_vlan_list) == NULL) {
1359 if (g_vlan->verbose) {
1360 printf("vlan_unconfig: last vlan on %s%d\n",
2d21ac55 1361 ifnet_name(p), ifnet_unit(p));
1c79356b 1362 }
91447636
A
1363 last_vlan = 1;
1364 }
1365
1366 /* back-out any effect our mtu might have had on the parent */
1367 (void)vlan_new_mtu(ifp, ETHERMTU - ifv->ifv_mtufudge);
1368
1369 vlan_unlock();
1370
1371 /* detach VLAN "protocol" */
1372 if (last_vlan) {
1373 (void)vlan_detach_protocol(p);
4a249263 1374 }
1c79356b 1375
91447636
A
1376 /* un-join multicast on parent interface */
1377 (void)multicast_list_remove(&ifv->ifv_multicast);
1378
2d21ac55
A
1379 /* Clear our MAC address. */
1380 ifnet_set_lladdr_and_type(ifp, NULL, 0, IFT_L2VLAN);
1381
91447636
A
1382 vlan_lock();
1383
1384 /* Disconnect from parent. */
1385 vlan_parent_remove_vlan(vlp, ifv);
1386
1387 /* return to the state we were in before SIFVLAN */
2d21ac55
A
1388 ifnet_set_mtu(ifp, 0);
1389 ifnet_set_flags(ifp, 0,
1390 IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_RUNNING);
1391 ifnet_set_offload(ifp, 0);
4a249263 1392 ifv->ifv_flags = 0;
4a249263 1393 ifv->ifv_mtufudge = 0;
1c79356b 1394
91447636
A
1395 if (!vlan_parent_flags_detaching(vlp) && vlan_parent_no_vlans(vlp)) {
1396 /* the vlan parent has no more VLAN's */
2d21ac55 1397 ifnet_set_eflags(p, 0, IFEF_VLAN);
91447636
A
1398 LIST_REMOVE(vlp, vlp_parent_list);
1399 /* release outside of the lock below */
1400 need_vlp_release++;
4a249263 1401 }
91447636
A
1402
1403 signal_done:
1404 vlan_parent_signal(vlp, "vlan_unconfig");
1405 vlan_unlock();
1406 vlan_parent_release(vlp); /* one because we waited */
1407
1408 while (need_vlp_release--) {
1409 vlan_parent_release(vlp);
1410 }
1411 vlan_lock();
1412 return (error);
1c79356b
A
1413}
1414
1415static int
91447636 1416vlan_set_promisc(struct ifnet * ifp)
4a249263 1417{
91447636
A
1418 int error = 0;
1419 ifvlan_ref ifv;
1420 vlan_parent_ref vlp;
1421
1422 vlan_lock();
2d21ac55 1423 ifv = (ifvlan_ref)ifnet_softc(ifp);
91447636
A
1424 if (ifv == NULL || ifvlan_flags_detaching(ifv)) {
1425 error = (ifv == NULL) ? EOPNOTSUPP : EBUSY;
1426 goto done;
1427 }
1c79356b 1428
91447636
A
1429 vlp = ifv->ifv_vlp;
1430 if (vlp == NULL) {
1431 goto done;
1432 }
2d21ac55 1433 if ((ifnet_flags(ifp) & IFF_PROMISC) != 0) {
91447636
A
1434 if (!ifvlan_flags_promisc(ifv)) {
1435 error = ifnet_set_promiscuous(vlp->vlp_ifp, 1);
1436 if (error == 0) {
1437 ifvlan_flags_set_promisc(ifv);
1438 }
4a249263
A
1439 }
1440 } else {
91447636
A
1441 if (ifvlan_flags_promisc(ifv)) {
1442 error = ifnet_set_promiscuous(vlp->vlp_ifp, 0);
1443 if (error == 0) {
1444 ifvlan_flags_clear_promisc(ifv);
1445 }
4a249263
A
1446 }
1447 }
91447636
A
1448 done:
1449 vlan_unlock();
1450 return (error);
1451}
1c79356b 1452
91447636
A
1453static int
1454vlan_new_mtu(struct ifnet * ifp, int mtu)
1455{
1456 struct ifdevmtu * devmtu_p;
1457 int error = 0;
1458 ifvlan_ref ifv;
1459 int max_mtu;
1460 int new_mtu = 0;
1461 int req_mtu;
1462 vlan_parent_ref vlp;
1463
1464 vlan_assert_lock_held();
2d21ac55 1465 ifv = (ifvlan_ref)ifnet_softc(ifp);
91447636
A
1466 vlp = ifv->ifv_vlp;
1467 devmtu_p = &vlp->vlp_devmtu;
1468 req_mtu = mtu + ifv->ifv_mtufudge;
1469 if (req_mtu > devmtu_p->ifdm_max || req_mtu < devmtu_p->ifdm_min) {
1470 return (EINVAL);
1471 }
1472 max_mtu = vlan_parent_find_max_mtu(vlp, ifv);
1473 if (req_mtu > max_mtu) {
1474 new_mtu = req_mtu;
1475 }
1476 else if (max_mtu < devmtu_p->ifdm_current) {
1477 new_mtu = max_mtu;
1478 }
1479 if (new_mtu != 0) {
1480 struct ifnet * p = vlp->vlp_ifp;
1481 vlan_unlock();
1482 error = siocsifaltmtu(p, new_mtu);
1483 vlan_lock();
1484 }
1485 if (error == 0) {
1486 if (new_mtu != 0) {
1487 devmtu_p->ifdm_current = new_mtu;
1488 }
2d21ac55 1489 ifnet_set_mtu(ifp, mtu);
91447636 1490 }
4a249263
A
1491 return (error);
1492}
1493
1494static int
91447636 1495vlan_set_mtu(struct ifnet * ifp, int mtu)
4a249263 1496{
91447636
A
1497 int error = 0;
1498 ifvlan_ref ifv;
1499 vlan_parent_ref vlp;
1500
1501 if (mtu < IF_MINMTU) {
1502 return (EINVAL);
1503 }
1504 vlan_lock();
2d21ac55 1505 ifv = (ifvlan_ref)ifnet_softc(ifp);
91447636
A
1506 if (ifv == NULL || ifvlan_flags_detaching(ifv)) {
1507 vlan_unlock();
1508 return ((ifv == NULL) ? EOPNOTSUPP : EBUSY);
1509 }
1510 vlp = ifv->ifv_vlp;
1511 if (vlp == NULL || vlan_parent_flags_detaching(vlp)) {
1512 vlan_unlock();
1513 if (mtu != 0) {
1514 return (EINVAL);
1515 }
1516 return (0);
1517 }
1518 vlan_parent_retain(vlp);
1519 vlan_parent_wait(vlp, "vlan_set_mtu");
1520
1521 /* check again, something might have changed */
2d21ac55 1522 ifv = (ifvlan_ref)ifnet_softc(ifp);
91447636
A
1523 if (ifv == NULL || ifvlan_flags_detaching(ifv)) {
1524 error = (ifv == NULL) ? EOPNOTSUPP : EBUSY;
1525 goto signal_done;
1526 }
1527 if (ifv->ifv_vlp != vlp) {
1528 /* vlan parent changed */
1529 goto signal_done;
1530 }
1531 if (vlp == NULL || vlan_parent_flags_detaching(vlp)) {
1532 if (mtu != 0) {
1533 error = EINVAL;
1534 }
1535 goto signal_done;
1536 }
1537 error = vlan_new_mtu(ifp, mtu);
1538
1539 signal_done:
1540 vlan_parent_signal(vlp, "vlan_set_mtu");
1541 vlan_unlock();
1542 vlan_parent_release(vlp);
1543
1544 return (error);
1545}
4a249263 1546
91447636
A
1547static int
1548vlan_ioctl(ifnet_t ifp, u_int32_t cmd, void * data)
1549{
1550 struct ifdevmtu * devmtu_p;
1551 int error = 0;
1552 struct ifaddr * ifa;
1553 struct ifmediareq64 * ifmr;
1554 struct ifreq * ifr;
1555 ifvlan_ref ifv;
1556 struct ifnet * p;
1557 u_short tag;
1558 user_addr_t user_addr;
1559 vlan_parent_ref vlp;
1560 struct vlanreq vlr;
1561
2d21ac55 1562 if (ifnet_type(ifp) != IFT_L2VLAN) {
91447636
A
1563 return (EOPNOTSUPP);
1564 }
4a249263
A
1565 ifr = (struct ifreq *)data;
1566 ifa = (struct ifaddr *)data;
4a249263
A
1567
1568 switch (cmd) {
1569 case SIOCSIFADDR:
91447636 1570 ifnet_set_flags(ifp, IFF_UP, IFF_UP);
4a249263
A
1571 break;
1572
91447636 1573 case SIOCGIFMEDIA64:
4a249263 1574 case SIOCGIFMEDIA:
91447636 1575 vlan_lock();
2d21ac55 1576 ifv = (ifvlan_ref)ifnet_softc(ifp);
91447636
A
1577 if (ifv == NULL || ifvlan_flags_detaching(ifv)) {
1578 vlan_unlock();
1579 return (ifv == NULL ? EOPNOTSUPP : EBUSY);
1580 }
1581 p = (ifv->ifv_vlp == NULL) ? NULL : ifv->ifv_vlp->vlp_ifp;
1582 vlan_unlock();
1583 ifmr = (struct ifmediareq64 *)data;
2d21ac55 1584 user_addr = proc_is64bit(current_proc())
91447636
A
1585 ? ifmr->ifm_ifmu.ifmu_ulist64
1586 : CAST_USER_ADDR_T(ifmr->ifm_ifmu.ifmu_ulist32);
1587 if (p != NULL) {
1588 struct ifmediareq64 p_ifmr;
4a249263 1589
91447636 1590 bzero(&p_ifmr, sizeof(p_ifmr));
2d21ac55 1591 error = ifnet_ioctl(p, 0, SIOCGIFMEDIA, &p_ifmr);
91447636
A
1592 if (error == 0) {
1593 ifmr->ifm_active = p_ifmr.ifm_active;
1594 ifmr->ifm_current = p_ifmr.ifm_current;
1595 ifmr->ifm_mask = p_ifmr.ifm_mask;
1596 ifmr->ifm_status = p_ifmr.ifm_status;
1597 ifmr->ifm_count = p_ifmr.ifm_count;
1598 /* Limit the result to the parent's current config. */
1599 if (ifmr->ifm_count >= 1 && user_addr != USER_ADDR_NULL) {
4a249263 1600 ifmr->ifm_count = 1;
91447636 1601 error = copyout(&ifmr->ifm_current, user_addr,
4a249263 1602 sizeof(int));
1c79356b 1603 }
4a249263
A
1604 }
1605 } else {
91447636 1606 ifmr->ifm_active = ifmr->ifm_current = IFM_NONE;
4a249263
A
1607 ifmr->ifm_mask = 0;
1608 ifmr->ifm_status = IFM_AVALID;
4a249263 1609 ifmr->ifm_count = 1;
91447636
A
1610 if (user_addr != USER_ADDR_NULL) {
1611 error = copyout(&ifmr->ifm_current, user_addr, sizeof(int));
4a249263 1612 }
4a249263
A
1613 }
1614 break;
1615
1616 case SIOCSIFMEDIA:
91447636 1617 error = EOPNOTSUPP;
4a249263
A
1618 break;
1619
91447636
A
1620 case SIOCGIFDEVMTU:
1621 vlan_lock();
2d21ac55 1622 ifv = (ifvlan_ref)ifnet_softc(ifp);
91447636
A
1623 if (ifv == NULL || ifvlan_flags_detaching(ifv)) {
1624 vlan_unlock();
1625 return (ifv == NULL ? EOPNOTSUPP : EBUSY);
4a249263 1626 }
91447636
A
1627 vlp = ifv->ifv_vlp;
1628 if (vlp != NULL) {
1629 int min_mtu = vlp->vlp_devmtu.ifdm_min - ifv->ifv_mtufudge;
1630 devmtu_p = &ifr->ifr_devmtu;
2d21ac55 1631 devmtu_p->ifdm_current = ifnet_mtu(ifp);
91447636
A
1632 devmtu_p->ifdm_min = max(min_mtu, IF_MINMTU);
1633 devmtu_p->ifdm_max = vlp->vlp_devmtu.ifdm_max - ifv->ifv_mtufudge;
4a249263 1634 }
91447636
A
1635 else {
1636 devmtu_p = &ifr->ifr_devmtu;
1637 devmtu_p->ifdm_current = 0;
1638 devmtu_p->ifdm_min = 0;
1639 devmtu_p->ifdm_max = 0;
4a249263 1640 }
91447636
A
1641 vlan_unlock();
1642 break;
1643
1644 case SIOCSIFMTU:
1645 error = vlan_set_mtu(ifp, ifr->ifr_mtu);
1646 break;
1647
1648 case SIOCSIFVLAN:
1649 user_addr = proc_is64bit(current_proc())
1650 ? ifr->ifr_data64 : CAST_USER_ADDR_T(ifr->ifr_data);
1651 error = copyin(user_addr, &vlr, sizeof(vlr));
4a249263 1652 if (error) {
4a249263
A
1653 break;
1654 }
91447636
A
1655 p = NULL;
1656 if (vlr.vlr_parent[0] != '\0') {
1657 if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1658 /*
1659 * Don't let the caller set up a VLAN tag with
1660 * anything except VLID bits.
1661 */
1662 error = EINVAL;
1663 break;
1664 }
1665 p = ifunit(vlr.vlr_parent);
1666 if (p == NULL) {
1667 error = ENXIO;
1668 break;
1669 }
1670 /* can't do VLAN over anything but ethernet or ethernet aggregate */
2d21ac55
A
1671 if (ifnet_type(p) != IFT_ETHER
1672 && ifnet_type(p) != IFT_IEEE8023ADLAG) {
91447636
A
1673 error = EPROTONOSUPPORT;
1674 break;
1675 }
1676 error = vlan_config(ifp, p, vlr.vlr_tag);
1677 if (error) {
1678 break;
1679 }
1680
1681 /* Update promiscuous mode, if necessary. */
1682 (void)vlan_set_promisc(ifp);
1683
1684 /* generate a link event based on the state of the parent */
1685 vlan_link_event(ifp, p);
1686 } else {
1687 vlan_lock();
2d21ac55 1688 ifv = (ifvlan_ref)ifnet_softc(ifp);
91447636
A
1689 if (ifv == NULL || ifvlan_flags_detaching(ifv)) {
1690 vlan_unlock();
1691 error = (ifv == NULL ? EOPNOTSUPP : EBUSY);
1692 break;
1693 }
1694 error = vlan_unconfig(ifp);
1695 vlan_unlock();
1696 if (error == 0) {
1697 interface_link_event(ifp, KEV_DL_LINK_OFF);
1698 }
1699 }
4a249263 1700 break;
1c79356b 1701
91447636 1702 case SIOCGIFVLAN:
4a249263 1703 bzero(&vlr, sizeof vlr);
91447636 1704 vlan_lock();
2d21ac55 1705 ifv = (ifvlan_ref)ifnet_softc(ifp);
91447636
A
1706 if (ifv == NULL || ifvlan_flags_detaching(ifv)) {
1707 vlan_unlock();
1708 return (ifv == NULL ? EOPNOTSUPP : EBUSY);
1709 }
1710 p = (ifv->ifv_vlp == NULL) ? NULL : ifv->ifv_vlp->vlp_ifp;
1711 tag = ifv->ifv_tag;
1712 vlan_unlock();
1713 if (p != NULL) {
4a249263 1714 snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent),
2d21ac55 1715 "%s%d", ifnet_name(p), ifnet_unit(p));
91447636 1716 vlr.vlr_tag = tag;
4a249263 1717 }
91447636
A
1718 user_addr = proc_is64bit(current_proc())
1719 ? ifr->ifr_data64 : CAST_USER_ADDR_T(ifr->ifr_data);
1720 error = copyout(&vlr, user_addr, sizeof(vlr));
4a249263 1721 break;
1c79356b 1722
4a249263
A
1723 case SIOCSIFFLAGS:
1724 /*
1725 * For promiscuous mode, we enable promiscuous mode on
1726 * the parent if we need promiscuous on the VLAN interface.
1727 */
91447636 1728 error = vlan_set_promisc(ifp);
4a249263
A
1729 break;
1730
1731 case SIOCADDMULTI:
1732 case SIOCDELMULTI:
1733 error = vlan_setmulti(ifp);
1734 break;
1735 default:
1736 error = EOPNOTSUPP;
1737 }
1738 return error;
1739}
1740
91447636 1741static void
4a249263
A
1742vlan_if_free(struct ifnet * ifp)
1743{
91447636 1744 ifvlan_ref ifv;
4a249263
A
1745
1746 if (ifp == NULL) {
91447636 1747 return;
4a249263 1748 }
91447636 1749 vlan_lock();
2d21ac55 1750 ifv = (ifvlan_ref)ifnet_softc(ifp);
4a249263 1751 if (ifv == NULL) {
91447636
A
1752 vlan_unlock();
1753 return;
4a249263 1754 }
91447636 1755 vlan_unlock();
2d21ac55 1756 ifnet_release(ifp);
4a249263 1757 FREE(ifv, M_VLAN);
4a249263
A
1758}
1759
91447636 1760static void
2d21ac55
A
1761vlan_event(struct ifnet * p, __unused protocol_family_t protocol,
1762 const struct kev_msg * event)
4a249263 1763{
91447636 1764 vlan_parent_ref vlp;
4a249263 1765
91447636
A
1766 /* Check if the interface we are attached to is being detached */
1767 if (event->vendor_code != KEV_VENDOR_APPLE
1768 || event->kev_class != KEV_NETWORK_CLASS
1769 || event->kev_subclass != KEV_DL_SUBCLASS) {
1770 return;
4a249263 1771 }
91447636
A
1772 switch (event->event_code) {
1773 case KEV_DL_IF_DETACHING:
1774 case KEV_DL_LINK_OFF:
1775 case KEV_DL_LINK_ON:
1776 break;
1777 default:
1778 return;
4a249263 1779 }
91447636 1780 vlan_lock();
2d21ac55 1781 if ((ifnet_eflags(p) & IFEF_VLAN) == 0) {
91447636
A
1782 vlan_unlock();
1783 /* no VLAN's */
1784 return;
4a249263 1785 }
91447636
A
1786 vlp = parent_list_lookup(p);
1787 if (vlp == NULL) {
1788 /* no VLAN's */
1789 vlan_unlock();
1790 return;
1791 }
1792 switch (event->event_code) {
1793 case KEV_DL_IF_DETACHING:
1794 vlan_parent_flags_set_detaching(vlp);
1795 vlan_parent_remove_all_vlans(vlp);
1796 break;
1797
1798 case KEV_DL_LINK_OFF:
1799 case KEV_DL_LINK_ON:
1800 vlan_parent_link_event(vlp, event->event_code);
1801 break;
1802 default:
1803 break;
4a249263 1804 }
91447636
A
1805 vlan_unlock();
1806 return;
4a249263
A
1807}
1808
1809static void
1810interface_link_event(struct ifnet * ifp, u_long event_code)
1811{
1812 struct {
1813 struct kern_event_msg header;
1814 u_long unit;
1815 char if_name[IFNAMSIZ];
1816 } event;
1817
1818 event.header.total_size = sizeof(event);
1819 event.header.vendor_code = KEV_VENDOR_APPLE;
1820 event.header.kev_class = KEV_NETWORK_CLASS;
1821 event.header.kev_subclass = KEV_DL_SUBCLASS;
1822 event.header.event_code = event_code;
2d21ac55
A
1823 event.header.event_data[0] = ifnet_family(ifp);
1824 event.unit = (u_long) ifnet_unit(ifp);
1825 strncpy(event.if_name, ifnet_name(ifp), IFNAMSIZ);
1826 ifnet_event(ifp, &event.header);
4a249263
A
1827 return;
1828}
1829
1830static void
91447636 1831vlan_parent_link_event(vlan_parent_ref vlp, u_long event_code)
4a249263 1832{
91447636 1833 ifvlan_ref ifv;
4a249263 1834
91447636
A
1835 LIST_FOREACH(ifv, &vlp->vlp_vlan_list, ifv_vlan_list) {
1836 interface_link_event(ifv->ifv_ifp, event_code);
4a249263
A
1837 }
1838 return;
1839
1840}
1841
4a249263
A
1842/*
1843 * Function: vlan_attach_protocol
1844 * Purpose:
1845 * Attach a DLIL protocol to the interface, using the ETHERTYPE_VLAN
91447636 1846 * demux ether type.
4a249263 1847 *
91447636
A
1848 * The ethernet demux actually special cases VLAN to support hardware.
1849 * The demux here isn't used. The demux will return PF_VLAN for the
1850 * appropriate packets and our vlan_input function will be called.
4a249263
A
1851 */
1852static int
1853vlan_attach_protocol(struct ifnet *ifp)
1854{
2d21ac55
A
1855 int error;
1856 struct ifnet_attach_proto_param reg;
91447636
A
1857
1858 bzero(&reg, sizeof(reg));
91447636
A
1859 reg.input = vlan_input;
1860 reg.event = vlan_event;
2d21ac55 1861 error = ifnet_attach_protocol(ifp, PF_VLAN, &reg);
4a249263 1862 if (error) {
2d21ac55
A
1863 printf("vlan_proto_attach(%s%d) ifnet_attach_protocol failed, %d\n",
1864 ifnet_name(ifp), ifnet_unit(ifp), error);
4a249263
A
1865 }
1866 return (error);
1867}
1868
1869/*
1870 * Function: vlan_detach_protocol
1871 * Purpose:
1872 * Detach our DLIL protocol from an interface
1873 */
1874static int
1875vlan_detach_protocol(struct ifnet *ifp)
1876{
4a249263
A
1877 int error;
1878
2d21ac55 1879 error = ifnet_detach_protocol(ifp, PF_VLAN);
4a249263 1880 if (error) {
2d21ac55
A
1881 printf("vlan_proto_detach(%s%d) ifnet_detach_protocol failed, %d\n",
1882 ifnet_name(ifp), ifnet_unit(ifp), error);
4a249263 1883 }
91447636 1884
4a249263
A
1885 return (error);
1886}
1887
1888/*
1889 * DLIL interface family functions
2d21ac55 1890 * We use the ethernet plumb functions, since that's all we support.
4a249263
A
1891 * If we wanted to handle multiple LAN types (tokenring, etc.), we'd
1892 * call the appropriate routines for that LAN type instead of hard-coding
1893 * ethernet.
1894 */
2d21ac55
A
1895static errno_t
1896vlan_attach_inet(struct ifnet *ifp, protocol_family_t protocol_family)
4a249263 1897{
91447636 1898 return (ether_attach_inet(ifp, protocol_family));
4a249263
A
1899}
1900
2d21ac55
A
1901static void
1902vlan_detach_inet(struct ifnet *ifp, protocol_family_t protocol_family)
4a249263 1903{
2d21ac55 1904 ether_detach_inet(ifp, protocol_family);
4a249263
A
1905}
1906
2d21ac55
A
1907#if INET6
1908static errno_t
1909vlan_attach_inet6(struct ifnet *ifp, protocol_family_t protocol_family)
4a249263 1910{
91447636 1911 return (ether_attach_inet6(ifp, protocol_family));
4a249263
A
1912}
1913
2d21ac55
A
1914static void
1915vlan_detach_inet6(struct ifnet *ifp, protocol_family_t protocol_family)
4a249263 1916{
2d21ac55 1917 ether_detach_inet6(ifp, protocol_family);
4a249263 1918}
2d21ac55 1919#endif /* INET6 */
4a249263 1920
2d21ac55
A
1921static errno_t
1922vlan_attach_at(struct ifnet *ifp, protocol_family_t protocol_family)
4a249263 1923{
2d21ac55 1924 return (ether_attach_at(ifp, protocol_family));
1c79356b
A
1925}
1926
2d21ac55
A
1927static void
1928vlan_detach_at(struct ifnet *ifp, protocol_family_t protocol_family)
4a249263 1929{
2d21ac55 1930 ether_detach_at(ifp, protocol_family);
4a249263
A
1931}
1932
4a249263 1933__private_extern__ int
91447636 1934vlan_family_init(void)
4a249263 1935{
91447636 1936 int error=0;
2d21ac55
A
1937
1938 error = proto_register_plumber(PF_INET, IFNET_FAMILY_VLAN,
1939 vlan_attach_inet, vlan_detach_inet);
1940 if (error != 0) {
1941 printf("proto_register_plumber failed for AF_INET error=%d\n",
1942 error);
4a249263
A
1943 goto done;
1944 }
2d21ac55
A
1945#if INET6
1946 error = proto_register_plumber(PF_INET6, IFNET_FAMILY_VLAN,
1947 vlan_attach_inet6, vlan_detach_inet6);
91447636 1948 if (error != 0) {
2d21ac55 1949 printf("proto_register_plumber failed for AF_INET6 error=%d\n",
91447636 1950 error);
4a249263
A
1951 goto done;
1952 }
2d21ac55
A
1953#endif
1954 error = proto_register_plumber(PF_APPLETALK, IFNET_FAMILY_VLAN,
1955 vlan_attach_at, vlan_detach_at);
91447636 1956 if (error != 0) {
2d21ac55 1957 printf("proto_register_plumber failed for AF_APPLETALK error=%d\n",
91447636 1958 error);
4a249263
A
1959 goto done;
1960 }
1961 vlan_clone_attach();
1962
1963 done:
4a249263
A
1964 return (error);
1965}