]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_bridge.c
xnu-1504.3.12.tar.gz
[apple/xnu.git] / bsd / net / if_bridge.c
1 /*
2 * Copyright (c) 2004-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
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 $ */
31
32 /*
33 * Copyright 2001 Wasabi Systems, Inc.
34 * All rights reserved.
35 *
36 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
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
52 * written permission.
53 *
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.
65 */
66
67 /*
68 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
69 * All rights reserved.
70 *
71 * Redistribution and use in source and binary forms, with or without
72 * modification, are permitted provided that the following conditions
73 * are met:
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.
84 *
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.
96 *
97 * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp
98 */
99
100 /*
101 * Network interface bridge support.
102 *
103 * TODO:
104 *
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).
109 */
110
111 #include <sys/cdefs.h>
112 //_KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.46 2006/11/23 04:07:07 rpaulo Exp $");
113
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
120
121 #define BRIDGE_DEBUG 0
122
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>
137
138 #include <libkern/libkern.h>
139
140 #include <kern/zalloc.h>
141
142 #if NBPFILTER > 0
143 #include <net/bpf.h>
144 #endif
145 #include <net/if.h>
146 #include <net/if_dl.h>
147 #include <net/if_types.h>
148 #include <net/if_llc.h>
149
150 #include <net/if_ether.h>
151 #include <net/if_bridgevar.h>
152 #include <net/dlil.h>
153
154 #include <net/kpi_interfacefilter.h>
155
156 #include <netinet/in.h>
157 #include <netinet/in_systm.h>
158 #include <netinet/ip.h>
159 #include <netinet/ip_var.h>
160 #ifdef INET6
161 #include <netinet/ip6.h>
162 #include <netinet6/in6_var.h>
163 #include <netinet6/ip6_var.h>
164 #endif
165
166 #if BRIDGE_DEBUG
167 #define static __private_extern__
168 #endif
169
170 extern void dlil_input_packet_list(struct ifnet *, struct mbuf *);
171
172 /*
173 * Size of the route hash table. Must be a power of two.
174 */
175 /* APPLE MODIFICATION - per Wasabi performance improvement, change the hash table size */
176 #if 0
177 #ifndef BRIDGE_RTHASH_SIZE
178 #define BRIDGE_RTHASH_SIZE 1024
179 #endif
180 #else
181 #ifndef BRIDGE_RTHASH_SIZE
182 #define BRIDGE_RTHASH_SIZE 256
183 #endif
184 #endif
185
186 /* APPLE MODIFICATION - support for HW checksums */
187 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
188 #include <netinet/udp.h>
189 #include <netinet/tcp.h>
190 #endif
191
192 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
193
194 //#include "carp.h"
195 #if NCARP > 0
196 #include <netinet/in.h>
197 #include <netinet/in_var.h>
198 #include <netinet/ip_carp.h>
199 #endif
200
201 /*
202 * Maximum number of addresses to cache.
203 */
204 #ifndef BRIDGE_RTABLE_MAX
205 #define BRIDGE_RTABLE_MAX 100
206 #endif
207
208 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
209 #if IEEE80211_PROXYSTA
210 /*
211 * Maximum (additional to maxcache) number of proxysta addresses to cache.
212 */
213 #ifndef BRIDGE_RTABLE_MAX_PROXYSTA
214 #define BRIDGE_RTABLE_MAX_PROXYSTA 16
215 #endif
216 #endif
217
218 /*
219 * Spanning tree defaults.
220 */
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
228
229 /*
230 * Timeout (in seconds) for entries learned dynamically.
231 */
232 #ifndef BRIDGE_RTABLE_TIMEOUT
233 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
234 #endif
235
236 /*
237 * Number of seconds between walks of the route list.
238 */
239 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
240 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
241 #endif
242
243 /*
244 * List of capabilities to mask on the member interface.
245 */
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 | \
251 IFCAP_CSUM_UDPv6_Tx)
252
253
254 int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
255
256 static zone_t bridge_rtnode_pool = NULL;
257
258 static errno_t
259 bridge_iff_input(void* cookie, ifnet_t ifp, __unused protocol_family_t protocol,
260 mbuf_t *data, char **frame_ptr);
261 static void
262 bridge_iff_event(void* cookie, ifnet_t ifp, __unused protocol_family_t protocol,
263 const struct kev_msg *event_msg);
264 static void
265 bridge_iff_detached(void* cookie, __unused ifnet_t interface);
266
267 static uint32_t
268 bridge_rthash(__unused struct bridge_softc *sc, const uint8_t *addr);
269
270 static int bridge_clone_create(struct if_clone *, int);
271 static void bridge_clone_destroy(struct ifnet *);
272
273 static errno_t bridge_ioctl(ifnet_t ifp, unsigned long cmd, void *data);
274 #if HAS_IF_CAP
275 static void bridge_mutecaps(struct bridge_iflist *, int);
276 #endif
277 static int bridge_init(struct ifnet *);
278 static void bridge_stop(struct ifnet *, int);
279
280 #if BRIDGE_MEMBER_OUT_FILTER
281 static errno_t
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 */
285
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);
290
291 static void bridge_detach(ifnet_t ifp);
292
293 static errno_t bridge_input(struct bridge_iflist *, struct ifnet *, struct mbuf *, void *frame_header);
294
295 static void bridge_forward(struct bridge_softc *, struct mbuf *m);
296
297 static void bridge_timer(void *);
298
299 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
300 struct mbuf *, int);
301
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 *);
312 #endif
313 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
314
315 static int bridge_rtable_init(struct bridge_softc *);
316 static void bridge_rtable_fini(struct bridge_softc *);
317
318 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
319 const uint8_t *);
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 *);
324
325 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
326 const char *name);
327 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
328 struct ifnet *ifp);
329 static void bridge_delete_member(struct bridge_softc *,
330 struct bridge_iflist *);
331
332 static void bridge_ifdetach(struct bridge_iflist *bif, struct ifnet *ifp);
333
334
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);
340 #endif
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 *);
366
367 struct bridge_control {
368 int (*bc_func)(struct bridge_softc *, void *);
369 unsigned int bc_argsize;
370 unsigned int bc_flags;
371 };
372
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 */
376
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 },
382
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 },
387
388 { bridge_ioctl_scache, sizeof(struct ifbrparam),
389 BC_F_COPYIN|BC_F_SUSER },
390 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
391 BC_F_COPYOUT },
392
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 },
397
398 { bridge_ioctl_saddr32, sizeof(struct ifbareq32),
399 BC_F_COPYIN|BC_F_SUSER },
400
401 { bridge_ioctl_sto, sizeof(struct ifbrparam),
402 BC_F_COPYIN|BC_F_SUSER },
403 { bridge_ioctl_gto, sizeof(struct ifbrparam),
404 BC_F_COPYOUT },
405
406 { bridge_ioctl_daddr32, sizeof(struct ifbareq32),
407 BC_F_COPYIN|BC_F_SUSER },
408
409 { bridge_ioctl_flush, sizeof(struct ifbreq),
410 BC_F_COPYIN|BC_F_SUSER },
411
412 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
413 BC_F_COPYOUT },
414 { bridge_ioctl_spri, sizeof(struct ifbrparam),
415 BC_F_COPYIN|BC_F_SUSER },
416
417 { bridge_ioctl_ght, sizeof(struct ifbrparam),
418 BC_F_COPYOUT },
419 { bridge_ioctl_sht, sizeof(struct ifbrparam),
420 BC_F_COPYIN|BC_F_SUSER },
421
422 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
423 BC_F_COPYOUT },
424 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
425 BC_F_COPYIN|BC_F_SUSER },
426
427 { bridge_ioctl_gma, sizeof(struct ifbrparam),
428 BC_F_COPYOUT },
429 { bridge_ioctl_sma, sizeof(struct ifbrparam),
430 BC_F_COPYIN|BC_F_SUSER },
431
432 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
433 BC_F_COPYIN|BC_F_SUSER },
434
435 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
436 BC_F_COPYIN|BC_F_SUSER },
437
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 },
442 #endif
443 };
444
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 },
450
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 },
455
456 { bridge_ioctl_scache, sizeof(struct ifbrparam),
457 BC_F_COPYIN|BC_F_SUSER },
458 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
459 BC_F_COPYOUT },
460
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 },
465
466 { bridge_ioctl_saddr64, sizeof(struct ifbareq64),
467 BC_F_COPYIN|BC_F_SUSER },
468
469 { bridge_ioctl_sto, sizeof(struct ifbrparam),
470 BC_F_COPYIN|BC_F_SUSER },
471 { bridge_ioctl_gto, sizeof(struct ifbrparam),
472 BC_F_COPYOUT },
473
474 { bridge_ioctl_daddr64, sizeof(struct ifbareq64),
475 BC_F_COPYIN|BC_F_SUSER },
476
477 { bridge_ioctl_flush, sizeof(struct ifbreq),
478 BC_F_COPYIN|BC_F_SUSER },
479
480 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
481 BC_F_COPYOUT },
482 { bridge_ioctl_spri, sizeof(struct ifbrparam),
483 BC_F_COPYIN|BC_F_SUSER },
484
485 { bridge_ioctl_ght, sizeof(struct ifbrparam),
486 BC_F_COPYOUT },
487 { bridge_ioctl_sht, sizeof(struct ifbrparam),
488 BC_F_COPYIN|BC_F_SUSER },
489
490 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
491 BC_F_COPYOUT },
492 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
493 BC_F_COPYIN|BC_F_SUSER },
494
495 { bridge_ioctl_gma, sizeof(struct ifbrparam),
496 BC_F_COPYOUT },
497 { bridge_ioctl_sma, sizeof(struct ifbrparam),
498 BC_F_COPYIN|BC_F_SUSER },
499
500 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
501 BC_F_COPYIN|BC_F_SUSER },
502
503 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
504 BC_F_COPYIN|BC_F_SUSER },
505
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 },
510 #endif
511 };
512
513 static const unsigned int bridge_control_table_size =
514 sizeof(bridge_control_table32) / sizeof(bridge_control_table32[0]);
515
516 static LIST_HEAD(, bridge_softc) bridge_list = LIST_HEAD_INITIALIZER(bridge_list);
517
518 static lck_grp_t *bridge_lock_grp = NULL;
519 static lck_attr_t *bridge_lock_attr = NULL;
520
521 static lck_rw_t *bridge_list_lock = NULL;
522
523
524 static struct if_clone bridge_cloner =
525 IF_CLONE_INITIALIZER("bridge",
526 bridge_clone_create,
527 bridge_clone_destroy,
528 0,
529 IF_MAXUNIT);
530
531 #if BRIDGE_DEBUG
532
533 SYSCTL_DECL(_net_link);
534
535 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Bridge");
536
537 __private_extern__ int _if_brige_debug = 0;
538
539 SYSCTL_INT(_net_link_bridge, OID_AUTO, debug, CTLFLAG_RW,
540 &_if_brige_debug, 0, "Bridge debug");
541
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);
547
548 void
549 printf_mbuf_pkthdr(mbuf_t m, const char *prefix, const char *suffix)
550 {
551 if (m)
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 : "");
556 else
557 printf("%s<NULL>%s\n", prefix, suffix);
558 }
559
560 void
561 printf_mbuf(mbuf_t m, const char *prefix, const char *suffix)
562 {
563 if (m) {
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);
571 } else
572 printf("%s<NULL>%s\n", prefix, suffix);
573 }
574
575 void
576 printf_mbuf_data(mbuf_t m, size_t offset, size_t len)
577 {
578 mbuf_t n;
579 size_t i, j;
580 size_t pktlen, mlen, maxlen;
581 unsigned char *ptr;
582
583 pktlen = mbuf_pkthdr_len(m);
584
585 if (offset > pktlen)
586 return;
587
588 maxlen = (pktlen - offset > len) ? len : pktlen;
589 n = m;
590 mlen = mbuf_len(n);
591 ptr = mbuf_data(n);
592 for (i = 0, j = 0; i < maxlen; i++, j++) {
593 if (j >= mlen) {
594 n = mbuf_next(n);
595 if (n == 0)
596 break;
597 ptr = mbuf_data(n);
598 mlen = mbuf_len(n);
599 j = 0;
600 }
601 if (i >= offset) {
602 printf("%02x%s", ptr[j], i % 2 ? " " : "");
603 }
604 }
605 return;
606 }
607
608 static void
609 printf_ether_header(struct ether_header *eh)
610 {
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],
616 eh->ether_type);
617 }
618 #endif /* BRIDGE_DEBUG */
619
620 /*
621 * bridgeattach:
622 *
623 * Pseudo-device attach routine.
624 */
625 __private_extern__ int
626 bridgeattach(__unused int n)
627 {
628 int error;
629 lck_grp_attr_t *lck_grp_attr = NULL;
630
631 bridge_rtnode_pool = zinit(sizeof(struct bridge_rtnode), 1024 * sizeof(struct bridge_rtnode),
632 0, "bridge_rtnode");
633
634 lck_grp_attr = lck_grp_attr_alloc_init();
635
636 bridge_lock_grp = lck_grp_alloc_init("if_bridge", lck_grp_attr);
637
638 bridge_lock_attr = lck_attr_alloc_init();
639
640 #if BRIDGE_DEBUG
641 lck_attr_setdebug(bridge_lock_attr);
642 #endif
643
644 bridge_list_lock = lck_rw_alloc_init(bridge_lock_grp, bridge_lock_attr);
645
646 // can free the attributes once we've allocated the group lock
647 lck_grp_attr_free(lck_grp_attr);
648
649 LIST_INIT(&bridge_list);
650 error = if_clone_attach(&bridge_cloner);
651
652 return error;
653 }
654
655 #if BRIDGE_DEBUG
656
657 static void
658 link_print(struct sockaddr_dl * dl_p)
659 {
660 int i;
661
662 #if 1
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);
667 #endif
668 for (i = 0; i < dl_p->sdl_alen; i++)
669 printf("%s%x", i ? ":" : "",
670 (CONST_LLADDR(dl_p))[i]);
671 printf("\n");
672 return;
673 }
674 #endif /* BRIDGE_DEBUG */
675
676
677 /*
678 * bridge_clone_create:
679 *
680 * Create a new bridge instance.
681 */
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
685 this extra param */
686 static int
687 bridge_clone_create(struct if_clone *ifc, int unit)
688 {
689 struct bridge_softc *sc = NULL;
690 struct ifnet *ifp = NULL;
691 u_char eaddr[6];
692 uint32_t r;
693 struct ifnet_init_params init_params;
694 errno_t error = 0;
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;
697
698 sc = _MALLOC(sizeof(*sc), M_DEVBUF, M_WAITOK);
699 memset(sc, 0, sizeof(*sc));
700
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;
705 #endif
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;
713 #ifndef BRIDGE_IPF
714 /*
715 * For backwards compatibility with previous behaviour...
716 * Switch off filtering on the bridge itself if BRIDGE_IPF is
717 * not defined.
718 */
719 sc->sc_filter_flags &= ~IFBF_FILT_USEIPF;
720 #endif
721
722 /* Initialize our routing table. */
723 error = bridge_rtable_init(sc);
724 if (error != 0) {
725 printf("bridge_clone_create: bridge_rtable_init failed %d\n", error);
726 goto done;
727 }
728
729 LIST_INIT(&sc->sc_iflist);
730
731 sc->sc_mtx = lck_mtx_alloc_init(bridge_lock_grp, bridge_lock_attr);
732
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);
756 if (error != 0) {
757 printf("bridge_clone_create: ifnet_allocate failed %d\n", error);
758 goto done;
759 }
760 sc->sc_if = ifp;
761
762 error = ifnet_set_mtu(ifp, ETHERMTU);
763 if (error != 0) {
764 printf("bridge_clone_create: ifnet_set_mtu failed %d\n", error);
765 goto done;
766 }
767 error = ifnet_set_addrlen(ifp, ETHER_ADDR_LEN);
768 if (error != 0) {
769 printf("bridge_clone_create: ifnet_set_addrlen failed %d\n", error);
770 goto done;
771 }
772 error = ifnet_set_baudrate(ifp, 10000000) ; // XXX: this is what IONetworking does
773 if (error != 0) {
774 printf("bridge_clone_create: ifnet_set_baudrate failed %d\n", error);
775 goto done;
776 }
777 error = ifnet_set_hdrlen(ifp, ETHER_HDR_LEN);
778 if (error != 0) {
779 printf("bridge_clone_create: ifnet_set_hdrlen failed %d\n", error);
780 goto done;
781 }
782 error = ifnet_set_flags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST,
783 0xffff);
784 if (error != 0) {
785 printf("bridge_clone_create: ifnet_set_flags failed %d\n", error);
786 goto done;
787 }
788
789 /*
790 * Generate a random ethernet address and use the private AC:DE:48
791 * OUI code.
792 */
793 read_random(&r, sizeof(r));
794 eaddr[0] = 0xAC;
795 eaddr[1] = 0xDE;
796 eaddr[2] = 0x48;
797 eaddr[3] = (r >> 0) & 0xffu;
798 eaddr[4] = (r >> 8) & 0xffu;
799 eaddr[5] = (r >> 16) & 0xffu;
800
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);
808
809 #if BRIDGE_DEBUG
810 link_print(sdl);
811 #endif
812
813 error = ifnet_attach(ifp, NULL);
814 if (error != 0) {
815 printf("bridge_clone_create: ifnet_attach failed %d\n", error);
816 goto done;
817 }
818
819 error = ifnet_set_lladdr_and_type(ifp, eaddr, ETHER_ADDR_LEN, IFT_ETHER);
820 if (error != 0) {
821 printf("bridge_clone_create: ifnet_set_lladdr_and_type failed %d\n", error);
822 goto done;
823 }
824
825 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
826 /*
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
832 */
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 ;
836 #endif
837
838 lck_rw_lock_exclusive(bridge_list_lock);
839 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
840 lck_rw_done(bridge_list_lock);
841
842 /* attach as ethernet */
843 error = bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header), NULL, NULL);
844
845 done:
846 if (error != 0) {
847 printf("bridge_clone_create failed error %d\n", error);
848 /* Cleanup TBD */
849 }
850
851 return error;
852 }
853
854 /*
855 * bridge_clone_destroy:
856 *
857 * Destroy a bridge instance.
858 */
859 static void
860 bridge_clone_destroy(struct ifnet *ifp)
861 {
862 struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp);
863 struct bridge_iflist *bif;
864 int error;
865
866 lck_mtx_lock(sc->sc_mtx);
867 if ((sc->sc_flags & SCF_DETACHING)) {
868 lck_mtx_unlock(sc->sc_mtx);
869 return;
870 }
871 sc->sc_flags |= SCF_DETACHING;
872
873 bridge_stop(ifp, 1);
874
875 error = ifnet_set_flags(ifp, 0, IFF_UP);
876 if (error != 0) {
877 printf("bridge_clone_destroy: ifnet_set_flags failed %d\n", error);
878 }
879
880 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
881 bridge_delete_member(sc, bif);
882
883 lck_mtx_unlock(sc->sc_mtx);
884
885 error = ifnet_detach(ifp);
886 if (error != 0) {
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);
892 }
893 }
894
895 return;
896 }
897
898 #define DRVSPEC \
899 if (ifd->ifd_cmd >= bridge_control_table_size) { \
900 error = EINVAL; \
901 break; \
902 } \
903 bc = &bridge_control_table[ifd->ifd_cmd]; \
904 \
905 if ((cmd & IOC_DIRMASK) == IOC_INOUT && \
906 (bc->bc_flags & BC_F_COPYOUT) == 0) { \
907 error = EINVAL; \
908 break; \
909 } \
910 else if (((cmd & IOC_DIRMASK) == IOC_IN) && \
911 (bc->bc_flags & BC_F_COPYOUT) != 0) { \
912 error = EINVAL; \
913 break; \
914 } \
915 \
916 if (bc->bc_flags & BC_F_SUSER) { \
917 error = kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER); \
918 if (error) \
919 break; \
920 } \
921 \
922 if (ifd->ifd_len != bc->bc_argsize || \
923 ifd->ifd_len > sizeof(args)) { \
924 error = EINVAL; \
925 break; \
926 } \
927 \
928 memset(&args, 0, sizeof(args)); \
929 if (bc->bc_flags & BC_F_COPYIN) { \
930 error = copyin(ifd->ifd_data, &args, ifd->ifd_len); \
931 if (error) \
932 break; \
933 } \
934 \
935 lck_mtx_lock(sc->sc_mtx); \
936 error = (*bc->bc_func)(sc, &args); \
937 lck_mtx_unlock(sc->sc_mtx); \
938 if (error) \
939 break; \
940 \
941 if (bc->bc_flags & BC_F_COPYOUT) \
942 error = copyout(&args, ifd->ifd_data, ifd->ifd_len)
943
944 /*
945 * bridge_ioctl:
946 *
947 * Handle a control request from the operator.
948 */
949 static errno_t
950 bridge_ioctl(ifnet_t ifp, unsigned long cmd, void *data)
951 {
952 struct bridge_softc *sc = ifnet_softc(ifp);
953 struct ifreq *ifr = (struct ifreq *) data;
954 int error = 0;
955
956 lck_mtx_assert(sc->sc_mtx, LCK_MTX_ASSERT_NOTOWNED);
957
958 #if BRIDGE_DEBUG
959 printf("bridge_ioctl: ifp %p cmd 0x%08lx (%c%c [%lu] %c %lu)\n",
960 ifp,
961 cmd,
962 (cmd & IOC_IN) ? 'I' : ' ',
963 (cmd & IOC_OUT) ? 'O' : ' ',
964 IOCPARM_LEN(cmd),
965 (char)IOCGROUP(cmd),
966 cmd & 0xff);
967 printf("SIOCGDRVSPEC32 %lx SIOCGDRVSPEC64 %lx\n", SIOCGDRVSPEC32, SIOCGDRVSPEC64);
968 #endif
969
970 switch (cmd) {
971 case SIOCADDMULTI:
972 break;
973 case SIOCDELMULTI:
974 break;
975
976 case SIOCSDRVSPEC32:
977 case SIOCGDRVSPEC32: {
978 union {
979 struct ifbreq ifbreq;
980 struct ifbifconf32 ifbifconf;
981 struct ifbareq32 ifbareq;
982 struct ifbaconf32 ifbaconf;
983 struct ifbrparam ifbrparam;
984 } args;
985 struct ifdrv32 *ifd = (struct ifdrv32 *) data;
986 const struct bridge_control *bridge_control_table = bridge_control_table32, *bc;
987
988 DRVSPEC;
989
990 break;
991 }
992 case SIOCSDRVSPEC64:
993 case SIOCGDRVSPEC64: {
994 union {
995 struct ifbreq ifbreq;
996 struct ifbifconf64 ifbifconf;
997 struct ifbareq64 ifbareq;
998 struct ifbaconf64 ifbaconf;
999 struct ifbrparam ifbrparam;
1000 } args;
1001 struct ifdrv64 *ifd = (struct ifdrv64 *) data;
1002 const struct bridge_control *bridge_control_table = bridge_control_table64, *bc;
1003
1004 DRVSPEC;
1005
1006 break;
1007 }
1008
1009 case SIOCSIFFLAGS:
1010 if ((ifnet_flags(ifp) & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
1011 /*
1012 * If interface is marked down and it is running,
1013 * then stop and disable it.
1014 */
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) {
1019 /*
1020 * If interface is marked up and it is stopped, then
1021 * start it.
1022 */
1023 lck_mtx_lock(sc->sc_mtx);
1024 error = bridge_init(ifp);
1025 lck_mtx_unlock(sc->sc_mtx);
1026 }
1027 break;
1028
1029 case SIOCSIFMTU:
1030 #if 0
1031 /* APPLE MODIFICATION <cbz@apple.com>
1032 if we wanted to support changing the MTU */
1033 {
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);
1041 if (error)
1042 break;
1043 }
1044 }
1045 #else
1046 /* Do not allow the MTU to be changed on the bridge */
1047 error = EINVAL;
1048 #endif
1049 break;
1050
1051 /* APPLE MODIFICATION - don't pass this down to ether_ioctl, just indicate we don't handle it */
1052 case SIOCGIFMEDIA:
1053 error = EINVAL;
1054 break;
1055
1056 case SIOCSIFLLADDR:
1057 error = ifnet_set_lladdr(ifp, ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1058 if (error != 0)
1059 printf("bridge_ioctl: ifnet_set_lladdr failed %d\n", error);
1060 break;
1061
1062 default:
1063 error = ether_ioctl(ifp, cmd, data);
1064 #if BRIDGE_DEBUG
1065 if (error != 0)
1066 printf("bridge_ioctl: ether_ioctl ifp %p cmd 0x%08lx (%c%c [%lu] %c %lu) failed error: %d\n",
1067 ifp,
1068 cmd,
1069 (cmd & IOC_IN) ? 'I' : ' ',
1070 (cmd & IOC_OUT) ? 'O' : ' ',
1071 IOCPARM_LEN(cmd),
1072 (char) IOCGROUP(cmd),
1073 cmd & 0xff,
1074 error);
1075 #endif /* BRIDGE_DEBUG */
1076 break;
1077 }
1078 lck_mtx_assert(sc->sc_mtx, LCK_MTX_ASSERT_NOTOWNED);
1079
1080 return (error);
1081 }
1082
1083 /*
1084 * bridge_mutecaps:
1085 *
1086 * Clear or restore unwanted capabilities on the member interface
1087 */
1088 #if HAS_IF_CAP
1089 void
1090 bridge_mutecaps(struct bridge_iflist *bif, int mute)
1091 {
1092 struct ifnet *ifp = bif->bif_ifp;
1093 struct ifcapreq ifcr;
1094
1095 if (ifp->if_ioctl == NULL)
1096 return;
1097
1098 memset(&ifcr, 0, sizeof(ifcr));
1099 ifcr.ifcr_capenable = ifp->if_capenable;
1100
1101 if (mute) {
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;
1106 } else
1107 /* restore muted capabilities */
1108 ifcr.ifcr_capenable |= bif->bif_mutecap;
1109
1110 if (bif->bif_mutecap != 0) {
1111 (void) (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifcr);
1112 }
1113 }
1114 #endif /* HAS_IF_CAP */
1115
1116 /*
1117 * bridge_lookup_member:
1118 */
1119 static struct bridge_iflist *
1120 bridge_lookup_member(struct bridge_softc *sc, const char *name)
1121 {
1122 struct bridge_iflist *bif;
1123 struct ifnet *ifp;
1124 char if_xname[IFNAMSIZ];
1125
1126 lck_mtx_assert(sc->sc_mtx, LCK_MTX_ASSERT_OWNED);
1127
1128 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1129 ifp = bif->bif_ifp;
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)
1133 return (bif);
1134 }
1135
1136 return (NULL);
1137 }
1138
1139 /*
1140 * bridge_lookup_member_if:
1141 */
1142 static struct bridge_iflist *
1143 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
1144 {
1145 struct bridge_iflist *bif;
1146
1147 lck_mtx_assert(sc->sc_mtx, LCK_MTX_ASSERT_OWNED);
1148
1149 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1150 if (bif->bif_ifp == member_ifp)
1151 return (bif);
1152 }
1153
1154 return (NULL);
1155 }
1156
1157 static errno_t
1158 bridge_iff_input(void* cookie, ifnet_t ifp, __unused protocol_family_t protocol,
1159 mbuf_t *data, char **frame_ptr)
1160 {
1161 errno_t error = 0;
1162 struct bridge_iflist *bif = (struct bridge_iflist *)cookie;
1163 struct bridge_softc *sc = bif->bif_sc;
1164 int included = 0;
1165 size_t frmlen = 0;
1166 mbuf_t m = *data;
1167
1168 if ((m->m_flags & M_PROTO1))
1169 goto out;
1170
1171 if (*frame_ptr >= (char *)mbuf_datastart(m) && *frame_ptr <= (char *)mbuf_data(m)) {
1172 included = 1;
1173 frmlen = (char *)mbuf_data(m) - *frame_ptr;
1174 }
1175 #if BRIDGE_DEBUG
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);
1181
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);
1186 printf("\n");
1187 }
1188 }
1189 #endif /* BRIDGE_DEBUG */
1190
1191 /* Move data pointer to start of frame to the link layer header */
1192 if (included) {
1193 (void) mbuf_setdata(m, (char *)mbuf_data(m) - frmlen, mbuf_len(m) + frmlen);
1194 (void) mbuf_pkthdr_adjustlen(m, frmlen);
1195 } else {
1196 printf("bridge_iff_input: frame_ptr outside mbuf\n");
1197 goto out;
1198 }
1199
1200 error = bridge_input(bif, ifp, m, *frame_ptr);
1201
1202 /* Adjust packet back to original */
1203 if (error == 0) {
1204 (void) mbuf_setdata(m, (char *)mbuf_data(m) + frmlen, mbuf_len(m) - frmlen);
1205 (void) mbuf_pkthdr_adjustlen(m, -frmlen);
1206 }
1207 #if BRIDGE_DEBUG
1208 if (_if_brige_debug > 1) {
1209 printf("\n");
1210 printf_mbuf(m, "bridge_iff_input]", "\n");
1211 }
1212 #endif /* BRIDGE_DEBUG */
1213
1214 out:
1215 lck_mtx_assert(sc->sc_mtx, LCK_MTX_ASSERT_NOTOWNED);
1216
1217 return error;
1218 }
1219
1220
1221 #if BRIDGE_MEMBER_OUT_FILTER
1222 static errno_t
1223 bridge_iff_output(void *cookie, ifnet_t ifp, __unused protocol_family_t protocol, mbuf_t *data)
1224 {
1225 errno_t error = 0;
1226 struct bridge_iflist *bif = (struct bridge_iflist *)cookie;
1227 struct bridge_softc *sc = bif->bif_sc;
1228 mbuf_t m = *data;
1229
1230 if ((m->m_flags & M_PROTO1))
1231 goto out;
1232
1233 #if BRIDGE_DEBUG
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),
1238 m, mbuf_data(m));
1239 }
1240 #endif /* BRIDGE_DEBUG */
1241
1242 error = bridge_output(sc, ifp, m);
1243 if (error != 0) {
1244 printf("bridge_iff_output: bridge_output failed error %d\n", error);
1245 }
1246
1247 out:
1248 lck_mtx_assert(sc->sc_mtx, LCK_MTX_ASSERT_NOTOWNED);
1249
1250 return error;
1251 }
1252 #endif /* BRIDGE_MEMBER_OUT_FILTER */
1253
1254
1255 static void
1256 bridge_iff_event(void* cookie, ifnet_t ifp, __unused protocol_family_t protocol,
1257 const struct kev_msg *event_msg)
1258 {
1259 struct bridge_iflist *bif = (struct bridge_iflist *)cookie;
1260
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);
1267 break;
1268
1269 default:
1270 break;
1271 }
1272 }
1273 }
1274
1275 static void
1276 bridge_iff_detached(void* cookie, __unused ifnet_t interface)
1277 {
1278 struct bridge_iflist *bif = (struct bridge_iflist *)cookie;
1279
1280 _FREE(bif, M_DEVBUF);
1281
1282 return;
1283 }
1284
1285 /*
1286 * bridge_delete_member:
1287 *
1288 * Delete the specified member interface.
1289 */
1290 static void
1291 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif)
1292 {
1293 struct ifnet *ifs = bif->bif_ifp;
1294
1295 switch (ifnet_type(ifs)) {
1296 case IFT_ETHER:
1297 /*
1298 * Take the interface out of promiscuous mode.
1299 */
1300 (void) ifnet_set_promiscuous(ifs, 0);
1301 break;
1302 #if NGIF > 0
1303 case IFT_GIF:
1304 break;
1305 #endif
1306 default:
1307 #ifdef DIAGNOSTIC
1308 panic("bridge_delete_member: impossible");
1309 #endif
1310 break;
1311 }
1312
1313 ifs->if_bridge = NULL;
1314 LIST_REMOVE(bif, bif_next);
1315
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);
1320
1321 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
1322
1323 if (ifnet_flags(sc->sc_if) & IFF_RUNNING)
1324 bstp_initialization(sc);
1325
1326 /* On the last deleted interface revert the MTU */
1327
1328 if (LIST_EMPTY(&sc->sc_iflist))
1329 (void) ifnet_set_mtu(sc->sc_if, ETHERMTU);
1330 }
1331
1332 static int
1333 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
1334 {
1335 struct ifbreq *req = arg;
1336 struct bridge_iflist *bif = NULL;
1337 struct ifnet *ifs;
1338 int error = 0;
1339 /* APPLE MODIFICATION <cbz@apple.com> - is this a proxy sta being added? */
1340 #if IEEE80211_PROXYSTA
1341 struct bridge_rtnode *brt;
1342 #endif
1343
1344 error = ifnet_find_by_name(req->ifbr_ifsname, &ifs);
1345 if (error || ifs == NULL)
1346 return (ENOENT);
1347
1348 /* Is the interface already attached to this bridge interface */
1349 if (ifs->if_bridge == sc)
1350 return (EEXIST);
1351
1352 if (ifs->if_bridge != NULL)
1353 return (EBUSY);
1354
1355 /* First added interface resets the MTU */
1356
1357 if (LIST_EMPTY(&sc->sc_iflist))
1358 (void) ifnet_set_mtu(sc->sc_if, ETHERMTU);
1359
1360 if (ifnet_mtu(sc->sc_if) != ifnet_mtu(ifs))
1361 return (EINVAL);
1362
1363 bif = _MALLOC(sizeof(*bif), M_DEVBUF, M_WAITOK|M_ZERO);
1364 if (bif == NULL)
1365 return (ENOMEM);
1366
1367 bif->bif_ifp = ifs;
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;
1371 bif->bif_sc = sc;
1372
1373 switch (ifnet_type(ifs)) {
1374 case IFT_ETHER:
1375 /*
1376 * Place the interface into promiscuous mode.
1377 */
1378 error = ifnet_set_promiscuous(ifs, 1);
1379 if (error)
1380 goto out;
1381 #if HAS_IF_CAP
1382 bridge_mutecaps(bif, 1);
1383 #endif
1384 break;
1385 #if NGIF > 0
1386 case IFT_GIF:
1387 break;
1388 #endif
1389 default:
1390 error = EINVAL;
1391 goto out;
1392 }
1393
1394 /*
1395 * If the LINK0 flag is set, and this is the first member interface,
1396 * attempt to inherit its link-layer address.
1397 */
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),
1401 ETHER_ADDR_LEN);
1402 }
1403
1404 // install an interface filter
1405 {
1406 struct iff_filter iff;
1407
1408 memset(&iff, 0, sizeof(struct iff_filter));
1409
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;
1418
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);
1423 if (error != 0) {
1424 printf("bridge_ioctl_add: iflt_attach failed %d\n", error);
1425 goto out;
1426 }
1427 }
1428 ifs->if_bridge = sc;
1429 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
1430
1431
1432 if (ifnet_flags(sc->sc_if) & IFF_RUNNING)
1433 bstp_initialization(sc);
1434 else
1435 bstp_stop(sc);
1436
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));
1440 if (brt) {
1441 #if DIAGNOSTIC
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 );
1445 #endif
1446 brt->brt_ifp_proxysta = ifs;
1447 }
1448 #endif
1449
1450
1451 out:
1452 if (error) {
1453 if (bif != NULL)
1454 _FREE(bif, M_DEVBUF);
1455 }
1456 return (error);
1457 }
1458
1459 static int
1460 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
1461 {
1462 struct ifbreq *req = arg;
1463 struct bridge_iflist *bif;
1464
1465 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1466 if (bif == NULL)
1467 return (ENOENT);
1468
1469 bridge_delete_member(sc, bif);
1470
1471 return (0);
1472 }
1473
1474 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
1475 #if IEEE80211_PROXYSTA
1476 static int
1477 bridge_ioctl_purge(struct bridge_softc *sc, void *arg)
1478 {
1479 struct ifbreq *req = arg;
1480 struct bridge_iflist *bif;
1481 struct ifnet *ifs;
1482
1483 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1484 if (bif == NULL)
1485 return (ENOENT);
1486
1487 ifs = bif->bif_ifp;
1488 bridge_rtpurge(sc, ifs);
1489
1490 return (0);
1491 }
1492 #endif
1493
1494 static int
1495 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
1496 {
1497 struct ifbreq *req = arg;
1498 struct bridge_iflist *bif;
1499
1500 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1501 if (bif == NULL)
1502 return (ENOENT);
1503
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;
1509
1510 return (0);
1511 }
1512
1513 static int
1514 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
1515 {
1516 struct ifbreq *req = arg;
1517 struct bridge_iflist *bif;
1518
1519 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1520 if (bif == NULL)
1521 return (ENOENT);
1522
1523 if (req->ifbr_ifsflags & IFBIF_STP) {
1524 switch (ifnet_type(bif->bif_ifp)) {
1525 case IFT_ETHER:
1526 /* These can do spanning tree. */
1527 break;
1528
1529 default:
1530 /* Nothing else can. */
1531 return (EINVAL);
1532 }
1533 }
1534
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);
1540 #endif
1541
1542 bif->bif_flags = req->ifbr_ifsflags;
1543
1544 if (ifnet_flags(sc->sc_if) & IFF_RUNNING)
1545 bstp_initialization(sc);
1546
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);
1551 #endif
1552
1553 return (0);
1554 }
1555
1556 static int
1557 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
1558 {
1559 struct ifbrparam *param = arg;
1560
1561 sc->sc_brtmax = param->ifbrp_csize;
1562 bridge_rttrim(sc);
1563
1564 return (0);
1565 }
1566
1567 static int
1568 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
1569 {
1570 struct ifbrparam *param = arg;
1571
1572 param->ifbrp_csize = sc->sc_brtmax;
1573
1574 return (0);
1575 }
1576
1577 #define BRIDGE_IOCTL_GIFS \
1578 struct bridge_iflist *bif; \
1579 struct ifbreq breq; \
1580 int count, error = 0; \
1581 uint32_t len; \
1582 \
1583 count = 0; \
1584 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) \
1585 count++; \
1586 \
1587 if (bifc->ifbic_len == 0) { \
1588 bifc->ifbic_len = sizeof(breq) * count; \
1589 return (0); \
1590 } \
1591 \
1592 count = 0; \
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)) \
1597 break; \
1598 \
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)); \
1607 if (error) \
1608 break; \
1609 count++; \
1610 len -= sizeof(breq); \
1611 } \
1612 \
1613 bifc->ifbic_len = sizeof(breq) * count
1614
1615
1616 static int
1617 bridge_ioctl_gifs64(struct bridge_softc *sc, void *arg)
1618 {
1619 struct ifbifconf64 *bifc = arg;
1620
1621 BRIDGE_IOCTL_GIFS;
1622
1623 return (error);
1624 }
1625
1626 static int
1627 bridge_ioctl_gifs32(struct bridge_softc *sc, void *arg)
1628 {
1629 struct ifbifconf32 *bifc = arg;
1630
1631 BRIDGE_IOCTL_GIFS;
1632
1633 return (error);
1634 }
1635
1636 #define BRIDGE_IOCTL_RTS \
1637 struct bridge_rtnode *brt; \
1638 int count = 0, error = 0; \
1639 uint32_t len; \
1640 struct timespec now; \
1641 \
1642 if (bac->ifbac_len == 0) \
1643 return (0); \
1644 \
1645 len = bac->ifbac_len; \
1646 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) { \
1647 if (len < sizeof(bareq)) \
1648 goto out; \
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) { \
1654 nanouptime(&now); \
1655 if (brt->brt_expire >= (unsigned long)now.tv_sec) \
1656 bareq.ifba_expire = brt->brt_expire - now.tv_sec; \
1657 else \
1658 bareq.ifba_expire = 0; \
1659 } else \
1660 bareq.ifba_expire = 0; \
1661 bareq.ifba_flags = brt->brt_flags; \
1662 \
1663 error = copyout(&bareq, bac->ifbac_req + count * sizeof(bareq), sizeof(bareq)); \
1664 if (error) \
1665 goto out; \
1666 count++; \
1667 len -= sizeof(bareq); \
1668 } \
1669 out: \
1670 bac->ifbac_len = sizeof(bareq) * count
1671
1672
1673 static int
1674 bridge_ioctl_rts64(struct bridge_softc *sc, void *arg)
1675 {
1676 struct ifbaconf64 *bac = arg;
1677 struct ifbareq64 bareq;
1678
1679 BRIDGE_IOCTL_RTS;
1680
1681 return (error);
1682 }
1683
1684 static int
1685 bridge_ioctl_rts32(struct bridge_softc *sc, void *arg)
1686 {
1687 struct ifbaconf32 *bac = arg;
1688 struct ifbareq32 bareq;
1689
1690 BRIDGE_IOCTL_RTS;
1691
1692 return (error);
1693 }
1694
1695 static int
1696 bridge_ioctl_saddr64(struct bridge_softc *sc, void *arg)
1697 {
1698 struct ifbareq64 *req = arg;
1699 struct bridge_iflist *bif;
1700 int error;
1701
1702 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1703 if (bif == NULL)
1704 return (ENOENT);
1705
1706 error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
1707 req->ifba_flags);
1708
1709 return (error);
1710 }
1711
1712 static int
1713 bridge_ioctl_saddr32(struct bridge_softc *sc, void *arg)
1714 {
1715 struct ifbareq32 *req = arg;
1716 struct bridge_iflist *bif;
1717 int error;
1718
1719 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1720 if (bif == NULL)
1721 return (ENOENT);
1722
1723 error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
1724 req->ifba_flags);
1725
1726 return (error);
1727 }
1728
1729 static int
1730 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1731 {
1732 struct ifbrparam *param = arg;
1733
1734 sc->sc_brttimeout = param->ifbrp_ctime;
1735
1736 return (0);
1737 }
1738
1739 static int
1740 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1741 {
1742 struct ifbrparam *param = arg;
1743
1744 param->ifbrp_ctime = sc->sc_brttimeout;
1745
1746 return (0);
1747 }
1748
1749 static int
1750 bridge_ioctl_daddr64(struct bridge_softc *sc, void *arg)
1751 {
1752 struct ifbareq64 *req = arg;
1753
1754 return (bridge_rtdaddr(sc, req->ifba_dst));
1755 }
1756
1757 static int
1758 bridge_ioctl_daddr32(struct bridge_softc *sc, void *arg)
1759 {
1760 struct ifbareq32 *req = arg;
1761
1762 return (bridge_rtdaddr(sc, req->ifba_dst));
1763 }
1764
1765 static int
1766 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1767 {
1768 struct ifbreq *req = arg;
1769
1770 bridge_rtflush(sc, req->ifbr_ifsflags);
1771
1772 return (0);
1773 }
1774
1775 static int
1776 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1777 {
1778 struct ifbrparam *param = arg;
1779
1780 param->ifbrp_prio = sc->sc_bridge_priority;
1781
1782 return (0);
1783 }
1784
1785 static int
1786 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1787 {
1788 struct ifbrparam *param = arg;
1789
1790 sc->sc_bridge_priority = param->ifbrp_prio;
1791
1792 if (ifnet_flags(sc->sc_if) & IFF_RUNNING)
1793 bstp_initialization(sc);
1794
1795 return (0);
1796 }
1797
1798 static int
1799 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1800 {
1801 struct ifbrparam *param = arg;
1802
1803 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1804
1805 return (0);
1806 }
1807
1808 static int
1809 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1810 {
1811 struct ifbrparam *param = arg;
1812
1813 if (param->ifbrp_hellotime == 0)
1814 return (EINVAL);
1815 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1816
1817 if (ifnet_flags(sc->sc_if) & IFF_RUNNING)
1818 bstp_initialization(sc);
1819
1820 return (0);
1821 }
1822
1823 static int
1824 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1825 {
1826 struct ifbrparam *param = arg;
1827
1828 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1829
1830 return (0);
1831 }
1832
1833 static int
1834 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1835 {
1836 struct ifbrparam *param = arg;
1837
1838 if (param->ifbrp_fwddelay == 0)
1839 return (EINVAL);
1840 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1841
1842 if (ifnet_flags(sc->sc_if) & IFF_RUNNING)
1843 bstp_initialization(sc);
1844
1845 return (0);
1846 }
1847
1848 static int
1849 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1850 {
1851 struct ifbrparam *param = arg;
1852
1853 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1854
1855 return (0);
1856 }
1857
1858 static int
1859 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1860 {
1861 struct ifbrparam *param = arg;
1862
1863 if (param->ifbrp_maxage == 0)
1864 return (EINVAL);
1865 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1866
1867 if (ifnet_flags(sc->sc_if) & IFF_RUNNING)
1868 bstp_initialization(sc);
1869
1870 return (0);
1871 }
1872
1873 static int
1874 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1875 {
1876 struct ifbreq *req = arg;
1877 struct bridge_iflist *bif;
1878
1879 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1880 if (bif == NULL)
1881 return (ENOENT);
1882
1883 bif->bif_priority = req->ifbr_priority;
1884
1885 if (ifnet_flags(sc->sc_if) & IFF_RUNNING)
1886 bstp_initialization(sc);
1887
1888 return (0);
1889 }
1890
1891 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
1892 #if IEEE80211_PROXYSTA
1893 static void
1894 bridge_proxysta_notify_macaddr(struct ifnet *ifp, int op, const uint8_t *mac)
1895 {
1896 struct proxy_sta_event iev;
1897
1898 memset(&iev, 0, sizeof(iev));
1899 memcpy(iev.iev_addr, mac, ETHER_ADDR_LEN);
1900
1901 rt_proxystamsg(ifp, op, &iev, sizeof(iev));
1902 }
1903
1904 static void
1905 bridge_proxysta_discover(struct ifnet *ifp, const uint8_t *mac)
1906 {
1907 bridge_proxysta_notify_macaddr( ifp, RTM_PROXYSTA_DISCOVERY, mac );
1908 }
1909
1910 static void
1911 bridge_proxysta_idle_timeout(struct ifnet *ifp, const uint8_t *mac)
1912 {
1913 bridge_proxysta_notify_macaddr( ifp, RTM_PROXYSTA_IDLE_TIMEOUT, mac );
1914 }
1915 #endif
1916
1917 static int
1918 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1919 {
1920 struct ifbreq *req = arg;
1921 struct bridge_iflist *bif;
1922
1923 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1924 if (bif == NULL)
1925 return (ENOENT);
1926
1927 bif->bif_path_cost = req->ifbr_path_cost;
1928
1929 if (ifnet_flags(sc->sc_if) & IFF_RUNNING)
1930 bstp_initialization(sc);
1931
1932 return (0);
1933 }
1934
1935 /*
1936 * bridge_ifdetach:
1937 *
1938 * Detach an interface from a bridge. Called when a member
1939 * interface is detaching.
1940 */
1941 static void
1942 bridge_ifdetach(struct bridge_iflist *bif, struct ifnet *ifp)
1943 {
1944 struct bridge_softc *sc = bif->bif_sc;
1945 struct ifbreq breq;
1946
1947 memset(&breq, 0, sizeof(breq));
1948 snprintf(breq.ifbr_ifsname, sizeof(breq.ifbr_ifsname), "%s%d",
1949 ifnet_name(ifp), ifnet_unit(ifp));
1950
1951 lck_mtx_lock(sc->sc_mtx);
1952
1953 (void) bridge_ioctl_del(sc, &breq);
1954
1955 lck_mtx_unlock(sc->sc_mtx);
1956 }
1957
1958 /*
1959 * bridge_init:
1960 *
1961 * Initialize a bridge interface.
1962 */
1963 static int
1964 bridge_init(struct ifnet *ifp)
1965 {
1966 struct bridge_softc *sc = ifnet_softc(ifp);
1967 struct timespec ts;
1968 errno_t error;
1969
1970 if (ifnet_flags(ifp) & IFF_RUNNING)
1971 return (0);
1972
1973 ts.tv_sec = bridge_rtable_prune_period;
1974 ts.tv_nsec = 0;
1975 bsd_timeout(bridge_timer, sc, &ts);
1976
1977 error = ifnet_set_flags(ifp, IFF_RUNNING, IFF_RUNNING);
1978 if (error == 0)
1979 bstp_initialization(sc);
1980
1981 return error;
1982 }
1983
1984 /*
1985 * bridge_stop:
1986 *
1987 * Stop the bridge interface.
1988 */
1989 static void
1990 bridge_stop(struct ifnet *ifp, __unused int disable)
1991 {
1992 struct bridge_softc *sc = ifnet_softc(ifp);
1993
1994 if ((ifnet_flags(ifp) & IFF_RUNNING) == 0)
1995 return;
1996
1997 bsd_untimeout(bridge_timer, sc);
1998 bstp_stop(sc);
1999
2000 bridge_rtflush(sc, IFBF_FLUSHDYN);
2001
2002 (void) ifnet_set_flags(ifp, 0, IFF_RUNNING);
2003 }
2004
2005 /*
2006 * bridge_enqueue:
2007 *
2008 * Enqueue a packet on a bridge member interface.
2009 *
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.
2013 */
2014 __private_extern__ void
2015 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m)
2016 {
2017 int len, error;
2018
2019 lck_mtx_assert(sc->sc_mtx, LCK_MTX_ASSERT_OWNED);
2020
2021 #if BRIDGE_DEBUG
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 */
2027
2028 len = m->m_pkthdr.len;
2029 m->m_flags |= M_PROTO1; //set to avoid loops
2030
2031 error = ifnet_output_raw(dst_ifp, 0, m);
2032 if (error == 0) {
2033 (void) ifnet_stat_increment_out(sc->sc_if, 1, len, 0);
2034 } else {
2035 (void) ifnet_stat_increment_out(sc->sc_if, 0, 0, 1);
2036 }
2037
2038 return;
2039 }
2040
2041
2042 #if BRIDGE_MEMBER_OUT_FILTER
2043
2044 /*
2045 * bridge_output:
2046 *
2047 * Send output from a bridge member interface. This
2048 * performs the bridging function for locally originated
2049 * packets.
2050 *
2051 * The mbuf has the Ethernet header already attached. We must
2052 * enqueue or free the mbuf before returning.
2053 */
2054 static int
2055 bridge_output(struct bridge_softc *sc, ifnet_t ifp, mbuf_t m)
2056 {
2057 struct ether_header *eh;
2058 struct ifnet *dst_if;
2059
2060 #if BRIDGE_DEBUG
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 */
2064
2065 if (m->m_len < ETHER_HDR_LEN) {
2066 m = m_pullup(m, ETHER_HDR_LEN);
2067 if (m == NULL) {
2068 printf("bridge_output ifp %p m_pullup failed\n", ifp);
2069 return EJUSTRETURN;
2070 }
2071 }
2072
2073 eh = mtod(m, struct ether_header *);
2074
2075 /* APPLE MODIFICATION <jhw@apple.com>
2076 * If the packet is an 802.1X ethertype, then only send on the
2077 * original output interface.
2078 */
2079 if (eh->ether_type == htons(ETHERTYPE_PAE)) {
2080 dst_if = ifp;
2081 goto sendunicast;
2082 }
2083
2084 /*
2085 * If bridge is down, but the original output interface is up,
2086 * go ahead and send out that interface. Otherwise, the packet
2087 * is dropped below.
2088 */
2089 if ((ifnet_flags(sc->sc_if) & IFF_RUNNING) == 0) {
2090 dst_if = ifp;
2091 goto sendunicast;
2092 }
2093
2094 lck_mtx_lock(sc->sc_mtx);
2095
2096 /*
2097 * If the packet is a multicast, or we don't know a better way to
2098 * get there, send to all interfaces.
2099 */
2100 if (ETHER_IS_MULTICAST(eh->ether_dhost))
2101 dst_if = NULL;
2102 else
2103 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
2104 if (dst_if == NULL) {
2105 struct bridge_iflist *bif;
2106 struct mbuf *mc;
2107 int used = 0;
2108
2109 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
2110 dst_if = bif->bif_ifp;
2111 if ((ifnet_flags(dst_if) & IFF_RUNNING) == 0)
2112 continue;
2113
2114 /*
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.
2119 */
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:
2126 continue;
2127 }
2128 }
2129
2130 if (LIST_NEXT(bif, bif_next) == NULL) {
2131 used = 1;
2132 mc = m;
2133 } else {
2134 mc = m_copym(m, 0, M_COPYALL, M_NOWAIT);
2135 if (mc == NULL) {
2136 printf("bridge_output ifp %p m_copym failed\n", ifp);
2137 (void) ifnet_stat_increment_out(sc->sc_if, 0, 0, 1);
2138 continue;
2139 }
2140 }
2141
2142 bridge_enqueue(sc, dst_if, mc);
2143 }
2144 if (used == 0) {
2145 printf("bridge_output ifp %p not used\n", ifp);
2146 m_freem(m);
2147 }
2148 lck_mtx_unlock(sc->sc_mtx);
2149
2150 return EJUSTRETURN;
2151 }
2152
2153 sendunicast:
2154 /*
2155 * XXX Spanning tree consideration here?
2156 */
2157
2158 if ((ifnet_flags(dst_if) & IFF_RUNNING) == 0) {
2159 printf("bridge_output ifp %p dst_if %p not running\n", ifp, dst_if);
2160 m_freem(m);
2161
2162 return EJUSTRETURN;
2163 }
2164
2165 if (dst_if != ifp) {
2166 lck_mtx_lock(sc->sc_mtx);
2167
2168 bridge_enqueue(sc, dst_if, m);
2169
2170 lck_mtx_unlock(sc->sc_mtx);
2171
2172 return EJUSTRETURN;
2173 }
2174
2175 return (0);
2176 }
2177 #endif /* BRIDGE_MEMBER_OUT_FILTER */
2178
2179 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
2180 static struct mbuf* bridge_fix_txcsum( struct mbuf *m )
2181 {
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
2189
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 );
2193
2194 /*
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
2198 */
2199
2200 #ifdef DIAGNOSTIC
2201 if ( m_tag_find( m, PACKET_TAG_VLAN, NULL ) != NULL )
2202 {
2203 printf( "bridge: transmitting packet tagged with VLAN?\n" );
2204 KASSERT( 0 );
2205 m_freem( m );
2206 return NULL;
2207 }
2208 #endif
2209
2210 if ( m->m_pkthdr.csum_flags & M_CSUM_IPv4 )
2211 {
2212 amt += hlen;
2213 }
2214 if ( m->m_pkthdr.csum_flags & M_CSUM_TCPv4 )
2215 {
2216 amt += off + sizeof( uint16_t );
2217 }
2218
2219 if ( m->m_pkthdr.csum_flags & M_CSUM_UDPv4 )
2220 {
2221 amt += off + sizeof( uint16_t );
2222 }
2223
2224 if ( m->m_len == ETHER_HDR_LEN )
2225 {
2226 // this is the case where there's an Ethernet header in an mbuf
2227
2228 // the first mbuf is the Ethernet header -- just strip it off and do the checksum
2229 struct mbuf *m_ip = m->m_next;
2230
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
2234 * M_PKTHDR.
2235 *
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
2243 * m_tag_init().
2244 *
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
2253 * panic.
2254 */
2255 m_tag_init(m_ip);
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;
2261
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;
2266 m->m_next = NULL;
2267
2268
2269 // now do the checksums we need -- first IP
2270 if ( m_ip->m_pkthdr.csum_flags & M_CSUM_IPv4 )
2271 {
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 ) );
2274 if ( m_ip == NULL )
2275 {
2276 printf( "bridge: failed to flatten header\n ");
2277 m_freem( m );
2278 return NULL;
2279 }
2280
2281 // now do the checksum
2282 {
2283 struct ip *ip = mtod( m_ip, struct ip* );
2284 ip->ip_sum = in_cksum( m_ip, hlen );
2285
2286 #ifdef VERY_VERY_VERY_DIAGNOSTIC
2287 printf( "bridge: performed IPv4 checksum\n" );
2288 #endif
2289 }
2290 }
2291
2292 // now do a TCP or UDP delayed checksum
2293 if ( m_ip->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4) )
2294 {
2295 in_delayed_cksum( m_ip );
2296
2297 #ifdef VERY_VERY_VERY_DIAGNOSTIC
2298 printf( "bridge: performed TCPv4/UDPv4 checksum\n" );
2299 #endif
2300 }
2301
2302 // now attach the ethernet header back onto the IP packet
2303 m->m_next = m_ip;
2304 m->m_pkthdr.len += m_length( m_ip );
2305
2306 // clear the M_PKTHDR flags on the ip packet (again, we re-attach later)
2307 m_ip->m_flags &= ~M_PKTHDR;
2308
2309 // and clear any csum flags
2310 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4|M_CSUM_IPv4);
2311 }
2312 else if ( m->m_len >= amt )
2313 {
2314 // everything fits in the first mbuf, so futz with m->m_data, m->m_len and m->m_pkthdr.len to
2315 // make it work
2316 m->m_len -= ETHER_HDR_LEN;
2317 m->m_data += ETHER_HDR_LEN;
2318 m->m_pkthdr.len -= ETHER_HDR_LEN;
2319
2320 // now do the checksums we need -- first IP
2321 if ( m->m_pkthdr.csum_flags & M_CSUM_IPv4 )
2322 {
2323 struct ip *ip = mtod( m, struct ip* );
2324 ip->ip_sum = in_cksum( m, hlen );
2325
2326 #ifdef VERY_VERY_VERY_DIAGNOSTIC
2327 printf( "bridge: performed IPv4 checksum\n" );
2328 #endif
2329 }
2330
2331 // now do a TCP or UDP delayed checksum
2332 if ( m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4) )
2333 {
2334 in_delayed_cksum( m );
2335
2336 #ifdef VERY_VERY_VERY_DIAGNOSTIC
2337 printf( "bridge: performed TCPv4/UDPv4 checksum\n" );
2338 #endif
2339 }
2340
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;
2345
2346 // and clear any csum flags
2347 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4|M_CSUM_IPv4);
2348 }
2349 else
2350 {
2351 struct mbuf *m_ip;
2352
2353 // general case -- need to simply split it off and deal
2354
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 );
2357 #if PARANOID
2358 off = M_CSUM_DATA_IPv4_OFFSET( m->m_pkthdr.csum_data );
2359
2360 if ( m->m_pkthdr.csum_flags & M_CSUM_IPv4 )
2361 {
2362 amt += hlen;
2363 }
2364
2365 if ( m->m_pkthdr.csum_flags & M_CSUM_TCPv4 )
2366 {
2367 amt += sizeof( struct tcphdr * );
2368 amt += off;
2369 }
2370
2371 if ( m->m_pkthdr.csum_flags & M_CSUM_UDPv4 )
2372 {
2373 amt += sizeof( struct udphdr * );
2374 amt += off;
2375 }
2376 #endif
2377
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 );
2380 if ( m_ip == NULL )
2381 {
2382 printf( "bridge_fix_txcsum: could not split ether header\n" );
2383
2384 m_freem( m );
2385 return NULL;
2386 }
2387
2388 #if PARANOID
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 )
2391 {
2392 printf( "bridge_fix_txcsum: could not make %d bytes writable\n", amt );
2393
2394 m_freem( m );
2395 m_freem( m_ip );
2396 return NULL;
2397 }
2398 #endif
2399
2400 m_ip->m_pkthdr.csum_flags = m->m_pkthdr.csum_flags;
2401 m_ip->m_pkthdr.csum_data = m->m_pkthdr.csum_data;
2402
2403 m->m_pkthdr.csum_flags = 0;
2404 m->m_pkthdr.csum_data = 0;
2405
2406 // now do the checksums we need -- first IP
2407 if ( m_ip->m_pkthdr.csum_flags & M_CSUM_IPv4 )
2408 {
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 ) );
2411 if ( m_ip == NULL )
2412 {
2413 printf( "bridge: failed to flatten header\n ");
2414 m_freem( m );
2415 return NULL;
2416 }
2417
2418 // now do the checksum
2419 {
2420 struct ip *ip = mtod( m_ip, struct ip* );
2421 ip->ip_sum = in_cksum( m_ip, hlen );
2422
2423 #ifdef VERY_VERY_VERY_DIAGNOSTIC
2424 printf( "bridge: performed IPv4 checksum\n" );
2425 #endif
2426 }
2427 }
2428
2429 // now do a TCP or UDP delayed checksum
2430 if ( m_ip->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4) )
2431 {
2432 in_delayed_cksum( m_ip );
2433
2434 #ifdef VERY_VERY_VERY_DIAGNOSTIC
2435 printf( "bridge: performed TCPv4/UDPv4 checksum\n" );
2436 #endif
2437 }
2438
2439 // now attach the ethernet header back onto the IP packet
2440 m->m_next = m_ip;
2441 m->m_pkthdr.len += m_length( m_ip );
2442
2443 // clear the M_PKTHDR flags on the ip packet (again, we re-attach later)
2444 m_ip->m_flags &= ~M_PKTHDR;
2445
2446 // and clear any csum flags
2447 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4|M_CSUM_IPv4);
2448 }
2449
2450 return m;
2451 }
2452 #endif
2453
2454 /*
2455 * bridge_start:
2456 *
2457 * Start output on a bridge.
2458 */
2459 static errno_t
2460 bridge_start(ifnet_t ifp, mbuf_t m)
2461 {
2462 struct bridge_softc *sc = ifnet_softc(ifp);
2463 struct ether_header *eh;
2464 struct ifnet *dst_if;
2465
2466 lck_mtx_assert(sc->sc_mtx, LCK_MTX_ASSERT_NOTOWNED);
2467
2468 eh = mtod(m, struct ether_header *);
2469
2470 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0 &&
2471 (dst_if = bridge_rtlookup(sc, eh->ether_dhost)) != NULL) {
2472
2473 {
2474 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
2475 /*
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
2480 */
2481 if (
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 )
2484 )
2485 {
2486 m = bridge_fix_txcsum( m );
2487 if ( m == NULL )
2488 {
2489 goto done;
2490 }
2491 }
2492
2493 #else
2494 if (eh->ether_type == htons(ETHERTYPE_IP))
2495 mbuf_outbound_finalize(m, PF_INET, sizeof(struct ether_header));
2496 else
2497 m->m_pkthdr.csum_flags = 0;
2498 #endif
2499 lck_mtx_lock(sc->sc_mtx);
2500 #if NBPFILTER > 0
2501 if (sc->sc_bpf_output)
2502 bridge_bpf_output(ifp, m);
2503 #endif
2504 bridge_enqueue(sc, dst_if, m);
2505 lck_mtx_unlock(sc->sc_mtx);
2506 }
2507 } else
2508 {
2509 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
2510
2511 /*
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
2516 */
2517
2518 if (
2519 (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4|M_CSUM_IPv4)) &&
2520 /*
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)
2523 */
2524 ( 1 )
2525 )
2526 {
2527 m = bridge_fix_txcsum( m );
2528 if ( m == NULL )
2529 {
2530 goto done;
2531 }
2532 }
2533 #else
2534 if (eh->ether_type == htons(ETHERTYPE_IP))
2535 mbuf_outbound_finalize(m, PF_INET, sizeof(struct ether_header));
2536 else
2537 m->m_pkthdr.csum_flags = 0;
2538 #endif
2539
2540 lck_mtx_lock(sc->sc_mtx);
2541 #if NBPFILTER > 0
2542 if (sc->sc_bpf_output)
2543 bridge_bpf_output(ifp, m);
2544 #endif
2545 bridge_broadcast(sc, ifp, m, 0);
2546 lck_mtx_unlock(sc->sc_mtx);
2547 }
2548 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
2549 done:
2550 #endif
2551
2552 return 0;
2553 }
2554
2555 /*
2556 * bridge_forward:
2557 *
2558 * The forwarding function of the bridge.
2559 */
2560 static void
2561 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
2562 {
2563 struct bridge_iflist *bif;
2564 struct ifnet *src_if, *dst_if;
2565 struct ether_header *eh;
2566
2567 lck_mtx_assert(sc->sc_mtx, LCK_MTX_ASSERT_OWNED);
2568
2569 #if BRIDGE_DEBUG
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 */
2573
2574 src_if = m->m_pkthdr.rcvif;
2575
2576 (void) ifnet_stat_increment_in(sc->sc_if, 1, m->m_pkthdr.len, 0);
2577
2578 /*
2579 * Look up the bridge_iflist.
2580 */
2581 bif = bridge_lookup_member_if(sc, src_if);
2582 if (bif == NULL) {
2583 /* Interface is not a bridge member (anymore?) */
2584 m_freem(m);
2585 return;
2586 }
2587
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. */
2592 m_freem(m);
2593 return;
2594 }
2595 #endif
2596
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:
2602 m_freem(m);
2603 return;
2604 }
2605 }
2606
2607 eh = mtod(m, struct ether_header *);
2608
2609 /*
2610 * If the interface is learning, and the source
2611 * address is valid and not multicast, record
2612 * the address.
2613 */
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);
2621 }
2622
2623 if ((bif->bif_flags & IFBIF_STP) != 0 &&
2624 bif->bif_state == BSTP_IFSTATE_LEARNING) {
2625 m_freem(m);
2626 return;
2627 }
2628
2629 /*
2630 * At this point, the port either doesn't participate
2631 * in spanning tree or it is in the forwarding state.
2632 */
2633
2634 /*
2635 * If the packet is unicast, destined for someone on
2636 * "this" side of the bridge, drop it.
2637 */
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)
2644 {
2645 struct bridge_rtnode *brt;
2646 dst_if = NULL;
2647 brt = bridge_rtnode_lookup(sc, eh->ether_shost);
2648 if (brt) {
2649 dst_if = brt->brt_ifp_proxysta;
2650 }
2651 if (dst_if == NULL) {
2652 m_freem(m);
2653 return;
2654 }
2655 }
2656 else
2657 #endif
2658 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
2659 if (src_if == dst_if) {
2660 m_freem(m);
2661 return;
2662 }
2663 } else {
2664 /* ...forward it to all interfaces. */
2665 sc->sc_if->if_imcasts++;
2666 dst_if = NULL;
2667 }
2668
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))
2675 */
2676 #if 1
2677 if (eh->ether_type == htons(ETHERTYPE_RSN_PREAUTH))
2678 {
2679 m->m_pkthdr.rcvif = sc->sc_if;
2680 #if NBPFILTER > 0
2681 if (sc->sc_bpf_input)
2682 bridge_bpf_input(sc->sc_if, m);
2683 #endif
2684 }
2685 #endif
2686
2687 if (dst_if == NULL) {
2688
2689 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
2690 /*
2691 * Clear any in-bound checksum flags for this packet.
2692 */
2693 m->m_pkthdr.csum_flags = 0;
2694 #else
2695 mbuf_inbound_modified(m);
2696 #endif
2697
2698 bridge_broadcast(sc, src_if, m, 1);
2699 return;
2700 }
2701
2702 /*
2703 * At this point, we're dealing with a unicast frame
2704 * going to a different interface.
2705 */
2706 if ((ifnet_flags(dst_if) & IFF_RUNNING) == 0) {
2707 m_freem(m);
2708 return;
2709 }
2710 bif = bridge_lookup_member_if(sc, dst_if);
2711 if (bif == NULL) {
2712 /* Not a member of the bridge (anymore?) */
2713 m_freem(m);
2714 return;
2715 }
2716
2717 if (bif->bif_flags & IFBIF_STP) {
2718 switch (bif->bif_state) {
2719 case BSTP_IFSTATE_DISABLED:
2720 case BSTP_IFSTATE_BLOCKING:
2721 m_freem(m);
2722 return;
2723 }
2724 }
2725
2726 #if APPLE_BRIDGE_HWCKSUM_SUPPORT
2727 /*
2728 * Clear any in-bound checksum flags for this packet.
2729 */
2730 {
2731 m->m_pkthdr.csum_flags = 0;
2732 }
2733 #else
2734 mbuf_inbound_modified(m);
2735 #endif
2736
2737 bridge_enqueue(sc, dst_if, m);
2738 }
2739
2740 char * ether_ntop(char *, size_t , const u_char *);
2741
2742 __private_extern__ char *
2743 ether_ntop(char *buf, size_t len, const u_char *ap)
2744 {
2745 snprintf(buf, len, "%02x:%02x:%02x:%02x:%02x:%02x",
2746 ap[0], ap[1], ap[2], ap[3], ap[4], ap[5]);
2747
2748 return buf;
2749 }
2750
2751 /*
2752 * bridge_input:
2753 *
2754 * Receive input from a member interface. Queue the packet for
2755 * bridging if it is not for us.
2756 */
2757 errno_t
2758 bridge_input(struct bridge_iflist *bif, struct ifnet *ifp, struct mbuf *m, void *frame_header)
2759 {
2760 struct ifnet *bifp;
2761 struct ether_header *eh;
2762 struct mbuf *mc;
2763 int is_for_us = 0;
2764 struct bridge_softc *sc = bif->bif_sc;
2765 struct bridge_iflist *brm;
2766
2767 #if BRIDGE_DEBUG
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),
2772 m, mbuf_data(m));
2773 #endif /* BRIDGE_DEBUG */
2774
2775 if ((ifnet_flags(sc->sc_if) & IFF_RUNNING) == 0) {
2776 #if BRIDGE_DEBUG
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 */
2781 return 0;
2782 }
2783
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);
2787
2788 lck_mtx_lock(sc->sc_mtx);
2789
2790 bifp = sc->sc_if;
2791
2792 /* Is it a good idea to reassign a new value to bif ? TBD */
2793 bif = bridge_lookup_member_if(sc, ifp);
2794 if (bif == NULL) {
2795 lck_mtx_unlock(sc->sc_mtx);
2796 #if BRIDGE_DEBUG
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 */
2801 return 0;
2802 }
2803
2804 eh = (struct ether_header *)mbuf_data(m);
2805
2806 /*
2807 * If the packet is for us, set the packets source as the
2808 * bridge, and return the packet back to ether_input for
2809 * local processing.
2810 */
2811 if (memcmp(eh->ether_dhost, ifnet_lladdr(bifp),
2812 ETHER_ADDR_LEN) == 0) {
2813
2814 /* Mark the packet as arriving on the bridge interface */
2815 (void) mbuf_pkthdr_setrcvif(m, bifp);
2816 mbuf_pkthdr_setheader(m, frame_header);
2817
2818 /*
2819 * If the interface is learning, and the source
2820 * address is valid and not multicast, record
2821 * the address.
2822 */
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);
2830 }
2831
2832 #if NBPFILTER > 0
2833 if (sc->sc_bpf_input)
2834 bridge_bpf_input(bifp, m);
2835 #endif
2836
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);
2839
2840 (void) ifnet_stat_increment_in(bifp, 1, mbuf_pkthdr_len(m), 0);
2841
2842 lck_mtx_unlock(sc->sc_mtx);
2843
2844 #if BRIDGE_DEBUG
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 */
2849
2850 dlil_input_packet_list(bifp, m);
2851
2852 return EJUSTRETURN;
2853 }
2854
2855 /*
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
2863 *
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
2867 */
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) {
2873 #endif
2874
2875 #ifdef VERY_VERY_VERY_DIAGNOSTIC
2876 printf("bridge_input: not forwarding packet bound for member interface\n" );
2877 #endif
2878 lck_mtx_unlock(sc->sc_mtx);
2879 return 0;
2880 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
2881 #if IEEE80211_PROXYSTA
2882 }
2883 #if VERY_VERY_VERY_DIAGNOSTIC
2884 else {
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] );
2888 }
2889 #endif
2890 #endif
2891 }
2892
2893 if ((m->m_flags & (M_BCAST|M_MCAST))) {
2894 struct ifmultiaddr *ifma = NULL;
2895
2896 if ((m->m_flags & M_BCAST)) {
2897 is_for_us = 1;
2898 } else {
2899 #if BRIDGE_DEBUG
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]);
2903
2904 for (ifma = bifp->if_multiaddrs.lh_first; ifma;
2905 ifma = ifma->ifma_link.le_next) {
2906
2907 if (ifma->ifma_addr == NULL)
2908 printf(" <none> ");
2909 else if (ifma->ifma_addr->sa_family == AF_INET) {
2910 struct sockaddr_in *sin = (struct sockaddr_in *)ifma->ifma_addr;
2911
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));
2917 }
2918 if (!ifma->ifma_ll || !ifma->ifma_ll->ifma_addr)
2919 printf("<none>\n");
2920 else {
2921 struct sockaddr_dl *sdl = (struct sockaddr_dl *)ifma->ifma_ll->ifma_addr;
2922
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]);
2926
2927 }
2928 }
2929 #endif /* BRIDGE_DEBUG */
2930
2931 /*
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.
2936 *
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
2940 */
2941
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;
2946
2947 if (memcmp(eh->ether_dhost, CONST_LLADDR(sdl), ETHER_ADDR_LEN) == 0)
2948 break;
2949 }
2950 }
2951 if (ifma != NULL) {
2952 /* this packet matches the bridge's own filter, so pass it up as coming from us */
2953
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;
2957
2958 /* keep track of this to help us decide about forwarding */
2959 is_for_us = 1;
2960
2961 #if BRIDGE_DEBUG
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) );
2965 #endif
2966 } else {
2967 #if BRIDGE_DEBUG
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) );
2971 #endif
2972 }
2973 }
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);
2978 if (m == NULL) {
2979 lck_mtx_unlock(sc->sc_mtx);
2980 #if BRIDGE_DEBUG
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 */
2985 return EJUSTRETURN;
2986 }
2987 }
2988
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:
2994 {
2995 lck_mtx_unlock(sc->sc_mtx);
2996
2997 #if BRIDGE_DEBUG
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 */
3002
3003 m_freem(m);
3004 return EJUSTRETURN;
3005 }
3006 }
3007 }
3008
3009 /*
3010 * If the interface is learning, and the source
3011 * address is valid and not multicast, record
3012 * the address.
3013 */
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);
3021 }
3022
3023 if (is_for_us) {
3024 /*
3025 * Make a deep copy of the packet and enqueue the copy
3026 * for bridge processing; return the original packet for
3027 * local processing.
3028 */
3029 mc = m_dup(m, M_NOWAIT);
3030 if (mc == NULL) {
3031 #ifdef DIAGNOSTIC
3032 printf( "bridge_input: failed to duplicate multicast frame, not forwarding\n" );
3033 #endif
3034 #if BRIDGE_DEBUG
3035 } else {
3036 if (_if_brige_debug) {
3037 printf_mbuf(mc, "mc for us: ", "\n");
3038 printf_mbuf_data(m, 0, 20);
3039 printf("\n");
3040 }
3041 #endif /* BRIDGE_DEBUG */
3042 }
3043 } else {
3044 /*
3045 * we'll just pass the original, since we don't need to pass it
3046 * up the stack
3047 */
3048 mc = m;
3049 }
3050
3051 /* Perform the bridge forwarding function with the copy. */
3052 if (mc != NULL) {
3053 #if BRIDGE_DEBUG
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);
3059 }
3060
3061 // TBD should have an option for type of bridge
3062 #if 0
3063 /*
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
3067 * bridge.
3068 */
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);
3072 if (mc != NULL) {
3073 mc->m_pkthdr.rcvif = bifp;
3074 #if NBPFILTER > 0
3075 if (sc->sc_bpf_input)
3076 bridge_bpf_input(bifp, mc);
3077 #endif
3078 }
3079 #endif
3080 /* Return the original packet for local processing. */
3081 if ( !is_for_us )
3082 {
3083 /* we don't free the packet -- bridge_forward already did so */
3084 lck_mtx_unlock(sc->sc_mtx);
3085
3086 #if BRIDGE_DEBUG
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));
3090 #endif
3091
3092 return EJUSTRETURN;
3093 }
3094
3095 // mark packet as arriving on the bridge
3096 m->m_pkthdr.rcvif = bifp;
3097 m->m_pkthdr.header = mbuf_data(m);
3098
3099 #if NBPFILTER > 0
3100 if (sc->sc_bpf_input)
3101 bridge_bpf_input(bifp, m);
3102 #endif
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);
3105
3106 (void) ifnet_stat_increment_in(bifp, 1, mbuf_pkthdr_len(m), 0);
3107
3108 lck_mtx_unlock(sc->sc_mtx);
3109
3110 #if BRIDGE_DEBUG
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 */
3115
3116 dlil_input_packet_list(bifp, m);
3117
3118 return EJUSTRETURN;
3119 }
3120
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);
3127
3128 #if BRIDGE_DEBUG
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 */
3133
3134 m_freem(m);
3135 return EJUSTRETURN;
3136 }
3137 }
3138
3139 /* this code is not needed for Apple's bridge where the stack attaches directly */
3140 #if 1 /* TBD should be an option */
3141 /*
3142 * Unicast. Make sure it's not for us.
3143 */
3144 LIST_FOREACH(brm, &sc->sc_iflist, bif_next) {
3145 if(ifnet_type(brm->bif_ifp) != IFT_ETHER)
3146 continue;
3147
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);
3156
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
3160 #if NGIF > 0
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);
3165 m = NULL;
3166 }
3167 #endif
3168 #endif
3169 lck_mtx_unlock(sc->sc_mtx);
3170
3171 #if BRIDGE_DEBUG
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 */
3177
3178 dlil_input_packet_list(brm->bif_ifp, m);
3179
3180 return EJUSTRETURN;
3181 }
3182
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);
3187
3188 #if BRIDGE_DEBUG
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 */
3193
3194 m_freem(m);
3195 return EJUSTRETURN;
3196 }
3197 }
3198 #endif
3199
3200 /*
3201 * If the interface is learning, and the source
3202 * address is valid and not multicast, record
3203 * the address.
3204 */
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);
3212 }
3213
3214 /* Perform the bridge forwarding function. */
3215 #if BRIDGE_DEBUG
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 */
3220
3221 bridge_forward(sc, m);
3222 lck_mtx_unlock(sc->sc_mtx);
3223 return EJUSTRETURN;
3224 }
3225
3226 /*
3227 * bridge_broadcast:
3228 *
3229 * Send a frame to all interfaces that are members of
3230 * the bridge, except for the one on which the packet
3231 * arrived.
3232 */
3233 static void
3234 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
3235 struct mbuf *m, __unused int runfilt)
3236 {
3237 struct bridge_iflist *bif;
3238 struct mbuf *mc;
3239 struct ifnet *dst_if;
3240 int used = 0;
3241
3242 lck_mtx_assert(sc->sc_mtx, LCK_MTX_ASSERT_OWNED);
3243
3244 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
3245 dst_if = bif->bif_ifp;
3246 if (dst_if == src_if)
3247 continue;
3248
3249 if (bif->bif_flags & IFBIF_STP) {
3250 switch (bif->bif_state) {
3251 case BSTP_IFSTATE_BLOCKING:
3252 case BSTP_IFSTATE_DISABLED:
3253 continue;
3254 }
3255 }
3256
3257 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
3258 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
3259 continue;
3260
3261 if ((ifnet_flags(dst_if) & IFF_RUNNING) == 0)
3262 continue;
3263
3264 if (LIST_NEXT(bif, bif_next) == NULL) {
3265 mc = m;
3266 used = 1;
3267 } else {
3268 mc = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
3269 if (mc == NULL) {
3270 (void) ifnet_stat_increment_out(sc->sc_if, 0, 0, 1);
3271 continue;
3272 }
3273 }
3274
3275 bridge_enqueue(sc, dst_if, mc);
3276 }
3277 if (used == 0)
3278 m_freem(m);
3279 }
3280
3281 /*
3282 * bridge_rtupdate:
3283 *
3284 * Add a bridge routing entry.
3285 */
3286 static int
3287 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
3288 struct ifnet *dst_if, int setflags, uint8_t flags)
3289 {
3290 struct bridge_rtnode *brt;
3291 int error;
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? */
3296 #endif
3297 struct timespec now;
3298
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)) {
3304 is_pds = 1;
3305 }
3306 else {
3307 is_pds = 0;
3308 }
3309 #endif
3310 /*
3311 * A route for this destination might already exist. If so,
3312 * update it, otherwise create a new one.
3313 */
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 */
3319 if (is_pds) {
3320 if (sc->sc_brtcnt >= (sc->sc_brtmax+sc->sc_brtmax_proxysta))
3321 return (ENOSPC);
3322 }
3323 else
3324 #endif
3325 if (sc->sc_brtcnt >= sc->sc_brtmax)
3326 return (ENOSPC);
3327
3328 /*
3329 * Allocate a new bridge forwarding node, and
3330 * initialize the expiration time and Ethernet
3331 * address.
3332 */
3333 brt = zalloc_noblock(bridge_rtnode_pool);
3334 if (brt == NULL)
3335 return (ENOMEM);
3336
3337 memset(brt, 0, sizeof(*brt));
3338 nanouptime(&now);
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);
3342
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
3346 if (is_pds) {
3347 brt->brt_flags_ext |= IFBAF_EXT_PROXYSTA;
3348 #if DIAGNOSTIC
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 );
3351 #endif
3352 bridge_proxysta_discover( dst_if, dst );
3353 }
3354 #endif
3355
3356 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
3357 zfree(bridge_rtnode_pool, brt);
3358 return (error);
3359 }
3360 }
3361
3362 brt->brt_ifp = dst_if;
3363 if (setflags) {
3364 brt->brt_flags = flags;
3365 brt->brt_expire = (flags & IFBAF_STATIC) ? 0 :
3366 now.tv_sec + sc->sc_brttimeout;
3367 }
3368
3369 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA - */
3370 #if IEEE80211_PROXYSTA
3371 if (is_pds) {
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 );
3375 #endif
3376 brt->brt_expire = (flags & IFBAF_STATIC) ? 0 :
3377 now.tv_sec + sc->sc_brttimeout;
3378 }
3379 #endif
3380
3381 return (0);
3382 }
3383
3384 /*
3385 * bridge_rtlookup:
3386 *
3387 * Lookup the destination interface for an address.
3388 */
3389 static struct ifnet *
3390 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
3391 {
3392 struct bridge_rtnode *brt;
3393
3394 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
3395 return (NULL);
3396
3397 return (brt->brt_ifp);
3398 }
3399
3400 /*
3401 * bridge_rttrim:
3402 *
3403 * Trim the routine table so that we have a number
3404 * of routing entries less than or equal to the
3405 * maximum number.
3406 */
3407 static void
3408 bridge_rttrim(struct bridge_softc *sc)
3409 {
3410 struct bridge_rtnode *brt, *nbrt;
3411
3412 /* Make sure we actually need to do this. */
3413 if (sc->sc_brtcnt <= sc->sc_brtmax)
3414 return;
3415
3416 /* Force an aging cycle; this might trim enough addresses. */
3417 bridge_rtage(sc);
3418 if (sc->sc_brtcnt <= sc->sc_brtmax)
3419 return;
3420
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)
3426 return;
3427 }
3428 }
3429 }
3430
3431 /*
3432 * bridge_timer:
3433 *
3434 * Aging timer for the bridge.
3435 */
3436 static void
3437 bridge_timer(void *arg)
3438 {
3439 struct bridge_softc *sc = arg;
3440 struct timespec ts;
3441
3442 lck_mtx_lock(sc->sc_mtx);
3443
3444 bridge_rtage(sc);
3445
3446 lck_mtx_unlock(sc->sc_mtx);
3447
3448 if (ifnet_flags(sc->sc_if) & IFF_RUNNING) {
3449 ts.tv_sec = bridge_rtable_prune_period;
3450 ts.tv_nsec = 0;
3451 bsd_timeout(bridge_timer, sc, &ts);
3452 }
3453 }
3454
3455 /*
3456 * bridge_rtage:
3457 *
3458 * Perform an aging cycle.
3459 */
3460 static void
3461 bridge_rtage(struct bridge_softc *sc)
3462 {
3463 struct bridge_rtnode *brt, *nbrt;
3464 struct timespec now;
3465
3466 nanouptime(&now);
3467
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);
3473 }
3474 }
3475 }
3476
3477 /*
3478 * bridge_rtflush:
3479 *
3480 * Remove all dynamic addresses from the bridge.
3481 */
3482 static void
3483 bridge_rtflush(struct bridge_softc *sc, int full)
3484 {
3485 struct bridge_rtnode *brt, *nbrt;
3486
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);
3491 }
3492 }
3493
3494 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
3495 #if IEEE80211_PROXYSTA
3496 /*
3497 * bridge_rtdiscovery:
3498 *
3499 */
3500 static void
3501 bridge_rtdiscovery(struct bridge_softc *sc)
3502 {
3503 struct bridge_rtnode *brt, *nbrt;
3504 struct bridge_iflist *bif;
3505
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)) {
3511 #if DIAGNOSTIC
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 );
3515 #endif
3516 brt->brt_flags_ext |= IFBAF_EXT_PROXYSTA;
3517 }
3518
3519 if (brt->brt_ifp_proxysta == NULL) {
3520 #if DIAGNOSTIC
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 );
3524 #endif
3525 bridge_proxysta_discover( brt->brt_ifp, brt->brt_addr );
3526 }
3527 }
3528 }
3529
3530 /*
3531 * bridge_rtpurge:
3532 *
3533 * Remove all dynamic addresses from a specific interface on the bridge.
3534 */
3535 static void
3536 bridge_rtpurge(struct bridge_softc *sc, struct ifnet *ifs)
3537 {
3538 struct bridge_rtnode *brt, *nbrt;
3539
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) {
3543 #if DIAGNOSTIC
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 );
3548 #endif
3549 bridge_rtnode_destroy(sc, brt);
3550 }
3551 }
3552 }
3553 #endif
3554
3555 /*
3556 * bridge_rtdaddr:
3557 *
3558 * Remove an address from the table.
3559 */
3560 static int
3561 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
3562 {
3563 struct bridge_rtnode *brt;
3564
3565 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
3566 return (ENOENT);
3567
3568 bridge_rtnode_destroy(sc, brt);
3569 return (0);
3570 }
3571
3572 /*
3573 * bridge_rtdelete:
3574 *
3575 * Delete routes to a speicifc member interface.
3576 */
3577 __private_extern__ void
3578 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
3579 {
3580 struct bridge_rtnode *brt, *nbrt;
3581
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);
3587 }
3588 }
3589
3590 /*
3591 * bridge_rtable_init:
3592 *
3593 * Initialize the route table for this bridge.
3594 */
3595 static int
3596 bridge_rtable_init(struct bridge_softc *sc)
3597 {
3598 int i;
3599
3600 sc->sc_rthash = _MALLOC(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
3601 M_DEVBUF, M_WAITOK);
3602 if (sc->sc_rthash == NULL)
3603 return (ENOMEM);
3604
3605 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
3606 LIST_INIT(&sc->sc_rthash[i]);
3607
3608 sc->sc_rthash_key = random();
3609
3610 LIST_INIT(&sc->sc_rtlist);
3611
3612 return (0);
3613 }
3614
3615 /*
3616 * bridge_rtable_fini:
3617 *
3618 * Deconstruct the route table for this bridge.
3619 */
3620 static void
3621 bridge_rtable_fini(struct bridge_softc *sc)
3622 {
3623
3624 _FREE(sc->sc_rthash, M_DEVBUF);
3625 }
3626
3627 /*
3628 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
3629 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
3630 */
3631 #define mix(a, b, c) \
3632 do { \
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)
3643
3644 static uint32_t
3645 bridge_rthash(__unused struct bridge_softc *sc, const uint8_t *addr)
3646 {
3647 /* APPLE MODIFICATION - wasabi performance improvment - simplify the hash algorithm */
3648 #if 0
3649 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
3650
3651 b += addr[5] << 8;
3652 b += addr[4];
3653 a += addr[3] << 24;
3654 a += addr[2] << 16;
3655 a += addr[1] << 8;
3656 a += addr[0];
3657
3658 mix(a, b, c);
3659
3660 return (c & BRIDGE_RTHASH_MASK);
3661 #else
3662 return addr[5];
3663 #endif
3664 }
3665
3666 #undef mix
3667
3668 /*
3669 * bridge_rtnode_lookup:
3670 *
3671 * Look up a bridge route node for the specified destination.
3672 */
3673 static struct bridge_rtnode *
3674 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
3675 {
3676 struct bridge_rtnode *brt;
3677 uint32_t hash;
3678 int dir;
3679
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);
3683 if (dir == 0)
3684 return (brt);
3685 if (dir > 0)
3686 return (NULL);
3687 }
3688
3689 return (NULL);
3690 }
3691
3692 /*
3693 * bridge_rtnode_insert:
3694 *
3695 * Insert the specified bridge node into the route table. We
3696 * assume the entry is not already in the table.
3697 */
3698 static int
3699 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
3700 {
3701 struct bridge_rtnode *lbrt;
3702 uint32_t hash;
3703 int dir;
3704
3705 hash = bridge_rthash(sc, brt->brt_addr);
3706
3707 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
3708 if (lbrt == NULL) {
3709 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
3710 goto out;
3711 }
3712
3713 do {
3714 dir = memcmp(brt->brt_addr, lbrt->brt_addr, ETHER_ADDR_LEN);
3715 if (dir == 0)
3716 return (EEXIST);
3717 if (dir > 0) {
3718 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
3719 goto out;
3720 }
3721 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
3722 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
3723 goto out;
3724 }
3725 lbrt = LIST_NEXT(lbrt, brt_hash);
3726 } while (lbrt != NULL);
3727
3728 #ifdef DIAGNOSTIC
3729 panic("bridge_rtnode_insert: impossible");
3730 #endif
3731
3732 out:
3733 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
3734 sc->sc_brtcnt++;
3735
3736 return (0);
3737 }
3738
3739 /*
3740 * bridge_rtnode_destroy:
3741 *
3742 * Destroy a bridge rtnode.
3743 */
3744 static void
3745 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
3746 {
3747 lck_mtx_assert(sc->sc_mtx, LCK_MTX_ASSERT_OWNED);
3748
3749 /* APPLE MODIFICATION <cbz@apple.com> - add support for Proxy STA */
3750 #if IEEE80211_PROXYSTA
3751 if (brt->brt_flags_ext & IFBAF_EXT_PROXYSTA) {
3752 #if DIAGNOSTIC
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 );
3758 #endif
3759 bridge_proxysta_idle_timeout( brt->brt_ifp, brt->brt_addr );
3760 }
3761 #endif
3762
3763 LIST_REMOVE(brt, brt_hash);
3764
3765 LIST_REMOVE(brt, brt_list);
3766 sc->sc_brtcnt--;
3767 zfree(bridge_rtnode_pool, brt);
3768 }
3769
3770 static errno_t
3771 bridge_set_bpf_tap(ifnet_t ifp, bpf_tap_mode mode, bpf_packet_func bpf_callback)
3772 {
3773 struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp);
3774
3775 //printf("bridge_set_bpf_tap ifp %p mode %d\n", ifp, mode);
3776
3777 /* TBD locking */
3778 if (sc == NULL || (sc->sc_flags & SCF_DETACHING)) {
3779 return ENODEV;
3780 }
3781
3782 switch (mode) {
3783 case BPF_TAP_DISABLE:
3784 sc->sc_bpf_input = sc->sc_bpf_output = NULL;
3785 break;
3786
3787 case BPF_TAP_INPUT:
3788 sc->sc_bpf_input = bpf_callback;
3789 break;
3790
3791 case BPF_TAP_OUTPUT:
3792 sc->sc_bpf_output = bpf_callback;
3793 break;
3794
3795 case BPF_TAP_INPUT_OUTPUT:
3796 sc->sc_bpf_input = sc->sc_bpf_output = bpf_callback;
3797 break;
3798
3799 default:
3800 break;
3801 }
3802
3803 return 0;
3804 }
3805
3806 static void
3807 bridge_detach(__unused ifnet_t ifp)
3808 {
3809 struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp);
3810
3811 /* Tear down the routing table. */
3812 bridge_rtable_fini(sc);
3813
3814 lck_rw_lock_exclusive(bridge_list_lock);
3815 LIST_REMOVE(sc, sc_list);
3816 lck_rw_done(bridge_list_lock);
3817
3818 ifnet_release(ifp);
3819
3820 lck_mtx_free(sc->sc_mtx, bridge_lock_grp);
3821
3822 _FREE(sc, M_DEVBUF);
3823 return;
3824 }
3825
3826 __private_extern__ errno_t bridge_bpf_input(ifnet_t ifp, struct mbuf *m)
3827 {
3828 struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp);
3829
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);
3834 }
3835 return 0;
3836 }
3837
3838 __private_extern__ errno_t bridge_bpf_output(ifnet_t ifp, struct mbuf *m)
3839 {
3840 struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp);
3841
3842 if (sc->sc_bpf_output) {
3843 (*sc->sc_bpf_output)(ifp, m);
3844 }
3845 return 0;
3846 }
3847