2 * Copyright (c) 2004-2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $ */
31 * Copyright 2001 Wasabi Systems, Inc.
32 * All rights reserved.
34 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed for the NetBSD Project by
47 * Wasabi Systems, Inc.
48 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
49 * or promote products derived from this software without specific prior
52 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
54 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
56 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62 * POSSIBILITY OF SUCH DAMAGE.
66 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
67 * All rights reserved.
69 * Redistribution and use in source and binary forms, with or without
70 * modification, are permitted provided that the following conditions
72 * 1. Redistributions of source code must retain the above copyright
73 * notice, this list of conditions and the following disclaimer.
74 * 2. Redistributions in binary form must reproduce the above copyright
75 * notice, this list of conditions and the following disclaimer in the
76 * documentation and/or other materials provided with the distribution.
78 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
79 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
80 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
81 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
82 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
83 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
84 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
86 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
87 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
88 * POSSIBILITY OF SUCH DAMAGE.
90 * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp
94 * Network interface bridge support.
98 * - Currently only supports Ethernet-like interfaces (Ethernet,
99 * 802.11, VLANs on Ethernet, etc.) Figure out a nice way
100 * to bridge other types of interfaces (FDDI-FDDI, and maybe
101 * consider heterogenous bridges).
103 * - GIF isn't handled due to the lack of IPPROTO_ETHERIP support.
106 #include <sys/cdefs.h>
108 #define BRIDGE_DEBUG 1
110 #include <sys/param.h>
111 #include <sys/mbuf.h>
112 #include <sys/malloc.h>
113 #include <sys/protosw.h>
114 #include <sys/systm.h>
115 #include <sys/time.h>
116 #include <sys/socket.h> /* for net/if.h */
117 #include <sys/sockio.h>
118 #include <sys/kernel.h>
119 #include <sys/random.h>
120 #include <sys/syslog.h>
121 #include <sys/sysctl.h>
122 #include <sys/proc.h>
123 #include <sys/lock.h>
124 #include <sys/mcache.h>
126 #include <sys/kauth.h>
128 #include <kern/thread_call.h>
130 #include <libkern/libkern.h>
132 #include <kern/zalloc.h>
138 #include <net/if_dl.h>
139 #include <net/if_types.h>
140 #include <net/if_var.h>
141 #include <net/if_media.h>
143 #include <netinet/in.h> /* for struct arpcom */
144 #include <netinet/in_systm.h>
145 #include <netinet/in_var.h>
146 #include <netinet/ip.h>
147 #include <netinet/ip_var.h>
149 #include <netinet/ip6.h>
150 #include <netinet6/ip6_var.h>
153 #include <netinet/ip_carp.h>
155 #include <netinet/if_ether.h> /* for struct arpcom */
156 #include <net/bridgestp.h>
157 #include <net/if_bridgevar.h>
158 #include <net/if_llc.h>
160 #include <net/if_vlan_var.h>
161 #endif /* NVLAN > 0 */
163 #include <net/if_ether.h>
164 #include <net/dlil.h>
165 #include <net/kpi_interfacefilter.h>
167 #include <net/route.h>
169 #include <netinet/ip_fw2.h>
170 #include <netinet/ip_dummynet.h>
171 #endif /* PFIL_HOOKS */
172 #include <dev/random/randomdev.h>
175 #define BR_DBGF_LIFECYCLE 0x0001
176 #define BR_DBGF_INPUT 0x0002
177 #define BR_DBGF_OUTPPUT 0x0004
178 #define BR_DBGF_RT_TABLE 0x0008
179 #define BR_DBGF_DELAYED_CALL 0x0010
180 #define BR_DBGF_IOCTL 0x0020
181 #define BR_DBGF_MBUF 0x0040
182 #define BR_DBGF_MCAST 0x0080
183 #endif /* BRIDGE_DEBUG */
185 #define _BRIDGE_LOCK(_sc) lck_mtx_lock(&(_sc)->sc_mtx)
186 #define _BRIDGE_UNLOCK(_sc) lck_mtx_unlock(&(_sc)->sc_mtx)
187 #define BRIDGE_LOCK_ASSERT_HELD(_sc) \
188 lck_mtx_assert(&(_sc)->sc_mtx, LCK_MTX_ASSERT_OWNED)
189 #define BRIDGE_LOCK_ASSERT_NOTHELD(_sc) \
190 lck_mtx_assert(&(_sc)->sc_mtx, LCK_MTX_ASSERT_NOTOWNED)
194 #define BR_LCKDBG_MAX 4
196 #define BRIDGE_LOCK(_sc) bridge_lock(_sc)
197 #define BRIDGE_UNLOCK(_sc) bridge_unlock(_sc)
198 #define BRIDGE_LOCK2REF(_sc, _err) _err = bridge_lock2ref(_sc)
199 #define BRIDGE_UNREF(_sc) bridge_unref(_sc)
200 #define BRIDGE_XLOCK(_sc) bridge_xlock(_sc)
201 #define BRIDGE_XDROP(_sc) bridge_xdrop(_sc)
203 #else /* !BRIDGE_DEBUG */
205 #define BRIDGE_LOCK(_sc) _BRIDGE_LOCK(_sc)
206 #define BRIDGE_UNLOCK(_sc) _BRIDGE_UNLOCK(_sc)
207 #define BRIDGE_LOCK2REF(_sc, _err) do { \
208 BRIDGE_LOCK_ASSERT_HELD(_sc); \
209 if ((_sc)->sc_iflist_xcnt > 0) \
212 (_sc)->sc_iflist_ref++; \
213 _BRIDGE_UNLOCK(_sc); \
215 #define BRIDGE_UNREF(_sc) do { \
217 (_sc)->sc_iflist_ref--; \
218 if (((_sc)->sc_iflist_xcnt > 0) && ((_sc)->sc_iflist_ref == 0)) { \
219 _BRIDGE_UNLOCK(_sc); \
220 wakeup(&(_sc)->sc_cv); \
222 _BRIDGE_UNLOCK(_sc); \
224 #define BRIDGE_XLOCK(_sc) do { \
225 BRIDGE_LOCK_ASSERT_HELD(_sc); \
226 (_sc)->sc_iflist_xcnt++; \
227 while ((_sc)->sc_iflist_ref > 0) \
228 msleep(&(_sc)->sc_cv, &(_sc)->sc_mtx, PZERO, \
229 "BRIDGE_XLOCK", NULL); \
231 #define BRIDGE_XDROP(_sc) do { \
232 BRIDGE_LOCK_ASSERT_HELD(_sc); \
233 (_sc)->sc_iflist_xcnt--; \
236 #endif /* BRIDGE_DEBUG */
239 #define BRIDGE_BPF_MTAP_INPUT(sc, m) \
240 if (sc->sc_bpf_input) \
241 bridge_bpf_input(sc->sc_ifp, m)
242 #else /* NBPFILTER */
243 #define BRIDGE_BPF_MTAP_INPUT(ifp, m)
244 #endif /* NBPFILTER */
247 * Initial size of the route hash table. Must be a power of two.
249 #ifndef BRIDGE_RTHASH_SIZE
250 #define BRIDGE_RTHASH_SIZE 16
254 * Maximum size of the routing hash table
256 #define BRIDGE_RTHASH_SIZE_MAX 2048
258 #define BRIDGE_RTHASH_MASK(sc) ((sc)->sc_rthash_size - 1)
261 * Maximum number of addresses to cache.
263 #ifndef BRIDGE_RTABLE_MAX
264 #define BRIDGE_RTABLE_MAX 100
269 * Timeout (in seconds) for entries learned dynamically.
271 #ifndef BRIDGE_RTABLE_TIMEOUT
272 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
276 * Number of seconds between walks of the route list.
278 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
279 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
283 * List of capabilities to possibly mask on the member interface.
285 #define BRIDGE_IFCAPS_MASK (IFCAP_TOE|IFCAP_TSO|IFCAP_TXCSUM)
287 * List of capabilities to disable on the member interface.
289 #define BRIDGE_IFCAPS_STRIP IFCAP_LRO
292 * Bridge interface list entry.
294 struct bridge_iflist
{
295 TAILQ_ENTRY(bridge_iflist
) bif_next
;
296 struct ifnet
*bif_ifp
; /* member if */
297 struct bstp_port bif_stp
; /* STP state */
298 uint32_t bif_ifflags
; /* member if flags */
299 int bif_savedcaps
; /* saved capabilities */
300 uint32_t bif_addrmax
; /* max # of addresses */
301 uint32_t bif_addrcnt
; /* cur. # of addresses */
302 uint32_t bif_addrexceeded
; /* # of address violations */
304 interface_filter_t bif_iff_ref
;
305 struct bridge_softc
*bif_sc
;
309 #define BIFF_PROMISC 0x01 /* promiscuous mode set */
310 #define BIFF_PROTO_ATTACHED 0x02 /* protocol attached */
311 #define BIFF_FILTER_ATTACHED 0x04 /* interface filter attached */
312 #define BIFF_MEDIA_ACTIVE 0x08 /* interface media active */
317 struct bridge_rtnode
{
318 LIST_ENTRY(bridge_rtnode
) brt_hash
; /* hash table linkage */
319 LIST_ENTRY(bridge_rtnode
) brt_list
; /* list linkage */
320 struct bridge_iflist
*brt_dst
; /* destination if */
321 unsigned long brt_expire
; /* expiration time */
322 uint8_t brt_flags
; /* address flags */
323 uint8_t brt_addr
[ETHER_ADDR_LEN
];
324 uint16_t brt_vlan
; /* vlan id */
327 #define brt_ifp brt_dst->bif_ifp
330 * Bridge delayed function call context
332 typedef void (*bridge_delayed_func_t
)(struct bridge_softc
*);
334 struct bridge_delayed_call
{
335 struct bridge_softc
*bdc_sc
;
336 bridge_delayed_func_t bdc_func
; /* Function to call */
337 struct timespec bdc_ts
; /* Time to call */
341 #define BDCF_OUTSTANDING 0x01 /* Delayed call has been scheduled */
342 #define BDCF_CANCELLING 0x02 /* May be waiting for call completion */
345 * Software state for each bridge.
348 LIST_HEAD(_bridge_rtnode_list
, bridge_rtnode
);
350 struct bridge_softc
{
351 struct ifnet
*sc_ifp
; /* make this an interface */
352 LIST_ENTRY(bridge_softc
) sc_list
;
353 decl_lck_mtx_data(, sc_mtx
);
355 uint32_t sc_brtmax
; /* max # of addresses */
356 uint32_t sc_brtcnt
; /* cur. # of addresses */
357 uint32_t sc_brttimeout
; /* rt timeout in seconds */
358 uint32_t sc_iflist_ref
; /* refcount for sc_iflist */
359 uint32_t sc_iflist_xcnt
; /* refcount for sc_iflist */
360 TAILQ_HEAD(, bridge_iflist
) sc_iflist
; /* member interface list */
361 struct _bridge_rtnode_list
*sc_rthash
; /* our forwarding table */
362 struct _bridge_rtnode_list sc_rtlist
; /* list version of above */
363 uint32_t sc_rthash_key
; /* key for hash */
364 uint32_t sc_rthash_size
; /* size of the hash table */
365 TAILQ_HEAD(, bridge_iflist
) sc_spanlist
; /* span ports list */
366 struct bstp_state sc_stp
; /* STP state */
367 uint32_t sc_brtexceeded
; /* # of cache drops */
368 uint32_t sc_filter_flags
; /* ipf and flags */
369 struct ifnet
*sc_ifaddr
; /* member mac copied from */
370 u_char sc_defaddr
[6]; /* Default MAC address */
372 char sc_if_xname
[IFNAMSIZ
];
373 bpf_packet_func sc_bpf_input
;
374 bpf_packet_func sc_bpf_output
;
376 struct bridge_delayed_call sc_aging_timer
;
377 struct bridge_delayed_call sc_resize_call
;
381 * Locking and unlocking calling history
383 void *lock_lr
[BR_LCKDBG_MAX
];
385 void *unlock_lr
[BR_LCKDBG_MAX
];
387 #endif /* BRIDGE_DEBUG */
390 #define SCF_DETACHING 0x01
391 #define SCF_RESIZING 0x02
392 #define SCF_MEDIA_ACTIVE 0x04
394 decl_lck_mtx_data(static, bridge_list_mtx
);
396 static int bridge_rtable_prune_period
= BRIDGE_RTABLE_PRUNE_PERIOD
;
398 static zone_t bridge_rtnode_pool
= NULL
;
400 static int bridge_clone_create(struct if_clone
*, uint32_t, void *);
401 static int bridge_clone_destroy(struct ifnet
*);
403 static errno_t
bridge_ioctl(struct ifnet
*, u_long
, void *);
405 static void bridge_mutecaps(struct bridge_softc
*);
406 static void bridge_set_ifcap(struct bridge_softc
*, struct bridge_iflist
*,
409 static errno_t
bridge_set_tso(struct bridge_softc
*);
410 __private_extern__
void bridge_ifdetach(struct bridge_iflist
*, struct ifnet
*);
411 static int bridge_init(struct ifnet
*);
412 #if HAS_BRIDGE_DUMMYNET
413 static void bridge_dummynet(struct mbuf
*, struct ifnet
*);
415 static void bridge_ifstop(struct ifnet
*, int);
416 static int bridge_output(struct ifnet
*, struct mbuf
*);
417 static void bridge_finalize_cksum(struct ifnet
*, struct mbuf
*);
418 static void bridge_start(struct ifnet
*);
419 __private_extern__ errno_t
bridge_input(struct ifnet
*, struct mbuf
*, void *);
420 #if BRIDGE_MEMBER_OUT_FILTER
421 static errno_t
bridge_iff_output(void *, ifnet_t
, protocol_family_t
,
423 static int bridge_member_output(struct ifnet
*, struct mbuf
*,
424 struct sockaddr
*, struct rtentry
*);
426 static int bridge_enqueue(struct bridge_softc
*, struct ifnet
*,
428 static void bridge_rtdelete(struct bridge_softc
*, struct ifnet
*ifp
, int);
430 static void bridge_forward(struct bridge_softc
*, struct bridge_iflist
*,
433 static void bridge_aging_timer(struct bridge_softc
*sc
);
435 static void bridge_broadcast(struct bridge_softc
*, struct ifnet
*,
437 static void bridge_span(struct bridge_softc
*, struct mbuf
*);
439 static int bridge_rtupdate(struct bridge_softc
*, const uint8_t *,
440 uint16_t, struct bridge_iflist
*, int, uint8_t);
441 static struct ifnet
*bridge_rtlookup(struct bridge_softc
*, const uint8_t *,
443 static void bridge_rttrim(struct bridge_softc
*);
444 static void bridge_rtage(struct bridge_softc
*);
445 static void bridge_rtflush(struct bridge_softc
*, int);
446 static int bridge_rtdaddr(struct bridge_softc
*, const uint8_t *,
449 static int bridge_rtable_init(struct bridge_softc
*);
450 static void bridge_rtable_fini(struct bridge_softc
*);
452 static void bridge_rthash_resize(struct bridge_softc
*);
454 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
455 static struct bridge_rtnode
*bridge_rtnode_lookup(struct bridge_softc
*,
456 const uint8_t *, uint16_t);
457 static int bridge_rtnode_hash(struct bridge_softc
*,
458 struct bridge_rtnode
*);
459 static int bridge_rtnode_insert(struct bridge_softc
*,
460 struct bridge_rtnode
*);
461 static void bridge_rtnode_destroy(struct bridge_softc
*,
462 struct bridge_rtnode
*);
464 static void bridge_rtable_expire(struct ifnet
*, int);
465 static void bridge_state_change(struct ifnet
*, int);
466 #endif /* BRIDGESTP */
468 static struct bridge_iflist
*bridge_lookup_member(struct bridge_softc
*,
470 static struct bridge_iflist
*bridge_lookup_member_if(struct bridge_softc
*,
472 static void bridge_delete_member(struct bridge_softc
*,
473 struct bridge_iflist
*, int);
474 static void bridge_delete_span(struct bridge_softc
*,
475 struct bridge_iflist
*);
477 static int bridge_ioctl_add(struct bridge_softc
*, void *);
478 static int bridge_ioctl_del(struct bridge_softc
*, void *);
479 static int bridge_ioctl_gifflags(struct bridge_softc
*, void *);
480 static int bridge_ioctl_sifflags(struct bridge_softc
*, void *);
481 static int bridge_ioctl_scache(struct bridge_softc
*, void *);
482 static int bridge_ioctl_gcache(struct bridge_softc
*, void *);
483 static int bridge_ioctl_gifs32(struct bridge_softc
*, void *);
484 static int bridge_ioctl_gifs64(struct bridge_softc
*, void *);
485 static int bridge_ioctl_rts32(struct bridge_softc
*, void *);
486 static int bridge_ioctl_rts64(struct bridge_softc
*, void *);
487 static int bridge_ioctl_saddr32(struct bridge_softc
*, void *);
488 static int bridge_ioctl_saddr64(struct bridge_softc
*, void *);
489 static int bridge_ioctl_sto(struct bridge_softc
*, void *);
490 static int bridge_ioctl_gto(struct bridge_softc
*, void *);
491 static int bridge_ioctl_daddr32(struct bridge_softc
*, void *);
492 static int bridge_ioctl_daddr64(struct bridge_softc
*, void *);
493 static int bridge_ioctl_flush(struct bridge_softc
*, void *);
494 static int bridge_ioctl_gpri(struct bridge_softc
*, void *);
495 static int bridge_ioctl_spri(struct bridge_softc
*, void *);
496 static int bridge_ioctl_ght(struct bridge_softc
*, void *);
497 static int bridge_ioctl_sht(struct bridge_softc
*, void *);
498 static int bridge_ioctl_gfd(struct bridge_softc
*, void *);
499 static int bridge_ioctl_sfd(struct bridge_softc
*, void *);
500 static int bridge_ioctl_gma(struct bridge_softc
*, void *);
501 static int bridge_ioctl_sma(struct bridge_softc
*, void *);
502 static int bridge_ioctl_sifprio(struct bridge_softc
*, void *);
503 static int bridge_ioctl_sifcost(struct bridge_softc
*, void *);
504 static int bridge_ioctl_sifmaxaddr(struct bridge_softc
*, void *);
505 static int bridge_ioctl_addspan(struct bridge_softc
*, void *);
506 static int bridge_ioctl_delspan(struct bridge_softc
*, void *);
507 static int bridge_ioctl_gbparam32(struct bridge_softc
*, void *);
508 static int bridge_ioctl_gbparam64(struct bridge_softc
*, void *);
509 static int bridge_ioctl_grte(struct bridge_softc
*, void *);
510 static int bridge_ioctl_gifsstp32(struct bridge_softc
*, void *);
511 static int bridge_ioctl_gifsstp64(struct bridge_softc
*, void *);
512 static int bridge_ioctl_sproto(struct bridge_softc
*, void *);
513 static int bridge_ioctl_stxhc(struct bridge_softc
*, void *);
514 static int bridge_ioctl_purge(struct bridge_softc
*sc
, void *);
515 static int bridge_ioctl_gfilt(struct bridge_softc
*, void *);
516 static int bridge_ioctl_sfilt(struct bridge_softc
*, void *);
518 static int bridge_pfil(struct mbuf
**, struct ifnet
*, struct ifnet
*,
520 static int bridge_ip_checkbasic(struct mbuf
**);
522 static int bridge_ip6_checkbasic(struct mbuf
**);
524 static int bridge_fragment(struct ifnet
*, struct mbuf
*,
525 struct ether_header
*, int, struct llc
*);
526 #endif /* PFIL_HOOKS */
528 static errno_t
bridge_set_bpf_tap(ifnet_t
, bpf_tap_mode
, bpf_packet_func
);
529 __private_extern__ errno_t
bridge_bpf_input(ifnet_t
, struct mbuf
*);
530 __private_extern__ errno_t
bridge_bpf_output(ifnet_t
, struct mbuf
*);
532 static void bridge_detach(ifnet_t
);
533 static void bridge_link_event(struct ifnet
*, u_int32_t
);
534 static void bridge_iflinkevent(struct ifnet
*);
535 static u_int32_t
bridge_updatelinkstatus(struct bridge_softc
*);
536 static int interface_media_active(struct ifnet
*);
537 static void bridge_schedule_delayed_call(struct bridge_delayed_call
*);
538 static void bridge_cancel_delayed_call(struct bridge_delayed_call
*);
540 #define m_copypacket(m, how) m_copym(m, 0, M_COPYALL, how)
542 /* The default bridge vlan is 1 (IEEE 802.1Q-2003 Table 9-2) */
543 #define VLANTAGOF(_m) 0
545 u_int8_t bstp_etheraddr
[ETHER_ADDR_LEN
] =
546 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
549 static struct bstp_cb_ops bridge_ops
= {
550 .bcb_state
= bridge_state_change
,
551 .bcb_rtage
= bridge_rtable_expire
553 #endif /* BRIDGESTP */
555 SYSCTL_DECL(_net_link
);
556 SYSCTL_NODE(_net_link
, IFT_BRIDGE
, bridge
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0,
559 static int bridge_inherit_mac
= 0; /* share MAC with first bridge member */
560 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, inherit_mac
,
561 CTLFLAG_RW
|CTLFLAG_LOCKED
,
562 &bridge_inherit_mac
, 0,
563 "Inherit MAC address from the first bridge member");
565 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, rtable_prune_period
,
566 CTLFLAG_RW
|CTLFLAG_LOCKED
,
567 &bridge_rtable_prune_period
, 0,
568 "Interval between pruning of routing table");
570 static unsigned int bridge_rtable_hash_size_max
= BRIDGE_RTHASH_SIZE_MAX
;
571 SYSCTL_UINT(_net_link_bridge
, OID_AUTO
, rtable_hash_size_max
,
572 CTLFLAG_RW
|CTLFLAG_LOCKED
,
573 &bridge_rtable_hash_size_max
, 0,
574 "Maximum size of the routing hash table");
576 #if BRIDGE_DEBUG_DELAYED_CALLBACK
577 static int bridge_delayed_callback_delay
= 0;
578 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, delayed_callback_delay
,
579 CTLFLAG_RW
|CTLFLAG_LOCKED
,
580 &bridge_delayed_callback_delay
, 0,
581 "Delay before calling delayed function");
584 #if defined(PFIL_HOOKS)
585 static int pfil_onlyip
= 1; /* only pass IP[46] packets when pfil is enabled */
586 static int pfil_bridge
= 1; /* run pfil hooks on the bridge interface */
587 static int pfil_member
= 1; /* run pfil hooks on the member interface */
588 static int pfil_ipfw
= 0; /* layer2 filter with ipfw */
589 static int pfil_ipfw_arp
= 0; /* layer2 filter with ipfw */
590 static int pfil_local_phys
= 0; /* run pfil hooks on the physical interface */
591 /* for locally destined packets */
592 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_onlyip
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
593 &pfil_onlyip
, 0, "Only pass IP packets when pfil is enabled");
594 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, ipfw_arp
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
595 &pfil_ipfw_arp
, 0, "Filter ARP packets through IPFW layer2");
596 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_bridge
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
597 &pfil_bridge
, 0, "Packet filter on the bridge interface");
598 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_member
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
599 &pfil_member
, 0, "Packet filter on the member interface");
600 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_local_phys
,
601 CTLFLAG_RW
|CTLFLAG_LOCKED
, &pfil_local_phys
, 0,
602 "Packet filter on the physical interface for locally destined packets");
603 #endif /* PFIL_HOOKS */
606 static int log_stp
= 0; /* log STP state changes */
607 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, log_stp
, CTLFLAG_RW
,
608 &log_stp
, 0, "Log STP state changes");
609 #endif /* BRIDGESTP */
611 struct bridge_control
{
612 int (*bc_func
)(struct bridge_softc
*, void *);
613 unsigned int bc_argsize
;
614 unsigned int bc_flags
;
617 #define BC_F_COPYIN 0x01 /* copy arguments in */
618 #define BC_F_COPYOUT 0x02 /* copy arguments out */
619 #define BC_F_SUSER 0x04 /* do super-user check */
621 static const struct bridge_control bridge_control_table32
[] = {
622 { bridge_ioctl_add
, sizeof (struct ifbreq
),
623 BC_F_COPYIN
|BC_F_SUSER
},
624 { bridge_ioctl_del
, sizeof (struct ifbreq
),
625 BC_F_COPYIN
|BC_F_SUSER
},
627 { bridge_ioctl_gifflags
, sizeof (struct ifbreq
),
628 BC_F_COPYIN
|BC_F_COPYOUT
},
629 { bridge_ioctl_sifflags
, sizeof (struct ifbreq
),
630 BC_F_COPYIN
|BC_F_SUSER
},
632 { bridge_ioctl_scache
, sizeof (struct ifbrparam
),
633 BC_F_COPYIN
|BC_F_SUSER
},
634 { bridge_ioctl_gcache
, sizeof (struct ifbrparam
),
637 { bridge_ioctl_gifs32
, sizeof (struct ifbifconf32
),
638 BC_F_COPYIN
|BC_F_COPYOUT
},
639 { bridge_ioctl_rts32
, sizeof (struct ifbaconf32
),
640 BC_F_COPYIN
|BC_F_COPYOUT
},
642 { bridge_ioctl_saddr32
, sizeof (struct ifbareq32
),
643 BC_F_COPYIN
|BC_F_SUSER
},
645 { bridge_ioctl_sto
, sizeof (struct ifbrparam
),
646 BC_F_COPYIN
|BC_F_SUSER
},
647 { bridge_ioctl_gto
, sizeof (struct ifbrparam
),
650 { bridge_ioctl_daddr32
, sizeof (struct ifbareq32
),
651 BC_F_COPYIN
|BC_F_SUSER
},
653 { bridge_ioctl_flush
, sizeof (struct ifbreq
),
654 BC_F_COPYIN
|BC_F_SUSER
},
656 { bridge_ioctl_gpri
, sizeof (struct ifbrparam
),
658 { bridge_ioctl_spri
, sizeof (struct ifbrparam
),
659 BC_F_COPYIN
|BC_F_SUSER
},
661 { bridge_ioctl_ght
, sizeof (struct ifbrparam
),
663 { bridge_ioctl_sht
, sizeof (struct ifbrparam
),
664 BC_F_COPYIN
|BC_F_SUSER
},
666 { bridge_ioctl_gfd
, sizeof (struct ifbrparam
),
668 { bridge_ioctl_sfd
, sizeof (struct ifbrparam
),
669 BC_F_COPYIN
|BC_F_SUSER
},
671 { bridge_ioctl_gma
, sizeof (struct ifbrparam
),
673 { bridge_ioctl_sma
, sizeof (struct ifbrparam
),
674 BC_F_COPYIN
|BC_F_SUSER
},
676 { bridge_ioctl_sifprio
, sizeof (struct ifbreq
),
677 BC_F_COPYIN
|BC_F_SUSER
},
679 { bridge_ioctl_sifcost
, sizeof (struct ifbreq
),
680 BC_F_COPYIN
|BC_F_SUSER
},
682 { bridge_ioctl_gfilt
, sizeof (struct ifbrparam
),
684 { bridge_ioctl_sfilt
, sizeof (struct ifbrparam
),
685 BC_F_COPYIN
|BC_F_SUSER
},
687 { bridge_ioctl_purge
, sizeof (struct ifbreq
),
688 BC_F_COPYIN
|BC_F_SUSER
},
690 { bridge_ioctl_addspan
, sizeof (struct ifbreq
),
691 BC_F_COPYIN
|BC_F_SUSER
},
692 { bridge_ioctl_delspan
, sizeof (struct ifbreq
),
693 BC_F_COPYIN
|BC_F_SUSER
},
695 { bridge_ioctl_gbparam32
, sizeof (struct ifbropreq32
),
698 { bridge_ioctl_grte
, sizeof (struct ifbrparam
),
701 { bridge_ioctl_gifsstp32
, sizeof (struct ifbpstpconf32
),
702 BC_F_COPYIN
|BC_F_COPYOUT
},
704 { bridge_ioctl_sproto
, sizeof (struct ifbrparam
),
705 BC_F_COPYIN
|BC_F_SUSER
},
707 { bridge_ioctl_stxhc
, sizeof (struct ifbrparam
),
708 BC_F_COPYIN
|BC_F_SUSER
},
710 { bridge_ioctl_sifmaxaddr
, sizeof (struct ifbreq
),
711 BC_F_COPYIN
|BC_F_SUSER
},
714 static const struct bridge_control bridge_control_table64
[] = {
715 { bridge_ioctl_add
, sizeof (struct ifbreq
),
716 BC_F_COPYIN
|BC_F_SUSER
},
717 { bridge_ioctl_del
, sizeof (struct ifbreq
),
718 BC_F_COPYIN
|BC_F_SUSER
},
720 { bridge_ioctl_gifflags
, sizeof (struct ifbreq
),
721 BC_F_COPYIN
|BC_F_COPYOUT
},
722 { bridge_ioctl_sifflags
, sizeof (struct ifbreq
),
723 BC_F_COPYIN
|BC_F_SUSER
},
725 { bridge_ioctl_scache
, sizeof (struct ifbrparam
),
726 BC_F_COPYIN
|BC_F_SUSER
},
727 { bridge_ioctl_gcache
, sizeof (struct ifbrparam
),
730 { bridge_ioctl_gifs64
, sizeof (struct ifbifconf64
),
731 BC_F_COPYIN
|BC_F_COPYOUT
},
732 { bridge_ioctl_rts64
, sizeof (struct ifbaconf64
),
733 BC_F_COPYIN
|BC_F_COPYOUT
},
735 { bridge_ioctl_saddr64
, sizeof (struct ifbareq64
),
736 BC_F_COPYIN
|BC_F_SUSER
},
738 { bridge_ioctl_sto
, sizeof (struct ifbrparam
),
739 BC_F_COPYIN
|BC_F_SUSER
},
740 { bridge_ioctl_gto
, sizeof (struct ifbrparam
),
743 { bridge_ioctl_daddr64
, sizeof (struct ifbareq64
),
744 BC_F_COPYIN
|BC_F_SUSER
},
746 { bridge_ioctl_flush
, sizeof (struct ifbreq
),
747 BC_F_COPYIN
|BC_F_SUSER
},
749 { bridge_ioctl_gpri
, sizeof (struct ifbrparam
),
751 { bridge_ioctl_spri
, sizeof (struct ifbrparam
),
752 BC_F_COPYIN
|BC_F_SUSER
},
754 { bridge_ioctl_ght
, sizeof (struct ifbrparam
),
756 { bridge_ioctl_sht
, sizeof (struct ifbrparam
),
757 BC_F_COPYIN
|BC_F_SUSER
},
759 { bridge_ioctl_gfd
, sizeof (struct ifbrparam
),
761 { bridge_ioctl_sfd
, sizeof (struct ifbrparam
),
762 BC_F_COPYIN
|BC_F_SUSER
},
764 { bridge_ioctl_gma
, sizeof (struct ifbrparam
),
766 { bridge_ioctl_sma
, sizeof (struct ifbrparam
),
767 BC_F_COPYIN
|BC_F_SUSER
},
769 { bridge_ioctl_sifprio
, sizeof (struct ifbreq
),
770 BC_F_COPYIN
|BC_F_SUSER
},
772 { bridge_ioctl_sifcost
, sizeof (struct ifbreq
),
773 BC_F_COPYIN
|BC_F_SUSER
},
775 { bridge_ioctl_gfilt
, sizeof (struct ifbrparam
),
777 { bridge_ioctl_sfilt
, sizeof (struct ifbrparam
),
778 BC_F_COPYIN
|BC_F_SUSER
},
780 { bridge_ioctl_purge
, sizeof (struct ifbreq
),
781 BC_F_COPYIN
|BC_F_SUSER
},
783 { bridge_ioctl_addspan
, sizeof (struct ifbreq
),
784 BC_F_COPYIN
|BC_F_SUSER
},
785 { bridge_ioctl_delspan
, sizeof (struct ifbreq
),
786 BC_F_COPYIN
|BC_F_SUSER
},
788 { bridge_ioctl_gbparam64
, sizeof (struct ifbropreq64
),
791 { bridge_ioctl_grte
, sizeof (struct ifbrparam
),
794 { bridge_ioctl_gifsstp64
, sizeof (struct ifbpstpconf64
),
795 BC_F_COPYIN
|BC_F_COPYOUT
},
797 { bridge_ioctl_sproto
, sizeof (struct ifbrparam
),
798 BC_F_COPYIN
|BC_F_SUSER
},
800 { bridge_ioctl_stxhc
, sizeof (struct ifbrparam
),
801 BC_F_COPYIN
|BC_F_SUSER
},
803 { bridge_ioctl_sifmaxaddr
, sizeof (struct ifbreq
),
804 BC_F_COPYIN
|BC_F_SUSER
},
807 static const unsigned int bridge_control_table_size
=
808 sizeof (bridge_control_table32
) / sizeof (bridge_control_table32
[0]);
810 static LIST_HEAD(, bridge_softc
) bridge_list
=
811 LIST_HEAD_INITIALIZER(bridge_list
);
813 static lck_grp_t
*bridge_lock_grp
= NULL
;
814 static lck_attr_t
*bridge_lock_attr
= NULL
;
816 static if_clone_t bridge_cloner
= NULL
;
818 static int if_bridge_txstart
= 0;
819 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, txstart
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
820 &if_bridge_txstart
, 0, "Bridge interface uses TXSTART model");
823 static int if_bridge_debug
= 0;
824 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
825 &if_bridge_debug
, 0, "Bridge debug");
827 static void printf_ether_header(struct ether_header
*);
828 static void printf_mbuf_data(mbuf_t
, size_t, size_t);
829 static void printf_mbuf_pkthdr(mbuf_t
, const char *, const char *);
830 static void printf_mbuf(mbuf_t
, const char *, const char *);
831 static void link_print(struct sockaddr_dl
*);
833 static void bridge_lock(struct bridge_softc
*);
834 static void bridge_unlock(struct bridge_softc
*);
835 static int bridge_lock2ref(struct bridge_softc
*);
836 static void bridge_unref(struct bridge_softc
*);
837 static void bridge_xlock(struct bridge_softc
*);
838 static void bridge_xdrop(struct bridge_softc
*);
841 bridge_lock(struct bridge_softc
*sc
)
843 void *lr_saved
= __builtin_return_address(0);
845 BRIDGE_LOCK_ASSERT_NOTHELD(sc
);
849 sc
->lock_lr
[sc
->next_lock_lr
] = lr_saved
;
850 sc
->next_lock_lr
= (sc
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
854 bridge_unlock(struct bridge_softc
*sc
)
856 void *lr_saved
= __builtin_return_address(0);
858 BRIDGE_LOCK_ASSERT_HELD(sc
);
860 sc
->unlock_lr
[sc
->next_unlock_lr
] = lr_saved
;
861 sc
->next_unlock_lr
= (sc
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
867 bridge_lock2ref(struct bridge_softc
*sc
)
870 void *lr_saved
= __builtin_return_address(0);
872 BRIDGE_LOCK_ASSERT_HELD(sc
);
874 if (sc
->sc_iflist_xcnt
> 0)
879 sc
->unlock_lr
[sc
->next_unlock_lr
] = lr_saved
;
880 sc
->next_unlock_lr
= (sc
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
888 bridge_unref(struct bridge_softc
*sc
)
890 void *lr_saved
= __builtin_return_address(0);
892 BRIDGE_LOCK_ASSERT_NOTHELD(sc
);
895 sc
->lock_lr
[sc
->next_lock_lr
] = lr_saved
;
896 sc
->next_lock_lr
= (sc
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
900 sc
->unlock_lr
[sc
->next_unlock_lr
] = lr_saved
;
901 sc
->next_unlock_lr
= (sc
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
902 if ((sc
->sc_iflist_xcnt
> 0) && (sc
->sc_iflist_ref
== 0)) {
910 bridge_xlock(struct bridge_softc
*sc
)
912 void *lr_saved
= __builtin_return_address(0);
914 BRIDGE_LOCK_ASSERT_HELD(sc
);
916 sc
->sc_iflist_xcnt
++;
917 while (sc
->sc_iflist_ref
> 0) {
918 sc
->unlock_lr
[sc
->next_unlock_lr
] = lr_saved
;
919 sc
->next_unlock_lr
= (sc
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
921 msleep(&sc
->sc_cv
, &sc
->sc_mtx
, PZERO
, "BRIDGE_XLOCK", NULL
);
923 sc
->lock_lr
[sc
->next_lock_lr
] = lr_saved
;
924 sc
->next_lock_lr
= (sc
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
929 bridge_xdrop(struct bridge_softc
*sc
)
931 BRIDGE_LOCK_ASSERT_HELD(sc
);
933 sc
->sc_iflist_xcnt
--;
937 printf_mbuf_pkthdr(mbuf_t m
, const char *prefix
, const char *suffix
)
940 printf("%spktlen: %u rcvif: 0x%llx header: 0x%llx "
942 prefix
? prefix
: "", (unsigned int)mbuf_pkthdr_len(m
),
943 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_pkthdr_rcvif(m
)),
944 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_pkthdr_header(m
)),
945 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_nextpkt(m
)),
946 suffix
? suffix
: "");
948 printf("%s<NULL>%s\n", prefix
, suffix
);
952 printf_mbuf(mbuf_t m
, const char *prefix
, const char *suffix
)
955 printf("%s0x%llx type: %u flags: 0x%x len: %u data: 0x%llx "
956 "maxlen: %u datastart: 0x%llx next: 0x%llx%s",
957 prefix
? prefix
: "", (uint64_t)VM_KERNEL_ADDRPERM(m
),
958 mbuf_type(m
), mbuf_flags(m
), (unsigned int)mbuf_len(m
),
959 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m
)),
960 (unsigned int)mbuf_maxlen(m
),
961 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_datastart(m
)),
962 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_next(m
)),
963 !suffix
|| (mbuf_flags(m
) & MBUF_PKTHDR
) ? "" : suffix
);
964 if ((mbuf_flags(m
) & MBUF_PKTHDR
))
965 printf_mbuf_pkthdr(m
, " ", suffix
);
967 printf("%s<NULL>%s\n", prefix
, suffix
);
971 printf_mbuf_data(mbuf_t m
, size_t offset
, size_t len
)
975 size_t pktlen
, mlen
, maxlen
;
978 pktlen
= mbuf_pkthdr_len(m
);
983 maxlen
= (pktlen
- offset
> len
) ? len
: pktlen
;
987 for (i
= 0, j
= 0; i
< maxlen
; i
++, j
++) {
997 printf("%02x%s", ptr
[j
], i
% 2 ? " " : "");
1003 printf_ether_header(struct ether_header
*eh
)
1005 printf("%02x:%02x:%02x:%02x:%02x:%02x > "
1006 "%02x:%02x:%02x:%02x:%02x:%02x 0x%04x ",
1007 eh
->ether_shost
[0], eh
->ether_shost
[1], eh
->ether_shost
[2],
1008 eh
->ether_shost
[3], eh
->ether_shost
[4], eh
->ether_shost
[5],
1009 eh
->ether_dhost
[0], eh
->ether_dhost
[1], eh
->ether_dhost
[2],
1010 eh
->ether_dhost
[3], eh
->ether_dhost
[4], eh
->ether_dhost
[5],
1011 ntohs(eh
->ether_type
));
1015 link_print(struct sockaddr_dl
*dl_p
)
1020 printf("sdl len %d index %d family %d type 0x%x nlen %d alen %d"
1021 " slen %d addr ", dl_p
->sdl_len
, dl_p
->sdl_index
,
1022 dl_p
->sdl_family
, dl_p
->sdl_type
, dl_p
->sdl_nlen
,
1023 dl_p
->sdl_alen
, dl_p
->sdl_slen
);
1025 for (i
= 0; i
< dl_p
->sdl_alen
; i
++)
1026 printf("%s%x", i
? ":" : "", (CONST_LLADDR(dl_p
))[i
]);
1030 #endif /* BRIDGE_DEBUG */
1035 * Pseudo-device attach routine.
1037 __private_extern__
int
1042 lck_grp_attr_t
*lck_grp_attr
= NULL
;
1043 struct ifnet_clone_params ifnet_clone_params
;
1045 bridge_rtnode_pool
= zinit(sizeof (struct bridge_rtnode
),
1046 1024 * sizeof (struct bridge_rtnode
), 0, "bridge_rtnode");
1047 zone_change(bridge_rtnode_pool
, Z_CALLERACCT
, FALSE
);
1049 lck_grp_attr
= lck_grp_attr_alloc_init();
1051 bridge_lock_grp
= lck_grp_alloc_init("if_bridge", lck_grp_attr
);
1053 bridge_lock_attr
= lck_attr_alloc_init();
1056 lck_attr_setdebug(bridge_lock_attr
);
1059 lck_mtx_init(&bridge_list_mtx
, bridge_lock_grp
, bridge_lock_attr
);
1061 /* can free the attributes once we've allocated the group lock */
1062 lck_grp_attr_free(lck_grp_attr
);
1064 LIST_INIT(&bridge_list
);
1068 #endif /* BRIDGESTP */
1070 ifnet_clone_params
.ifc_name
= "bridge";
1071 ifnet_clone_params
.ifc_create
= bridge_clone_create
;
1072 ifnet_clone_params
.ifc_destroy
= bridge_clone_destroy
;
1074 error
= ifnet_clone_attach(&ifnet_clone_params
, &bridge_cloner
);
1076 printf("%s: ifnet_clone_attach failed %d\n", __func__
, error
);
1081 #if defined(PFIL_HOOKS)
1083 * handler for net.link.bridge.pfil_ipfw
1086 sysctl_pfil_ipfw SYSCTL_HANDLER_ARGS
1088 #pragma unused(arg1, arg2)
1089 int enable
= pfil_ipfw
;
1092 error
= sysctl_handle_int(oidp
, &enable
, 0, req
);
1093 enable
= (enable
) ? 1 : 0;
1095 if (enable
!= pfil_ipfw
) {
1099 * Disable pfil so that ipfw doesnt run twice, if the user
1100 * really wants both then they can re-enable pfil_bridge and/or
1101 * pfil_member. Also allow non-ip packets as ipfw can filter by
1114 SYSCTL_PROC(_net_link_bridge
, OID_AUTO
, ipfw
, CTLTYPE_INT
|CTLFLAG_RW
,
1115 &pfil_ipfw
, 0, &sysctl_pfil_ipfw
, "I", "Layer2 filter with IPFW");
1116 #endif /* PFIL_HOOKS */
1119 * bridge_clone_create:
1121 * Create a new bridge instance.
1124 bridge_clone_create(struct if_clone
*ifc
, uint32_t unit
, void *params
)
1126 #pragma unused(params)
1127 struct ifnet
*ifp
= NULL
;
1128 struct bridge_softc
*sc
, *sc2
;
1129 struct ifnet_init_eparams init_params
;
1131 uint32_t sdl_buffer
[offsetof(struct sockaddr_dl
, sdl_data
) +
1132 IFNAMSIZ
+ ETHER_ADDR_LEN
];
1133 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sdl_buffer
;
1134 uint8_t eth_hostid
[ETHER_ADDR_LEN
];
1135 int fb
, retry
, has_hostid
;
1137 sc
= _MALLOC(sizeof (*sc
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
1139 lck_mtx_init(&sc
->sc_mtx
, bridge_lock_grp
, bridge_lock_attr
);
1140 sc
->sc_brtmax
= BRIDGE_RTABLE_MAX
;
1141 sc
->sc_brttimeout
= BRIDGE_RTABLE_TIMEOUT
;
1142 sc
->sc_filter_flags
= IFBF_FILT_DEFAULT
;
1145 * For backwards compatibility with previous behaviour...
1146 * Switch off filtering on the bridge itself if BRIDGE_IPF is
1149 sc
->sc_filter_flags
&= ~IFBF_FILT_USEIPF
;
1152 /* Initialize our routing table. */
1153 error
= bridge_rtable_init(sc
);
1155 printf("%s: bridge_rtable_init failed %d\n", __func__
, error
);
1159 TAILQ_INIT(&sc
->sc_iflist
);
1160 TAILQ_INIT(&sc
->sc_spanlist
);
1162 /* use the interface name as the unique id for ifp recycle */
1163 snprintf(sc
->sc_if_xname
, sizeof (sc
->sc_if_xname
), "%s%d",
1164 ifc
->ifc_name
, unit
);
1165 bzero(&init_params
, sizeof (init_params
));
1166 init_params
.ver
= IFNET_INIT_CURRENT_VERSION
;
1167 init_params
.len
= sizeof (init_params
);
1168 if (if_bridge_txstart
) {
1169 init_params
.start
= bridge_start
;
1171 init_params
.flags
= IFNET_INIT_LEGACY
;
1172 init_params
.output
= bridge_output
;
1174 init_params
.uniqueid
= sc
->sc_if_xname
;
1175 init_params
.uniqueid_len
= strlen(sc
->sc_if_xname
);
1176 init_params
.sndq_maxlen
= IFQ_MAXLEN
;
1177 init_params
.name
= ifc
->ifc_name
;
1178 init_params
.unit
= unit
;
1179 init_params
.family
= IFNET_FAMILY_ETHERNET
;
1180 init_params
.type
= IFT_BRIDGE
;
1181 init_params
.demux
= ether_demux
;
1182 init_params
.add_proto
= ether_add_proto
;
1183 init_params
.del_proto
= ether_del_proto
;
1184 init_params
.check_multi
= ether_check_multi
;
1185 init_params
.framer_extended
= ether_frameout_extended
;
1186 init_params
.softc
= sc
;
1187 init_params
.ioctl
= bridge_ioctl
;
1188 init_params
.set_bpf_tap
= bridge_set_bpf_tap
;
1189 init_params
.detach
= bridge_detach
;
1190 init_params
.broadcast_addr
= etherbroadcastaddr
;
1191 init_params
.broadcast_len
= ETHER_ADDR_LEN
;
1192 error
= ifnet_allocate_extended(&init_params
, &ifp
);
1194 printf("%s: ifnet_allocate failed %d\n", __func__
, error
);
1199 error
= ifnet_set_mtu(ifp
, ETHERMTU
);
1201 printf("%s: ifnet_set_mtu failed %d\n", __func__
, error
);
1204 error
= ifnet_set_addrlen(ifp
, ETHER_ADDR_LEN
);
1206 printf("%s: ifnet_set_addrlen failed %d\n", __func__
, error
);
1209 error
= ifnet_set_hdrlen(ifp
, ETHER_HDR_LEN
);
1211 printf("%s: ifnet_set_hdrlen failed %d\n", __func__
, error
);
1214 error
= ifnet_set_flags(ifp
,
1215 IFF_BROADCAST
| IFF_SIMPLEX
| IFF_NOTRAILERS
| IFF_MULTICAST
,
1218 printf("%s: ifnet_set_flags failed %d\n", __func__
, error
);
1223 * Generate an ethernet address with a locally administered address.
1225 * Since we are using random ethernet addresses for the bridge, it is
1226 * possible that we might have address collisions, so make sure that
1227 * this hardware address isn't already in use on another bridge.
1228 * The first try uses the "hostid" and falls back to read_random();
1229 * for "hostid", we use the MAC address of the first-encountered
1230 * Ethernet-type interface that is currently configured.
1233 has_hostid
= (uuid_get_ethernet(ð_hostid
[0]) == 0);
1234 for (retry
= 1; retry
!= 0; ) {
1235 if (fb
|| has_hostid
== 0) {
1236 read_random(&sc
->sc_defaddr
, ETHER_ADDR_LEN
);
1237 sc
->sc_defaddr
[0] &= ~1; /* clear multicast bit */
1238 sc
->sc_defaddr
[0] |= 2; /* set the LAA bit */
1240 bcopy(ð_hostid
[0], &sc
->sc_defaddr
,
1242 sc
->sc_defaddr
[0] &= ~1; /* clear multicast bit */
1243 sc
->sc_defaddr
[0] |= 2; /* set the LAA bit */
1244 sc
->sc_defaddr
[3] = /* stir it up a bit */
1245 ((sc
->sc_defaddr
[3] & 0x0f) << 4) |
1246 ((sc
->sc_defaddr
[3] & 0xf0) >> 4);
1248 * Mix in the LSB as it's actually pretty significant,
1249 * see rdar://14076061
1252 (((sc
->sc_defaddr
[4] & 0x0f) << 4) |
1253 ((sc
->sc_defaddr
[4] & 0xf0) >> 4)) ^
1255 sc
->sc_defaddr
[5] = ifp
->if_unit
& 0xff;
1260 lck_mtx_lock(&bridge_list_mtx
);
1261 LIST_FOREACH(sc2
, &bridge_list
, sc_list
) {
1262 if (memcmp(sc
->sc_defaddr
,
1263 IF_LLADDR(sc2
->sc_ifp
), ETHER_ADDR_LEN
) == 0)
1266 lck_mtx_unlock(&bridge_list_mtx
);
1269 memset(sdl
, 0, sizeof (sdl_buffer
));
1270 sdl
->sdl_family
= AF_LINK
;
1271 sdl
->sdl_nlen
= strlen(sc
->sc_if_xname
);
1272 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
1273 sdl
->sdl_len
= offsetof(struct sockaddr_dl
, sdl_data
);
1274 memcpy(sdl
->sdl_data
, sc
->sc_if_xname
, sdl
->sdl_nlen
);
1275 memcpy(LLADDR(sdl
), sc
->sc_defaddr
, ETHER_ADDR_LEN
);
1277 sc
->sc_flags
&= ~SCF_MEDIA_ACTIVE
;
1280 if (if_bridge_debug
& BR_DBGF_LIFECYCLE
)
1284 error
= ifnet_attach(ifp
, NULL
);
1286 printf("%s: ifnet_attach failed %d\n", __func__
, error
);
1290 error
= ifnet_set_lladdr_and_type(ifp
, sc
->sc_defaddr
, ETHER_ADDR_LEN
,
1293 printf("%s: ifnet_set_lladdr_and_type failed %d\n", __func__
,
1298 ifnet_set_offload(ifp
,
1299 IFNET_CSUM_IP
| IFNET_CSUM_TCP
| IFNET_CSUM_UDP
|
1300 IFNET_CSUM_TCPIPV6
| IFNET_CSUM_UDPIPV6
| IFNET_MULTIPAGES
);
1302 error
= bridge_set_tso(sc
);
1304 printf("%s: bridge_set_tso failed %d\n", __func__
, error
);
1309 bstp_attach(&sc
->sc_stp
, &bridge_ops
);
1310 #endif /* BRIDGESTP */
1312 lck_mtx_lock(&bridge_list_mtx
);
1313 LIST_INSERT_HEAD(&bridge_list
, sc
, sc_list
);
1314 lck_mtx_unlock(&bridge_list_mtx
);
1316 /* attach as ethernet */
1317 error
= bpf_attach(ifp
, DLT_EN10MB
, sizeof (struct ether_header
),
1322 printf("%s failed error %d\n", __func__
, error
);
1330 * bridge_clone_destroy:
1332 * Destroy a bridge instance.
1335 bridge_clone_destroy(struct ifnet
*ifp
)
1337 struct bridge_softc
*sc
= ifp
->if_softc
;
1338 struct bridge_iflist
*bif
;
1342 if ((sc
->sc_flags
& SCF_DETACHING
)) {
1346 sc
->sc_flags
|= SCF_DETACHING
;
1348 bridge_ifstop(ifp
, 1);
1350 bridge_cancel_delayed_call(&sc
->sc_resize_call
);
1352 error
= ifnet_set_flags(ifp
, 0, IFF_UP
);
1354 printf("%s: ifnet_set_flags failed %d\n", __func__
, error
);
1357 while ((bif
= TAILQ_FIRST(&sc
->sc_iflist
)) != NULL
)
1358 bridge_delete_member(sc
, bif
, 0);
1360 while ((bif
= TAILQ_FIRST(&sc
->sc_spanlist
)) != NULL
) {
1361 bridge_delete_span(sc
, bif
);
1366 error
= ifnet_detach(ifp
);
1368 panic("bridge_clone_destroy: ifnet_detach(%p) failed %d\n",
1370 if ((sc
= (struct bridge_softc
*)ifnet_softc(ifp
)) != NULL
) {
1372 sc
->sc_flags
&= ~SCF_DETACHING
;
1381 #define DRVSPEC do { \
1382 if (ifd->ifd_cmd >= bridge_control_table_size) { \
1386 bc = &bridge_control_table[ifd->ifd_cmd]; \
1388 if (cmd == SIOCGDRVSPEC && \
1389 (bc->bc_flags & BC_F_COPYOUT) == 0) { \
1392 } else if (cmd == SIOCSDRVSPEC && \
1393 (bc->bc_flags & BC_F_COPYOUT) != 0) { \
1398 if (bc->bc_flags & BC_F_SUSER) { \
1399 error = kauth_authorize_generic(kauth_cred_get(), \
1400 KAUTH_GENERIC_ISSUSER); \
1405 if (ifd->ifd_len != bc->bc_argsize || \
1406 ifd->ifd_len > sizeof (args)) { \
1411 bzero(&args, sizeof (args)); \
1412 if (bc->bc_flags & BC_F_COPYIN) { \
1413 error = copyin(ifd->ifd_data, &args, ifd->ifd_len); \
1419 error = (*bc->bc_func)(sc, &args); \
1420 BRIDGE_UNLOCK(sc); \
1424 if (bc->bc_flags & BC_F_COPYOUT) \
1425 error = copyout(&args, ifd->ifd_data, ifd->ifd_len); \
1431 * Handle a control request from the operator.
1434 bridge_ioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
1436 struct bridge_softc
*sc
= ifp
->if_softc
;
1437 struct ifreq
*ifr
= (struct ifreq
*)data
;
1438 struct bridge_iflist
*bif
;
1441 BRIDGE_LOCK_ASSERT_NOTHELD(sc
);
1444 if (if_bridge_debug
& BR_DBGF_IOCTL
)
1445 printf("%s: ifp %s cmd 0x%08lx (%c%c [%lu] %c %lu)\n",
1446 __func__
, ifp
->if_xname
, cmd
, (cmd
& IOC_IN
) ? 'I' : ' ',
1447 (cmd
& IOC_OUT
) ? 'O' : ' ', IOCPARM_LEN(cmd
),
1448 (char)IOCGROUP(cmd
), cmd
& 0xff);
1449 #endif /* BRIDGE_DEBUG */
1455 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
1458 case SIOCGIFMEDIA32
:
1459 case SIOCGIFMEDIA64
: {
1460 struct ifmediareq
*ifmr
= (struct ifmediareq
*)data
;
1461 user_addr_t user_addr
;
1463 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
1464 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
1465 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
1467 ifmr
->ifm_status
= IFM_AVALID
;
1469 ifmr
->ifm_count
= 1;
1472 if (!(sc
->sc_flags
& SCF_DETACHING
) &&
1473 (sc
->sc_flags
& SCF_MEDIA_ACTIVE
)) {
1474 ifmr
->ifm_status
|= IFM_ACTIVE
;
1475 ifmr
->ifm_active
= ifmr
->ifm_current
=
1476 IFM_ETHER
| IFM_AUTO
;
1478 ifmr
->ifm_active
= ifmr
->ifm_current
= IFM_NONE
;
1482 if (user_addr
!= USER_ADDR_NULL
) {
1483 error
= copyout(&ifmr
->ifm_current
, user_addr
,
1493 case SIOCSDRVSPEC32
:
1494 case SIOCGDRVSPEC32
: {
1496 struct ifbreq ifbreq
;
1497 struct ifbifconf32 ifbifconf
;
1498 struct ifbareq32 ifbareq
;
1499 struct ifbaconf32 ifbaconf
;
1500 struct ifbrparam ifbrparam
;
1501 struct ifbropreq32 ifbropreq
;
1503 struct ifdrv32
*ifd
= (struct ifdrv32
*)data
;
1504 const struct bridge_control
*bridge_control_table
=
1505 bridge_control_table32
, *bc
;
1511 case SIOCSDRVSPEC64
:
1512 case SIOCGDRVSPEC64
: {
1514 struct ifbreq ifbreq
;
1515 struct ifbifconf64 ifbifconf
;
1516 struct ifbareq64 ifbareq
;
1517 struct ifbaconf64 ifbaconf
;
1518 struct ifbrparam ifbrparam
;
1519 struct ifbropreq64 ifbropreq
;
1521 struct ifdrv64
*ifd
= (struct ifdrv64
*)data
;
1522 const struct bridge_control
*bridge_control_table
=
1523 bridge_control_table64
, *bc
;
1531 if (!(ifp
->if_flags
& IFF_UP
) &&
1532 (ifp
->if_flags
& IFF_RUNNING
)) {
1534 * If interface is marked down and it is running,
1535 * then stop and disable it.
1538 bridge_ifstop(ifp
, 1);
1540 } else if ((ifp
->if_flags
& IFF_UP
) &&
1541 !(ifp
->if_flags
& IFF_RUNNING
)) {
1543 * If interface is marked up and it is stopped, then
1547 error
= bridge_init(ifp
);
1553 error
= ifnet_set_lladdr(ifp
, ifr
->ifr_addr
.sa_data
,
1554 ifr
->ifr_addr
.sa_len
);
1556 printf("%s: SIOCSIFLLADDR error %d\n", ifp
->if_xname
,
1561 if (ifr
->ifr_mtu
< 576) {
1566 if (TAILQ_EMPTY(&sc
->sc_iflist
)) {
1567 sc
->sc_ifp
->if_mtu
= ifr
->ifr_mtu
;
1571 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1572 if (bif
->bif_ifp
->if_mtu
!= (unsigned)ifr
->ifr_mtu
) {
1573 printf("%s: invalid MTU: %u(%s) != %d\n",
1574 sc
->sc_ifp
->if_xname
,
1575 bif
->bif_ifp
->if_mtu
,
1576 bif
->bif_ifp
->if_xname
, ifr
->ifr_mtu
);
1582 sc
->sc_ifp
->if_mtu
= ifr
->ifr_mtu
;
1587 error
= ether_ioctl(ifp
, cmd
, data
);
1589 if (error
!= 0 && error
!= EOPNOTSUPP
)
1590 printf("%s: ifp %s cmd 0x%08lx "
1591 "(%c%c [%lu] %c %lu) failed error: %d\n",
1592 __func__
, ifp
->if_xname
, cmd
,
1593 (cmd
& IOC_IN
) ? 'I' : ' ',
1594 (cmd
& IOC_OUT
) ? 'O' : ' ',
1595 IOCPARM_LEN(cmd
), (char)IOCGROUP(cmd
),
1597 #endif /* BRIDGE_DEBUG */
1600 BRIDGE_LOCK_ASSERT_NOTHELD(sc
);
1609 * Clear or restore unwanted capabilities on the member interface
1612 bridge_mutecaps(struct bridge_softc
*sc
)
1614 struct bridge_iflist
*bif
;
1617 /* Initial bitmask of capabilities to test */
1618 mask
= BRIDGE_IFCAPS_MASK
;
1620 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1621 /* Every member must support it or its disabled */
1622 mask
&= bif
->bif_savedcaps
;
1625 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1626 enabled
= bif
->bif_ifp
->if_capenable
;
1627 enabled
&= ~BRIDGE_IFCAPS_STRIP
;
1628 /* strip off mask bits and enable them again if allowed */
1629 enabled
&= ~BRIDGE_IFCAPS_MASK
;
1632 bridge_set_ifcap(sc
, bif
, enabled
);
1638 bridge_set_ifcap(struct bridge_softc
*sc
, struct bridge_iflist
*bif
, int set
)
1640 struct ifnet
*ifp
= bif
->bif_ifp
;
1644 bzero(&ifr
, sizeof (ifr
));
1645 ifr
.ifr_reqcap
= set
;
1647 if (ifp
->if_capenable
!= set
) {
1649 error
= (*ifp
->if_ioctl
)(ifp
, SIOCSIFCAP
, (caddr_t
)&ifr
);
1650 IFF_UNLOCKGIANT(ifp
);
1652 printf("%s: %s error setting interface capabilities "
1653 "on %s\n", __func__
, sc
->sc_ifp
->if_xname
,
1657 #endif /* HAS_IF_CAP */
1660 bridge_set_tso(struct bridge_softc
*sc
)
1662 struct bridge_iflist
*bif
;
1663 u_int32_t tso_v4_mtu
;
1664 u_int32_t tso_v6_mtu
;
1665 ifnet_offload_t offload
;
1668 /* By default, support TSO */
1669 offload
= sc
->sc_ifp
->if_hwassist
| IFNET_TSO_IPV4
| IFNET_TSO_IPV6
;
1670 tso_v4_mtu
= IP_MAXPACKET
;
1671 tso_v6_mtu
= IP_MAXPACKET
;
1673 /* Use the lowest common denominator of the members */
1674 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1675 ifnet_t ifp
= bif
->bif_ifp
;
1680 if (offload
& IFNET_TSO_IPV4
) {
1681 if (ifp
->if_hwassist
& IFNET_TSO_IPV4
) {
1682 if (tso_v4_mtu
> ifp
->if_tso_v4_mtu
)
1683 tso_v4_mtu
= ifp
->if_tso_v4_mtu
;
1685 offload
&= ~IFNET_TSO_IPV4
;
1689 if (offload
& IFNET_TSO_IPV6
) {
1690 if (ifp
->if_hwassist
& IFNET_TSO_IPV6
) {
1691 if (tso_v6_mtu
> ifp
->if_tso_v6_mtu
)
1692 tso_v6_mtu
= ifp
->if_tso_v6_mtu
;
1694 offload
&= ~IFNET_TSO_IPV6
;
1700 if (offload
!= sc
->sc_ifp
->if_hwassist
) {
1701 error
= ifnet_set_offload(sc
->sc_ifp
, offload
);
1704 if (if_bridge_debug
& BR_DBGF_LIFECYCLE
)
1705 printf("%s: ifnet_set_offload(%s, 0x%x) "
1706 "failed %d\n", __func__
,
1707 sc
->sc_ifp
->if_xname
, offload
, error
);
1708 #endif /* BRIDGE_DEBUG */
1712 * For ifnet_set_tso_mtu() sake, the TSO MTU must be at least
1713 * as large as the interface MTU
1715 if (sc
->sc_ifp
->if_hwassist
& IFNET_TSO_IPV4
) {
1716 if (tso_v4_mtu
< sc
->sc_ifp
->if_mtu
)
1717 tso_v4_mtu
= sc
->sc_ifp
->if_mtu
;
1718 error
= ifnet_set_tso_mtu(sc
->sc_ifp
, AF_INET
,
1722 if (if_bridge_debug
& BR_DBGF_LIFECYCLE
)
1723 printf("%s: ifnet_set_tso_mtu(%s, "
1724 "AF_INET, %u) failed %d\n",
1725 __func__
, sc
->sc_ifp
->if_xname
,
1727 #endif /* BRIDGE_DEBUG */
1731 if (sc
->sc_ifp
->if_hwassist
& IFNET_TSO_IPV6
) {
1732 if (tso_v6_mtu
< sc
->sc_ifp
->if_mtu
)
1733 tso_v6_mtu
= sc
->sc_ifp
->if_mtu
;
1734 error
= ifnet_set_tso_mtu(sc
->sc_ifp
, AF_INET6
,
1738 if (if_bridge_debug
& BR_DBGF_LIFECYCLE
)
1739 printf("%s: ifnet_set_tso_mtu(%s, "
1740 "AF_INET6, %u) failed %d\n",
1741 __func__
, sc
->sc_ifp
->if_xname
,
1743 #endif /* BRIDGE_DEBUG */
1753 * bridge_lookup_member:
1755 * Lookup a bridge member interface.
1757 static struct bridge_iflist
*
1758 bridge_lookup_member(struct bridge_softc
*sc
, const char *name
)
1760 struct bridge_iflist
*bif
;
1763 BRIDGE_LOCK_ASSERT_HELD(sc
);
1765 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1767 if (strcmp(ifp
->if_xname
, name
) == 0)
1775 * bridge_lookup_member_if:
1777 * Lookup a bridge member interface by ifnet*.
1779 static struct bridge_iflist
*
1780 bridge_lookup_member_if(struct bridge_softc
*sc
, struct ifnet
*member_ifp
)
1782 struct bridge_iflist
*bif
;
1784 BRIDGE_LOCK_ASSERT_HELD(sc
);
1786 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1787 if (bif
->bif_ifp
== member_ifp
)
1795 bridge_iff_input(void *cookie
, ifnet_t ifp
, protocol_family_t protocol
,
1796 mbuf_t
*data
, char **frame_ptr
)
1798 #pragma unused(protocol)
1800 struct bridge_iflist
*bif
= (struct bridge_iflist
*)cookie
;
1801 struct bridge_softc
*sc
= bif
->bif_sc
;
1806 if ((m
->m_flags
& M_PROTO1
))
1809 if (*frame_ptr
>= (char *)mbuf_datastart(m
) &&
1810 *frame_ptr
<= (char *)mbuf_data(m
)) {
1812 frmlen
= (char *)mbuf_data(m
) - *frame_ptr
;
1815 if (if_bridge_debug
& BR_DBGF_INPUT
) {
1816 printf("%s: %s from %s m 0x%llx data 0x%llx frame 0x%llx %s "
1817 "frmlen %lu\n", __func__
, sc
->sc_ifp
->if_xname
,
1818 ifp
->if_xname
, (uint64_t)VM_KERNEL_ADDRPERM(m
),
1819 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m
)),
1820 (uint64_t)VM_KERNEL_ADDRPERM(*frame_ptr
),
1821 included
? "inside" : "outside", frmlen
);
1823 if (if_bridge_debug
& BR_DBGF_MBUF
) {
1824 printf_mbuf(m
, "bridge_iff_input[", "\n");
1825 printf_ether_header((struct ether_header
*)
1826 (void *)*frame_ptr
);
1827 printf_mbuf_data(m
, 0, 20);
1831 #endif /* BRIDGE_DEBUG */
1833 /* Move data pointer to start of frame to the link layer header */
1835 (void) mbuf_setdata(m
, (char *)mbuf_data(m
) - frmlen
,
1836 mbuf_len(m
) + frmlen
);
1837 (void) mbuf_pkthdr_adjustlen(m
, frmlen
);
1839 printf("%s: frame_ptr outside mbuf\n", __func__
);
1843 error
= bridge_input(ifp
, m
, *frame_ptr
);
1845 /* Adjust packet back to original */
1847 (void) mbuf_setdata(m
, (char *)mbuf_data(m
) + frmlen
,
1848 mbuf_len(m
) - frmlen
);
1849 (void) mbuf_pkthdr_adjustlen(m
, -frmlen
);
1852 if ((if_bridge_debug
& BR_DBGF_INPUT
) &&
1853 (if_bridge_debug
& BR_DBGF_MBUF
)) {
1855 printf_mbuf(m
, "bridge_iff_input]", "\n");
1857 #endif /* BRIDGE_DEBUG */
1860 BRIDGE_LOCK_ASSERT_NOTHELD(sc
);
1865 #if BRIDGE_MEMBER_OUT_FILTER
1867 bridge_iff_output(void *cookie
, ifnet_t ifp
, protocol_family_t protocol
,
1870 #pragma unused(protocol)
1872 struct bridge_iflist
*bif
= (struct bridge_iflist
*)cookie
;
1873 struct bridge_softc
*sc
= bif
->bif_sc
;
1876 if ((m
->m_flags
& M_PROTO1
))
1880 if (if_bridge_debug
& BR_DBGF_OUTPPUT
) {
1881 printf("%s: %s from %s m 0x%llx data 0x%llx\n", __func__
,
1882 sc
->sc_ifp
->if_xname
, ifp
->if_xname
,
1883 (uint64_t)VM_KERNEL_ADDRPERM(m
),
1884 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m
)));
1886 #endif /* BRIDGE_DEBUG */
1888 error
= bridge_member_output(sc
, ifp
, m
);
1890 printf("%s: bridge_member_output failed error %d\n", __func__
,
1895 BRIDGE_LOCK_ASSERT_NOTHELD(sc
);
1899 #endif /* BRIDGE_MEMBER_OUT_FILTER */
1902 bridge_iff_event(void *cookie
, ifnet_t ifp
, protocol_family_t protocol
,
1903 const struct kev_msg
*event_msg
)
1905 #pragma unused(protocol)
1906 struct bridge_iflist
*bif
= (struct bridge_iflist
*)cookie
;
1907 struct bridge_softc
*sc
= bif
->bif_sc
;
1909 if (event_msg
->vendor_code
== KEV_VENDOR_APPLE
&&
1910 event_msg
->kev_class
== KEV_NETWORK_CLASS
&&
1911 event_msg
->kev_subclass
== KEV_DL_SUBCLASS
) {
1913 if (if_bridge_debug
& BR_DBGF_LIFECYCLE
)
1914 printf("%s: %s event_code %u - %s\n", __func__
,
1915 ifp
->if_xname
, event_msg
->event_code
,
1916 dlil_kev_dl_code_str(event_msg
->event_code
));
1917 #endif /* BRIDGE_DEBUG */
1919 switch (event_msg
->event_code
) {
1920 case KEV_DL_IF_DETACHING
:
1921 case KEV_DL_IF_DETACHED
: {
1922 bridge_ifdetach(bif
, ifp
);
1925 case KEV_DL_LINK_OFF
:
1926 case KEV_DL_LINK_ON
: {
1927 bridge_iflinkevent(ifp
);
1929 bstp_linkstate(ifp
, event_msg
->event_code
);
1930 #endif /* BRIDGESTP */
1933 case KEV_DL_SIFFLAGS
: {
1934 if ((bif
->bif_flags
& BIFF_PROMISC
) == 0 &&
1935 (ifp
->if_flags
& IFF_UP
)) {
1938 error
= ifnet_set_promiscuous(ifp
, 1);
1941 "ifnet_set_promiscuous (%s)"
1943 __func__
, ifp
->if_xname
,
1946 bif
->bif_flags
|= BIFF_PROMISC
;
1951 case KEV_DL_IFCAP_CHANGED
: {
1964 * bridge_iff_detached:
1966 * Detach an interface from a bridge. Called when a member
1967 * interface is detaching.
1970 bridge_iff_detached(void *cookie
, ifnet_t ifp
)
1972 struct bridge_iflist
*bif
= (struct bridge_iflist
*)cookie
;
1975 if (if_bridge_debug
& BR_DBGF_LIFECYCLE
)
1976 printf("%s: %s\n", __func__
, ifp
->if_xname
);
1977 #endif /* BRIDGE_DEBUG */
1979 bridge_ifdetach(bif
, ifp
);
1981 _FREE(bif
, M_DEVBUF
);
1985 bridge_proto_input(ifnet_t ifp
, protocol_family_t protocol
, mbuf_t packet
,
1988 #pragma unused(protocol, packet, header)
1990 printf("%s: unexpected packet from %s\n", __func__
,
1992 #endif /* BRIDGE_DEBUG */
1997 bridge_attach_protocol(struct ifnet
*ifp
)
2000 struct ifnet_attach_proto_param reg
;
2003 if (if_bridge_debug
& BR_DBGF_LIFECYCLE
)
2004 printf("%s: %s\n", __func__
, ifp
->if_xname
);
2005 #endif /* BRIDGE_DEBUG */
2007 bzero(®
, sizeof (reg
));
2008 reg
.input
= bridge_proto_input
;
2010 error
= ifnet_attach_protocol(ifp
, PF_BRIDGE
, ®
);
2012 printf("%s: ifnet_attach_protocol(%s) failed, %d\n",
2013 __func__
, ifp
->if_xname
, error
);
2019 bridge_detach_protocol(struct ifnet
*ifp
)
2024 if (if_bridge_debug
& BR_DBGF_LIFECYCLE
)
2025 printf("%s: %s\n", __func__
, ifp
->if_xname
);
2026 #endif /* BRIDGE_DEBUG */
2027 error
= ifnet_detach_protocol(ifp
, PF_BRIDGE
);
2029 printf("%s: ifnet_detach_protocol(%s) failed, %d\n",
2030 __func__
, ifp
->if_xname
, error
);
2036 * bridge_delete_member:
2038 * Delete the specified member interface.
2041 bridge_delete_member(struct bridge_softc
*sc
, struct bridge_iflist
*bif
,
2044 struct ifnet
*ifs
= bif
->bif_ifp
, *bifp
= sc
->sc_ifp
;
2045 int lladdr_changed
= 0, error
, filt_attached
;
2046 uint8_t eaddr
[ETHER_ADDR_LEN
];
2047 u_int32_t event_code
= 0;
2049 BRIDGE_LOCK_ASSERT_HELD(sc
);
2050 VERIFY(ifs
!= NULL
);
2053 switch (ifs
->if_type
) {
2057 * Take the interface out of promiscuous mode.
2059 if (bif
->bif_flags
& BIFF_PROMISC
)
2060 (void) ifnet_set_promiscuous(ifs
, 0);
2064 /* currently not supported */
2072 /* reneable any interface capabilities */
2073 bridge_set_ifcap(sc
, bif
, bif
->bif_savedcaps
);
2077 if (bif
->bif_flags
& BIFF_PROTO_ATTACHED
) {
2078 /* Respect lock ordering with DLIL lock */
2080 (void) bridge_detach_protocol(ifs
);
2084 if (bif
->bif_ifflags
& IFBIF_STP
)
2085 bstp_disable(&bif
->bif_stp
);
2086 #endif /* BRIDGESTP */
2089 TAILQ_REMOVE(&sc
->sc_iflist
, bif
, bif_next
);
2093 * If removing the interface that gave the bridge its mac address, set
2094 * the mac address of the bridge to the address of the next member, or
2095 * to its default address if no members are left.
2097 if (bridge_inherit_mac
&& sc
->sc_ifaddr
== ifs
) {
2098 ifnet_release(sc
->sc_ifaddr
);
2099 if (TAILQ_EMPTY(&sc
->sc_iflist
)) {
2100 bcopy(sc
->sc_defaddr
, eaddr
, ETHER_ADDR_LEN
);
2101 sc
->sc_ifaddr
= NULL
;
2104 TAILQ_FIRST(&sc
->sc_iflist
)->bif_ifp
;
2105 bcopy(IF_LLADDR(fif
), eaddr
, ETHER_ADDR_LEN
);
2106 sc
->sc_ifaddr
= fif
;
2107 ifnet_reference(fif
); /* for sc_ifaddr */
2113 bridge_mutecaps(sc
); /* recalculate now this interface is removed */
2114 #endif /* HAS_IF_CAP */
2116 error
= bridge_set_tso(sc
);
2118 printf("%s: bridge_set_tso failed %d\n", __func__
, error
);
2121 bridge_rtdelete(sc
, ifs
, IFBF_FLUSHALL
);
2122 KASSERT(bif
->bif_addrcnt
== 0,
2123 ("%s: %d bridge routes referenced", __func__
, bif
->bif_addrcnt
));
2125 filt_attached
= bif
->bif_flags
& BIFF_FILTER_ATTACHED
;
2128 * Update link status of the bridge based on its remaining members
2130 event_code
= bridge_updatelinkstatus(sc
);
2134 if (lladdr_changed
&&
2135 (error
= ifnet_set_lladdr(bifp
, eaddr
, ETHER_ADDR_LEN
)) != 0)
2136 printf("%s: ifnet_set_lladdr failed %d\n", __func__
, error
);
2138 if (event_code
!= 0)
2139 bridge_link_event(bifp
, event_code
);
2142 bstp_destroy(&bif
->bif_stp
); /* prepare to free */
2143 #endif /* BRIDGESTP */
2146 iflt_detach(bif
->bif_iff_ref
);
2148 _FREE(bif
, M_DEVBUF
);
2150 ifs
->if_bridge
= NULL
;
2157 * bridge_delete_span:
2159 * Delete the specified span interface.
2162 bridge_delete_span(struct bridge_softc
*sc
, struct bridge_iflist
*bif
)
2164 BRIDGE_LOCK_ASSERT_HELD(sc
);
2166 KASSERT(bif
->bif_ifp
->if_bridge
== NULL
,
2167 ("%s: not a span interface", __func__
));
2169 ifnet_release(bif
->bif_ifp
);
2171 TAILQ_REMOVE(&sc
->sc_spanlist
, bif
, bif_next
);
2172 _FREE(bif
, M_DEVBUF
);
2176 bridge_ioctl_add(struct bridge_softc
*sc
, void *arg
)
2178 struct ifbreq
*req
= arg
;
2179 struct bridge_iflist
*bif
= NULL
;
2180 struct ifnet
*ifs
, *bifp
= sc
->sc_ifp
;
2181 int error
= 0, lladdr_changed
= 0;
2182 uint8_t eaddr
[ETHER_ADDR_LEN
];
2183 struct iff_filter iff
;
2184 u_int32_t event_code
= 0;
2186 ifs
= ifunit(req
->ifbr_ifsname
);
2189 if (ifs
->if_ioctl
== NULL
) /* must be supported */
2192 /* If it's in the span list, it can't be a member. */
2193 TAILQ_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
2194 if (ifs
== bif
->bif_ifp
)
2197 if (ifs
->if_bridge
== sc
)
2200 if (ifs
->if_bridge
!= NULL
)
2203 switch (ifs
->if_type
) {
2206 /* permitted interface types */
2209 /* currently not supported */
2215 bif
= _MALLOC(sizeof (*bif
), M_DEVBUF
, M_NOWAIT
| M_ZERO
);
2220 ifnet_reference(ifs
);
2221 bif
->bif_ifflags
= IFBIF_LEARNING
| IFBIF_DISCOVER
;
2223 bif
->bif_savedcaps
= ifs
->if_capenable
;
2224 #endif /* HAS_IF_CAP */
2227 /* Allow the first Ethernet member to define the MTU */
2228 if (TAILQ_EMPTY(&sc
->sc_iflist
))
2229 sc
->sc_ifp
->if_mtu
= ifs
->if_mtu
;
2230 else if (sc
->sc_ifp
->if_mtu
!= ifs
->if_mtu
) {
2231 printf("%s: %s: invalid MTU for %s", __func__
,
2232 sc
->sc_ifp
->if_xname
,
2238 * Assign the interface's MAC address to the bridge if it's the first
2239 * member and the MAC address of the bridge has not been changed from
2240 * the default (randomly) generated one.
2242 if (bridge_inherit_mac
&& TAILQ_EMPTY(&sc
->sc_iflist
) &&
2243 !memcmp(IF_LLADDR(sc
->sc_ifp
), sc
->sc_defaddr
, ETHER_ADDR_LEN
)) {
2244 bcopy(IF_LLADDR(ifs
), eaddr
, ETHER_ADDR_LEN
);
2245 sc
->sc_ifaddr
= ifs
;
2246 ifnet_reference(ifs
); /* for sc_ifaddr */
2250 ifs
->if_bridge
= sc
;
2252 bstp_create(&sc
->sc_stp
, &bif
->bif_stp
, bif
->bif_ifp
);
2253 #endif /* BRIDGESTP */
2256 * XXX: XLOCK HERE!?!
2258 TAILQ_INSERT_TAIL(&sc
->sc_iflist
, bif
, bif_next
);
2261 /* Set interface capabilities to the intersection set of all members */
2262 bridge_mutecaps(sc
);
2263 #endif /* HAS_IF_CAP */
2269 * Place the interface into promiscuous mode.
2271 switch (ifs
->if_type
) {
2274 error
= ifnet_set_promiscuous(ifs
, 1);
2276 /* Ignore error when device is not up */
2277 if (error
!= ENETDOWN
)
2281 bif
->bif_flags
|= BIFF_PROMISC
;
2290 * The new member may change the link status of the bridge interface
2292 if (interface_media_active(ifs
))
2293 bif
->bif_flags
|= BIFF_MEDIA_ACTIVE
;
2295 bif
->bif_flags
&= ~BIFF_MEDIA_ACTIVE
;
2297 event_code
= bridge_updatelinkstatus(sc
);
2300 * Respect lock ordering with DLIL lock for the following operations
2305 * install an interface filter
2307 memset(&iff
, 0, sizeof (struct iff_filter
));
2308 iff
.iff_cookie
= bif
;
2309 iff
.iff_name
= "com.apple.kernel.bsd.net.if_bridge";
2310 iff
.iff_input
= bridge_iff_input
;
2311 #if BRIDGE_MEMBER_OUT_FILTER
2312 iff
.iff_output
= bridge_iff_output
;
2313 #endif /* BRIDGE_MEMBER_OUT_FILTER */
2314 iff
.iff_event
= bridge_iff_event
;
2315 iff
.iff_detached
= bridge_iff_detached
;
2316 error
= dlil_attach_filter(ifs
, &iff
, &bif
->bif_iff_ref
, DLIL_IFF_TSO
);
2318 printf("%s: iflt_attach failed %d\n", __func__
, error
);
2322 bif
->bif_flags
|= BIFF_FILTER_ATTACHED
;
2325 * install an dummy "bridge" protocol
2327 if ((error
= bridge_attach_protocol(ifs
)) != 0) {
2329 printf("%s: bridge_attach_protocol failed %d\n",
2335 bif
->bif_flags
|= BIFF_PROTO_ATTACHED
;
2337 if (lladdr_changed
&&
2338 (error
= ifnet_set_lladdr(bifp
, eaddr
, ETHER_ADDR_LEN
)) != 0)
2339 printf("%s: ifnet_set_lladdr failed %d\n", __func__
, error
);
2341 if (event_code
!= 0)
2342 bridge_link_event(bifp
, event_code
);
2347 if (error
&& bif
!= NULL
)
2348 bridge_delete_member(sc
, bif
, 1);
2354 bridge_ioctl_del(struct bridge_softc
*sc
, void *arg
)
2356 struct ifbreq
*req
= arg
;
2357 struct bridge_iflist
*bif
;
2359 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
2363 bridge_delete_member(sc
, bif
, 0);
2369 bridge_ioctl_purge(struct bridge_softc
*sc
, void *arg
)
2371 #pragma unused(sc, arg)
2376 bridge_ioctl_gifflags(struct bridge_softc
*sc
, void *arg
)
2378 struct ifbreq
*req
= arg
;
2379 struct bridge_iflist
*bif
;
2380 struct bstp_port
*bp
;
2382 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
2387 req
->ifbr_ifsflags
= bif
->bif_ifflags
;
2388 req
->ifbr_state
= bp
->bp_state
;
2389 req
->ifbr_priority
= bp
->bp_priority
;
2390 req
->ifbr_path_cost
= bp
->bp_path_cost
;
2391 req
->ifbr_portno
= bif
->bif_ifp
->if_index
& 0xfff;
2392 req
->ifbr_proto
= bp
->bp_protover
;
2393 req
->ifbr_role
= bp
->bp_role
;
2394 req
->ifbr_stpflags
= bp
->bp_flags
;
2395 req
->ifbr_addrcnt
= bif
->bif_addrcnt
;
2396 req
->ifbr_addrmax
= bif
->bif_addrmax
;
2397 req
->ifbr_addrexceeded
= bif
->bif_addrexceeded
;
2399 /* Copy STP state options as flags */
2400 if (bp
->bp_operedge
)
2401 req
->ifbr_ifsflags
|= IFBIF_BSTP_EDGE
;
2402 if (bp
->bp_flags
& BSTP_PORT_AUTOEDGE
)
2403 req
->ifbr_ifsflags
|= IFBIF_BSTP_AUTOEDGE
;
2404 if (bp
->bp_ptp_link
)
2405 req
->ifbr_ifsflags
|= IFBIF_BSTP_PTP
;
2406 if (bp
->bp_flags
& BSTP_PORT_AUTOPTP
)
2407 req
->ifbr_ifsflags
|= IFBIF_BSTP_AUTOPTP
;
2408 if (bp
->bp_flags
& BSTP_PORT_ADMEDGE
)
2409 req
->ifbr_ifsflags
|= IFBIF_BSTP_ADMEDGE
;
2410 if (bp
->bp_flags
& BSTP_PORT_ADMCOST
)
2411 req
->ifbr_ifsflags
|= IFBIF_BSTP_ADMCOST
;
2416 bridge_ioctl_sifflags(struct bridge_softc
*sc
, void *arg
)
2418 struct ifbreq
*req
= arg
;
2419 struct bridge_iflist
*bif
;
2421 struct bstp_port
*bp
;
2423 #endif /* BRIDGESTP */
2425 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
2429 if (req
->ifbr_ifsflags
& IFBIF_SPAN
)
2430 /* SPAN is readonly */
2435 if (req
->ifbr_ifsflags
& IFBIF_STP
) {
2436 if ((bif
->bif_ifflags
& IFBIF_STP
) == 0) {
2437 error
= bstp_enable(&bif
->bif_stp
);
2442 if ((bif
->bif_ifflags
& IFBIF_STP
) != 0)
2443 bstp_disable(&bif
->bif_stp
);
2446 /* Pass on STP flags */
2448 bstp_set_edge(bp
, req
->ifbr_ifsflags
& IFBIF_BSTP_EDGE
? 1 : 0);
2449 bstp_set_autoedge(bp
, req
->ifbr_ifsflags
& IFBIF_BSTP_AUTOEDGE
? 1 : 0);
2450 bstp_set_ptp(bp
, req
->ifbr_ifsflags
& IFBIF_BSTP_PTP
? 1 : 0);
2451 bstp_set_autoptp(bp
, req
->ifbr_ifsflags
& IFBIF_BSTP_AUTOPTP
? 1 : 0);
2452 #else /* !BRIDGESTP */
2453 if (req
->ifbr_ifsflags
& IFBIF_STP
)
2454 return (EOPNOTSUPP
);
2455 #endif /* !BRIDGESTP */
2457 /* Save the bits relating to the bridge */
2458 bif
->bif_ifflags
= req
->ifbr_ifsflags
& IFBIFMASK
;
2465 bridge_ioctl_scache(struct bridge_softc
*sc
, void *arg
)
2467 struct ifbrparam
*param
= arg
;
2469 sc
->sc_brtmax
= param
->ifbrp_csize
;
2476 bridge_ioctl_gcache(struct bridge_softc
*sc
, void *arg
)
2478 struct ifbrparam
*param
= arg
;
2480 param
->ifbrp_csize
= sc
->sc_brtmax
;
2485 #define BRIDGE_IOCTL_GIFS do { \
2486 struct bridge_iflist *bif; \
2487 struct ifbreq breq; \
2488 char *buf, *outbuf; \
2489 unsigned int count, buflen, len; \
2492 TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) \
2494 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) \
2497 buflen = sizeof (breq) * count; \
2498 if (bifc->ifbic_len == 0) { \
2499 bifc->ifbic_len = buflen; \
2502 BRIDGE_UNLOCK(sc); \
2503 outbuf = _MALLOC(buflen, M_TEMP, M_WAITOK | M_ZERO); \
2508 len = min(bifc->ifbic_len, buflen); \
2509 bzero(&breq, sizeof (breq)); \
2510 TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { \
2511 if (len < sizeof (breq)) \
2514 snprintf(breq.ifbr_ifsname, sizeof (breq.ifbr_ifsname), \
2515 "%s", bif->bif_ifp->if_xname); \
2516 /* Fill in the ifbreq structure */ \
2517 error = bridge_ioctl_gifflags(sc, &breq); \
2520 memcpy(buf, &breq, sizeof (breq)); \
2522 buf += sizeof (breq); \
2523 len -= sizeof (breq); \
2525 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) { \
2526 if (len < sizeof (breq)) \
2529 snprintf(breq.ifbr_ifsname, sizeof (breq.ifbr_ifsname), \
2530 "%s", bif->bif_ifp->if_xname); \
2531 breq.ifbr_ifsflags = bif->bif_ifflags; \
2532 breq.ifbr_portno = bif->bif_ifp->if_index & 0xfff; \
2533 memcpy(buf, &breq, sizeof (breq)); \
2535 buf += sizeof (breq); \
2536 len -= sizeof (breq); \
2539 BRIDGE_UNLOCK(sc); \
2540 bifc->ifbic_len = sizeof (breq) * count; \
2541 error = copyout(outbuf, bifc->ifbic_req, bifc->ifbic_len); \
2543 _FREE(outbuf, M_TEMP); \
2547 bridge_ioctl_gifs64(struct bridge_softc
*sc
, void *arg
)
2549 struct ifbifconf64
*bifc
= arg
;
2558 bridge_ioctl_gifs32(struct bridge_softc
*sc
, void *arg
)
2560 struct ifbifconf32
*bifc
= arg
;
2568 #define BRIDGE_IOCTL_RTS do { \
2569 struct bridge_rtnode *brt; \
2570 char *buf, *outbuf; \
2571 unsigned int count, buflen, len; \
2572 unsigned long now; \
2574 if (bac->ifbac_len == 0) \
2578 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) \
2580 buflen = sizeof (bareq) * count; \
2582 BRIDGE_UNLOCK(sc); \
2583 outbuf = _MALLOC(buflen, M_TEMP, M_WAITOK | M_ZERO); \
2588 len = min(bac->ifbac_len, buflen); \
2589 bzero(&bareq, sizeof (bareq)); \
2590 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) { \
2591 if (len < sizeof (bareq)) \
2593 snprintf(bareq.ifba_ifsname, sizeof (bareq.ifba_ifsname), \
2594 "%s", brt->brt_ifp->if_xname); \
2595 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof (brt->brt_addr)); \
2596 bareq.ifba_vlan = brt->brt_vlan; \
2597 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { \
2598 now = (unsigned long) net_uptime(); \
2599 if (now < brt->brt_expire) \
2600 bareq.ifba_expire = \
2601 brt->brt_expire - now; \
2603 bareq.ifba_expire = 0; \
2604 bareq.ifba_flags = brt->brt_flags; \
2606 memcpy(buf, &bareq, sizeof (bareq)); \
2608 buf += sizeof (bareq); \
2609 len -= sizeof (bareq); \
2612 BRIDGE_UNLOCK(sc); \
2613 bac->ifbac_len = sizeof (bareq) * count; \
2614 error = copyout(outbuf, bac->ifbac_req, bac->ifbac_len); \
2616 _FREE(outbuf, M_TEMP); \
2621 bridge_ioctl_rts64(struct bridge_softc
*sc
, void *arg
)
2623 struct ifbaconf64
*bac
= arg
;
2624 struct ifbareq64 bareq
;
2633 bridge_ioctl_rts32(struct bridge_softc
*sc
, void *arg
)
2635 struct ifbaconf32
*bac
= arg
;
2636 struct ifbareq32 bareq
;
2645 bridge_ioctl_saddr32(struct bridge_softc
*sc
, void *arg
)
2647 struct ifbareq32
*req
= arg
;
2648 struct bridge_iflist
*bif
;
2651 bif
= bridge_lookup_member(sc
, req
->ifba_ifsname
);
2655 error
= bridge_rtupdate(sc
, req
->ifba_dst
, req
->ifba_vlan
, bif
, 1,
2662 bridge_ioctl_saddr64(struct bridge_softc
*sc
, void *arg
)
2664 struct ifbareq64
*req
= arg
;
2665 struct bridge_iflist
*bif
;
2668 bif
= bridge_lookup_member(sc
, req
->ifba_ifsname
);
2672 error
= bridge_rtupdate(sc
, req
->ifba_dst
, req
->ifba_vlan
, bif
, 1,
2679 bridge_ioctl_sto(struct bridge_softc
*sc
, void *arg
)
2681 struct ifbrparam
*param
= arg
;
2683 sc
->sc_brttimeout
= param
->ifbrp_ctime
;
2688 bridge_ioctl_gto(struct bridge_softc
*sc
, void *arg
)
2690 struct ifbrparam
*param
= arg
;
2692 param
->ifbrp_ctime
= sc
->sc_brttimeout
;
2697 bridge_ioctl_daddr32(struct bridge_softc
*sc
, void *arg
)
2699 struct ifbareq32
*req
= arg
;
2701 return (bridge_rtdaddr(sc
, req
->ifba_dst
, req
->ifba_vlan
));
2705 bridge_ioctl_daddr64(struct bridge_softc
*sc
, void *arg
)
2707 struct ifbareq64
*req
= arg
;
2709 return (bridge_rtdaddr(sc
, req
->ifba_dst
, req
->ifba_vlan
));
2713 bridge_ioctl_flush(struct bridge_softc
*sc
, void *arg
)
2715 struct ifbreq
*req
= arg
;
2717 bridge_rtflush(sc
, req
->ifbr_ifsflags
);
2722 bridge_ioctl_gpri(struct bridge_softc
*sc
, void *arg
)
2724 struct ifbrparam
*param
= arg
;
2725 struct bstp_state
*bs
= &sc
->sc_stp
;
2727 param
->ifbrp_prio
= bs
->bs_bridge_priority
;
2732 bridge_ioctl_spri(struct bridge_softc
*sc
, void *arg
)
2735 struct ifbrparam
*param
= arg
;
2737 return (bstp_set_priority(&sc
->sc_stp
, param
->ifbrp_prio
));
2738 #else /* !BRIDGESTP */
2739 #pragma unused(sc, arg)
2740 return (EOPNOTSUPP
);
2741 #endif /* !BRIDGESTP */
2745 bridge_ioctl_ght(struct bridge_softc
*sc
, void *arg
)
2747 struct ifbrparam
*param
= arg
;
2748 struct bstp_state
*bs
= &sc
->sc_stp
;
2750 param
->ifbrp_hellotime
= bs
->bs_bridge_htime
>> 8;
2755 bridge_ioctl_sht(struct bridge_softc
*sc
, void *arg
)
2758 struct ifbrparam
*param
= arg
;
2760 return (bstp_set_htime(&sc
->sc_stp
, param
->ifbrp_hellotime
));
2761 #else /* !BRIDGESTP */
2762 #pragma unused(sc, arg)
2763 return (EOPNOTSUPP
);
2764 #endif /* !BRIDGESTP */
2768 bridge_ioctl_gfd(struct bridge_softc
*sc
, void *arg
)
2770 struct ifbrparam
*param
= arg
;
2771 struct bstp_state
*bs
= &sc
->sc_stp
;
2773 param
->ifbrp_fwddelay
= bs
->bs_bridge_fdelay
>> 8;
2778 bridge_ioctl_sfd(struct bridge_softc
*sc
, void *arg
)
2781 struct ifbrparam
*param
= arg
;
2783 return (bstp_set_fdelay(&sc
->sc_stp
, param
->ifbrp_fwddelay
));
2784 #else /* !BRIDGESTP */
2785 #pragma unused(sc, arg)
2786 return (EOPNOTSUPP
);
2787 #endif /* !BRIDGESTP */
2791 bridge_ioctl_gma(struct bridge_softc
*sc
, void *arg
)
2793 struct ifbrparam
*param
= arg
;
2794 struct bstp_state
*bs
= &sc
->sc_stp
;
2796 param
->ifbrp_maxage
= bs
->bs_bridge_max_age
>> 8;
2801 bridge_ioctl_sma(struct bridge_softc
*sc
, void *arg
)
2804 struct ifbrparam
*param
= arg
;
2806 return (bstp_set_maxage(&sc
->sc_stp
, param
->ifbrp_maxage
));
2807 #else /* !BRIDGESTP */
2808 #pragma unused(sc, arg)
2809 return (EOPNOTSUPP
);
2810 #endif /* !BRIDGESTP */
2814 bridge_ioctl_sifprio(struct bridge_softc
*sc
, void *arg
)
2817 struct ifbreq
*req
= arg
;
2818 struct bridge_iflist
*bif
;
2820 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
2824 return (bstp_set_port_priority(&bif
->bif_stp
, req
->ifbr_priority
));
2825 #else /* !BRIDGESTP */
2826 #pragma unused(sc, arg)
2827 return (EOPNOTSUPP
);
2828 #endif /* !BRIDGESTP */
2832 bridge_ioctl_sifcost(struct bridge_softc
*sc
, void *arg
)
2835 struct ifbreq
*req
= arg
;
2836 struct bridge_iflist
*bif
;
2838 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
2842 return (bstp_set_path_cost(&bif
->bif_stp
, req
->ifbr_path_cost
));
2843 #else /* !BRIDGESTP */
2844 #pragma unused(sc, arg)
2845 return (EOPNOTSUPP
);
2846 #endif /* !BRIDGESTP */
2850 bridge_ioctl_gfilt(struct bridge_softc
*sc
, void *arg
)
2852 struct ifbrparam
*param
= arg
;
2854 param
->ifbrp_filter
= sc
->sc_filter_flags
;
2860 bridge_ioctl_sfilt(struct bridge_softc
*sc
, void *arg
)
2862 struct ifbrparam
*param
= arg
;
2864 if (param
->ifbrp_filter
& ~IFBF_FILT_MASK
)
2868 if (param
->ifbrp_filter
& IFBF_FILT_USEIPF
)
2872 sc
->sc_filter_flags
= param
->ifbrp_filter
;
2878 bridge_ioctl_sifmaxaddr(struct bridge_softc
*sc
, void *arg
)
2880 struct ifbreq
*req
= arg
;
2881 struct bridge_iflist
*bif
;
2883 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
2887 bif
->bif_addrmax
= req
->ifbr_addrmax
;
2892 bridge_ioctl_addspan(struct bridge_softc
*sc
, void *arg
)
2894 struct ifbreq
*req
= arg
;
2895 struct bridge_iflist
*bif
= NULL
;
2898 ifs
= ifunit(req
->ifbr_ifsname
);
2902 TAILQ_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
2903 if (ifs
== bif
->bif_ifp
)
2906 if (ifs
->if_bridge
!= NULL
)
2909 switch (ifs
->if_type
) {
2914 /* currently not supported */
2920 bif
= _MALLOC(sizeof (*bif
), M_DEVBUF
, M_NOWAIT
| M_ZERO
);
2925 bif
->bif_ifflags
= IFBIF_SPAN
;
2927 ifnet_reference(bif
->bif_ifp
);
2929 TAILQ_INSERT_HEAD(&sc
->sc_spanlist
, bif
, bif_next
);
2935 bridge_ioctl_delspan(struct bridge_softc
*sc
, void *arg
)
2937 struct ifbreq
*req
= arg
;
2938 struct bridge_iflist
*bif
;
2941 ifs
= ifunit(req
->ifbr_ifsname
);
2945 TAILQ_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
2946 if (ifs
== bif
->bif_ifp
)
2952 bridge_delete_span(sc
, bif
);
2957 #define BRIDGE_IOCTL_GBPARAM do { \
2958 struct bstp_state *bs = &sc->sc_stp; \
2959 struct bstp_port *root_port; \
2961 req->ifbop_maxage = bs->bs_bridge_max_age >> 8; \
2962 req->ifbop_hellotime = bs->bs_bridge_htime >> 8; \
2963 req->ifbop_fwddelay = bs->bs_bridge_fdelay >> 8; \
2965 root_port = bs->bs_root_port; \
2966 if (root_port == NULL) \
2967 req->ifbop_root_port = 0; \
2969 req->ifbop_root_port = root_port->bp_ifp->if_index; \
2971 req->ifbop_holdcount = bs->bs_txholdcount; \
2972 req->ifbop_priority = bs->bs_bridge_priority; \
2973 req->ifbop_protocol = bs->bs_protover; \
2974 req->ifbop_root_path_cost = bs->bs_root_pv.pv_cost; \
2975 req->ifbop_bridgeid = bs->bs_bridge_pv.pv_dbridge_id; \
2976 req->ifbop_designated_root = bs->bs_root_pv.pv_root_id; \
2977 req->ifbop_designated_bridge = bs->bs_root_pv.pv_dbridge_id; \
2978 req->ifbop_last_tc_time.tv_sec = bs->bs_last_tc_time.tv_sec; \
2979 req->ifbop_last_tc_time.tv_usec = bs->bs_last_tc_time.tv_usec; \
2983 bridge_ioctl_gbparam32(struct bridge_softc
*sc
, void *arg
)
2985 struct ifbropreq32
*req
= arg
;
2987 BRIDGE_IOCTL_GBPARAM
;
2993 bridge_ioctl_gbparam64(struct bridge_softc
*sc
, void *arg
)
2995 struct ifbropreq64
*req
= arg
;
2997 BRIDGE_IOCTL_GBPARAM
;
3003 bridge_ioctl_grte(struct bridge_softc
*sc
, void *arg
)
3005 struct ifbrparam
*param
= arg
;
3007 param
->ifbrp_cexceeded
= sc
->sc_brtexceeded
;
3011 #define BRIDGE_IOCTL_GIFSSTP do { \
3012 struct bridge_iflist *bif; \
3013 struct bstp_port *bp; \
3014 struct ifbpstpreq bpreq; \
3015 char *buf, *outbuf; \
3016 unsigned int count, buflen, len; \
3019 TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { \
3020 if ((bif->bif_ifflags & IFBIF_STP) != 0) \
3024 buflen = sizeof (bpreq) * count; \
3025 if (bifstp->ifbpstp_len == 0) { \
3026 bifstp->ifbpstp_len = buflen; \
3030 BRIDGE_UNLOCK(sc); \
3031 outbuf = _MALLOC(buflen, M_TEMP, M_WAITOK | M_ZERO); \
3036 len = min(bifstp->ifbpstp_len, buflen); \
3037 bzero(&bpreq, sizeof (bpreq)); \
3038 TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { \
3039 if (len < sizeof (bpreq)) \
3042 if ((bif->bif_ifflags & IFBIF_STP) == 0) \
3045 bp = &bif->bif_stp; \
3046 bpreq.ifbp_portno = bif->bif_ifp->if_index & 0xfff; \
3047 bpreq.ifbp_fwd_trans = bp->bp_forward_transitions; \
3048 bpreq.ifbp_design_cost = bp->bp_desg_pv.pv_cost; \
3049 bpreq.ifbp_design_port = bp->bp_desg_pv.pv_port_id; \
3050 bpreq.ifbp_design_bridge = bp->bp_desg_pv.pv_dbridge_id; \
3051 bpreq.ifbp_design_root = bp->bp_desg_pv.pv_root_id; \
3053 memcpy(buf, &bpreq, sizeof (bpreq)); \
3055 buf += sizeof (bpreq); \
3056 len -= sizeof (bpreq); \
3059 BRIDGE_UNLOCK(sc); \
3060 bifstp->ifbpstp_len = sizeof (bpreq) * count; \
3061 error = copyout(outbuf, bifstp->ifbpstp_req, bifstp->ifbpstp_len); \
3063 _FREE(outbuf, M_TEMP); \
3068 bridge_ioctl_gifsstp32(struct bridge_softc
*sc
, void *arg
)
3070 struct ifbpstpconf32
*bifstp
= arg
;
3073 BRIDGE_IOCTL_GIFSSTP
;
3079 bridge_ioctl_gifsstp64(struct bridge_softc
*sc
, void *arg
)
3081 struct ifbpstpconf64
*bifstp
= arg
;
3084 BRIDGE_IOCTL_GIFSSTP
;
3090 bridge_ioctl_sproto(struct bridge_softc
*sc
, void *arg
)
3093 struct ifbrparam
*param
= arg
;
3095 return (bstp_set_protocol(&sc
->sc_stp
, param
->ifbrp_proto
));
3096 #else /* !BRIDGESTP */
3097 #pragma unused(sc, arg)
3098 return (EOPNOTSUPP
);
3099 #endif /* !BRIDGESTP */
3103 bridge_ioctl_stxhc(struct bridge_softc
*sc
, void *arg
)
3106 struct ifbrparam
*param
= arg
;
3108 return (bstp_set_holdcount(&sc
->sc_stp
, param
->ifbrp_txhc
));
3109 #else /* !BRIDGESTP */
3110 #pragma unused(sc, arg)
3111 return (EOPNOTSUPP
);
3112 #endif /* !BRIDGESTP */
3118 * Detach an interface from a bridge. Called when a member
3119 * interface is detaching.
3121 __private_extern__
void
3122 bridge_ifdetach(struct bridge_iflist
*bif
, struct ifnet
*ifp
)
3124 struct bridge_softc
*sc
= ifp
->if_bridge
;
3127 if (if_bridge_debug
& BR_DBGF_LIFECYCLE
)
3128 printf("%s: %s\n", __func__
, ifp
->if_xname
);
3129 #endif /* BRIDGE_DEBUG */
3131 /* Check if the interface is a bridge member */
3134 bif
= bridge_lookup_member_if(sc
, ifp
);
3136 bridge_delete_member(sc
, bif
, 1);
3141 /* Check if the interface is a span port */
3142 lck_mtx_lock(&bridge_list_mtx
);
3143 LIST_FOREACH(sc
, &bridge_list
, sc_list
) {
3145 TAILQ_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
3146 if (ifp
== bif
->bif_ifp
) {
3147 bridge_delete_span(sc
, bif
);
3152 lck_mtx_unlock(&bridge_list_mtx
);
3156 * interface_media_active:
3158 * Tells if an interface media is active.
3161 interface_media_active(struct ifnet
*ifp
)
3163 struct ifmediareq ifmr
;
3166 bzero(&ifmr
, sizeof(ifmr
));
3167 if (ifnet_ioctl(ifp
, 0, SIOCGIFMEDIA
, &ifmr
) == 0) {
3168 if ((ifmr
.ifm_status
& IFM_AVALID
) && ifmr
.ifm_count
> 0)
3169 status
= ifmr
.ifm_status
& IFM_ACTIVE
? 1 : 0;
3176 * bridge_updatelinkstatus:
3178 * Update the media active status of the bridge based on the
3179 * media active status of its member.
3180 * If changed, return the corresponding onf/off link event.
3183 bridge_updatelinkstatus(struct bridge_softc
*sc
)
3185 struct bridge_iflist
*bif
;
3186 int active_member
= 0;
3187 u_int32_t event_code
= 0;
3189 BRIDGE_LOCK_ASSERT_HELD(sc
);
3192 * Find out if we have an active interface
3194 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
3195 if (bif
->bif_flags
& BIFF_MEDIA_ACTIVE
) {
3201 if (active_member
&& !(sc
->sc_flags
& SCF_MEDIA_ACTIVE
)) {
3202 sc
->sc_flags
|= SCF_MEDIA_ACTIVE
;
3203 event_code
= KEV_DL_LINK_ON
;
3204 } else if (!active_member
&& (sc
->sc_flags
& SCF_MEDIA_ACTIVE
)) {
3205 sc
->sc_flags
&= ~SCF_MEDIA_ACTIVE
;
3206 event_code
= KEV_DL_LINK_OFF
;
3209 return (event_code
);
3213 * bridge_iflinkevent:
3216 bridge_iflinkevent(struct ifnet
*ifp
)
3218 struct bridge_softc
*sc
= ifp
->if_bridge
;
3219 struct bridge_iflist
*bif
;
3220 u_int32_t event_code
= 0;
3223 if (if_bridge_debug
& BR_DBGF_LIFECYCLE
)
3224 printf("%s: %s\n", __func__
, ifp
->if_xname
);
3225 #endif /* BRIDGE_DEBUG */
3227 /* Check if the interface is a bridge member */
3232 bif
= bridge_lookup_member_if(sc
, ifp
);
3234 if (interface_media_active(ifp
))
3235 bif
->bif_flags
|= BIFF_MEDIA_ACTIVE
;
3237 bif
->bif_flags
&= ~BIFF_MEDIA_ACTIVE
;
3239 event_code
= bridge_updatelinkstatus(sc
);
3243 if (event_code
!= 0)
3244 bridge_link_event(sc
->sc_ifp
, event_code
);
3248 * bridge_delayed_callback:
3250 * Makes a delayed call
3253 bridge_delayed_callback(void *param
)
3255 struct bridge_delayed_call
*call
= (struct bridge_delayed_call
*)param
;
3256 struct bridge_softc
*sc
= call
->bdc_sc
;
3258 #if BRIDGE_DEBUG_DELAYED_CALLBACK
3259 if (bridge_delayed_callback_delay
> 0) {
3262 ts
.tv_sec
= bridge_delayed_callback_delay
;
3265 printf("%s: sleeping for %d seconds\n",
3266 __func__
, bridge_delayed_callback_delay
);
3268 msleep(&bridge_delayed_callback_delay
, NULL
, PZERO
,
3271 printf("%s: awoken\n", __func__
);
3273 #endif /* BRIDGE_DEBUG_DELAYED_CALLBACK */
3278 if (if_bridge_debug
& BR_DBGF_DELAYED_CALL
)
3279 printf("%s: %s call 0x%llx flags 0x%x\n", __func__
,
3280 sc
->sc_if_xname
, (uint64_t)VM_KERNEL_ADDRPERM(call
),
3282 #endif /* BRIDGE_DEBUG */
3284 if (call
->bdc_flags
& BDCF_CANCELLING
) {
3287 if ((sc
->sc_flags
& SCF_DETACHING
) == 0)
3288 (*call
->bdc_func
)(sc
);
3290 call
->bdc_flags
&= ~BDCF_OUTSTANDING
;
3295 * bridge_schedule_delayed_call:
3297 * Schedule a function to be called on a separate thread
3298 * The actual call may be scheduled to run at a given time or ASAP.
3301 bridge_schedule_delayed_call(struct bridge_delayed_call
*call
)
3303 uint64_t deadline
= 0;
3304 struct bridge_softc
*sc
= call
->bdc_sc
;
3306 BRIDGE_LOCK_ASSERT_HELD(sc
);
3308 if ((sc
->sc_flags
& SCF_DETACHING
) ||
3309 (call
->bdc_flags
& (BDCF_OUTSTANDING
| BDCF_CANCELLING
)))
3312 if (call
->bdc_ts
.tv_sec
|| call
->bdc_ts
.tv_nsec
) {
3313 nanoseconds_to_absolutetime(
3314 (uint64_t)call
->bdc_ts
.tv_sec
* NSEC_PER_SEC
+
3315 call
->bdc_ts
.tv_nsec
, &deadline
);
3316 clock_absolutetime_interval_to_deadline(deadline
, &deadline
);
3319 call
->bdc_flags
= BDCF_OUTSTANDING
;
3322 if (if_bridge_debug
& BR_DBGF_DELAYED_CALL
)
3323 printf("%s: %s call 0x%llx flags 0x%x\n", __func__
,
3324 sc
->sc_if_xname
, (uint64_t)VM_KERNEL_ADDRPERM(call
),
3326 #endif /* BRIDGE_DEBUG */
3328 thread_call_func_delayed((thread_call_func_t
)bridge_delayed_callback
,
3333 * bridge_cancel_delayed_call:
3335 * Cancel a queued or running delayed call.
3336 * If call is running, does not return until the call is done to
3337 * prevent race condition with the brigde interface getting destroyed
3340 bridge_cancel_delayed_call(struct bridge_delayed_call
*call
)
3343 struct bridge_softc
*sc
= call
->bdc_sc
;
3346 * The call was never scheduled
3351 BRIDGE_LOCK_ASSERT_HELD(sc
);
3353 call
->bdc_flags
|= BDCF_CANCELLING
;
3355 while (call
->bdc_flags
& BDCF_OUTSTANDING
) {
3357 if (if_bridge_debug
& BR_DBGF_DELAYED_CALL
)
3358 printf("%s: %s call 0x%llx flags 0x%x\n", __func__
,
3359 sc
->sc_if_xname
, (uint64_t)VM_KERNEL_ADDRPERM(call
),
3361 #endif /* BRIDGE_DEBUG */
3362 result
= thread_call_func_cancel(
3363 (thread_call_func_t
)bridge_delayed_callback
, call
, FALSE
);
3367 * We managed to dequeue the delayed call
3369 call
->bdc_flags
&= ~BDCF_OUTSTANDING
;
3372 * Wait for delayed call do be done running
3374 msleep(call
, &sc
->sc_mtx
, PZERO
, __func__
, NULL
);
3377 call
->bdc_flags
&= ~BDCF_CANCELLING
;
3383 * Initialize a bridge interface.
3386 bridge_init(struct ifnet
*ifp
)
3388 struct bridge_softc
*sc
= (struct bridge_softc
*)ifp
->if_softc
;
3391 BRIDGE_LOCK_ASSERT_HELD(sc
);
3393 if ((ifnet_flags(ifp
) & IFF_RUNNING
))
3396 error
= ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
3399 * Calling bridge_aging_timer() is OK as there are no entries to
3400 * age so we're just going to arm the timer
3402 bridge_aging_timer(sc
);
3406 bstp_init(&sc
->sc_stp
); /* Initialize Spanning Tree */
3407 #endif /* BRIDGESTP */
3415 * Stop the bridge interface.
3418 bridge_ifstop(struct ifnet
*ifp
, int disable
)
3420 #pragma unused(disable)
3421 struct bridge_softc
*sc
= ifp
->if_softc
;
3423 BRIDGE_LOCK_ASSERT_HELD(sc
);
3425 if ((ifnet_flags(ifp
) & IFF_RUNNING
) == 0)
3428 bridge_cancel_delayed_call(&sc
->sc_aging_timer
);
3431 bstp_stop(&sc
->sc_stp
);
3432 #endif /* BRIDGESTP */
3434 bridge_rtflush(sc
, IFBF_FLUSHDYN
);
3436 (void) ifnet_set_flags(ifp
, 0, IFF_RUNNING
);
3442 * Enqueue a packet on a bridge member interface.
3446 bridge_enqueue(struct bridge_softc
*sc
, struct ifnet
*dst_ifp
, struct mbuf
*m
)
3452 VERIFY(dst_ifp
!= NULL
);
3455 * We may be sending a fragment so traverse the mbuf
3457 * NOTE: bridge_fragment() is called only when PFIL_HOOKS is enabled.
3461 struct flowadv adv
= { FADV_SUCCESS
};
3464 m
->m_nextpkt
= NULL
;
3466 len
= m
->m_pkthdr
.len
;
3467 mflags
= m
->m_flags
;
3468 m
->m_flags
|= M_PROTO1
; /* set to avoid loops */
3470 bridge_finalize_cksum(dst_ifp
, m
);
3474 * If underlying interface can not do VLAN tag insertion itself
3475 * then attach a packet tag that holds it.
3477 if ((m
->m_flags
& M_VLANTAG
) &&
3478 (dst_ifp
->if_capenable
& IFCAP_VLAN_HWTAGGING
) == 0) {
3479 m
= ether_vlanencap(m
, m
->m_pkthdr
.ether_vtag
);
3481 printf("%s: %s: unable to prepend VLAN "
3482 "header\n", __func__
, dst_ifp
->if_xname
);
3483 (void) ifnet_stat_increment_out(dst_ifp
,
3487 m
->m_flags
&= ~M_VLANTAG
;
3489 #endif /* HAS_IF_CAP */
3491 _error
= dlil_output(dst_ifp
, 0, m
, NULL
, NULL
, 1, &adv
);
3493 /* Preserve existing error value */
3497 else if (adv
.code
== FADV_FLOW_CONTROLLED
)
3499 else if (adv
.code
== FADV_SUSPENDED
)
3500 error
= EQSUSPENDED
;
3504 (void) ifnet_stat_increment_out(sc
->sc_ifp
, 1, len
, 0);
3506 (void) ifnet_stat_increment_out(sc
->sc_ifp
, 0, 0, 1);
3513 #if HAS_BRIDGE_DUMMYNET
3517 * Receive a queued packet from dummynet and pass it on to the output
3520 * The mbuf has the Ethernet header already attached.
3523 bridge_dummynet(struct mbuf
*m
, struct ifnet
*ifp
)
3525 struct bridge_softc
*sc
;
3527 sc
= ifp
->if_bridge
;
3530 * The packet didnt originate from a member interface. This should only
3531 * ever happen if a member interface is removed while packets are
3539 if (PFIL_HOOKED(&inet_pfil_hook
) || PFIL_HOOKED_INET6
) {
3540 if (bridge_pfil(&m
, sc
->sc_ifp
, ifp
, PFIL_OUT
) != 0)
3546 (void) bridge_enqueue(sc
, ifp
, m
);
3548 #endif /* HAS_BRIDGE_DUMMYNET */
3550 #if BRIDGE_MEMBER_OUT_FILTER
3552 * bridge_member_output:
3554 * Send output from a bridge member interface. This
3555 * performs the bridging function for locally originated
3558 * The mbuf has the Ethernet header already attached. We must
3559 * enqueue or free the mbuf before returning.
3562 bridge_member_output(struct ifnet
*ifp
, struct mbuf
*m
, struct sockaddr
*sa
,
3565 #pragma unused(sa, rt)
3566 struct ether_header
*eh
;
3567 struct ifnet
*dst_if
;
3568 struct bridge_softc
*sc
;
3572 if (if_bridge_debug
& BR_DBGF_OUTPPUT
)
3573 printf("%s: ifp %s\n", __func__
, ifp
->if_xname
);
3574 #endif /* BRIDGE_DEBUG */
3576 if (m
->m_len
< ETHER_HDR_LEN
) {
3577 m
= m_pullup(m
, ETHER_HDR_LEN
);
3582 eh
= mtod(m
, struct ether_header
*);
3583 sc
= ifp
->if_bridge
;
3584 vlan
= VLANTAGOF(m
);
3589 * APPLE MODIFICATION
3590 * If the packet is an 802.1X ethertype, then only send on the
3591 * original output interface.
3593 if (eh
->ether_type
== htons(ETHERTYPE_PAE
)) {
3599 * If bridge is down, but the original output interface is up,
3600 * go ahead and send out that interface. Otherwise, the packet
3603 if ((sc
->sc_ifp
->if_flags
& IFF_RUNNING
) == 0) {
3609 * If the packet is a multicast, or we don't know a better way to
3610 * get there, send to all interfaces.
3612 if (ETHER_IS_MULTICAST(eh
->ether_dhost
))
3615 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
, vlan
);
3616 if (dst_if
== NULL
) {
3617 struct bridge_iflist
*bif
;
3619 int error
= 0, used
= 0;
3623 BRIDGE_LOCK2REF(sc
, error
);
3629 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
3630 dst_if
= bif
->bif_ifp
;
3632 if (dst_if
->if_type
== IFT_GIF
)
3634 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
3638 * If this is not the original output interface,
3639 * and the interface is participating in spanning
3640 * tree, make sure the port is in a state that
3641 * allows forwarding.
3643 if (dst_if
!= ifp
&& (bif
->bif_ifflags
& IFBIF_STP
) &&
3644 bif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
)
3647 if (LIST_NEXT(bif
, bif_next
) == NULL
) {
3651 mc
= m_copypacket(m
, M_DONTWAIT
);
3653 (void) ifnet_stat_increment_out(
3654 sc
->sc_ifp
, 0, 0, 1);
3659 (void) bridge_enqueue(sc
, dst_if
, mc
);
3669 * XXX Spanning tree consideration here?
3673 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0) {
3680 (void) bridge_enqueue(sc
, dst_if
, m
);
3683 #endif /* BRIDGE_MEMBER_OUT_FILTER */
3688 * This routine is called externally from above only when if_bridge_txstart
3689 * is disabled; otherwise it is called internally by bridge_start().
3692 bridge_output(struct ifnet
*ifp
, struct mbuf
*m
)
3694 struct bridge_softc
*sc
= ifnet_softc(ifp
);
3695 struct ether_header
*eh
;
3696 struct ifnet
*dst_if
;
3699 eh
= mtod(m
, struct ether_header
*);
3703 if (!(m
->m_flags
& (M_BCAST
|M_MCAST
)))
3704 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
, 0);
3706 (void) ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
3709 if (sc
->sc_bpf_output
)
3710 bridge_bpf_output(ifp
, m
);
3713 if (dst_if
== NULL
) {
3714 /* callee will unlock */
3715 bridge_broadcast(sc
, ifp
, m
, 0);
3718 error
= bridge_enqueue(sc
, dst_if
, m
);
3725 bridge_finalize_cksum(struct ifnet
*ifp
, struct mbuf
*m
)
3727 struct ether_header
*eh
= mtod(m
, struct ether_header
*);
3728 uint32_t sw_csum
, hwcap
;
3731 hwcap
= (ifp
->if_hwassist
| CSUM_DATA_VALID
);
3735 /* do in software what the hardware cannot */
3736 sw_csum
= m
->m_pkthdr
.csum_flags
& ~IF_HWASSIST_CSUM_FLAGS(hwcap
);
3737 sw_csum
&= IF_HWASSIST_CSUM_MASK
;
3739 switch (ntohs(eh
->ether_type
)) {
3741 if ((hwcap
& CSUM_PARTIAL
) && !(sw_csum
& CSUM_DELAY_DATA
) &&
3742 (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
)) {
3743 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP
) {
3745 sizeof (*eh
) + sizeof (struct ip
);
3747 m
->m_pkthdr
.csum_data
& 0xffff;
3748 m
->m_pkthdr
.csum_flags
|=
3749 (CSUM_DATA_VALID
| CSUM_PARTIAL
);
3750 m
->m_pkthdr
.csum_tx_stuff
= (ulpoff
+ start
);
3751 m
->m_pkthdr
.csum_tx_start
= start
;
3753 sw_csum
|= (CSUM_DELAY_DATA
&
3754 m
->m_pkthdr
.csum_flags
);
3757 (void) in_finalize_cksum(m
, sizeof (*eh
), sw_csum
);
3761 case ETHERTYPE_IPV6
:
3762 if ((hwcap
& CSUM_PARTIAL
) &&
3763 !(sw_csum
& CSUM_DELAY_IPV6_DATA
) &&
3764 (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
)) {
3765 if (m
->m_pkthdr
.csum_flags
& CSUM_TCPIPV6
) {
3767 sizeof (*eh
) + sizeof (struct ip6_hdr
);
3769 m
->m_pkthdr
.csum_data
& 0xffff;
3770 m
->m_pkthdr
.csum_flags
|=
3771 (CSUM_DATA_VALID
| CSUM_PARTIAL
);
3772 m
->m_pkthdr
.csum_tx_stuff
= (ulpoff
+ start
);
3773 m
->m_pkthdr
.csum_tx_start
= start
;
3775 sw_csum
|= (CSUM_DELAY_IPV6_DATA
&
3776 m
->m_pkthdr
.csum_flags
);
3779 (void) in6_finalize_cksum(m
, sizeof (*eh
), -1, -1, sw_csum
);
3788 * Start output on a bridge.
3790 * This routine is invoked by the start worker thread; because we never call
3791 * it directly, there is no need do deploy any serialization mechanism other
3792 * than what's already used by the worker thread, i.e. this is already single
3795 * This routine is called only when if_bridge_txstart is enabled.
3798 bridge_start(struct ifnet
*ifp
)
3803 if (ifnet_dequeue(ifp
, &m
) != 0)
3806 (void) bridge_output(ifp
, m
);
3813 * The forwarding function of the bridge.
3815 * NOTE: Releases the lock on return.
3818 bridge_forward(struct bridge_softc
*sc
, struct bridge_iflist
*sbif
,
3821 struct bridge_iflist
*dbif
;
3822 struct ifnet
*src_if
, *dst_if
, *ifp
;
3823 struct ether_header
*eh
;
3828 BRIDGE_LOCK_ASSERT_HELD(sc
);
3831 if (if_bridge_debug
& BR_DBGF_OUTPPUT
)
3832 printf("%s: %s m 0x%llx\n", __func__
, sc
->sc_ifp
->if_xname
,
3833 (uint64_t)VM_KERNEL_ADDRPERM(m
));
3834 #endif /* BRIDGE_DEBUG */
3836 src_if
= m
->m_pkthdr
.rcvif
;
3839 (void) ifnet_stat_increment_in(ifp
, 1, m
->m_pkthdr
.len
, 0);
3840 vlan
= VLANTAGOF(m
);
3843 if ((sbif
->bif_ifflags
& IFBIF_STP
) &&
3844 sbif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
)
3847 eh
= mtod(m
, struct ether_header
*);
3848 dst
= eh
->ether_dhost
;
3850 /* If the interface is learning, record the address. */
3851 if (sbif
->bif_ifflags
& IFBIF_LEARNING
) {
3852 error
= bridge_rtupdate(sc
, eh
->ether_shost
, vlan
,
3853 sbif
, 0, IFBAF_DYNAMIC
);
3855 * If the interface has addresses limits then deny any source
3856 * that is not in the cache.
3858 if (error
&& sbif
->bif_addrmax
)
3862 if ((sbif
->bif_ifflags
& IFBIF_STP
) != 0 &&
3863 sbif
->bif_stp
.bp_state
== BSTP_IFSTATE_LEARNING
)
3867 * At this point, the port either doesn't participate
3868 * in spanning tree or it is in the forwarding state.
3872 * If the packet is unicast, destined for someone on
3873 * "this" side of the bridge, drop it.
3875 if ((m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0) {
3876 dst_if
= bridge_rtlookup(sc
, dst
, vlan
);
3877 if (src_if
== dst_if
)
3881 * Check if its a reserved multicast address, any address
3882 * listed in 802.1D section 7.12.6 may not be forwarded by the
3884 * This is currently 01-80-C2-00-00-00 to 01-80-C2-00-00-0F
3886 if (dst
[0] == 0x01 && dst
[1] == 0x80 &&
3887 dst
[2] == 0xc2 && dst
[3] == 0x00 &&
3888 dst
[4] == 0x00 && dst
[5] <= 0x0f)
3892 /* ...forward it to all interfaces. */
3893 atomic_add_64(&ifp
->if_imcasts
, 1);
3898 * If we have a destination interface which is a member of our bridge,
3899 * OR this is a unicast packet, push it through the bpf(4) machinery.
3900 * For broadcast or multicast packets, don't bother because it will
3901 * be reinjected into ether_input. We do this before we pass the packets
3902 * through the pfil(9) framework, as it is possible that pfil(9) will
3903 * drop the packet, or possibly modify it, making it difficult to debug
3904 * firewall issues on the bridge.
3907 if (eh
->ether_type
== htons(ETHERTYPE_RSN_PREAUTH
) ||
3908 dst_if
!= NULL
|| (m
->m_flags
& (M_BCAST
| M_MCAST
)) == 0) {
3909 m
->m_pkthdr
.rcvif
= ifp
;
3910 if (sc
->sc_bpf_input
)
3911 bridge_bpf_input(ifp
, m
);
3913 #endif /* NBPFILTER */
3915 #if defined(PFIL_HOOKS)
3916 /* run the packet filter */
3917 if (PFIL_HOOKED(&inet_pfil_hook
) || PFIL_HOOKED_INET6
) {
3919 if (bridge_pfil(&m
, ifp
, src_if
, PFIL_IN
) != 0)
3925 #endif /* PFIL_HOOKS */
3927 if (dst_if
== NULL
) {
3928 bridge_broadcast(sc
, src_if
, m
, 1);
3933 * At this point, we're dealing with a unicast frame
3934 * going to a different interface.
3936 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
3939 dbif
= bridge_lookup_member_if(sc
, dst_if
);
3941 /* Not a member of the bridge (anymore?) */
3944 /* Private segments can not talk to each other */
3945 if (sbif
->bif_ifflags
& dbif
->bif_ifflags
& IFBIF_PRIVATE
)
3948 if ((dbif
->bif_ifflags
& IFBIF_STP
) &&
3949 dbif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
)
3953 /* APPLE MODIFICATION <rdar:6985737> */
3954 if ((dst_if
->if_extflags
& IFEXTF_DHCPRA_MASK
) != 0) {
3955 m
= ip_xdhcpra_output(dst_if
, m
);
3957 ++sc
->sc_sc
.sc_ifp
.if_xdhcpra
;
3961 #endif /* HAS_DHCPRA_MASK */
3965 #if defined(PFIL_HOOKS)
3966 if (PFIL_HOOKED(&inet_pfil_hook
) || PFIL_HOOKED_INET6
) {
3967 if (bridge_pfil(&m
, ifp
, dst_if
, PFIL_OUT
) != 0)
3972 #endif /* PFIL_HOOKS */
3974 (void) bridge_enqueue(sc
, dst_if
, m
);
3984 char *ether_ntop(char *, size_t, const u_char
*);
3986 __private_extern__
char *
3987 ether_ntop(char *buf
, size_t len
, const u_char
*ap
)
3989 snprintf(buf
, len
, "%02x:%02x:%02x:%02x:%02x:%02x",
3990 ap
[0], ap
[1], ap
[2], ap
[3], ap
[4], ap
[5]);
3995 #endif /* BRIDGE_DEBUG */
4000 * Filter input from a member interface. Queue the packet for
4001 * bridging if it is not for us.
4003 __private_extern__ errno_t
4004 bridge_input(struct ifnet
*ifp
, struct mbuf
*m
, void *frame_header
)
4006 struct bridge_softc
*sc
= ifp
->if_bridge
;
4007 struct bridge_iflist
*bif
, *bif2
;
4009 struct ether_header
*eh
;
4010 struct mbuf
*mc
, *mc2
;
4015 if (if_bridge_debug
& BR_DBGF_INPUT
)
4016 printf("%s: %s from %s m 0x%llx data 0x%llx\n", __func__
,
4017 sc
->sc_ifp
->if_xname
, ifp
->if_xname
,
4018 (uint64_t)VM_KERNEL_ADDRPERM(m
),
4019 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m
)));
4020 #endif /* BRIDGE_DEBUG */
4022 if ((sc
->sc_ifp
->if_flags
& IFF_RUNNING
) == 0) {
4024 if (if_bridge_debug
& BR_DBGF_INPUT
)
4025 printf("%s: %s not running passing along\n",
4026 __func__
, sc
->sc_ifp
->if_xname
);
4027 #endif /* BRIDGE_DEBUG */
4032 vlan
= VLANTAGOF(m
);
4036 * Implement support for bridge monitoring. If this flag has been
4037 * set on this interface, discard the packet once we push it through
4038 * the bpf(4) machinery, but before we do, increment the byte and
4039 * packet counters associated with this interface.
4041 if ((bifp
->if_flags
& IFF_MONITOR
) != 0) {
4042 m
->m_pkthdr
.rcvif
= bifp
;
4043 BRIDGE_BPF_MTAP_INPUT(sc
, m
);
4044 (void) ifnet_stat_increment_in(bifp
, 1, m
->m_pkthdr
.len
, 0);
4046 return (EJUSTRETURN
);
4048 #endif /* IFF_MONITOR */
4051 * Need to clear the promiscous flags otherwise it will be
4052 * dropped by DLIL after processing filters
4054 if ((mbuf_flags(m
) & MBUF_PROMISC
))
4055 mbuf_setflags_mask(m
, 0, MBUF_PROMISC
);
4058 bif
= bridge_lookup_member_if(sc
, ifp
);
4062 if (if_bridge_debug
& BR_DBGF_INPUT
)
4063 printf("%s: %s bridge_lookup_member_if failed\n",
4064 __func__
, sc
->sc_ifp
->if_xname
);
4065 #endif /* BRIDGE_DEBUG */
4069 eh
= mtod(m
, struct ether_header
*);
4073 if (m
->m_flags
& (M_BCAST
|M_MCAST
)) {
4076 if (if_bridge_debug
& BR_DBGF_MCAST
)
4077 if ((m
->m_flags
& M_MCAST
))
4078 printf("%s: multicast: "
4079 "%02x:%02x:%02x:%02x:%02x:%02x\n",
4081 eh
->ether_dhost
[0], eh
->ether_dhost
[1],
4082 eh
->ether_dhost
[2], eh
->ether_dhost
[3],
4083 eh
->ether_dhost
[4], eh
->ether_dhost
[5]);
4084 #endif /* BRIDGE_DEBUG */
4086 /* Tap off 802.1D packets; they do not get forwarded. */
4087 if (memcmp(eh
->ether_dhost
, bstp_etheraddr
,
4088 ETHER_ADDR_LEN
) == 0) {
4090 m
= bstp_input(&bif
->bif_stp
, ifp
, m
);
4091 #else /* !BRIDGESTP */
4094 #endif /* !BRIDGESTP */
4097 return (EJUSTRETURN
);
4101 if ((bif
->bif_ifflags
& IFBIF_STP
) &&
4102 bif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
) {
4108 * Make a deep copy of the packet and enqueue the copy
4109 * for bridge processing; return the original packet for
4112 mc
= m_dup(m
, M_DONTWAIT
);
4119 * Perform the bridge forwarding function with the copy.
4121 * Note that bridge_forward calls BRIDGE_UNLOCK
4123 bridge_forward(sc
, bif
, mc
);
4126 * Reinject the mbuf as arriving on the bridge so we have a
4127 * chance at claiming multicast packets. We can not loop back
4128 * here from ether_input as a bridge is never a member of a
4131 VERIFY(bifp
->if_bridge
== NULL
);
4132 mc2
= m_dup(m
, M_DONTWAIT
);
4134 /* Keep the layer3 header aligned */
4135 int i
= min(mc2
->m_pkthdr
.len
, max_protohdr
);
4136 mc2
= m_copyup(mc2
, i
, ETHER_ALIGN
);
4139 /* mark packet as arriving on the bridge */
4140 mc2
->m_pkthdr
.rcvif
= bifp
;
4141 mc2
->m_pkthdr
.pkt_hdr
= mbuf_data(mc2
);
4144 if (sc
->sc_bpf_input
)
4145 bridge_bpf_input(bifp
, mc2
);
4146 #endif /* NBPFILTER */
4147 (void) mbuf_setdata(mc2
,
4148 (char *)mbuf_data(mc2
) + ETHER_HDR_LEN
,
4149 mbuf_len(mc2
) - ETHER_HDR_LEN
);
4150 (void) mbuf_pkthdr_adjustlen(mc2
, - ETHER_HDR_LEN
);
4152 (void) ifnet_stat_increment_in(bifp
, 1,
4153 mbuf_pkthdr_len(mc2
), 0);
4156 if (if_bridge_debug
& BR_DBGF_MCAST
)
4157 printf("%s: %s mcast for us\n", __func__
,
4158 sc
->sc_ifp
->if_xname
);
4159 #endif /* BRIDGE_DEBUG */
4161 dlil_input_packet_list(bifp
, mc2
);
4164 /* Return the original packet for local processing. */
4168 if ((bif
->bif_ifflags
& IFBIF_STP
) &&
4169 bif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
) {
4175 #define CARP_CHECK_WE_ARE_DST(iface) \
4176 ((iface)->if_carp &&\
4177 carp_forus((iface)->if_carp, eh->ether_dhost))
4178 #define CARP_CHECK_WE_ARE_SRC(iface) \
4179 ((iface)->if_carp &&\
4180 carp_forus((iface)->if_carp, eh->ether_shost))
4182 #define CARP_CHECK_WE_ARE_DST(iface) 0
4183 #define CARP_CHECK_WE_ARE_SRC(iface) 0
4187 #define PFIL_HOOKED_INET6 PFIL_HOOKED(&inet6_pfil_hook)
4189 #define PFIL_HOOKED_INET6 0
4192 #if defined(PFIL_HOOKS)
4193 #define PFIL_PHYS(sc, ifp, m) do { \
4194 if (pfil_local_phys && \
4195 (PFIL_HOOKED(&inet_pfil_hook) || PFIL_HOOKED_INET6)) { \
4196 if (bridge_pfil(&m, NULL, ifp, \
4197 PFIL_IN) != 0 || m == NULL) { \
4198 BRIDGE_UNLOCK(sc); \
4203 #else /* PFIL_HOOKS */
4204 #define PFIL_PHYS(sc, ifp, m)
4205 #endif /* PFIL_HOOKS */
4207 #define GRAB_OUR_PACKETS(iface) \
4208 if ((iface)->if_type == IFT_GIF) \
4210 /* It is destined for us. */ \
4211 if (memcmp(IF_LLADDR((iface)), eh->ether_dhost, \
4212 ETHER_ADDR_LEN) == 0 || CARP_CHECK_WE_ARE_DST((iface))) { \
4213 if ((iface)->if_type == IFT_BRIDGE) { \
4214 BRIDGE_BPF_MTAP_INPUT(sc, m); \
4215 /* Filter on the physical interface. */ \
4216 PFIL_PHYS(sc, iface, m); \
4218 if (bif->bif_ifflags & IFBIF_LEARNING) { \
4219 error = bridge_rtupdate(sc, eh->ether_shost, \
4220 vlan, bif, 0, IFBAF_DYNAMIC); \
4221 if (error && bif->bif_addrmax) { \
4222 BRIDGE_UNLOCK(sc); \
4223 return (EJUSTRETURN); \
4226 m->m_pkthdr.rcvif = iface; \
4227 BRIDGE_UNLOCK(sc); \
4231 /* We just received a packet that we sent out. */ \
4232 if (memcmp(IF_LLADDR((iface)), eh->ether_shost, \
4233 ETHER_ADDR_LEN) == 0 || CARP_CHECK_WE_ARE_SRC((iface))) { \
4234 BRIDGE_UNLOCK(sc); \
4235 return (EJUSTRETURN); \
4242 * If the packet is for us, set the packets source as the
4243 * bridge, and return the packet back to ether_input for
4246 if (memcmp(eh
->ether_dhost
, IF_LLADDR(bifp
),
4247 ETHER_ADDR_LEN
) == 0 || CARP_CHECK_WE_ARE_DST(bifp
)) {
4249 /* Mark the packet as arriving on the bridge interface */
4250 (void) mbuf_pkthdr_setrcvif(m
, bifp
);
4251 mbuf_pkthdr_setheader(m
, frame_header
);
4254 * If the interface is learning, and the source
4255 * address is valid and not multicast, record
4258 if (bif
->bif_ifflags
& IFBIF_LEARNING
)
4259 (void) bridge_rtupdate(sc
, eh
->ether_shost
,
4260 vlan
, bif
, 0, IFBAF_DYNAMIC
);
4262 BRIDGE_BPF_MTAP_INPUT(sc
, m
);
4264 (void) mbuf_setdata(m
, (char *)mbuf_data(m
) + ETHER_HDR_LEN
,
4265 mbuf_len(m
) - ETHER_HDR_LEN
);
4266 (void) mbuf_pkthdr_adjustlen(m
, - ETHER_HDR_LEN
);
4268 (void) ifnet_stat_increment_in(bifp
, 1, mbuf_pkthdr_len(m
), 0);
4273 if (if_bridge_debug
& BR_DBGF_INPUT
)
4274 printf("%s: %s packet for bridge\n", __func__
,
4275 sc
->sc_ifp
->if_xname
);
4276 #endif /* BRIDGE_DEBUG */
4278 dlil_input_packet_list(bifp
, m
);
4280 return (EJUSTRETURN
);
4284 * if the destination of the packet is for the MAC address of
4285 * the member interface itself, then we don't need to forward
4286 * it -- just pass it back. Note that it'll likely just be
4287 * dropped by the stack, but if something else is bound to
4288 * the interface directly (for example, the wireless stats
4289 * protocol -- although that actually uses BPF right now),
4290 * then it will consume the packet
4292 * ALSO, note that we do this check AFTER checking for the
4293 * bridge's own MAC address, because the bridge may be
4294 * using the SAME MAC address as one of its interfaces
4296 if (memcmp(eh
->ether_dhost
, IF_LLADDR(ifp
), ETHER_ADDR_LEN
) == 0) {
4298 #ifdef VERY_VERY_VERY_DIAGNOSTIC
4299 printf("%s: not forwarding packet bound for member "
4300 "interface\n", __func__
);
4306 /* Now check the all bridge members. */
4307 TAILQ_FOREACH(bif2
, &sc
->sc_iflist
, bif_next
) {
4308 GRAB_OUR_PACKETS(bif2
->bif_ifp
)
4311 #undef CARP_CHECK_WE_ARE_DST
4312 #undef CARP_CHECK_WE_ARE_SRC
4313 #undef GRAB_OUR_PACKETS
4316 * Perform the bridge forwarding function.
4318 * Note that bridge_forward calls BRIDGE_UNLOCK
4320 bridge_forward(sc
, bif
, m
);
4322 return (EJUSTRETURN
);
4328 * Send a frame to all interfaces that are members of
4329 * the bridge, except for the one on which the packet
4332 * NOTE: Releases the lock on return.
4335 bridge_broadcast(struct bridge_softc
*sc
, struct ifnet
*src_if
,
4336 struct mbuf
*m
, int runfilt
)
4339 #pragma unused(runfilt)
4341 struct bridge_iflist
*dbif
, *sbif
;
4343 struct ifnet
*dst_if
;
4344 int error
= 0, used
= 0;
4346 sbif
= bridge_lookup_member_if(sc
, src_if
);
4348 BRIDGE_LOCK2REF(sc
, error
);
4355 /* Filter on the bridge interface before broadcasting */
4356 if (runfilt
&& (PFIL_HOOKED(&inet_pfil_hook
) || PFIL_HOOKED_INET6
)) {
4357 if (bridge_pfil(&m
, sc
->sc_ifp
, NULL
, PFIL_OUT
) != 0)
4362 #endif /* PFIL_HOOKS */
4364 TAILQ_FOREACH(dbif
, &sc
->sc_iflist
, bif_next
) {
4365 dst_if
= dbif
->bif_ifp
;
4366 if (dst_if
== src_if
)
4369 /* Private segments can not talk to each other */
4371 (sbif
->bif_ifflags
& dbif
->bif_ifflags
& IFBIF_PRIVATE
))
4374 if ((dbif
->bif_ifflags
& IFBIF_STP
) &&
4375 dbif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
)
4378 if ((dbif
->bif_ifflags
& IFBIF_DISCOVER
) == 0 &&
4379 (m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0)
4382 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
4385 if (TAILQ_NEXT(dbif
, bif_next
) == NULL
) {
4389 mc
= m_dup(m
, M_DONTWAIT
);
4391 (void) ifnet_stat_increment_out(sc
->sc_ifp
,
4399 * Filter on the output interface. Pass a NULL bridge interface
4400 * pointer so we do not redundantly filter on the bridge for
4401 * each interface we broadcast on.
4404 (PFIL_HOOKED(&inet_pfil_hook
) || PFIL_HOOKED_INET6
)) {
4406 /* Keep the layer3 header aligned */
4407 int i
= min(mc
->m_pkthdr
.len
, max_protohdr
);
4408 mc
= m_copyup(mc
, i
, ETHER_ALIGN
);
4410 (void) ifnet_stat_increment_out(
4411 sc
->sc_ifp
, 0, 0, 1);
4415 if (bridge_pfil(&mc
, NULL
, dst_if
, PFIL_OUT
) != 0)
4420 #endif /* PFIL_HOOKS */
4422 (void) bridge_enqueue(sc
, dst_if
, mc
);
4429 #endif /* PFIL_HOOKS */
4437 * Duplicate a packet out one or more interfaces that are in span mode,
4438 * the original mbuf is unmodified.
4441 bridge_span(struct bridge_softc
*sc
, struct mbuf
*m
)
4443 struct bridge_iflist
*bif
;
4444 struct ifnet
*dst_if
;
4447 if (TAILQ_EMPTY(&sc
->sc_spanlist
))
4450 TAILQ_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
) {
4451 dst_if
= bif
->bif_ifp
;
4453 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0)
4456 mc
= m_copypacket(m
, M_DONTWAIT
);
4458 (void) ifnet_stat_increment_out(sc
->sc_ifp
, 0, 0, 1);
4462 (void) bridge_enqueue(sc
, dst_if
, mc
);
4470 * Add a bridge routing entry.
4473 bridge_rtupdate(struct bridge_softc
*sc
, const uint8_t *dst
, uint16_t vlan
,
4474 struct bridge_iflist
*bif
, int setflags
, uint8_t flags
)
4476 struct bridge_rtnode
*brt
;
4479 BRIDGE_LOCK_ASSERT_HELD(sc
);
4481 /* Check the source address is valid and not multicast. */
4482 if (ETHER_IS_MULTICAST(dst
) ||
4483 (dst
[0] == 0 && dst
[1] == 0 && dst
[2] == 0 &&
4484 dst
[3] == 0 && dst
[4] == 0 && dst
[5] == 0) != 0)
4488 /* 802.1p frames map to vlan 1 */
4493 * A route for this destination might already exist. If so,
4494 * update it, otherwise create a new one.
4496 if ((brt
= bridge_rtnode_lookup(sc
, dst
, vlan
)) == NULL
) {
4497 if (sc
->sc_brtcnt
>= sc
->sc_brtmax
) {
4498 sc
->sc_brtexceeded
++;
4501 /* Check per interface address limits (if enabled) */
4502 if (bif
->bif_addrmax
&& bif
->bif_addrcnt
>= bif
->bif_addrmax
) {
4503 bif
->bif_addrexceeded
++;
4508 * Allocate a new bridge forwarding node, and
4509 * initialize the expiration time and Ethernet
4512 brt
= zalloc_noblock(bridge_rtnode_pool
);
4515 bzero(brt
, sizeof(struct bridge_rtnode
));
4517 if (bif
->bif_ifflags
& IFBIF_STICKY
)
4518 brt
->brt_flags
= IFBAF_STICKY
;
4520 brt
->brt_flags
= IFBAF_DYNAMIC
;
4522 memcpy(brt
->brt_addr
, dst
, ETHER_ADDR_LEN
);
4523 brt
->brt_vlan
= vlan
;
4526 if ((error
= bridge_rtnode_insert(sc
, brt
)) != 0) {
4527 zfree(bridge_rtnode_pool
, brt
);
4533 if (if_bridge_debug
& BR_DBGF_RT_TABLE
)
4534 printf("%s: added %02x:%02x:%02x:%02x:%02x:%02x "
4535 "on %s count %u hashsize %u\n", __func__
,
4536 dst
[0], dst
[1], dst
[2], dst
[3], dst
[4], dst
[5],
4537 sc
->sc_ifp
->if_xname
, sc
->sc_brtcnt
,
4538 sc
->sc_rthash_size
);
4542 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
&&
4543 brt
->brt_dst
!= bif
) {
4544 brt
->brt_dst
->bif_addrcnt
--;
4546 brt
->brt_dst
->bif_addrcnt
++;
4549 if ((flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
4552 now
= (unsigned long) net_uptime();
4553 brt
->brt_expire
= now
+ sc
->sc_brttimeout
;
4556 brt
->brt_flags
= flags
;
4565 * Lookup the destination interface for an address.
4567 static struct ifnet
*
4568 bridge_rtlookup(struct bridge_softc
*sc
, const uint8_t *addr
, uint16_t vlan
)
4570 struct bridge_rtnode
*brt
;
4572 BRIDGE_LOCK_ASSERT_HELD(sc
);
4574 if ((brt
= bridge_rtnode_lookup(sc
, addr
, vlan
)) == NULL
)
4577 return (brt
->brt_ifp
);
4583 * Trim the routine table so that we have a number
4584 * of routing entries less than or equal to the
4588 bridge_rttrim(struct bridge_softc
*sc
)
4590 struct bridge_rtnode
*brt
, *nbrt
;
4592 BRIDGE_LOCK_ASSERT_HELD(sc
);
4594 /* Make sure we actually need to do this. */
4595 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
4598 /* Force an aging cycle; this might trim enough addresses. */
4600 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
4603 LIST_FOREACH_SAFE(brt
, &sc
->sc_rtlist
, brt_list
, nbrt
) {
4604 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
4605 bridge_rtnode_destroy(sc
, brt
);
4606 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
4613 * bridge_aging_timer:
4615 * Aging periodic timer for the bridge routing table.
4618 bridge_aging_timer(struct bridge_softc
*sc
)
4620 BRIDGE_LOCK_ASSERT_HELD(sc
);
4624 if ((sc
->sc_ifp
->if_flags
& IFF_RUNNING
) &&
4625 (sc
->sc_flags
& SCF_DETACHING
) == 0) {
4626 sc
->sc_aging_timer
.bdc_sc
= sc
;
4627 sc
->sc_aging_timer
.bdc_func
= bridge_aging_timer
;
4628 sc
->sc_aging_timer
.bdc_ts
.tv_sec
= bridge_rtable_prune_period
;
4629 bridge_schedule_delayed_call(&sc
->sc_aging_timer
);
4636 * Perform an aging cycle.
4639 bridge_rtage(struct bridge_softc
*sc
)
4641 struct bridge_rtnode
*brt
, *nbrt
;
4644 BRIDGE_LOCK_ASSERT_HELD(sc
);
4646 now
= (unsigned long) net_uptime();
4648 LIST_FOREACH_SAFE(brt
, &sc
->sc_rtlist
, brt_list
, nbrt
) {
4649 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
4650 if (now
>= brt
->brt_expire
)
4651 bridge_rtnode_destroy(sc
, brt
);
4659 * Remove all dynamic addresses from the bridge.
4662 bridge_rtflush(struct bridge_softc
*sc
, int full
)
4664 struct bridge_rtnode
*brt
, *nbrt
;
4666 BRIDGE_LOCK_ASSERT_HELD(sc
);
4668 LIST_FOREACH_SAFE(brt
, &sc
->sc_rtlist
, brt_list
, nbrt
) {
4669 if (full
|| (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
4670 bridge_rtnode_destroy(sc
, brt
);
4677 * Remove an address from the table.
4680 bridge_rtdaddr(struct bridge_softc
*sc
, const uint8_t *addr
, uint16_t vlan
)
4682 struct bridge_rtnode
*brt
;
4685 BRIDGE_LOCK_ASSERT_HELD(sc
);
4688 * If vlan is zero then we want to delete for all vlans so the lookup
4689 * may return more than one.
4691 while ((brt
= bridge_rtnode_lookup(sc
, addr
, vlan
)) != NULL
) {
4692 bridge_rtnode_destroy(sc
, brt
);
4696 return (found
? 0 : ENOENT
);
4702 * Delete routes to a speicifc member interface.
4705 bridge_rtdelete(struct bridge_softc
*sc
, struct ifnet
*ifp
, int full
)
4707 struct bridge_rtnode
*brt
, *nbrt
;
4709 BRIDGE_LOCK_ASSERT_HELD(sc
);
4711 LIST_FOREACH_SAFE(brt
, &sc
->sc_rtlist
, brt_list
, nbrt
) {
4712 if (brt
->brt_ifp
== ifp
&& (full
||
4713 (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
))
4714 bridge_rtnode_destroy(sc
, brt
);
4719 * bridge_rtable_init:
4721 * Initialize the route table for this bridge.
4724 bridge_rtable_init(struct bridge_softc
*sc
)
4728 sc
->sc_rthash
= _MALLOC(sizeof (*sc
->sc_rthash
) * BRIDGE_RTHASH_SIZE
,
4729 M_DEVBUF
, M_WAITOK
| M_ZERO
);
4730 if (sc
->sc_rthash
== NULL
) {
4731 printf("%s: no memory\n", __func__
);
4734 sc
->sc_rthash_size
= BRIDGE_RTHASH_SIZE
;
4736 for (i
= 0; i
< sc
->sc_rthash_size
; i
++)
4737 LIST_INIT(&sc
->sc_rthash
[i
]);
4739 sc
->sc_rthash_key
= RandomULong();
4741 LIST_INIT(&sc
->sc_rtlist
);
4747 * bridge_rthash_delayed_resize:
4749 * Resize the routing table hash on a delayed thread call.
4752 bridge_rthash_delayed_resize(struct bridge_softc
*sc
)
4754 u_int32_t new_rthash_size
;
4755 struct _bridge_rtnode_list
*new_rthash
= NULL
;
4756 struct _bridge_rtnode_list
*old_rthash
= NULL
;
4758 struct bridge_rtnode
*brt
;
4761 BRIDGE_LOCK_ASSERT_HELD(sc
);
4764 * Four entries per hash bucket is our ideal load factor
4766 if (sc
->sc_brtcnt
< sc
->sc_rthash_size
* 4)
4770 * Doubling the number of hash buckets may be too simplistic
4771 * especially when facing a spike of new entries
4773 new_rthash_size
= sc
->sc_rthash_size
* 2;
4775 sc
->sc_flags
|= SCF_RESIZING
;
4778 new_rthash
= _MALLOC(sizeof (*sc
->sc_rthash
) * new_rthash_size
,
4779 M_DEVBUF
, M_WAITOK
| M_ZERO
);
4782 sc
->sc_flags
&= ~SCF_RESIZING
;
4784 if (new_rthash
== NULL
) {
4788 if ((sc
->sc_flags
& SCF_DETACHING
)) {
4793 * Fail safe from here on
4795 old_rthash
= sc
->sc_rthash
;
4796 sc
->sc_rthash
= new_rthash
;
4797 sc
->sc_rthash_size
= new_rthash_size
;
4800 * Get a new key to force entries to be shuffled around to reduce
4801 * the likelihood they will land in the same buckets
4803 sc
->sc_rthash_key
= RandomULong();
4805 for (i
= 0; i
< sc
->sc_rthash_size
; i
++)
4806 LIST_INIT(&sc
->sc_rthash
[i
]);
4808 LIST_FOREACH(brt
, &sc
->sc_rtlist
, brt_list
) {
4809 LIST_REMOVE(brt
, brt_hash
);
4810 (void) bridge_rtnode_hash(sc
, brt
);
4815 if (if_bridge_debug
& BR_DBGF_RT_TABLE
)
4816 printf("%s: %s new size %u\n", __func__
,
4817 sc
->sc_ifp
->if_xname
, sc
->sc_rthash_size
);
4818 #endif /* BRIDGE_DEBUG */
4820 _FREE(old_rthash
, M_DEVBUF
);
4823 printf("%s: %s failed %d\n", __func__
,
4824 sc
->sc_ifp
->if_xname
, error
);
4825 #endif /* BRIDGE_DEBUG */
4826 if (new_rthash
!= NULL
)
4827 _FREE(new_rthash
, M_DEVBUF
);
4832 * Resize the number of hash buckets based on the load factor
4833 * Currently only grow
4834 * Failing to resize the hash table is not fatal
4837 bridge_rthash_resize(struct bridge_softc
*sc
)
4839 BRIDGE_LOCK_ASSERT_HELD(sc
);
4841 if ((sc
->sc_flags
& SCF_DETACHING
) || (sc
->sc_flags
& SCF_RESIZING
))
4845 * Four entries per hash bucket is our ideal load factor
4847 if (sc
->sc_brtcnt
< sc
->sc_rthash_size
* 4)
4850 * Hard limit on the size of the routing hash table
4852 if (sc
->sc_rthash_size
>= bridge_rtable_hash_size_max
)
4855 sc
->sc_resize_call
.bdc_sc
= sc
;
4856 sc
->sc_resize_call
.bdc_func
= bridge_rthash_delayed_resize
;
4857 bridge_schedule_delayed_call(&sc
->sc_resize_call
);
4861 * bridge_rtable_fini:
4863 * Deconstruct the route table for this bridge.
4866 bridge_rtable_fini(struct bridge_softc
*sc
)
4868 KASSERT(sc
->sc_brtcnt
== 0,
4869 ("%s: %d bridge routes referenced", __func__
, sc
->sc_brtcnt
));
4870 if (sc
->sc_rthash
) {
4871 _FREE(sc
->sc_rthash
, M_DEVBUF
);
4872 sc
->sc_rthash
= NULL
;
4877 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
4878 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
4880 #define mix(a, b, c) \
4882 a -= b; a -= c; a ^= (c >> 13); \
4883 b -= c; b -= a; b ^= (a << 8); \
4884 c -= a; c -= b; c ^= (b >> 13); \
4885 a -= b; a -= c; a ^= (c >> 12); \
4886 b -= c; b -= a; b ^= (a << 16); \
4887 c -= a; c -= b; c ^= (b >> 5); \
4888 a -= b; a -= c; a ^= (c >> 3); \
4889 b -= c; b -= a; b ^= (a << 10); \
4890 c -= a; c -= b; c ^= (b >> 15); \
4891 } while (/*CONSTCOND*/0)
4893 static __inline
uint32_t
4894 bridge_rthash(struct bridge_softc
*sc
, const uint8_t *addr
)
4896 uint32_t a
= 0x9e3779b9, b
= 0x9e3779b9, c
= sc
->sc_rthash_key
;
4907 return (c
& BRIDGE_RTHASH_MASK(sc
));
4913 bridge_rtnode_addr_cmp(const uint8_t *a
, const uint8_t *b
)
4917 for (i
= 0, d
= 0; i
< ETHER_ADDR_LEN
&& d
== 0; i
++) {
4918 d
= ((int)a
[i
]) - ((int)b
[i
]);
4925 * bridge_rtnode_lookup:
4927 * Look up a bridge route node for the specified destination. Compare the
4928 * vlan id or if zero then just return the first match.
4930 static struct bridge_rtnode
*
4931 bridge_rtnode_lookup(struct bridge_softc
*sc
, const uint8_t *addr
,
4934 struct bridge_rtnode
*brt
;
4938 BRIDGE_LOCK_ASSERT_HELD(sc
);
4940 hash
= bridge_rthash(sc
, addr
);
4941 LIST_FOREACH(brt
, &sc
->sc_rthash
[hash
], brt_hash
) {
4942 dir
= bridge_rtnode_addr_cmp(addr
, brt
->brt_addr
);
4943 if (dir
== 0 && (brt
->brt_vlan
== vlan
|| vlan
== 0))
4953 * bridge_rtnode_hash:
4955 * Insert the specified bridge node into the route hash table.
4956 * This is used when adding a new node or to rehash when resizing
4960 bridge_rtnode_hash(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
4962 struct bridge_rtnode
*lbrt
;
4966 BRIDGE_LOCK_ASSERT_HELD(sc
);
4968 hash
= bridge_rthash(sc
, brt
->brt_addr
);
4970 lbrt
= LIST_FIRST(&sc
->sc_rthash
[hash
]);
4972 LIST_INSERT_HEAD(&sc
->sc_rthash
[hash
], brt
, brt_hash
);
4977 dir
= bridge_rtnode_addr_cmp(brt
->brt_addr
, lbrt
->brt_addr
);
4978 if (dir
== 0 && brt
->brt_vlan
== lbrt
->brt_vlan
) {
4980 if (if_bridge_debug
& BR_DBGF_RT_TABLE
)
4981 printf("%s: %s EEXIST "
4982 "%02x:%02x:%02x:%02x:%02x:%02x\n",
4983 __func__
, sc
->sc_ifp
->if_xname
,
4984 brt
->brt_addr
[0], brt
->brt_addr
[1],
4985 brt
->brt_addr
[2], brt
->brt_addr
[3],
4986 brt
->brt_addr
[4], brt
->brt_addr
[5]);
4991 LIST_INSERT_BEFORE(lbrt
, brt
, brt_hash
);
4994 if (LIST_NEXT(lbrt
, brt_hash
) == NULL
) {
4995 LIST_INSERT_AFTER(lbrt
, brt
, brt_hash
);
4998 lbrt
= LIST_NEXT(lbrt
, brt_hash
);
4999 } while (lbrt
!= NULL
);
5002 if (if_bridge_debug
& BR_DBGF_RT_TABLE
)
5003 printf("%s: %s impossible %02x:%02x:%02x:%02x:%02x:%02x\n",
5004 __func__
, sc
->sc_ifp
->if_xname
,
5005 brt
->brt_addr
[0], brt
->brt_addr
[1], brt
->brt_addr
[2],
5006 brt
->brt_addr
[3], brt
->brt_addr
[4], brt
->brt_addr
[5]);
5014 * bridge_rtnode_insert:
5016 * Insert the specified bridge node into the route table. We
5017 * assume the entry is not already in the table.
5020 bridge_rtnode_insert(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
5024 error
= bridge_rtnode_hash(sc
, brt
);
5028 LIST_INSERT_HEAD(&sc
->sc_rtlist
, brt
, brt_list
);
5031 bridge_rthash_resize(sc
);
5037 * bridge_rtnode_destroy:
5039 * Destroy a bridge rtnode.
5042 bridge_rtnode_destroy(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
5044 BRIDGE_LOCK_ASSERT_HELD(sc
);
5046 LIST_REMOVE(brt
, brt_hash
);
5048 LIST_REMOVE(brt
, brt_list
);
5050 brt
->brt_dst
->bif_addrcnt
--;
5051 zfree(bridge_rtnode_pool
, brt
);
5056 * bridge_rtable_expire:
5058 * Set the expiry time for all routes on an interface.
5061 bridge_rtable_expire(struct ifnet
*ifp
, int age
)
5063 struct bridge_softc
*sc
= ifp
->if_bridge
;
5064 struct bridge_rtnode
*brt
;
5069 * If the age is zero then flush, otherwise set all the expiry times to
5070 * age for the interface
5073 bridge_rtdelete(sc
, ifp
, IFBF_FLUSHDYN
);
5077 now
= (unsigned long) net_uptime();
5079 LIST_FOREACH(brt
, &sc
->sc_rtlist
, brt_list
) {
5080 /* Cap the expiry time to 'age' */
5081 if (brt
->brt_ifp
== ifp
&&
5082 brt
->brt_expire
> now
+ age
&&
5083 (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
5084 brt
->brt_expire
= now
+ age
;
5091 * bridge_state_change:
5093 * Callback from the bridgestp code when a port changes states.
5096 bridge_state_change(struct ifnet
*ifp
, int state
)
5098 struct bridge_softc
*sc
= ifp
->if_bridge
;
5099 static const char *stpstates
[] = {
5109 log(LOG_NOTICE
, "%s: state changed to %s on %s\n",
5110 sc
->sc_ifp
->if_xname
,
5111 stpstates
[state
], ifp
->if_xname
);
5113 #endif /* BRIDGESTP */
5117 * Send bridge packets through pfil if they are one of the types pfil can deal
5118 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
5119 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
5123 bridge_pfil(struct mbuf
**mp
, struct ifnet
*bifp
, struct ifnet
*ifp
, int dir
)
5125 int snap
, error
, i
, hlen
;
5126 struct ether_header
*eh1
, eh2
;
5127 struct ip_fw_args args
;
5130 u_int16_t ether_type
;
5133 error
= -1; /* Default error if not error == 0 */
5136 /* we may return with the IP fields swapped, ensure its not shared */
5137 KASSERT(M_WRITABLE(*mp
), ("%s: modifying a shared mbuf", __func__
));
5140 if (pfil_bridge
== 0 && pfil_member
== 0 && pfil_ipfw
== 0)
5141 return (0); /* filtering is disabled */
5143 i
= min((*mp
)->m_pkthdr
.len
, max_protohdr
);
5144 if ((*mp
)->m_len
< i
) {
5145 *mp
= m_pullup(*mp
, i
);
5147 printf("%s: m_pullup failed\n", __func__
);
5152 eh1
= mtod(*mp
, struct ether_header
*);
5153 ether_type
= ntohs(eh1
->ether_type
);
5156 * Check for SNAP/LLC.
5158 if (ether_type
< ETHERMTU
) {
5159 struct llc
*llc2
= (struct llc
*)(eh1
+ 1);
5161 if ((*mp
)->m_len
>= ETHER_HDR_LEN
+ 8 &&
5162 llc2
->llc_dsap
== LLC_SNAP_LSAP
&&
5163 llc2
->llc_ssap
== LLC_SNAP_LSAP
&&
5164 llc2
->llc_control
== LLC_UI
) {
5165 ether_type
= htons(llc2
->llc_un
.type_snap
.ether_type
);
5171 * If we're trying to filter bridge traffic, don't look at anything
5172 * other than IP and ARP traffic. If the filter doesn't understand
5173 * IPv6, don't allow IPv6 through the bridge either. This is lame
5174 * since if we really wanted, say, an AppleTalk filter, we are hosed,
5175 * but of course we don't have an AppleTalk filter to begin with.
5176 * (Note that since pfil doesn't understand ARP it will pass *ALL*
5179 switch (ether_type
) {
5181 case ETHERTYPE_REVARP
:
5182 if (pfil_ipfw_arp
== 0)
5183 return (0); /* Automatically pass */
5188 case ETHERTYPE_IPV6
:
5193 * Check to see if the user wants to pass non-ip
5194 * packets, these will not be checked by pfil(9) and
5195 * passed unconditionally so the default is to drop.
5201 /* Strip off the Ethernet header and keep a copy. */
5202 m_copydata(*mp
, 0, ETHER_HDR_LEN
, (caddr_t
)&eh2
);
5203 m_adj(*mp
, ETHER_HDR_LEN
);
5205 /* Strip off snap header, if present */
5207 m_copydata(*mp
, 0, sizeof (struct llc
), (caddr_t
)&llc1
);
5208 m_adj(*mp
, sizeof (struct llc
));
5212 * Check the IP header for alignment and errors
5214 if (dir
== PFIL_IN
) {
5215 switch (ether_type
) {
5217 error
= bridge_ip_checkbasic(mp
);
5220 case ETHERTYPE_IPV6
:
5221 error
= bridge_ip6_checkbasic(mp
);
5231 if (IPFW_LOADED
&& pfil_ipfw
!= 0 && dir
== PFIL_OUT
&& ifp
!= NULL
) {
5233 args
.rule
= ip_dn_claim_rule(*mp
);
5234 if (args
.rule
!= NULL
&& fw_one_pass
)
5235 goto ipfwpass
; /* packet already partially processed */
5239 args
.next_hop
= NULL
;
5241 args
.inp
= NULL
; /* used by ipfw uid/gid/jail rules */
5242 i
= ip_fw_chk_ptr(&args
);
5248 if (DUMMYNET_LOADED
&& (i
== IP_FW_DUMMYNET
)) {
5250 /* put the Ethernet header back on */
5251 M_PREPEND(*mp
, ETHER_HDR_LEN
, M_DONTWAIT
);
5254 bcopy(&eh2
, mtod(*mp
, caddr_t
), ETHER_HDR_LEN
);
5257 * Pass the pkt to dummynet, which consumes it. The
5258 * packet will return to us via bridge_dummynet().
5261 ip_dn_io_ptr(mp
, DN_TO_IFB_FWD
, &args
, DN_CLIENT_IPFW
);
5265 if (i
!= IP_FW_PASS
) /* drop */
5273 * Run the packet through pfil
5275 switch (ether_type
) {
5278 * before calling the firewall, swap fields the same as
5279 * IP does. here we assume the header is contiguous
5281 ip
= mtod(*mp
, struct ip
*);
5283 ip
->ip_len
= ntohs(ip
->ip_len
);
5284 ip
->ip_off
= ntohs(ip
->ip_off
);
5287 * Run pfil on the member interface and the bridge, both can
5288 * be skipped by clearing pfil_member or pfil_bridge.
5291 * in_if -> bridge_if -> out_if
5293 if (pfil_bridge
&& dir
== PFIL_OUT
&& bifp
!= NULL
)
5294 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, bifp
,
5297 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
5300 if (pfil_member
&& ifp
!= NULL
)
5301 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, ifp
,
5304 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
5307 if (pfil_bridge
&& dir
== PFIL_IN
&& bifp
!= NULL
)
5308 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, bifp
,
5311 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
5314 /* check if we need to fragment the packet */
5315 if (pfil_member
&& ifp
!= NULL
&& dir
== PFIL_OUT
) {
5316 i
= (*mp
)->m_pkthdr
.len
;
5317 if (i
> ifp
->if_mtu
) {
5318 error
= bridge_fragment(ifp
, *mp
, &eh2
, snap
,
5324 /* Recalculate the ip checksum and restore byte ordering */
5325 ip
= mtod(*mp
, struct ip
*);
5326 hlen
= ip
->ip_hl
<< 2;
5327 if (hlen
< sizeof (struct ip
))
5329 if (hlen
> (*mp
)->m_len
) {
5330 if ((*mp
= m_pullup(*mp
, hlen
)) == 0)
5332 ip
= mtod(*mp
, struct ip
*);
5336 ip
->ip_len
= htons(ip
->ip_len
);
5337 ip
->ip_off
= htons(ip
->ip_off
);
5339 if (hlen
== sizeof (struct ip
))
5340 ip
->ip_sum
= in_cksum_hdr(ip
);
5342 ip
->ip_sum
= in_cksum(*mp
, hlen
);
5346 case ETHERTYPE_IPV6
:
5347 if (pfil_bridge
&& dir
== PFIL_OUT
&& bifp
!= NULL
)
5348 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, bifp
,
5351 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
5354 if (pfil_member
&& ifp
!= NULL
)
5355 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, ifp
,
5358 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
5361 if (pfil_bridge
&& dir
== PFIL_IN
&& bifp
!= NULL
)
5362 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, bifp
,
5379 * Finally, put everything back the way it was and return
5382 M_PREPEND(*mp
, sizeof (struct llc
), M_DONTWAIT
);
5385 bcopy(&llc1
, mtod(*mp
, caddr_t
), sizeof (struct llc
));
5388 M_PREPEND(*mp
, ETHER_HDR_LEN
, M_DONTWAIT
);
5391 bcopy(&eh2
, mtod(*mp
, caddr_t
), ETHER_HDR_LEN
);
5402 * Perform basic checks on header size since
5403 * pfil assumes ip_input has already processed
5404 * it for it. Cut-and-pasted from ip_input.c.
5405 * Given how simple the IPv6 version is,
5406 * does the IPv4 version really need to be
5409 * XXX Should we update ipstat here, or not?
5410 * XXX Right now we update ipstat but not
5414 bridge_ip_checkbasic(struct mbuf
**mp
)
5416 struct mbuf
*m
= *mp
;
5424 if (IP_HDR_ALIGNED_P(mtod(m
, caddr_t
)) == 0) {
5425 /* max_linkhdr is already rounded up to nearest 4-byte */
5426 if ((m
= m_copyup(m
, sizeof (struct ip
),
5427 max_linkhdr
)) == NULL
) {
5428 /* XXXJRT new stat, please */
5429 ipstat
.ips_toosmall
++;
5432 } else if (__predict_false(m
->m_len
< sizeof (struct ip
))) {
5433 if ((m
= m_pullup(m
, sizeof (struct ip
))) == NULL
) {
5434 ipstat
.ips_toosmall
++;
5438 ip
= mtod(m
, struct ip
*);
5439 if (ip
== NULL
) goto bad
;
5441 if (ip
->ip_v
!= IPVERSION
) {
5442 ipstat
.ips_badvers
++;
5445 hlen
= ip
->ip_hl
<< 2;
5446 if (hlen
< sizeof (struct ip
)) { /* minimum header length */
5447 ipstat
.ips_badhlen
++;
5450 if (hlen
> m
->m_len
) {
5451 if ((m
= m_pullup(m
, hlen
)) == 0) {
5452 ipstat
.ips_badhlen
++;
5455 ip
= mtod(m
, struct ip
*);
5456 if (ip
== NULL
) goto bad
;
5459 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
5460 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
5462 if (hlen
== sizeof (struct ip
)) {
5463 sum
= in_cksum_hdr(ip
);
5465 sum
= in_cksum(m
, hlen
);
5469 ipstat
.ips_badsum
++;
5473 /* Retrieve the packet length. */
5474 len
= ntohs(ip
->ip_len
);
5477 * Check for additional length bogosity
5480 ipstat
.ips_badlen
++;
5485 * Check that the amount of data in the buffers
5486 * is as at least much as the IP header would have us expect.
5487 * Drop packet if shorter than we expect.
5489 if (m
->m_pkthdr
.len
< len
) {
5490 ipstat
.ips_tooshort
++;
5494 /* Checks out, proceed */
5505 * Same as above, but for IPv6.
5506 * Cut-and-pasted from ip6_input.c.
5507 * XXX Should we update ip6stat, or not?
5510 bridge_ip6_checkbasic(struct mbuf
**mp
)
5512 struct mbuf
*m
= *mp
;
5513 struct ip6_hdr
*ip6
;
5516 * If the IPv6 header is not aligned, slurp it up into a new
5517 * mbuf with space for link headers, in the event we forward
5518 * it. Otherwise, if it is aligned, make sure the entire base
5519 * IPv6 header is in the first mbuf of the chain.
5521 if (IP6_HDR_ALIGNED_P(mtod(m
, caddr_t
)) == 0) {
5522 struct ifnet
*inifp
= m
->m_pkthdr
.rcvif
;
5523 /* max_linkhdr is already rounded up to nearest 4-byte */
5524 if ((m
= m_copyup(m
, sizeof (struct ip6_hdr
),
5525 max_linkhdr
)) == NULL
) {
5526 /* XXXJRT new stat, please */
5527 ip6stat
.ip6s_toosmall
++;
5528 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
5531 } else if (__predict_false(m
->m_len
< sizeof (struct ip6_hdr
))) {
5532 struct ifnet
*inifp
= m
->m_pkthdr
.rcvif
;
5533 if ((m
= m_pullup(m
, sizeof (struct ip6_hdr
))) == NULL
) {
5534 ip6stat
.ip6s_toosmall
++;
5535 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
5540 ip6
= mtod(m
, struct ip6_hdr
*);
5542 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
5543 ip6stat
.ip6s_badvers
++;
5544 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_hdrerr
);
5548 /* Checks out, proceed */
5561 * Return a fragmented mbuf chain.
5564 bridge_fragment(struct ifnet
*ifp
, struct mbuf
*m
, struct ether_header
*eh
,
5565 int snap
, struct llc
*llc
)
5571 if (m
->m_len
< sizeof (struct ip
) &&
5572 (m
= m_pullup(m
, sizeof (struct ip
))) == NULL
)
5574 ip
= mtod(m
, struct ip
*);
5576 error
= ip_fragment(ip
, &m
, ifp
->if_mtu
, ifp
->if_hwassist
,
5581 /* walk the chain and re-add the Ethernet header */
5582 for (m0
= m
; m0
; m0
= m0
->m_nextpkt
) {
5585 M_PREPEND(m0
, sizeof (struct llc
), M_DONTWAIT
);
5590 bcopy(llc
, mtod(m0
, caddr_t
),
5591 sizeof (struct llc
));
5593 M_PREPEND(m0
, ETHER_HDR_LEN
, M_DONTWAIT
);
5598 bcopy(eh
, mtod(m0
, caddr_t
), ETHER_HDR_LEN
);
5605 ipstat
.ips_fragmented
++;
5614 #endif /* PFIL_HOOKS */
5617 * bridge_set_bpf_tap:
5619 * Sets ups the BPF callbacks.
5622 bridge_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
, bpf_packet_func bpf_callback
)
5624 struct bridge_softc
*sc
= (struct bridge_softc
*)ifnet_softc(ifp
);
5627 if (sc
== NULL
|| (sc
->sc_flags
& SCF_DETACHING
)) {
5632 case BPF_TAP_DISABLE
:
5633 sc
->sc_bpf_input
= sc
->sc_bpf_output
= NULL
;
5637 sc
->sc_bpf_input
= bpf_callback
;
5640 case BPF_TAP_OUTPUT
:
5641 sc
->sc_bpf_output
= bpf_callback
;
5644 case BPF_TAP_INPUT_OUTPUT
:
5645 sc
->sc_bpf_input
= sc
->sc_bpf_output
= bpf_callback
;
5658 * Callback when interface has been detached.
5661 bridge_detach(ifnet_t ifp
)
5663 struct bridge_softc
*sc
= (struct bridge_softc
*)ifnet_softc(ifp
);
5666 bstp_detach(&sc
->sc_stp
);
5667 #endif /* BRIDGESTP */
5669 /* Tear down the routing table. */
5670 bridge_rtable_fini(sc
);
5672 lck_mtx_lock(&bridge_list_mtx
);
5673 LIST_REMOVE(sc
, sc_list
);
5674 lck_mtx_unlock(&bridge_list_mtx
);
5678 lck_mtx_destroy(&sc
->sc_mtx
, bridge_lock_grp
);
5680 _FREE(sc
, M_DEVBUF
);
5686 * Invoke the input BPF callback if enabled
5688 __private_extern__ errno_t
5689 bridge_bpf_input(ifnet_t ifp
, struct mbuf
*m
)
5691 struct bridge_softc
*sc
= (struct bridge_softc
*)ifnet_softc(ifp
);
5693 if (sc
->sc_bpf_input
) {
5694 if (mbuf_pkthdr_rcvif(m
) != ifp
) {
5695 printf("%s: rcvif: 0x%llx != ifp 0x%llx\n", __func__
,
5696 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_pkthdr_rcvif(m
)),
5697 (uint64_t)VM_KERNEL_ADDRPERM(ifp
));
5699 (*sc
->sc_bpf_input
)(ifp
, m
);
5705 * bridge_bpf_output:
5707 * Invoke the output BPF callback if enabled
5709 __private_extern__ errno_t
5710 bridge_bpf_output(ifnet_t ifp
, struct mbuf
*m
)
5712 struct bridge_softc
*sc
= (struct bridge_softc
*)ifnet_softc(ifp
);
5714 if (sc
->sc_bpf_output
) {
5715 (*sc
->sc_bpf_output
)(ifp
, m
);
5721 * bridge_link_event:
5723 * Report a data link event on an interface
5726 bridge_link_event(struct ifnet
*ifp
, u_int32_t event_code
)
5729 struct kern_event_msg header
;
5731 char if_name
[IFNAMSIZ
];
5735 if (if_bridge_debug
& BR_DBGF_LIFECYCLE
)
5736 printf("%s: %s event_code %u - %s\n", __func__
, ifp
->if_xname
,
5737 event_code
, dlil_kev_dl_code_str(event_code
));
5738 #endif /* BRIDGE_DEBUG */
5740 bzero(&event
, sizeof (event
));
5741 event
.header
.total_size
= sizeof (event
);
5742 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
5743 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
5744 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
5745 event
.header
.event_code
= event_code
;
5746 event
.header
.event_data
[0] = ifnet_family(ifp
);
5747 event
.unit
= (u_int32_t
)ifnet_unit(ifp
);
5748 strncpy(event
.if_name
, ifnet_name(ifp
), IFNAMSIZ
);
5749 ifnet_event(ifp
, &event
.header
);