2 * Copyright (c) 2004-2019 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>
142 #include <net/net_api_stats.h>
144 #include <netinet/in.h> /* for struct arpcom */
145 #include <netinet/in_systm.h>
146 #include <netinet/in_var.h>
148 #include <netinet/ip.h>
149 #include <netinet/ip_var.h>
151 #include <netinet/ip6.h>
152 #include <netinet6/ip6_var.h>
155 #include <netinet/ip_carp.h>
157 #include <netinet/if_ether.h> /* for struct arpcom */
158 #include <net/bridgestp.h>
159 #include <net/if_bridgevar.h>
160 #include <net/if_llc.h>
162 #include <net/if_vlan_var.h>
163 #endif /* NVLAN > 0 */
165 #include <net/if_ether.h>
166 #include <net/dlil.h>
167 #include <net/kpi_interfacefilter.h>
169 #include <net/route.h>
171 #include <netinet/ip_fw2.h>
172 #include <netinet/ip_dummynet.h>
173 #endif /* PFIL_HOOKS */
174 #include <dev/random/randomdev.h>
176 #include <netinet/bootp.h>
177 #include <netinet/dhcp.h>
181 #define BR_DBGF_LIFECYCLE 0x0001
182 #define BR_DBGF_INPUT 0x0002
183 #define BR_DBGF_OUTPUT 0x0004
184 #define BR_DBGF_RT_TABLE 0x0008
185 #define BR_DBGF_DELAYED_CALL 0x0010
186 #define BR_DBGF_IOCTL 0x0020
187 #define BR_DBGF_MBUF 0x0040
188 #define BR_DBGF_MCAST 0x0080
189 #define BR_DBGF_HOSTFILTER 0x0100
190 #define BR_DBGF_CHECKSUM 0x0200
191 #define BR_DBGF_MAC_NAT 0x0400
192 #endif /* BRIDGE_DEBUG */
194 #define _BRIDGE_LOCK(_sc) lck_mtx_lock(&(_sc)->sc_mtx)
195 #define _BRIDGE_UNLOCK(_sc) lck_mtx_unlock(&(_sc)->sc_mtx)
196 #define BRIDGE_LOCK_ASSERT_HELD(_sc) \
197 LCK_MTX_ASSERT(&(_sc)->sc_mtx, LCK_MTX_ASSERT_OWNED)
198 #define BRIDGE_LOCK_ASSERT_NOTHELD(_sc) \
199 LCK_MTX_ASSERT(&(_sc)->sc_mtx, LCK_MTX_ASSERT_NOTOWNED)
203 #define BR_LCKDBG_MAX 4
205 #define BRIDGE_LOCK(_sc) bridge_lock(_sc)
206 #define BRIDGE_UNLOCK(_sc) bridge_unlock(_sc)
207 #define BRIDGE_LOCK2REF(_sc, _err) _err = bridge_lock2ref(_sc)
208 #define BRIDGE_UNREF(_sc) bridge_unref(_sc)
209 #define BRIDGE_XLOCK(_sc) bridge_xlock(_sc)
210 #define BRIDGE_XDROP(_sc) bridge_xdrop(_sc)
211 #define IF_BRIDGE_DEBUG(f) bridge_debug_flag_is_set(f)
213 #else /* !BRIDGE_DEBUG */
215 #define BRIDGE_LOCK(_sc) _BRIDGE_LOCK(_sc)
216 #define BRIDGE_UNLOCK(_sc) _BRIDGE_UNLOCK(_sc)
217 #define BRIDGE_LOCK2REF(_sc, _err) do { \
218 BRIDGE_LOCK_ASSERT_HELD(_sc); \
219 if ((_sc)->sc_iflist_xcnt > 0) \
222 (_sc)->sc_iflist_ref++; \
223 _BRIDGE_UNLOCK(_sc); \
225 #define BRIDGE_UNREF(_sc) do { \
227 (_sc)->sc_iflist_ref--; \
228 if (((_sc)->sc_iflist_xcnt > 0) && ((_sc)->sc_iflist_ref == 0)) { \
229 _BRIDGE_UNLOCK(_sc); \
230 wakeup(&(_sc)->sc_cv); \
232 _BRIDGE_UNLOCK(_sc); \
234 #define BRIDGE_XLOCK(_sc) do { \
235 BRIDGE_LOCK_ASSERT_HELD(_sc); \
236 (_sc)->sc_iflist_xcnt++; \
237 while ((_sc)->sc_iflist_ref > 0) \
238 msleep(&(_sc)->sc_cv, &(_sc)->sc_mtx, PZERO, \
239 "BRIDGE_XLOCK", NULL); \
241 #define BRIDGE_XDROP(_sc) do { \
242 BRIDGE_LOCK_ASSERT_HELD(_sc); \
243 (_sc)->sc_iflist_xcnt--; \
246 #define IF_BRIDGE_DEBUG(f) FALSE
248 #endif /* BRIDGE_DEBUG */
251 #define BRIDGE_BPF_MTAP_INPUT(sc, m) \
252 if (sc->sc_bpf_input != NULL) \
253 bridge_bpf_input(sc->sc_ifp, m, __func__, __LINE__)
254 #else /* NBPFILTER */
255 #define BRIDGE_BPF_MTAP_INPUT(ifp, m)
256 #endif /* NBPFILTER */
259 * Initial size of the route hash table. Must be a power of two.
261 #ifndef BRIDGE_RTHASH_SIZE
262 #define BRIDGE_RTHASH_SIZE 16
266 * Maximum size of the routing hash table
268 #define BRIDGE_RTHASH_SIZE_MAX 2048
270 #define BRIDGE_RTHASH_MASK(sc) ((sc)->sc_rthash_size - 1)
273 * Maximum number of addresses to cache.
275 #ifndef BRIDGE_RTABLE_MAX
276 #define BRIDGE_RTABLE_MAX 100
281 * Timeout (in seconds) for entries learned dynamically.
283 #ifndef BRIDGE_RTABLE_TIMEOUT
284 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
288 * Number of seconds between walks of the route list.
290 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
291 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
295 * Number of MAC NAT entries
296 * - sized based on 16 clients (including MAC NAT interface)
297 * each with 4 addresses
299 #ifndef BRIDGE_MAC_NAT_ENTRY_MAX
300 #define BRIDGE_MAC_NAT_ENTRY_MAX 64
301 #endif /* BRIDGE_MAC_NAT_ENTRY_MAX */
304 * List of capabilities to possibly mask on the member interface.
306 #define BRIDGE_IFCAPS_MASK (IFCAP_TOE|IFCAP_TSO|IFCAP_TXCSUM)
308 * List of capabilities to disable on the member interface.
310 #define BRIDGE_IFCAPS_STRIP IFCAP_LRO
313 * Bridge interface list entry.
315 struct bridge_iflist
{
316 TAILQ_ENTRY(bridge_iflist
) bif_next
;
317 struct ifnet
*bif_ifp
; /* member if */
318 struct bstp_port bif_stp
; /* STP state */
319 uint32_t bif_ifflags
; /* member if flags */
320 int bif_savedcaps
; /* saved capabilities */
321 uint32_t bif_addrmax
; /* max # of addresses */
322 uint32_t bif_addrcnt
; /* cur. # of addresses */
323 uint32_t bif_addrexceeded
; /* # of address violations */
325 interface_filter_t bif_iff_ref
;
326 struct bridge_softc
*bif_sc
;
329 struct in_addr bif_hf_ipsrc
;
330 uint8_t bif_hf_hwsrc
[ETHER_ADDR_LEN
];
333 #define BIFF_PROMISC 0x01 /* promiscuous mode set */
334 #define BIFF_PROTO_ATTACHED 0x02 /* protocol attached */
335 #define BIFF_FILTER_ATTACHED 0x04 /* interface filter attached */
336 #define BIFF_MEDIA_ACTIVE 0x08 /* interface media active */
337 #define BIFF_HOST_FILTER 0x10 /* host filter enabled */
338 #define BIFF_HF_HWSRC 0x20 /* host filter source MAC is set */
339 #define BIFF_HF_IPSRC 0x40 /* host filter source IP is set */
340 #define BIFF_INPUT_BROADCAST 0x80 /* send broadcast packets in */
344 * - translates between an IP address and MAC address on a specific
345 * bridge interface member
347 struct mac_nat_entry
{
348 LIST_ENTRY(mac_nat_entry
) mne_list
; /* list linkage */
349 struct bridge_iflist
*mne_bif
; /* originating interface */
350 unsigned long mne_expire
; /* expiration time */
352 struct in_addr mneu_ip
; /* originating IPv4 address */
353 struct in6_addr mneu_ip6
; /* originating IPv6 address */
355 uint8_t mne_mac
[ETHER_ADDR_LEN
];
357 uint8_t mne_reserved
;
359 #define mne_ip mne_u.mneu_ip
360 #define mne_ip6 mne_u.mneu_ip6
362 #define MNE_FLAGS_IPV6 0x01 /* IPv6 address */
364 LIST_HEAD(mac_nat_entry_list
, mac_nat_entry
);
368 * - used by bridge_mac_nat_output() to convey the translation that needs
369 * to take place in bridge_mac_nat_translate
370 * - holds enough information so that the translation can be done later without
371 * holding the bridge lock
373 struct mac_nat_record
{
374 uint16_t mnr_ether_type
;
376 uint16_t mnru_arp_offset
;
378 uint16_t mnruip_dhcp_flags
;
379 uint16_t mnruip_udp_csum
;
380 uint8_t mnruip_header_len
;
383 uint16_t mnruip6_icmp6_len
;
384 uint16_t mnruip6_lladdr_offset
;
385 uint8_t mnruip6_icmp6_type
;
386 uint8_t mnruip6_header_len
;
391 #define mnr_arp_offset mnr_u.mnru_arp_offset
393 #define mnr_ip_header_len mnr_u.mnru_ip.mnruip_header_len
394 #define mnr_ip_dhcp_flags mnr_u.mnru_ip.mnruip_dhcp_flags
395 #define mnr_ip_udp_csum mnr_u.mnru_ip.mnruip_udp_csum
397 #define mnr_ip6_icmp6_len mnr_u.mnru_ip6.mnruip6_icmp6_len
398 #define mnr_ip6_icmp6_type mnr_u.mnru_ip6.mnruip6_icmp6_type
399 #define mnr_ip6_header_len mnr_u.mnru_ip6.mnruip6_header_len
400 #define mnr_ip6_lladdr_offset mnr_u.mnru_ip6.mnruip6_lladdr_offset
405 struct bridge_rtnode
{
406 LIST_ENTRY(bridge_rtnode
) brt_hash
; /* hash table linkage */
407 LIST_ENTRY(bridge_rtnode
) brt_list
; /* list linkage */
408 struct bridge_iflist
*brt_dst
; /* destination if */
409 unsigned long brt_expire
; /* expiration time */
410 uint8_t brt_flags
; /* address flags */
411 uint8_t brt_addr
[ETHER_ADDR_LEN
];
412 uint16_t brt_vlan
; /* vlan id */
415 #define brt_ifp brt_dst->bif_ifp
418 * Bridge delayed function call context
420 typedef void (*bridge_delayed_func_t
)(struct bridge_softc
*);
422 struct bridge_delayed_call
{
423 struct bridge_softc
*bdc_sc
;
424 bridge_delayed_func_t bdc_func
; /* Function to call */
425 struct timespec bdc_ts
; /* Time to call */
427 thread_call_t bdc_thread_call
;
430 #define BDCF_OUTSTANDING 0x01 /* Delayed call has been scheduled */
431 #define BDCF_CANCELLING 0x02 /* May be waiting for call completion */
434 * Software state for each bridge.
436 LIST_HEAD(_bridge_rtnode_list
, bridge_rtnode
);
438 struct bridge_softc
{
439 struct ifnet
*sc_ifp
; /* make this an interface */
441 LIST_ENTRY(bridge_softc
) sc_list
;
442 decl_lck_mtx_data(, sc_mtx
);
443 struct _bridge_rtnode_list
*sc_rthash
; /* our forwarding table */
444 struct _bridge_rtnode_list sc_rtlist
; /* list version of above */
445 uint32_t sc_rthash_key
; /* key for hash */
446 uint32_t sc_rthash_size
; /* size of the hash table */
447 struct bridge_delayed_call sc_aging_timer
;
448 struct bridge_delayed_call sc_resize_call
;
449 TAILQ_HEAD(, bridge_iflist
) sc_spanlist
; /* span ports list */
450 struct bstp_state sc_stp
; /* STP state */
451 bpf_packet_func sc_bpf_input
;
452 bpf_packet_func sc_bpf_output
;
454 uint32_t sc_brtmax
; /* max # of addresses */
455 uint32_t sc_brtcnt
; /* cur. # of addresses */
456 uint32_t sc_brttimeout
; /* rt timeout in seconds */
457 uint32_t sc_iflist_ref
; /* refcount for sc_iflist */
458 uint32_t sc_iflist_xcnt
; /* refcount for sc_iflist */
459 TAILQ_HEAD(, bridge_iflist
) sc_iflist
; /* member interface list */
460 uint32_t sc_brtexceeded
; /* # of cache drops */
461 uint32_t sc_filter_flags
; /* ipf and flags */
462 struct ifnet
*sc_ifaddr
; /* member mac copied from */
463 u_char sc_defaddr
[6]; /* Default MAC address */
464 char sc_if_xname
[IFNAMSIZ
];
466 struct bridge_iflist
*sc_mac_nat_bif
; /* single MAC NAT interface */
467 struct mac_nat_entry_list sc_mne_list
; /* MAC NAT IPv4 */
468 struct mac_nat_entry_list sc_mne_list_v6
;/* MAC NAT IPv6 */
469 uint32_t sc_mne_max
; /* max # of entries */
470 uint32_t sc_mne_count
; /* cur. # of entries */
471 uint32_t sc_mne_allocation_failures
;
474 * Locking and unlocking calling history
476 void *lock_lr
[BR_LCKDBG_MAX
];
478 void *unlock_lr
[BR_LCKDBG_MAX
];
480 #endif /* BRIDGE_DEBUG */
483 #define SCF_DETACHING 0x01
484 #define SCF_RESIZING 0x02
485 #define SCF_MEDIA_ACTIVE 0x04
488 kChecksumOperationNone
= 0,
489 kChecksumOperationClear
= 1,
490 kChecksumOperationFinalize
= 2,
491 kChecksumOperationCompute
= 3,
494 struct bridge_hostfilter_stats bridge_hostfilter_stats
;
496 decl_lck_mtx_data(static, bridge_list_mtx
);
498 static int bridge_rtable_prune_period
= BRIDGE_RTABLE_PRUNE_PERIOD
;
500 static zone_t bridge_rtnode_pool
= NULL
;
501 static zone_t bridge_mne_pool
= NULL
;
503 static int bridge_clone_create(struct if_clone
*, uint32_t, void *);
504 static int bridge_clone_destroy(struct ifnet
*);
506 static errno_t
bridge_ioctl(struct ifnet
*, u_long
, void *);
508 static void bridge_mutecaps(struct bridge_softc
*);
509 static void bridge_set_ifcap(struct bridge_softc
*, struct bridge_iflist
*,
512 static errno_t
bridge_set_tso(struct bridge_softc
*);
513 static void bridge_ifdetach(struct ifnet
*);
514 static void bridge_proto_attach_changed(struct ifnet
*);
515 static int bridge_init(struct ifnet
*);
516 #if HAS_BRIDGE_DUMMYNET
517 static void bridge_dummynet(struct mbuf
*, struct ifnet
*);
519 static void bridge_ifstop(struct ifnet
*, int);
520 static int bridge_output(struct ifnet
*, struct mbuf
*);
521 static void bridge_finalize_cksum(struct ifnet
*, struct mbuf
*);
522 static void bridge_start(struct ifnet
*);
523 static errno_t
bridge_input(struct ifnet
*, mbuf_t
*);
524 static errno_t
bridge_iff_input(void *, ifnet_t
, protocol_family_t
,
526 static errno_t
bridge_iff_output(void *, ifnet_t
, protocol_family_t
,
528 static errno_t
bridge_member_output(struct bridge_softc
*sc
, ifnet_t ifp
,
531 static int bridge_enqueue(ifnet_t
, struct ifnet
*,
532 struct ifnet
*, struct mbuf
*, ChecksumOperation
);
533 static void bridge_rtdelete(struct bridge_softc
*, struct ifnet
*ifp
, int);
535 static void bridge_forward(struct bridge_softc
*, struct bridge_iflist
*,
538 static void bridge_aging_timer(struct bridge_softc
*sc
);
540 static void bridge_broadcast(struct bridge_softc
*, struct ifnet
*,
542 static void bridge_span(struct bridge_softc
*, struct mbuf
*);
544 static int bridge_rtupdate(struct bridge_softc
*, const uint8_t *,
545 uint16_t, struct bridge_iflist
*, int, uint8_t);
546 static struct ifnet
*bridge_rtlookup(struct bridge_softc
*, const uint8_t *,
548 static void bridge_rttrim(struct bridge_softc
*);
549 static void bridge_rtage(struct bridge_softc
*);
550 static void bridge_rtflush(struct bridge_softc
*, int);
551 static int bridge_rtdaddr(struct bridge_softc
*, const uint8_t *,
554 static int bridge_rtable_init(struct bridge_softc
*);
555 static void bridge_rtable_fini(struct bridge_softc
*);
557 static void bridge_rthash_resize(struct bridge_softc
*);
559 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
560 static struct bridge_rtnode
*bridge_rtnode_lookup(struct bridge_softc
*,
561 const uint8_t *, uint16_t);
562 static int bridge_rtnode_hash(struct bridge_softc
*,
563 struct bridge_rtnode
*);
564 static int bridge_rtnode_insert(struct bridge_softc
*,
565 struct bridge_rtnode
*);
566 static void bridge_rtnode_destroy(struct bridge_softc
*,
567 struct bridge_rtnode
*);
569 static void bridge_rtable_expire(struct ifnet
*, int);
570 static void bridge_state_change(struct ifnet
*, int);
571 #endif /* BRIDGESTP */
573 static struct bridge_iflist
*bridge_lookup_member(struct bridge_softc
*,
575 static struct bridge_iflist
*bridge_lookup_member_if(struct bridge_softc
*,
577 static void bridge_delete_member(struct bridge_softc
*,
578 struct bridge_iflist
*, int);
579 static void bridge_delete_span(struct bridge_softc
*,
580 struct bridge_iflist
*);
582 static int bridge_ioctl_add(struct bridge_softc
*, void *);
583 static int bridge_ioctl_del(struct bridge_softc
*, void *);
584 static int bridge_ioctl_gifflags(struct bridge_softc
*, void *);
585 static int bridge_ioctl_sifflags(struct bridge_softc
*, void *);
586 static int bridge_ioctl_scache(struct bridge_softc
*, void *);
587 static int bridge_ioctl_gcache(struct bridge_softc
*, void *);
588 static int bridge_ioctl_gifs32(struct bridge_softc
*, void *);
589 static int bridge_ioctl_gifs64(struct bridge_softc
*, void *);
590 static int bridge_ioctl_rts32(struct bridge_softc
*, void *);
591 static int bridge_ioctl_rts64(struct bridge_softc
*, void *);
592 static int bridge_ioctl_saddr32(struct bridge_softc
*, void *);
593 static int bridge_ioctl_saddr64(struct bridge_softc
*, void *);
594 static int bridge_ioctl_sto(struct bridge_softc
*, void *);
595 static int bridge_ioctl_gto(struct bridge_softc
*, void *);
596 static int bridge_ioctl_daddr32(struct bridge_softc
*, void *);
597 static int bridge_ioctl_daddr64(struct bridge_softc
*, void *);
598 static int bridge_ioctl_flush(struct bridge_softc
*, void *);
599 static int bridge_ioctl_gpri(struct bridge_softc
*, void *);
600 static int bridge_ioctl_spri(struct bridge_softc
*, void *);
601 static int bridge_ioctl_ght(struct bridge_softc
*, void *);
602 static int bridge_ioctl_sht(struct bridge_softc
*, void *);
603 static int bridge_ioctl_gfd(struct bridge_softc
*, void *);
604 static int bridge_ioctl_sfd(struct bridge_softc
*, void *);
605 static int bridge_ioctl_gma(struct bridge_softc
*, void *);
606 static int bridge_ioctl_sma(struct bridge_softc
*, void *);
607 static int bridge_ioctl_sifprio(struct bridge_softc
*, void *);
608 static int bridge_ioctl_sifcost(struct bridge_softc
*, void *);
609 static int bridge_ioctl_sifmaxaddr(struct bridge_softc
*, void *);
610 static int bridge_ioctl_addspan(struct bridge_softc
*, void *);
611 static int bridge_ioctl_delspan(struct bridge_softc
*, void *);
612 static int bridge_ioctl_gbparam32(struct bridge_softc
*, void *);
613 static int bridge_ioctl_gbparam64(struct bridge_softc
*, void *);
614 static int bridge_ioctl_grte(struct bridge_softc
*, void *);
615 static int bridge_ioctl_gifsstp32(struct bridge_softc
*, void *);
616 static int bridge_ioctl_gifsstp64(struct bridge_softc
*, void *);
617 static int bridge_ioctl_sproto(struct bridge_softc
*, void *);
618 static int bridge_ioctl_stxhc(struct bridge_softc
*, void *);
619 static int bridge_ioctl_purge(struct bridge_softc
*sc
, void *);
620 static int bridge_ioctl_gfilt(struct bridge_softc
*, void *);
621 static int bridge_ioctl_sfilt(struct bridge_softc
*, void *);
622 static int bridge_ioctl_ghostfilter(struct bridge_softc
*, void *);
623 static int bridge_ioctl_shostfilter(struct bridge_softc
*, void *);
624 static int bridge_ioctl_gmnelist32(struct bridge_softc
*, void *);
625 static int bridge_ioctl_gmnelist64(struct bridge_softc
*, void *);
627 static int bridge_pfil(struct mbuf
**, struct ifnet
*, struct ifnet
*,
629 static int bridge_fragment(struct ifnet
*, struct mbuf
*,
630 struct ether_header
*, int, struct llc
*);
631 #endif /* PFIL_HOOKS */
632 static int bridge_ip_checkbasic(struct mbuf
**);
634 static int bridge_ip6_checkbasic(struct mbuf
**);
637 static int bridge_pf(struct mbuf
**, struct ifnet
*, uint32_t sc_filter_flags
, int input
);
639 static errno_t
bridge_set_bpf_tap(ifnet_t
, bpf_tap_mode
, bpf_packet_func
);
640 static errno_t
bridge_bpf_input(ifnet_t
, struct mbuf
*, const char *, int);
641 static errno_t
bridge_bpf_output(ifnet_t
, struct mbuf
*);
643 static void bridge_detach(ifnet_t
);
644 static void bridge_link_event(struct ifnet
*, u_int32_t
);
645 static void bridge_iflinkevent(struct ifnet
*);
646 static u_int32_t
bridge_updatelinkstatus(struct bridge_softc
*);
647 static int interface_media_active(struct ifnet
*);
648 static void bridge_schedule_delayed_call(struct bridge_delayed_call
*);
649 static void bridge_cancel_delayed_call(struct bridge_delayed_call
*);
650 static void bridge_cleanup_delayed_call(struct bridge_delayed_call
*);
651 static int bridge_host_filter(struct bridge_iflist
*, mbuf_t
*);
653 static errno_t
bridge_mac_nat_enable(struct bridge_softc
*,
654 struct bridge_iflist
*);
655 static void bridge_mac_nat_disable(struct bridge_softc
*sc
);
656 static void bridge_mac_nat_age_entries(struct bridge_softc
*sc
, unsigned long);
657 static void bridge_mac_nat_populate_entries(struct bridge_softc
*sc
);
658 static void bridge_mac_nat_flush_entries(struct bridge_softc
*sc
,
659 struct bridge_iflist
*);
660 static ifnet_t
bridge_mac_nat_input(struct bridge_softc
*, mbuf_t
*,
662 static boolean_t
bridge_mac_nat_output(struct bridge_softc
*,
663 struct bridge_iflist
*, mbuf_t
*, struct mac_nat_record
*);
664 static void bridge_mac_nat_translate(mbuf_t
*, struct mac_nat_record
*,
667 #define m_copypacket(m, how) m_copym(m, 0, M_COPYALL, how)
669 /* The default bridge vlan is 1 (IEEE 802.1Q-2003 Table 9-2) */
670 #define VLANTAGOF(_m) 0
672 u_int8_t bstp_etheraddr
[ETHER_ADDR_LEN
] =
673 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
675 static u_int8_t ethernulladdr
[ETHER_ADDR_LEN
] =
676 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
679 static struct bstp_cb_ops bridge_ops
= {
680 .bcb_state
= bridge_state_change
,
681 .bcb_rtage
= bridge_rtable_expire
683 #endif /* BRIDGESTP */
685 SYSCTL_DECL(_net_link
);
686 SYSCTL_NODE(_net_link
, IFT_BRIDGE
, bridge
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0,
689 static int bridge_inherit_mac
= 0; /* share MAC with first bridge member */
690 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, inherit_mac
,
691 CTLFLAG_RW
| CTLFLAG_LOCKED
,
692 &bridge_inherit_mac
, 0,
693 "Inherit MAC address from the first bridge member");
695 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, rtable_prune_period
,
696 CTLFLAG_RW
| CTLFLAG_LOCKED
,
697 &bridge_rtable_prune_period
, 0,
698 "Interval between pruning of routing table");
700 static unsigned int bridge_rtable_hash_size_max
= BRIDGE_RTHASH_SIZE_MAX
;
701 SYSCTL_UINT(_net_link_bridge
, OID_AUTO
, rtable_hash_size_max
,
702 CTLFLAG_RW
| CTLFLAG_LOCKED
,
703 &bridge_rtable_hash_size_max
, 0,
704 "Maximum size of the routing hash table");
706 #if BRIDGE_DEBUG_DELAYED_CALLBACK
707 static int bridge_delayed_callback_delay
= 0;
708 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, delayed_callback_delay
,
709 CTLFLAG_RW
| CTLFLAG_LOCKED
,
710 &bridge_delayed_callback_delay
, 0,
711 "Delay before calling delayed function");
714 SYSCTL_STRUCT(_net_link_bridge
, OID_AUTO
,
715 hostfilterstats
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
716 &bridge_hostfilter_stats
, bridge_hostfilter_stats
, "");
718 #if defined(PFIL_HOOKS)
719 static int pfil_onlyip
= 1; /* only pass IP[46] packets when pfil is enabled */
720 static int pfil_bridge
= 1; /* run pfil hooks on the bridge interface */
721 static int pfil_member
= 1; /* run pfil hooks on the member interface */
722 static int pfil_ipfw
= 0; /* layer2 filter with ipfw */
723 static int pfil_ipfw_arp
= 0; /* layer2 filter with ipfw */
724 static int pfil_local_phys
= 0; /* run pfil hooks on the physical interface */
725 /* for locally destined packets */
726 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_onlyip
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
727 &pfil_onlyip
, 0, "Only pass IP packets when pfil is enabled");
728 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, ipfw_arp
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
729 &pfil_ipfw_arp
, 0, "Filter ARP packets through IPFW layer2");
730 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_bridge
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
731 &pfil_bridge
, 0, "Packet filter on the bridge interface");
732 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_member
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
733 &pfil_member
, 0, "Packet filter on the member interface");
734 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_local_phys
,
735 CTLFLAG_RW
| CTLFLAG_LOCKED
, &pfil_local_phys
, 0,
736 "Packet filter on the physical interface for locally destined packets");
737 #endif /* PFIL_HOOKS */
740 static int log_stp
= 0; /* log STP state changes */
741 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, log_stp
, CTLFLAG_RW
,
742 &log_stp
, 0, "Log STP state changes");
743 #endif /* BRIDGESTP */
745 struct bridge_control
{
746 int (*bc_func
)(struct bridge_softc
*, void *);
747 unsigned int bc_argsize
;
748 unsigned int bc_flags
;
751 #define BC_F_COPYIN 0x01 /* copy arguments in */
752 #define BC_F_COPYOUT 0x02 /* copy arguments out */
753 #define BC_F_SUSER 0x04 /* do super-user check */
755 static const struct bridge_control bridge_control_table32
[] = {
756 { .bc_func
= bridge_ioctl_add
, .bc_argsize
= sizeof(struct ifbreq
), /* 0 */
757 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
758 { .bc_func
= bridge_ioctl_del
, .bc_argsize
= sizeof(struct ifbreq
),
759 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
761 { .bc_func
= bridge_ioctl_gifflags
, .bc_argsize
= sizeof(struct ifbreq
),
762 .bc_flags
= BC_F_COPYIN
| BC_F_COPYOUT
},
763 { .bc_func
= bridge_ioctl_sifflags
, .bc_argsize
= sizeof(struct ifbreq
),
764 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
766 { .bc_func
= bridge_ioctl_scache
, .bc_argsize
= sizeof(struct ifbrparam
),
767 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
768 { .bc_func
= bridge_ioctl_gcache
, .bc_argsize
= sizeof(struct ifbrparam
),
769 .bc_flags
= BC_F_COPYOUT
},
771 { .bc_func
= bridge_ioctl_gifs32
, .bc_argsize
= sizeof(struct ifbifconf32
),
772 .bc_flags
= BC_F_COPYIN
| BC_F_COPYOUT
},
773 { .bc_func
= bridge_ioctl_rts32
, .bc_argsize
= sizeof(struct ifbaconf32
),
774 .bc_flags
= BC_F_COPYIN
| BC_F_COPYOUT
},
776 { .bc_func
= bridge_ioctl_saddr32
, .bc_argsize
= sizeof(struct ifbareq32
),
777 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
779 { .bc_func
= bridge_ioctl_sto
, .bc_argsize
= sizeof(struct ifbrparam
),
780 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
781 { .bc_func
= bridge_ioctl_gto
, .bc_argsize
= sizeof(struct ifbrparam
), /* 10 */
782 .bc_flags
= BC_F_COPYOUT
},
784 { .bc_func
= bridge_ioctl_daddr32
, .bc_argsize
= sizeof(struct ifbareq32
),
785 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
787 { .bc_func
= bridge_ioctl_flush
, .bc_argsize
= sizeof(struct ifbreq
),
788 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
790 { .bc_func
= bridge_ioctl_gpri
, .bc_argsize
= sizeof(struct ifbrparam
),
791 .bc_flags
= BC_F_COPYOUT
},
792 { .bc_func
= bridge_ioctl_spri
, .bc_argsize
= sizeof(struct ifbrparam
),
793 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
795 { .bc_func
= bridge_ioctl_ght
, .bc_argsize
= sizeof(struct ifbrparam
),
796 .bc_flags
= BC_F_COPYOUT
},
797 { .bc_func
= bridge_ioctl_sht
, .bc_argsize
= sizeof(struct ifbrparam
),
798 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
800 { .bc_func
= bridge_ioctl_gfd
, .bc_argsize
= sizeof(struct ifbrparam
),
801 .bc_flags
= BC_F_COPYOUT
},
802 { .bc_func
= bridge_ioctl_sfd
, .bc_argsize
= sizeof(struct ifbrparam
),
803 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
805 { .bc_func
= bridge_ioctl_gma
, .bc_argsize
= sizeof(struct ifbrparam
),
806 .bc_flags
= BC_F_COPYOUT
},
807 { .bc_func
= bridge_ioctl_sma
, .bc_argsize
= sizeof(struct ifbrparam
), /* 20 */
808 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
810 { .bc_func
= bridge_ioctl_sifprio
, .bc_argsize
= sizeof(struct ifbreq
),
811 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
813 { .bc_func
= bridge_ioctl_sifcost
, .bc_argsize
= sizeof(struct ifbreq
),
814 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
816 { .bc_func
= bridge_ioctl_gfilt
, .bc_argsize
= sizeof(struct ifbrparam
),
817 .bc_flags
= BC_F_COPYOUT
},
818 { .bc_func
= bridge_ioctl_sfilt
, .bc_argsize
= sizeof(struct ifbrparam
),
819 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
821 { .bc_func
= bridge_ioctl_purge
, .bc_argsize
= sizeof(struct ifbreq
),
822 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
824 { .bc_func
= bridge_ioctl_addspan
, .bc_argsize
= sizeof(struct ifbreq
),
825 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
826 { .bc_func
= bridge_ioctl_delspan
, .bc_argsize
= sizeof(struct ifbreq
),
827 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
829 { .bc_func
= bridge_ioctl_gbparam32
, .bc_argsize
= sizeof(struct ifbropreq32
),
830 .bc_flags
= BC_F_COPYOUT
},
832 { .bc_func
= bridge_ioctl_grte
, .bc_argsize
= sizeof(struct ifbrparam
),
833 .bc_flags
= BC_F_COPYOUT
},
835 { .bc_func
= bridge_ioctl_gifsstp32
, .bc_argsize
= sizeof(struct ifbpstpconf32
), /* 30 */
836 .bc_flags
= BC_F_COPYIN
| BC_F_COPYOUT
},
838 { .bc_func
= bridge_ioctl_sproto
, .bc_argsize
= sizeof(struct ifbrparam
),
839 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
841 { .bc_func
= bridge_ioctl_stxhc
, .bc_argsize
= sizeof(struct ifbrparam
),
842 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
844 { .bc_func
= bridge_ioctl_sifmaxaddr
, .bc_argsize
= sizeof(struct ifbreq
),
845 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
847 { .bc_func
= bridge_ioctl_ghostfilter
, .bc_argsize
= sizeof(struct ifbrhostfilter
),
848 .bc_flags
= BC_F_COPYIN
| BC_F_COPYOUT
},
849 { .bc_func
= bridge_ioctl_shostfilter
, .bc_argsize
= sizeof(struct ifbrhostfilter
),
850 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
852 { .bc_func
= bridge_ioctl_gmnelist32
, .bc_argsize
= sizeof(struct ifbrmnelist32
),
853 .bc_flags
= BC_F_COPYIN
| BC_F_COPYOUT
},
856 static const struct bridge_control bridge_control_table64
[] = {
857 { .bc_func
= bridge_ioctl_add
, .bc_argsize
= sizeof(struct ifbreq
), /* 0 */
858 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
859 { .bc_func
= bridge_ioctl_del
, .bc_argsize
= sizeof(struct ifbreq
),
860 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
862 { .bc_func
= bridge_ioctl_gifflags
, .bc_argsize
= sizeof(struct ifbreq
),
863 .bc_flags
= BC_F_COPYIN
| BC_F_COPYOUT
},
864 { .bc_func
= bridge_ioctl_sifflags
, .bc_argsize
= sizeof(struct ifbreq
),
865 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
867 { .bc_func
= bridge_ioctl_scache
, .bc_argsize
= sizeof(struct ifbrparam
),
868 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
869 { .bc_func
= bridge_ioctl_gcache
, .bc_argsize
= sizeof(struct ifbrparam
),
870 .bc_flags
= BC_F_COPYOUT
},
872 { .bc_func
= bridge_ioctl_gifs64
, .bc_argsize
= sizeof(struct ifbifconf64
),
873 .bc_flags
= BC_F_COPYIN
| BC_F_COPYOUT
},
874 { .bc_func
= bridge_ioctl_rts64
, .bc_argsize
= sizeof(struct ifbaconf64
),
875 .bc_flags
= BC_F_COPYIN
| BC_F_COPYOUT
},
877 { .bc_func
= bridge_ioctl_saddr64
, .bc_argsize
= sizeof(struct ifbareq64
),
878 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
880 { .bc_func
= bridge_ioctl_sto
, .bc_argsize
= sizeof(struct ifbrparam
),
881 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
882 { .bc_func
= bridge_ioctl_gto
, .bc_argsize
= sizeof(struct ifbrparam
), /* 10 */
883 .bc_flags
= BC_F_COPYOUT
},
885 { .bc_func
= bridge_ioctl_daddr64
, .bc_argsize
= sizeof(struct ifbareq64
),
886 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
888 { .bc_func
= bridge_ioctl_flush
, .bc_argsize
= sizeof(struct ifbreq
),
889 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
891 { .bc_func
= bridge_ioctl_gpri
, .bc_argsize
= sizeof(struct ifbrparam
),
892 .bc_flags
= BC_F_COPYOUT
},
893 { .bc_func
= bridge_ioctl_spri
, .bc_argsize
= sizeof(struct ifbrparam
),
894 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
896 { .bc_func
= bridge_ioctl_ght
, .bc_argsize
= sizeof(struct ifbrparam
),
897 .bc_flags
= BC_F_COPYOUT
},
898 { .bc_func
= bridge_ioctl_sht
, .bc_argsize
= sizeof(struct ifbrparam
),
899 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
901 { .bc_func
= bridge_ioctl_gfd
, .bc_argsize
= sizeof(struct ifbrparam
),
902 .bc_flags
= BC_F_COPYOUT
},
903 { .bc_func
= bridge_ioctl_sfd
, .bc_argsize
= sizeof(struct ifbrparam
),
904 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
906 { .bc_func
= bridge_ioctl_gma
, .bc_argsize
= sizeof(struct ifbrparam
),
907 .bc_flags
= BC_F_COPYOUT
},
908 { .bc_func
= bridge_ioctl_sma
, .bc_argsize
= sizeof(struct ifbrparam
), /* 20 */
909 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
911 { .bc_func
= bridge_ioctl_sifprio
, .bc_argsize
= sizeof(struct ifbreq
),
912 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
914 { .bc_func
= bridge_ioctl_sifcost
, .bc_argsize
= sizeof(struct ifbreq
),
915 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
917 { .bc_func
= bridge_ioctl_gfilt
, .bc_argsize
= sizeof(struct ifbrparam
),
918 .bc_flags
= BC_F_COPYOUT
},
919 { .bc_func
= bridge_ioctl_sfilt
, .bc_argsize
= sizeof(struct ifbrparam
),
920 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
922 { .bc_func
= bridge_ioctl_purge
, .bc_argsize
= sizeof(struct ifbreq
),
923 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
925 { .bc_func
= bridge_ioctl_addspan
, .bc_argsize
= sizeof(struct ifbreq
),
926 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
927 { .bc_func
= bridge_ioctl_delspan
, .bc_argsize
= sizeof(struct ifbreq
),
928 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
930 { .bc_func
= bridge_ioctl_gbparam64
, .bc_argsize
= sizeof(struct ifbropreq64
),
931 .bc_flags
= BC_F_COPYOUT
},
933 { .bc_func
= bridge_ioctl_grte
, .bc_argsize
= sizeof(struct ifbrparam
),
934 .bc_flags
= BC_F_COPYOUT
},
936 { .bc_func
= bridge_ioctl_gifsstp64
, .bc_argsize
= sizeof(struct ifbpstpconf64
), /* 30 */
937 .bc_flags
= BC_F_COPYIN
| BC_F_COPYOUT
},
939 { .bc_func
= bridge_ioctl_sproto
, .bc_argsize
= sizeof(struct ifbrparam
),
940 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
942 { .bc_func
= bridge_ioctl_stxhc
, .bc_argsize
= sizeof(struct ifbrparam
),
943 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
945 { .bc_func
= bridge_ioctl_sifmaxaddr
, .bc_argsize
= sizeof(struct ifbreq
),
946 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
948 { .bc_func
= bridge_ioctl_ghostfilter
, .bc_argsize
= sizeof(struct ifbrhostfilter
),
949 .bc_flags
= BC_F_COPYIN
| BC_F_COPYOUT
},
950 { .bc_func
= bridge_ioctl_shostfilter
, .bc_argsize
= sizeof(struct ifbrhostfilter
),
951 .bc_flags
= BC_F_COPYIN
| BC_F_SUSER
},
953 { .bc_func
= bridge_ioctl_gmnelist64
, .bc_argsize
= sizeof(struct ifbrmnelist64
),
954 .bc_flags
= BC_F_COPYIN
| BC_F_COPYOUT
},
957 static const unsigned int bridge_control_table_size
=
958 sizeof(bridge_control_table32
) / sizeof(bridge_control_table32
[0]);
960 static LIST_HEAD(, bridge_softc
) bridge_list
=
961 LIST_HEAD_INITIALIZER(bridge_list
);
963 static lck_grp_t
*bridge_lock_grp
= NULL
;
964 static lck_attr_t
*bridge_lock_attr
= NULL
;
966 #define BRIDGENAME "bridge"
967 #define BRIDGES_MAX IF_MAXUNIT
968 #define BRIDGE_ZONE_MAX_ELEM MIN(IFNETS_MAX, BRIDGES_MAX)
970 static struct if_clone bridge_cloner
=
971 IF_CLONE_INITIALIZER(BRIDGENAME
, bridge_clone_create
, bridge_clone_destroy
,
972 0, BRIDGES_MAX
, BRIDGE_ZONE_MAX_ELEM
, sizeof(struct bridge_softc
));
974 static int if_bridge_txstart
= 0;
975 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, txstart
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
976 &if_bridge_txstart
, 0, "Bridge interface uses TXSTART model");
979 static int if_bridge_debug
= 0;
980 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
981 &if_bridge_debug
, 0, "Bridge debug");
983 static void printf_ether_header(struct ether_header
*);
984 static void printf_mbuf_data(mbuf_t
, size_t, size_t);
985 static void printf_mbuf_pkthdr(mbuf_t
, const char *, const char *);
986 static void printf_mbuf(mbuf_t
, const char *, const char *);
987 static void link_print(struct bridge_softc
* sc
);
989 static void bridge_lock(struct bridge_softc
*);
990 static void bridge_unlock(struct bridge_softc
*);
991 static int bridge_lock2ref(struct bridge_softc
*);
992 static void bridge_unref(struct bridge_softc
*);
993 static void bridge_xlock(struct bridge_softc
*);
994 static void bridge_xdrop(struct bridge_softc
*);
997 bridge_lock(struct bridge_softc
*sc
)
999 void *lr_saved
= __builtin_return_address(0);
1001 BRIDGE_LOCK_ASSERT_NOTHELD(sc
);
1005 sc
->lock_lr
[sc
->next_lock_lr
] = lr_saved
;
1006 sc
->next_lock_lr
= (sc
->next_lock_lr
+ 1) % SO_LCKDBG_MAX
;
1010 bridge_unlock(struct bridge_softc
*sc
)
1012 void *lr_saved
= __builtin_return_address(0);
1014 BRIDGE_LOCK_ASSERT_HELD(sc
);
1016 sc
->unlock_lr
[sc
->next_unlock_lr
] = lr_saved
;
1017 sc
->next_unlock_lr
= (sc
->next_unlock_lr
+ 1) % SO_LCKDBG_MAX
;
1023 bridge_lock2ref(struct bridge_softc
*sc
)
1026 void *lr_saved
= __builtin_return_address(0);
1028 BRIDGE_LOCK_ASSERT_HELD(sc
);
1030 if (sc
->sc_iflist_xcnt
> 0) {
1033 sc
->sc_iflist_ref
++;
1036 sc
->unlock_lr
[sc
->next_unlock_lr
] = lr_saved
;
1037 sc
->next_unlock_lr
= (sc
->next_unlock_lr
+ 1) % SO_LCKDBG_MAX
;
1045 bridge_unref(struct bridge_softc
*sc
)
1047 void *lr_saved
= __builtin_return_address(0);
1049 BRIDGE_LOCK_ASSERT_NOTHELD(sc
);
1052 sc
->lock_lr
[sc
->next_lock_lr
] = lr_saved
;
1053 sc
->next_lock_lr
= (sc
->next_lock_lr
+ 1) % SO_LCKDBG_MAX
;
1055 sc
->sc_iflist_ref
--;
1057 sc
->unlock_lr
[sc
->next_unlock_lr
] = lr_saved
;
1058 sc
->next_unlock_lr
= (sc
->next_unlock_lr
+ 1) % SO_LCKDBG_MAX
;
1059 if ((sc
->sc_iflist_xcnt
> 0) && (sc
->sc_iflist_ref
== 0)) {
1068 bridge_xlock(struct bridge_softc
*sc
)
1070 void *lr_saved
= __builtin_return_address(0);
1072 BRIDGE_LOCK_ASSERT_HELD(sc
);
1074 sc
->sc_iflist_xcnt
++;
1075 while (sc
->sc_iflist_ref
> 0) {
1076 sc
->unlock_lr
[sc
->next_unlock_lr
] = lr_saved
;
1077 sc
->next_unlock_lr
= (sc
->next_unlock_lr
+ 1) % SO_LCKDBG_MAX
;
1079 msleep(&sc
->sc_cv
, &sc
->sc_mtx
, PZERO
, "BRIDGE_XLOCK", NULL
);
1081 sc
->lock_lr
[sc
->next_lock_lr
] = lr_saved
;
1082 sc
->next_lock_lr
= (sc
->next_lock_lr
+ 1) % SO_LCKDBG_MAX
;
1087 bridge_xdrop(struct bridge_softc
*sc
)
1089 BRIDGE_LOCK_ASSERT_HELD(sc
);
1091 sc
->sc_iflist_xcnt
--;
1095 printf_mbuf_pkthdr(mbuf_t m
, const char *prefix
, const char *suffix
)
1098 printf("%spktlen: %u rcvif: 0x%llx header: 0x%llx "
1099 "nextpkt: 0x%llx%s",
1100 prefix
? prefix
: "", (unsigned int)mbuf_pkthdr_len(m
),
1101 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_pkthdr_rcvif(m
)),
1102 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_pkthdr_header(m
)),
1103 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_nextpkt(m
)),
1104 suffix
? suffix
: "");
1106 printf("%s<NULL>%s\n", prefix
, suffix
);
1111 printf_mbuf(mbuf_t m
, const char *prefix
, const char *suffix
)
1114 printf("%s0x%llx type: %u flags: 0x%x len: %u data: 0x%llx "
1115 "maxlen: %u datastart: 0x%llx next: 0x%llx%s",
1116 prefix
? prefix
: "", (uint64_t)VM_KERNEL_ADDRPERM(m
),
1117 mbuf_type(m
), mbuf_flags(m
), (unsigned int)mbuf_len(m
),
1118 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m
)),
1119 (unsigned int)mbuf_maxlen(m
),
1120 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_datastart(m
)),
1121 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_next(m
)),
1122 !suffix
|| (mbuf_flags(m
) & MBUF_PKTHDR
) ? "" : suffix
);
1123 if ((mbuf_flags(m
) & MBUF_PKTHDR
)) {
1124 printf_mbuf_pkthdr(m
, " ", suffix
);
1127 printf("%s<NULL>%s\n", prefix
, suffix
);
1132 printf_mbuf_data(mbuf_t m
, size_t offset
, size_t len
)
1136 size_t pktlen
, mlen
, maxlen
;
1139 pktlen
= mbuf_pkthdr_len(m
);
1141 if (offset
> pktlen
) {
1145 maxlen
= (pktlen
- offset
> len
) ? len
: pktlen
- offset
;
1149 for (i
= 0, j
= 0; i
< maxlen
; i
++, j
++) {
1160 printf("%02x%s", ptr
[j
], i
% 2 ? " " : "");
1166 printf_ether_header(struct ether_header
*eh
)
1168 printf("%02x:%02x:%02x:%02x:%02x:%02x > "
1169 "%02x:%02x:%02x:%02x:%02x:%02x 0x%04x ",
1170 eh
->ether_shost
[0], eh
->ether_shost
[1], eh
->ether_shost
[2],
1171 eh
->ether_shost
[3], eh
->ether_shost
[4], eh
->ether_shost
[5],
1172 eh
->ether_dhost
[0], eh
->ether_dhost
[1], eh
->ether_dhost
[2],
1173 eh
->ether_dhost
[3], eh
->ether_dhost
[4], eh
->ether_dhost
[5],
1174 ntohs(eh
->ether_type
));
1178 link_print(struct bridge_softc
* sc
)
1181 uint32_t sdl_buffer
[offsetof(struct sockaddr_dl
, sdl_data
) +
1182 IFNAMSIZ
+ ETHER_ADDR_LEN
];
1183 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sdl_buffer
;
1185 memset(sdl
, 0, sizeof(sdl_buffer
));
1186 sdl
->sdl_family
= AF_LINK
;
1187 sdl
->sdl_nlen
= strlen(sc
->sc_if_xname
);
1188 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
1189 sdl
->sdl_len
= offsetof(struct sockaddr_dl
, sdl_data
);
1190 memcpy(sdl
->sdl_data
, sc
->sc_if_xname
, sdl
->sdl_nlen
);
1191 memcpy(LLADDR(sdl
), sc
->sc_defaddr
, ETHER_ADDR_LEN
);
1194 printf("sdl len %d index %d family %d type 0x%x nlen %d alen %d"
1195 " slen %d addr ", sdl
->sdl_len
, sdl
->sdl_index
,
1196 sdl
->sdl_family
, sdl
->sdl_type
, sdl
->sdl_nlen
,
1197 sdl
->sdl_alen
, sdl
->sdl_slen
);
1199 for (i
= 0; i
< sdl
->sdl_alen
; i
++) {
1200 printf("%s%x", i
? ":" : "", (CONST_LLADDR(sdl
))[i
]);
1206 bridge_debug_flag_is_set(uint32_t flag
)
1208 return (if_bridge_debug
& flag
) != 0;
1211 #endif /* BRIDGE_DEBUG */
1216 * Pseudo-device attach routine.
1218 __private_extern__
int
1223 lck_grp_attr_t
*lck_grp_attr
= NULL
;
1225 bridge_rtnode_pool
= zinit(sizeof(struct bridge_rtnode
),
1226 1024 * sizeof(struct bridge_rtnode
), 0, "bridge_rtnode");
1227 zone_change(bridge_rtnode_pool
, Z_CALLERACCT
, FALSE
);
1229 bridge_mne_pool
= zinit(sizeof(struct mac_nat_entry
),
1230 256 * sizeof(struct mac_nat_entry
), 0, "bridge_mac_nat_entry");
1231 zone_change(bridge_mne_pool
, Z_CALLERACCT
, FALSE
);
1233 lck_grp_attr
= lck_grp_attr_alloc_init();
1235 bridge_lock_grp
= lck_grp_alloc_init("if_bridge", lck_grp_attr
);
1237 bridge_lock_attr
= lck_attr_alloc_init();
1240 lck_attr_setdebug(bridge_lock_attr
);
1243 lck_mtx_init(&bridge_list_mtx
, bridge_lock_grp
, bridge_lock_attr
);
1245 /* can free the attributes once we've allocated the group lock */
1246 lck_grp_attr_free(lck_grp_attr
);
1248 LIST_INIT(&bridge_list
);
1252 #endif /* BRIDGESTP */
1254 error
= if_clone_attach(&bridge_cloner
);
1256 printf("%s: ifnet_clone_attach failed %d\n", __func__
, error
);
1262 #if defined(PFIL_HOOKS)
1264 * handler for net.link.bridge.pfil_ipfw
1267 sysctl_pfil_ipfw SYSCTL_HANDLER_ARGS
1269 #pragma unused(arg1, arg2)
1270 int enable
= pfil_ipfw
;
1273 error
= sysctl_handle_int(oidp
, &enable
, 0, req
);
1274 enable
= (enable
) ? 1 : 0;
1276 if (enable
!= pfil_ipfw
) {
1280 * Disable pfil so that ipfw doesnt run twice, if the user
1281 * really wants both then they can re-enable pfil_bridge and/or
1282 * pfil_member. Also allow non-ip packets as ipfw can filter by
1295 SYSCTL_PROC(_net_link_bridge
, OID_AUTO
, ipfw
, CTLTYPE_INT
| CTLFLAG_RW
,
1296 &pfil_ipfw
, 0, &sysctl_pfil_ipfw
, "I", "Layer2 filter with IPFW");
1297 #endif /* PFIL_HOOKS */
1300 bridge_ifnet_set_attrs(struct ifnet
* ifp
)
1304 error
= ifnet_set_mtu(ifp
, ETHERMTU
);
1306 printf("%s: ifnet_set_mtu failed %d\n", __func__
, error
);
1309 error
= ifnet_set_addrlen(ifp
, ETHER_ADDR_LEN
);
1311 printf("%s: ifnet_set_addrlen failed %d\n", __func__
, error
);
1314 error
= ifnet_set_hdrlen(ifp
, ETHER_HDR_LEN
);
1316 printf("%s: ifnet_set_hdrlen failed %d\n", __func__
, error
);
1319 error
= ifnet_set_flags(ifp
,
1320 IFF_BROADCAST
| IFF_SIMPLEX
| IFF_NOTRAILERS
| IFF_MULTICAST
,
1324 printf("%s: ifnet_set_flags failed %d\n", __func__
, error
);
1332 * bridge_clone_create:
1334 * Create a new bridge instance.
1337 bridge_clone_create(struct if_clone
*ifc
, uint32_t unit
, void *params
)
1339 #pragma unused(params)
1340 struct ifnet
*ifp
= NULL
;
1341 struct bridge_softc
*sc
= NULL
;
1342 struct bridge_softc
*sc2
= NULL
;
1343 struct ifnet_init_eparams init_params
;
1345 uint8_t eth_hostid
[ETHER_ADDR_LEN
];
1346 int fb
, retry
, has_hostid
;
1348 sc
= if_clone_softc_allocate(&bridge_cloner
);
1354 lck_mtx_init(&sc
->sc_mtx
, bridge_lock_grp
, bridge_lock_attr
);
1355 sc
->sc_brtmax
= BRIDGE_RTABLE_MAX
;
1356 sc
->sc_mne_max
= BRIDGE_MAC_NAT_ENTRY_MAX
;
1357 sc
->sc_brttimeout
= BRIDGE_RTABLE_TIMEOUT
;
1358 sc
->sc_filter_flags
= 0;
1360 TAILQ_INIT(&sc
->sc_iflist
);
1362 /* use the interface name as the unique id for ifp recycle */
1363 snprintf(sc
->sc_if_xname
, sizeof(sc
->sc_if_xname
), "%s%d",
1364 ifc
->ifc_name
, unit
);
1365 bzero(&init_params
, sizeof(init_params
));
1366 init_params
.ver
= IFNET_INIT_CURRENT_VERSION
;
1367 init_params
.len
= sizeof(init_params
);
1368 /* Initialize our routing table. */
1369 error
= bridge_rtable_init(sc
);
1371 printf("%s: bridge_rtable_init failed %d\n",
1375 TAILQ_INIT(&sc
->sc_spanlist
);
1376 if (if_bridge_txstart
) {
1377 init_params
.start
= bridge_start
;
1379 init_params
.flags
= IFNET_INIT_LEGACY
;
1380 init_params
.output
= bridge_output
;
1382 init_params
.set_bpf_tap
= bridge_set_bpf_tap
;
1383 init_params
.uniqueid
= sc
->sc_if_xname
;
1384 init_params
.uniqueid_len
= strlen(sc
->sc_if_xname
);
1385 init_params
.sndq_maxlen
= IFQ_MAXLEN
;
1386 init_params
.name
= ifc
->ifc_name
;
1387 init_params
.unit
= unit
;
1388 init_params
.family
= IFNET_FAMILY_ETHERNET
;
1389 init_params
.type
= IFT_BRIDGE
;
1390 init_params
.demux
= ether_demux
;
1391 init_params
.add_proto
= ether_add_proto
;
1392 init_params
.del_proto
= ether_del_proto
;
1393 init_params
.check_multi
= ether_check_multi
;
1394 init_params
.framer_extended
= ether_frameout_extended
;
1395 init_params
.softc
= sc
;
1396 init_params
.ioctl
= bridge_ioctl
;
1397 init_params
.detach
= bridge_detach
;
1398 init_params
.broadcast_addr
= etherbroadcastaddr
;
1399 init_params
.broadcast_len
= ETHER_ADDR_LEN
;
1401 error
= ifnet_allocate_extended(&init_params
, &ifp
);
1403 printf("%s: ifnet_allocate failed %d\n",
1407 LIST_INIT(&sc
->sc_mne_list
);
1408 LIST_INIT(&sc
->sc_mne_list_v6
);
1410 error
= bridge_ifnet_set_attrs(ifp
);
1412 printf("%s: bridge_ifnet_set_attrs failed %d\n",
1417 * Generate an ethernet address with a locally administered address.
1419 * Since we are using random ethernet addresses for the bridge, it is
1420 * possible that we might have address collisions, so make sure that
1421 * this hardware address isn't already in use on another bridge.
1422 * The first try uses the "hostid" and falls back to read_frandom();
1423 * for "hostid", we use the MAC address of the first-encountered
1424 * Ethernet-type interface that is currently configured.
1427 has_hostid
= (uuid_get_ethernet(ð_hostid
[0]) == 0);
1428 for (retry
= 1; retry
!= 0;) {
1429 if (fb
|| has_hostid
== 0) {
1430 read_frandom(&sc
->sc_defaddr
, ETHER_ADDR_LEN
);
1431 sc
->sc_defaddr
[0] &= ~1; /* clear multicast bit */
1432 sc
->sc_defaddr
[0] |= 2; /* set the LAA bit */
1434 bcopy(ð_hostid
[0], &sc
->sc_defaddr
,
1436 sc
->sc_defaddr
[0] &= ~1; /* clear multicast bit */
1437 sc
->sc_defaddr
[0] |= 2; /* set the LAA bit */
1438 sc
->sc_defaddr
[3] = /* stir it up a bit */
1439 ((sc
->sc_defaddr
[3] & 0x0f) << 4) |
1440 ((sc
->sc_defaddr
[3] & 0xf0) >> 4);
1442 * Mix in the LSB as it's actually pretty significant,
1443 * see rdar://14076061
1446 (((sc
->sc_defaddr
[4] & 0x0f) << 4) |
1447 ((sc
->sc_defaddr
[4] & 0xf0) >> 4)) ^
1449 sc
->sc_defaddr
[5] = ifp
->if_unit
& 0xff;
1454 lck_mtx_lock(&bridge_list_mtx
);
1455 LIST_FOREACH(sc2
, &bridge_list
, sc_list
) {
1456 if (memcmp(sc
->sc_defaddr
,
1457 IF_LLADDR(sc2
->sc_ifp
), ETHER_ADDR_LEN
) == 0) {
1461 lck_mtx_unlock(&bridge_list_mtx
);
1464 sc
->sc_flags
&= ~SCF_MEDIA_ACTIVE
;
1467 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE
)) {
1471 error
= ifnet_attach(ifp
, NULL
);
1473 printf("%s: ifnet_attach failed %d\n", __func__
, error
);
1477 error
= ifnet_set_lladdr_and_type(ifp
, sc
->sc_defaddr
, ETHER_ADDR_LEN
,
1480 printf("%s: ifnet_set_lladdr_and_type failed %d\n", __func__
,
1485 ifnet_set_offload(ifp
,
1486 IFNET_CSUM_IP
| IFNET_CSUM_TCP
| IFNET_CSUM_UDP
|
1487 IFNET_CSUM_TCPIPV6
| IFNET_CSUM_UDPIPV6
| IFNET_MULTIPAGES
);
1488 error
= bridge_set_tso(sc
);
1490 printf("%s: bridge_set_tso failed %d\n",
1495 bstp_attach(&sc
->sc_stp
, &bridge_ops
);
1496 #endif /* BRIDGESTP */
1498 lck_mtx_lock(&bridge_list_mtx
);
1499 LIST_INSERT_HEAD(&bridge_list
, sc
, sc_list
);
1500 lck_mtx_unlock(&bridge_list_mtx
);
1502 /* attach as ethernet */
1503 error
= bpf_attach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
),
1508 printf("%s failed error %d\n", __func__
, error
);
1509 /* TBD: Clean up: sc, sc_rthash etc */
1516 * bridge_clone_destroy:
1518 * Destroy a bridge instance.
1521 bridge_clone_destroy(struct ifnet
*ifp
)
1523 struct bridge_softc
*sc
= ifp
->if_softc
;
1524 struct bridge_iflist
*bif
;
1528 if ((sc
->sc_flags
& SCF_DETACHING
)) {
1532 sc
->sc_flags
|= SCF_DETACHING
;
1534 bridge_ifstop(ifp
, 1);
1536 bridge_cancel_delayed_call(&sc
->sc_resize_call
);
1538 bridge_cleanup_delayed_call(&sc
->sc_resize_call
);
1539 bridge_cleanup_delayed_call(&sc
->sc_aging_timer
);
1541 error
= ifnet_set_flags(ifp
, 0, IFF_UP
);
1543 printf("%s: ifnet_set_flags failed %d\n", __func__
, error
);
1546 while ((bif
= TAILQ_FIRST(&sc
->sc_iflist
)) != NULL
) {
1547 bridge_delete_member(sc
, bif
, 0);
1550 while ((bif
= TAILQ_FIRST(&sc
->sc_spanlist
)) != NULL
) {
1551 bridge_delete_span(sc
, bif
);
1555 error
= ifnet_detach(ifp
);
1557 panic("%s: ifnet_detach(%p) failed %d\n",
1558 __func__
, ifp
, error
);
1563 #define DRVSPEC do { \
1564 if (ifd->ifd_cmd >= bridge_control_table_size) { \
1568 bc = &bridge_control_table[ifd->ifd_cmd]; \
1570 if (cmd == SIOCGDRVSPEC && \
1571 (bc->bc_flags & BC_F_COPYOUT) == 0) { \
1574 } else if (cmd == SIOCSDRVSPEC && \
1575 (bc->bc_flags & BC_F_COPYOUT) != 0) { \
1580 if (bc->bc_flags & BC_F_SUSER) { \
1581 error = kauth_authorize_generic(kauth_cred_get(), \
1582 KAUTH_GENERIC_ISSUSER); \
1587 if (ifd->ifd_len != bc->bc_argsize || \
1588 ifd->ifd_len > sizeof (args)) { \
1593 bzero(&args, sizeof (args)); \
1594 if (bc->bc_flags & BC_F_COPYIN) { \
1595 error = copyin(ifd->ifd_data, &args, ifd->ifd_len); \
1601 error = (*bc->bc_func)(sc, &args); \
1602 BRIDGE_UNLOCK(sc); \
1606 if (bc->bc_flags & BC_F_COPYOUT) \
1607 error = copyout(&args, ifd->ifd_data, ifd->ifd_len); \
1613 * Handle a control request from the operator.
1616 bridge_ioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
1618 struct bridge_softc
*sc
= ifp
->if_softc
;
1619 struct ifreq
*ifr
= (struct ifreq
*)data
;
1620 struct bridge_iflist
*bif
;
1623 BRIDGE_LOCK_ASSERT_NOTHELD(sc
);
1626 if (IF_BRIDGE_DEBUG(BR_DBGF_IOCTL
)) {
1627 printf("%s: ifp %s cmd 0x%08lx (%c%c [%lu] %c %lu)\n",
1628 __func__
, ifp
->if_xname
, cmd
, (cmd
& IOC_IN
) ? 'I' : ' ',
1629 (cmd
& IOC_OUT
) ? 'O' : ' ', IOCPARM_LEN(cmd
),
1630 (char)IOCGROUP(cmd
), cmd
& 0xff);
1632 #endif /* BRIDGE_DEBUG */
1637 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
1640 case SIOCGIFMEDIA32
:
1641 case SIOCGIFMEDIA64
: {
1642 struct ifmediareq
*ifmr
= (struct ifmediareq
*)data
;
1643 user_addr_t user_addr
;
1645 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
1646 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
1647 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
1649 ifmr
->ifm_status
= IFM_AVALID
;
1651 ifmr
->ifm_count
= 1;
1654 if (!(sc
->sc_flags
& SCF_DETACHING
) &&
1655 (sc
->sc_flags
& SCF_MEDIA_ACTIVE
)) {
1656 ifmr
->ifm_status
|= IFM_ACTIVE
;
1657 ifmr
->ifm_active
= ifmr
->ifm_current
=
1658 IFM_ETHER
| IFM_AUTO
;
1660 ifmr
->ifm_active
= ifmr
->ifm_current
= IFM_NONE
;
1664 if (user_addr
!= USER_ADDR_NULL
) {
1665 error
= copyout(&ifmr
->ifm_current
, user_addr
,
1675 case SIOCSDRVSPEC32
:
1676 case SIOCGDRVSPEC32
: {
1678 struct ifbreq ifbreq
;
1679 struct ifbifconf32 ifbifconf
;
1680 struct ifbareq32 ifbareq
;
1681 struct ifbaconf32 ifbaconf
;
1682 struct ifbrparam ifbrparam
;
1683 struct ifbropreq32 ifbropreq
;
1685 struct ifdrv32
*ifd
= (struct ifdrv32
*)data
;
1686 const struct bridge_control
*bridge_control_table
=
1687 bridge_control_table32
, *bc
;
1693 case SIOCSDRVSPEC64
:
1694 case SIOCGDRVSPEC64
: {
1696 struct ifbreq ifbreq
;
1697 struct ifbifconf64 ifbifconf
;
1698 struct ifbareq64 ifbareq
;
1699 struct ifbaconf64 ifbaconf
;
1700 struct ifbrparam ifbrparam
;
1701 struct ifbropreq64 ifbropreq
;
1703 struct ifdrv64
*ifd
= (struct ifdrv64
*)data
;
1704 const struct bridge_control
*bridge_control_table
=
1705 bridge_control_table64
, *bc
;
1713 if (!(ifp
->if_flags
& IFF_UP
) &&
1714 (ifp
->if_flags
& IFF_RUNNING
)) {
1716 * If interface is marked down and it is running,
1717 * then stop and disable it.
1720 bridge_ifstop(ifp
, 1);
1722 } else if ((ifp
->if_flags
& IFF_UP
) &&
1723 !(ifp
->if_flags
& IFF_RUNNING
)) {
1725 * If interface is marked up and it is stopped, then
1729 error
= bridge_init(ifp
);
1735 error
= ifnet_set_lladdr(ifp
, ifr
->ifr_addr
.sa_data
,
1736 ifr
->ifr_addr
.sa_len
);
1738 printf("%s: SIOCSIFLLADDR error %d\n", ifp
->if_xname
,
1744 if (ifr
->ifr_mtu
< 576) {
1749 if (TAILQ_EMPTY(&sc
->sc_iflist
)) {
1750 sc
->sc_ifp
->if_mtu
= ifr
->ifr_mtu
;
1754 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1755 if (bif
->bif_ifp
->if_mtu
!= (unsigned)ifr
->ifr_mtu
) {
1756 printf("%s: invalid MTU: %u(%s) != %d\n",
1757 sc
->sc_ifp
->if_xname
,
1758 bif
->bif_ifp
->if_mtu
,
1759 bif
->bif_ifp
->if_xname
, ifr
->ifr_mtu
);
1765 sc
->sc_ifp
->if_mtu
= ifr
->ifr_mtu
;
1771 error
= ether_ioctl(ifp
, cmd
, data
);
1773 if (error
!= 0 && error
!= EOPNOTSUPP
) {
1774 printf("%s: ifp %s cmd 0x%08lx "
1775 "(%c%c [%lu] %c %lu) failed error: %d\n",
1776 __func__
, ifp
->if_xname
, cmd
,
1777 (cmd
& IOC_IN
) ? 'I' : ' ',
1778 (cmd
& IOC_OUT
) ? 'O' : ' ',
1779 IOCPARM_LEN(cmd
), (char)IOCGROUP(cmd
),
1782 #endif /* BRIDGE_DEBUG */
1785 BRIDGE_LOCK_ASSERT_NOTHELD(sc
);
1794 * Clear or restore unwanted capabilities on the member interface
1797 bridge_mutecaps(struct bridge_softc
*sc
)
1799 struct bridge_iflist
*bif
;
1802 /* Initial bitmask of capabilities to test */
1803 mask
= BRIDGE_IFCAPS_MASK
;
1805 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1806 /* Every member must support it or its disabled */
1807 mask
&= bif
->bif_savedcaps
;
1810 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1811 enabled
= bif
->bif_ifp
->if_capenable
;
1812 enabled
&= ~BRIDGE_IFCAPS_STRIP
;
1813 /* strip off mask bits and enable them again if allowed */
1814 enabled
&= ~BRIDGE_IFCAPS_MASK
;
1817 bridge_set_ifcap(sc
, bif
, enabled
);
1822 bridge_set_ifcap(struct bridge_softc
*sc
, struct bridge_iflist
*bif
, int set
)
1824 struct ifnet
*ifp
= bif
->bif_ifp
;
1828 bzero(&ifr
, sizeof(ifr
));
1829 ifr
.ifr_reqcap
= set
;
1831 if (ifp
->if_capenable
!= set
) {
1833 error
= (*ifp
->if_ioctl
)(ifp
, SIOCSIFCAP
, (caddr_t
)&ifr
);
1834 IFF_UNLOCKGIANT(ifp
);
1836 printf("%s: %s error setting interface capabilities "
1837 "on %s\n", __func__
, sc
->sc_ifp
->if_xname
,
1842 #endif /* HAS_IF_CAP */
1845 bridge_set_tso(struct bridge_softc
*sc
)
1847 struct bridge_iflist
*bif
;
1848 u_int32_t tso_v4_mtu
;
1849 u_int32_t tso_v6_mtu
;
1850 ifnet_offload_t offload
;
1853 /* By default, support TSO */
1854 offload
= sc
->sc_ifp
->if_hwassist
| IFNET_TSO_IPV4
| IFNET_TSO_IPV6
;
1855 tso_v4_mtu
= IP_MAXPACKET
;
1856 tso_v6_mtu
= IP_MAXPACKET
;
1858 /* Use the lowest common denominator of the members */
1859 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1860 ifnet_t ifp
= bif
->bif_ifp
;
1866 if (offload
& IFNET_TSO_IPV4
) {
1867 if (ifp
->if_hwassist
& IFNET_TSO_IPV4
) {
1868 if (tso_v4_mtu
> ifp
->if_tso_v4_mtu
) {
1869 tso_v4_mtu
= ifp
->if_tso_v4_mtu
;
1872 offload
&= ~IFNET_TSO_IPV4
;
1876 if (offload
& IFNET_TSO_IPV6
) {
1877 if (ifp
->if_hwassist
& IFNET_TSO_IPV6
) {
1878 if (tso_v6_mtu
> ifp
->if_tso_v6_mtu
) {
1879 tso_v6_mtu
= ifp
->if_tso_v6_mtu
;
1882 offload
&= ~IFNET_TSO_IPV6
;
1888 if (offload
!= sc
->sc_ifp
->if_hwassist
) {
1889 error
= ifnet_set_offload(sc
->sc_ifp
, offload
);
1892 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE
)) {
1893 printf("%s: ifnet_set_offload(%s, 0x%x) "
1894 "failed %d\n", __func__
,
1895 sc
->sc_ifp
->if_xname
, offload
, error
);
1897 #endif /* BRIDGE_DEBUG */
1901 * For ifnet_set_tso_mtu() sake, the TSO MTU must be at least
1902 * as large as the interface MTU
1904 if (sc
->sc_ifp
->if_hwassist
& IFNET_TSO_IPV4
) {
1905 if (tso_v4_mtu
< sc
->sc_ifp
->if_mtu
) {
1906 tso_v4_mtu
= sc
->sc_ifp
->if_mtu
;
1908 error
= ifnet_set_tso_mtu(sc
->sc_ifp
, AF_INET
,
1912 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE
)) {
1913 printf("%s: ifnet_set_tso_mtu(%s, "
1914 "AF_INET, %u) failed %d\n",
1915 __func__
, sc
->sc_ifp
->if_xname
,
1918 #endif /* BRIDGE_DEBUG */
1922 if (sc
->sc_ifp
->if_hwassist
& IFNET_TSO_IPV6
) {
1923 if (tso_v6_mtu
< sc
->sc_ifp
->if_mtu
) {
1924 tso_v6_mtu
= sc
->sc_ifp
->if_mtu
;
1926 error
= ifnet_set_tso_mtu(sc
->sc_ifp
, AF_INET6
,
1930 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE
)) {
1931 printf("%s: ifnet_set_tso_mtu(%s, "
1932 "AF_INET6, %u) failed %d\n",
1933 __func__
, sc
->sc_ifp
->if_xname
,
1936 #endif /* BRIDGE_DEBUG */
1946 * bridge_lookup_member:
1948 * Lookup a bridge member interface.
1950 static struct bridge_iflist
*
1951 bridge_lookup_member(struct bridge_softc
*sc
, const char *name
)
1953 struct bridge_iflist
*bif
;
1956 BRIDGE_LOCK_ASSERT_HELD(sc
);
1958 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1960 if (strcmp(ifp
->if_xname
, name
) == 0) {
1969 * bridge_lookup_member_if:
1971 * Lookup a bridge member interface by ifnet*.
1973 static struct bridge_iflist
*
1974 bridge_lookup_member_if(struct bridge_softc
*sc
, struct ifnet
*member_ifp
)
1976 struct bridge_iflist
*bif
;
1978 BRIDGE_LOCK_ASSERT_HELD(sc
);
1980 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1981 if (bif
->bif_ifp
== member_ifp
) {
1990 bridge_iff_input(void *cookie
, ifnet_t ifp
, protocol_family_t protocol
,
1991 mbuf_t
*data
, char **frame_ptr
)
1993 #pragma unused(protocol)
1995 struct bridge_iflist
*bif
= (struct bridge_iflist
*)cookie
;
1996 struct bridge_softc
*sc
= bif
->bif_sc
;
2001 if ((m
->m_flags
& M_PROTO1
)) {
2005 if (*frame_ptr
>= (char *)mbuf_datastart(m
) &&
2006 *frame_ptr
<= (char *)mbuf_data(m
)) {
2008 frmlen
= (char *)mbuf_data(m
) - *frame_ptr
;
2011 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT
)) {
2012 printf("%s: %s from %s m 0x%llx data 0x%llx frame 0x%llx %s "
2013 "frmlen %lu\n", __func__
, sc
->sc_ifp
->if_xname
,
2014 ifp
->if_xname
, (uint64_t)VM_KERNEL_ADDRPERM(m
),
2015 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m
)),
2016 (uint64_t)VM_KERNEL_ADDRPERM(*frame_ptr
),
2017 included
? "inside" : "outside", frmlen
);
2019 if (IF_BRIDGE_DEBUG(BR_DBGF_MBUF
)) {
2020 printf_mbuf(m
, "bridge_iff_input[", "\n");
2021 printf_ether_header((struct ether_header
*)
2022 (void *)*frame_ptr
);
2023 printf_mbuf_data(m
, 0, 20);
2027 #endif /* BRIDGE_DEBUG */
2028 if (included
== 0) {
2029 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT
)) {
2030 printf("%s: frame_ptr outside mbuf\n", __func__
);
2035 /* Move data pointer to start of frame to the link layer header */
2036 (void) mbuf_setdata(m
, (char *)mbuf_data(m
) - frmlen
,
2037 mbuf_len(m
) + frmlen
);
2038 (void) mbuf_pkthdr_adjustlen(m
, frmlen
);
2040 /* make sure we can access the ethernet header */
2041 if (mbuf_pkthdr_len(m
) < sizeof(struct ether_header
)) {
2042 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT
)) {
2043 printf("%s: short frame %lu < %lu\n", __func__
,
2044 mbuf_pkthdr_len(m
), sizeof(struct ether_header
));
2048 if (mbuf_len(m
) < sizeof(struct ether_header
)) {
2049 error
= mbuf_pullup(data
, sizeof(struct ether_header
));
2051 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT
)) {
2052 printf("%s: mbuf_pullup(%lu) failed %d\n",
2053 __func__
, sizeof(struct ether_header
),
2056 error
= EJUSTRETURN
;
2061 *frame_ptr
= mbuf_data(m
);
2065 error
= bridge_input(ifp
, data
);
2067 /* Adjust packet back to original */
2069 /* bridge_input might have modified *data */
2072 *frame_ptr
= mbuf_data(m
);
2074 (void) mbuf_setdata(m
, (char *)mbuf_data(m
) + frmlen
,
2075 mbuf_len(m
) - frmlen
);
2076 (void) mbuf_pkthdr_adjustlen(m
, -frmlen
);
2079 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT
) &&
2080 IF_BRIDGE_DEBUG(BR_DBGF_MBUF
)) {
2082 printf_mbuf(m
, "bridge_iff_input]", "\n");
2084 #endif /* BRIDGE_DEBUG */
2087 BRIDGE_LOCK_ASSERT_NOTHELD(sc
);
2093 bridge_iff_output(void *cookie
, ifnet_t ifp
, protocol_family_t protocol
,
2096 #pragma unused(protocol)
2098 struct bridge_iflist
*bif
= (struct bridge_iflist
*)cookie
;
2099 struct bridge_softc
*sc
= bif
->bif_sc
;
2102 if ((m
->m_flags
& M_PROTO1
)) {
2107 if (IF_BRIDGE_DEBUG(BR_DBGF_OUTPUT
)) {
2108 printf("%s: %s from %s m 0x%llx data 0x%llx\n", __func__
,
2109 sc
->sc_ifp
->if_xname
, ifp
->if_xname
,
2110 (uint64_t)VM_KERNEL_ADDRPERM(m
),
2111 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m
)));
2113 #endif /* BRIDGE_DEBUG */
2115 error
= bridge_member_output(sc
, ifp
, data
);
2116 if (error
!= 0 && error
!= EJUSTRETURN
) {
2117 printf("%s: bridge_member_output failed error %d\n", __func__
,
2121 BRIDGE_LOCK_ASSERT_NOTHELD(sc
);
2127 bridge_iff_event(void *cookie
, ifnet_t ifp
, protocol_family_t protocol
,
2128 const struct kev_msg
*event_msg
)
2130 #pragma unused(protocol)
2131 struct bridge_iflist
*bif
= (struct bridge_iflist
*)cookie
;
2132 struct bridge_softc
*sc
= bif
->bif_sc
;
2134 if (event_msg
->vendor_code
== KEV_VENDOR_APPLE
&&
2135 event_msg
->kev_class
== KEV_NETWORK_CLASS
&&
2136 event_msg
->kev_subclass
== KEV_DL_SUBCLASS
) {
2138 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE
)) {
2139 printf("%s: %s event_code %u - %s\n", __func__
,
2140 ifp
->if_xname
, event_msg
->event_code
,
2141 dlil_kev_dl_code_str(event_msg
->event_code
));
2143 #endif /* BRIDGE_DEBUG */
2145 switch (event_msg
->event_code
) {
2146 case KEV_DL_IF_DETACHING
:
2147 case KEV_DL_IF_DETACHED
: {
2148 bridge_ifdetach(ifp
);
2151 case KEV_DL_LINK_OFF
:
2152 case KEV_DL_LINK_ON
: {
2153 bridge_iflinkevent(ifp
);
2155 bstp_linkstate(ifp
, event_msg
->event_code
);
2156 #endif /* BRIDGESTP */
2159 case KEV_DL_SIFFLAGS
: {
2160 if ((bif
->bif_flags
& BIFF_PROMISC
) == 0 &&
2161 (ifp
->if_flags
& IFF_UP
)) {
2164 error
= ifnet_set_promiscuous(ifp
, 1);
2167 "ifnet_set_promiscuous (%s)"
2169 __func__
, ifp
->if_xname
,
2172 bif
->bif_flags
|= BIFF_PROMISC
;
2177 case KEV_DL_IFCAP_CHANGED
: {
2183 case KEV_DL_PROTO_DETACHED
:
2184 case KEV_DL_PROTO_ATTACHED
: {
2185 bridge_proto_attach_changed(ifp
);
2195 * bridge_iff_detached:
2197 * Detach an interface from a bridge. Called when a member
2198 * interface is detaching.
2201 bridge_iff_detached(void *cookie
, ifnet_t ifp
)
2203 struct bridge_iflist
*bif
= (struct bridge_iflist
*)cookie
;
2206 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE
)) {
2207 printf("%s: %s\n", __func__
, ifp
->if_xname
);
2209 #endif /* BRIDGE_DEBUG */
2211 bridge_ifdetach(ifp
);
2213 _FREE(bif
, M_DEVBUF
);
2217 bridge_proto_input(ifnet_t ifp
, protocol_family_t protocol
, mbuf_t packet
,
2220 #pragma unused(protocol, packet, header)
2222 printf("%s: unexpected packet from %s\n", __func__
,
2224 #endif /* BRIDGE_DEBUG */
2229 bridge_attach_protocol(struct ifnet
*ifp
)
2232 struct ifnet_attach_proto_param reg
;
2235 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE
)) {
2236 printf("%s: %s\n", __func__
, ifp
->if_xname
);
2238 #endif /* BRIDGE_DEBUG */
2240 bzero(®
, sizeof(reg
));
2241 reg
.input
= bridge_proto_input
;
2243 error
= ifnet_attach_protocol(ifp
, PF_BRIDGE
, ®
);
2245 printf("%s: ifnet_attach_protocol(%s) failed, %d\n",
2246 __func__
, ifp
->if_xname
, error
);
2253 bridge_detach_protocol(struct ifnet
*ifp
)
2258 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE
)) {
2259 printf("%s: %s\n", __func__
, ifp
->if_xname
);
2261 #endif /* BRIDGE_DEBUG */
2262 error
= ifnet_detach_protocol(ifp
, PF_BRIDGE
);
2264 printf("%s: ifnet_detach_protocol(%s) failed, %d\n",
2265 __func__
, ifp
->if_xname
, error
);
2272 * bridge_delete_member:
2274 * Delete the specified member interface.
2277 bridge_delete_member(struct bridge_softc
*sc
, struct bridge_iflist
*bif
,
2280 struct ifnet
*ifs
= bif
->bif_ifp
, *bifp
= sc
->sc_ifp
;
2281 int lladdr_changed
= 0, error
, filt_attached
;
2282 uint8_t eaddr
[ETHER_ADDR_LEN
];
2283 u_int32_t event_code
= 0;
2285 BRIDGE_LOCK_ASSERT_HELD(sc
);
2286 VERIFY(ifs
!= NULL
);
2289 * Remove the member from the list first so it cannot be found anymore
2290 * when we release the bridge lock below
2293 TAILQ_REMOVE(&sc
->sc_iflist
, bif
, bif_next
);
2296 if (sc
->sc_mac_nat_bif
!= NULL
) {
2297 if (bif
== sc
->sc_mac_nat_bif
) {
2298 bridge_mac_nat_disable(sc
);
2300 bridge_mac_nat_flush_entries(sc
, bif
);
2305 switch (ifs
->if_type
) {
2309 * Take the interface out of promiscuous mode.
2311 if (bif
->bif_flags
& BIFF_PROMISC
) {
2313 * Unlock to prevent deadlock with bridge_iff_event() in
2314 * case the driver generates an interface event
2317 (void) ifnet_set_promiscuous(ifs
, 0);
2323 /* currently not supported */
2331 /* reneable any interface capabilities */
2332 bridge_set_ifcap(sc
, bif
, bif
->bif_savedcaps
);
2336 if (bif
->bif_flags
& BIFF_PROTO_ATTACHED
) {
2337 /* Respect lock ordering with DLIL lock */
2339 (void) bridge_detach_protocol(ifs
);
2343 if ((bif
->bif_ifflags
& IFBIF_STP
) != 0) {
2344 bstp_disable(&bif
->bif_stp
);
2346 #endif /* BRIDGESTP */
2349 * If removing the interface that gave the bridge its mac address, set
2350 * the mac address of the bridge to the address of the next member, or
2351 * to its default address if no members are left.
2353 if (bridge_inherit_mac
&& sc
->sc_ifaddr
== ifs
) {
2354 ifnet_release(sc
->sc_ifaddr
);
2355 if (TAILQ_EMPTY(&sc
->sc_iflist
)) {
2356 bcopy(sc
->sc_defaddr
, eaddr
, ETHER_ADDR_LEN
);
2357 sc
->sc_ifaddr
= NULL
;
2360 TAILQ_FIRST(&sc
->sc_iflist
)->bif_ifp
;
2361 bcopy(IF_LLADDR(fif
), eaddr
, ETHER_ADDR_LEN
);
2362 sc
->sc_ifaddr
= fif
;
2363 ifnet_reference(fif
); /* for sc_ifaddr */
2369 bridge_mutecaps(sc
); /* recalculate now this interface is removed */
2370 #endif /* HAS_IF_CAP */
2372 error
= bridge_set_tso(sc
);
2374 printf("%s: bridge_set_tso failed %d\n", __func__
, error
);
2377 bridge_rtdelete(sc
, ifs
, IFBF_FLUSHALL
);
2379 KASSERT(bif
->bif_addrcnt
== 0,
2380 ("%s: %d bridge routes referenced", __func__
, bif
->bif_addrcnt
));
2382 filt_attached
= bif
->bif_flags
& BIFF_FILTER_ATTACHED
;
2385 * Update link status of the bridge based on its remaining members
2387 event_code
= bridge_updatelinkstatus(sc
);
2392 if (lladdr_changed
&&
2393 (error
= ifnet_set_lladdr(bifp
, eaddr
, ETHER_ADDR_LEN
)) != 0) {
2394 printf("%s: ifnet_set_lladdr failed %d\n", __func__
, error
);
2397 if (event_code
!= 0) {
2398 bridge_link_event(bifp
, event_code
);
2402 bstp_destroy(&bif
->bif_stp
); /* prepare to free */
2403 #endif /* BRIDGESTP */
2405 if (filt_attached
) {
2406 iflt_detach(bif
->bif_iff_ref
);
2408 _FREE(bif
, M_DEVBUF
);
2411 ifs
->if_bridge
= NULL
;
2418 * bridge_delete_span:
2420 * Delete the specified span interface.
2423 bridge_delete_span(struct bridge_softc
*sc
, struct bridge_iflist
*bif
)
2425 BRIDGE_LOCK_ASSERT_HELD(sc
);
2427 KASSERT(bif
->bif_ifp
->if_bridge
== NULL
,
2428 ("%s: not a span interface", __func__
));
2430 ifnet_release(bif
->bif_ifp
);
2432 TAILQ_REMOVE(&sc
->sc_spanlist
, bif
, bif_next
);
2433 _FREE(bif
, M_DEVBUF
);
2437 bridge_ioctl_add(struct bridge_softc
*sc
, void *arg
)
2439 struct ifbreq
*req
= arg
;
2440 struct bridge_iflist
*bif
= NULL
;
2441 struct ifnet
*ifs
, *bifp
= sc
->sc_ifp
;
2442 int error
= 0, lladdr_changed
= 0;
2443 uint8_t eaddr
[ETHER_ADDR_LEN
];
2444 struct iff_filter iff
;
2445 u_int32_t event_code
= 0;
2446 boolean_t mac_nat
= FALSE
;
2448 ifs
= ifunit(req
->ifbr_ifsname
);
2452 if (ifs
->if_ioctl
== NULL
) { /* must be supported */
2456 if (IFNET_IS_INTCOPROC(ifs
)) {
2460 /* If it's in the span list, it can't be a member. */
2461 TAILQ_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
) {
2462 if (ifs
== bif
->bif_ifp
) {
2467 if (ifs
->if_bridge
== sc
) {
2471 if (ifs
->if_bridge
!= NULL
) {
2475 switch (ifs
->if_type
) {
2477 if (strcmp(ifs
->if_name
, "en") == 0 &&
2478 ifs
->if_subfamily
== IFNET_SUBFAMILY_WIFI
) {
2479 /* XXX is there a better way to identify Wi-Fi STA? */
2483 /* permitted interface types */
2486 /* currently not supported */
2492 /* fail to add the interface if the MTU doesn't match */
2493 if (!TAILQ_EMPTY(&sc
->sc_iflist
) && sc
->sc_ifp
->if_mtu
!= ifs
->if_mtu
) {
2494 printf("%s: %s: invalid MTU for %s", __func__
,
2495 sc
->sc_ifp
->if_xname
,
2500 /* there's already an interface that's doing MAC NAT */
2501 if (mac_nat
&& sc
->sc_mac_nat_bif
!= NULL
) {
2504 bif
= _MALLOC(sizeof(*bif
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
2509 ifnet_reference(ifs
);
2510 bif
->bif_ifflags
|= IFBIF_LEARNING
| IFBIF_DISCOVER
;
2512 bif
->bif_savedcaps
= ifs
->if_capenable
;
2513 #endif /* HAS_IF_CAP */
2516 (void)bridge_mac_nat_enable(sc
, bif
);
2519 /* Allow the first Ethernet member to define the MTU */
2520 if (TAILQ_EMPTY(&sc
->sc_iflist
)) {
2521 sc
->sc_ifp
->if_mtu
= ifs
->if_mtu
;
2525 * Assign the interface's MAC address to the bridge if it's the first
2526 * member and the MAC address of the bridge has not been changed from
2527 * the default (randomly) generated one.
2529 if (bridge_inherit_mac
&& TAILQ_EMPTY(&sc
->sc_iflist
) &&
2530 !memcmp(IF_LLADDR(sc
->sc_ifp
), sc
->sc_defaddr
, ETHER_ADDR_LEN
)) {
2531 bcopy(IF_LLADDR(ifs
), eaddr
, ETHER_ADDR_LEN
);
2532 sc
->sc_ifaddr
= ifs
;
2533 ifnet_reference(ifs
); /* for sc_ifaddr */
2537 ifs
->if_bridge
= sc
;
2539 bstp_create(&sc
->sc_stp
, &bif
->bif_stp
, bif
->bif_ifp
);
2540 #endif /* BRIDGESTP */
2543 * XXX: XLOCK HERE!?!
2545 TAILQ_INSERT_TAIL(&sc
->sc_iflist
, bif
, bif_next
);
2548 /* Set interface capabilities to the intersection set of all members */
2549 bridge_mutecaps(sc
);
2550 #endif /* HAS_IF_CAP */
2556 * Place the interface into promiscuous mode.
2558 switch (ifs
->if_type
) {
2561 error
= ifnet_set_promiscuous(ifs
, 1);
2563 /* Ignore error when device is not up */
2564 if (error
!= ENETDOWN
) {
2569 bif
->bif_flags
|= BIFF_PROMISC
;
2578 * The new member may change the link status of the bridge interface
2580 if (interface_media_active(ifs
)) {
2581 bif
->bif_flags
|= BIFF_MEDIA_ACTIVE
;
2583 bif
->bif_flags
&= ~BIFF_MEDIA_ACTIVE
;
2586 event_code
= bridge_updatelinkstatus(sc
);
2589 * Respect lock ordering with DLIL lock for the following operations
2595 * install an interface filter
2597 memset(&iff
, 0, sizeof(struct iff_filter
));
2598 iff
.iff_cookie
= bif
;
2599 iff
.iff_name
= "com.apple.kernel.bsd.net.if_bridge";
2600 iff
.iff_input
= bridge_iff_input
;
2601 iff
.iff_output
= bridge_iff_output
;
2602 iff
.iff_event
= bridge_iff_event
;
2603 iff
.iff_detached
= bridge_iff_detached
;
2604 error
= dlil_attach_filter(ifs
, &iff
, &bif
->bif_iff_ref
,
2605 DLIL_IFF_TSO
| DLIL_IFF_INTERNAL
);
2607 printf("%s: iflt_attach failed %d\n", __func__
, error
);
2612 bif
->bif_flags
|= BIFF_FILTER_ATTACHED
;
2616 * install a dummy "bridge" protocol
2618 if ((error
= bridge_attach_protocol(ifs
)) != 0) {
2620 printf("%s: bridge_attach_protocol failed %d\n",
2627 bif
->bif_flags
|= BIFF_PROTO_ATTACHED
;
2630 if (lladdr_changed
&&
2631 (error
= ifnet_set_lladdr(bifp
, eaddr
, ETHER_ADDR_LEN
)) != 0) {
2632 printf("%s: ifnet_set_lladdr failed %d\n", __func__
, error
);
2635 if (event_code
!= 0) {
2636 bridge_link_event(bifp
, event_code
);
2642 if (error
&& bif
!= NULL
) {
2643 bridge_delete_member(sc
, bif
, 1);
2650 bridge_ioctl_del(struct bridge_softc
*sc
, void *arg
)
2652 struct ifbreq
*req
= arg
;
2653 struct bridge_iflist
*bif
;
2655 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
2660 bridge_delete_member(sc
, bif
, 0);
2666 bridge_ioctl_purge(struct bridge_softc
*sc
, void *arg
)
2668 #pragma unused(sc, arg)
2673 bridge_ioctl_gifflags(struct bridge_softc
*sc
, void *arg
)
2675 struct ifbreq
*req
= arg
;
2676 struct bridge_iflist
*bif
;
2678 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
2683 struct bstp_port
*bp
;
2686 req
->ifbr_state
= bp
->bp_state
;
2687 req
->ifbr_priority
= bp
->bp_priority
;
2688 req
->ifbr_path_cost
= bp
->bp_path_cost
;
2689 req
->ifbr_proto
= bp
->bp_protover
;
2690 req
->ifbr_role
= bp
->bp_role
;
2691 req
->ifbr_stpflags
= bp
->bp_flags
;
2692 req
->ifbr_ifsflags
= bif
->bif_ifflags
;
2694 /* Copy STP state options as flags */
2695 if (bp
->bp_operedge
) {
2696 req
->ifbr_ifsflags
|= IFBIF_BSTP_EDGE
;
2698 if (bp
->bp_flags
& BSTP_PORT_AUTOEDGE
) {
2699 req
->ifbr_ifsflags
|= IFBIF_BSTP_AUTOEDGE
;
2701 if (bp
->bp_ptp_link
) {
2702 req
->ifbr_ifsflags
|= IFBIF_BSTP_PTP
;
2704 if (bp
->bp_flags
& BSTP_PORT_AUTOPTP
) {
2705 req
->ifbr_ifsflags
|= IFBIF_BSTP_AUTOPTP
;
2707 if (bp
->bp_flags
& BSTP_PORT_ADMEDGE
) {
2708 req
->ifbr_ifsflags
|= IFBIF_BSTP_ADMEDGE
;
2710 if (bp
->bp_flags
& BSTP_PORT_ADMCOST
) {
2711 req
->ifbr_ifsflags
|= IFBIF_BSTP_ADMCOST
;
2714 req
->ifbr_portno
= bif
->bif_ifp
->if_index
& 0xfff;
2715 req
->ifbr_addrcnt
= bif
->bif_addrcnt
;
2716 req
->ifbr_addrmax
= bif
->bif_addrmax
;
2717 req
->ifbr_addrexceeded
= bif
->bif_addrexceeded
;
2723 bridge_ioctl_sifflags(struct bridge_softc
*sc
, void *arg
)
2725 struct ifbreq
*req
= arg
;
2726 struct bridge_iflist
*bif
;
2728 struct bstp_port
*bp
;
2730 #endif /* BRIDGESTP */
2732 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
2737 if (req
->ifbr_ifsflags
& IFBIF_SPAN
) {
2738 /* SPAN is readonly */
2741 if ((req
->ifbr_ifsflags
& IFBIF_MAC_NAT
) != 0) {
2743 error
= bridge_mac_nat_enable(sc
, bif
);
2747 } else if (sc
->sc_mac_nat_bif
!= NULL
) {
2748 bridge_mac_nat_disable(sc
);
2753 if (req
->ifbr_ifsflags
& IFBIF_STP
) {
2754 if ((bif
->bif_ifflags
& IFBIF_STP
) == 0) {
2755 error
= bstp_enable(&bif
->bif_stp
);
2761 if ((bif
->bif_ifflags
& IFBIF_STP
) != 0) {
2762 bstp_disable(&bif
->bif_stp
);
2766 /* Pass on STP flags */
2768 bstp_set_edge(bp
, req
->ifbr_ifsflags
& IFBIF_BSTP_EDGE
? 1 : 0);
2769 bstp_set_autoedge(bp
, req
->ifbr_ifsflags
& IFBIF_BSTP_AUTOEDGE
? 1 : 0);
2770 bstp_set_ptp(bp
, req
->ifbr_ifsflags
& IFBIF_BSTP_PTP
? 1 : 0);
2771 bstp_set_autoptp(bp
, req
->ifbr_ifsflags
& IFBIF_BSTP_AUTOPTP
? 1 : 0);
2772 #else /* !BRIDGESTP */
2773 if (req
->ifbr_ifsflags
& IFBIF_STP
) {
2776 #endif /* !BRIDGESTP */
2778 /* Save the bits relating to the bridge */
2779 bif
->bif_ifflags
= req
->ifbr_ifsflags
& IFBIFMASK
;
2786 bridge_ioctl_scache(struct bridge_softc
*sc
, void *arg
)
2788 struct ifbrparam
*param
= arg
;
2790 sc
->sc_brtmax
= param
->ifbrp_csize
;
2796 bridge_ioctl_gcache(struct bridge_softc
*sc
, void *arg
)
2798 struct ifbrparam
*param
= arg
;
2800 param
->ifbrp_csize
= sc
->sc_brtmax
;
2805 #define BRIDGE_IOCTL_GIFS do { \
2806 struct bridge_iflist *bif; \
2807 struct ifbreq breq; \
2808 char *buf, *outbuf; \
2809 unsigned int count, buflen, len; \
2812 TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) \
2814 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) \
2817 buflen = sizeof (breq) * count; \
2818 if (bifc->ifbic_len == 0) { \
2819 bifc->ifbic_len = buflen; \
2822 BRIDGE_UNLOCK(sc); \
2823 outbuf = _MALLOC(buflen, M_TEMP, M_WAITOK | M_ZERO); \
2828 len = min(bifc->ifbic_len, buflen); \
2829 bzero(&breq, sizeof (breq)); \
2830 TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { \
2831 if (len < sizeof (breq)) \
2834 snprintf(breq.ifbr_ifsname, sizeof (breq.ifbr_ifsname), \
2835 "%s", bif->bif_ifp->if_xname); \
2836 /* Fill in the ifbreq structure */ \
2837 error = bridge_ioctl_gifflags(sc, &breq); \
2840 memcpy(buf, &breq, sizeof (breq)); \
2842 buf += sizeof (breq); \
2843 len -= sizeof (breq); \
2845 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) { \
2846 if (len < sizeof (breq)) \
2849 snprintf(breq.ifbr_ifsname, \
2850 sizeof (breq.ifbr_ifsname), \
2851 "%s", bif->bif_ifp->if_xname); \
2852 breq.ifbr_ifsflags = bif->bif_ifflags; \
2854 = bif->bif_ifp->if_index & 0xfff; \
2855 memcpy(buf, &breq, sizeof (breq)); \
2857 buf += sizeof (breq); \
2858 len -= sizeof (breq); \
2861 BRIDGE_UNLOCK(sc); \
2862 bifc->ifbic_len = sizeof (breq) * count; \
2863 error = copyout(outbuf, bifc->ifbic_req, bifc->ifbic_len); \
2865 _FREE(outbuf, M_TEMP); \
2869 bridge_ioctl_gifs64(struct bridge_softc
*sc
, void *arg
)
2871 struct ifbifconf64
*bifc
= arg
;
2880 bridge_ioctl_gifs32(struct bridge_softc
*sc
, void *arg
)
2882 struct ifbifconf32
*bifc
= arg
;
2890 #define BRIDGE_IOCTL_RTS do { \
2891 struct bridge_rtnode *brt; \
2893 char *outbuf = NULL; \
2894 unsigned int count, buflen, len; \
2895 unsigned long now; \
2897 if (bac->ifbac_len == 0) \
2900 bzero(&bareq, sizeof (bareq)); \
2902 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) \
2904 buflen = sizeof (bareq) * count; \
2906 BRIDGE_UNLOCK(sc); \
2907 outbuf = _MALLOC(buflen, M_TEMP, M_WAITOK | M_ZERO); \
2912 len = min(bac->ifbac_len, buflen); \
2913 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) { \
2914 if (len < sizeof (bareq)) \
2916 snprintf(bareq.ifba_ifsname, sizeof (bareq.ifba_ifsname), \
2917 "%s", brt->brt_ifp->if_xname); \
2918 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof (brt->brt_addr)); \
2919 bareq.ifba_vlan = brt->brt_vlan; \
2920 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { \
2921 now = (unsigned long) net_uptime(); \
2922 if (now < brt->brt_expire) \
2923 bareq.ifba_expire = \
2924 brt->brt_expire - now; \
2926 bareq.ifba_expire = 0; \
2927 bareq.ifba_flags = brt->brt_flags; \
2929 memcpy(buf, &bareq, sizeof (bareq)); \
2931 buf += sizeof (bareq); \
2932 len -= sizeof (bareq); \
2935 bac->ifbac_len = sizeof (bareq) * count; \
2936 if (outbuf != NULL) { \
2937 BRIDGE_UNLOCK(sc); \
2938 error = copyout(outbuf, bac->ifbac_req, bac->ifbac_len); \
2939 _FREE(outbuf, M_TEMP); \
2946 bridge_ioctl_rts64(struct bridge_softc
*sc
, void *arg
)
2948 struct ifbaconf64
*bac
= arg
;
2949 struct ifbareq64 bareq
;
2957 bridge_ioctl_rts32(struct bridge_softc
*sc
, void *arg
)
2959 struct ifbaconf32
*bac
= arg
;
2960 struct ifbareq32 bareq
;
2968 bridge_ioctl_saddr32(struct bridge_softc
*sc
, void *arg
)
2970 struct ifbareq32
*req
= arg
;
2971 struct bridge_iflist
*bif
;
2974 bif
= bridge_lookup_member(sc
, req
->ifba_ifsname
);
2979 error
= bridge_rtupdate(sc
, req
->ifba_dst
, req
->ifba_vlan
, bif
, 1,
2986 bridge_ioctl_saddr64(struct bridge_softc
*sc
, void *arg
)
2988 struct ifbareq64
*req
= arg
;
2989 struct bridge_iflist
*bif
;
2992 bif
= bridge_lookup_member(sc
, req
->ifba_ifsname
);
2997 error
= bridge_rtupdate(sc
, req
->ifba_dst
, req
->ifba_vlan
, bif
, 1,
3004 bridge_ioctl_sto(struct bridge_softc
*sc
, void *arg
)
3006 struct ifbrparam
*param
= arg
;
3008 sc
->sc_brttimeout
= param
->ifbrp_ctime
;
3013 bridge_ioctl_gto(struct bridge_softc
*sc
, void *arg
)
3015 struct ifbrparam
*param
= arg
;
3017 param
->ifbrp_ctime
= sc
->sc_brttimeout
;
3022 bridge_ioctl_daddr32(struct bridge_softc
*sc
, void *arg
)
3024 struct ifbareq32
*req
= arg
;
3026 return bridge_rtdaddr(sc
, req
->ifba_dst
, req
->ifba_vlan
);
3030 bridge_ioctl_daddr64(struct bridge_softc
*sc
, void *arg
)
3032 struct ifbareq64
*req
= arg
;
3034 return bridge_rtdaddr(sc
, req
->ifba_dst
, req
->ifba_vlan
);
3038 bridge_ioctl_flush(struct bridge_softc
*sc
, void *arg
)
3040 struct ifbreq
*req
= arg
;
3042 bridge_rtflush(sc
, req
->ifbr_ifsflags
);
3047 bridge_ioctl_gpri(struct bridge_softc
*sc
, void *arg
)
3049 struct ifbrparam
*param
= arg
;
3050 struct bstp_state
*bs
= &sc
->sc_stp
;
3052 param
->ifbrp_prio
= bs
->bs_bridge_priority
;
3057 bridge_ioctl_spri(struct bridge_softc
*sc
, void *arg
)
3060 struct ifbrparam
*param
= arg
;
3062 return bstp_set_priority(&sc
->sc_stp
, param
->ifbrp_prio
);
3063 #else /* !BRIDGESTP */
3064 #pragma unused(sc, arg)
3066 #endif /* !BRIDGESTP */
3070 bridge_ioctl_ght(struct bridge_softc
*sc
, void *arg
)
3072 struct ifbrparam
*param
= arg
;
3073 struct bstp_state
*bs
= &sc
->sc_stp
;
3075 param
->ifbrp_hellotime
= bs
->bs_bridge_htime
>> 8;
3080 bridge_ioctl_sht(struct bridge_softc
*sc
, void *arg
)
3083 struct ifbrparam
*param
= arg
;
3085 return bstp_set_htime(&sc
->sc_stp
, param
->ifbrp_hellotime
);
3086 #else /* !BRIDGESTP */
3087 #pragma unused(sc, arg)
3089 #endif /* !BRIDGESTP */
3093 bridge_ioctl_gfd(struct bridge_softc
*sc
, void *arg
)
3095 struct ifbrparam
*param
;
3096 struct bstp_state
*bs
;
3100 param
->ifbrp_fwddelay
= bs
->bs_bridge_fdelay
>> 8;
3105 bridge_ioctl_sfd(struct bridge_softc
*sc
, void *arg
)
3108 struct ifbrparam
*param
= arg
;
3110 return bstp_set_fdelay(&sc
->sc_stp
, param
->ifbrp_fwddelay
);
3111 #else /* !BRIDGESTP */
3112 #pragma unused(sc, arg)
3114 #endif /* !BRIDGESTP */
3118 bridge_ioctl_gma(struct bridge_softc
*sc
, void *arg
)
3120 struct ifbrparam
*param
;
3121 struct bstp_state
*bs
;
3125 param
->ifbrp_maxage
= bs
->bs_bridge_max_age
>> 8;
3130 bridge_ioctl_sma(struct bridge_softc
*sc
, void *arg
)
3133 struct ifbrparam
*param
= arg
;
3135 return bstp_set_maxage(&sc
->sc_stp
, param
->ifbrp_maxage
);
3136 #else /* !BRIDGESTP */
3137 #pragma unused(sc, arg)
3139 #endif /* !BRIDGESTP */
3143 bridge_ioctl_sifprio(struct bridge_softc
*sc
, void *arg
)
3146 struct ifbreq
*req
= arg
;
3147 struct bridge_iflist
*bif
;
3149 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
3154 return bstp_set_port_priority(&bif
->bif_stp
, req
->ifbr_priority
);
3155 #else /* !BRIDGESTP */
3156 #pragma unused(sc, arg)
3158 #endif /* !BRIDGESTP */
3162 bridge_ioctl_sifcost(struct bridge_softc
*sc
, void *arg
)
3165 struct ifbreq
*req
= arg
;
3166 struct bridge_iflist
*bif
;
3168 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
3173 return bstp_set_path_cost(&bif
->bif_stp
, req
->ifbr_path_cost
);
3174 #else /* !BRIDGESTP */
3175 #pragma unused(sc, arg)
3177 #endif /* !BRIDGESTP */
3181 bridge_ioctl_gfilt(struct bridge_softc
*sc
, void *arg
)
3183 struct ifbrparam
*param
= arg
;
3185 param
->ifbrp_filter
= sc
->sc_filter_flags
;
3191 bridge_ioctl_sfilt(struct bridge_softc
*sc
, void *arg
)
3193 struct ifbrparam
*param
= arg
;
3195 if (param
->ifbrp_filter
& ~IFBF_FILT_MASK
) {
3199 if (param
->ifbrp_filter
& IFBF_FILT_USEIPF
) {
3203 sc
->sc_filter_flags
= param
->ifbrp_filter
;
3209 bridge_ioctl_sifmaxaddr(struct bridge_softc
*sc
, void *arg
)
3211 struct ifbreq
*req
= arg
;
3212 struct bridge_iflist
*bif
;
3214 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
3219 bif
->bif_addrmax
= req
->ifbr_addrmax
;
3224 bridge_ioctl_addspan(struct bridge_softc
*sc
, void *arg
)
3226 struct ifbreq
*req
= arg
;
3227 struct bridge_iflist
*bif
= NULL
;
3230 ifs
= ifunit(req
->ifbr_ifsname
);
3235 if (IFNET_IS_INTCOPROC(ifs
)) {
3239 TAILQ_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
3240 if (ifs
== bif
->bif_ifp
) {
3244 if (ifs
->if_bridge
!= NULL
) {
3248 switch (ifs
->if_type
) {
3253 /* currently not supported */
3259 bif
= _MALLOC(sizeof(*bif
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
3265 bif
->bif_ifflags
= IFBIF_SPAN
;
3267 ifnet_reference(bif
->bif_ifp
);
3269 TAILQ_INSERT_HEAD(&sc
->sc_spanlist
, bif
, bif_next
);
3275 bridge_ioctl_delspan(struct bridge_softc
*sc
, void *arg
)
3277 struct ifbreq
*req
= arg
;
3278 struct bridge_iflist
*bif
;
3281 ifs
= ifunit(req
->ifbr_ifsname
);
3286 TAILQ_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
3287 if (ifs
== bif
->bif_ifp
) {
3295 bridge_delete_span(sc
, bif
);
3300 #define BRIDGE_IOCTL_GBPARAM do { \
3301 struct bstp_state *bs = &sc->sc_stp; \
3302 struct bstp_port *root_port; \
3304 req->ifbop_maxage = bs->bs_bridge_max_age >> 8; \
3305 req->ifbop_hellotime = bs->bs_bridge_htime >> 8; \
3306 req->ifbop_fwddelay = bs->bs_bridge_fdelay >> 8; \
3308 root_port = bs->bs_root_port; \
3309 if (root_port == NULL) \
3310 req->ifbop_root_port = 0; \
3312 req->ifbop_root_port = root_port->bp_ifp->if_index; \
3314 req->ifbop_holdcount = bs->bs_txholdcount; \
3315 req->ifbop_priority = bs->bs_bridge_priority; \
3316 req->ifbop_protocol = bs->bs_protover; \
3317 req->ifbop_root_path_cost = bs->bs_root_pv.pv_cost; \
3318 req->ifbop_bridgeid = bs->bs_bridge_pv.pv_dbridge_id; \
3319 req->ifbop_designated_root = bs->bs_root_pv.pv_root_id; \
3320 req->ifbop_designated_bridge = bs->bs_root_pv.pv_dbridge_id; \
3321 req->ifbop_last_tc_time.tv_sec = bs->bs_last_tc_time.tv_sec; \
3322 req->ifbop_last_tc_time.tv_usec = bs->bs_last_tc_time.tv_usec; \
3326 bridge_ioctl_gbparam32(struct bridge_softc
*sc
, void *arg
)
3328 struct ifbropreq32
*req
= arg
;
3330 BRIDGE_IOCTL_GBPARAM
;
3335 bridge_ioctl_gbparam64(struct bridge_softc
*sc
, void *arg
)
3337 struct ifbropreq64
*req
= arg
;
3339 BRIDGE_IOCTL_GBPARAM
;
3344 bridge_ioctl_grte(struct bridge_softc
*sc
, void *arg
)
3346 struct ifbrparam
*param
= arg
;
3348 param
->ifbrp_cexceeded
= sc
->sc_brtexceeded
;
3352 #define BRIDGE_IOCTL_GIFSSTP do { \
3353 struct bridge_iflist *bif; \
3354 struct bstp_port *bp; \
3355 struct ifbpstpreq bpreq; \
3356 char *buf, *outbuf; \
3357 unsigned int count, buflen, len; \
3360 TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { \
3361 if ((bif->bif_ifflags & IFBIF_STP) != 0) \
3365 buflen = sizeof (bpreq) * count; \
3366 if (bifstp->ifbpstp_len == 0) { \
3367 bifstp->ifbpstp_len = buflen; \
3371 BRIDGE_UNLOCK(sc); \
3372 outbuf = _MALLOC(buflen, M_TEMP, M_WAITOK | M_ZERO); \
3377 len = min(bifstp->ifbpstp_len, buflen); \
3378 bzero(&bpreq, sizeof (bpreq)); \
3379 TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { \
3380 if (len < sizeof (bpreq)) \
3383 if ((bif->bif_ifflags & IFBIF_STP) == 0) \
3386 bp = &bif->bif_stp; \
3387 bpreq.ifbp_portno = bif->bif_ifp->if_index & 0xfff; \
3388 bpreq.ifbp_fwd_trans = bp->bp_forward_transitions; \
3389 bpreq.ifbp_design_cost = bp->bp_desg_pv.pv_cost; \
3390 bpreq.ifbp_design_port = bp->bp_desg_pv.pv_port_id; \
3391 bpreq.ifbp_design_bridge = bp->bp_desg_pv.pv_dbridge_id; \
3392 bpreq.ifbp_design_root = bp->bp_desg_pv.pv_root_id; \
3394 memcpy(buf, &bpreq, sizeof (bpreq)); \
3396 buf += sizeof (bpreq); \
3397 len -= sizeof (bpreq); \
3400 BRIDGE_UNLOCK(sc); \
3401 bifstp->ifbpstp_len = sizeof (bpreq) * count; \
3402 error = copyout(outbuf, bifstp->ifbpstp_req, bifstp->ifbpstp_len); \
3404 _FREE(outbuf, M_TEMP); \
3409 bridge_ioctl_gifsstp32(struct bridge_softc
*sc
, void *arg
)
3411 struct ifbpstpconf32
*bifstp
= arg
;
3414 BRIDGE_IOCTL_GIFSSTP
;
3419 bridge_ioctl_gifsstp64(struct bridge_softc
*sc
, void *arg
)
3421 struct ifbpstpconf64
*bifstp
= arg
;
3424 BRIDGE_IOCTL_GIFSSTP
;
3429 bridge_ioctl_sproto(struct bridge_softc
*sc
, void *arg
)
3432 struct ifbrparam
*param
= arg
;
3434 return bstp_set_protocol(&sc
->sc_stp
, param
->ifbrp_proto
);
3435 #else /* !BRIDGESTP */
3436 #pragma unused(sc, arg)
3438 #endif /* !BRIDGESTP */
3442 bridge_ioctl_stxhc(struct bridge_softc
*sc
, void *arg
)
3445 struct ifbrparam
*param
= arg
;
3447 return bstp_set_holdcount(&sc
->sc_stp
, param
->ifbrp_txhc
);
3448 #else /* !BRIDGESTP */
3449 #pragma unused(sc, arg)
3451 #endif /* !BRIDGESTP */
3456 bridge_ioctl_ghostfilter(struct bridge_softc
*sc
, void *arg
)
3458 struct ifbrhostfilter
*req
= arg
;
3459 struct bridge_iflist
*bif
;
3461 bif
= bridge_lookup_member(sc
, req
->ifbrhf_ifsname
);
3466 bzero(req
, sizeof(struct ifbrhostfilter
));
3467 if (bif
->bif_flags
& BIFF_HOST_FILTER
) {
3468 req
->ifbrhf_flags
|= IFBRHF_ENABLED
;
3469 bcopy(bif
->bif_hf_hwsrc
, req
->ifbrhf_hwsrca
,
3471 req
->ifbrhf_ipsrc
= bif
->bif_hf_ipsrc
.s_addr
;
3477 bridge_ioctl_shostfilter(struct bridge_softc
*sc
, void *arg
)
3479 struct ifbrhostfilter
*req
= arg
;
3480 struct bridge_iflist
*bif
;
3482 bif
= bridge_lookup_member(sc
, req
->ifbrhf_ifsname
);
3487 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_vmnet_total
);
3489 if (req
->ifbrhf_flags
& IFBRHF_ENABLED
) {
3490 bif
->bif_flags
|= BIFF_HOST_FILTER
;
3492 if (req
->ifbrhf_flags
& IFBRHF_HWSRC
) {
3493 bcopy(req
->ifbrhf_hwsrca
, bif
->bif_hf_hwsrc
,
3495 if (bcmp(req
->ifbrhf_hwsrca
, ethernulladdr
,
3496 ETHER_ADDR_LEN
) != 0) {
3497 bif
->bif_flags
|= BIFF_HF_HWSRC
;
3499 bif
->bif_flags
&= ~BIFF_HF_HWSRC
;
3502 if (req
->ifbrhf_flags
& IFBRHF_IPSRC
) {
3503 bif
->bif_hf_ipsrc
.s_addr
= req
->ifbrhf_ipsrc
;
3504 if (bif
->bif_hf_ipsrc
.s_addr
!= INADDR_ANY
) {
3505 bif
->bif_flags
|= BIFF_HF_IPSRC
;
3507 bif
->bif_flags
&= ~BIFF_HF_IPSRC
;
3511 bif
->bif_flags
&= ~(BIFF_HOST_FILTER
| BIFF_HF_HWSRC
|
3513 bzero(bif
->bif_hf_hwsrc
, ETHER_ADDR_LEN
);
3514 bif
->bif_hf_ipsrc
.s_addr
= INADDR_ANY
;
3521 bridge_mac_nat_entry_out(struct mac_nat_entry_list
* list
,
3522 unsigned int * count_p
, char *buf
, unsigned int *len_p
)
3524 unsigned int count
= *count_p
;
3525 struct ifbrmne ifbmne
;
3526 unsigned int len
= *len_p
;
3527 struct mac_nat_entry
*mne
;
3530 bzero(&ifbmne
, sizeof(ifbmne
));
3531 LIST_FOREACH(mne
, list
, mne_list
) {
3532 if (len
< sizeof(ifbmne
)) {
3535 snprintf(ifbmne
.ifbmne_ifname
, sizeof(ifbmne
.ifbmne_ifname
),
3536 "%s", mne
->mne_bif
->bif_ifp
->if_xname
);
3537 memcpy(ifbmne
.ifbmne_mac
, mne
->mne_mac
,
3538 sizeof(ifbmne
.ifbmne_mac
));
3539 now
= (unsigned long) net_uptime();
3540 if (now
< mne
->mne_expire
) {
3541 ifbmne
.ifbmne_expire
= mne
->mne_expire
- now
;
3543 ifbmne
.ifbmne_expire
= 0;
3545 if ((mne
->mne_flags
& MNE_FLAGS_IPV6
) != 0) {
3546 ifbmne
.ifbmne_af
= AF_INET6
;
3547 ifbmne
.ifbmne_ip6_addr
= mne
->mne_ip6
;
3549 ifbmne
.ifbmne_af
= AF_INET
;
3550 ifbmne
.ifbmne_ip_addr
= mne
->mne_ip
;
3552 memcpy(buf
, &ifbmne
, sizeof(ifbmne
));
3554 buf
+= sizeof(ifbmne
);
3555 len
-= sizeof(ifbmne
);
3563 * bridge_ioctl_gmnelist()
3564 * Perform the get mac_nat_entry list ioctl.
3567 * The struct ifbrmnelist32 and struct ifbrmnelist64 have the same
3568 * field size/layout except for the last field ifbml_buf, the user-supplied
3569 * buffer pointer. That is passed in separately via the 'user_addr'
3570 * parameter from the respective 32-bit or 64-bit ioctl routine.
3573 bridge_ioctl_gmnelist(struct bridge_softc
*sc
, struct ifbrmnelist32
*mnl
,
3574 user_addr_t user_addr
)
3579 char *outbuf
= NULL
;
3580 struct mac_nat_entry
*mne
;
3581 unsigned int buflen
;
3584 mnl
->ifbml_elsize
= sizeof(struct ifbrmne
);
3586 LIST_FOREACH(mne
, &sc
->sc_mne_list
, mne_list
)
3588 LIST_FOREACH(mne
, &sc
->sc_mne_list_v6
, mne_list
)
3590 buflen
= sizeof(struct ifbrmne
) * count
;
3591 if (buflen
== 0 || mnl
->ifbml_len
== 0) {
3592 mnl
->ifbml_len
= buflen
;
3596 outbuf
= _MALLOC(buflen
, M_TEMP
, M_WAITOK
| M_ZERO
);
3600 len
= min(mnl
->ifbml_len
, buflen
);
3601 buf
= bridge_mac_nat_entry_out(&sc
->sc_mne_list
, &count
, buf
, &len
);
3602 buf
= bridge_mac_nat_entry_out(&sc
->sc_mne_list_v6
, &count
, buf
, &len
);
3603 mnl
->ifbml_len
= count
* sizeof(struct ifbrmne
);
3605 error
= copyout(outbuf
, user_addr
, mnl
->ifbml_len
);
3606 _FREE(outbuf
, M_TEMP
);
3612 bridge_ioctl_gmnelist64(struct bridge_softc
*sc
, void *arg
)
3614 struct ifbrmnelist64
*mnl
= arg
;
3616 return bridge_ioctl_gmnelist(sc
, arg
, mnl
->ifbml_buf
);
3620 bridge_ioctl_gmnelist32(struct bridge_softc
*sc
, void *arg
)
3622 struct ifbrmnelist32
*mnl
= arg
;
3624 return bridge_ioctl_gmnelist(sc
, arg
,
3625 CAST_USER_ADDR_T(mnl
->ifbml_buf
));
3631 * Detach an interface from a bridge. Called when a member
3632 * interface is detaching.
3635 bridge_ifdetach(struct ifnet
*ifp
)
3637 struct bridge_iflist
*bif
;
3638 struct bridge_softc
*sc
= ifp
->if_bridge
;
3641 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE
)) {
3642 printf("%s: %s\n", __func__
, ifp
->if_xname
);
3644 #endif /* BRIDGE_DEBUG */
3646 /* Check if the interface is a bridge member */
3649 bif
= bridge_lookup_member_if(sc
, ifp
);
3651 bridge_delete_member(sc
, bif
, 1);
3656 /* Check if the interface is a span port */
3657 lck_mtx_lock(&bridge_list_mtx
);
3658 LIST_FOREACH(sc
, &bridge_list
, sc_list
) {
3660 TAILQ_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
3661 if (ifp
== bif
->bif_ifp
) {
3662 bridge_delete_span(sc
, bif
);
3667 lck_mtx_unlock(&bridge_list_mtx
);
3671 * bridge_proto_attach_changed
3673 * Called when protocol attachment on the interface changes.
3676 bridge_proto_attach_changed(struct ifnet
*ifp
)
3678 boolean_t changed
= FALSE
;
3679 struct bridge_iflist
*bif
;
3680 boolean_t input_broadcast
;
3681 struct bridge_softc
*sc
= ifp
->if_bridge
;
3684 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE
)) {
3685 printf("%s: %s\n", __func__
, ifp
->if_xname
);
3687 #endif /* BRIDGE_DEBUG */
3692 * Selectively enable input broadcast only when necessary.
3693 * The bridge interface itself attaches a fake protocol
3694 * so checking for at least two protocols means that the
3695 * interface is being used for something besides bridging.
3697 input_broadcast
= if_get_protolist(ifp
, NULL
, 0) >= 2;
3699 bif
= bridge_lookup_member_if(sc
, ifp
);
3701 if (input_broadcast
) {
3702 if ((bif
->bif_flags
& BIFF_INPUT_BROADCAST
) == 0) {
3703 bif
->bif_flags
|= BIFF_INPUT_BROADCAST
;
3706 } else if ((bif
->bif_flags
& BIFF_INPUT_BROADCAST
) != 0) {
3708 bif
->bif_flags
&= ~BIFF_INPUT_BROADCAST
;
3713 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE
)) {
3714 printf("%s: input broadcast %s", ifp
->if_xname
,
3715 input_broadcast
? "ENABLED" : "DISABLED");
3717 #endif /* BRIDGE_DEBUG */
3722 * interface_media_active:
3724 * Tells if an interface media is active.
3727 interface_media_active(struct ifnet
*ifp
)
3729 struct ifmediareq ifmr
;
3732 bzero(&ifmr
, sizeof(ifmr
));
3733 if (ifnet_ioctl(ifp
, 0, SIOCGIFMEDIA
, &ifmr
) == 0) {
3734 if ((ifmr
.ifm_status
& IFM_AVALID
) && ifmr
.ifm_count
> 0) {
3735 status
= ifmr
.ifm_status
& IFM_ACTIVE
? 1 : 0;
3743 * bridge_updatelinkstatus:
3745 * Update the media active status of the bridge based on the
3746 * media active status of its member.
3747 * If changed, return the corresponding onf/off link event.
3750 bridge_updatelinkstatus(struct bridge_softc
*sc
)
3752 struct bridge_iflist
*bif
;
3753 int active_member
= 0;
3754 u_int32_t event_code
= 0;
3756 BRIDGE_LOCK_ASSERT_HELD(sc
);
3759 * Find out if we have an active interface
3761 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
3762 if (bif
->bif_flags
& BIFF_MEDIA_ACTIVE
) {
3768 if (active_member
&& !(sc
->sc_flags
& SCF_MEDIA_ACTIVE
)) {
3769 sc
->sc_flags
|= SCF_MEDIA_ACTIVE
;
3770 event_code
= KEV_DL_LINK_ON
;
3771 } else if (!active_member
&& (sc
->sc_flags
& SCF_MEDIA_ACTIVE
)) {
3772 sc
->sc_flags
&= ~SCF_MEDIA_ACTIVE
;
3773 event_code
= KEV_DL_LINK_OFF
;
3780 * bridge_iflinkevent:
3783 bridge_iflinkevent(struct ifnet
*ifp
)
3785 struct bridge_softc
*sc
= ifp
->if_bridge
;
3786 struct bridge_iflist
*bif
;
3787 u_int32_t event_code
= 0;
3790 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE
)) {
3791 printf("%s: %s\n", __func__
, ifp
->if_xname
);
3793 #endif /* BRIDGE_DEBUG */
3795 /* Check if the interface is a bridge member */
3801 bif
= bridge_lookup_member_if(sc
, ifp
);
3803 if (interface_media_active(ifp
)) {
3804 bif
->bif_flags
|= BIFF_MEDIA_ACTIVE
;
3806 bif
->bif_flags
&= ~BIFF_MEDIA_ACTIVE
;
3808 if (sc
->sc_mac_nat_bif
!= NULL
) {
3809 bridge_mac_nat_flush_entries(sc
, bif
);
3812 event_code
= bridge_updatelinkstatus(sc
);
3816 if (event_code
!= 0) {
3817 bridge_link_event(sc
->sc_ifp
, event_code
);
3822 * bridge_delayed_callback:
3824 * Makes a delayed call
3827 bridge_delayed_callback(void *param
)
3829 struct bridge_delayed_call
*call
= (struct bridge_delayed_call
*)param
;
3830 struct bridge_softc
*sc
= call
->bdc_sc
;
3832 #if BRIDGE_DEBUG_DELAYED_CALLBACK
3833 if (bridge_delayed_callback_delay
> 0) {
3836 ts
.tv_sec
= bridge_delayed_callback_delay
;
3839 printf("%s: sleeping for %d seconds\n",
3840 __func__
, bridge_delayed_callback_delay
);
3842 msleep(&bridge_delayed_callback_delay
, NULL
, PZERO
,
3845 printf("%s: awoken\n", __func__
);
3847 #endif /* BRIDGE_DEBUG_DELAYED_CALLBACK */
3851 #if BRIDGE_DEBUG_DELAYED_CALLBACK
3852 if (IF_BRIDGE_DEBUG(BR_DBGF_DELAYED_CALL
)) {
3853 printf("%s: %s call 0x%llx flags 0x%x\n", __func__
,
3854 sc
->sc_if_xname
, (uint64_t)VM_KERNEL_ADDRPERM(call
),
3857 #endif /* BRIDGE_DEBUG_DELAYED_CALLBACK */
3859 if (call
->bdc_flags
& BDCF_CANCELLING
) {
3862 if ((sc
->sc_flags
& SCF_DETACHING
) == 0) {
3863 (*call
->bdc_func
)(sc
);
3866 call
->bdc_flags
&= ~BDCF_OUTSTANDING
;
3871 * bridge_schedule_delayed_call:
3873 * Schedule a function to be called on a separate thread
3874 * The actual call may be scheduled to run at a given time or ASAP.
3877 bridge_schedule_delayed_call(struct bridge_delayed_call
*call
)
3879 uint64_t deadline
= 0;
3880 struct bridge_softc
*sc
= call
->bdc_sc
;
3882 BRIDGE_LOCK_ASSERT_HELD(sc
);
3884 if ((sc
->sc_flags
& SCF_DETACHING
) ||
3885 (call
->bdc_flags
& (BDCF_OUTSTANDING
| BDCF_CANCELLING
))) {
3889 if (call
->bdc_ts
.tv_sec
|| call
->bdc_ts
.tv_nsec
) {
3890 nanoseconds_to_absolutetime(
3891 (uint64_t)call
->bdc_ts
.tv_sec
* NSEC_PER_SEC
+
3892 call
->bdc_ts
.tv_nsec
, &deadline
);
3893 clock_absolutetime_interval_to_deadline(deadline
, &deadline
);
3896 call
->bdc_flags
= BDCF_OUTSTANDING
;
3898 #if BRIDGE_DEBUG_DELAYED_CALLBACK
3899 if (IF_BRIDGE_DEBUG(BR_DBGF_DELAYED_CALL
)) {
3900 printf("%s: %s call 0x%llx flags 0x%x\n", __func__
,
3901 sc
->sc_if_xname
, (uint64_t)VM_KERNEL_ADDRPERM(call
),
3904 #endif /* BRIDGE_DEBUG_DELAYED_CALLBACK */
3906 if (call
->bdc_ts
.tv_sec
|| call
->bdc_ts
.tv_nsec
) {
3907 thread_call_func_delayed(
3908 (thread_call_func_t
)bridge_delayed_callback
,
3911 if (call
->bdc_thread_call
== NULL
) {
3912 call
->bdc_thread_call
= thread_call_allocate(
3913 (thread_call_func_t
)bridge_delayed_callback
,
3916 thread_call_enter(call
->bdc_thread_call
);
3921 * bridge_cancel_delayed_call:
3923 * Cancel a queued or running delayed call.
3924 * If call is running, does not return until the call is done to
3925 * prevent race condition with the brigde interface getting destroyed
3928 bridge_cancel_delayed_call(struct bridge_delayed_call
*call
)
3931 struct bridge_softc
*sc
= call
->bdc_sc
;
3934 * The call was never scheduled
3940 BRIDGE_LOCK_ASSERT_HELD(sc
);
3942 call
->bdc_flags
|= BDCF_CANCELLING
;
3944 while (call
->bdc_flags
& BDCF_OUTSTANDING
) {
3946 if (IF_BRIDGE_DEBUG(BR_DBGF_DELAYED_CALL
)) {
3947 printf("%s: %s call 0x%llx flags 0x%x\n", __func__
,
3948 sc
->sc_if_xname
, (uint64_t)VM_KERNEL_ADDRPERM(call
),
3951 #endif /* BRIDGE_DEBUG */
3952 result
= thread_call_func_cancel(
3953 (thread_call_func_t
)bridge_delayed_callback
, call
, FALSE
);
3957 * We managed to dequeue the delayed call
3959 call
->bdc_flags
&= ~BDCF_OUTSTANDING
;
3962 * Wait for delayed call do be done running
3964 msleep(call
, &sc
->sc_mtx
, PZERO
, __func__
, NULL
);
3967 call
->bdc_flags
&= ~BDCF_CANCELLING
;
3971 * bridge_cleanup_delayed_call:
3973 * Dispose resource allocated for a delayed call
3974 * Assume the delayed call is not queued or running .
3977 bridge_cleanup_delayed_call(struct bridge_delayed_call
*call
)
3980 struct bridge_softc
*sc
= call
->bdc_sc
;
3983 * The call was never scheduled
3989 BRIDGE_LOCK_ASSERT_HELD(sc
);
3991 VERIFY((call
->bdc_flags
& BDCF_OUTSTANDING
) == 0);
3992 VERIFY((call
->bdc_flags
& BDCF_CANCELLING
) == 0);
3994 if (call
->bdc_thread_call
!= NULL
) {
3995 result
= thread_call_free(call
->bdc_thread_call
);
3996 if (result
== FALSE
) {
3997 panic("%s thread_call_free() failed for call %p",
4000 call
->bdc_thread_call
= NULL
;
4007 * Initialize a bridge interface.
4010 bridge_init(struct ifnet
*ifp
)
4012 struct bridge_softc
*sc
= (struct bridge_softc
*)ifp
->if_softc
;
4015 BRIDGE_LOCK_ASSERT_HELD(sc
);
4017 if ((ifnet_flags(ifp
) & IFF_RUNNING
)) {
4021 error
= ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
4024 * Calling bridge_aging_timer() is OK as there are no entries to
4025 * age so we're just going to arm the timer
4027 bridge_aging_timer(sc
);
4030 bstp_init(&sc
->sc_stp
); /* Initialize Spanning Tree */
4032 #endif /* BRIDGESTP */
4039 * Stop the bridge interface.
4042 bridge_ifstop(struct ifnet
*ifp
, int disable
)
4044 #pragma unused(disable)
4045 struct bridge_softc
*sc
= ifp
->if_softc
;
4047 BRIDGE_LOCK_ASSERT_HELD(sc
);
4049 if ((ifnet_flags(ifp
) & IFF_RUNNING
) == 0) {
4053 bridge_cancel_delayed_call(&sc
->sc_aging_timer
);
4056 bstp_stop(&sc
->sc_stp
);
4057 #endif /* BRIDGESTP */
4059 bridge_rtflush(sc
, IFBF_FLUSHDYN
);
4060 (void) ifnet_set_flags(ifp
, 0, IFF_RUNNING
);
4064 * bridge_compute_cksum:
4066 * If the packet has checksum flags, compare the hardware checksum
4067 * capabilities of the source and destination interfaces. If they
4068 * are the same, there's nothing to do. If they are different,
4069 * finalize the checksum so that it can be sent on the destination
4073 bridge_compute_cksum(struct ifnet
*src_if
, struct ifnet
*dst_if
, struct mbuf
*m
)
4075 uint32_t csum_flags
;
4076 uint16_t dst_hw_csum
;
4078 struct ether_header
*eh
;
4079 uint16_t src_hw_csum
;
4081 csum_flags
= m
->m_pkthdr
.csum_flags
& IF_HWASSIST_CSUM_MASK
;
4082 if (csum_flags
== 0) {
4083 /* no checksum offload */
4088 * if destination/source differ in checksum offload
4089 * capabilities, finalize/compute the checksum
4091 dst_hw_csum
= IF_HWASSIST_CSUM_FLAGS(dst_if
->if_hwassist
);
4092 src_hw_csum
= IF_HWASSIST_CSUM_FLAGS(src_if
->if_hwassist
);
4093 if (dst_hw_csum
== src_hw_csum
) {
4096 eh
= mtod(m
, struct ether_header
*);
4097 switch (ntohs(eh
->ether_type
)) {
4099 did_sw
= in_finalize_cksum(m
, sizeof(*eh
), csum_flags
);
4102 case ETHERTYPE_IPV6
:
4103 did_sw
= in6_finalize_cksum(m
, sizeof(*eh
), -1, -1, csum_flags
);
4108 if (IF_BRIDGE_DEBUG(BR_DBGF_CHECKSUM
)) {
4109 printf("%s: [%s -> %s] before 0x%x did 0x%x after 0x%x\n",
4111 src_if
->if_xname
, dst_if
->if_xname
, csum_flags
, did_sw
,
4112 m
->m_pkthdr
.csum_flags
);
4114 #endif /* BRIDGE_DEBUG */
4120 * Enqueue a packet on a bridge member interface.
4124 bridge_enqueue(ifnet_t bridge_ifp
, struct ifnet
*src_ifp
,
4125 struct ifnet
*dst_ifp
, struct mbuf
*m
, ChecksumOperation cksum_op
)
4128 struct mbuf
*next_m
;
4130 VERIFY(dst_ifp
!= NULL
);
4133 * We may be sending a fragment so traverse the mbuf
4135 * NOTE: bridge_fragment() is called only when PFIL_HOOKS is enabled.
4137 for (; m
; m
= next_m
) {
4139 struct flowadv adv
= { .code
= FADV_SUCCESS
};
4141 next_m
= m
->m_nextpkt
;
4142 m
->m_nextpkt
= NULL
;
4144 len
= m
->m_pkthdr
.len
;
4145 m
->m_flags
|= M_PROTO1
; /* set to avoid loops */
4148 case kChecksumOperationClear
:
4149 m
->m_pkthdr
.csum_flags
= 0;
4151 case kChecksumOperationFinalize
:
4152 /* the checksum might not be correct, finalize now */
4153 bridge_finalize_cksum(dst_ifp
, m
);
4155 case kChecksumOperationCompute
:
4156 bridge_compute_cksum(src_ifp
, dst_ifp
, m
);
4163 * If underlying interface can not do VLAN tag insertion itself
4164 * then attach a packet tag that holds it.
4166 if ((m
->m_flags
& M_VLANTAG
) &&
4167 (dst_ifp
->if_capenable
& IFCAP_VLAN_HWTAGGING
) == 0) {
4168 m
= ether_vlanencap(m
, m
->m_pkthdr
.ether_vtag
);
4170 printf("%s: %s: unable to prepend VLAN "
4171 "header\n", __func__
, dst_ifp
->if_xname
);
4172 (void) ifnet_stat_increment_out(dst_ifp
,
4176 m
->m_flags
&= ~M_VLANTAG
;
4178 #endif /* HAS_IF_CAP */
4180 _error
= dlil_output(dst_ifp
, 0, m
, NULL
, NULL
, 1, &adv
);
4182 /* Preserve existing error value */
4186 } else if (adv
.code
== FADV_FLOW_CONTROLLED
) {
4188 } else if (adv
.code
== FADV_SUSPENDED
) {
4189 error
= EQSUSPENDED
;
4194 (void) ifnet_stat_increment_out(bridge_ifp
, 1, len
, 0);
4196 (void) ifnet_stat_increment_out(bridge_ifp
, 0, 0, 1);
4203 #if HAS_BRIDGE_DUMMYNET
4207 * Receive a queued packet from dummynet and pass it on to the output
4210 * The mbuf has the Ethernet header already attached.
4213 bridge_dummynet(struct mbuf
*m
, struct ifnet
*ifp
)
4215 struct bridge_softc
*sc
;
4217 sc
= ifp
->if_bridge
;
4220 * The packet didn't originate from a member interface. This should only
4221 * ever happen if a member interface is removed while packets are
4229 if (PFIL_HOOKED(&inet_pfil_hook
) || PFIL_HOOKED_INET6
) {
4230 if (bridge_pfil(&m
, sc
->sc_ifp
, ifp
, PFIL_OUT
) != 0) {
4237 (void) bridge_enqueue(sc
->sc_ifp
, NULL
, ifp
, m
, kChecksumOperationNone
);
4239 #endif /* HAS_BRIDGE_DUMMYNET */
4242 * bridge_member_output:
4244 * Send output from a bridge member interface. This
4245 * performs the bridging function for locally originated
4248 * The mbuf has the Ethernet header already attached.
4251 bridge_member_output(struct bridge_softc
*sc
, ifnet_t ifp
, mbuf_t
*data
)
4254 struct ether_header
*eh
;
4255 struct ifnet
*dst_if
;
4257 struct bridge_iflist
*mac_nat_bif
;
4258 ifnet_t mac_nat_ifp
;
4262 if (IF_BRIDGE_DEBUG(BR_DBGF_OUTPUT
)) {
4263 printf("%s: ifp %s\n", __func__
, ifp
->if_xname
);
4265 #endif /* BRIDGE_DEBUG */
4267 if (m
->m_len
< ETHER_HDR_LEN
) {
4268 m
= m_pullup(m
, ETHER_HDR_LEN
);
4275 eh
= mtod(m
, struct ether_header
*);
4276 vlan
= VLANTAGOF(m
);
4279 mac_nat_bif
= sc
->sc_mac_nat_bif
;
4280 mac_nat_ifp
= (mac_nat_bif
!= NULL
) ? mac_nat_bif
->bif_ifp
: NULL
;
4281 if (mac_nat_ifp
== ifp
) {
4282 /* record the IP address used by the MAC NAT interface */
4283 (void)bridge_mac_nat_output(sc
, mac_nat_bif
, data
, NULL
);
4286 /* packet was deallocated */
4291 bridge_ifp
= sc
->sc_ifp
;
4294 * APPLE MODIFICATION
4295 * If the packet is an 802.1X ethertype, then only send on the
4296 * original output interface.
4298 if (eh
->ether_type
== htons(ETHERTYPE_PAE
)) {
4304 * If bridge is down, but the original output interface is up,
4305 * go ahead and send out that interface. Otherwise, the packet
4308 if ((bridge_ifp
->if_flags
& IFF_RUNNING
) == 0) {
4314 * If the packet is a multicast, or we don't know a better way to
4315 * get there, send to all interfaces.
4317 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
4320 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
, vlan
);
4322 if (dst_if
== NULL
) {
4323 struct bridge_iflist
*bif
;
4331 BRIDGE_LOCK2REF(sc
, error
);
4337 TAILQ_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
4338 /* skip interface with inactive link status */
4339 if ((bif
->bif_flags
& BIFF_MEDIA_ACTIVE
) == 0) {
4342 dst_if
= bif
->bif_ifp
;
4344 if (dst_if
->if_type
== IFT_GIF
) {
4347 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0) {
4350 if (dst_if
!= ifp
) {
4352 * If this is not the original output interface,
4353 * and the interface is participating in spanning
4354 * tree, make sure the port is in a state that
4355 * allows forwarding.
4357 if ((bif
->bif_ifflags
& IFBIF_STP
) &&
4358 bif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
) {
4362 * If this is not the original output interface,
4363 * and the destination is the MAC NAT interface,
4364 * drop the packet. The packet can't be sent
4365 * if the source MAC is incorrect.
4367 if (dst_if
== mac_nat_ifp
) {
4371 if (TAILQ_NEXT(bif
, bif_next
) == NULL
) {
4375 mc
= m_dup(m
, M_DONTWAIT
);
4377 (void) ifnet_stat_increment_out(
4378 bridge_ifp
, 0, 0, 1);
4382 (void) bridge_enqueue(bridge_ifp
, ifp
, dst_if
,
4383 mc
, kChecksumOperationCompute
);
4394 * XXX Spanning tree consideration here?
4398 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0) {
4405 if (dst_if
== ifp
) {
4406 /* just let the packet continue on its way */
4409 if (dst_if
!= mac_nat_ifp
) {
4410 (void) bridge_enqueue(bridge_ifp
, ifp
, dst_if
, m
,
4411 kChecksumOperationCompute
);
4414 * This is not the original output interface
4415 * and the destination is the MAC NAT interface.
4416 * Drop the packet because the packet can't be sent
4417 * if the source MAC is incorrect.
4427 * This routine is called externally from above only when if_bridge_txstart
4428 * is disabled; otherwise it is called internally by bridge_start().
4431 bridge_output(struct ifnet
*ifp
, struct mbuf
*m
)
4433 struct bridge_softc
*sc
= ifnet_softc(ifp
);
4434 struct ether_header
*eh
;
4435 struct ifnet
*dst_if
= NULL
;
4438 eh
= mtod(m
, struct ether_header
*);
4442 if (!(m
->m_flags
& (M_BCAST
| M_MCAST
))) {
4443 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
, 0);
4446 (void) ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
4449 if (sc
->sc_bpf_output
) {
4450 bridge_bpf_output(ifp
, m
);
4454 if (dst_if
== NULL
) {
4455 /* callee will unlock */
4456 bridge_broadcast(sc
, NULL
, m
, 0);
4460 bridge_ifp
= sc
->sc_ifp
;
4462 error
= bridge_enqueue(bridge_ifp
, NULL
, dst_if
, m
,
4463 kChecksumOperationFinalize
);
4470 bridge_finalize_cksum(struct ifnet
*ifp
, struct mbuf
*m
)
4472 struct ether_header
*eh
= mtod(m
, struct ether_header
*);
4473 uint32_t sw_csum
, hwcap
;
4477 hwcap
= (ifp
->if_hwassist
| CSUM_DATA_VALID
);
4482 /* do in software what the hardware cannot */
4483 sw_csum
= m
->m_pkthdr
.csum_flags
& ~IF_HWASSIST_CSUM_FLAGS(hwcap
);
4484 sw_csum
&= IF_HWASSIST_CSUM_MASK
;
4486 switch (ntohs(eh
->ether_type
)) {
4488 if ((hwcap
& CSUM_PARTIAL
) && !(sw_csum
& CSUM_DELAY_DATA
) &&
4489 (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
)) {
4490 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP
) {
4492 sizeof(*eh
) + sizeof(struct ip
);
4494 m
->m_pkthdr
.csum_data
& 0xffff;
4495 m
->m_pkthdr
.csum_flags
|=
4496 (CSUM_DATA_VALID
| CSUM_PARTIAL
);
4497 m
->m_pkthdr
.csum_tx_stuff
= (ulpoff
+ start
);
4498 m
->m_pkthdr
.csum_tx_start
= start
;
4500 sw_csum
|= (CSUM_DELAY_DATA
&
4501 m
->m_pkthdr
.csum_flags
);
4504 (void) in_finalize_cksum(m
, sizeof(*eh
), sw_csum
);
4508 case ETHERTYPE_IPV6
:
4509 if ((hwcap
& CSUM_PARTIAL
) &&
4510 !(sw_csum
& CSUM_DELAY_IPV6_DATA
) &&
4511 (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
)) {
4512 if (m
->m_pkthdr
.csum_flags
& CSUM_TCPIPV6
) {
4514 sizeof(*eh
) + sizeof(struct ip6_hdr
);
4516 m
->m_pkthdr
.csum_data
& 0xffff;
4517 m
->m_pkthdr
.csum_flags
|=
4518 (CSUM_DATA_VALID
| CSUM_PARTIAL
);
4519 m
->m_pkthdr
.csum_tx_stuff
= (ulpoff
+ start
);
4520 m
->m_pkthdr
.csum_tx_start
= start
;
4522 sw_csum
|= (CSUM_DELAY_IPV6_DATA
&
4523 m
->m_pkthdr
.csum_flags
);
4526 (void) in6_finalize_cksum(m
, sizeof(*eh
), -1, -1, sw_csum
);
4535 * Start output on a bridge.
4537 * This routine is invoked by the start worker thread; because we never call
4538 * it directly, there is no need do deploy any serialization mechanism other
4539 * than what's already used by the worker thread, i.e. this is already single
4542 * This routine is called only when if_bridge_txstart is enabled.
4545 bridge_start(struct ifnet
*ifp
)
4550 if (ifnet_dequeue(ifp
, &m
) != 0) {
4554 (void) bridge_output(ifp
, m
);
4561 * The forwarding function of the bridge.
4563 * NOTE: Releases the lock on return.
4566 bridge_forward(struct bridge_softc
*sc
, struct bridge_iflist
*sbif
,
4569 struct bridge_iflist
*dbif
;
4571 struct ifnet
*src_if
, *dst_if
;
4572 struct ether_header
*eh
;
4576 struct mac_nat_record mnr
;
4577 boolean_t translate_mac
= FALSE
;
4578 uint32_t sc_filter_flags
= 0;
4580 BRIDGE_LOCK_ASSERT_HELD(sc
);
4582 bridge_ifp
= sc
->sc_ifp
;
4584 if (IF_BRIDGE_DEBUG(BR_DBGF_OUTPUT
)) {
4585 printf("%s: %s m 0x%llx\n", __func__
, bridge_ifp
->if_xname
,
4586 (uint64_t)VM_KERNEL_ADDRPERM(m
));
4588 #endif /* BRIDGE_DEBUG */
4590 src_if
= m
->m_pkthdr
.rcvif
;
4592 (void) ifnet_stat_increment_in(bridge_ifp
, 1, m
->m_pkthdr
.len
, 0);
4593 vlan
= VLANTAGOF(m
);
4596 if ((sbif
->bif_ifflags
& IFBIF_STP
) &&
4597 sbif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
) {
4601 eh
= mtod(m
, struct ether_header
*);
4602 dst
= eh
->ether_dhost
;
4604 /* If the interface is learning, record the address. */
4605 if (sbif
->bif_ifflags
& IFBIF_LEARNING
) {
4606 error
= bridge_rtupdate(sc
, eh
->ether_shost
, vlan
,
4607 sbif
, 0, IFBAF_DYNAMIC
);
4609 * If the interface has addresses limits then deny any source
4610 * that is not in the cache.
4612 if (error
&& sbif
->bif_addrmax
) {
4617 if ((sbif
->bif_ifflags
& IFBIF_STP
) != 0 &&
4618 sbif
->bif_stp
.bp_state
== BSTP_IFSTATE_LEARNING
) {
4623 * At this point, the port either doesn't participate
4624 * in spanning tree or it is in the forwarding state.
4628 * If the packet is unicast, destined for someone on
4629 * "this" side of the bridge, drop it.
4631 if ((m
->m_flags
& (M_BCAST
| M_MCAST
)) == 0) {
4633 dst_if
= bridge_rtlookup(sc
, dst
, vlan
);
4634 if (src_if
== dst_if
) {
4638 /* broadcast/multicast */
4641 * Check if its a reserved multicast address, any address
4642 * listed in 802.1D section 7.12.6 may not be forwarded by the
4644 * This is currently 01-80-C2-00-00-00 to 01-80-C2-00-00-0F
4646 if (dst
[0] == 0x01 && dst
[1] == 0x80 &&
4647 dst
[2] == 0xc2 && dst
[3] == 0x00 &&
4648 dst
[4] == 0x00 && dst
[5] <= 0x0f) {
4653 /* ...forward it to all interfaces. */
4654 atomic_add_64(&bridge_ifp
->if_imcasts
, 1);
4659 * If we have a destination interface which is a member of our bridge,
4660 * OR this is a unicast packet, push it through the bpf(4) machinery.
4661 * For broadcast or multicast packets, don't bother because it will
4662 * be reinjected into ether_input. We do this before we pass the packets
4663 * through the pfil(9) framework, as it is possible that pfil(9) will
4664 * drop the packet, or possibly modify it, making it difficult to debug
4665 * firewall issues on the bridge.
4668 if (eh
->ether_type
== htons(ETHERTYPE_RSN_PREAUTH
) ||
4669 dst_if
!= NULL
|| (m
->m_flags
& (M_BCAST
| M_MCAST
)) == 0) {
4670 m
->m_pkthdr
.rcvif
= bridge_ifp
;
4671 BRIDGE_BPF_MTAP_INPUT(sc
, m
);
4673 #endif /* NBPFILTER */
4675 #if defined(PFIL_HOOKS)
4676 /* run the packet filter */
4677 if (PFIL_HOOKED(&inet_pfil_hook
) || PFIL_HOOKED_INET6
) {
4679 if (bridge_pfil(&m
, bridge_ifp
, src_if
, PFIL_IN
) != 0) {
4687 #endif /* PFIL_HOOKS */
4689 if (dst_if
== NULL
) {
4690 /* bridge_broadcast will unlock */
4691 bridge_broadcast(sc
, src_if
, m
, 1);
4699 * At this point, we're dealing with a unicast frame
4700 * going to a different interface.
4702 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0) {
4706 dbif
= bridge_lookup_member_if(sc
, dst_if
);
4708 /* Not a member of the bridge (anymore?) */
4712 /* Private segments can not talk to each other */
4713 if (sbif
->bif_ifflags
& dbif
->bif_ifflags
& IFBIF_PRIVATE
) {
4717 if ((dbif
->bif_ifflags
& IFBIF_STP
) &&
4718 dbif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
) {
4723 /* APPLE MODIFICATION <rdar:6985737> */
4724 if ((dst_if
->if_extflags
& IFEXTF_DHCPRA_MASK
) != 0) {
4725 m
= ip_xdhcpra_output(dst_if
, m
);
4727 ++bridge_ifp
.if_xdhcpra
;
4732 #endif /* HAS_DHCPRA_MASK */
4734 if (dbif
== sc
->sc_mac_nat_bif
) {
4735 /* determine how to translate the packet */
4737 = bridge_mac_nat_output(sc
, sbif
, &m
, &mnr
);
4739 /* packet was deallocated */
4745 #if defined(PFIL_HOOKS)
4746 if (PFIL_HOOKED(&inet_pfil_hook
) || PFIL_HOOKED_INET6
) {
4747 if (bridge_pfil(&m
, bridge_ifp
, dst_if
, PFIL_OUT
) != 0) {
4754 #endif /* PFIL_HOOKS */
4756 sc_filter_flags
= sc
->sc_filter_flags
;
4758 if (PF_IS_ENABLED
&& (sc_filter_flags
& IFBF_FILT_MEMBER
)) {
4759 if (bridge_pf(&m
, dst_if
, sc_filter_flags
, FALSE
) != 0) {
4767 /* if we need to, translate the MAC address */
4768 if (translate_mac
) {
4769 bridge_mac_nat_translate(&m
, &mnr
, IF_LLADDR(dst_if
));
4772 * This is an inbound packet where the checksum
4773 * (if applicable) is already present/valid. Since
4774 * we are just doing layer 2 forwarding (not IP
4775 * forwarding), there's no need to validate the checksum.
4776 * Clear the checksum offload flags and send it along.
4779 (void) bridge_enqueue(bridge_ifp
, NULL
, dst_if
, m
,
4780 kChecksumOperationClear
);
4792 ether_ntop(char *buf
, size_t len
, const u_char
*ap
)
4794 snprintf(buf
, len
, "%02x:%02x:%02x:%02x:%02x:%02x",
4795 ap
[0], ap
[1], ap
[2], ap
[3], ap
[4], ap
[5]);
4800 #endif /* BRIDGE_DEBUG */
4803 inject_input_packet(ifnet_t ifp
, mbuf_t m
)
4805 mbuf_pkthdr_setrcvif(m
, ifp
);
4806 mbuf_pkthdr_setheader(m
, mbuf_data(m
));
4807 mbuf_setdata(m
, (char *)mbuf_data(m
) + ETHER_HDR_LEN
,
4808 mbuf_len(m
) - ETHER_HDR_LEN
);
4809 mbuf_pkthdr_adjustlen(m
, -ETHER_HDR_LEN
);
4810 m
->m_flags
|= M_PROTO1
; /* set to avoid loops */
4811 dlil_input_packet_list(ifp
, m
);
4818 * Filter input from a member interface. Queue the packet for
4819 * bridging if it is not for us.
4822 bridge_input(struct ifnet
*ifp
, mbuf_t
*data
)
4824 struct bridge_softc
*sc
= ifp
->if_bridge
;
4825 struct bridge_iflist
*bif
, *bif2
;
4827 struct ether_header
*eh
;
4828 struct mbuf
*mc
, *mc2
;
4831 boolean_t is_ifp_mac
= FALSE
;
4833 uint32_t sc_filter_flags
= 0;
4835 bridge_ifp
= sc
->sc_ifp
;
4837 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT
)) {
4838 printf("%s: %s from %s m 0x%llx data 0x%llx\n", __func__
,
4839 bridge_ifp
->if_xname
, ifp
->if_xname
,
4840 (uint64_t)VM_KERNEL_ADDRPERM(m
),
4841 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m
)));
4843 #endif /* BRIDGE_DEBUG */
4845 if ((sc
->sc_ifp
->if_flags
& IFF_RUNNING
) == 0) {
4847 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT
)) {
4848 printf("%s: %s not running passing along\n",
4849 __func__
, bridge_ifp
->if_xname
);
4851 #endif /* BRIDGE_DEBUG */
4855 vlan
= VLANTAGOF(m
);
4859 * Implement support for bridge monitoring. If this flag has been
4860 * set on this interface, discard the packet once we push it through
4861 * the bpf(4) machinery, but before we do, increment the byte and
4862 * packet counters associated with this interface.
4864 if ((bridge_ifp
->if_flags
& IFF_MONITOR
) != 0) {
4865 m
->m_pkthdr
.rcvif
= bridge_ifp
;
4866 BRIDGE_BPF_MTAP_INPUT(sc
, m
);
4867 (void) ifnet_stat_increment_in(bridge_ifp
, 1, m
->m_pkthdr
.len
, 0);
4871 #endif /* IFF_MONITOR */
4874 * Need to clear the promiscous flags otherwise it will be
4875 * dropped by DLIL after processing filters
4877 if ((mbuf_flags(m
) & MBUF_PROMISC
)) {
4878 mbuf_setflags_mask(m
, 0, MBUF_PROMISC
);
4881 sc_filter_flags
= sc
->sc_filter_flags
;
4882 if (PF_IS_ENABLED
&& (sc_filter_flags
& IFBF_FILT_MEMBER
)) {
4883 error
= bridge_pf(&m
, ifp
, sc_filter_flags
, TRUE
);
4891 * bridge_pf could have modified the pointer on success in order
4892 * to do its processing. Updated data such that we don't use a
4899 bif
= bridge_lookup_member_if(sc
, ifp
);
4903 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT
)) {
4904 printf("%s: %s bridge_lookup_member_if failed\n",
4905 __func__
, bridge_ifp
->if_xname
);
4907 #endif /* BRIDGE_DEBUG */
4911 if (bif
->bif_flags
& BIFF_HOST_FILTER
) {
4912 error
= bridge_host_filter(bif
, data
);
4914 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT
)) {
4915 printf("%s: %s bridge_host_filter failed\n",
4916 __func__
, bif
->bif_ifp
->if_xname
);
4924 eh
= mtod(m
, struct ether_header
*);
4928 if (m
->m_flags
& (M_BCAST
| M_MCAST
)) {
4930 if (IF_BRIDGE_DEBUG(BR_DBGF_MCAST
)) {
4931 if ((m
->m_flags
& M_MCAST
)) {
4932 printf("%s: multicast: "
4933 "%02x:%02x:%02x:%02x:%02x:%02x\n",
4935 eh
->ether_dhost
[0], eh
->ether_dhost
[1],
4936 eh
->ether_dhost
[2], eh
->ether_dhost
[3],
4937 eh
->ether_dhost
[4], eh
->ether_dhost
[5]);
4940 #endif /* BRIDGE_DEBUG */
4942 /* Tap off 802.1D packets; they do not get forwarded. */
4943 if (memcmp(eh
->ether_dhost
, bstp_etheraddr
,
4944 ETHER_ADDR_LEN
) == 0) {
4946 m
= bstp_input(&bif
->bif_stp
, ifp
, m
);
4947 #else /* !BRIDGESTP */
4950 #endif /* !BRIDGESTP */
4957 if ((bif
->bif_ifflags
& IFBIF_STP
) &&
4958 bif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
) {
4964 * Make a deep copy of the packet and enqueue the copy
4965 * for bridge processing; return the original packet for
4968 mc
= m_dup(m
, M_DONTWAIT
);
4975 * Perform the bridge forwarding function with the copy.
4977 * Note that bridge_forward calls BRIDGE_UNLOCK
4979 bridge_forward(sc
, bif
, mc
);
4982 * Reinject the mbuf as arriving on the bridge so we have a
4983 * chance at claiming multicast packets. We can not loop back
4984 * here from ether_input as a bridge is never a member of a
4987 VERIFY(bridge_ifp
->if_bridge
== NULL
);
4988 mc2
= m_dup(m
, M_DONTWAIT
);
4990 /* Keep the layer3 header aligned */
4991 int i
= min(mc2
->m_pkthdr
.len
, max_protohdr
);
4992 mc2
= m_copyup(mc2
, i
, ETHER_ALIGN
);
4995 /* mark packet as arriving on the bridge */
4996 mc2
->m_pkthdr
.rcvif
= bridge_ifp
;
4997 mc2
->m_pkthdr
.pkt_hdr
= mbuf_data(mc2
);
4999 BRIDGE_BPF_MTAP_INPUT(sc
, m
);
5001 (void) mbuf_setdata(mc2
,
5002 (char *)mbuf_data(mc2
) + ETHER_HDR_LEN
,
5003 mbuf_len(mc2
) - ETHER_HDR_LEN
);
5004 (void) mbuf_pkthdr_adjustlen(mc2
, -ETHER_HDR_LEN
);
5006 (void) ifnet_stat_increment_in(bridge_ifp
, 1,
5007 mbuf_pkthdr_len(mc2
), 0);
5010 if (IF_BRIDGE_DEBUG(BR_DBGF_MCAST
)) {
5011 printf("%s: %s mcast for us\n", __func__
,
5012 bridge_ifp
->if_xname
);
5014 #endif /* BRIDGE_DEBUG */
5016 dlil_input_packet_list(bridge_ifp
, mc2
);
5019 /* Return the original packet for local processing. */
5023 if ((bif
->bif_ifflags
& IFBIF_STP
) &&
5024 bif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
) {
5030 #define CARP_CHECK_WE_ARE_DST(iface) \
5031 ((iface)->if_carp &&\
5032 carp_forus((iface)->if_carp, eh->ether_dhost))
5033 #define CARP_CHECK_WE_ARE_SRC(iface) \
5034 ((iface)->if_carp &&\
5035 carp_forus((iface)->if_carp, eh->ether_shost))
5037 #define CARP_CHECK_WE_ARE_DST(iface) 0
5038 #define CARP_CHECK_WE_ARE_SRC(iface) 0
5042 #define PFIL_HOOKED_INET6 PFIL_HOOKED(&inet6_pfil_hook)
5044 #define PFIL_HOOKED_INET6 0
5047 #if defined(PFIL_HOOKS)
5048 #define PFIL_PHYS(sc, ifp, m) do { \
5049 if (pfil_local_phys && \
5050 (PFIL_HOOKED(&inet_pfil_hook) || PFIL_HOOKED_INET6)) { \
5051 if (bridge_pfil(&m, NULL, ifp, \
5052 PFIL_IN) != 0 || m == NULL) { \
5053 BRIDGE_UNLOCK(sc); \
5058 #else /* PFIL_HOOKS */
5059 #define PFIL_PHYS(sc, ifp, m)
5060 #endif /* PFIL_HOOKS */
5062 #define GRAB_OUR_PACKETS(iface) \
5063 if ((iface)->if_type == IFT_GIF) \
5065 /* It is destined for us. */ \
5066 if (memcmp(IF_LLADDR((iface)), eh->ether_dhost, \
5067 ETHER_ADDR_LEN) == 0 || CARP_CHECK_WE_ARE_DST((iface))) { \
5068 if ((iface)->if_type == IFT_BRIDGE) { \
5069 BRIDGE_BPF_MTAP_INPUT(sc, m); \
5070 /* Filter on the physical interface. */ \
5071 PFIL_PHYS(sc, iface, m); \
5073 bpf_tap_in(iface, DLT_EN10MB, m, NULL, 0); \
5075 if (bif->bif_ifflags & IFBIF_LEARNING) { \
5076 error = bridge_rtupdate(sc, eh->ether_shost, \
5077 vlan, bif, 0, IFBAF_DYNAMIC); \
5078 if (error && bif->bif_addrmax) { \
5079 BRIDGE_UNLOCK(sc); \
5081 return (EJUSTRETURN); \
5084 BRIDGE_UNLOCK(sc); \
5085 inject_input_packet(iface, m); \
5086 return (EJUSTRETURN); \
5089 /* We just received a packet that we sent out. */ \
5090 if (memcmp(IF_LLADDR((iface)), eh->ether_shost, \
5091 ETHER_ADDR_LEN) == 0 || CARP_CHECK_WE_ARE_SRC((iface))) { \
5092 BRIDGE_UNLOCK(sc); \
5094 return (EJUSTRETURN); \
5100 if (memcmp(eh
->ether_dhost
, IF_LLADDR(ifp
), ETHER_ADDR_LEN
) == 0) {
5104 /* handle MAC-NAT if enabled */
5105 if (is_ifp_mac
&& sc
->sc_mac_nat_bif
== bif
) {
5107 boolean_t is_input
= FALSE
;
5109 dst_if
= bridge_mac_nat_input(sc
, data
, &is_input
);
5111 if (dst_if
== ifp
) {
5112 /* our input packet */
5113 } else if (dst_if
!= NULL
|| m
== NULL
) {
5115 if (dst_if
!= NULL
) {
5118 inject_input_packet(dst_if
, m
);
5120 (void)bridge_enqueue(bridge_ifp
, NULL
,
5122 kChecksumOperationClear
);
5130 * If the packet is for the bridge, set the packet's source interface
5131 * and return the packet back to ether_input for local processing.
5133 if (memcmp(eh
->ether_dhost
, IF_LLADDR(bridge_ifp
),
5134 ETHER_ADDR_LEN
) == 0 || CARP_CHECK_WE_ARE_DST(bridge_ifp
)) {
5135 /* Mark the packet as arriving on the bridge interface */
5136 (void) mbuf_pkthdr_setrcvif(m
, bridge_ifp
);
5137 mbuf_pkthdr_setheader(m
, mbuf_data(m
));
5140 * If the interface is learning, and the source
5141 * address is valid and not multicast, record
5144 if (bif
->bif_ifflags
& IFBIF_LEARNING
) {
5145 (void) bridge_rtupdate(sc
, eh
->ether_shost
,
5146 vlan
, bif
, 0, IFBAF_DYNAMIC
);
5149 BRIDGE_BPF_MTAP_INPUT(sc
, m
);
5151 (void) mbuf_setdata(m
, (char *)mbuf_data(m
) + ETHER_HDR_LEN
,
5152 mbuf_len(m
) - ETHER_HDR_LEN
);
5153 (void) mbuf_pkthdr_adjustlen(m
, -ETHER_HDR_LEN
);
5155 (void) ifnet_stat_increment_in(bridge_ifp
, 1, mbuf_pkthdr_len(m
), 0);
5160 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT
)) {
5161 printf("%s: %s packet for bridge\n", __func__
,
5162 bridge_ifp
->if_xname
);
5164 #endif /* BRIDGE_DEBUG */
5166 dlil_input_packet_list(bridge_ifp
, m
);
5172 * if the destination of the packet is for the MAC address of
5173 * the member interface itself, then we don't need to forward
5174 * it -- just pass it back. Note that it'll likely just be
5175 * dropped by the stack, but if something else is bound to
5176 * the interface directly (for example, the wireless stats
5177 * protocol -- although that actually uses BPF right now),
5178 * then it will consume the packet
5180 * ALSO, note that we do this check AFTER checking for the
5181 * bridge's own MAC address, because the bridge may be
5182 * using the SAME MAC address as one of its interfaces
5186 #ifdef VERY_VERY_VERY_DIAGNOSTIC
5187 printf("%s: not forwarding packet bound for member "
5188 "interface\n", __func__
);
5195 /* Now check the remaining bridge members. */
5196 TAILQ_FOREACH(bif2
, &sc
->sc_iflist
, bif_next
) {
5197 if (bif2
->bif_ifp
!= ifp
) {
5198 GRAB_OUR_PACKETS(bif2
->bif_ifp
);
5202 #undef CARP_CHECK_WE_ARE_DST
5203 #undef CARP_CHECK_WE_ARE_SRC
5204 #undef GRAB_OUR_PACKETS
5207 * Perform the bridge forwarding function.
5209 * Note that bridge_forward calls BRIDGE_UNLOCK
5211 bridge_forward(sc
, bif
, m
);
5219 * Send a frame to all interfaces that are members of
5220 * the bridge, except for the one on which the packet
5223 * NOTE: Releases the lock on return.
5226 bridge_broadcast(struct bridge_softc
*sc
, struct ifnet
*src_if
,
5227 struct mbuf
*m
, int runfilt
)
5230 struct bridge_iflist
*dbif
, *sbif
;
5233 struct ifnet
*dst_if
;
5234 int error
= 0, used
= 0;
5235 boolean_t bridge_if_out
;
5236 ChecksumOperation cksum_op
;
5237 struct mac_nat_record mnr
;
5238 struct bridge_iflist
*mac_nat_bif
= sc
->sc_mac_nat_bif
;
5239 boolean_t translate_mac
= FALSE
;
5240 uint32_t sc_filter_flags
= 0;
5242 bridge_ifp
= sc
->sc_ifp
;
5243 if (src_if
!= NULL
) {
5244 bridge_if_out
= FALSE
;
5245 cksum_op
= kChecksumOperationClear
;
5246 sbif
= bridge_lookup_member_if(sc
, src_if
);
5247 if (sbif
!= NULL
&& mac_nat_bif
!= NULL
&& sbif
!= mac_nat_bif
) {
5248 /* get the translation record while holding the lock */
5250 = bridge_mac_nat_output(sc
, sbif
, &m
, &mnr
);
5252 /* packet was deallocated */
5259 * src_if is NULL when the bridge interface calls
5260 * bridge_broadcast().
5262 bridge_if_out
= TRUE
;
5263 cksum_op
= kChecksumOperationFinalize
;
5267 BRIDGE_LOCK2REF(sc
, error
);
5274 /* Filter on the bridge interface before broadcasting */
5275 if (runfilt
&& (PFIL_HOOKED(&inet_pfil_hook
) || PFIL_HOOKED_INET6
)) {
5276 if (bridge_pfil(&m
, bridge_ifp
, NULL
, PFIL_OUT
) != 0) {
5283 #endif /* PFIL_HOOKS */
5284 TAILQ_FOREACH(dbif
, &sc
->sc_iflist
, bif_next
) {
5285 dst_if
= dbif
->bif_ifp
;
5286 if (dst_if
== src_if
) {
5287 /* skip the interface that the packet came in on */
5291 /* Private segments can not talk to each other */
5293 (sbif
->bif_ifflags
& dbif
->bif_ifflags
& IFBIF_PRIVATE
)) {
5297 if ((dbif
->bif_ifflags
& IFBIF_STP
) &&
5298 dbif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
) {
5302 if ((dbif
->bif_ifflags
& IFBIF_DISCOVER
) == 0 &&
5303 (m
->m_flags
& (M_BCAST
| M_MCAST
)) == 0) {
5307 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0) {
5311 if (!(dbif
->bif_flags
& BIFF_MEDIA_ACTIVE
)) {
5315 if (TAILQ_NEXT(dbif
, bif_next
) == NULL
) {
5319 mc
= m_dup(m
, M_DONTWAIT
);
5321 (void) ifnet_stat_increment_out(bridge_ifp
,
5328 * If broadcast input is enabled, do so only if this
5329 * is an input packet.
5331 if (!bridge_if_out
&&
5332 (dbif
->bif_flags
& BIFF_INPUT_BROADCAST
) != 0) {
5333 mc_in
= m_dup(mc
, M_DONTWAIT
);
5334 /* this could fail, but we continue anyways */
5341 * Filter on the output interface. Pass a NULL bridge interface
5342 * pointer so we do not redundantly filter on the bridge for
5343 * each interface we broadcast on.
5346 (PFIL_HOOKED(&inet_pfil_hook
) || PFIL_HOOKED_INET6
)) {
5348 /* Keep the layer3 header aligned */
5349 int i
= min(mc
->m_pkthdr
.len
, max_protohdr
);
5350 mc
= m_copyup(mc
, i
, ETHER_ALIGN
);
5352 (void) ifnet_stat_increment_out(
5353 bridge_ifp
, 0, 0, 1);
5354 if (mc_in
!= NULL
) {
5360 if (bridge_pfil(&mc
, NULL
, dst_if
, PFIL_OUT
) != 0) {
5361 if (mc_in
!= NULL
) {
5367 if (mc_in
!= NULL
) {
5373 #endif /* PFIL_HOOKS */
5376 if (translate_mac
&& mac_nat_bif
== dbif
) {
5377 /* translate the packet without holding the lock */
5378 bridge_mac_nat_translate(&mc
, &mnr
, IF_LLADDR(dst_if
));
5381 sc_filter_flags
= sc
->sc_filter_flags
;
5383 PF_IS_ENABLED
&& (sc_filter_flags
& IFBF_FILT_MEMBER
)) {
5385 /* Keep the layer3 header aligned */
5386 int i
= min(mc
->m_pkthdr
.len
, max_protohdr
);
5387 mc
= m_copyup(mc
, i
, ETHER_ALIGN
);
5389 (void) ifnet_stat_increment_out(
5390 sc
->sc_ifp
, 0, 0, 1);
5391 if (mc_in
!= NULL
) {
5398 if (bridge_pf(&mc
, dst_if
, sc_filter_flags
, FALSE
) != 0) {
5399 if (mc_in
!= NULL
) {
5406 if (mc_in
!= NULL
) {
5415 (void) bridge_enqueue(bridge_ifp
,
5416 NULL
, dst_if
, mc
, cksum_op
);
5420 if (mc_in
== NULL
) {
5423 bpf_tap_in(dst_if
, DLT_EN10MB
, mc_in
, NULL
, 0);
5424 mbuf_pkthdr_setrcvif(mc_in
, dst_if
);
5425 mbuf_pkthdr_setheader(mc_in
, mbuf_data(mc_in
));
5426 mbuf_setdata(mc_in
, (char *)mbuf_data(mc_in
) + ETHER_HDR_LEN
,
5427 mbuf_len(mc_in
) - ETHER_HDR_LEN
);
5428 mbuf_pkthdr_adjustlen(mc_in
, -ETHER_HDR_LEN
);
5429 mc_in
->m_flags
|= M_PROTO1
; /* set to avoid loops */
5430 dlil_input_packet_list(dst_if
, mc_in
);
5438 #endif /* PFIL_HOOKS */
5446 * Duplicate a packet out one or more interfaces that are in span mode,
5447 * the original mbuf is unmodified.
5450 bridge_span(struct bridge_softc
*sc
, struct mbuf
*m
)
5452 struct bridge_iflist
*bif
;
5453 struct ifnet
*dst_if
;
5456 if (TAILQ_EMPTY(&sc
->sc_spanlist
)) {
5460 TAILQ_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
) {
5461 dst_if
= bif
->bif_ifp
;
5463 if ((dst_if
->if_flags
& IFF_RUNNING
) == 0) {
5467 mc
= m_copypacket(m
, M_DONTWAIT
);
5469 (void) ifnet_stat_increment_out(sc
->sc_ifp
, 0, 0, 1);
5473 (void) bridge_enqueue(sc
->sc_ifp
, NULL
, dst_if
, mc
,
5474 kChecksumOperationNone
);
5482 * Add a bridge routing entry.
5485 bridge_rtupdate(struct bridge_softc
*sc
, const uint8_t *dst
, uint16_t vlan
,
5486 struct bridge_iflist
*bif
, int setflags
, uint8_t flags
)
5488 struct bridge_rtnode
*brt
;
5491 BRIDGE_LOCK_ASSERT_HELD(sc
);
5493 /* Check the source address is valid and not multicast. */
5494 if (ETHER_IS_MULTICAST(dst
) ||
5495 (dst
[0] == 0 && dst
[1] == 0 && dst
[2] == 0 &&
5496 dst
[3] == 0 && dst
[4] == 0 && dst
[5] == 0) != 0) {
5501 /* 802.1p frames map to vlan 1 */
5507 * A route for this destination might already exist. If so,
5508 * update it, otherwise create a new one.
5510 if ((brt
= bridge_rtnode_lookup(sc
, dst
, vlan
)) == NULL
) {
5511 if (sc
->sc_brtcnt
>= sc
->sc_brtmax
) {
5512 sc
->sc_brtexceeded
++;
5515 /* Check per interface address limits (if enabled) */
5516 if (bif
->bif_addrmax
&& bif
->bif_addrcnt
>= bif
->bif_addrmax
) {
5517 bif
->bif_addrexceeded
++;
5522 * Allocate a new bridge forwarding node, and
5523 * initialize the expiration time and Ethernet
5526 brt
= zalloc_noblock(bridge_rtnode_pool
);
5528 if (IF_BRIDGE_DEBUG(BR_DBGF_RT_TABLE
)) {
5529 printf("%s: zalloc_nolock failed", __func__
);
5533 bzero(brt
, sizeof(struct bridge_rtnode
));
5535 if (bif
->bif_ifflags
& IFBIF_STICKY
) {
5536 brt
->brt_flags
= IFBAF_STICKY
;
5538 brt
->brt_flags
= IFBAF_DYNAMIC
;
5541 memcpy(brt
->brt_addr
, dst
, ETHER_ADDR_LEN
);
5542 brt
->brt_vlan
= vlan
;
5545 if ((error
= bridge_rtnode_insert(sc
, brt
)) != 0) {
5546 zfree(bridge_rtnode_pool
, brt
);
5552 if (IF_BRIDGE_DEBUG(BR_DBGF_RT_TABLE
)) {
5553 printf("%s: added %02x:%02x:%02x:%02x:%02x:%02x "
5554 "on %s count %u hashsize %u\n", __func__
,
5555 dst
[0], dst
[1], dst
[2], dst
[3], dst
[4], dst
[5],
5556 sc
->sc_ifp
->if_xname
, sc
->sc_brtcnt
,
5557 sc
->sc_rthash_size
);
5562 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
&&
5563 brt
->brt_dst
!= bif
) {
5564 brt
->brt_dst
->bif_addrcnt
--;
5566 brt
->brt_dst
->bif_addrcnt
++;
5569 if ((flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
5572 now
= (unsigned long) net_uptime();
5573 brt
->brt_expire
= now
+ sc
->sc_brttimeout
;
5576 brt
->brt_flags
= flags
;
5586 * Lookup the destination interface for an address.
5588 static struct ifnet
*
5589 bridge_rtlookup(struct bridge_softc
*sc
, const uint8_t *addr
, uint16_t vlan
)
5591 struct bridge_rtnode
*brt
;
5593 BRIDGE_LOCK_ASSERT_HELD(sc
);
5595 if ((brt
= bridge_rtnode_lookup(sc
, addr
, vlan
)) == NULL
) {
5599 return brt
->brt_ifp
;
5605 * Trim the routine table so that we have a number
5606 * of routing entries less than or equal to the
5610 bridge_rttrim(struct bridge_softc
*sc
)
5612 struct bridge_rtnode
*brt
, *nbrt
;
5614 BRIDGE_LOCK_ASSERT_HELD(sc
);
5616 /* Make sure we actually need to do this. */
5617 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
) {
5621 /* Force an aging cycle; this might trim enough addresses. */
5623 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
) {
5627 LIST_FOREACH_SAFE(brt
, &sc
->sc_rtlist
, brt_list
, nbrt
) {
5628 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
5629 bridge_rtnode_destroy(sc
, brt
);
5630 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
) {
5638 * bridge_aging_timer:
5640 * Aging periodic timer for the bridge routing table.
5643 bridge_aging_timer(struct bridge_softc
*sc
)
5645 BRIDGE_LOCK_ASSERT_HELD(sc
);
5648 if ((sc
->sc_ifp
->if_flags
& IFF_RUNNING
) &&
5649 (sc
->sc_flags
& SCF_DETACHING
) == 0) {
5650 sc
->sc_aging_timer
.bdc_sc
= sc
;
5651 sc
->sc_aging_timer
.bdc_func
= bridge_aging_timer
;
5652 sc
->sc_aging_timer
.bdc_ts
.tv_sec
= bridge_rtable_prune_period
;
5653 bridge_schedule_delayed_call(&sc
->sc_aging_timer
);
5660 * Perform an aging cycle.
5663 bridge_rtage(struct bridge_softc
*sc
)
5665 struct bridge_rtnode
*brt
, *nbrt
;
5668 BRIDGE_LOCK_ASSERT_HELD(sc
);
5670 now
= (unsigned long) net_uptime();
5672 LIST_FOREACH_SAFE(brt
, &sc
->sc_rtlist
, brt_list
, nbrt
) {
5673 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
5674 if (now
>= brt
->brt_expire
) {
5675 bridge_rtnode_destroy(sc
, brt
);
5679 if (sc
->sc_mac_nat_bif
!= NULL
) {
5680 bridge_mac_nat_age_entries(sc
, now
);
5687 * Remove all dynamic addresses from the bridge.
5690 bridge_rtflush(struct bridge_softc
*sc
, int full
)
5692 struct bridge_rtnode
*brt
, *nbrt
;
5694 BRIDGE_LOCK_ASSERT_HELD(sc
);
5696 LIST_FOREACH_SAFE(brt
, &sc
->sc_rtlist
, brt_list
, nbrt
) {
5697 if (full
|| (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
5698 bridge_rtnode_destroy(sc
, brt
);
5706 * Remove an address from the table.
5709 bridge_rtdaddr(struct bridge_softc
*sc
, const uint8_t *addr
, uint16_t vlan
)
5711 struct bridge_rtnode
*brt
;
5714 BRIDGE_LOCK_ASSERT_HELD(sc
);
5717 * If vlan is zero then we want to delete for all vlans so the lookup
5718 * may return more than one.
5720 while ((brt
= bridge_rtnode_lookup(sc
, addr
, vlan
)) != NULL
) {
5721 bridge_rtnode_destroy(sc
, brt
);
5725 return found
? 0 : ENOENT
;
5731 * Delete routes to a specific member interface.
5734 bridge_rtdelete(struct bridge_softc
*sc
, struct ifnet
*ifp
, int full
)
5736 struct bridge_rtnode
*brt
, *nbrt
;
5738 BRIDGE_LOCK_ASSERT_HELD(sc
);
5740 LIST_FOREACH_SAFE(brt
, &sc
->sc_rtlist
, brt_list
, nbrt
) {
5741 if (brt
->brt_ifp
== ifp
&& (full
||
5742 (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)) {
5743 bridge_rtnode_destroy(sc
, brt
);
5749 * bridge_rtable_init:
5751 * Initialize the route table for this bridge.
5754 bridge_rtable_init(struct bridge_softc
*sc
)
5758 sc
->sc_rthash
= _MALLOC(sizeof(*sc
->sc_rthash
) * BRIDGE_RTHASH_SIZE
,
5759 M_DEVBUF
, M_WAITOK
| M_ZERO
);
5760 if (sc
->sc_rthash
== NULL
) {
5761 printf("%s: no memory\n", __func__
);
5764 sc
->sc_rthash_size
= BRIDGE_RTHASH_SIZE
;
5766 for (i
= 0; i
< sc
->sc_rthash_size
; i
++) {
5767 LIST_INIT(&sc
->sc_rthash
[i
]);
5770 sc
->sc_rthash_key
= RandomULong();
5772 LIST_INIT(&sc
->sc_rtlist
);
5778 * bridge_rthash_delayed_resize:
5780 * Resize the routing table hash on a delayed thread call.
5783 bridge_rthash_delayed_resize(struct bridge_softc
*sc
)
5785 u_int32_t new_rthash_size
;
5786 struct _bridge_rtnode_list
*new_rthash
= NULL
;
5787 struct _bridge_rtnode_list
*old_rthash
= NULL
;
5789 struct bridge_rtnode
*brt
;
5792 BRIDGE_LOCK_ASSERT_HELD(sc
);
5795 * Four entries per hash bucket is our ideal load factor
5797 if (sc
->sc_brtcnt
< sc
->sc_rthash_size
* 4) {
5802 * Doubling the number of hash buckets may be too simplistic
5803 * especially when facing a spike of new entries
5805 new_rthash_size
= sc
->sc_rthash_size
* 2;
5807 sc
->sc_flags
|= SCF_RESIZING
;
5810 new_rthash
= _MALLOC(sizeof(*sc
->sc_rthash
) * new_rthash_size
,
5811 M_DEVBUF
, M_WAITOK
| M_ZERO
);
5814 sc
->sc_flags
&= ~SCF_RESIZING
;
5816 if (new_rthash
== NULL
) {
5820 if ((sc
->sc_flags
& SCF_DETACHING
)) {
5825 * Fail safe from here on
5827 old_rthash
= sc
->sc_rthash
;
5828 sc
->sc_rthash
= new_rthash
;
5829 sc
->sc_rthash_size
= new_rthash_size
;
5832 * Get a new key to force entries to be shuffled around to reduce
5833 * the likelihood they will land in the same buckets
5835 sc
->sc_rthash_key
= RandomULong();
5837 for (i
= 0; i
< sc
->sc_rthash_size
; i
++) {
5838 LIST_INIT(&sc
->sc_rthash
[i
]);
5841 LIST_FOREACH(brt
, &sc
->sc_rtlist
, brt_list
) {
5842 LIST_REMOVE(brt
, brt_hash
);
5843 (void) bridge_rtnode_hash(sc
, brt
);
5848 if (IF_BRIDGE_DEBUG(BR_DBGF_RT_TABLE
)) {
5849 printf("%s: %s new size %u\n", __func__
,
5850 sc
->sc_ifp
->if_xname
, sc
->sc_rthash_size
);
5852 #endif /* BRIDGE_DEBUG */
5854 _FREE(old_rthash
, M_DEVBUF
);
5858 printf("%s: %s failed %d\n", __func__
,
5859 sc
->sc_ifp
->if_xname
, error
);
5860 #endif /* BRIDGE_DEBUG */
5861 if (new_rthash
!= NULL
) {
5862 _FREE(new_rthash
, M_DEVBUF
);
5868 * Resize the number of hash buckets based on the load factor
5869 * Currently only grow
5870 * Failing to resize the hash table is not fatal
5873 bridge_rthash_resize(struct bridge_softc
*sc
)
5875 BRIDGE_LOCK_ASSERT_HELD(sc
);
5877 if ((sc
->sc_flags
& SCF_DETACHING
) || (sc
->sc_flags
& SCF_RESIZING
)) {
5882 * Four entries per hash bucket is our ideal load factor
5884 if (sc
->sc_brtcnt
< sc
->sc_rthash_size
* 4) {
5888 * Hard limit on the size of the routing hash table
5890 if (sc
->sc_rthash_size
>= bridge_rtable_hash_size_max
) {
5894 sc
->sc_resize_call
.bdc_sc
= sc
;
5895 sc
->sc_resize_call
.bdc_func
= bridge_rthash_delayed_resize
;
5896 bridge_schedule_delayed_call(&sc
->sc_resize_call
);
5900 * bridge_rtable_fini:
5902 * Deconstruct the route table for this bridge.
5905 bridge_rtable_fini(struct bridge_softc
*sc
)
5907 KASSERT(sc
->sc_brtcnt
== 0,
5908 ("%s: %d bridge routes referenced", __func__
, sc
->sc_brtcnt
));
5909 if (sc
->sc_rthash
) {
5910 _FREE(sc
->sc_rthash
, M_DEVBUF
);
5911 sc
->sc_rthash
= NULL
;
5916 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
5917 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
5919 #define mix(a, b, c) \
5921 a -= b; a -= c; a ^= (c >> 13); \
5922 b -= c; b -= a; b ^= (a << 8); \
5923 c -= a; c -= b; c ^= (b >> 13); \
5924 a -= b; a -= c; a ^= (c >> 12); \
5925 b -= c; b -= a; b ^= (a << 16); \
5926 c -= a; c -= b; c ^= (b >> 5); \
5927 a -= b; a -= c; a ^= (c >> 3); \
5928 b -= c; b -= a; b ^= (a << 10); \
5929 c -= a; c -= b; c ^= (b >> 15); \
5930 } while ( /*CONSTCOND*/ 0)
5932 static __inline
uint32_t
5933 bridge_rthash(struct bridge_softc
*sc
, const uint8_t *addr
)
5935 uint32_t a
= 0x9e3779b9, b
= 0x9e3779b9, c
= sc
->sc_rthash_key
;
5946 return c
& BRIDGE_RTHASH_MASK(sc
);
5952 bridge_rtnode_addr_cmp(const uint8_t *a
, const uint8_t *b
)
5956 for (i
= 0, d
= 0; i
< ETHER_ADDR_LEN
&& d
== 0; i
++) {
5957 d
= ((int)a
[i
]) - ((int)b
[i
]);
5964 * bridge_rtnode_lookup:
5966 * Look up a bridge route node for the specified destination. Compare the
5967 * vlan id or if zero then just return the first match.
5969 static struct bridge_rtnode
*
5970 bridge_rtnode_lookup(struct bridge_softc
*sc
, const uint8_t *addr
,
5973 struct bridge_rtnode
*brt
;
5977 BRIDGE_LOCK_ASSERT_HELD(sc
);
5979 hash
= bridge_rthash(sc
, addr
);
5980 LIST_FOREACH(brt
, &sc
->sc_rthash
[hash
], brt_hash
) {
5981 dir
= bridge_rtnode_addr_cmp(addr
, brt
->brt_addr
);
5982 if (dir
== 0 && (brt
->brt_vlan
== vlan
|| vlan
== 0)) {
5994 * bridge_rtnode_hash:
5996 * Insert the specified bridge node into the route hash table.
5997 * This is used when adding a new node or to rehash when resizing
6001 bridge_rtnode_hash(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
6003 struct bridge_rtnode
*lbrt
;
6007 BRIDGE_LOCK_ASSERT_HELD(sc
);
6009 hash
= bridge_rthash(sc
, brt
->brt_addr
);
6011 lbrt
= LIST_FIRST(&sc
->sc_rthash
[hash
]);
6013 LIST_INSERT_HEAD(&sc
->sc_rthash
[hash
], brt
, brt_hash
);
6018 dir
= bridge_rtnode_addr_cmp(brt
->brt_addr
, lbrt
->brt_addr
);
6019 if (dir
== 0 && brt
->brt_vlan
== lbrt
->brt_vlan
) {
6021 if (IF_BRIDGE_DEBUG(BR_DBGF_RT_TABLE
)) {
6022 printf("%s: %s EEXIST "
6023 "%02x:%02x:%02x:%02x:%02x:%02x\n",
6024 __func__
, sc
->sc_ifp
->if_xname
,
6025 brt
->brt_addr
[0], brt
->brt_addr
[1],
6026 brt
->brt_addr
[2], brt
->brt_addr
[3],
6027 brt
->brt_addr
[4], brt
->brt_addr
[5]);
6033 LIST_INSERT_BEFORE(lbrt
, brt
, brt_hash
);
6036 if (LIST_NEXT(lbrt
, brt_hash
) == NULL
) {
6037 LIST_INSERT_AFTER(lbrt
, brt
, brt_hash
);
6040 lbrt
= LIST_NEXT(lbrt
, brt_hash
);
6041 } while (lbrt
!= NULL
);
6044 if (IF_BRIDGE_DEBUG(BR_DBGF_RT_TABLE
)) {
6045 printf("%s: %s impossible %02x:%02x:%02x:%02x:%02x:%02x\n",
6046 __func__
, sc
->sc_ifp
->if_xname
,
6047 brt
->brt_addr
[0], brt
->brt_addr
[1], brt
->brt_addr
[2],
6048 brt
->brt_addr
[3], brt
->brt_addr
[4], brt
->brt_addr
[5]);
6057 * bridge_rtnode_insert:
6059 * Insert the specified bridge node into the route table. We
6060 * assume the entry is not already in the table.
6063 bridge_rtnode_insert(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
6067 error
= bridge_rtnode_hash(sc
, brt
);
6072 LIST_INSERT_HEAD(&sc
->sc_rtlist
, brt
, brt_list
);
6075 bridge_rthash_resize(sc
);
6081 * bridge_rtnode_destroy:
6083 * Destroy a bridge rtnode.
6086 bridge_rtnode_destroy(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
6088 BRIDGE_LOCK_ASSERT_HELD(sc
);
6090 LIST_REMOVE(brt
, brt_hash
);
6092 LIST_REMOVE(brt
, brt_list
);
6094 brt
->brt_dst
->bif_addrcnt
--;
6095 zfree(bridge_rtnode_pool
, brt
);
6100 * bridge_rtable_expire:
6102 * Set the expiry time for all routes on an interface.
6105 bridge_rtable_expire(struct ifnet
*ifp
, int age
)
6107 struct bridge_softc
*sc
= ifp
->if_bridge
;
6108 struct bridge_rtnode
*brt
;
6113 * If the age is zero then flush, otherwise set all the expiry times to
6114 * age for the interface
6117 bridge_rtdelete(sc
, ifp
, IFBF_FLUSHDYN
);
6121 now
= (unsigned long) net_uptime();
6123 LIST_FOREACH(brt
, &sc
->sc_rtlist
, brt_list
) {
6124 /* Cap the expiry time to 'age' */
6125 if (brt
->brt_ifp
== ifp
&&
6126 brt
->brt_expire
> now
+ age
&&
6127 (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
6128 brt
->brt_expire
= now
+ age
;
6136 * bridge_state_change:
6138 * Callback from the bridgestp code when a port changes states.
6141 bridge_state_change(struct ifnet
*ifp
, int state
)
6143 struct bridge_softc
*sc
= ifp
->if_bridge
;
6144 static const char *stpstates
[] = {
6154 log(LOG_NOTICE
, "%s: state changed to %s on %s\n",
6155 sc
->sc_ifp
->if_xname
,
6156 stpstates
[state
], ifp
->if_xname
);
6159 #endif /* BRIDGESTP */
6163 * Send bridge packets through pfil if they are one of the types pfil can deal
6164 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
6165 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
6169 bridge_pfil(struct mbuf
**mp
, struct ifnet
*bifp
, struct ifnet
*ifp
, int dir
)
6171 int snap
, error
, i
, hlen
;
6172 struct ether_header
*eh1
, eh2
;
6173 struct ip_fw_args args
;
6176 u_int16_t ether_type
;
6179 error
= -1; /* Default error if not error == 0 */
6182 /* we may return with the IP fields swapped, ensure its not shared */
6183 KASSERT(M_WRITABLE(*mp
), ("%s: modifying a shared mbuf", __func__
));
6186 if (pfil_bridge
== 0 && pfil_member
== 0 && pfil_ipfw
== 0) {
6187 return 0; /* filtering is disabled */
6189 i
= min((*mp
)->m_pkthdr
.len
, max_protohdr
);
6190 if ((*mp
)->m_len
< i
) {
6191 *mp
= m_pullup(*mp
, i
);
6193 printf("%s: m_pullup failed\n", __func__
);
6198 eh1
= mtod(*mp
, struct ether_header
*);
6199 ether_type
= ntohs(eh1
->ether_type
);
6202 * Check for SNAP/LLC.
6204 if (ether_type
< ETHERMTU
) {
6205 struct llc
*llc2
= (struct llc
*)(eh1
+ 1);
6207 if ((*mp
)->m_len
>= ETHER_HDR_LEN
+ 8 &&
6208 llc2
->llc_dsap
== LLC_SNAP_LSAP
&&
6209 llc2
->llc_ssap
== LLC_SNAP_LSAP
&&
6210 llc2
->llc_control
== LLC_UI
) {
6211 ether_type
= htons(llc2
->llc_un
.type_snap
.ether_type
);
6217 * If we're trying to filter bridge traffic, don't look at anything
6218 * other than IP and ARP traffic. If the filter doesn't understand
6219 * IPv6, don't allow IPv6 through the bridge either. This is lame
6220 * since if we really wanted, say, an AppleTalk filter, we are hosed,
6221 * but of course we don't have an AppleTalk filter to begin with.
6222 * (Note that since pfil doesn't understand ARP it will pass *ALL*
6225 switch (ether_type
) {
6227 case ETHERTYPE_REVARP
:
6228 if (pfil_ipfw_arp
== 0) {
6229 return 0; /* Automatically pass */
6235 case ETHERTYPE_IPV6
:
6240 * Check to see if the user wants to pass non-ip
6241 * packets, these will not be checked by pfil(9) and
6242 * passed unconditionally so the default is to drop.
6249 /* Strip off the Ethernet header and keep a copy. */
6250 m_copydata(*mp
, 0, ETHER_HDR_LEN
, (caddr_t
)&eh2
);
6251 m_adj(*mp
, ETHER_HDR_LEN
);
6253 /* Strip off snap header, if present */
6255 m_copydata(*mp
, 0, sizeof(struct llc
), (caddr_t
)&llc1
);
6256 m_adj(*mp
, sizeof(struct llc
));
6260 * Check the IP header for alignment and errors
6262 if (dir
== PFIL_IN
) {
6263 switch (ether_type
) {
6265 error
= bridge_ip_checkbasic(mp
);
6268 case ETHERTYPE_IPV6
:
6269 error
= bridge_ip6_checkbasic(mp
);
6280 if (IPFW_LOADED
&& pfil_ipfw
!= 0 && dir
== PFIL_OUT
&& ifp
!= NULL
) {
6282 args
.rule
= ip_dn_claim_rule(*mp
);
6283 if (args
.rule
!= NULL
&& fw_one_pass
) {
6284 goto ipfwpass
; /* packet already partially processed */
6288 args
.next_hop
= NULL
;
6290 args
.inp
= NULL
; /* used by ipfw uid/gid/jail rules */
6291 i
= ip_fw_chk_ptr(&args
);
6298 if (DUMMYNET_LOADED
&& (i
== IP_FW_DUMMYNET
)) {
6299 /* put the Ethernet header back on */
6300 M_PREPEND(*mp
, ETHER_HDR_LEN
, M_DONTWAIT
, 0);
6304 bcopy(&eh2
, mtod(*mp
, caddr_t
), ETHER_HDR_LEN
);
6307 * Pass the pkt to dummynet, which consumes it. The
6308 * packet will return to us via bridge_dummynet().
6311 ip_dn_io_ptr(mp
, DN_TO_IFB_FWD
, &args
, DN_CLIENT_IPFW
);
6315 if (i
!= IP_FW_PASS
) { /* drop */
6324 * Run the packet through pfil
6326 switch (ether_type
) {
6329 * before calling the firewall, swap fields the same as
6330 * IP does. here we assume the header is contiguous
6332 ip
= mtod(*mp
, struct ip
*);
6334 ip
->ip_len
= ntohs(ip
->ip_len
);
6335 ip
->ip_off
= ntohs(ip
->ip_off
);
6338 * Run pfil on the member interface and the bridge, both can
6339 * be skipped by clearing pfil_member or pfil_bridge.
6342 * in_if -> bridge_if -> out_if
6344 if (pfil_bridge
&& dir
== PFIL_OUT
&& bifp
!= NULL
) {
6345 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, bifp
,
6349 if (*mp
== NULL
|| error
!= 0) { /* filter may consume */
6353 if (pfil_member
&& ifp
!= NULL
) {
6354 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, ifp
,
6358 if (*mp
== NULL
|| error
!= 0) { /* filter may consume */
6362 if (pfil_bridge
&& dir
== PFIL_IN
&& bifp
!= NULL
) {
6363 error
= pfil_run_hooks(&inet_pfil_hook
, mp
, bifp
,
6367 if (*mp
== NULL
|| error
!= 0) { /* filter may consume */
6371 /* check if we need to fragment the packet */
6372 if (pfil_member
&& ifp
!= NULL
&& dir
== PFIL_OUT
) {
6373 i
= (*mp
)->m_pkthdr
.len
;
6374 if (i
> ifp
->if_mtu
) {
6375 error
= bridge_fragment(ifp
, *mp
, &eh2
, snap
,
6381 /* Recalculate the ip checksum and restore byte ordering */
6382 ip
= mtod(*mp
, struct ip
*);
6383 hlen
= ip
->ip_hl
<< 2;
6384 if (hlen
< sizeof(struct ip
)) {
6387 if (hlen
> (*mp
)->m_len
) {
6388 if ((*mp
= m_pullup(*mp
, hlen
)) == 0) {
6391 ip
= mtod(*mp
, struct ip
*);
6396 ip
->ip_len
= htons(ip
->ip_len
);
6397 ip
->ip_off
= htons(ip
->ip_off
);
6399 if (hlen
== sizeof(struct ip
)) {
6400 ip
->ip_sum
= in_cksum_hdr(ip
);
6402 ip
->ip_sum
= in_cksum(*mp
, hlen
);
6407 case ETHERTYPE_IPV6
:
6408 if (pfil_bridge
&& dir
== PFIL_OUT
&& bifp
!= NULL
) {
6409 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, bifp
,
6413 if (*mp
== NULL
|| error
!= 0) { /* filter may consume */
6417 if (pfil_member
&& ifp
!= NULL
) {
6418 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, ifp
,
6422 if (*mp
== NULL
|| error
!= 0) { /* filter may consume */
6426 if (pfil_bridge
&& dir
== PFIL_IN
&& bifp
!= NULL
) {
6427 error
= pfil_run_hooks(&inet6_pfil_hook
, mp
, bifp
,
6447 * Finally, put everything back the way it was and return
6450 M_PREPEND(*mp
, sizeof(struct llc
), M_DONTWAIT
, 0);
6454 bcopy(&llc1
, mtod(*mp
, caddr_t
), sizeof(struct llc
));
6457 M_PREPEND(*mp
, ETHER_HDR_LEN
, M_DONTWAIT
, 0);
6461 bcopy(&eh2
, mtod(*mp
, caddr_t
), ETHER_HDR_LEN
);
6470 #endif /* PFIL_HOOKS */
6473 * Perform basic checks on header size since
6474 * pfil assumes ip_input has already processed
6475 * it for it. Cut-and-pasted from ip_input.c.
6476 * Given how simple the IPv6 version is,
6477 * does the IPv4 version really need to be
6480 * XXX Should we update ipstat here, or not?
6481 * XXX Right now we update ipstat but not
6485 bridge_ip_checkbasic(struct mbuf
**mp
)
6487 struct mbuf
*m
= *mp
;
6496 if (IP_HDR_ALIGNED_P(mtod(m
, caddr_t
)) == 0) {
6497 /* max_linkhdr is already rounded up to nearest 4-byte */
6498 if ((m
= m_copyup(m
, sizeof(struct ip
),
6499 max_linkhdr
)) == NULL
) {
6500 /* XXXJRT new stat, please */
6501 ipstat
.ips_toosmall
++;
6504 } else if (OS_EXPECT((size_t)m
->m_len
< sizeof(struct ip
), 0)) {
6505 if ((m
= m_pullup(m
, sizeof(struct ip
))) == NULL
) {
6506 ipstat
.ips_toosmall
++;
6510 ip
= mtod(m
, struct ip
*);
6515 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
6516 ipstat
.ips_badvers
++;
6519 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
6520 if (hlen
< (int)sizeof(struct ip
)) { /* minimum header length */
6521 ipstat
.ips_badhlen
++;
6524 if (hlen
> m
->m_len
) {
6525 if ((m
= m_pullup(m
, hlen
)) == 0) {
6526 ipstat
.ips_badhlen
++;
6529 ip
= mtod(m
, struct ip
*);
6535 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
6536 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
6538 if (hlen
== sizeof(struct ip
)) {
6539 sum
= in_cksum_hdr(ip
);
6541 sum
= in_cksum(m
, hlen
);
6545 ipstat
.ips_badsum
++;
6549 /* Retrieve the packet length. */
6550 len
= ntohs(ip
->ip_len
);
6553 * Check for additional length bogosity
6556 ipstat
.ips_badlen
++;
6561 * Check that the amount of data in the buffers
6562 * is as at least much as the IP header would have us expect.
6563 * Drop packet if shorter than we expect.
6565 if (m
->m_pkthdr
.len
< len
) {
6566 ipstat
.ips_tooshort
++;
6570 /* Checks out, proceed */
6581 * Same as above, but for IPv6.
6582 * Cut-and-pasted from ip6_input.c.
6583 * XXX Should we update ip6stat, or not?
6586 bridge_ip6_checkbasic(struct mbuf
**mp
)
6588 struct mbuf
*m
= *mp
;
6589 struct ip6_hdr
*ip6
;
6592 * If the IPv6 header is not aligned, slurp it up into a new
6593 * mbuf with space for link headers, in the event we forward
6594 * it. Otherwise, if it is aligned, make sure the entire base
6595 * IPv6 header is in the first mbuf of the chain.
6597 if (IP6_HDR_ALIGNED_P(mtod(m
, caddr_t
)) == 0) {
6598 struct ifnet
*inifp
= m
->m_pkthdr
.rcvif
;
6599 /* max_linkhdr is already rounded up to nearest 4-byte */
6600 if ((m
= m_copyup(m
, sizeof(struct ip6_hdr
),
6601 max_linkhdr
)) == NULL
) {
6602 /* XXXJRT new stat, please */
6603 ip6stat
.ip6s_toosmall
++;
6604 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
6607 } else if (OS_EXPECT((size_t)m
->m_len
< sizeof(struct ip6_hdr
), 0)) {
6608 struct ifnet
*inifp
= m
->m_pkthdr
.rcvif
;
6609 if ((m
= m_pullup(m
, sizeof(struct ip6_hdr
))) == NULL
) {
6610 ip6stat
.ip6s_toosmall
++;
6611 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
6616 ip6
= mtod(m
, struct ip6_hdr
*);
6618 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
6619 ip6stat
.ip6s_badvers
++;
6620 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_hdrerr
);
6624 /* Checks out, proceed */
6638 * Return a fragmented mbuf chain.
6641 bridge_fragment(struct ifnet
*ifp
, struct mbuf
*m
, struct ether_header
*eh
,
6642 int snap
, struct llc
*llc
)
6648 if (m
->m_len
< sizeof(struct ip
) &&
6649 (m
= m_pullup(m
, sizeof(struct ip
))) == NULL
) {
6652 ip
= mtod(m
, struct ip
*);
6654 error
= ip_fragment(ip
, &m
, ifp
->if_mtu
, ifp
->if_hwassist
,
6660 /* walk the chain and re-add the Ethernet header */
6661 for (m0
= m
; m0
; m0
= m0
->m_nextpkt
) {
6664 M_PREPEND(m0
, sizeof(struct llc
), M_DONTWAIT
, 0);
6669 bcopy(llc
, mtod(m0
, caddr_t
),
6670 sizeof(struct llc
));
6672 M_PREPEND(m0
, ETHER_HDR_LEN
, M_DONTWAIT
, 0);
6677 bcopy(eh
, mtod(m0
, caddr_t
), ETHER_HDR_LEN
);
6684 ipstat
.ips_fragmented
++;
6695 #endif /* PFIL_HOOKS */
6698 * bridge_set_bpf_tap:
6700 * Sets ups the BPF callbacks.
6703 bridge_set_bpf_tap(ifnet_t ifp
, bpf_tap_mode mode
, bpf_packet_func bpf_callback
)
6705 struct bridge_softc
*sc
= (struct bridge_softc
*)ifnet_softc(ifp
);
6708 if (sc
== NULL
|| (sc
->sc_flags
& SCF_DETACHING
)) {
6712 case BPF_TAP_DISABLE
:
6713 sc
->sc_bpf_input
= sc
->sc_bpf_output
= NULL
;
6717 sc
->sc_bpf_input
= bpf_callback
;
6720 case BPF_TAP_OUTPUT
:
6721 sc
->sc_bpf_output
= bpf_callback
;
6724 case BPF_TAP_INPUT_OUTPUT
:
6725 sc
->sc_bpf_input
= sc
->sc_bpf_output
= bpf_callback
;
6738 * Callback when interface has been detached.
6741 bridge_detach(ifnet_t ifp
)
6743 struct bridge_softc
*sc
= (struct bridge_softc
*)ifnet_softc(ifp
);
6746 bstp_detach(&sc
->sc_stp
);
6747 #endif /* BRIDGESTP */
6749 /* Tear down the routing table. */
6750 bridge_rtable_fini(sc
);
6752 lck_mtx_lock(&bridge_list_mtx
);
6753 LIST_REMOVE(sc
, sc_list
);
6754 lck_mtx_unlock(&bridge_list_mtx
);
6758 lck_mtx_destroy(&sc
->sc_mtx
, bridge_lock_grp
);
6759 if_clone_softc_deallocate(&bridge_cloner
, sc
);
6765 * Invoke the input BPF callback if enabled
6768 bridge_bpf_input(ifnet_t ifp
, struct mbuf
*m
, const char * func
, int line
)
6770 struct bridge_softc
*sc
= (struct bridge_softc
*)ifnet_softc(ifp
);
6771 bpf_packet_func input_func
= sc
->sc_bpf_input
;
6773 if (input_func
!= NULL
) {
6774 if (mbuf_pkthdr_rcvif(m
) != ifp
) {
6775 printf("%s.%d: rcvif: 0x%llx != ifp 0x%llx\n", func
, line
,
6776 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_pkthdr_rcvif(m
)),
6777 (uint64_t)VM_KERNEL_ADDRPERM(ifp
));
6779 (*input_func
)(ifp
, m
);
6785 * bridge_bpf_output:
6787 * Invoke the output BPF callback if enabled
6790 bridge_bpf_output(ifnet_t ifp
, struct mbuf
*m
)
6792 struct bridge_softc
*sc
= (struct bridge_softc
*)ifnet_softc(ifp
);
6793 bpf_packet_func output_func
= sc
->sc_bpf_output
;
6795 if (output_func
!= NULL
) {
6796 (*output_func
)(ifp
, m
);
6802 * bridge_link_event:
6804 * Report a data link event on an interface
6807 bridge_link_event(struct ifnet
*ifp
, u_int32_t event_code
)
6810 struct kern_event_msg header
;
6812 char if_name
[IFNAMSIZ
];
6816 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE
)) {
6817 printf("%s: %s event_code %u - %s\n", __func__
, ifp
->if_xname
,
6818 event_code
, dlil_kev_dl_code_str(event_code
));
6820 #endif /* BRIDGE_DEBUG */
6822 bzero(&event
, sizeof(event
));
6823 event
.header
.total_size
= sizeof(event
);
6824 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
6825 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
6826 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
6827 event
.header
.event_code
= event_code
;
6828 event
.header
.event_data
[0] = ifnet_family(ifp
);
6829 event
.unit
= (u_int32_t
)ifnet_unit(ifp
);
6830 strlcpy(event
.if_name
, ifnet_name(ifp
), IFNAMSIZ
);
6831 ifnet_event(ifp
, &event
.header
);
6834 #define BRIDGE_HF_DROP(reason, func, line) { \
6835 bridge_hostfilter_stats.reason++; \
6836 if (IF_BRIDGE_DEBUG(BR_DBGF_HOSTFILTER)) { \
6837 printf("%s.%d" #reason, func, line); \
6843 * Make sure this is a DHCP or Bootp request that match the host filter
6846 bridge_dhcp_filter(struct bridge_iflist
*bif
, struct mbuf
*m
, size_t offset
)
6852 * Note: We use the dhcp structure because bootp structure definition
6853 * is larger and some vendors do not pad the request
6855 error
= mbuf_copydata(m
, offset
, sizeof(struct dhcp
), &dhcp
);
6857 BRIDGE_HF_DROP(brhf_dhcp_too_small
, __func__
, __LINE__
);
6860 if (dhcp
.dp_op
!= BOOTREQUEST
) {
6861 BRIDGE_HF_DROP(brhf_dhcp_bad_op
, __func__
, __LINE__
);
6865 * The hardware address must be an exact match
6867 if (dhcp
.dp_htype
!= ARPHRD_ETHER
) {
6868 BRIDGE_HF_DROP(brhf_dhcp_bad_htype
, __func__
, __LINE__
);
6871 if (dhcp
.dp_hlen
!= ETHER_ADDR_LEN
) {
6872 BRIDGE_HF_DROP(brhf_dhcp_bad_hlen
, __func__
, __LINE__
);
6875 if (bcmp(dhcp
.dp_chaddr
, bif
->bif_hf_hwsrc
,
6876 ETHER_ADDR_LEN
) != 0) {
6877 BRIDGE_HF_DROP(brhf_dhcp_bad_chaddr
, __func__
, __LINE__
);
6881 * Client address must match the host address or be not specified
6883 if (dhcp
.dp_ciaddr
.s_addr
!= bif
->bif_hf_ipsrc
.s_addr
&&
6884 dhcp
.dp_ciaddr
.s_addr
!= INADDR_ANY
) {
6885 BRIDGE_HF_DROP(brhf_dhcp_bad_ciaddr
, __func__
, __LINE__
);
6894 bridge_host_filter(struct bridge_iflist
*bif
, mbuf_t
*data
)
6897 struct ether_header
*eh
;
6898 static struct in_addr inaddr_any
= { .s_addr
= INADDR_ANY
};
6901 eh
= mtod(m
, struct ether_header
*);
6904 * Restrict the source hardware address
6906 if ((bif
->bif_flags
& BIFF_HF_HWSRC
) == 0 ||
6907 bcmp(eh
->ether_shost
, bif
->bif_hf_hwsrc
,
6908 ETHER_ADDR_LEN
) != 0) {
6909 BRIDGE_HF_DROP(brhf_bad_ether_srchw_addr
, __func__
, __LINE__
);
6914 * Restrict Ethernet protocols to ARP and IP
6916 if (eh
->ether_type
== htons(ETHERTYPE_ARP
)) {
6917 struct ether_arp
*ea
;
6918 size_t minlen
= sizeof(struct ether_header
) +
6919 sizeof(struct ether_arp
);
6922 * Make the Ethernet and ARP headers contiguous
6924 if (mbuf_pkthdr_len(m
) < minlen
) {
6925 BRIDGE_HF_DROP(brhf_arp_too_small
, __func__
, __LINE__
);
6928 if (mbuf_len(m
) < minlen
&& mbuf_pullup(data
, minlen
) != 0) {
6929 BRIDGE_HF_DROP(brhf_arp_pullup_failed
,
6930 __func__
, __LINE__
);
6936 * Verify this is an ethernet/ip arp
6938 eh
= mtod(m
, struct ether_header
*);
6939 ea
= (struct ether_arp
*)(eh
+ 1);
6940 if (ea
->arp_hrd
!= htons(ARPHRD_ETHER
)) {
6941 BRIDGE_HF_DROP(brhf_arp_bad_hw_type
,
6942 __func__
, __LINE__
);
6945 if (ea
->arp_pro
!= htons(ETHERTYPE_IP
)) {
6946 BRIDGE_HF_DROP(brhf_arp_bad_pro_type
,
6947 __func__
, __LINE__
);
6951 * Verify the address lengths are correct
6953 if (ea
->arp_hln
!= ETHER_ADDR_LEN
) {
6954 BRIDGE_HF_DROP(brhf_arp_bad_hw_len
, __func__
, __LINE__
);
6957 if (ea
->arp_pln
!= sizeof(struct in_addr
)) {
6958 BRIDGE_HF_DROP(brhf_arp_bad_pro_len
,
6959 __func__
, __LINE__
);
6964 * Allow only ARP request or ARP reply
6966 if (ea
->arp_op
!= htons(ARPOP_REQUEST
) &&
6967 ea
->arp_op
!= htons(ARPOP_REPLY
)) {
6968 BRIDGE_HF_DROP(brhf_arp_bad_op
, __func__
, __LINE__
);
6972 * Verify source hardware address matches
6974 if (bcmp(ea
->arp_sha
, bif
->bif_hf_hwsrc
,
6975 ETHER_ADDR_LEN
) != 0) {
6976 BRIDGE_HF_DROP(brhf_arp_bad_sha
, __func__
, __LINE__
);
6980 * Verify source protocol address:
6981 * May be null for an ARP probe
6983 if (bcmp(ea
->arp_spa
, &bif
->bif_hf_ipsrc
.s_addr
,
6984 sizeof(struct in_addr
)) != 0 &&
6985 bcmp(ea
->arp_spa
, &inaddr_any
,
6986 sizeof(struct in_addr
)) != 0) {
6987 BRIDGE_HF_DROP(brhf_arp_bad_spa
, __func__
, __LINE__
);
6990 bridge_hostfilter_stats
.brhf_arp_ok
+= 1;
6992 } else if (eh
->ether_type
== htons(ETHERTYPE_IP
)) {
6993 size_t minlen
= sizeof(struct ether_header
) + sizeof(struct ip
);
6998 * Make the Ethernet and IP headers contiguous
7000 if (mbuf_pkthdr_len(m
) < minlen
) {
7001 BRIDGE_HF_DROP(brhf_ip_too_small
, __func__
, __LINE__
);
7004 offset
= sizeof(struct ether_header
);
7005 error
= mbuf_copydata(m
, offset
, sizeof(struct ip
), &iphdr
);
7007 BRIDGE_HF_DROP(brhf_ip_too_small
, __func__
, __LINE__
);
7011 * Verify the source IP address
7013 if (iphdr
.ip_p
== IPPROTO_UDP
) {
7016 minlen
+= sizeof(struct udphdr
);
7017 if (mbuf_pkthdr_len(m
) < minlen
) {
7018 BRIDGE_HF_DROP(brhf_ip_too_small
,
7019 __func__
, __LINE__
);
7024 * Allow all zero addresses for DHCP requests
7026 if (iphdr
.ip_src
.s_addr
!= bif
->bif_hf_ipsrc
.s_addr
&&
7027 iphdr
.ip_src
.s_addr
!= INADDR_ANY
) {
7028 BRIDGE_HF_DROP(brhf_ip_bad_srcaddr
,
7029 __func__
, __LINE__
);
7032 offset
= sizeof(struct ether_header
) +
7033 (IP_VHL_HL(iphdr
.ip_vhl
) << 2);
7034 error
= mbuf_copydata(m
, offset
,
7035 sizeof(struct udphdr
), &udp
);
7037 BRIDGE_HF_DROP(brhf_ip_too_small
,
7038 __func__
, __LINE__
);
7042 * Either it's a Bootp/DHCP packet that we like or
7043 * it's a UDP packet from the host IP as source address
7045 if (udp
.uh_sport
== htons(IPPORT_BOOTPC
) &&
7046 udp
.uh_dport
== htons(IPPORT_BOOTPS
)) {
7047 minlen
+= sizeof(struct dhcp
);
7048 if (mbuf_pkthdr_len(m
) < minlen
) {
7049 BRIDGE_HF_DROP(brhf_ip_too_small
,
7050 __func__
, __LINE__
);
7053 offset
+= sizeof(struct udphdr
);
7054 error
= bridge_dhcp_filter(bif
, m
, offset
);
7058 } else if (iphdr
.ip_src
.s_addr
== INADDR_ANY
) {
7059 BRIDGE_HF_DROP(brhf_ip_bad_srcaddr
,
7060 __func__
, __LINE__
);
7063 } else if (iphdr
.ip_src
.s_addr
!= bif
->bif_hf_ipsrc
.s_addr
||
7064 bif
->bif_hf_ipsrc
.s_addr
== INADDR_ANY
) {
7065 BRIDGE_HF_DROP(brhf_ip_bad_srcaddr
, __func__
, __LINE__
);
7069 * Allow only boring IP protocols
7071 if (iphdr
.ip_p
!= IPPROTO_TCP
&&
7072 iphdr
.ip_p
!= IPPROTO_UDP
&&
7073 iphdr
.ip_p
!= IPPROTO_ICMP
&&
7074 iphdr
.ip_p
!= IPPROTO_ESP
&&
7075 iphdr
.ip_p
!= IPPROTO_AH
&&
7076 iphdr
.ip_p
!= IPPROTO_GRE
) {
7077 BRIDGE_HF_DROP(brhf_ip_bad_proto
, __func__
, __LINE__
);
7080 bridge_hostfilter_stats
.brhf_ip_ok
+= 1;
7083 BRIDGE_HF_DROP(brhf_bad_ether_type
, __func__
, __LINE__
);
7088 if (IF_BRIDGE_DEBUG(BR_DBGF_HOSTFILTER
)) {
7090 printf_mbuf_data(m
, 0,
7091 sizeof(struct ether_header
) +
7109 bridge_mac_nat_enable(struct bridge_softc
*sc
, struct bridge_iflist
*bif
)
7113 BRIDGE_LOCK_ASSERT_HELD(sc
);
7115 if (sc
->sc_mac_nat_bif
!= NULL
) {
7116 if (sc
->sc_mac_nat_bif
!= bif
) {
7121 sc
->sc_mac_nat_bif
= bif
;
7122 bif
->bif_ifflags
|= IFBIF_MAC_NAT
;
7123 bridge_mac_nat_populate_entries(sc
);
7130 bridge_mac_nat_disable(struct bridge_softc
*sc
)
7132 struct bridge_iflist
*mac_nat_bif
= sc
->sc_mac_nat_bif
;
7134 assert(mac_nat_bif
!= NULL
);
7135 bridge_mac_nat_flush_entries(sc
, mac_nat_bif
);
7136 mac_nat_bif
->bif_ifflags
&= ~IFBIF_MAC_NAT
;
7137 sc
->sc_mac_nat_bif
= NULL
;
7142 mac_nat_entry_print2(struct mac_nat_entry
*mne
,
7143 char *ifname
, const char *msg1
, const char *msg2
)
7147 char ntopbuf
[MAX_IPv6_STR_LEN
];
7150 af
= ((mne
->mne_flags
& MNE_FLAGS_IPV6
) != 0) ? AF_INET6
: AF_INET
;
7151 ether_ntop(etopbuf
, sizeof(etopbuf
), mne
->mne_mac
);
7152 (void)inet_ntop(af
, &mne
->mne_u
, ntopbuf
, sizeof(ntopbuf
));
7159 printf("%s %s%s%s %p (%s, %s, %s)\n",
7160 ifname
, msg1
, space
, msg2
, mne
, mne
->mne_bif
->bif_ifp
->if_xname
,
7165 mac_nat_entry_print(struct mac_nat_entry
*mne
,
7166 char *ifname
, const char *msg
)
7168 mac_nat_entry_print2(mne
, ifname
, msg
, NULL
);
7171 static struct mac_nat_entry
*
7172 bridge_lookup_mac_nat_entry(struct bridge_softc
*sc
, int af
, void * ip
)
7174 struct mac_nat_entry
*mne
;
7175 struct mac_nat_entry
*ret_mne
= NULL
;
7177 if (af
== AF_INET
) {
7178 in_addr_t s_addr
= ((struct in_addr
*)ip
)->s_addr
;
7180 LIST_FOREACH(mne
, &sc
->sc_mne_list
, mne_list
) {
7181 if (mne
->mne_ip
.s_addr
== s_addr
) {
7182 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7183 mac_nat_entry_print(mne
, sc
->sc_if_xname
,
7191 const struct in6_addr
*ip6
= (const struct in6_addr
*)ip
;
7193 LIST_FOREACH(mne
, &sc
->sc_mne_list_v6
, mne_list
) {
7194 if (IN6_ARE_ADDR_EQUAL(&mne
->mne_ip6
, ip6
)) {
7195 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7196 mac_nat_entry_print(mne
, sc
->sc_if_xname
,
7208 bridge_destroy_mac_nat_entry(struct bridge_softc
*sc
,
7209 struct mac_nat_entry
*mne
, const char *reason
)
7211 LIST_REMOVE(mne
, mne_list
);
7212 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7213 mac_nat_entry_print(mne
, sc
->sc_if_xname
, reason
);
7215 zfree(bridge_mne_pool
, mne
);
7219 static struct mac_nat_entry
*
7220 bridge_create_mac_nat_entry(struct bridge_softc
*sc
,
7221 struct bridge_iflist
*bif
, int af
, const void *ip
, uint8_t *eaddr
)
7223 struct mac_nat_entry_list
*list
;
7224 struct mac_nat_entry
*mne
;
7226 if (sc
->sc_mne_count
>= sc
->sc_mne_max
) {
7227 sc
->sc_mne_allocation_failures
++;
7230 mne
= zalloc_noblock(bridge_mne_pool
);
7232 sc
->sc_mne_allocation_failures
++;
7236 bzero(mne
, sizeof(*mne
));
7237 bcopy(eaddr
, mne
->mne_mac
, sizeof(mne
->mne_mac
));
7239 if (af
== AF_INET
) {
7240 bcopy(ip
, &mne
->mne_ip
, sizeof(mne
->mne_ip
));
7241 list
= &sc
->sc_mne_list
;
7243 bcopy(ip
, &mne
->mne_ip6
, sizeof(mne
->mne_ip6
));
7244 mne
->mne_flags
|= MNE_FLAGS_IPV6
;
7245 list
= &sc
->sc_mne_list_v6
;
7247 LIST_INSERT_HEAD(list
, mne
, mne_list
);
7248 mne
->mne_expire
= (unsigned long)net_uptime() + sc
->sc_brttimeout
;
7249 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7250 mac_nat_entry_print(mne
, sc
->sc_if_xname
, "created");
7255 static struct mac_nat_entry
*
7256 bridge_update_mac_nat_entry(struct bridge_softc
*sc
,
7257 struct bridge_iflist
*bif
, int af
, void *ip
, uint8_t *eaddr
)
7259 struct mac_nat_entry
*mne
;
7261 mne
= bridge_lookup_mac_nat_entry(sc
, af
, ip
);
7263 struct bridge_iflist
*mac_nat_bif
= sc
->sc_mac_nat_bif
;
7265 if (mne
->mne_bif
== mac_nat_bif
) {
7266 /* the MAC NAT interface takes precedence */
7267 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7268 if (mne
->mne_bif
!= bif
) {
7269 mac_nat_entry_print2(mne
,
7270 sc
->sc_if_xname
, "reject",
7271 bif
->bif_ifp
->if_xname
);
7274 } else if (mne
->mne_bif
!= bif
) {
7275 const char *old_if
= mne
->mne_bif
->bif_ifp
->if_xname
;
7278 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7279 mac_nat_entry_print2(mne
,
7280 sc
->sc_if_xname
, "replaced",
7283 bcopy(eaddr
, mne
->mne_mac
, sizeof(mne
->mne_mac
));
7285 mne
->mne_expire
= (unsigned long)net_uptime() +
7288 mne
= bridge_create_mac_nat_entry(sc
, bif
, af
, ip
, eaddr
);
7294 bridge_mac_nat_flush_entries_common(struct bridge_softc
*sc
,
7295 struct mac_nat_entry_list
*list
, struct bridge_iflist
*bif
)
7297 struct mac_nat_entry
*mne
;
7298 struct mac_nat_entry
*tmne
;
7300 LIST_FOREACH_SAFE(mne
, list
, mne_list
, tmne
) {
7301 if (bif
!= NULL
&& mne
->mne_bif
!= bif
) {
7304 bridge_destroy_mac_nat_entry(sc
, mne
, "flushed");
7309 * bridge_mac_nat_flush_entries:
7311 * Flush MAC NAT entries for the specified member. Flush all entries if
7312 * the member is the one that requires MAC NAT, otherwise just flush the
7313 * ones for the specified member.
7316 bridge_mac_nat_flush_entries(struct bridge_softc
*sc
, struct bridge_iflist
* bif
)
7318 struct bridge_iflist
*flush_bif
;
7320 flush_bif
= (bif
== sc
->sc_mac_nat_bif
) ? NULL
: bif
;
7321 bridge_mac_nat_flush_entries_common(sc
, &sc
->sc_mne_list
, flush_bif
);
7322 bridge_mac_nat_flush_entries_common(sc
, &sc
->sc_mne_list_v6
, flush_bif
);
7326 bridge_mac_nat_populate_entries(struct bridge_softc
*sc
)
7331 struct bridge_iflist
*mac_nat_bif
= sc
->sc_mac_nat_bif
;
7333 assert(mac_nat_bif
!= NULL
);
7334 ifp
= mac_nat_bif
->bif_ifp
;
7335 error
= ifnet_get_address_list(ifp
, &list
);
7337 printf("%s: ifnet_get_address_list(%s) failed %d\n",
7338 __func__
, ifp
->if_xname
, error
);
7341 for (ifaddr_t
*scan
= list
; *scan
!= NULL
; scan
++) {
7347 struct sockaddr_in sin
;
7348 struct sockaddr_in6 sin6
;
7350 af
= ifaddr_address_family(*scan
);
7354 error
= ifaddr_address(*scan
, &u
.sa
, sizeof(u
));
7356 printf("%s: ifaddr_address failed %d\n",
7360 if (af
== AF_INET
) {
7361 ip
= (void *)&u
.sin
.sin_addr
;
7363 if (IN6_IS_ADDR_LINKLOCAL(&u
.sin6
.sin6_addr
)) {
7364 /* remove scope ID */
7365 u
.sin6
.sin6_addr
.s6_addr16
[1] = 0;
7367 ip
= (void *)&u
.sin6
.sin6_addr
;
7369 bridge_create_mac_nat_entry(sc
, mac_nat_bif
, af
, ip
,
7370 (uint8_t *)IF_LLADDR(ifp
));
7376 ifnet_free_address_list(list
);
7381 bridge_mac_nat_age_entries_common(struct bridge_softc
*sc
,
7382 struct mac_nat_entry_list
*list
, unsigned long now
)
7384 struct mac_nat_entry
*mne
;
7385 struct mac_nat_entry
*tmne
;
7387 LIST_FOREACH_SAFE(mne
, list
, mne_list
, tmne
) {
7388 if (now
>= mne
->mne_expire
) {
7389 bridge_destroy_mac_nat_entry(sc
, mne
, "aged out");
7395 bridge_mac_nat_age_entries(struct bridge_softc
*sc
, unsigned long now
)
7397 if (sc
->sc_mac_nat_bif
== NULL
) {
7400 bridge_mac_nat_age_entries_common(sc
, &sc
->sc_mne_list
, now
);
7401 bridge_mac_nat_age_entries_common(sc
, &sc
->sc_mne_list_v6
, now
);
7405 get_in_out_string(boolean_t is_output
)
7407 return is_output
? "OUT" : "IN";
7411 * is_valid_arp_packet:
7412 * Verify that this is a valid ARP packet.
7414 * Returns TRUE if the packet is valid, FALSE otherwise.
7417 is_valid_arp_packet(mbuf_t
*data
, boolean_t is_output
,
7418 struct ether_header
**eh_p
, struct ether_arp
**ea_p
)
7420 struct ether_arp
*ea
;
7421 struct ether_header
*eh
;
7422 size_t minlen
= sizeof(struct ether_header
) + sizeof(struct ether_arp
);
7423 boolean_t is_valid
= FALSE
;
7424 int flags
= is_output
? BR_DBGF_OUTPUT
: BR_DBGF_INPUT
;
7426 if (mbuf_pkthdr_len(*data
) < minlen
) {
7427 if (IF_BRIDGE_DEBUG(flags
)) {
7428 printf("%s: ARP %s short frame %lu < %lu\n",
7430 get_in_out_string(is_output
),
7431 mbuf_pkthdr_len(*data
), minlen
);
7435 if (mbuf_len(*data
) < minlen
&& mbuf_pullup(data
, minlen
) != 0) {
7436 if (IF_BRIDGE_DEBUG(flags
)) {
7437 printf("%s: ARP %s size %lu mbuf_pullup fail\n",
7439 get_in_out_string(is_output
),
7446 /* validate ARP packet */
7447 eh
= mtod(*data
, struct ether_header
*);
7448 ea
= (struct ether_arp
*)(eh
+ 1);
7449 if (ntohs(ea
->arp_hrd
) != ARPHRD_ETHER
) {
7450 if (IF_BRIDGE_DEBUG(flags
)) {
7451 printf("%s: ARP %s htype not ethernet\n",
7453 get_in_out_string(is_output
));
7457 if (ea
->arp_hln
!= ETHER_ADDR_LEN
) {
7458 if (IF_BRIDGE_DEBUG(flags
)) {
7459 printf("%s: ARP %s hlen not ethernet\n",
7461 get_in_out_string(is_output
));
7465 if (ntohs(ea
->arp_pro
) != ETHERTYPE_IP
) {
7466 if (IF_BRIDGE_DEBUG(flags
)) {
7467 printf("%s: ARP %s ptype not IP\n",
7469 get_in_out_string(is_output
));
7473 if (ea
->arp_pln
!= sizeof(struct in_addr
)) {
7474 if (IF_BRIDGE_DEBUG(flags
)) {
7475 printf("%s: ARP %s plen not IP\n",
7477 get_in_out_string(is_output
));
7488 static struct mac_nat_entry
*
7489 bridge_mac_nat_arp_input(struct bridge_softc
*sc
, mbuf_t
*data
)
7491 struct ether_arp
*ea
;
7492 struct ether_header
*eh
;
7493 struct mac_nat_entry
*mne
= NULL
;
7497 if (!is_valid_arp_packet(data
, FALSE
, &eh
, &ea
)) {
7500 op
= ntohs(ea
->arp_op
);
7504 /* only care about REQUEST and REPLY */
7510 /* check the target IP address for a NAT entry */
7511 bcopy(ea
->arp_tpa
, &tpa
, sizeof(tpa
));
7512 if (tpa
.s_addr
!= 0) {
7513 mne
= bridge_lookup_mac_nat_entry(sc
, AF_INET
, &tpa
);
7516 if (op
== ARPOP_REPLY
) {
7517 /* translate the MAC address */
7518 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7522 ether_ntop(mac_src
, sizeof(mac_src
),
7524 ether_ntop(mac_dst
, sizeof(mac_dst
),
7526 printf("%s %s ARP %s -> %s\n",
7528 mne
->mne_bif
->bif_ifp
->if_xname
,
7531 bcopy(mne
->mne_mac
, ea
->arp_tha
, sizeof(ea
->arp_tha
));
7534 /* handle conflicting ARP (sender matches mne) */
7537 bcopy(ea
->arp_spa
, &spa
, sizeof(spa
));
7538 if (spa
.s_addr
!= 0 && spa
.s_addr
!= tpa
.s_addr
) {
7539 /* check the source IP for a NAT entry */
7540 mne
= bridge_lookup_mac_nat_entry(sc
, AF_INET
, &spa
);
7549 bridge_mac_nat_arp_output(struct bridge_softc
*sc
,
7550 struct bridge_iflist
*bif
, mbuf_t
*data
, struct mac_nat_record
*mnr
)
7552 struct ether_arp
*ea
;
7553 struct ether_header
*eh
;
7555 struct mac_nat_entry
*mne
= NULL
;
7557 boolean_t translate
= FALSE
;
7559 if (!is_valid_arp_packet(data
, TRUE
, &eh
, &ea
)) {
7562 op
= ntohs(ea
->arp_op
);
7566 /* only care about REQUEST and REPLY */
7572 bcopy(ea
->arp_spa
, &ip
, sizeof(ip
));
7573 if (ip
.s_addr
== 0) {
7576 /* XXX validate IP address: no multicast/broadcast */
7577 mne
= bridge_update_mac_nat_entry(sc
, bif
, AF_INET
, &ip
, ea
->arp_sha
);
7578 if (mnr
!= NULL
&& mne
!= NULL
) {
7579 /* record the offset to do the replacement */
7581 mnr
->mnr_arp_offset
= (char *)ea
->arp_sha
- (char *)eh
;
7588 #define ETHER_IPV4_HEADER_LEN (sizeof(struct ether_header) + \
7589 + sizeof(struct ip))
7590 static struct ether_header
*
7591 get_ether_ip_header(mbuf_t
*data
, boolean_t is_output
)
7593 struct ether_header
*eh
= NULL
;
7594 int flags
= is_output
? BR_DBGF_OUTPUT
: BR_DBGF_INPUT
;
7595 size_t minlen
= ETHER_IPV4_HEADER_LEN
;
7597 if (mbuf_pkthdr_len(*data
) < minlen
) {
7598 if (IF_BRIDGE_DEBUG(flags
)) {
7599 printf("%s: IP %s short frame %lu < %lu\n",
7601 get_in_out_string(is_output
),
7602 mbuf_pkthdr_len(*data
), minlen
);
7606 if (mbuf_len(*data
) < minlen
&& mbuf_pullup(data
, minlen
) != 0) {
7607 if (IF_BRIDGE_DEBUG(flags
)) {
7608 printf("%s: IP %s size %lu mbuf_pullup fail\n",
7610 get_in_out_string(is_output
),
7616 eh
= mtod(*data
, struct ether_header
*);
7621 static struct mac_nat_entry
*
7622 bridge_mac_nat_ip_input(struct bridge_softc
*sc
, mbuf_t
*data
)
7625 struct ether_header
*eh
;
7627 struct mac_nat_entry
*mne
= NULL
;
7629 eh
= get_ether_ip_header(data
, FALSE
);
7633 iphdr
= (struct ip
*)(void *)(eh
+ 1);
7634 bcopy(&iphdr
->ip_dst
, &dst
, sizeof(dst
));
7635 /* XXX validate IP address */
7636 if (dst
.s_addr
== 0) {
7639 mne
= bridge_lookup_mac_nat_entry(sc
, AF_INET
, &dst
);
7645 bridge_mac_nat_udp_output(struct bridge_softc
*sc
,
7646 struct bridge_iflist
*bif
, mbuf_t m
,
7647 uint8_t ip_header_len
, struct mac_nat_record
*mnr
)
7652 struct udphdr udphdr
;
7654 /* copy the UDP header */
7655 offset
= sizeof(struct ether_header
) + ip_header_len
;
7656 error
= mbuf_copydata(m
, offset
, sizeof(struct udphdr
), &udphdr
);
7658 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7659 printf("%s: mbuf_copydata udphdr failed %d",
7664 if (ntohs(udphdr
.uh_sport
) != IPPORT_BOOTPC
||
7665 ntohs(udphdr
.uh_dport
) != IPPORT_BOOTPS
) {
7666 /* not a BOOTP/DHCP packet */
7669 /* check whether the broadcast bit is already set */
7670 offset
+= sizeof(struct udphdr
) + offsetof(struct dhcp
, dp_flags
);
7671 error
= mbuf_copydata(m
, offset
, sizeof(dp_flags
), &dp_flags
);
7673 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7674 printf("%s: mbuf_copydata dp_flags failed %d",
7679 if ((ntohs(dp_flags
) & DHCP_FLAGS_BROADCAST
) != 0) {
7680 /* it's already set, nothing to do */
7683 /* broadcast bit needs to be set */
7684 mnr
->mnr_ip_dhcp_flags
= dp_flags
| htons(DHCP_FLAGS_BROADCAST
);
7685 mnr
->mnr_ip_header_len
= ip_header_len
;
7686 if (udphdr
.uh_sum
!= 0) {
7689 /* adjust checksum to take modified dp_flags into account */
7690 delta
= dp_flags
- mnr
->mnr_ip_dhcp_flags
;
7691 mnr
->mnr_ip_udp_csum
= udphdr
.uh_sum
+ delta
;
7693 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7694 printf("%s %s DHCP dp_flags 0x%x UDP cksum 0x%x\n",
7696 bif
->bif_ifp
->if_xname
,
7697 ntohs(mnr
->mnr_ip_dhcp_flags
),
7698 ntohs(mnr
->mnr_ip_udp_csum
));
7704 bridge_mac_nat_ip_output(struct bridge_softc
*sc
,
7705 struct bridge_iflist
*bif
, mbuf_t
*data
, struct mac_nat_record
*mnr
)
7708 struct ether_header
*eh
;
7711 uint8_t ip_header_len
;
7712 struct mac_nat_entry
*mne
= NULL
;
7713 boolean_t translate
= FALSE
;
7715 eh
= get_ether_ip_header(data
, TRUE
);
7719 iphdr
= (struct ip
*)(void *)(eh
+ 1);
7720 ip_header_len
= IP_VHL_HL(iphdr
->ip_vhl
) << 2;
7721 if (ip_header_len
< sizeof(ip
)) {
7722 /* bogus IP header */
7725 bcopy(&iphdr
->ip_src
, &ip
, sizeof(ip
));
7726 /* XXX validate the source address */
7727 if (ip
.s_addr
!= 0) {
7728 mne
= bridge_update_mac_nat_entry(sc
, bif
, AF_INET
, &ip
,
7732 if (iphdr
->ip_p
== IPPROTO_UDP
) {
7733 /* handle DHCP must broadcast */
7734 bridge_mac_nat_udp_output(sc
, bif
, *data
,
7735 ip_header_len
, mnr
);
7743 #define ETHER_IPV6_HEADER_LEN (sizeof(struct ether_header) + \
7744 + sizeof(struct ip6_hdr))
7745 static struct ether_header
*
7746 get_ether_ipv6_header(mbuf_t
*data
, boolean_t is_output
)
7748 struct ether_header
*eh
= NULL
;
7749 int flags
= is_output
? BR_DBGF_OUTPUT
: BR_DBGF_INPUT
;
7750 size_t minlen
= ETHER_IPV6_HEADER_LEN
;
7752 if (mbuf_pkthdr_len(*data
) < minlen
) {
7753 if (IF_BRIDGE_DEBUG(flags
)) {
7754 printf("%s: IP %s short frame %lu < %lu\n",
7756 get_in_out_string(is_output
),
7757 mbuf_pkthdr_len(*data
), minlen
);
7761 if (mbuf_len(*data
) < minlen
&& mbuf_pullup(data
, minlen
) != 0) {
7762 if (IF_BRIDGE_DEBUG(flags
)) {
7763 printf("%s: IP %s size %lu mbuf_pullup fail\n",
7765 get_in_out_string(is_output
),
7771 eh
= mtod(*data
, struct ether_header
*);
7778 bridge_mac_nat_icmpv6_input(struct bridge_softc
*sc
, mbuf_t
*data
,
7779 struct ether_header
*eh
, struct ip6_hdr
*hdr
)
7782 #pragma unused(data)
7789 #include <netinet/icmp6.h>
7790 #include <netinet6/nd6.h>
7792 #define ETHER_ND_LLADDR_LEN (ETHER_ADDR_LEN + sizeof(struct nd_opt_hdr))
7795 bridge_mac_nat_icmpv6_output(struct bridge_softc
*sc
, struct bridge_iflist
*bif
,
7796 mbuf_t
*data
, struct ether_header
*eh
,
7797 struct ip6_hdr
*ip6h
, struct in6_addr
*saddrp
, struct mac_nat_record
*mnr
)
7799 struct icmp6_hdr
*icmp6
;
7800 unsigned int icmp6len
;
7802 char *lladdr
= NULL
;
7804 unsigned int off
= sizeof(*ip6h
);
7806 icmp6len
= m
->m_pkthdr
.len
- sizeof(*eh
) - off
;
7807 if (icmp6len
< sizeof(*icmp6
)) {
7808 printf("%s: short packet %d < %lu\n", __func__
,
7809 icmp6len
, sizeof(*icmp6
));
7812 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6h
+ off
);
7813 switch (icmp6
->icmp6_type
) {
7814 case ND_NEIGHBOR_SOLICIT
: {
7815 struct nd_neighbor_solicit
*nd_ns
;
7816 union nd_opts ndopts
;
7817 boolean_t is_dad_probe
;
7818 struct in6_addr taddr
;
7820 if (icmp6len
< sizeof(*nd_ns
)) {
7821 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7822 printf("%s: short nd_ns %d < %lu\n", __func__
,
7823 icmp6len
, sizeof(*nd_ns
));
7828 nd_ns
= (struct nd_neighbor_solicit
*)(void *)icmp6
;
7829 bcopy(&nd_ns
->nd_ns_target
, &taddr
, sizeof(taddr
));
7830 if (IN6_IS_ADDR_MULTICAST(&taddr
) ||
7831 IN6_IS_ADDR_UNSPECIFIED(&taddr
)) {
7832 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7833 printf("%s: invalid target ignored\n", __func__
);
7838 nd6_option_init(nd_ns
+ 1, icmp6len
- sizeof(*nd_ns
), &ndopts
);
7839 if (nd6_options(&ndopts
) < 0) {
7840 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7841 printf("%s: invalid ND6 NS option\n", __func__
);
7845 if (ndopts
.nd_opts_src_lladdr
!= NULL
) {
7846 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
7847 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
7849 is_dad_probe
= IN6_IS_ADDR_UNSPECIFIED(saddrp
);
7850 if (lladdr
!= NULL
) {
7852 printf("%s: bad ND6 DAD packet\n", __func__
);
7855 if (lladdrlen
!= ETHER_ND_LLADDR_LEN
) {
7856 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7857 printf("%s: source lladdrlen %d != %lu\n",
7859 lladdrlen
, ETHER_ND_LLADDR_LEN
);
7863 mnr
->mnr_ip6_lladdr_offset
= (void *)lladdr
-
7865 mnr
->mnr_ip6_icmp6_len
= icmp6len
;
7866 mnr
->mnr_ip6_icmp6_type
= icmp6
->icmp6_type
;
7867 mnr
->mnr_ip6_header_len
= off
;
7870 /* node is trying use taddr, create an mne using taddr */
7875 case ND_NEIGHBOR_ADVERT
: {
7876 struct nd_neighbor_advert
*nd_na
;
7877 union nd_opts ndopts
;
7878 struct in6_addr taddr
;
7881 nd_na
= (struct nd_neighbor_advert
*)(void *)icmp6
;
7883 if (icmp6len
< sizeof(*nd_na
)) {
7884 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7885 printf("%s: short nd_na %d < %lu\n", __func__
,
7886 icmp6len
, sizeof(*nd_na
));
7891 bcopy(&nd_na
->nd_na_target
, &taddr
, sizeof(taddr
));
7892 if (IN6_IS_ADDR_MULTICAST(&taddr
) ||
7893 IN6_IS_ADDR_UNSPECIFIED(&taddr
)) {
7894 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7895 printf("%s: invalid target ignored\n", __func__
);
7900 nd6_option_init(nd_na
+ 1, icmp6len
- sizeof(*nd_na
), &ndopts
);
7901 if (nd6_options(&ndopts
) < 0) {
7902 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7903 printf("%s: invalid ND6 NA option\n", __func__
);
7907 if (ndopts
.nd_opts_tgt_lladdr
== NULL
) {
7908 /* target linklayer, nothing to do */
7911 lladdr
= (char *)(ndopts
.nd_opts_tgt_lladdr
+ 1);
7912 lladdrlen
= ndopts
.nd_opts_tgt_lladdr
->nd_opt_len
<< 3;
7913 if (lladdrlen
!= ETHER_ND_LLADDR_LEN
) {
7914 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7915 printf("%s: target lladdrlen %d != %lu\n",
7916 __func__
, lladdrlen
, ETHER_ND_LLADDR_LEN
);
7920 mnr
->mnr_ip6_lladdr_offset
= (void *)lladdr
- (void *)eh
;
7921 mnr
->mnr_ip6_icmp6_len
= icmp6len
;
7922 mnr
->mnr_ip6_header_len
= off
;
7923 mnr
->mnr_ip6_icmp6_type
= icmp6
->icmp6_type
;
7926 case ND_ROUTER_SOLICIT
: {
7927 struct nd_router_solicit
*nd_rs
;
7928 union nd_opts ndopts
;
7930 if (icmp6len
< sizeof(*nd_rs
)) {
7931 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7932 printf("%s: short nd_rs %d < %lu\n", __func__
,
7933 icmp6len
, sizeof(*nd_rs
));
7937 nd_rs
= (struct nd_router_solicit
*)(void *)icmp6
;
7940 nd6_option_init(nd_rs
+ 1, icmp6len
- sizeof(*nd_rs
), &ndopts
);
7941 if (nd6_options(&ndopts
) < 0) {
7942 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7943 printf("%s: invalid ND6 RS option\n", __func__
);
7947 if (ndopts
.nd_opts_src_lladdr
!= NULL
) {
7948 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
7949 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
7951 if (lladdr
!= NULL
) {
7952 if (lladdrlen
!= ETHER_ND_LLADDR_LEN
) {
7953 if (IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7954 printf("%s: source lladdrlen %d != %lu\n",
7956 lladdrlen
, ETHER_ND_LLADDR_LEN
);
7960 mnr
->mnr_ip6_lladdr_offset
= (void *)lladdr
-
7962 mnr
->mnr_ip6_icmp6_len
= icmp6len
;
7963 mnr
->mnr_ip6_icmp6_type
= icmp6
->icmp6_type
;
7964 mnr
->mnr_ip6_header_len
= off
;
7971 if (mnr
->mnr_ip6_lladdr_offset
!= 0 &&
7972 IF_BRIDGE_DEBUG(BR_DBGF_MAC_NAT
)) {
7975 switch (mnr
->mnr_ip6_icmp6_type
) {
7976 case ND_ROUTER_SOLICIT
:
7977 str
= "ROUTER SOLICIT";
7979 case ND_NEIGHBOR_ADVERT
:
7980 str
= "NEIGHBOR ADVERT";
7982 case ND_NEIGHBOR_SOLICIT
:
7983 str
= "NEIGHBOR SOLICIT";
7989 printf("%s %s %s ip6len %d icmp6len %d lladdr offset %d\n",
7990 sc
->sc_if_xname
, bif
->bif_ifp
->if_xname
, str
,
7991 mnr
->mnr_ip6_header_len
,
7992 mnr
->mnr_ip6_icmp6_len
, mnr
->mnr_ip6_lladdr_offset
);
7996 static struct mac_nat_entry
*
7997 bridge_mac_nat_ipv6_input(struct bridge_softc
*sc
, mbuf_t
*data
)
7999 struct in6_addr dst
;
8000 struct ether_header
*eh
;
8001 struct ip6_hdr
*ip6h
;
8002 struct mac_nat_entry
*mne
= NULL
;
8004 eh
= get_ether_ipv6_header(data
, FALSE
);
8008 ip6h
= (struct ip6_hdr
*)(void *)(eh
+ 1);
8010 if (ip6h
->ip6_nxt
== IPPROTO_ICMPV6
) {
8011 bridge_mac_nat_icmpv6_input(sc
, data
, eh
, ip6h
);
8014 bcopy(&ip6h
->ip6_dst
, &dst
, sizeof(dst
));
8015 /* XXX validate IPv6 address */
8016 if (IN6_IS_ADDR_UNSPECIFIED(&dst
)) {
8019 mne
= bridge_lookup_mac_nat_entry(sc
, AF_INET6
, &dst
);
8026 bridge_mac_nat_ipv6_output(struct bridge_softc
*sc
,
8027 struct bridge_iflist
*bif
, mbuf_t
*data
, struct mac_nat_record
*mnr
)
8029 struct ether_header
*eh
;
8030 struct ip6_hdr
*ip6h
;
8031 struct in6_addr saddr
;
8032 boolean_t translate
;
8034 translate
= (bif
== sc
->sc_mac_nat_bif
) ? FALSE
: TRUE
;
8035 eh
= get_ether_ipv6_header(data
, TRUE
);
8040 ip6h
= (struct ip6_hdr
*)(void *)(eh
+ 1);
8041 bcopy(&ip6h
->ip6_src
, &saddr
, sizeof(saddr
));
8042 if (mnr
!= NULL
&& ip6h
->ip6_nxt
== IPPROTO_ICMPV6
) {
8043 bridge_mac_nat_icmpv6_output(sc
, bif
, data
,
8044 eh
, ip6h
, &saddr
, mnr
);
8046 if (IN6_IS_ADDR_UNSPECIFIED(&saddr
)) {
8049 (void)bridge_update_mac_nat_entry(sc
, bif
, AF_INET6
, &saddr
,
8057 * bridge_mac_nat_input:
8058 * Process a packet arriving on the MAC NAT interface (sc_mac_nat_bif).
8059 * This interface is the "external" interface with respect to NAT.
8060 * The interface is only capable of receiving a single MAC address
8061 * (e.g. a Wi-Fi STA interface).
8063 * When a packet arrives on the external interface, look up the destination
8064 * IP address in the mac_nat_entry table. If there is a match, *is_input
8065 * is set to TRUE if it's for the MAC NAT interface, otherwise *is_input
8066 * is set to FALSE and translate the MAC address if necessary.
8069 * The internal interface to direct the packet to, or NULL if the packet
8070 * should not be redirected.
8072 * *data may be updated to point at a different mbuf chain, or set to NULL
8073 * if the chain was deallocated during processing.
8076 bridge_mac_nat_input(struct bridge_softc
*sc
, mbuf_t
*data
,
8077 boolean_t
*is_input
)
8079 ifnet_t dst_if
= NULL
;
8080 struct ether_header
*eh
;
8081 uint16_t ether_type
;
8082 boolean_t is_unicast
;
8084 struct mac_nat_entry
*mne
= NULL
;
8086 BRIDGE_LOCK_ASSERT_HELD(sc
);
8088 assert(sc
->sc_mac_nat_bif
!= NULL
);
8089 is_unicast
= ((m
->m_flags
& (M_BCAST
| M_MCAST
)) == 0);
8090 eh
= mtod(m
, struct ether_header
*);
8091 ether_type
= ntohs(eh
->ether_type
);
8092 switch (ether_type
) {
8094 mne
= bridge_mac_nat_arp_input(sc
, data
);
8098 mne
= bridge_mac_nat_ip_input(sc
, data
);
8101 case ETHERTYPE_IPV6
:
8103 mne
= bridge_mac_nat_ipv6_input(sc
, data
);
8112 /* it may have changed */
8113 eh
= mtod(*data
, struct ether_header
*);
8115 bcopy(mne
->mne_mac
, eh
->ether_dhost
,
8116 sizeof(eh
->ether_dhost
));
8118 dst_if
= mne
->mne_bif
->bif_ifp
;
8119 *is_input
= (mne
->mne_bif
== sc
->sc_mac_nat_bif
);
8125 * bridge_mac_nat_output:
8126 * Process a packet destined to the MAC NAT interface (sc_mac_nat_bif)
8127 * from the interface 'bif'.
8129 * Create a mac_nat_entry containing the source IP address and MAC address
8130 * from the packet. Populate a mac_nat_record with information detailing
8131 * how to translate the packet. Translation takes place later when
8132 * the bridge lock is no longer held.
8134 * If 'bif' == sc_mac_nat_bif, the stack over the MAC NAT
8135 * interface is generating an output packet. No translation is required in this
8136 * case, we just record the IP address used to prevent another bif from
8137 * claiming our IP address.
8140 * TRUE if the packet should be translated (*mnr updated as well),
8143 * *data may be updated to point at a different mbuf chain or NULL if
8144 * the chain was deallocated during processing.
8148 bridge_mac_nat_output(struct bridge_softc
*sc
,
8149 struct bridge_iflist
*bif
, mbuf_t
*data
, struct mac_nat_record
*mnr
)
8151 struct ether_header
*eh
;
8152 uint16_t ether_type
;
8153 boolean_t translate
= FALSE
;
8155 BRIDGE_LOCK_ASSERT_HELD(sc
);
8156 assert(sc
->sc_mac_nat_bif
!= NULL
);
8158 eh
= mtod(*data
, struct ether_header
*);
8159 ether_type
= ntohs(eh
->ether_type
);
8161 bzero(mnr
, sizeof(*mnr
));
8162 mnr
->mnr_ether_type
= ether_type
;
8164 switch (ether_type
) {
8166 translate
= bridge_mac_nat_arp_output(sc
, bif
, data
, mnr
);
8169 translate
= bridge_mac_nat_ip_output(sc
, bif
, data
, mnr
);
8171 case ETHERTYPE_IPV6
:
8172 translate
= bridge_mac_nat_ipv6_output(sc
, bif
, data
, mnr
);
8181 bridge_mac_nat_arp_translate(mbuf_t
*data
, struct mac_nat_record
*mnr
,
8182 const caddr_t eaddr
)
8186 if (mnr
->mnr_arp_offset
== 0) {
8189 /* replace the source hardware address */
8190 error
= mbuf_copyback(*data
, mnr
->mnr_arp_offset
,
8191 ETHER_ADDR_LEN
, eaddr
,
8194 printf("%s: mbuf_copyback failed\n",
8203 bridge_mac_nat_ip_translate(mbuf_t
*data
, struct mac_nat_record
*mnr
)
8208 if (mnr
->mnr_ip_header_len
== 0) {
8211 /* update the UDP checksum */
8212 offset
= sizeof(struct ether_header
) + mnr
->mnr_ip_header_len
;
8213 error
= mbuf_copyback(*data
, offset
+ offsetof(struct udphdr
, uh_sum
),
8214 sizeof(mnr
->mnr_ip_udp_csum
),
8215 &mnr
->mnr_ip_udp_csum
,
8218 printf("%s: mbuf_copyback uh_sum failed\n",
8223 /* update the DHCP must broadcast flag */
8224 offset
+= sizeof(struct udphdr
);
8225 error
= mbuf_copyback(*data
, offset
+ offsetof(struct dhcp
, dp_flags
),
8226 sizeof(mnr
->mnr_ip_dhcp_flags
),
8227 &mnr
->mnr_ip_dhcp_flags
,
8230 printf("%s: mbuf_copyback dp_flags failed\n",
8238 bridge_mac_nat_ipv6_translate(mbuf_t
*data
, struct mac_nat_record
*mnr
,
8239 const caddr_t eaddr
)
8245 if (mnr
->mnr_ip6_header_len
== 0) {
8248 switch (mnr
->mnr_ip6_icmp6_type
) {
8249 case ND_ROUTER_SOLICIT
:
8250 case ND_NEIGHBOR_SOLICIT
:
8251 case ND_NEIGHBOR_ADVERT
:
8252 if (mnr
->mnr_ip6_lladdr_offset
== 0) {
8262 * replace the lladdr
8264 error
= mbuf_copyback(m
, mnr
->mnr_ip6_lladdr_offset
,
8265 ETHER_ADDR_LEN
, eaddr
,
8268 printf("%s: mbuf_copyback lladdr failed\n",
8276 * recompute the icmp6 checksum
8279 /* skip past the ethernet header */
8280 mbuf_setdata(m
, (char *)mbuf_data(m
) + ETHER_HDR_LEN
,
8281 mbuf_len(m
) - ETHER_HDR_LEN
);
8282 mbuf_pkthdr_adjustlen(m
, -ETHER_HDR_LEN
);
8284 #define CKSUM_OFFSET_ICMP6 offsetof(struct icmp6_hdr, icmp6_cksum)
8285 /* set the checksum to zero */
8287 error
= mbuf_copyback(m
, mnr
->mnr_ip6_header_len
+ CKSUM_OFFSET_ICMP6
,
8288 sizeof(cksum
), &cksum
, MBUF_DONTWAIT
);
8290 printf("%s: mbuf_copyback cksum=0 failed\n",
8296 /* compute and set the new checksum */
8297 cksum
= in6_cksum(m
, IPPROTO_ICMPV6
, mnr
->mnr_ip6_header_len
,
8298 mnr
->mnr_ip6_icmp6_len
);
8299 error
= mbuf_copyback(m
, mnr
->mnr_ip6_header_len
+ CKSUM_OFFSET_ICMP6
,
8300 sizeof(cksum
), &cksum
, MBUF_DONTWAIT
);
8302 printf("%s: mbuf_copyback cksum failed\n",
8308 /* restore the ethernet header */
8309 mbuf_setdata(m
, (char *)mbuf_data(m
) - ETHER_HDR_LEN
,
8310 mbuf_len(m
) + ETHER_HDR_LEN
);
8311 mbuf_pkthdr_adjustlen(m
, ETHER_HDR_LEN
);
8316 bridge_mac_nat_translate(mbuf_t
*data
, struct mac_nat_record
*mnr
,
8317 const caddr_t eaddr
)
8319 struct ether_header
*eh
;
8321 /* replace the source ethernet address with the single MAC */
8322 eh
= mtod(*data
, struct ether_header
*);
8323 bcopy(eaddr
, eh
->ether_shost
, sizeof(eh
->ether_shost
));
8324 switch (mnr
->mnr_ether_type
) {
8326 bridge_mac_nat_arp_translate(data
, mnr
, eaddr
);
8330 bridge_mac_nat_ip_translate(data
, mnr
);
8333 case ETHERTYPE_IPV6
:
8334 bridge_mac_nat_ipv6_translate(data
, mnr
, eaddr
);
8344 * bridge packet filtering
8348 * the PF routines expect to be called from ip_input, so we
8349 * need to do and undo here some of the same processing.
8351 * XXX : this is heavily inspired on bridge_pfil()
8355 bridge_pf(struct mbuf
**mp
, struct ifnet
*ifp
, uint32_t sc_filter_flags
, int input
)
8358 * XXX : mpetit : heavily inspired by bridge_pfil()
8361 int snap
, error
, i
, hlen
;
8362 struct ether_header
*eh1
, eh2
;
8365 u_int16_t ether_type
;
8368 error
= -1; /* Default error if not error == 0 */
8370 if ((sc_filter_flags
& IFBF_FILT_MEMBER
) == 0) {
8371 return 0; /* filtering is disabled */
8373 i
= min((*mp
)->m_pkthdr
.len
, max_protohdr
);
8374 if ((*mp
)->m_len
< i
) {
8375 *mp
= m_pullup(*mp
, i
);
8377 printf("%s: m_pullup failed\n", __func__
);
8382 eh1
= mtod(*mp
, struct ether_header
*);
8383 ether_type
= ntohs(eh1
->ether_type
);
8386 * Check for SNAP/LLC.
8388 if (ether_type
< ETHERMTU
) {
8389 struct llc
*llc2
= (struct llc
*)(eh1
+ 1);
8391 if ((*mp
)->m_len
>= ETHER_HDR_LEN
+ 8 &&
8392 llc2
->llc_dsap
== LLC_SNAP_LSAP
&&
8393 llc2
->llc_ssap
== LLC_SNAP_LSAP
&&
8394 llc2
->llc_control
== LLC_UI
) {
8395 ether_type
= htons(llc2
->llc_un
.type_snap
.ether_type
);
8401 * If we're trying to filter bridge traffic, don't look at anything
8402 * other than IP and ARP traffic. If the filter doesn't understand
8403 * IPv6, don't allow IPv6 through the bridge either. This is lame
8404 * since if we really wanted, say, an AppleTalk filter, we are hosed,
8405 * but of course we don't have an AppleTalk filter to begin with.
8406 * (Note that since pfil doesn't understand ARP it will pass *ALL*
8409 switch (ether_type
) {
8411 case ETHERTYPE_REVARP
:
8412 return 0; /* Automatically pass */
8415 case ETHERTYPE_IPV6
:
8419 * Check to see if the user wants to pass non-ip
8420 * packets, these will not be checked by pf and
8421 * passed unconditionally so the default is to drop.
8423 if ((sc_filter_flags
& IFBF_FILT_ONLYIP
)) {
8429 /* Strip off the Ethernet header and keep a copy. */
8430 m_copydata(*mp
, 0, ETHER_HDR_LEN
, (caddr_t
)&eh2
);
8431 m_adj(*mp
, ETHER_HDR_LEN
);
8433 /* Strip off snap header, if present */
8435 m_copydata(*mp
, 0, sizeof(struct llc
), (caddr_t
)&llc1
);
8436 m_adj(*mp
, sizeof(struct llc
));
8440 * Check the IP header for alignment and errors
8442 switch (ether_type
) {
8444 error
= bridge_ip_checkbasic(mp
);
8446 case ETHERTYPE_IPV6
:
8447 error
= bridge_ip6_checkbasic(mp
);
8460 * Run the packet through pf rules
8462 switch (ether_type
) {
8465 * before calling the firewall, swap fields the same as
8466 * IP does. here we assume the header is contiguous
8468 ip
= mtod(*mp
, struct ip
*);
8470 ip
->ip_len
= ntohs(ip
->ip_len
);
8471 ip
->ip_off
= ntohs(ip
->ip_off
);
8474 error
= pf_af_hook(ifp
, 0, mp
, AF_INET
, input
, NULL
);
8477 if (*mp
== NULL
|| error
!= 0) { /* filter may consume */
8481 /* Recalculate the ip checksum and restore byte ordering */
8482 ip
= mtod(*mp
, struct ip
*);
8483 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
8484 if (hlen
< (int)sizeof(struct ip
)) {
8487 if (hlen
> (*mp
)->m_len
) {
8488 if ((*mp
= m_pullup(*mp
, hlen
)) == 0) {
8491 ip
= mtod(*mp
, struct ip
*);
8496 ip
->ip_len
= htons(ip
->ip_len
);
8497 ip
->ip_off
= htons(ip
->ip_off
);
8499 if (hlen
== sizeof(struct ip
)) {
8500 ip
->ip_sum
= in_cksum_hdr(ip
);
8502 ip
->ip_sum
= in_cksum(*mp
, hlen
);
8506 case ETHERTYPE_IPV6
:
8508 error
= pf_af_hook(ifp
, 0, mp
, AF_INET6
, input
, NULL
);
8511 if (*mp
== NULL
|| error
!= 0) { /* filter may consume */
8530 * Finally, put everything back the way it was and return
8533 M_PREPEND(*mp
, sizeof(struct llc
), M_DONTWAIT
, 0);
8537 bcopy(&llc1
, mtod(*mp
, caddr_t
), sizeof(struct llc
));
8540 M_PREPEND(*mp
, ETHER_HDR_LEN
, M_DONTWAIT
, 0);
8544 bcopy(&eh2
, mtod(*mp
, caddr_t
), ETHER_HDR_LEN
);