2 * Copyright (c) 2004-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $apfw: Revision 1.19 2008/10/24 02:34:06 cbzimmer Exp $ */
30 /* $NetBSD: if_bridge.c,v 1.46 2006/11/23 04:07:07 rpaulo Exp $ */
33 * Copyright 2001 Wasabi Systems, Inc.
34 * All rights reserved.
36 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed for the NetBSD Project by
49 * Wasabi Systems, Inc.
50 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
51 * or promote products derived from this software without specific prior
54 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
58 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
68 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
69 * All rights reserved.
71 * Redistribution and use in source and binary forms, with or without
72 * modification, are permitted provided that the following conditions
74 * 1. Redistributions of source code must retain the above copyright
75 * notice, this list of conditions and the following disclaimer.
76 * 2. Redistributions in binary form must reproduce the above copyright
77 * notice, this list of conditions and the following disclaimer in the
78 * documentation and/or other materials provided with the distribution.
79 * 3. All advertising materials mentioning features or use of this software
80 * must display the following acknowledgement:
81 * This product includes software developed by Jason L. Wright
82 * 4. The name of the author may not be used to endorse or promote products
83 * derived from this software without specific prior written permission.
85 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
86 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
87 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
88 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
89 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
90 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
91 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
93 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
94 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
95 * POSSIBILITY OF SUCH DAMAGE.
97 * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp
101 * Network interface bridge support.
105 * - Currently only supports Ethernet-like interfaces (Ethernet,
106 * 802.11, VLANs on Ethernet, etc.) Figure out a nice way
107 * to bridge other types of interfaces (FDDI-FDDI, and maybe
108 * consider heterogenous bridges).
111 #include <sys/cdefs.h>
112 //_KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.46 2006/11/23 04:07:07 rpaulo Exp $");
114 //#include "opt_bridge_ipf.h"
115 //#include "opt_inet.h"
116 //#include "opt_pfil_hooks.h"
117 //#include "opt_wlan.h" /* APPLE MODIFICATION <cbz@apple.com> - Proxy STA support */
118 //#include "bpfilter.h"
119 //#include "gif.h" // APPLE MODIFICATION - add gif support
121 #define BRIDGE_DEBUG 0
123 #include <sys/param.h>
124 #include <sys/kernel.h>
125 #include <sys/mbuf.h>
126 #include <sys/queue.h>
127 #include <sys/socket.h>
128 #include <sys/sockio.h>
129 #include <sys/systm.h>
130 #include <sys/proc.h>
131 //#include <sys/pool.h>
132 #include <sys/kauth.h>
133 #include <sys/random.h>
134 #include <sys/kern_event.h>
135 #include <sys/systm.h>
136 #include <sys/sysctl.h>
138 #include <libkern/libkern.h>
140 #include <kern/zalloc.h>
146 #include <net/if_dl.h>
147 #include <net/if_types.h>
148 #include <net/if_llc.h>
150 #include <net/if_ether.h>
151 #include <net/if_bridgevar.h>
152 #include <net/dlil.h>
154 #include <net/kpi_interfacefilter.h>
156 #include <netinet/in.h>
157 #include <netinet/in_systm.h>
158 #include <netinet/ip.h>
159 #include <netinet/ip_var.h>
161 #include <netinet/ip6.h>
162 #include <netinet6/in6_var.h>
163 #include <netinet6/ip6_var.h>
167 #define static __private_extern__
170 extern void dlil_input_packet_list(struct ifnet
*, struct mbuf
*);
173 * Size of the route hash table. Must be a power of two.
175 /* APPLE MODIFICATION - per Wasabi performance improvement, change the hash table size */
177 #ifndef BRIDGE_RTHASH_SIZE
178 #define BRIDGE_RTHASH_SIZE 1024
181 #ifndef BRIDGE_RTHASH_SIZE
182 #define BRIDGE_RTHASH_SIZE 256
186 /* APPLE MODIFICATION - support for HW checksums */
187 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
188 #include <netinet/udp.h>
189 #include <netinet/tcp.h>
192 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
196 #include <netinet/in.h>
197 #include <netinet/in_var.h>
198 #include <netinet/ip_carp.h>
202 * Maximum number of addresses to cache.
204 #ifndef BRIDGE_RTABLE_MAX
205 #define BRIDGE_RTABLE_MAX 100
208 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
209 #if IEEE80211_PROXYSTA
211 * Maximum (additional to maxcache) number of proxysta addresses to cache.
213 #ifndef BRIDGE_RTABLE_MAX_PROXYSTA
214 #define BRIDGE_RTABLE_MAX_PROXYSTA 16
219 * Spanning tree defaults.
221 #define BSTP_DEFAULT_MAX_AGE (20 * 256)
222 #define BSTP_DEFAULT_HELLO_TIME (2 * 256)
223 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256)
224 #define BSTP_DEFAULT_HOLD_TIME (1 * 256)
225 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000
226 #define BSTP_DEFAULT_PORT_PRIORITY 0x80
227 #define BSTP_DEFAULT_PATH_COST 55
230 * Timeout (in seconds) for entries learned dynamically.
232 #ifndef BRIDGE_RTABLE_TIMEOUT
233 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
237 * Number of seconds between walks of the route list.
239 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
240 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
244 * List of capabilities to mask on the member interface.
246 #define BRIDGE_IFCAPS_MASK \
247 (IFCAP_CSUM_IPv4_Tx | \
248 IFCAP_CSUM_TCPv4_Tx | \
249 IFCAP_CSUM_UDPv4_Tx | \
250 IFCAP_CSUM_TCPv6_Tx | \
254 int bridge_rtable_prune_period
= BRIDGE_RTABLE_PRUNE_PERIOD
;
256 static zone_t bridge_rtnode_pool
= NULL
;
259 bridge_iff_input(void* cookie
, ifnet_t ifp
, __unused protocol_family_t protocol
,
260 mbuf_t
*data
, char **frame_ptr
);
262 bridge_iff_event(void* cookie
, ifnet_t ifp
, __unused protocol_family_t protocol
,
263 const struct kev_msg
*event_msg
);
265 bridge_iff_detached(void* cookie
, __unused ifnet_t interface
);
268 bridge_rthash(__unused
struct bridge_softc
*sc
, const uint8_t *addr
);
270 static int bridge_clone_create(struct if_clone
*, int);
271 static void bridge_clone_destroy(struct ifnet
*);
273 static errno_t
bridge_ioctl(ifnet_t ifp
, unsigned long cmd
, void *data
);
275 static void bridge_mutecaps(struct bridge_iflist
*, int);
277 static int bridge_init(struct ifnet
*);
278 static void bridge_stop(struct ifnet
*, int);
280 #if BRIDGE_MEMBER_OUT_FILTER
282 bridge_iff_output(void *cookie
, ifnet_t ifp
, protocol_family_t protocol
, mbuf_t
*data
);
283 static int bridge_output(struct bridge_softc
*sc
, ifnet_t ifp
, mbuf_t m
);
284 #endif /* BRIDGE_MEMBER_OUT_FILTER */
286 static errno_t
bridge_start(struct ifnet
*, mbuf_t
);
287 static errno_t
bridge_set_bpf_tap(ifnet_t ifn
, bpf_tap_mode mode
, bpf_packet_func bpf_callback
);
288 __private_extern__ errno_t
bridge_bpf_input(ifnet_t ifp
, struct mbuf
*m
);
289 __private_extern__ errno_t
bridge_bpf_output(ifnet_t ifp
, struct mbuf
*m
);
291 static void bridge_detach(ifnet_t ifp
);
293 static errno_t
bridge_input(struct bridge_iflist
*, struct ifnet
*, struct mbuf
*, void *frame_header
);
295 static void bridge_forward(struct bridge_softc
*, struct mbuf
*m
);
297 static void bridge_timer(void *);
299 static void bridge_broadcast(struct bridge_softc
*, struct ifnet
*,
302 static int bridge_rtupdate(struct bridge_softc
*, const uint8_t *,
303 struct ifnet
*, int, uint8_t);
304 static struct ifnet
*bridge_rtlookup(struct bridge_softc
*, const uint8_t *);
305 static void bridge_rttrim(struct bridge_softc
*);
306 static void bridge_rtage(struct bridge_softc
*);
307 static void bridge_rtflush(struct bridge_softc
*, int);
308 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
309 #if IEEE80211_PROXYSTA
310 static void bridge_rtdiscovery(struct bridge_softc
*);
311 static void bridge_rtpurge(struct bridge_softc
*, struct ifnet
*);
313 static int bridge_rtdaddr(struct bridge_softc
*, const uint8_t *);
315 static int bridge_rtable_init(struct bridge_softc
*);
316 static void bridge_rtable_fini(struct bridge_softc
*);
318 static struct bridge_rtnode
*bridge_rtnode_lookup(struct bridge_softc
*,
320 static int bridge_rtnode_insert(struct bridge_softc
*,
321 struct bridge_rtnode
*);
322 static void bridge_rtnode_destroy(struct bridge_softc
*,
323 struct bridge_rtnode
*);
325 static struct bridge_iflist
*bridge_lookup_member(struct bridge_softc
*,
327 static struct bridge_iflist
*bridge_lookup_member_if(struct bridge_softc
*,
329 static void bridge_delete_member(struct bridge_softc
*,
330 struct bridge_iflist
*);
332 static void bridge_ifdetach(struct bridge_iflist
*bif
, struct ifnet
*ifp
);
335 static int bridge_ioctl_add(struct bridge_softc
*, void *);
336 static int bridge_ioctl_del(struct bridge_softc
*, void *);
337 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
338 #if IEEE80211_PROXYSTA
339 static int bridge_ioctl_purge(struct bridge_softc
*sc
, void *arg
);
341 static int bridge_ioctl_gifflags(struct bridge_softc
*, void *);
342 static int bridge_ioctl_sifflags(struct bridge_softc
*, void *);
343 static int bridge_ioctl_scache(struct bridge_softc
*, void *);
344 static int bridge_ioctl_gcache(struct bridge_softc
*, void *);
345 static int bridge_ioctl_gifs32(struct bridge_softc
*, void *);
346 static int bridge_ioctl_gifs64(struct bridge_softc
*, void *);
347 static int bridge_ioctl_rts32(struct bridge_softc
*, void *);
348 static int bridge_ioctl_rts64(struct bridge_softc
*, void *);
349 static int bridge_ioctl_saddr32(struct bridge_softc
*, void *);
350 static int bridge_ioctl_saddr64(struct bridge_softc
*, void *);
351 static int bridge_ioctl_sto(struct bridge_softc
*, void *);
352 static int bridge_ioctl_gto(struct bridge_softc
*, void *);
353 static int bridge_ioctl_daddr32(struct bridge_softc
*, void *);
354 static int bridge_ioctl_daddr64(struct bridge_softc
*, void *);
355 static int bridge_ioctl_flush(struct bridge_softc
*, void *);
356 static int bridge_ioctl_gpri(struct bridge_softc
*, void *);
357 static int bridge_ioctl_spri(struct bridge_softc
*, void *);
358 static int bridge_ioctl_ght(struct bridge_softc
*, void *);
359 static int bridge_ioctl_sht(struct bridge_softc
*, void *);
360 static int bridge_ioctl_gfd(struct bridge_softc
*, void *);
361 static int bridge_ioctl_sfd(struct bridge_softc
*, void *);
362 static int bridge_ioctl_gma(struct bridge_softc
*, void *);
363 static int bridge_ioctl_sma(struct bridge_softc
*, void *);
364 static int bridge_ioctl_sifprio(struct bridge_softc
*, void *);
365 static int bridge_ioctl_sifcost(struct bridge_softc
*, void *);
367 struct bridge_control
{
368 int (*bc_func
)(struct bridge_softc
*, void *);
369 unsigned int bc_argsize
;
370 unsigned int bc_flags
;
373 #define BC_F_COPYIN 0x01 /* copy arguments in */
374 #define BC_F_COPYOUT 0x02 /* copy arguments out */
375 #define BC_F_SUSER 0x04 /* do super-user check */
377 static const struct bridge_control bridge_control_table32
[] = {
378 { bridge_ioctl_add
, sizeof(struct ifbreq
),
379 BC_F_COPYIN
|BC_F_SUSER
},
380 { bridge_ioctl_del
, sizeof(struct ifbreq
),
381 BC_F_COPYIN
|BC_F_SUSER
},
383 { bridge_ioctl_gifflags
, sizeof(struct ifbreq
),
384 BC_F_COPYIN
|BC_F_COPYOUT
},
385 { bridge_ioctl_sifflags
, sizeof(struct ifbreq
),
386 BC_F_COPYIN
|BC_F_SUSER
},
388 { bridge_ioctl_scache
, sizeof(struct ifbrparam
),
389 BC_F_COPYIN
|BC_F_SUSER
},
390 { bridge_ioctl_gcache
, sizeof(struct ifbrparam
),
393 { bridge_ioctl_gifs32
, sizeof(struct ifbifconf32
),
394 BC_F_COPYIN
|BC_F_COPYOUT
},
395 { bridge_ioctl_rts32
, sizeof(struct ifbaconf32
),
396 BC_F_COPYIN
|BC_F_COPYOUT
},
398 { bridge_ioctl_saddr32
, sizeof(struct ifbareq32
),
399 BC_F_COPYIN
|BC_F_SUSER
},
401 { bridge_ioctl_sto
, sizeof(struct ifbrparam
),
402 BC_F_COPYIN
|BC_F_SUSER
},
403 { bridge_ioctl_gto
, sizeof(struct ifbrparam
),
406 { bridge_ioctl_daddr32
, sizeof(struct ifbareq32
),
407 BC_F_COPYIN
|BC_F_SUSER
},
409 { bridge_ioctl_flush
, sizeof(struct ifbreq
),
410 BC_F_COPYIN
|BC_F_SUSER
},
412 { bridge_ioctl_gpri
, sizeof(struct ifbrparam
),
414 { bridge_ioctl_spri
, sizeof(struct ifbrparam
),
415 BC_F_COPYIN
|BC_F_SUSER
},
417 { bridge_ioctl_ght
, sizeof(struct ifbrparam
),
419 { bridge_ioctl_sht
, sizeof(struct ifbrparam
),
420 BC_F_COPYIN
|BC_F_SUSER
},
422 { bridge_ioctl_gfd
, sizeof(struct ifbrparam
),
424 { bridge_ioctl_sfd
, sizeof(struct ifbrparam
),
425 BC_F_COPYIN
|BC_F_SUSER
},
427 { bridge_ioctl_gma
, sizeof(struct ifbrparam
),
429 { bridge_ioctl_sma
, sizeof(struct ifbrparam
),
430 BC_F_COPYIN
|BC_F_SUSER
},
432 { bridge_ioctl_sifprio
, sizeof(struct ifbreq
),
433 BC_F_COPYIN
|BC_F_SUSER
},
435 { bridge_ioctl_sifcost
, sizeof(struct ifbreq
),
436 BC_F_COPYIN
|BC_F_SUSER
},
438 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
439 #if IEEE80211_PROXYSTA
440 { bridge_ioctl_purge
, sizeof(struct ifbreq
),
441 BC_F_COPYIN
|BC_F_SUSER
},
445 static const struct bridge_control bridge_control_table64
[] = {
446 { bridge_ioctl_add
, sizeof(struct ifbreq
),
447 BC_F_COPYIN
|BC_F_SUSER
},
448 { bridge_ioctl_del
, sizeof(struct ifbreq
),
449 BC_F_COPYIN
|BC_F_SUSER
},
451 { bridge_ioctl_gifflags
, sizeof(struct ifbreq
),
452 BC_F_COPYIN
|BC_F_COPYOUT
},
453 { bridge_ioctl_sifflags
, sizeof(struct ifbreq
),
454 BC_F_COPYIN
|BC_F_SUSER
},
456 { bridge_ioctl_scache
, sizeof(struct ifbrparam
),
457 BC_F_COPYIN
|BC_F_SUSER
},
458 { bridge_ioctl_gcache
, sizeof(struct ifbrparam
),
461 { bridge_ioctl_gifs64
, sizeof(struct ifbifconf64
),
462 BC_F_COPYIN
|BC_F_COPYOUT
},
463 { bridge_ioctl_rts64
, sizeof(struct ifbaconf64
),
464 BC_F_COPYIN
|BC_F_COPYOUT
},
466 { bridge_ioctl_saddr64
, sizeof(struct ifbareq64
),
467 BC_F_COPYIN
|BC_F_SUSER
},
469 { bridge_ioctl_sto
, sizeof(struct ifbrparam
),
470 BC_F_COPYIN
|BC_F_SUSER
},
471 { bridge_ioctl_gto
, sizeof(struct ifbrparam
),
474 { bridge_ioctl_daddr64
, sizeof(struct ifbareq64
),
475 BC_F_COPYIN
|BC_F_SUSER
},
477 { bridge_ioctl_flush
, sizeof(struct ifbreq
),
478 BC_F_COPYIN
|BC_F_SUSER
},
480 { bridge_ioctl_gpri
, sizeof(struct ifbrparam
),
482 { bridge_ioctl_spri
, sizeof(struct ifbrparam
),
483 BC_F_COPYIN
|BC_F_SUSER
},
485 { bridge_ioctl_ght
, sizeof(struct ifbrparam
),
487 { bridge_ioctl_sht
, sizeof(struct ifbrparam
),
488 BC_F_COPYIN
|BC_F_SUSER
},
490 { bridge_ioctl_gfd
, sizeof(struct ifbrparam
),
492 { bridge_ioctl_sfd
, sizeof(struct ifbrparam
),
493 BC_F_COPYIN
|BC_F_SUSER
},
495 { bridge_ioctl_gma
, sizeof(struct ifbrparam
),
497 { bridge_ioctl_sma
, sizeof(struct ifbrparam
),
498 BC_F_COPYIN
|BC_F_SUSER
},
500 { bridge_ioctl_sifprio
, sizeof(struct ifbreq
),
501 BC_F_COPYIN
|BC_F_SUSER
},
503 { bridge_ioctl_sifcost
, sizeof(struct ifbreq
),
504 BC_F_COPYIN
|BC_F_SUSER
},
506 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
507 #if IEEE80211_PROXYSTA
508 { bridge_ioctl_purge
, sizeof(struct ifbreq
),
509 BC_F_COPYIN
|BC_F_SUSER
},
513 static const unsigned int bridge_control_table_size
=
514 sizeof(bridge_control_table32
) / sizeof(bridge_control_table32
[0]);
516 static LIST_HEAD(, bridge_softc
) bridge_list
= LIST_HEAD_INITIALIZER(bridge_list
);
518 static lck_grp_t
*bridge_lock_grp
= NULL
;
519 static lck_attr_t
*bridge_lock_attr
= NULL
;
521 static lck_rw_t
*bridge_list_lock
= NULL
;
524 static struct if_clone bridge_cloner
=
525 IF_CLONE_INITIALIZER("bridge",
527 bridge_clone_destroy
,
533 SYSCTL_DECL(_net_link
);
535 SYSCTL_NODE(_net_link
, IFT_BRIDGE
, bridge
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "Bridge");
537 __private_extern__
int _if_brige_debug
= 0;
539 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, debug
, CTLFLAG_RW
,
540 &_if_brige_debug
, 0, "Bridge debug");
542 static void printf_ether_header(struct ether_header
*eh
);
543 static void printf_mbuf_data(mbuf_t m
, size_t offset
, size_t len
);
544 static void printf_mbuf_pkthdr(mbuf_t m
, const char *prefix
, const char *suffix
);
545 static void printf_mbuf(mbuf_t m
, const char *prefix
, const char *suffix
);
546 static void link_print(struct sockaddr_dl
* dl_p
);
549 printf_mbuf_pkthdr(mbuf_t m
, const char *prefix
, const char *suffix
)
552 printf("%spktlen: %u rcvif: %p header: %p nextpkt: %p%s",
553 prefix
? prefix
: "",
554 (unsigned int)mbuf_pkthdr_len(m
), mbuf_pkthdr_rcvif(m
), mbuf_pkthdr_header(m
), mbuf_nextpkt(m
),
555 suffix
? suffix
: "");
557 printf("%s<NULL>%s\n", prefix
, suffix
);
561 printf_mbuf(mbuf_t m
, const char *prefix
, const char *suffix
)
564 printf("%s%p type: %u flags: 0x%x len: %u data: %p maxlen: %u datastart: %p next: %p%s",
565 prefix
? prefix
: "",
566 m
, mbuf_type(m
), mbuf_flags(m
), (unsigned int)mbuf_len(m
), mbuf_data(m
),
567 (unsigned int)mbuf_maxlen(m
), mbuf_datastart(m
), mbuf_next(m
),
568 !suffix
|| (mbuf_flags(m
) & MBUF_PKTHDR
) ? "" : suffix
);
569 if ((mbuf_flags(m
) & MBUF_PKTHDR
))
570 printf_mbuf_pkthdr(m
, " ", suffix
);
572 printf("%s<NULL>%s\n", prefix
, suffix
);
576 printf_mbuf_data(mbuf_t m
, size_t offset
, size_t len
)
580 size_t pktlen
, mlen
, maxlen
;
583 pktlen
= mbuf_pkthdr_len(m
);
588 maxlen
= (pktlen
- offset
> len
) ? len
: pktlen
;
592 for (i
= 0, j
= 0; i
< maxlen
; i
++, j
++) {
602 printf("%02x%s", ptr
[j
], i
% 2 ? " " : "");
609 printf_ether_header(struct ether_header
*eh
)
611 printf("%02x:%02x:%02x:%02x:%02x:%02x > %02x:%02x:%02x:%02x:%02x:%02x 0x%04x ",
612 eh
->ether_shost
[0], eh
->ether_shost
[1], eh
->ether_shost
[2],
613 eh
->ether_shost
[3], eh
->ether_shost
[4], eh
->ether_shost
[5],
614 eh
->ether_dhost
[0], eh
->ether_dhost
[1], eh
->ether_dhost
[2],
615 eh
->ether_dhost
[3], eh
->ether_dhost
[4], eh
->ether_dhost
[5],
618 #endif /* BRIDGE_DEBUG */
623 * Pseudo-device attach routine.
625 __private_extern__
int
626 bridgeattach(__unused
int n
)
629 lck_grp_attr_t
*lck_grp_attr
= NULL
;
631 bridge_rtnode_pool
= zinit(sizeof(struct bridge_rtnode
), 1024 * sizeof(struct bridge_rtnode
),
634 lck_grp_attr
= lck_grp_attr_alloc_init();
636 bridge_lock_grp
= lck_grp_alloc_init("if_bridge", lck_grp_attr
);
638 bridge_lock_attr
= lck_attr_alloc_init();
641 lck_attr_setdebug(bridge_lock_attr
);
644 bridge_list_lock
= lck_rw_alloc_init(bridge_lock_grp
, bridge_lock_attr
);
646 // can free the attributes once we've allocated the group lock
647 lck_grp_attr_free(lck_grp_attr
);
649 LIST_INIT(&bridge_list
);
650 error
= if_clone_attach(&bridge_cloner
);
658 link_print(struct sockaddr_dl
* dl_p
)
663 printf("sdl len %d index %d family %d type 0x%x nlen %d alen %d"
664 " slen %d addr ", dl_p
->sdl_len
,
665 dl_p
->sdl_index
, dl_p
->sdl_family
, dl_p
->sdl_type
,
666 dl_p
->sdl_nlen
, dl_p
->sdl_alen
, dl_p
->sdl_slen
);
668 for (i
= 0; i
< dl_p
->sdl_alen
; i
++)
669 printf("%s%x", i
? ":" : "",
670 (CONST_LLADDR(dl_p
))[i
]);
674 #endif /* BRIDGE_DEBUG */
678 * bridge_clone_create:
680 * Create a new bridge instance.
682 /* APPLE MODIFICATION <cbz@apple.com> - add opaque <const caddr_t params> argument for cloning. This is done for
683 net80211's VAP creation (with the Marvell codebase). I think this could end up being useful
684 for other devices, too. This is not in an ifdef because it doesn't hurt anything to have
687 bridge_clone_create(struct if_clone
*ifc
, int unit
)
689 struct bridge_softc
*sc
= NULL
;
690 struct ifnet
*ifp
= NULL
;
693 struct ifnet_init_params init_params
;
695 uint32_t sdl_buffer
[offsetof(struct sockaddr_dl
, sdl_data
) + IFNAMSIZ
+ ETHER_ADDR_LEN
];
696 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sdl_buffer
;
698 sc
= _MALLOC(sizeof(*sc
), M_DEVBUF
, M_WAITOK
);
699 memset(sc
, 0, sizeof(*sc
));
701 sc
->sc_brtmax
= BRIDGE_RTABLE_MAX
;
702 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
703 #if IEEE80211_PROXYSTA
704 sc
->sc_brtmax_proxysta
= BRIDGE_RTABLE_MAX_PROXYSTA
;
706 sc
->sc_brttimeout
= BRIDGE_RTABLE_TIMEOUT
;
707 sc
->sc_bridge_max_age
= BSTP_DEFAULT_MAX_AGE
;
708 sc
->sc_bridge_hello_time
= BSTP_DEFAULT_HELLO_TIME
;
709 sc
->sc_bridge_forward_delay
= BSTP_DEFAULT_FORWARD_DELAY
;
710 sc
->sc_bridge_priority
= BSTP_DEFAULT_BRIDGE_PRIORITY
;
711 sc
->sc_hold_time
= BSTP_DEFAULT_HOLD_TIME
;
712 sc
->sc_filter_flags
= IFBF_FILT_DEFAULT
;
715 * For backwards compatibility with previous behaviour...
716 * Switch off filtering on the bridge itself if BRIDGE_IPF is
719 sc
->sc_filter_flags
&= ~IFBF_FILT_USEIPF
;
722 /* Initialize our routing table. */
723 error
= bridge_rtable_init(sc
);
725 printf("bridge_clone_create: bridge_rtable_init failed %d\n", error
);
729 LIST_INIT(&sc
->sc_iflist
);
731 sc
->sc_mtx
= lck_mtx_alloc_init(bridge_lock_grp
, bridge_lock_attr
);
733 /* use the interface name as the unique id for ifp recycle */
734 snprintf(sc
->sc_if_xname
, sizeof(sc
->sc_if_xname
), "%s%d",
735 ifc
->ifc_name
, unit
);
736 memset(&init_params
, 0, sizeof(struct ifnet_init_params
));
737 init_params
.uniqueid
= sc
->sc_if_xname
;
738 init_params
.uniqueid_len
= strlen(sc
->sc_if_xname
);
739 init_params
.name
= ifc
->ifc_name
;
740 init_params
.unit
= unit
;
741 init_params
.family
= IFNET_FAMILY_ETHERNET
;
742 init_params
.type
= IFT_BRIDGE
;
743 init_params
.output
= bridge_start
;
744 init_params
.demux
= ether_demux
;
745 init_params
.add_proto
= ether_add_proto
;
746 init_params
.del_proto
= ether_del_proto
;
747 init_params
.check_multi
= ether_check_multi
;
748 init_params
.framer
= ether_frameout
;
749 init_params
.softc
= sc
;
750 init_params
.ioctl
= bridge_ioctl
;
751 init_params
.set_bpf_tap
= bridge_set_bpf_tap
;
752 init_params
.detach
= bridge_detach
;
753 init_params
.broadcast_addr
= etherbroadcastaddr
;
754 init_params
.broadcast_len
= ETHER_ADDR_LEN
;
755 error
= ifnet_allocate(&init_params
, &ifp
);
757 printf("bridge_clone_create: ifnet_allocate failed %d\n", error
);
762 error
= ifnet_set_mtu(ifp
, ETHERMTU
);
764 printf("bridge_clone_create: ifnet_set_mtu failed %d\n", error
);
767 error
= ifnet_set_addrlen(ifp
, ETHER_ADDR_LEN
);
769 printf("bridge_clone_create: ifnet_set_addrlen failed %d\n", error
);
772 error
= ifnet_set_baudrate(ifp
, 10000000) ; // XXX: this is what IONetworking does
774 printf("bridge_clone_create: ifnet_set_baudrate failed %d\n", error
);
777 error
= ifnet_set_hdrlen(ifp
, ETHER_HDR_LEN
);
779 printf("bridge_clone_create: ifnet_set_hdrlen failed %d\n", error
);
782 error
= ifnet_set_flags(ifp
, IFF_BROADCAST
| IFF_SIMPLEX
| IFF_NOTRAILERS
| IFF_MULTICAST
,
785 printf("bridge_clone_create: ifnet_set_flags failed %d\n", error
);
790 * Generate a random ethernet address and use the private AC:DE:48
793 read_random(&r
, sizeof(r
));
797 eaddr
[3] = (r
>> 0) & 0xffu
;
798 eaddr
[4] = (r
>> 8) & 0xffu
;
799 eaddr
[5] = (r
>> 16) & 0xffu
;
801 memset(sdl
, 0, sizeof(sdl_buffer
));
802 sdl
->sdl_family
= AF_LINK
;
803 sdl
->sdl_nlen
= strlen(sc
->sc_if_xname
);
804 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
805 sdl
->sdl_len
= offsetof(struct sockaddr_dl
, sdl_data
);
806 memcpy(sdl
->sdl_data
, sc
->sc_if_xname
, sdl
->sdl_nlen
);
807 memcpy(LLADDR(sdl
), eaddr
, ETHER_ADDR_LEN
);
813 error
= ifnet_attach(ifp
, NULL
);
815 printf("bridge_clone_create: ifnet_attach failed %d\n", error
);
819 error
= ifnet_set_lladdr_and_type(ifp
, eaddr
, ETHER_ADDR_LEN
, IFT_ETHER
);
821 printf("bridge_clone_create: ifnet_set_lladdr_and_type failed %d\n", error
);
825 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
827 * APPLE MODIFICATION - our bridge can support HW checksums
828 * (useful if underlying interfaces support them) on TX,
829 * RX is not that interesting, since the stack just looks to
830 * see if the packet has been checksummed already (I think)
831 * but we might as well indicate we support it
833 ifp
->if_capabilities
=
834 IFCAP_CSUM_IPv4_Tx
| IFCAP_CSUM_TCPv4_Tx
| IFCAP_CSUM_UDPv4_Tx
|
835 IFCAP_CSUM_IPv4_Rx
| IFCAP_CSUM_TCPv4_Rx
| IFCAP_CSUM_UDPv4_Rx
;
838 lck_rw_lock_exclusive(bridge_list_lock
);
839 LIST_INSERT_HEAD(&bridge_list
, sc
, sc_list
);
840 lck_rw_done(bridge_list_lock
);
842 /* attach as ethernet */
843 error
= bpf_attach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
), NULL
, NULL
);
847 printf("bridge_clone_create failed error %d\n", error
);
855 * bridge_clone_destroy:
857 * Destroy a bridge instance.
860 bridge_clone_destroy(struct ifnet
*ifp
)
862 struct bridge_softc
*sc
= (struct bridge_softc
*)ifnet_softc(ifp
);
863 struct bridge_iflist
*bif
;
866 lck_mtx_lock(sc
->sc_mtx
);
867 if ((sc
->sc_flags
& SCF_DETACHING
)) {
868 lck_mtx_unlock(sc
->sc_mtx
);
871 sc
->sc_flags
|= SCF_DETACHING
;
875 error
= ifnet_set_flags(ifp
, 0, IFF_UP
);
877 printf("bridge_clone_destroy: ifnet_set_flags failed %d\n", error
);
880 while ((bif
= LIST_FIRST(&sc
->sc_iflist
)) != NULL
)
881 bridge_delete_member(sc
, bif
);
883 lck_mtx_unlock(sc
->sc_mtx
);
885 error
= ifnet_detach(ifp
);
887 printf("bridge_clone_destroy: ifnet_detach failed %d\n", error
);
888 if ((sc
= (struct bridge_softc
*)ifnet_softc(ifp
)) != NULL
) {
889 lck_mtx_lock(sc
->sc_mtx
);
890 sc
->sc_flags
&= ~SCF_DETACHING
;
891 lck_mtx_unlock(sc
->sc_mtx
);
899 if (ifd->ifd_cmd >= bridge_control_table_size) { \
903 bc = &bridge_control_table[ifd->ifd_cmd]; \
905 if ((cmd & IOC_DIRMASK) == IOC_INOUT && \
906 (bc->bc_flags & BC_F_COPYOUT) == 0) { \
910 else if (((cmd & IOC_DIRMASK) == IOC_IN) && \
911 (bc->bc_flags & BC_F_COPYOUT) != 0) { \
916 if (bc->bc_flags & BC_F_SUSER) { \
917 error = kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER); \
922 if (ifd->ifd_len != bc->bc_argsize || \
923 ifd->ifd_len > sizeof(args)) { \
928 memset(&args, 0, sizeof(args)); \
929 if (bc->bc_flags & BC_F_COPYIN) { \
930 error = copyin(ifd->ifd_data, &args, ifd->ifd_len); \
935 lck_mtx_lock(sc->sc_mtx); \
936 error = (*bc->bc_func)(sc, &args); \
937 lck_mtx_unlock(sc->sc_mtx); \
941 if (bc->bc_flags & BC_F_COPYOUT) \
942 error = copyout(&args, ifd->ifd_data, ifd->ifd_len)
947 * Handle a control request from the operator.
950 bridge_ioctl(ifnet_t ifp
, unsigned long cmd
, void *data
)
952 struct bridge_softc
*sc
= ifnet_softc(ifp
);
953 struct ifreq
*ifr
= (struct ifreq
*) data
;
956 lck_mtx_assert(sc
->sc_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
959 printf("bridge_ioctl: ifp %p cmd 0x%08lx (%c%c [%lu] %c %lu)\n",
962 (cmd
& IOC_IN
) ? 'I' : ' ',
963 (cmd
& IOC_OUT
) ? 'O' : ' ',
967 printf("SIOCGDRVSPEC32 %lx SIOCGDRVSPEC64 %lx\n", SIOCGDRVSPEC32
, SIOCGDRVSPEC64
);
977 case SIOCGDRVSPEC32
: {
979 struct ifbreq ifbreq
;
980 struct ifbifconf32 ifbifconf
;
981 struct ifbareq32 ifbareq
;
982 struct ifbaconf32 ifbaconf
;
983 struct ifbrparam ifbrparam
;
985 struct ifdrv32
*ifd
= (struct ifdrv32
*) data
;
986 const struct bridge_control
*bridge_control_table
= bridge_control_table32
, *bc
;
993 case SIOCGDRVSPEC64
: {
995 struct ifbreq ifbreq
;
996 struct ifbifconf64 ifbifconf
;
997 struct ifbareq64 ifbareq
;
998 struct ifbaconf64 ifbaconf
;
999 struct ifbrparam ifbrparam
;
1001 struct ifdrv64
*ifd
= (struct ifdrv64
*) data
;
1002 const struct bridge_control
*bridge_control_table
= bridge_control_table64
, *bc
;
1010 if ((ifnet_flags(ifp
) & (IFF_UP
|IFF_RUNNING
)) == IFF_RUNNING
) {
1012 * If interface is marked down and it is running,
1013 * then stop and disable it.
1015 lck_mtx_lock(sc
->sc_mtx
);
1016 bridge_stop(ifp
, 1);
1017 lck_mtx_unlock(sc
->sc_mtx
);
1018 } else if ((ifnet_flags(ifp
) & (IFF_UP
|IFF_RUNNING
)) == IFF_UP
) {
1020 * If interface is marked up and it is stopped, then
1023 lck_mtx_lock(sc
->sc_mtx
);
1024 error
= bridge_init(ifp
);
1025 lck_mtx_unlock(sc
->sc_mtx
);
1031 /* APPLE MODIFICATION <cbz@apple.com>
1032 if we wanted to support changing the MTU */
1034 struct ifreq
*ifr
= (struct ifreq
*)data
;
1035 struct bridge_iflist
*bif
;
1036 struct ifnet
*dst_if
;
1037 sc
->sc_if
.if_mtu
= ifr
->ifr_mtu
;
1038 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1039 dst_if
= bif
->bif_ifp
;
1040 error
= ifnet_ioctl(dst_if
, 0, cmd
, data
);
1046 /* Do not allow the MTU to be changed on the bridge */
1051 /* APPLE MODIFICATION - don't pass this down to ether_ioctl, just indicate we don't handle it */
1057 error
= ifnet_set_lladdr(ifp
, ifr
->ifr_addr
.sa_data
, ifr
->ifr_addr
.sa_len
);
1059 printf("bridge_ioctl: ifnet_set_lladdr failed %d\n", error
);
1063 error
= ether_ioctl(ifp
, cmd
, data
);
1066 printf("bridge_ioctl: ether_ioctl ifp %p cmd 0x%08lx (%c%c [%lu] %c %lu) failed error: %d\n",
1069 (cmd
& IOC_IN
) ? 'I' : ' ',
1070 (cmd
& IOC_OUT
) ? 'O' : ' ',
1072 (char) IOCGROUP(cmd
),
1075 #endif /* BRIDGE_DEBUG */
1078 lck_mtx_assert(sc
->sc_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1086 * Clear or restore unwanted capabilities on the member interface
1090 bridge_mutecaps(struct bridge_iflist
*bif
, int mute
)
1092 struct ifnet
*ifp
= bif
->bif_ifp
;
1093 struct ifcapreq ifcr
;
1095 if (ifp
->if_ioctl
== NULL
)
1098 memset(&ifcr
, 0, sizeof(ifcr
));
1099 ifcr
.ifcr_capenable
= ifp
->if_capenable
;
1102 /* mask off and save capabilities */
1103 bif
->bif_mutecap
= ifcr
.ifcr_capenable
& BRIDGE_IFCAPS_MASK
;
1104 if (bif
->bif_mutecap
!= 0)
1105 ifcr
.ifcr_capenable
&= ~BRIDGE_IFCAPS_MASK
;
1107 /* restore muted capabilities */
1108 ifcr
.ifcr_capenable
|= bif
->bif_mutecap
;
1110 if (bif
->bif_mutecap
!= 0) {
1111 (void) (*ifp
->if_ioctl
)(ifp
, SIOCSIFCAP
, (caddr_t
)&ifcr
);
1114 #endif /* HAS_IF_CAP */
1117 * bridge_lookup_member:
1119 static struct bridge_iflist
*
1120 bridge_lookup_member(struct bridge_softc
*sc
, const char *name
)
1122 struct bridge_iflist
*bif
;
1124 char if_xname
[IFNAMSIZ
];
1126 lck_mtx_assert(sc
->sc_mtx
, LCK_MTX_ASSERT_OWNED
);
1128 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1130 snprintf(if_xname
, sizeof(if_xname
), "%s%d",
1131 ifnet_name(ifp
), ifnet_unit(ifp
));
1132 if (strncmp(if_xname
, name
, sizeof(if_xname
)) == 0)
1140 * bridge_lookup_member_if:
1142 static struct bridge_iflist
*
1143 bridge_lookup_member_if(struct bridge_softc
*sc
, struct ifnet
*member_ifp
)
1145 struct bridge_iflist
*bif
;
1147 lck_mtx_assert(sc
->sc_mtx
, LCK_MTX_ASSERT_OWNED
);
1149 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1150 if (bif
->bif_ifp
== member_ifp
)
1158 bridge_iff_input(void* cookie
, ifnet_t ifp
, __unused protocol_family_t protocol
,
1159 mbuf_t
*data
, char **frame_ptr
)
1162 struct bridge_iflist
*bif
= (struct bridge_iflist
*)cookie
;
1163 struct bridge_softc
*sc
= bif
->bif_sc
;
1168 if ((m
->m_flags
& M_PROTO1
))
1171 if (*frame_ptr
>= (char *)mbuf_datastart(m
) && *frame_ptr
<= (char *)mbuf_data(m
)) {
1173 frmlen
= (char *)mbuf_data(m
) - *frame_ptr
;
1176 if (_if_brige_debug
) {
1177 printf("bridge_iff_input %s%d from %s%d m %p data %p frame %p %s frmlen %lu\n",
1178 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
),
1179 ifnet_name(ifp
), ifnet_unit(ifp
),
1180 m
, mbuf_data(m
), *frame_ptr
, included
? "inside" : "outside", frmlen
);
1182 if (_if_brige_debug
> 1) {
1183 printf_mbuf(m
, "bridge_iff_input[", "\n");
1184 printf_ether_header((struct ether_header
*)*frame_ptr
);
1185 printf_mbuf_data(m
, 0, 20);
1189 #endif /* BRIDGE_DEBUG */
1191 /* Move data pointer to start of frame to the link layer header */
1193 (void) mbuf_setdata(m
, (char *)mbuf_data(m
) - frmlen
, mbuf_len(m
) + frmlen
);
1194 (void) mbuf_pkthdr_adjustlen(m
, frmlen
);
1196 printf("bridge_iff_input: frame_ptr outside mbuf\n");
1200 error
= bridge_input(bif
, ifp
, m
, *frame_ptr
);
1202 /* Adjust packet back to original */
1204 (void) mbuf_setdata(m
, (char *)mbuf_data(m
) + frmlen
, mbuf_len(m
) - frmlen
);
1205 (void) mbuf_pkthdr_adjustlen(m
, -frmlen
);
1208 if (_if_brige_debug
> 1) {
1210 printf_mbuf(m
, "bridge_iff_input]", "\n");
1212 #endif /* BRIDGE_DEBUG */
1215 lck_mtx_assert(sc
->sc_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1221 #if BRIDGE_MEMBER_OUT_FILTER
1223 bridge_iff_output(void *cookie
, ifnet_t ifp
, __unused protocol_family_t protocol
, mbuf_t
*data
)
1226 struct bridge_iflist
*bif
= (struct bridge_iflist
*)cookie
;
1227 struct bridge_softc
*sc
= bif
->bif_sc
;
1230 if ((m
->m_flags
& M_PROTO1
))
1234 if (_if_brige_debug
) {
1235 printf("bridge_iff_output %s%d from %s%d m %p data %p\n",
1236 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
),
1237 ifnet_name(ifp
), ifnet_unit(ifp
),
1240 #endif /* BRIDGE_DEBUG */
1242 error
= bridge_output(sc
, ifp
, m
);
1244 printf("bridge_iff_output: bridge_output failed error %d\n", error
);
1248 lck_mtx_assert(sc
->sc_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1252 #endif /* BRIDGE_MEMBER_OUT_FILTER */
1256 bridge_iff_event(void* cookie
, ifnet_t ifp
, __unused protocol_family_t protocol
,
1257 const struct kev_msg
*event_msg
)
1259 struct bridge_iflist
*bif
= (struct bridge_iflist
*)cookie
;
1261 if (event_msg
->vendor_code
== KEV_VENDOR_APPLE
&&
1262 event_msg
->kev_class
== KEV_NETWORK_CLASS
&&
1263 event_msg
->kev_subclass
== KEV_DL_SUBCLASS
) {
1264 switch (event_msg
->event_code
) {
1265 case KEV_DL_IF_DETACHING
:
1266 bridge_ifdetach(bif
, ifp
);
1276 bridge_iff_detached(void* cookie
, __unused ifnet_t interface
)
1278 struct bridge_iflist
*bif
= (struct bridge_iflist
*)cookie
;
1280 _FREE(bif
, M_DEVBUF
);
1286 * bridge_delete_member:
1288 * Delete the specified member interface.
1291 bridge_delete_member(struct bridge_softc
*sc
, struct bridge_iflist
*bif
)
1293 struct ifnet
*ifs
= bif
->bif_ifp
;
1295 switch (ifnet_type(ifs
)) {
1298 * Take the interface out of promiscuous mode.
1300 (void) ifnet_set_promiscuous(ifs
, 0);
1308 panic("bridge_delete_member: impossible");
1313 ifs
->if_bridge
= NULL
;
1314 LIST_REMOVE(bif
, bif_next
);
1316 /* Respect lock ordering with DLIL lock */
1317 lck_mtx_unlock(sc
->sc_mtx
);
1318 iflt_detach(bif
->bif_iff_ref
);
1319 lck_mtx_lock(sc
->sc_mtx
);
1321 bridge_rtdelete(sc
, ifs
, IFBF_FLUSHALL
);
1323 if (ifnet_flags(sc
->sc_if
) & IFF_RUNNING
)
1324 bstp_initialization(sc
);
1326 /* On the last deleted interface revert the MTU */
1328 if (LIST_EMPTY(&sc
->sc_iflist
))
1329 (void) ifnet_set_mtu(sc
->sc_if
, ETHERMTU
);
1333 bridge_ioctl_add(struct bridge_softc
*sc
, void *arg
)
1335 struct ifbreq
*req
= arg
;
1336 struct bridge_iflist
*bif
= NULL
;
1339 /* APPLE MODIFICATION <cbz@apple.com> - is this a proxy sta being added? */
1340 #if IEEE80211_PROXYSTA
1341 struct bridge_rtnode
*brt
;
1344 error
= ifnet_find_by_name(req
->ifbr_ifsname
, &ifs
);
1345 if (error
|| ifs
== NULL
)
1348 /* Is the interface already attached to this bridge interface */
1349 if (ifs
->if_bridge
== sc
)
1352 if (ifs
->if_bridge
!= NULL
)
1355 /* First added interface resets the MTU */
1357 if (LIST_EMPTY(&sc
->sc_iflist
))
1358 (void) ifnet_set_mtu(sc
->sc_if
, ETHERMTU
);
1360 if (ifnet_mtu(sc
->sc_if
) != ifnet_mtu(ifs
))
1363 bif
= _MALLOC(sizeof(*bif
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
1368 bif
->bif_flags
= IFBIF_LEARNING
| IFBIF_DISCOVER
;
1369 bif
->bif_priority
= BSTP_DEFAULT_PORT_PRIORITY
;
1370 bif
->bif_path_cost
= BSTP_DEFAULT_PATH_COST
;
1373 switch (ifnet_type(ifs
)) {
1376 * Place the interface into promiscuous mode.
1378 error
= ifnet_set_promiscuous(ifs
, 1);
1382 bridge_mutecaps(bif
, 1);
1395 * If the LINK0 flag is set, and this is the first member interface,
1396 * attempt to inherit its link-layer address.
1398 if ((ifnet_flags(sc
->sc_if
) & IFF_LINK0
) && LIST_EMPTY(&sc
->sc_iflist
) &&
1399 ifnet_type(ifs
) == IFT_ETHER
) {
1400 (void) ifnet_set_lladdr(sc
->sc_if
, ifnet_lladdr(ifs
),
1404 // install an interface filter
1406 struct iff_filter iff
;
1408 memset(&iff
, 0, sizeof(struct iff_filter
));
1410 iff
.iff_cookie
= bif
;
1411 iff
.iff_name
= "com.apple.kernel.bsd.net.if_bridge";
1412 iff
.iff_input
= bridge_iff_input
;
1413 #if BRIDGE_MEMBER_OUT_FILTER
1414 iff
.iff_output
= bridge_iff_output
;
1415 #endif /* BRIDGE_MEMBER_OUT_FILTER */
1416 iff
.iff_event
= bridge_iff_event
;
1417 iff
.iff_detached
= bridge_iff_detached
;
1419 /* Respect lock ordering with DLIL lock */
1420 lck_mtx_unlock(sc
->sc_mtx
);
1421 error
= iflt_attach(ifs
, &iff
, &bif
->bif_iff_ref
);
1422 lck_mtx_lock(sc
->sc_mtx
);
1424 printf("bridge_ioctl_add: iflt_attach failed %d\n", error
);
1428 ifs
->if_bridge
= sc
;
1429 LIST_INSERT_HEAD(&sc
->sc_iflist
, bif
, bif_next
);
1432 if (ifnet_flags(sc
->sc_if
) & IFF_RUNNING
)
1433 bstp_initialization(sc
);
1437 /* APPLE MODIFICATION <cbz@apple.com> - is this a proxy sta being added? */
1438 #if IEEE80211_PROXYSTA
1439 brt
= bridge_rtnode_lookup(sc
, ifnet_lladdr(ifs
));
1442 printf( "%s: attach %s to bridge as proxysta for %02x:%02x:%02x:%02x:%02x:%02x discovered on %s\n",
1443 __func__
, ifs
->if_xname
, brt
->brt_addr
[0], brt
->brt_addr
[1], brt
->brt_addr
[2],
1444 brt
->brt_addr
[3], brt
->brt_addr
[4], brt
->brt_addr
[5], brt
->brt_ifp
->if_xname
);
1446 brt
->brt_ifp_proxysta
= ifs
;
1454 _FREE(bif
, M_DEVBUF
);
1460 bridge_ioctl_del(struct bridge_softc
*sc
, void *arg
)
1462 struct ifbreq
*req
= arg
;
1463 struct bridge_iflist
*bif
;
1465 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1469 bridge_delete_member(sc
, bif
);
1474 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
1475 #if IEEE80211_PROXYSTA
1477 bridge_ioctl_purge(struct bridge_softc
*sc
, void *arg
)
1479 struct ifbreq
*req
= arg
;
1480 struct bridge_iflist
*bif
;
1483 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1488 bridge_rtpurge(sc
, ifs
);
1495 bridge_ioctl_gifflags(struct bridge_softc
*sc
, void *arg
)
1497 struct ifbreq
*req
= arg
;
1498 struct bridge_iflist
*bif
;
1500 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1504 req
->ifbr_ifsflags
= bif
->bif_flags
;
1505 req
->ifbr_state
= bif
->bif_state
;
1506 req
->ifbr_priority
= bif
->bif_priority
;
1507 req
->ifbr_path_cost
= bif
->bif_path_cost
;
1508 req
->ifbr_portno
= ifnet_index(bif
->bif_ifp
) & 0xffff;
1514 bridge_ioctl_sifflags(struct bridge_softc
*sc
, void *arg
)
1516 struct ifbreq
*req
= arg
;
1517 struct bridge_iflist
*bif
;
1519 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1523 if (req
->ifbr_ifsflags
& IFBIF_STP
) {
1524 switch (ifnet_type(bif
->bif_ifp
)) {
1526 /* These can do spanning tree. */
1530 /* Nothing else can. */
1535 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
1536 #if IEEE80211_PROXYSTA
1537 if ((bif
->bif_flags
& IFBIF_PROXYSTA_DISCOVER
) &&
1538 ((req
->ifbr_ifsflags
& IFBIF_PROXYSTA_DISCOVER
) == 0))
1539 bridge_rtpurge(sc
, bif
->bif_ifp
);
1542 bif
->bif_flags
= req
->ifbr_ifsflags
;
1544 if (ifnet_flags(sc
->sc_if
) & IFF_RUNNING
)
1545 bstp_initialization(sc
);
1547 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
1548 #if IEEE80211_PROXYSTA
1549 if (bif
->bif_flags
& IFBIF_PROXYSTA_DISCOVER
)
1550 bridge_rtdiscovery(sc
);
1557 bridge_ioctl_scache(struct bridge_softc
*sc
, void *arg
)
1559 struct ifbrparam
*param
= arg
;
1561 sc
->sc_brtmax
= param
->ifbrp_csize
;
1568 bridge_ioctl_gcache(struct bridge_softc
*sc
, void *arg
)
1570 struct ifbrparam
*param
= arg
;
1572 param
->ifbrp_csize
= sc
->sc_brtmax
;
1577 #define BRIDGE_IOCTL_GIFS \
1578 struct bridge_iflist *bif; \
1579 struct ifbreq breq; \
1580 int count, error = 0; \
1584 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) \
1587 if (bifc->ifbic_len == 0) { \
1588 bifc->ifbic_len = sizeof(breq) * count; \
1593 len = bifc->ifbic_len; \
1594 memset(&breq, 0, sizeof breq); \
1595 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { \
1596 if (len < sizeof(breq)) \
1599 snprintf(breq.ifbr_ifsname, sizeof(breq.ifbr_ifsname), "%s%d", \
1600 ifnet_name(bif->bif_ifp), ifnet_unit(bif->bif_ifp)); \
1601 breq.ifbr_ifsflags = bif->bif_flags; \
1602 breq.ifbr_state = bif->bif_state; \
1603 breq.ifbr_priority = bif->bif_priority; \
1604 breq.ifbr_path_cost = bif->bif_path_cost; \
1605 breq.ifbr_portno = ifnet_index(bif->bif_ifp) & 0xffff; \
1606 error = copyout(&breq, bifc->ifbic_req + count * sizeof(breq), sizeof(breq)); \
1610 len -= sizeof(breq); \
1613 bifc->ifbic_len = sizeof(breq) * count
1617 bridge_ioctl_gifs64(struct bridge_softc
*sc
, void *arg
)
1619 struct ifbifconf64
*bifc
= arg
;
1627 bridge_ioctl_gifs32(struct bridge_softc
*sc
, void *arg
)
1629 struct ifbifconf32
*bifc
= arg
;
1636 #define BRIDGE_IOCTL_RTS \
1637 struct bridge_rtnode *brt; \
1638 int count = 0, error = 0; \
1640 struct timespec now; \
1642 if (bac->ifbac_len == 0) \
1645 len = bac->ifbac_len; \
1646 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) { \
1647 if (len < sizeof(bareq)) \
1649 memset(&bareq, 0, sizeof(bareq)); \
1650 snprintf(bareq.ifba_ifsname, sizeof(bareq.ifba_ifsname), "%s%d", \
1651 ifnet_name(brt->brt_ifp), ifnet_unit(brt->brt_ifp)); \
1652 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr)); \
1653 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { \
1655 if (brt->brt_expire >= (unsigned long)now.tv_sec) \
1656 bareq.ifba_expire = brt->brt_expire - now.tv_sec; \
1658 bareq.ifba_expire = 0; \
1660 bareq.ifba_expire = 0; \
1661 bareq.ifba_flags = brt->brt_flags; \
1663 error = copyout(&bareq, bac->ifbac_req + count * sizeof(bareq), sizeof(bareq)); \
1667 len -= sizeof(bareq); \
1670 bac->ifbac_len = sizeof(bareq) * count
1674 bridge_ioctl_rts64(struct bridge_softc
*sc
, void *arg
)
1676 struct ifbaconf64
*bac
= arg
;
1677 struct ifbareq64 bareq
;
1685 bridge_ioctl_rts32(struct bridge_softc
*sc
, void *arg
)
1687 struct ifbaconf32
*bac
= arg
;
1688 struct ifbareq32 bareq
;
1696 bridge_ioctl_saddr64(struct bridge_softc
*sc
, void *arg
)
1698 struct ifbareq64
*req
= arg
;
1699 struct bridge_iflist
*bif
;
1702 bif
= bridge_lookup_member(sc
, req
->ifba_ifsname
);
1706 error
= bridge_rtupdate(sc
, req
->ifba_dst
, bif
->bif_ifp
, 1,
1713 bridge_ioctl_saddr32(struct bridge_softc
*sc
, void *arg
)
1715 struct ifbareq32
*req
= arg
;
1716 struct bridge_iflist
*bif
;
1719 bif
= bridge_lookup_member(sc
, req
->ifba_ifsname
);
1723 error
= bridge_rtupdate(sc
, req
->ifba_dst
, bif
->bif_ifp
, 1,
1730 bridge_ioctl_sto(struct bridge_softc
*sc
, void *arg
)
1732 struct ifbrparam
*param
= arg
;
1734 sc
->sc_brttimeout
= param
->ifbrp_ctime
;
1740 bridge_ioctl_gto(struct bridge_softc
*sc
, void *arg
)
1742 struct ifbrparam
*param
= arg
;
1744 param
->ifbrp_ctime
= sc
->sc_brttimeout
;
1750 bridge_ioctl_daddr64(struct bridge_softc
*sc
, void *arg
)
1752 struct ifbareq64
*req
= arg
;
1754 return (bridge_rtdaddr(sc
, req
->ifba_dst
));
1758 bridge_ioctl_daddr32(struct bridge_softc
*sc
, void *arg
)
1760 struct ifbareq32
*req
= arg
;
1762 return (bridge_rtdaddr(sc
, req
->ifba_dst
));
1766 bridge_ioctl_flush(struct bridge_softc
*sc
, void *arg
)
1768 struct ifbreq
*req
= arg
;
1770 bridge_rtflush(sc
, req
->ifbr_ifsflags
);
1776 bridge_ioctl_gpri(struct bridge_softc
*sc
, void *arg
)
1778 struct ifbrparam
*param
= arg
;
1780 param
->ifbrp_prio
= sc
->sc_bridge_priority
;
1786 bridge_ioctl_spri(struct bridge_softc
*sc
, void *arg
)
1788 struct ifbrparam
*param
= arg
;
1790 sc
->sc_bridge_priority
= param
->ifbrp_prio
;
1792 if (ifnet_flags(sc
->sc_if
) & IFF_RUNNING
)
1793 bstp_initialization(sc
);
1799 bridge_ioctl_ght(struct bridge_softc
*sc
, void *arg
)
1801 struct ifbrparam
*param
= arg
;
1803 param
->ifbrp_hellotime
= sc
->sc_bridge_hello_time
>> 8;
1809 bridge_ioctl_sht(struct bridge_softc
*sc
, void *arg
)
1811 struct ifbrparam
*param
= arg
;
1813 if (param
->ifbrp_hellotime
== 0)
1815 sc
->sc_bridge_hello_time
= param
->ifbrp_hellotime
<< 8;
1817 if (ifnet_flags(sc
->sc_if
) & IFF_RUNNING
)
1818 bstp_initialization(sc
);
1824 bridge_ioctl_gfd(struct bridge_softc
*sc
, void *arg
)
1826 struct ifbrparam
*param
= arg
;
1828 param
->ifbrp_fwddelay
= sc
->sc_bridge_forward_delay
>> 8;
1834 bridge_ioctl_sfd(struct bridge_softc
*sc
, void *arg
)
1836 struct ifbrparam
*param
= arg
;
1838 if (param
->ifbrp_fwddelay
== 0)
1840 sc
->sc_bridge_forward_delay
= param
->ifbrp_fwddelay
<< 8;
1842 if (ifnet_flags(sc
->sc_if
) & IFF_RUNNING
)
1843 bstp_initialization(sc
);
1849 bridge_ioctl_gma(struct bridge_softc
*sc
, void *arg
)
1851 struct ifbrparam
*param
= arg
;
1853 param
->ifbrp_maxage
= sc
->sc_bridge_max_age
>> 8;
1859 bridge_ioctl_sma(struct bridge_softc
*sc
, void *arg
)
1861 struct ifbrparam
*param
= arg
;
1863 if (param
->ifbrp_maxage
== 0)
1865 sc
->sc_bridge_max_age
= param
->ifbrp_maxage
<< 8;
1867 if (ifnet_flags(sc
->sc_if
) & IFF_RUNNING
)
1868 bstp_initialization(sc
);
1874 bridge_ioctl_sifprio(struct bridge_softc
*sc
, void *arg
)
1876 struct ifbreq
*req
= arg
;
1877 struct bridge_iflist
*bif
;
1879 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1883 bif
->bif_priority
= req
->ifbr_priority
;
1885 if (ifnet_flags(sc
->sc_if
) & IFF_RUNNING
)
1886 bstp_initialization(sc
);
1891 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
1892 #if IEEE80211_PROXYSTA
1894 bridge_proxysta_notify_macaddr(struct ifnet
*ifp
, int op
, const uint8_t *mac
)
1896 struct proxy_sta_event iev
;
1898 memset(&iev
, 0, sizeof(iev
));
1899 memcpy(iev
.iev_addr
, mac
, ETHER_ADDR_LEN
);
1901 rt_proxystamsg(ifp
, op
, &iev
, sizeof(iev
));
1905 bridge_proxysta_discover(struct ifnet
*ifp
, const uint8_t *mac
)
1907 bridge_proxysta_notify_macaddr( ifp
, RTM_PROXYSTA_DISCOVERY
, mac
);
1911 bridge_proxysta_idle_timeout(struct ifnet
*ifp
, const uint8_t *mac
)
1913 bridge_proxysta_notify_macaddr( ifp
, RTM_PROXYSTA_IDLE_TIMEOUT
, mac
);
1918 bridge_ioctl_sifcost(struct bridge_softc
*sc
, void *arg
)
1920 struct ifbreq
*req
= arg
;
1921 struct bridge_iflist
*bif
;
1923 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1927 bif
->bif_path_cost
= req
->ifbr_path_cost
;
1929 if (ifnet_flags(sc
->sc_if
) & IFF_RUNNING
)
1930 bstp_initialization(sc
);
1938 * Detach an interface from a bridge. Called when a member
1939 * interface is detaching.
1942 bridge_ifdetach(struct bridge_iflist
*bif
, struct ifnet
*ifp
)
1944 struct bridge_softc
*sc
= bif
->bif_sc
;
1947 memset(&breq
, 0, sizeof(breq
));
1948 snprintf(breq
.ifbr_ifsname
, sizeof(breq
.ifbr_ifsname
), "%s%d",
1949 ifnet_name(ifp
), ifnet_unit(ifp
));
1951 lck_mtx_lock(sc
->sc_mtx
);
1953 (void) bridge_ioctl_del(sc
, &breq
);
1955 lck_mtx_unlock(sc
->sc_mtx
);
1961 * Initialize a bridge interface.
1964 bridge_init(struct ifnet
*ifp
)
1966 struct bridge_softc
*sc
= ifnet_softc(ifp
);
1970 if (ifnet_flags(ifp
) & IFF_RUNNING
)
1973 ts
.tv_sec
= bridge_rtable_prune_period
;
1975 bsd_timeout(bridge_timer
, sc
, &ts
);
1977 error
= ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
1979 bstp_initialization(sc
);
1987 * Stop the bridge interface.
1990 bridge_stop(struct ifnet
*ifp
, __unused
int disable
)
1992 struct bridge_softc
*sc
= ifnet_softc(ifp
);
1994 if ((ifnet_flags(ifp
) & IFF_RUNNING
) == 0)
1997 bsd_untimeout(bridge_timer
, sc
);
2000 bridge_rtflush(sc
, IFBF_FLUSHDYN
);
2002 (void) ifnet_set_flags(ifp
, 0, IFF_RUNNING
);
2008 * Enqueue a packet on a bridge member interface.
2010 * Note: this is called both on the input and output path so this routine
2011 * cannot simply muck with the HW checksum flag. For the time being we
2012 * rely on the caller to do the right thing.
2014 __private_extern__
void
2015 bridge_enqueue(struct bridge_softc
*sc
, struct ifnet
*dst_ifp
, struct mbuf
*m
)
2019 lck_mtx_assert(sc
->sc_mtx
, LCK_MTX_ASSERT_OWNED
);
2022 if (_if_brige_debug
)
2023 printf("bridge_enqueue sc %s%d to dst_ifp %s%d m %p\n",
2024 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
),
2025 ifnet_name(dst_ifp
), ifnet_unit(dst_ifp
), m
);
2026 #endif /* BRIDGE_DEBUG */
2028 len
= m
->m_pkthdr
.len
;
2029 m
->m_flags
|= M_PROTO1
; //set to avoid loops
2031 error
= ifnet_output_raw(dst_ifp
, 0, m
);
2033 (void) ifnet_stat_increment_out(sc
->sc_if
, 1, len
, 0);
2035 (void) ifnet_stat_increment_out(sc
->sc_if
, 0, 0, 1);
2042 #if BRIDGE_MEMBER_OUT_FILTER
2047 * Send output from a bridge member interface. This
2048 * performs the bridging function for locally originated
2051 * The mbuf has the Ethernet header already attached. We must
2052 * enqueue or free the mbuf before returning.
2055 bridge_output(struct bridge_softc
*sc
, ifnet_t ifp
, mbuf_t m
)
2057 struct ether_header
*eh
;
2058 struct ifnet
*dst_if
;
2061 if (_if_brige_debug
)
2062 printf("bridge_output ifp %p %s%d\n", ifp
, ifnet_name(ifp
), ifnet_unit(ifp
));
2063 #endif /* BRIDGE_DEBUG */
2065 if (m
->m_len
< ETHER_HDR_LEN
) {
2066 m
= m_pullup(m
, ETHER_HDR_LEN
);
2068 printf("bridge_output ifp %p m_pullup failed\n", ifp
);
2073 eh
= mtod(m
, struct ether_header
*);
2075 /* APPLE MODIFICATION <jhw@apple.com>
2076 * If the packet is an 802.1X ethertype, then only send on the
2077 * original output interface.
2079 if (eh
->ether_type
== htons(ETHERTYPE_PAE
)) {
2085 * If bridge is down, but the original output interface is up,
2086 * go ahead and send out that interface. Otherwise, the packet
2089 if ((ifnet_flags(sc
->sc_if
) & IFF_RUNNING
) == 0) {
2094 lck_mtx_lock(sc
->sc_mtx
);
2097 * If the packet is a multicast, or we don't know a better way to
2098 * get there, send to all interfaces.
2100 if (ETHER_IS_MULTICAST(eh
->ether_dhost
))
2103 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
);
2104 if (dst_if
== NULL
) {
2105 struct bridge_iflist
*bif
;
2109 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
2110 dst_if
= bif
->bif_ifp
;
2111 if ((ifnet_flags(dst_if
) & IFF_RUNNING
) == 0)
2115 * If this is not the original output interface,
2116 * and the interface is participating in spanning
2117 * tree, make sure the port is in a state that
2118 * allows forwarding.
2120 if (dst_if
!= ifp
&&
2121 (bif
->bif_flags
& IFBIF_STP
) != 0) {
2122 switch (bif
->bif_state
) {
2123 case BSTP_IFSTATE_BLOCKING
:
2124 case BSTP_IFSTATE_LISTENING
:
2125 case BSTP_IFSTATE_DISABLED
:
2130 if (LIST_NEXT(bif
, bif_next
) == NULL
) {
2134 mc
= m_copym(m
, 0, M_COPYALL
, M_NOWAIT
);
2136 printf("bridge_output ifp %p m_copym failed\n", ifp
);
2137 (void) ifnet_stat_increment_out(sc
->sc_if
, 0, 0, 1);
2142 bridge_enqueue(sc
, dst_if
, mc
);
2145 printf("bridge_output ifp %p not used\n", ifp
);
2148 lck_mtx_unlock(sc
->sc_mtx
);
2155 * XXX Spanning tree consideration here?
2158 if ((ifnet_flags(dst_if
) & IFF_RUNNING
) == 0) {
2159 printf("bridge_output ifp %p dst_if %p not running\n", ifp
, dst_if
);
2165 if (dst_if
!= ifp
) {
2166 lck_mtx_lock(sc
->sc_mtx
);
2168 bridge_enqueue(sc
, dst_if
, m
);
2170 lck_mtx_unlock(sc
->sc_mtx
);
2177 #endif /* BRIDGE_MEMBER_OUT_FILTER */
2179 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
2180 static struct mbuf
* bridge_fix_txcsum( struct mbuf
*m
)
2182 // basic tests indicate that the vast majority of packets being processed
2183 // here have an Ethernet header mbuf pre-pended to them (the first case below)
2184 // the second highest are those where the Ethernet and IP/TCP/UDP headers are
2185 // all in one mbuf (second case below)
2186 // the third case has, in fact, never hit for me -- although if I comment out
2187 // the first two cases, that code works for them, so I consider it a
2188 // decent general solution
2190 int amt
= ETHER_HDR_LEN
;
2191 int hlen
= M_CSUM_DATA_IPv4_IPHL( m
->m_pkthdr
.csum_data
);
2192 int off
= M_CSUM_DATA_IPv4_OFFSET( m
->m_pkthdr
.csum_data
);
2195 * NOTE we should never get vlan-attached packets here;
2196 * support for those COULD be added, but we don't use them
2197 * and it really kinda slows things down to worry about them
2201 if ( m_tag_find( m
, PACKET_TAG_VLAN
, NULL
) != NULL
)
2203 printf( "bridge: transmitting packet tagged with VLAN?\n" );
2210 if ( m
->m_pkthdr
.csum_flags
& M_CSUM_IPv4
)
2214 if ( m
->m_pkthdr
.csum_flags
& M_CSUM_TCPv4
)
2216 amt
+= off
+ sizeof( uint16_t );
2219 if ( m
->m_pkthdr
.csum_flags
& M_CSUM_UDPv4
)
2221 amt
+= off
+ sizeof( uint16_t );
2224 if ( m
->m_len
== ETHER_HDR_LEN
)
2226 // this is the case where there's an Ethernet header in an mbuf
2228 // the first mbuf is the Ethernet header -- just strip it off and do the checksum
2229 struct mbuf
*m_ip
= m
->m_next
;
2231 // set up m_ip so the cksum operations work
2232 /* APPLE MODIFICATION 22 Apr 2008 <mvega@apple.com>
2233 * <rdar://5817385> Clear the m_tag list before setting
2236 * If this m_buf chain was extended via M_PREPEND(), then
2237 * m_ip->m_pkthdr is identical to m->m_pkthdr (see
2238 * M_MOVE_PKTHDR()). The only thing preventing access to this
2239 * invalid packet header data is the fact that the M_PKTHDR
2240 * flag is clear, i.e., m_ip->m_flag & M_PKTHDR == 0, but we're
2241 * about to set the M_PKTHDR flag, so to be safe we initialize,
2242 * more accurately, we clear, m_ip->m_pkthdr.tags via
2245 * Suppose that we do not do this; if m_pullup(), below, fails,
2246 * then m_ip will be freed along with m_ip->m_pkthdr.tags, but
2247 * we will also free m soon after, via m_freem(), and
2248 * consequently attempt to free m->m_pkthdr.tags in the
2249 * process. The problem is that m->m_pkthdr.tags will have
2250 * already been freed by virtue of being equal to
2251 * m_ip->m_pkthdr.tags. Attempts to dereference
2252 * m->m_pkthdr.tags in m_tag_delete_chain() will result in a
2256 /* END MODIFICATION */
2257 m_ip
->m_flags
|= M_PKTHDR
;
2258 m_ip
->m_pkthdr
.csum_flags
= m
->m_pkthdr
.csum_flags
;
2259 m_ip
->m_pkthdr
.csum_data
= m
->m_pkthdr
.csum_data
;
2260 m_ip
->m_pkthdr
.len
= m
->m_pkthdr
.len
- ETHER_HDR_LEN
;
2262 // set up the header mbuf so we can prepend it back on again later
2263 m
->m_pkthdr
.csum_flags
= 0;
2264 m
->m_pkthdr
.csum_data
= 0;
2265 m
->m_pkthdr
.len
= ETHER_HDR_LEN
;
2269 // now do the checksums we need -- first IP
2270 if ( m_ip
->m_pkthdr
.csum_flags
& M_CSUM_IPv4
)
2272 // make sure the IP header (or at least the part with the cksum) is there
2273 m_ip
= m_pullup( m_ip
, sizeof( struct ip
) );
2276 printf( "bridge: failed to flatten header\n ");
2281 // now do the checksum
2283 struct ip
*ip
= mtod( m_ip
, struct ip
* );
2284 ip
->ip_sum
= in_cksum( m_ip
, hlen
);
2286 #ifdef VERY_VERY_VERY_DIAGNOSTIC
2287 printf( "bridge: performed IPv4 checksum\n" );
2292 // now do a TCP or UDP delayed checksum
2293 if ( m_ip
->m_pkthdr
.csum_flags
& (M_CSUM_TCPv4
|M_CSUM_UDPv4
) )
2295 in_delayed_cksum( m_ip
);
2297 #ifdef VERY_VERY_VERY_DIAGNOSTIC
2298 printf( "bridge: performed TCPv4/UDPv4 checksum\n" );
2302 // now attach the ethernet header back onto the IP packet
2304 m
->m_pkthdr
.len
+= m_length( m_ip
);
2306 // clear the M_PKTHDR flags on the ip packet (again, we re-attach later)
2307 m_ip
->m_flags
&= ~M_PKTHDR
;
2309 // and clear any csum flags
2310 m
->m_pkthdr
.csum_flags
&= ~(M_CSUM_TCPv4
|M_CSUM_UDPv4
|M_CSUM_IPv4
);
2312 else if ( m
->m_len
>= amt
)
2314 // everything fits in the first mbuf, so futz with m->m_data, m->m_len and m->m_pkthdr.len to
2316 m
->m_len
-= ETHER_HDR_LEN
;
2317 m
->m_data
+= ETHER_HDR_LEN
;
2318 m
->m_pkthdr
.len
-= ETHER_HDR_LEN
;
2320 // now do the checksums we need -- first IP
2321 if ( m
->m_pkthdr
.csum_flags
& M_CSUM_IPv4
)
2323 struct ip
*ip
= mtod( m
, struct ip
* );
2324 ip
->ip_sum
= in_cksum( m
, hlen
);
2326 #ifdef VERY_VERY_VERY_DIAGNOSTIC
2327 printf( "bridge: performed IPv4 checksum\n" );
2331 // now do a TCP or UDP delayed checksum
2332 if ( m
->m_pkthdr
.csum_flags
& (M_CSUM_TCPv4
|M_CSUM_UDPv4
) )
2334 in_delayed_cksum( m
);
2336 #ifdef VERY_VERY_VERY_DIAGNOSTIC
2337 printf( "bridge: performed TCPv4/UDPv4 checksum\n" );
2341 // now stick the ethernet header back on
2342 m
->m_len
+= ETHER_HDR_LEN
;
2343 m
->m_data
-= ETHER_HDR_LEN
;
2344 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
2346 // and clear any csum flags
2347 m
->m_pkthdr
.csum_flags
&= ~(M_CSUM_TCPv4
|M_CSUM_UDPv4
|M_CSUM_IPv4
);
2353 // general case -- need to simply split it off and deal
2355 // first, calculate how much needs to be made writable (we may have a read-only mbuf here)
2356 hlen
= M_CSUM_DATA_IPv4_IPHL( m
->m_pkthdr
.csum_data
);
2358 off
= M_CSUM_DATA_IPv4_OFFSET( m
->m_pkthdr
.csum_data
);
2360 if ( m
->m_pkthdr
.csum_flags
& M_CSUM_IPv4
)
2365 if ( m
->m_pkthdr
.csum_flags
& M_CSUM_TCPv4
)
2367 amt
+= sizeof( struct tcphdr
* );
2371 if ( m
->m_pkthdr
.csum_flags
& M_CSUM_UDPv4
)
2373 amt
+= sizeof( struct udphdr
* );
2378 // now split the ethernet header off of the IP packet (we'll re-attach later)
2379 m_ip
= m_split( m
, ETHER_HDR_LEN
, M_NOWAIT
);
2382 printf( "bridge_fix_txcsum: could not split ether header\n" );
2389 // make sure that the IP packet is writable for the portion we need
2390 if ( m_makewritable( &m_ip
, 0, amt
, M_DONTWAIT
) != 0 )
2392 printf( "bridge_fix_txcsum: could not make %d bytes writable\n", amt
);
2400 m_ip
->m_pkthdr
.csum_flags
= m
->m_pkthdr
.csum_flags
;
2401 m_ip
->m_pkthdr
.csum_data
= m
->m_pkthdr
.csum_data
;
2403 m
->m_pkthdr
.csum_flags
= 0;
2404 m
->m_pkthdr
.csum_data
= 0;
2406 // now do the checksums we need -- first IP
2407 if ( m_ip
->m_pkthdr
.csum_flags
& M_CSUM_IPv4
)
2409 // make sure the IP header (or at least the part with the cksum) is there
2410 m_ip
= m_pullup( m_ip
, sizeof( struct ip
) );
2413 printf( "bridge: failed to flatten header\n ");
2418 // now do the checksum
2420 struct ip
*ip
= mtod( m_ip
, struct ip
* );
2421 ip
->ip_sum
= in_cksum( m_ip
, hlen
);
2423 #ifdef VERY_VERY_VERY_DIAGNOSTIC
2424 printf( "bridge: performed IPv4 checksum\n" );
2429 // now do a TCP or UDP delayed checksum
2430 if ( m_ip
->m_pkthdr
.csum_flags
& (M_CSUM_TCPv4
|M_CSUM_UDPv4
) )
2432 in_delayed_cksum( m_ip
);
2434 #ifdef VERY_VERY_VERY_DIAGNOSTIC
2435 printf( "bridge: performed TCPv4/UDPv4 checksum\n" );
2439 // now attach the ethernet header back onto the IP packet
2441 m
->m_pkthdr
.len
+= m_length( m_ip
);
2443 // clear the M_PKTHDR flags on the ip packet (again, we re-attach later)
2444 m_ip
->m_flags
&= ~M_PKTHDR
;
2446 // and clear any csum flags
2447 m
->m_pkthdr
.csum_flags
&= ~(M_CSUM_TCPv4
|M_CSUM_UDPv4
|M_CSUM_IPv4
);
2457 * Start output on a bridge.
2460 bridge_start(ifnet_t ifp
, mbuf_t m
)
2462 struct bridge_softc
*sc
= ifnet_softc(ifp
);
2463 struct ether_header
*eh
;
2464 struct ifnet
*dst_if
;
2466 lck_mtx_assert(sc
->sc_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
2468 eh
= mtod(m
, struct ether_header
*);
2470 if ((m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0 &&
2471 (dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
)) != NULL
) {
2474 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
2476 * APPLE MODIFICATION - if the packet needs a checksum (i.e.,
2477 * checksum has been deferred for HW support) AND the destination
2478 * interface doesn't support HW checksums, then we
2479 * need to fix-up the checksum here
2482 ( (m
->m_pkthdr
.csum_flags
& (M_CSUM_TCPv4
|M_CSUM_UDPv4
|M_CSUM_IPv4
) ) != 0 ) &&
2483 ( (dst_if
->if_csum_flags_tx
& m
->m_pkthdr
.csum_flags
) != m
->m_pkthdr
.csum_flags
)
2486 m
= bridge_fix_txcsum( m
);
2494 if (eh
->ether_type
== htons(ETHERTYPE_IP
))
2495 mbuf_outbound_finalize(m
, PF_INET
, sizeof(struct ether_header
));
2497 m
->m_pkthdr
.csum_flags
= 0;
2499 lck_mtx_lock(sc
->sc_mtx
);
2501 if (sc
->sc_bpf_output
)
2502 bridge_bpf_output(ifp
, m
);
2504 bridge_enqueue(sc
, dst_if
, m
);
2505 lck_mtx_unlock(sc
->sc_mtx
);
2509 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
2512 * APPLE MODIFICATION - if the MULTICAST packet needs a checksum (i.e.,
2513 * checksum has been deferred for HW support) AND at least one destination
2514 * interface doesn't support HW checksums, then we go ahead and fix it up
2515 * here, since it doesn't make sense to do it more than once
2519 (m
->m_pkthdr
.csum_flags
& (M_CSUM_TCPv4
|M_CSUM_UDPv4
|M_CSUM_IPv4
)) &&
2521 * XXX FIX ME: keep track of whether or not we have any interfaces that
2522 * do not support checksums (for now, assume we do)
2527 m
= bridge_fix_txcsum( m
);
2534 if (eh
->ether_type
== htons(ETHERTYPE_IP
))
2535 mbuf_outbound_finalize(m
, PF_INET
, sizeof(struct ether_header
));
2537 m
->m_pkthdr
.csum_flags
= 0;
2540 lck_mtx_lock(sc
->sc_mtx
);
2542 if (sc
->sc_bpf_output
)
2543 bridge_bpf_output(ifp
, m
);
2545 bridge_broadcast(sc
, ifp
, m
, 0);
2546 lck_mtx_unlock(sc
->sc_mtx
);
2548 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
2558 * The forwarding function of the bridge.
2561 bridge_forward(struct bridge_softc
*sc
, struct mbuf
*m
)
2563 struct bridge_iflist
*bif
;
2564 struct ifnet
*src_if
, *dst_if
;
2565 struct ether_header
*eh
;
2567 lck_mtx_assert(sc
->sc_mtx
, LCK_MTX_ASSERT_OWNED
);
2570 if (_if_brige_debug
)
2571 printf("bridge_forward %s%d m%p\n", ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
), m
);
2572 #endif /* BRIDGE_DEBUG */
2574 src_if
= m
->m_pkthdr
.rcvif
;
2576 (void) ifnet_stat_increment_in(sc
->sc_if
, 1, m
->m_pkthdr
.len
, 0);
2579 * Look up the bridge_iflist.
2581 bif
= bridge_lookup_member_if(sc
, src_if
);
2583 /* Interface is not a bridge member (anymore?) */
2588 /* APPLE MODIFICATION <cbz@apple.com> - add the ability to block forwarding of packets; for the guest network */
2589 #if ( APPLE_HAVE_80211_GUEST_NETWORK )
2590 if (bif
->bif_flags
& IFBIF_NO_FORWARDING
) {
2591 /* Drop the packet and we're done. */
2597 if (bif
->bif_flags
& IFBIF_STP
) {
2598 switch (bif
->bif_state
) {
2599 case BSTP_IFSTATE_BLOCKING
:
2600 case BSTP_IFSTATE_LISTENING
:
2601 case BSTP_IFSTATE_DISABLED
:
2607 eh
= mtod(m
, struct ether_header
*);
2610 * If the interface is learning, and the source
2611 * address is valid and not multicast, record
2614 if ((bif
->bif_flags
& IFBIF_LEARNING
) != 0 &&
2615 ETHER_IS_MULTICAST(eh
->ether_shost
) == 0 &&
2616 (eh
->ether_shost
[0] | eh
->ether_shost
[1] |
2617 eh
->ether_shost
[2] | eh
->ether_shost
[3] |
2618 eh
->ether_shost
[4] | eh
->ether_shost
[5]) != 0) {
2619 (void) bridge_rtupdate(sc
, eh
->ether_shost
,
2620 src_if
, 0, IFBAF_DYNAMIC
);
2623 if ((bif
->bif_flags
& IFBIF_STP
) != 0 &&
2624 bif
->bif_state
== BSTP_IFSTATE_LEARNING
) {
2630 * At this point, the port either doesn't participate
2631 * in spanning tree or it is in the forwarding state.
2635 * If the packet is unicast, destined for someone on
2636 * "this" side of the bridge, drop it.
2638 if ((m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0) {
2639 /* APPLE MODIFICATION <cbz@apple.com> - if the packet came in on a proxy sta discovery interface,
2640 we need to not look up the node by DA of the packet; we need to look up the proxy sta which
2641 matches the SA. If it's not found yet, drop the packet. */
2642 #if IEEE80211_PROXYSTA
2643 if (bif
->bif_flags
& IFBIF_PROXYSTA_DISCOVER
)
2645 struct bridge_rtnode
*brt
;
2647 brt
= bridge_rtnode_lookup(sc
, eh
->ether_shost
);
2649 dst_if
= brt
->brt_ifp_proxysta
;
2651 if (dst_if
== NULL
) {
2658 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
);
2659 if (src_if
== dst_if
) {
2664 /* ...forward it to all interfaces. */
2665 sc
->sc_if
->if_imcasts
++;
2669 /* APPLE MODIFICATION
2670 <rnewberry@apple.com> - this is now handled by bridge_input
2671 <cbz@apple.com> - turning this back on because all packets are not bpf_mtap'd
2672 equally. RSN Preauth were not getting through; we're
2673 conditionalizing this call on
2674 (eh->ether_type == htons(ETHERTYPE_RSN_PREAUTH))
2677 if (eh
->ether_type
== htons(ETHERTYPE_RSN_PREAUTH
))
2679 m
->m_pkthdr
.rcvif
= sc
->sc_if
;
2681 if (sc
->sc_bpf_input
)
2682 bridge_bpf_input(sc
->sc_if
, m
);
2687 if (dst_if
== NULL
) {
2689 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
2691 * Clear any in-bound checksum flags for this packet.
2693 m
->m_pkthdr
.csum_flags
= 0;
2695 mbuf_inbound_modified(m
);
2698 bridge_broadcast(sc
, src_if
, m
, 1);
2703 * At this point, we're dealing with a unicast frame
2704 * going to a different interface.
2706 if ((ifnet_flags(dst_if
) & IFF_RUNNING
) == 0) {
2710 bif
= bridge_lookup_member_if(sc
, dst_if
);
2712 /* Not a member of the bridge (anymore?) */
2717 if (bif
->bif_flags
& IFBIF_STP
) {
2718 switch (bif
->bif_state
) {
2719 case BSTP_IFSTATE_DISABLED
:
2720 case BSTP_IFSTATE_BLOCKING
:
2726 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
2728 * Clear any in-bound checksum flags for this packet.
2731 m
->m_pkthdr
.csum_flags
= 0;
2734 mbuf_inbound_modified(m
);
2737 bridge_enqueue(sc
, dst_if
, m
);
2740 char * ether_ntop(char *, size_t , const u_char
*);
2742 __private_extern__
char *
2743 ether_ntop(char *buf
, size_t len
, const u_char
*ap
)
2745 snprintf(buf
, len
, "%02x:%02x:%02x:%02x:%02x:%02x",
2746 ap
[0], ap
[1], ap
[2], ap
[3], ap
[4], ap
[5]);
2754 * Receive input from a member interface. Queue the packet for
2755 * bridging if it is not for us.
2758 bridge_input(struct bridge_iflist
*bif
, struct ifnet
*ifp
, struct mbuf
*m
, void *frame_header
)
2761 struct ether_header
*eh
;
2764 struct bridge_softc
*sc
= bif
->bif_sc
;
2765 struct bridge_iflist
*brm
;
2768 if (_if_brige_debug
)
2769 printf("bridge_input: %s%d from %s%d m %p data %p\n",
2770 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
),
2771 ifnet_name(ifp
), ifnet_unit(ifp
),
2773 #endif /* BRIDGE_DEBUG */
2775 if ((ifnet_flags(sc
->sc_if
) & IFF_RUNNING
) == 0) {
2777 if (_if_brige_debug
)
2778 printf( "bridge_input: %s%d not running passing along\n",
2779 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
));
2780 #endif /* BRIDGE_DEBUG */
2784 /* Need to clear the promiscous flags otherwise it will be dropped by DLIL after processing filters */
2785 if ((mbuf_flags(m
) & MBUF_PROMISC
))
2786 mbuf_setflags_mask(m
, 0, MBUF_PROMISC
);
2788 lck_mtx_lock(sc
->sc_mtx
);
2792 /* Is it a good idea to reassign a new value to bif ? TBD */
2793 bif
= bridge_lookup_member_if(sc
, ifp
);
2795 lck_mtx_unlock(sc
->sc_mtx
);
2797 if (_if_brige_debug
)
2798 printf( "bridge_input: %s%d bridge_lookup_member_if failed\n",
2799 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
));
2800 #endif /* BRIDGE_DEBUG */
2804 eh
= (struct ether_header
*)mbuf_data(m
);
2807 * If the packet is for us, set the packets source as the
2808 * bridge, and return the packet back to ether_input for
2811 if (memcmp(eh
->ether_dhost
, ifnet_lladdr(bifp
),
2812 ETHER_ADDR_LEN
) == 0) {
2814 /* Mark the packet as arriving on the bridge interface */
2815 (void) mbuf_pkthdr_setrcvif(m
, bifp
);
2816 mbuf_pkthdr_setheader(m
, frame_header
);
2819 * If the interface is learning, and the source
2820 * address is valid and not multicast, record
2823 if ((bif
->bif_flags
& IFBIF_LEARNING
) != 0 &&
2824 ETHER_IS_MULTICAST(eh
->ether_shost
) == 0 &&
2825 (eh
->ether_shost
[0] | eh
->ether_shost
[1] |
2826 eh
->ether_shost
[2] | eh
->ether_shost
[3] |
2827 eh
->ether_shost
[4] | eh
->ether_shost
[5]) != 0) {
2828 (void) bridge_rtupdate(sc
, eh
->ether_shost
,
2829 ifp
, 0, IFBAF_DYNAMIC
);
2833 if (sc
->sc_bpf_input
)
2834 bridge_bpf_input(bifp
, m
);
2837 (void) mbuf_setdata(m
, (char *)mbuf_data(m
) + ETHER_HDR_LEN
, mbuf_len(m
) - ETHER_HDR_LEN
);
2838 (void) mbuf_pkthdr_adjustlen(m
, - ETHER_HDR_LEN
);
2840 (void) ifnet_stat_increment_in(bifp
, 1, mbuf_pkthdr_len(m
), 0);
2842 lck_mtx_unlock(sc
->sc_mtx
);
2845 if (_if_brige_debug
)
2846 printf( "bridge_input: %s%d packet for bridge\n",
2847 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
));
2848 #endif /* BRIDGE_DEBUG */
2850 dlil_input_packet_list(bifp
, m
);
2856 * if the destination of the packet is for the MAC address of
2857 * the member interface itself, then we don't need to forward
2858 * it -- just pass it back. Note that it'll likely just be
2859 * dropped by the stack, but if something else is bound to
2860 * the interface directly (for example, the wireless stats
2861 * protocol -- although that actually uses BPF right now),
2862 * then it will consume the packet
2864 * ALSO, note that we do this check AFTER checking for the
2865 * bridge's own MAC address, because the bridge may be
2866 * using the SAME MAC address as one of its interfaces
2868 if (memcmp(eh
->ether_dhost
, ifnet_lladdr(ifp
),
2869 ETHER_ADDR_LEN
) == 0) {
2870 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
2871 #if IEEE80211_PROXYSTA
2872 if ((bif
->bif_flags
& IFBIF_PROXYSTA
) == 0) {
2875 #ifdef VERY_VERY_VERY_DIAGNOSTIC
2876 printf("bridge_input: not forwarding packet bound for member interface\n" );
2878 lck_mtx_unlock(sc
->sc_mtx
);
2880 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
2881 #if IEEE80211_PROXYSTA
2883 #if VERY_VERY_VERY_DIAGNOSTIC
2885 printf( "%s: pkt rx on %s [proxysta iface], da is %02x:%02x:%02x:%02x:%02x:%02x\n",
2886 __func__
, ifp
->if_xname
, eh
->ether_dhost
[0], eh
->ether_dhost
[1], eh
->ether_dhost
[2],
2887 eh
->ether_dhost
[3], eh
->ether_dhost
[4], eh
->ether_dhost
[5] );
2893 if ((m
->m_flags
& (M_BCAST
|M_MCAST
))) {
2894 struct ifmultiaddr
*ifma
= NULL
;
2896 if ((m
->m_flags
& M_BCAST
)) {
2900 printf("mulicast: %02x:%02x:%02x:%02x:%02x:%02x\n",
2901 eh
->ether_dhost
[0], eh
->ether_dhost
[1], eh
->ether_dhost
[2],
2902 eh
->ether_dhost
[3], eh
->ether_dhost
[4], eh
->ether_dhost
[5]);
2904 for (ifma
= bifp
->if_multiaddrs
.lh_first
; ifma
;
2905 ifma
= ifma
->ifma_link
.le_next
) {
2907 if (ifma
->ifma_addr
== NULL
)
2909 else if (ifma
->ifma_addr
->sa_family
== AF_INET
) {
2910 struct sockaddr_in
*sin
= (struct sockaddr_in
*)ifma
->ifma_addr
;
2912 printf(" %u.%u.%u.%u ",
2913 (sin
->sin_addr
.s_addr
& 0xff000000) >> 24,
2914 (sin
->sin_addr
.s_addr
& 0x00ff0000) >> 16,
2915 (sin
->sin_addr
.s_addr
& 0x0000ff00) >> 8,
2916 (sin
->sin_addr
.s_addr
& 0x000000ff));
2918 if (!ifma
->ifma_ll
|| !ifma
->ifma_ll
->ifma_addr
)
2921 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)ifma
->ifma_ll
->ifma_addr
;
2923 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
2924 CONST_LLADDR(sdl
)[0], CONST_LLADDR(sdl
)[1], CONST_LLADDR(sdl
)[2],
2925 CONST_LLADDR(sdl
)[3], CONST_LLADDR(sdl
)[4], CONST_LLADDR(sdl
)[5]);
2929 #endif /* BRIDGE_DEBUG */
2932 * the upper layer of the stack have attached a list of multicast addresses to the bridge itself
2933 * (for example, the IP stack has bound 01:00:5e:00:00:01 to the 224.0.0.1 all hosts address), since
2934 * the IP stack is bound to the bridge. so we need to see if the packets arriving here SHOULD be
2935 * passed up as coming from the bridge.
2937 * furthermore, since we know the IP stack is attached to the bridge, and NOTHING is attached
2938 * to the underlying devices themselves, we can drop packets that don't need to go up (by returning NULL
2939 * from bridge_input to the caller) after we forward the packet to other interfaces
2942 for (ifma
= bifp
->if_multiaddrs
.lh_first
; ifma
;
2943 ifma
= ifma
->ifma_link
.le_next
) {
2944 if (ifma
->ifma_ll
&& ifma
->ifma_ll
->ifma_addr
) {
2945 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)ifma
->ifma_ll
->ifma_addr
;
2947 if (memcmp(eh
->ether_dhost
, CONST_LLADDR(sdl
), ETHER_ADDR_LEN
) == 0)
2952 /* this packet matches the bridge's own filter, so pass it up as coming from us */
2954 /* Mark the packet as arriving on the bridge interface */
2955 // don't do this until AFTER we forward the packet -- bridge_forward uses this information
2956 //m->m_pkthdr.rcvif = bifp;
2958 /* keep track of this to help us decide about forwarding */
2962 char addr
[sizeof("XX:XX:XX:XX:XX:XX")+1];
2963 printf( "bridge_input: multicast frame for us (%s)\n",
2964 ether_ntop(addr
, sizeof(addr
), eh
->ether_dhost
) );
2968 char addr
[sizeof("XX:XX:XX:XX:XX:XX")+1];
2969 printf( "bridge_input: multicast frame for unbound address (%s), forwarding but not passing to stack\n",
2970 ether_ntop(addr
, sizeof(addr
), eh
->ether_dhost
) );
2974 /* Tap off 802.1D packets; they do not get forwarded. */
2975 if (memcmp(eh
->ether_dhost
, bstp_etheraddr
,
2976 ETHER_ADDR_LEN
) == 0) {
2977 m
= bstp_input(sc
, ifp
, m
);
2979 lck_mtx_unlock(sc
->sc_mtx
);
2981 if (_if_brige_debug
)
2982 printf( "bridge_input: %s%d mcast BSTP not forwarded\n",
2983 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
));
2984 #endif /* BRIDGE_DEBUG */
2989 if (bif
->bif_flags
& IFBIF_STP
) {
2990 switch (bif
->bif_state
) {
2991 case BSTP_IFSTATE_BLOCKING
:
2992 case BSTP_IFSTATE_LISTENING
:
2993 case BSTP_IFSTATE_DISABLED
:
2995 lck_mtx_unlock(sc
->sc_mtx
);
2998 if (_if_brige_debug
)
2999 printf( "bridge_input: %s%d mcast bridge not learning or forwarding \n",
3000 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
));
3001 #endif /* BRIDGE_DEBUG */
3010 * If the interface is learning, and the source
3011 * address is valid and not multicast, record
3014 if ((bif
->bif_flags
& IFBIF_LEARNING
) != 0 &&
3015 ETHER_IS_MULTICAST(eh
->ether_shost
) == 0 &&
3016 (eh
->ether_shost
[0] | eh
->ether_shost
[1] |
3017 eh
->ether_shost
[2] | eh
->ether_shost
[3] |
3018 eh
->ether_shost
[4] | eh
->ether_shost
[5]) != 0) {
3019 (void) bridge_rtupdate(sc
, eh
->ether_shost
,
3020 ifp
, 0, IFBAF_DYNAMIC
);
3025 * Make a deep copy of the packet and enqueue the copy
3026 * for bridge processing; return the original packet for
3029 mc
= m_dup(m
, M_NOWAIT
);
3032 printf( "bridge_input: failed to duplicate multicast frame, not forwarding\n" );
3036 if (_if_brige_debug
) {
3037 printf_mbuf(mc
, "mc for us: ", "\n");
3038 printf_mbuf_data(m
, 0, 20);
3041 #endif /* BRIDGE_DEBUG */
3045 * we'll just pass the original, since we don't need to pass it
3051 /* Perform the bridge forwarding function with the copy. */
3054 if (_if_brige_debug
)
3055 printf( "bridge_input: %s%d mcast forwarding \n",
3056 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
));
3057 #endif /* BRIDGE_DEBUG */
3058 bridge_forward(sc
, mc
);
3061 // TBD should have an option for type of bridge
3064 * Reinject the mbuf as arriving on the bridge so we have a
3065 * chance at claiming multicast packets. We can not loop back
3066 * here from ether_input as a bridge is never a member of a
3069 if (bifp
->if_bridge
!= NULL
)
3070 panic("brige_input: brige %p in a bridge %p\n", bifp
, bifp
->if_bridge
);
3071 mc
= m_dup(m
, M_NOWAIT
);
3073 mc
->m_pkthdr
.rcvif
= bifp
;
3075 if (sc
->sc_bpf_input
)
3076 bridge_bpf_input(bifp
, mc
);
3080 /* Return the original packet for local processing. */
3083 /* we don't free the packet -- bridge_forward already did so */
3084 lck_mtx_unlock(sc
->sc_mtx
);
3087 if (_if_brige_debug
)
3088 printf( "bridge_input: %s%d mcast local processing\n",
3089 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
));
3095 // mark packet as arriving on the bridge
3096 m
->m_pkthdr
.rcvif
= bifp
;
3097 m
->m_pkthdr
.header
= mbuf_data(m
);
3100 if (sc
->sc_bpf_input
)
3101 bridge_bpf_input(bifp
, m
);
3103 (void) mbuf_setdata(m
, (char *)mbuf_data(m
) + ETHER_HDR_LEN
, mbuf_len(m
) - ETHER_HDR_LEN
);
3104 (void) mbuf_pkthdr_adjustlen(m
, - ETHER_HDR_LEN
);
3106 (void) ifnet_stat_increment_in(bifp
, 1, mbuf_pkthdr_len(m
), 0);
3108 lck_mtx_unlock(sc
->sc_mtx
);
3111 if (_if_brige_debug
)
3112 printf( "bridge_input: %s%d mcast for us\n",
3113 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
));
3114 #endif /* BRIDGE_DEBUG */
3116 dlil_input_packet_list(bifp
, m
);
3121 if (bif
->bif_flags
& IFBIF_STP
) {
3122 switch (bif
->bif_state
) {
3123 case BSTP_IFSTATE_BLOCKING
:
3124 case BSTP_IFSTATE_LISTENING
:
3125 case BSTP_IFSTATE_DISABLED
:
3126 lck_mtx_unlock(sc
->sc_mtx
);
3129 if (_if_brige_debug
)
3130 printf( "bridge_input: %s%d ucast bridge not learning or forwarding \n",
3131 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
));
3132 #endif /* BRIDGE_DEBUG */
3139 /* this code is not needed for Apple's bridge where the stack attaches directly */
3140 #if 1 /* TBD should be an option */
3142 * Unicast. Make sure it's not for us.
3144 LIST_FOREACH(brm
, &sc
->sc_iflist
, bif_next
) {
3145 if(ifnet_type(brm
->bif_ifp
) != IFT_ETHER
)
3148 /* It is destined for us. */
3149 if (memcmp(ifnet_lladdr(brm
->bif_ifp
), eh
->ether_dhost
,
3150 ETHER_ADDR_LEN
) == 0) {
3151 if (brm
->bif_flags
& IFBIF_LEARNING
)
3152 (void) bridge_rtupdate(sc
,
3153 eh
->ether_shost
, ifp
, 0, IFBAF_DYNAMIC
);
3154 m
->m_pkthdr
.rcvif
= brm
->bif_ifp
;
3155 m
->m_pkthdr
.header
= mbuf_data(m
);
3157 (void) mbuf_setdata(m
, (char *)mbuf_data(m
) + ETHER_HDR_LEN
, mbuf_len(m
) - ETHER_HDR_LEN
);
3158 (void) mbuf_pkthdr_adjustlen(m
, - ETHER_HDR_LEN
);
3159 #if BRIDGE_SUPPORT_GIF
3161 if (ifnet_type(ifp
) == IFT_GIF
) {
3162 m
->m_flags
|= M_PROTO1
;
3163 m
->m_pkthdr
.rcvif
= brm
->bif_ifp
;
3164 (*brm
->bif_ifp
->if_input
)(brm
->bif_ifp
, m
);
3169 lck_mtx_unlock(sc
->sc_mtx
);
3172 if (_if_brige_debug
)
3173 printf( "bridge_input: %s%d ucast to member %s%d\n",
3174 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
),
3175 ifnet_name(brm
->bif_ifp
), ifnet_unit(brm
->bif_ifp
));
3176 #endif /* BRIDGE_DEBUG */
3178 dlil_input_packet_list(brm
->bif_ifp
, m
);
3183 /* We just received a packet that we sent out. */
3184 if (memcmp(ifnet_lladdr(brm
->bif_ifp
), eh
->ether_shost
,
3185 ETHER_ADDR_LEN
) == 0) {
3186 lck_mtx_unlock(sc
->sc_mtx
);
3189 if (_if_brige_debug
)
3190 printf( "bridge_input: %s%d ucast drop packet we sent out\n",
3191 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
));
3192 #endif /* BRIDGE_DEBUG */
3201 * If the interface is learning, and the source
3202 * address is valid and not multicast, record
3205 if ((bif
->bif_flags
& IFBIF_LEARNING
) != 0 &&
3206 ETHER_IS_MULTICAST(eh
->ether_shost
) == 0 &&
3207 (eh
->ether_shost
[0] | eh
->ether_shost
[1] |
3208 eh
->ether_shost
[2] | eh
->ether_shost
[3] |
3209 eh
->ether_shost
[4] | eh
->ether_shost
[5]) != 0) {
3210 (void) bridge_rtupdate(sc
, eh
->ether_shost
,
3211 ifp
, 0, IFBAF_DYNAMIC
);
3214 /* Perform the bridge forwarding function. */
3216 if (_if_brige_debug
)
3217 printf( "bridge_input: %s%d ucast forwarding\n",
3218 ifnet_name(sc
->sc_if
), ifnet_unit(sc
->sc_if
));
3219 #endif /* BRIDGE_DEBUG */
3221 bridge_forward(sc
, m
);
3222 lck_mtx_unlock(sc
->sc_mtx
);
3229 * Send a frame to all interfaces that are members of
3230 * the bridge, except for the one on which the packet
3234 bridge_broadcast(struct bridge_softc
*sc
, struct ifnet
*src_if
,
3235 struct mbuf
*m
, __unused
int runfilt
)
3237 struct bridge_iflist
*bif
;
3239 struct ifnet
*dst_if
;
3242 lck_mtx_assert(sc
->sc_mtx
, LCK_MTX_ASSERT_OWNED
);
3244 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
3245 dst_if
= bif
->bif_ifp
;
3246 if (dst_if
== src_if
)
3249 if (bif
->bif_flags
& IFBIF_STP
) {
3250 switch (bif
->bif_state
) {
3251 case BSTP_IFSTATE_BLOCKING
:
3252 case BSTP_IFSTATE_DISABLED
:
3257 if ((bif
->bif_flags
& IFBIF_DISCOVER
) == 0 &&
3258 (m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0)
3261 if ((ifnet_flags(dst_if
) & IFF_RUNNING
) == 0)
3264 if (LIST_NEXT(bif
, bif_next
) == NULL
) {
3268 mc
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
);
3270 (void) ifnet_stat_increment_out(sc
->sc_if
, 0, 0, 1);
3275 bridge_enqueue(sc
, dst_if
, mc
);
3284 * Add a bridge routing entry.
3287 bridge_rtupdate(struct bridge_softc
*sc
, const uint8_t *dst
,
3288 struct ifnet
*dst_if
, int setflags
, uint8_t flags
)
3290 struct bridge_rtnode
*brt
;
3292 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
3293 #if IEEE80211_PROXYSTA
3294 struct bridge_iflist
*bif
;
3295 int is_pds
; /* are we a proxy sta discovery interface? */
3297 struct timespec now
;
3299 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA - is this an interface
3300 we want to do proxy sta discovery on? */
3301 #if IEEE80211_PROXYSTA
3302 bif
= bridge_lookup_member_if(sc
, dst_if
);
3303 if ((bif
) && (bif
->bif_flags
& IFBIF_PROXYSTA_DISCOVER
)) {
3311 * A route for this destination might already exist. If so,
3312 * update it, otherwise create a new one.
3314 if ((brt
= bridge_rtnode_lookup(sc
, dst
)) == NULL
) {
3315 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
3316 #if IEEE80211_PROXYSTA
3317 /* don't count this address against the bridge cache (well, allow proxy stas to double that
3318 number...put *some* boundary on it.) if we are a proxy sta discovery interface */
3320 if (sc
->sc_brtcnt
>= (sc
->sc_brtmax
+sc
->sc_brtmax_proxysta
))
3325 if (sc
->sc_brtcnt
>= sc
->sc_brtmax
)
3329 * Allocate a new bridge forwarding node, and
3330 * initialize the expiration time and Ethernet
3333 brt
= zalloc_noblock(bridge_rtnode_pool
);
3337 memset(brt
, 0, sizeof(*brt
));
3339 brt
->brt_expire
= now
.tv_sec
+ sc
->sc_brttimeout
;
3340 brt
->brt_flags
= IFBAF_DYNAMIC
;
3341 memcpy(brt
->brt_addr
, dst
, ETHER_ADDR_LEN
);
3343 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA - is this an interface
3344 we want to do proxy sta discovery on? If so, post a monitoring event */
3345 #if IEEE80211_PROXYSTA
3347 brt
->brt_flags_ext
|= IFBAF_EXT_PROXYSTA
;
3349 printf( "%s: proxysta %02x:%02x:%02x:%02x:%02x:%02x on %s; discovery\n",
3350 __func__
, dst
[0], dst
[1], dst
[2], dst
[3], dst
[4], dst
[5], dst_if
->if_xname
);
3352 bridge_proxysta_discover( dst_if
, dst
);
3356 if ((error
= bridge_rtnode_insert(sc
, brt
)) != 0) {
3357 zfree(bridge_rtnode_pool
, brt
);
3362 brt
->brt_ifp
= dst_if
;
3364 brt
->brt_flags
= flags
;
3365 brt
->brt_expire
= (flags
& IFBAF_STATIC
) ? 0 :
3366 now
.tv_sec
+ sc
->sc_brttimeout
;
3369 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA - */
3370 #if IEEE80211_PROXYSTA
3372 #if VERY_VERY_DIAGNOSTIC
3373 printf( "%s: proxysta %02x:%02x:%02x:%02x:%02x:%02x on %s; reset timeout\n",
3374 __func__
, dst
[0], dst
[1], dst
[2], dst
[3], dst
[4], dst
[5], dst_if
->if_xname
);
3376 brt
->brt_expire
= (flags
& IFBAF_STATIC
) ? 0 :
3377 now
.tv_sec
+ sc
->sc_brttimeout
;
3387 * Lookup the destination interface for an address.
3389 static struct ifnet
*
3390 bridge_rtlookup(struct bridge_softc
*sc
, const uint8_t *addr
)
3392 struct bridge_rtnode
*brt
;
3394 if ((brt
= bridge_rtnode_lookup(sc
, addr
)) == NULL
)
3397 return (brt
->brt_ifp
);
3403 * Trim the routine table so that we have a number
3404 * of routing entries less than or equal to the
3408 bridge_rttrim(struct bridge_softc
*sc
)
3410 struct bridge_rtnode
*brt
, *nbrt
;
3412 /* Make sure we actually need to do this. */
3413 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
3416 /* Force an aging cycle; this might trim enough addresses. */
3418 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
3421 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
3422 nbrt
= LIST_NEXT(brt
, brt_list
);
3423 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
3424 bridge_rtnode_destroy(sc
, brt
);
3425 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
3434 * Aging timer for the bridge.
3437 bridge_timer(void *arg
)
3439 struct bridge_softc
*sc
= arg
;
3442 lck_mtx_lock(sc
->sc_mtx
);
3446 lck_mtx_unlock(sc
->sc_mtx
);
3448 if (ifnet_flags(sc
->sc_if
) & IFF_RUNNING
) {
3449 ts
.tv_sec
= bridge_rtable_prune_period
;
3451 bsd_timeout(bridge_timer
, sc
, &ts
);
3458 * Perform an aging cycle.
3461 bridge_rtage(struct bridge_softc
*sc
)
3463 struct bridge_rtnode
*brt
, *nbrt
;
3464 struct timespec now
;
3468 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
3469 nbrt
= LIST_NEXT(brt
, brt_list
);
3470 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
3471 if ((unsigned long)now
.tv_sec
>= brt
->brt_expire
)
3472 bridge_rtnode_destroy(sc
, brt
);
3480 * Remove all dynamic addresses from the bridge.
3483 bridge_rtflush(struct bridge_softc
*sc
, int full
)
3485 struct bridge_rtnode
*brt
, *nbrt
;
3487 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
3488 nbrt
= LIST_NEXT(brt
, brt_list
);
3489 if (full
|| (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
3490 bridge_rtnode_destroy(sc
, brt
);
3494 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
3495 #if IEEE80211_PROXYSTA
3497 * bridge_rtdiscovery:
3501 bridge_rtdiscovery(struct bridge_softc
*sc
)
3503 struct bridge_rtnode
*brt
, *nbrt
;
3504 struct bridge_iflist
*bif
;
3506 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
3507 nbrt
= LIST_NEXT(brt
, brt_list
);
3508 bif
= bridge_lookup_member_if(sc
, brt
->brt_ifp
);
3509 if ((bif
) && (bif
->bif_flags
& IFBIF_PROXYSTA_DISCOVER
) &&
3510 ((brt
->brt_flags_ext
& IFBAF_EXT_PROXYSTA
) == 0)) {
3512 printf( "%s: proxysta %02x:%02x:%02x:%02x:%02x:%02x on %s; found before IFBIF_PROXYSTA_DISCOVER\n",
3513 __func__
, brt
->brt_addr
[0], brt
->brt_addr
[1], brt
->brt_addr
[2], brt
->brt_addr
[3],
3514 brt
->brt_addr
[4], brt
->brt_addr
[5], brt
->brt_ifp
->if_xname
);
3516 brt
->brt_flags_ext
|= IFBAF_EXT_PROXYSTA
;
3519 if (brt
->brt_ifp_proxysta
== NULL
) {
3521 printf( "%s: proxysta %02x:%02x:%02x:%02x:%02x:%02x on %s; discovery\n",
3522 __func__
, brt
->brt_addr
[0], brt
->brt_addr
[1], brt
->brt_addr
[2], brt
->brt_addr
[3],
3523 brt
->brt_addr
[4], brt
->brt_addr
[5], brt
->brt_ifp
->if_xname
);
3525 bridge_proxysta_discover( brt
->brt_ifp
, brt
->brt_addr
);
3533 * Remove all dynamic addresses from a specific interface on the bridge.
3536 bridge_rtpurge(struct bridge_softc
*sc
, struct ifnet
*ifs
)
3538 struct bridge_rtnode
*brt
, *nbrt
;
3540 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
3541 nbrt
= LIST_NEXT(brt
, brt_list
);
3542 if (brt
->brt_ifp
== ifs
) {
3544 printf( "%s: purge %s [%02x:%02x:%02x:%02x:%02x:%02x] discovered on %s\n",
3545 __func__
, brt
->brt_ifp_proxysta
? brt
->brt_ifp_proxysta
->if_xname
: brt
->brt_ifp
->if_xname
,
3546 brt
->brt_addr
[0], brt
->brt_addr
[1], brt
->brt_addr
[2],
3547 brt
->brt_addr
[3], brt
->brt_addr
[4], brt
->brt_addr
[5], brt
->brt_ifp
->if_xname
);
3549 bridge_rtnode_destroy(sc
, brt
);
3558 * Remove an address from the table.
3561 bridge_rtdaddr(struct bridge_softc
*sc
, const uint8_t *addr
)
3563 struct bridge_rtnode
*brt
;
3565 if ((brt
= bridge_rtnode_lookup(sc
, addr
)) == NULL
)
3568 bridge_rtnode_destroy(sc
, brt
);
3575 * Delete routes to a speicifc member interface.
3577 __private_extern__
void
3578 bridge_rtdelete(struct bridge_softc
*sc
, struct ifnet
*ifp
, int full
)
3580 struct bridge_rtnode
*brt
, *nbrt
;
3582 for (brt
= LIST_FIRST(&sc
->sc_rtlist
); brt
!= NULL
; brt
= nbrt
) {
3583 nbrt
= LIST_NEXT(brt
, brt_list
);
3584 if (brt
->brt_ifp
== ifp
&& (full
||
3585 (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
))
3586 bridge_rtnode_destroy(sc
, brt
);
3591 * bridge_rtable_init:
3593 * Initialize the route table for this bridge.
3596 bridge_rtable_init(struct bridge_softc
*sc
)
3600 sc
->sc_rthash
= _MALLOC(sizeof(*sc
->sc_rthash
) * BRIDGE_RTHASH_SIZE
,
3601 M_DEVBUF
, M_WAITOK
);
3602 if (sc
->sc_rthash
== NULL
)
3605 for (i
= 0; i
< BRIDGE_RTHASH_SIZE
; i
++)
3606 LIST_INIT(&sc
->sc_rthash
[i
]);
3608 sc
->sc_rthash_key
= random();
3610 LIST_INIT(&sc
->sc_rtlist
);
3616 * bridge_rtable_fini:
3618 * Deconstruct the route table for this bridge.
3621 bridge_rtable_fini(struct bridge_softc
*sc
)
3624 _FREE(sc
->sc_rthash
, M_DEVBUF
);
3628 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
3629 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
3631 #define mix(a, b, c) \
3633 a -= b; a -= c; a ^= (c >> 13); \
3634 b -= c; b -= a; b ^= (a << 8); \
3635 c -= a; c -= b; c ^= (b >> 13); \
3636 a -= b; a -= c; a ^= (c >> 12); \
3637 b -= c; b -= a; b ^= (a << 16); \
3638 c -= a; c -= b; c ^= (b >> 5); \
3639 a -= b; a -= c; a ^= (c >> 3); \
3640 b -= c; b -= a; b ^= (a << 10); \
3641 c -= a; c -= b; c ^= (b >> 15); \
3642 } while (/*CONSTCOND*/0)
3645 bridge_rthash(__unused
struct bridge_softc
*sc
, const uint8_t *addr
)
3647 /* APPLE MODIFICATION - wasabi performance improvment - simplify the hash algorithm */
3649 uint32_t a
= 0x9e3779b9, b
= 0x9e3779b9, c
= sc
->sc_rthash_key
;
3660 return (c
& BRIDGE_RTHASH_MASK
);
3669 * bridge_rtnode_lookup:
3671 * Look up a bridge route node for the specified destination.
3673 static struct bridge_rtnode
*
3674 bridge_rtnode_lookup(struct bridge_softc
*sc
, const uint8_t *addr
)
3676 struct bridge_rtnode
*brt
;
3680 hash
= bridge_rthash(sc
, addr
);
3681 LIST_FOREACH(brt
, &sc
->sc_rthash
[hash
], brt_hash
) {
3682 dir
= memcmp(addr
, brt
->brt_addr
, ETHER_ADDR_LEN
);
3693 * bridge_rtnode_insert:
3695 * Insert the specified bridge node into the route table. We
3696 * assume the entry is not already in the table.
3699 bridge_rtnode_insert(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
3701 struct bridge_rtnode
*lbrt
;
3705 hash
= bridge_rthash(sc
, brt
->brt_addr
);
3707 lbrt
= LIST_FIRST(&sc
->sc_rthash
[hash
]);
3709 LIST_INSERT_HEAD(&sc
->sc_rthash
[hash
], brt
, brt_hash
);
3714 dir
= memcmp(brt
->brt_addr
, lbrt
->brt_addr
, ETHER_ADDR_LEN
);
3718 LIST_INSERT_BEFORE(lbrt
, brt
, brt_hash
);
3721 if (LIST_NEXT(lbrt
, brt_hash
) == NULL
) {
3722 LIST_INSERT_AFTER(lbrt
, brt
, brt_hash
);
3725 lbrt
= LIST_NEXT(lbrt
, brt_hash
);
3726 } while (lbrt
!= NULL
);
3729 panic("bridge_rtnode_insert: impossible");
3733 LIST_INSERT_HEAD(&sc
->sc_rtlist
, brt
, brt_list
);
3740 * bridge_rtnode_destroy:
3742 * Destroy a bridge rtnode.
3745 bridge_rtnode_destroy(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
3747 lck_mtx_assert(sc
->sc_mtx
, LCK_MTX_ASSERT_OWNED
);
3749 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
3750 #if IEEE80211_PROXYSTA
3751 if (brt
->brt_flags_ext
& IFBAF_EXT_PROXYSTA
) {
3753 printf( "%s: proxysta %02x:%02x:%02x:%02x:%02x:%02x %s from %s; idle timeout\n",
3754 __func__
, brt
->brt_addr
[0], brt
->brt_addr
[1], brt
->brt_addr
[2],
3755 brt
->brt_addr
[3], brt
->brt_addr
[4], brt
->brt_addr
[5],
3756 brt
->brt_ifp_proxysta
? brt
->brt_ifp_proxysta
->if_xname
: "unknown",
3757 brt
->brt_ifp
->if_xname
);
3759 bridge_proxysta_idle_timeout( brt
->brt_ifp
, brt
->brt_addr
);
3763 LIST_REMOVE(brt
, brt_hash
);
3765 LIST_REMOVE(brt
, brt_list
);
3767 zfree(bridge_rtnode_pool
, brt
);
3771 bridge_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
, bpf_packet_func bpf_callback
)
3773 struct bridge_softc
*sc
= (struct bridge_softc
*)ifnet_softc(ifp
);
3775 //printf("bridge_set_bpf_tap ifp %p mode %d\n", ifp, mode);
3778 if (sc
== NULL
|| (sc
->sc_flags
& SCF_DETACHING
)) {
3783 case BPF_TAP_DISABLE
:
3784 sc
->sc_bpf_input
= sc
->sc_bpf_output
= NULL
;
3788 sc
->sc_bpf_input
= bpf_callback
;
3791 case BPF_TAP_OUTPUT
:
3792 sc
->sc_bpf_output
= bpf_callback
;
3795 case BPF_TAP_INPUT_OUTPUT
:
3796 sc
->sc_bpf_input
= sc
->sc_bpf_output
= bpf_callback
;
3807 bridge_detach(__unused ifnet_t ifp
)
3809 struct bridge_softc
*sc
= (struct bridge_softc
*)ifnet_softc(ifp
);
3811 /* Tear down the routing table. */
3812 bridge_rtable_fini(sc
);
3814 lck_rw_lock_exclusive(bridge_list_lock
);
3815 LIST_REMOVE(sc
, sc_list
);
3816 lck_rw_done(bridge_list_lock
);
3820 lck_mtx_free(sc
->sc_mtx
, bridge_lock_grp
);
3822 _FREE(sc
, M_DEVBUF
);
3826 __private_extern__ errno_t
bridge_bpf_input(ifnet_t ifp
, struct mbuf
*m
)
3828 struct bridge_softc
*sc
= (struct bridge_softc
*)ifnet_softc(ifp
);
3830 if (sc
->sc_bpf_input
) {
3831 if (mbuf_pkthdr_rcvif(m
) != ifp
)
3832 printf("bridge_bpf_input rcvif: %p != ifp %p\n", mbuf_pkthdr_rcvif(m
), ifp
);
3833 (*sc
->sc_bpf_input
)(ifp
, m
);
3838 __private_extern__ errno_t
bridge_bpf_output(ifnet_t ifp
, struct mbuf
*m
)
3840 struct bridge_softc
*sc
= (struct bridge_softc
*)ifnet_softc(ifp
);
3842 if (sc
->sc_bpf_output
) {
3843 (*sc
->sc_bpf_output
)(ifp
, m
);