xref: /xnu-8796.101.5/bsd/net/if_bridge.c (revision aca3beaa3dfbd42498b42c5e5ce20a938e6554e5)
1 /*
2  * Copyright (c) 2004-2023 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /*	$NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $	*/
30 /*
31  * Copyright 2001 Wasabi Systems, Inc.
32  * All rights reserved.
33  *
34  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
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
50  *    written permission.
51  *
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.
63  */
64 
65 /*
66  * Copyright (c) 1999, 2000 Jason L. Wright ([email protected])
67  * All rights reserved.
68  *
69  * Redistribution and use in source and binary forms, with or without
70  * modification, are permitted provided that the following conditions
71  * are met:
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.
77  *
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.
89  *
90  * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp
91  */
92 
93 /*
94  * Network interface bridge support.
95  *
96  * TODO:
97  *
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).
102  *
103  *	- GIF isn't handled due to the lack of IPPROTO_ETHERIP support.
104  */
105 
106 #include <sys/cdefs.h>
107 
108 #include <sys/param.h>
109 #include <sys/mbuf.h>
110 #include <sys/malloc.h>
111 #include <sys/protosw.h>
112 #include <sys/systm.h>
113 #include <sys/time.h>
114 #include <sys/socket.h> /* for net/if.h */
115 #include <sys/sockio.h>
116 #include <sys/kernel.h>
117 #include <sys/random.h>
118 #include <sys/syslog.h>
119 #include <sys/sysctl.h>
120 #include <sys/proc.h>
121 #include <sys/lock.h>
122 #include <sys/mcache.h>
123 
124 #include <sys/kauth.h>
125 
126 #include <kern/thread_call.h>
127 
128 #include <libkern/libkern.h>
129 
130 #include <kern/zalloc.h>
131 
132 #if NBPFILTER > 0
133 #include <net/bpf.h>
134 #endif
135 #include <net/if.h>
136 #include <net/if_dl.h>
137 #include <net/if_types.h>
138 #include <net/if_var.h>
139 #include <net/if_media.h>
140 #include <net/net_api_stats.h>
141 #include <net/pfvar.h>
142 
143 #include <netinet/in.h> /* for struct arpcom */
144 #include <netinet/tcp.h> /* for struct tcphdr */
145 #include <netinet/in_systm.h>
146 #include <netinet/in_var.h>
147 #define _IP_VHL
148 #include <netinet/ip.h>
149 #include <netinet/ip_var.h>
150 #include <netinet/ip6.h>
151 #include <netinet6/ip6_var.h>
152 #ifdef DEV_CARP
153 #include <netinet/ip_carp.h>
154 #endif
155 #include <netinet/if_ether.h> /* for struct arpcom */
156 #include <net/bridgestp.h>
157 #include <net/if_bridgevar.h>
158 #include <net/if_llc.h>
159 #if NVLAN > 0
160 #include <net/if_vlan_var.h>
161 #endif /* NVLAN > 0 */
162 
163 #include <net/if_ether.h>
164 #include <net/dlil.h>
165 #include <net/kpi_interfacefilter.h>
166 
167 #include <net/route.h>
168 #include <dev/random/randomdev.h>
169 
170 #include <netinet/bootp.h>
171 #include <netinet/dhcp.h>
172 
173 #if SKYWALK
174 #include <skywalk/nexus/netif/nx_netif.h>
175 #endif /* SKYWALK */
176 
177 #include <os/log.h>
178 
179 /*
180  * if_bridge_debug, BR_DBGF_*
181  * - 'if_bridge_debug' is a bitmask of BR_DBGF_* flags that can be set
182  *   to enable additional logs for the corresponding bridge function
183  * - "sysctl net.link.bridge.debug" controls the value of
184  *   'if_bridge_debug'
185  */
186 static uint32_t if_bridge_debug = 0;
187 #define BR_DBGF_LIFECYCLE       0x0001
188 #define BR_DBGF_INPUT           0x0002
189 #define BR_DBGF_OUTPUT          0x0004
190 #define BR_DBGF_RT_TABLE        0x0008
191 #define BR_DBGF_DELAYED_CALL    0x0010
192 #define BR_DBGF_IOCTL           0x0020
193 #define BR_DBGF_MBUF            0x0040
194 #define BR_DBGF_MCAST           0x0080
195 #define BR_DBGF_HOSTFILTER      0x0100
196 #define BR_DBGF_CHECKSUM        0x0200
197 #define BR_DBGF_MAC_NAT         0x0400
198 
199 /*
200  * if_bridge_log_level
201  * - 'if_bridge_log_level' ensures that by default important logs are
202  *   logged regardless of if_bridge_debug by comparing the log level
203  *   in BRIDGE_LOG to if_bridge_log_level
204  * - use "sysctl net.link.bridge.log_level" controls the value of
205  *   'if_bridge_log_level'
206  * - the default value of 'if_bridge_log_level' is LOG_NOTICE; important
207  *   logs must use LOG_NOTICE to ensure they appear by default
208  */
209 static int if_bridge_log_level = LOG_NOTICE;
210 
211 #define BRIDGE_DBGF_ENABLED(__flag)     ((if_bridge_debug & __flag) != 0)
212 
213 /*
214  * BRIDGE_LOG, BRIDGE_LOG_SIMPLE
215  * - macros to generate the specified log conditionally based on
216  *   the specified log level and debug flags
217  * - BRIDGE_LOG_SIMPLE does not include the function name in the log
218  */
219 #define BRIDGE_LOG(__level, __dbgf, __string, ...)              \
220 	do {                                                            \
221 	        if (__level <= if_bridge_log_level ||                   \
222 	            BRIDGE_DBGF_ENABLED(__dbgf)) {                      \
223 	                os_log(OS_LOG_DEFAULT, "%s: " __string, \
224 	                       __func__, ## __VA_ARGS__);       \
225 	        }                                                       \
226 	} while (0)
227 #define BRIDGE_LOG_SIMPLE(__level, __dbgf, __string, ...)               \
228 	do {                                                    \
229 	        if (__level <= if_bridge_log_level ||           \
230 	            BRIDGE_DBGF_ENABLED(__dbgf)) {                      \
231 	                os_log(OS_LOG_DEFAULT, __string, ## __VA_ARGS__); \
232 	        }                                                               \
233 	} while (0)
234 
235 #define _BRIDGE_LOCK(_sc)               lck_mtx_lock(&(_sc)->sc_mtx)
236 #define _BRIDGE_UNLOCK(_sc)             lck_mtx_unlock(&(_sc)->sc_mtx)
237 #define BRIDGE_LOCK_ASSERT_HELD(_sc)            \
238 	LCK_MTX_ASSERT(&(_sc)->sc_mtx, LCK_MTX_ASSERT_OWNED)
239 #define BRIDGE_LOCK_ASSERT_NOTHELD(_sc)         \
240 	LCK_MTX_ASSERT(&(_sc)->sc_mtx, LCK_MTX_ASSERT_NOTOWNED)
241 
242 #define BRIDGE_LOCK_DEBUG      1
243 #if BRIDGE_LOCK_DEBUG
244 
245 #define BR_LCKDBG_MAX                   4
246 
247 #define BRIDGE_LOCK(_sc)                bridge_lock(_sc)
248 #define BRIDGE_UNLOCK(_sc)              bridge_unlock(_sc)
249 #define BRIDGE_LOCK2REF(_sc, _err)      _err = bridge_lock2ref(_sc)
250 #define BRIDGE_UNREF(_sc)               bridge_unref(_sc)
251 #define BRIDGE_XLOCK(_sc)               bridge_xlock(_sc)
252 #define BRIDGE_XDROP(_sc)               bridge_xdrop(_sc)
253 
254 #else /* !BRIDGE_LOCK_DEBUG */
255 
256 #define BRIDGE_LOCK(_sc)                _BRIDGE_LOCK(_sc)
257 #define BRIDGE_UNLOCK(_sc)              _BRIDGE_UNLOCK(_sc)
258 #define BRIDGE_LOCK2REF(_sc, _err)      do {                            \
259 	BRIDGE_LOCK_ASSERT_HELD(_sc);                                   \
260 	if ((_sc)->sc_iflist_xcnt > 0)                                  \
261 	        (_err) = EBUSY;                                         \
262 	else {                                                          \
263 	        (_sc)->sc_iflist_ref++;                                 \
264 	        (_err) = 0;                                             \
265 	}                                                               \
266 	_BRIDGE_UNLOCK(_sc);                                            \
267 } while (0)
268 #define BRIDGE_UNREF(_sc)               do {                            \
269 	_BRIDGE_LOCK(_sc);                                              \
270 	(_sc)->sc_iflist_ref--;                                         \
271 	if (((_sc)->sc_iflist_xcnt > 0) && ((_sc)->sc_iflist_ref == 0))	{ \
272 	        _BRIDGE_UNLOCK(_sc);                                    \
273 	        wakeup(&(_sc)->sc_cv);                                  \
274 	} else                                                          \
275 	        _BRIDGE_UNLOCK(_sc);                                    \
276 } while (0)
277 #define BRIDGE_XLOCK(_sc)               do {                            \
278 	BRIDGE_LOCK_ASSERT_HELD(_sc);                                   \
279 	(_sc)->sc_iflist_xcnt++;                                        \
280 	while ((_sc)->sc_iflist_ref > 0)                                \
281 	        msleep(&(_sc)->sc_cv, &(_sc)->sc_mtx, PZERO,            \
282 	            "BRIDGE_XLOCK", NULL);                              \
283 } while (0)
284 #define BRIDGE_XDROP(_sc)               do {                            \
285 	BRIDGE_LOCK_ASSERT_HELD(_sc);                                   \
286 	(_sc)->sc_iflist_xcnt--;                                        \
287 } while (0)
288 
289 #endif /* BRIDGE_LOCK_DEBUG */
290 
291 #if NBPFILTER > 0
292 #define BRIDGE_BPF_MTAP_INPUT(sc, m)                                    \
293 	if (sc->sc_bpf_input != NULL)                                   \
294 	        bridge_bpf_input(sc->sc_ifp, m, __func__, __LINE__)
295 #else /* NBPFILTER */
296 #define BRIDGE_BPF_MTAP_INPUT(ifp, m)
297 #endif /* NBPFILTER */
298 
299 /*
300  * Initial size of the route hash table.  Must be a power of two.
301  */
302 #ifndef BRIDGE_RTHASH_SIZE
303 #define BRIDGE_RTHASH_SIZE              16
304 #endif
305 
306 /*
307  * Maximum size of the routing hash table
308  */
309 #define BRIDGE_RTHASH_SIZE_MAX          2048
310 
311 #define BRIDGE_RTHASH_MASK(sc)          ((sc)->sc_rthash_size - 1)
312 
313 /*
314  * Maximum number of addresses to cache.
315  */
316 #ifndef BRIDGE_RTABLE_MAX
317 #define BRIDGE_RTABLE_MAX               100
318 #endif
319 
320 
321 /*
322  * Timeout (in seconds) for entries learned dynamically.
323  */
324 #ifndef BRIDGE_RTABLE_TIMEOUT
325 #define BRIDGE_RTABLE_TIMEOUT           (20 * 60)       /* same as ARP */
326 #endif
327 
328 /*
329  * Number of seconds between walks of the route list.
330  */
331 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
332 #define BRIDGE_RTABLE_PRUNE_PERIOD      (5 * 60)
333 #endif
334 
335 /*
336  * Number of MAC NAT entries
337  * - sized based on 16 clients (including MAC NAT interface)
338  *   each with 4 addresses
339  */
340 #ifndef BRIDGE_MAC_NAT_ENTRY_MAX
341 #define BRIDGE_MAC_NAT_ENTRY_MAX        64
342 #endif /* BRIDGE_MAC_NAT_ENTRY_MAX */
343 
344 /*
345  * List of capabilities to possibly mask on the member interface.
346  */
347 #define BRIDGE_IFCAPS_MASK              (IFCAP_TSO | IFCAP_TXCSUM)
348 /*
349  * List of capabilities to disable on the member interface.
350  */
351 #define BRIDGE_IFCAPS_STRIP             IFCAP_LRO
352 
353 /*
354  * Bridge interface list entry.
355  */
356 struct bridge_iflist {
357 	TAILQ_ENTRY(bridge_iflist) bif_next;
358 	struct ifnet            *bif_ifp;       /* member if */
359 	struct bstp_port        bif_stp;        /* STP state */
360 	uint32_t                bif_ifflags;    /* member if flags */
361 	int                     bif_savedcaps;  /* saved capabilities */
362 	uint32_t                bif_addrmax;    /* max # of addresses */
363 	uint32_t                bif_addrcnt;    /* cur. # of addresses */
364 	uint32_t                bif_addrexceeded; /* # of address violations */
365 
366 	interface_filter_t      bif_iff_ref;
367 	struct bridge_softc     *bif_sc;
368 	uint32_t                bif_flags;
369 
370 	/* host filter */
371 	struct in_addr          bif_hf_ipsrc;
372 	uint8_t                 bif_hf_hwsrc[ETHER_ADDR_LEN];
373 
374 	struct ifbrmstats       bif_stats;
375 };
376 
377 static inline bool
bif_ifflags_are_set(struct bridge_iflist * bif,uint32_t flags)378 bif_ifflags_are_set(struct bridge_iflist * bif, uint32_t flags)
379 {
380 	return (bif->bif_ifflags & flags) == flags;
381 }
382 
383 static inline bool
bif_has_checksum_offload(struct bridge_iflist * bif)384 bif_has_checksum_offload(struct bridge_iflist * bif)
385 {
386 	return bif_ifflags_are_set(bif, IFBIF_CHECKSUM_OFFLOAD);
387 }
388 
389 /* fake errors to make the code clearer */
390 #define _EBADIP                 EJUSTRETURN
391 #define _EBADIPCHECKSUM         EJUSTRETURN
392 #define _EBADIPV6               EJUSTRETURN
393 #define _EBADUDP                EJUSTRETURN
394 #define _EBADTCP                EJUSTRETURN
395 #define _EBADUDPCHECKSUM        EJUSTRETURN
396 #define _EBADTCPCHECKSUM        EJUSTRETURN
397 
398 #define BIFF_PROMISC            0x01    /* promiscuous mode set */
399 #define BIFF_PROTO_ATTACHED     0x02    /* protocol attached */
400 #define BIFF_FILTER_ATTACHED    0x04    /* interface filter attached */
401 #define BIFF_MEDIA_ACTIVE       0x08    /* interface media active */
402 #define BIFF_HOST_FILTER        0x10    /* host filter enabled */
403 #define BIFF_HF_HWSRC           0x20    /* host filter source MAC is set */
404 #define BIFF_HF_IPSRC           0x40    /* host filter source IP is set */
405 #define BIFF_INPUT_BROADCAST    0x80    /* send broadcast packets in */
406 #define BIFF_IN_MEMBER_LIST     0x100   /* added to the member list */
407 #define BIFF_WIFI_INFRA         0x200   /* interface is Wi-Fi infra */
408 #define BIFF_ALL_MULTI          0x400   /* allmulti set */
409 #if SKYWALK
410 #define BIFF_FLOWSWITCH_ATTACHED 0x1000   /* we attached the flowswitch */
411 #define BIFF_NETAGENT_REMOVED    0x2000   /* we removed the netagent */
412 #endif /* SKYWALK */
413 
414 /*
415  * mac_nat_entry
416  * - translates between an IP address and MAC address on a specific
417  *   bridge interface member
418  */
419 struct mac_nat_entry {
420 	LIST_ENTRY(mac_nat_entry) mne_list;     /* list linkage */
421 	struct bridge_iflist    *mne_bif;       /* originating interface */
422 	unsigned long           mne_expire;     /* expiration time */
423 	union {
424 		struct in_addr  mneu_ip;        /* originating IPv4 address */
425 		struct in6_addr mneu_ip6;       /* originating IPv6 address */
426 	} mne_u;
427 	uint8_t                 mne_mac[ETHER_ADDR_LEN];
428 	uint8_t                 mne_flags;
429 	uint8_t                 mne_reserved;
430 };
431 #define mne_ip  mne_u.mneu_ip
432 #define mne_ip6 mne_u.mneu_ip6
433 
434 #define MNE_FLAGS_IPV6          0x01    /* IPv6 address */
435 
436 LIST_HEAD(mac_nat_entry_list, mac_nat_entry);
437 
438 /*
439  * mac_nat_record
440  * - used by bridge_mac_nat_output() to convey the translation that needs
441  *   to take place in bridge_mac_nat_translate
442  * - holds enough information so that the translation can be done later without
443  *   holding the bridge lock
444  */
445 struct mac_nat_record {
446 	uint16_t                mnr_ether_type;
447 	union {
448 		uint16_t        mnru_arp_offset;
449 		struct {
450 			uint16_t mnruip_dhcp_flags;
451 			uint16_t mnruip_udp_csum;
452 			uint8_t  mnruip_header_len;
453 		} mnru_ip;
454 		struct {
455 			uint16_t mnruip6_icmp6_len;
456 			uint16_t mnruip6_lladdr_offset;
457 			uint8_t mnruip6_icmp6_type;
458 			uint8_t mnruip6_header_len;
459 		} mnru_ip6;
460 	} mnr_u;
461 };
462 
463 #define mnr_arp_offset  mnr_u.mnru_arp_offset
464 
465 #define mnr_ip_header_len       mnr_u.mnru_ip.mnruip_header_len
466 #define mnr_ip_dhcp_flags       mnr_u.mnru_ip.mnruip_dhcp_flags
467 #define mnr_ip_udp_csum         mnr_u.mnru_ip.mnruip_udp_csum
468 
469 #define mnr_ip6_icmp6_len       mnr_u.mnru_ip6.mnruip6_icmp6_len
470 #define mnr_ip6_icmp6_type      mnr_u.mnru_ip6.mnruip6_icmp6_type
471 #define mnr_ip6_header_len      mnr_u.mnru_ip6.mnruip6_header_len
472 #define mnr_ip6_lladdr_offset   mnr_u.mnru_ip6.mnruip6_lladdr_offset
473 
474 /*
475  * Bridge route node.
476  */
477 struct bridge_rtnode {
478 	LIST_ENTRY(bridge_rtnode) brt_hash;     /* hash table linkage */
479 	LIST_ENTRY(bridge_rtnode) brt_list;     /* list linkage */
480 	struct bridge_iflist    *brt_dst;       /* destination if */
481 	unsigned long           brt_expire;     /* expiration time */
482 	uint8_t                 brt_flags;      /* address flags */
483 	uint8_t                 brt_addr[ETHER_ADDR_LEN];
484 	uint16_t                brt_vlan;       /* vlan id */
485 
486 };
487 #define brt_ifp                 brt_dst->bif_ifp
488 
489 /*
490  * Bridge delayed function call context
491  */
492 typedef void (*bridge_delayed_func_t)(struct bridge_softc *);
493 
494 struct bridge_delayed_call {
495 	struct bridge_softc     *bdc_sc;
496 	bridge_delayed_func_t   bdc_func; /* Function to call */
497 	struct timespec         bdc_ts; /* Time to call */
498 	u_int32_t               bdc_flags;
499 	thread_call_t           bdc_thread_call;
500 };
501 
502 #define BDCF_OUTSTANDING        0x01    /* Delayed call has been scheduled */
503 #define BDCF_CANCELLING         0x02    /* May be waiting for call completion */
504 
505 /*
506  * Software state for each bridge.
507  */
508 LIST_HEAD(_bridge_rtnode_list, bridge_rtnode);
509 
510 struct bridge_softc {
511 	struct ifnet            *sc_ifp;        /* make this an interface */
512 	u_int32_t               sc_flags;
513 	LIST_ENTRY(bridge_softc) sc_list;
514 	decl_lck_mtx_data(, sc_mtx);
515 	struct _bridge_rtnode_list *sc_rthash;  /* our forwarding table */
516 	struct _bridge_rtnode_list sc_rtlist;   /* list version of above */
517 	uint32_t                sc_rthash_key;  /* key for hash */
518 	uint32_t                sc_rthash_size; /* size of the hash table */
519 	struct bridge_delayed_call sc_aging_timer;
520 	struct bridge_delayed_call sc_resize_call;
521 	TAILQ_HEAD(, bridge_iflist) sc_spanlist;        /* span ports list */
522 	struct bstp_state       sc_stp;         /* STP state */
523 	bpf_packet_func         sc_bpf_input;
524 	bpf_packet_func         sc_bpf_output;
525 	void                    *sc_cv;
526 	uint32_t                sc_brtmax;      /* max # of addresses */
527 	uint32_t                sc_brtcnt;      /* cur. # of addresses */
528 	uint32_t                sc_brttimeout;  /* rt timeout in seconds */
529 	uint32_t                sc_iflist_ref;  /* refcount for sc_iflist */
530 	uint32_t                sc_iflist_xcnt; /* refcount for sc_iflist */
531 	TAILQ_HEAD(, bridge_iflist) sc_iflist;  /* member interface list */
532 	uint32_t                sc_brtexceeded; /* # of cache drops */
533 	uint32_t                sc_filter_flags; /* ipf and flags */
534 	struct ifnet            *sc_ifaddr;     /* member mac copied from */
535 	u_char                  sc_defaddr[6];  /* Default MAC address */
536 	char                    sc_if_xname[IFNAMSIZ];
537 
538 	struct bridge_iflist    *sc_mac_nat_bif; /* single MAC NAT interface */
539 	struct mac_nat_entry_list sc_mne_list;  /* MAC NAT IPv4 */
540 	struct mac_nat_entry_list sc_mne_list_v6;/* MAC NAT IPv6 */
541 	uint32_t                sc_mne_max;      /* max # of entries */
542 	uint32_t                sc_mne_count;    /* cur. # of entries */
543 	uint32_t                sc_mne_allocation_failures;
544 #if BRIDGE_LOCK_DEBUG
545 	/*
546 	 * Locking and unlocking calling history
547 	 */
548 	void                    *lock_lr[BR_LCKDBG_MAX];
549 	int                     next_lock_lr;
550 	void                    *unlock_lr[BR_LCKDBG_MAX];
551 	int                     next_unlock_lr;
552 #endif /* BRIDGE_LOCK_DEBUG */
553 };
554 
555 #define SCF_DETACHING            0x01
556 #define SCF_RESIZING             0x02
557 #define SCF_MEDIA_ACTIVE         0x04
558 
559 typedef enum {
560 	CHECKSUM_OPERATION_NONE = 0,
561 	CHECKSUM_OPERATION_CLEAR_OFFLOAD = 1,
562 	CHECKSUM_OPERATION_FINALIZE = 2,
563 	CHECKSUM_OPERATION_COMPUTE = 3,
564 } ChecksumOperation;
565 
566 union iphdr {
567 	struct ip *ip;
568 	struct ip6_hdr *ip6;
569 	void * ptr;
570 };
571 
572 typedef struct {
573 	u_int           ip_hlen;        /* IP header length */
574 	u_int           ip_pay_len;     /* length of payload (exclusive of ip_hlen) */
575 	u_int           ip_opt_len;     /* IPv6 options headers length */
576 	uint8_t         ip_proto;       /* IPPROTO_TCP, IPPROTO_UDP, etc. */
577 	bool            ip_is_ipv4;
578 	bool            ip_is_fragmented;
579 	union iphdr     ip_hdr;         /* pointer to IP header */
580 	void *          ip_proto_hdr;   /* ptr to protocol header (TCP) */
581 } ip_packet_info, *ip_packet_info_t;
582 
583 struct bridge_hostfilter_stats bridge_hostfilter_stats;
584 
585 static LCK_GRP_DECLARE(bridge_lock_grp, "if_bridge");
586 #if BRIDGE_LOCK_DEBUG
587 static LCK_ATTR_DECLARE(bridge_lock_attr, 0, 0);
588 #else
589 static LCK_ATTR_DECLARE(bridge_lock_attr, LCK_ATTR_DEBUG, 0);
590 #endif
591 static LCK_MTX_DECLARE_ATTR(bridge_list_mtx, &bridge_lock_grp, &bridge_lock_attr);
592 
593 static int      bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
594 
595 static KALLOC_TYPE_DEFINE(bridge_rtnode_pool, struct bridge_rtnode, NET_KT_DEFAULT);
596 static KALLOC_TYPE_DEFINE(bridge_mne_pool, struct mac_nat_entry, NET_KT_DEFAULT);
597 
598 static int      bridge_clone_create(struct if_clone *, uint32_t, void *);
599 static int      bridge_clone_destroy(struct ifnet *);
600 
601 static errno_t  bridge_ioctl(struct ifnet *, u_long, void *);
602 #if HAS_IF_CAP
603 static void     bridge_mutecaps(struct bridge_softc *);
604 static void     bridge_set_ifcap(struct bridge_softc *, struct bridge_iflist *,
605     int);
606 #endif
607 static errno_t bridge_set_tso(struct bridge_softc *);
608 static void     bridge_proto_attach_changed(struct ifnet *);
609 static int      bridge_init(struct ifnet *);
610 #if HAS_BRIDGE_DUMMYNET
611 static void     bridge_dummynet(struct mbuf *, struct ifnet *);
612 #endif
613 static void     bridge_ifstop(struct ifnet *, int);
614 static int      bridge_output(struct ifnet *, struct mbuf *);
615 static void     bridge_finalize_cksum(struct ifnet *, struct mbuf *);
616 static void     bridge_start(struct ifnet *);
617 static errno_t  bridge_input(struct ifnet *, mbuf_t *);
618 static errno_t  bridge_iff_input(void *, ifnet_t, protocol_family_t,
619     mbuf_t *, char **);
620 static errno_t  bridge_iff_output(void *, ifnet_t, protocol_family_t,
621     mbuf_t *);
622 static errno_t  bridge_member_output(struct bridge_softc *sc, ifnet_t ifp,
623     mbuf_t *m);
624 
625 static int      bridge_enqueue(ifnet_t, struct ifnet *,
626     struct ifnet *, struct mbuf *, ChecksumOperation);
627 static void     bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp, int);
628 
629 static void     bridge_forward(struct bridge_softc *, struct bridge_iflist *,
630     struct mbuf *);
631 
632 static void     bridge_aging_timer(struct bridge_softc *sc);
633 
634 static void     bridge_broadcast(struct bridge_softc *, struct bridge_iflist *,
635     struct mbuf *, int);
636 static void     bridge_span(struct bridge_softc *, struct mbuf *);
637 
638 static int      bridge_rtupdate(struct bridge_softc *, const uint8_t *,
639     uint16_t, struct bridge_iflist *, int, uint8_t);
640 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *,
641     uint16_t);
642 static void     bridge_rttrim(struct bridge_softc *);
643 static void     bridge_rtage(struct bridge_softc *);
644 static void     bridge_rtflush(struct bridge_softc *, int);
645 static int      bridge_rtdaddr(struct bridge_softc *, const uint8_t *,
646     uint16_t);
647 
648 static int      bridge_rtable_init(struct bridge_softc *);
649 static void     bridge_rtable_fini(struct bridge_softc *);
650 
651 static void     bridge_rthash_resize(struct bridge_softc *);
652 
653 static int      bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
654 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
655     const uint8_t *, uint16_t);
656 static int      bridge_rtnode_hash(struct bridge_softc *,
657     struct bridge_rtnode *);
658 static int      bridge_rtnode_insert(struct bridge_softc *,
659     struct bridge_rtnode *);
660 static void     bridge_rtnode_destroy(struct bridge_softc *,
661     struct bridge_rtnode *);
662 #if BRIDGESTP
663 static void     bridge_rtable_expire(struct ifnet *, int);
664 static void     bridge_state_change(struct ifnet *, int);
665 #endif /* BRIDGESTP */
666 
667 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
668     const char *name);
669 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
670     struct ifnet *ifp);
671 static void     bridge_delete_member(struct bridge_softc *,
672     struct bridge_iflist *);
673 static void     bridge_delete_span(struct bridge_softc *,
674     struct bridge_iflist *);
675 
676 static int      bridge_ioctl_add(struct bridge_softc *, void *);
677 static int      bridge_ioctl_del(struct bridge_softc *, void *);
678 static int      bridge_ioctl_gifflags(struct bridge_softc *, void *);
679 static int      bridge_ioctl_sifflags(struct bridge_softc *, void *);
680 static int      bridge_ioctl_scache(struct bridge_softc *, void *);
681 static int      bridge_ioctl_gcache(struct bridge_softc *, void *);
682 static int      bridge_ioctl_gifs32(struct bridge_softc *, void *);
683 static int      bridge_ioctl_gifs64(struct bridge_softc *, void *);
684 static int      bridge_ioctl_rts32(struct bridge_softc *, void *);
685 static int      bridge_ioctl_rts64(struct bridge_softc *, void *);
686 static int      bridge_ioctl_saddr32(struct bridge_softc *, void *);
687 static int      bridge_ioctl_saddr64(struct bridge_softc *, void *);
688 static int      bridge_ioctl_sto(struct bridge_softc *, void *);
689 static int      bridge_ioctl_gto(struct bridge_softc *, void *);
690 static int      bridge_ioctl_daddr32(struct bridge_softc *, void *);
691 static int      bridge_ioctl_daddr64(struct bridge_softc *, void *);
692 static int      bridge_ioctl_flush(struct bridge_softc *, void *);
693 static int      bridge_ioctl_gpri(struct bridge_softc *, void *);
694 static int      bridge_ioctl_spri(struct bridge_softc *, void *);
695 static int      bridge_ioctl_ght(struct bridge_softc *, void *);
696 static int      bridge_ioctl_sht(struct bridge_softc *, void *);
697 static int      bridge_ioctl_gfd(struct bridge_softc *, void *);
698 static int      bridge_ioctl_sfd(struct bridge_softc *, void *);
699 static int      bridge_ioctl_gma(struct bridge_softc *, void *);
700 static int      bridge_ioctl_sma(struct bridge_softc *, void *);
701 static int      bridge_ioctl_sifprio(struct bridge_softc *, void *);
702 static int      bridge_ioctl_sifcost(struct bridge_softc *, void *);
703 static int      bridge_ioctl_sifmaxaddr(struct bridge_softc *, void *);
704 static int      bridge_ioctl_addspan(struct bridge_softc *, void *);
705 static int      bridge_ioctl_delspan(struct bridge_softc *, void *);
706 static int      bridge_ioctl_gbparam32(struct bridge_softc *, void *);
707 static int      bridge_ioctl_gbparam64(struct bridge_softc *, void *);
708 static int      bridge_ioctl_grte(struct bridge_softc *, void *);
709 static int      bridge_ioctl_gifsstp32(struct bridge_softc *, void *);
710 static int      bridge_ioctl_gifsstp64(struct bridge_softc *, void *);
711 static int      bridge_ioctl_sproto(struct bridge_softc *, void *);
712 static int      bridge_ioctl_stxhc(struct bridge_softc *, void *);
713 static int      bridge_ioctl_purge(struct bridge_softc *sc, void *);
714 static int      bridge_ioctl_gfilt(struct bridge_softc *, void *);
715 static int      bridge_ioctl_sfilt(struct bridge_softc *, void *);
716 static int      bridge_ioctl_ghostfilter(struct bridge_softc *, void *);
717 static int      bridge_ioctl_shostfilter(struct bridge_softc *, void *);
718 static int      bridge_ioctl_gmnelist32(struct bridge_softc *, void *);
719 static int      bridge_ioctl_gmnelist64(struct bridge_softc *, void *);
720 static int      bridge_ioctl_gifstats32(struct bridge_softc *, void *);
721 static int      bridge_ioctl_gifstats64(struct bridge_softc *, void *);
722 
723 static int bridge_pf(struct mbuf **, struct ifnet *, uint32_t sc_filter_flags, int input);
724 static int bridge_ip_checkbasic(struct mbuf **);
725 static int bridge_ip6_checkbasic(struct mbuf **);
726 
727 static errno_t bridge_set_bpf_tap(ifnet_t, bpf_tap_mode, bpf_packet_func);
728 static errno_t bridge_bpf_input(ifnet_t, struct mbuf *, const char *, int);
729 static errno_t bridge_bpf_output(ifnet_t, struct mbuf *);
730 
731 static void bridge_detach(ifnet_t);
732 static void bridge_link_event(struct ifnet *, u_int32_t);
733 static void bridge_iflinkevent(struct ifnet *);
734 static u_int32_t bridge_updatelinkstatus(struct bridge_softc *);
735 static int interface_media_active(struct ifnet *);
736 static void bridge_schedule_delayed_call(struct bridge_delayed_call *);
737 static void bridge_cancel_delayed_call(struct bridge_delayed_call *);
738 static void bridge_cleanup_delayed_call(struct bridge_delayed_call *);
739 static int bridge_host_filter(struct bridge_iflist *, mbuf_t *);
740 
741 static errno_t bridge_mac_nat_enable(struct bridge_softc *,
742     struct bridge_iflist *);
743 static void bridge_mac_nat_disable(struct bridge_softc *sc);
744 static void bridge_mac_nat_age_entries(struct bridge_softc *sc, unsigned long);
745 static void bridge_mac_nat_populate_entries(struct bridge_softc *sc);
746 static void bridge_mac_nat_flush_entries(struct bridge_softc *sc,
747     struct bridge_iflist *);
748 static ifnet_t bridge_mac_nat_input(struct bridge_softc *, mbuf_t *,
749     boolean_t *);
750 static boolean_t bridge_mac_nat_output(struct bridge_softc *,
751     struct bridge_iflist *, mbuf_t *, struct mac_nat_record *);
752 static void bridge_mac_nat_translate(mbuf_t *, struct mac_nat_record *,
753     const caddr_t);
754 static bool is_broadcast_ip_packet(mbuf_t *);
755 static bool in_addr_is_ours(const struct in_addr);
756 static bool in6_addr_is_ours(const struct in6_addr *, uint32_t);
757 
758 #define m_copypacket(m, how) m_copym(m, 0, M_COPYALL, how)
759 
760 static int
761 gso_tcp(struct ifnet *ifp, struct mbuf **mp, u_int mac_hlen, bool is_ipv4,
762     boolean_t is_tx);
763 
764 /* The default bridge vlan is 1 (IEEE 802.1Q-2003 Table 9-2) */
765 #define VLANTAGOF(_m)   0
766 
767 u_int8_t bstp_etheraddr[ETHER_ADDR_LEN] =
768 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
769 
770 static u_int8_t ethernulladdr[ETHER_ADDR_LEN] =
771 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
772 
773 #if BRIDGESTP
774 static struct bstp_cb_ops bridge_ops = {
775 	.bcb_state = bridge_state_change,
776 	.bcb_rtage = bridge_rtable_expire
777 };
778 #endif /* BRIDGESTP */
779 
780 SYSCTL_DECL(_net_link);
781 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
782     "Bridge");
783 
784 static int bridge_inherit_mac = 0;   /* share MAC with first bridge member */
785 SYSCTL_INT(_net_link_bridge, OID_AUTO, inherit_mac,
786     CTLFLAG_RW | CTLFLAG_LOCKED,
787     &bridge_inherit_mac, 0,
788     "Inherit MAC address from the first bridge member");
789 
790 SYSCTL_INT(_net_link_bridge, OID_AUTO, rtable_prune_period,
791     CTLFLAG_RW | CTLFLAG_LOCKED,
792     &bridge_rtable_prune_period, 0,
793     "Interval between pruning of routing table");
794 
795 static unsigned int bridge_rtable_hash_size_max = BRIDGE_RTHASH_SIZE_MAX;
796 SYSCTL_UINT(_net_link_bridge, OID_AUTO, rtable_hash_size_max,
797     CTLFLAG_RW | CTLFLAG_LOCKED,
798     &bridge_rtable_hash_size_max, 0,
799     "Maximum size of the routing hash table");
800 
801 #if BRIDGE_DELAYED_CALLBACK_DEBUG
802 static int bridge_delayed_callback_delay = 0;
803 SYSCTL_INT(_net_link_bridge, OID_AUTO, delayed_callback_delay,
804     CTLFLAG_RW | CTLFLAG_LOCKED,
805     &bridge_delayed_callback_delay, 0,
806     "Delay before calling delayed function");
807 #endif
808 
809 SYSCTL_STRUCT(_net_link_bridge, OID_AUTO,
810     hostfilterstats, CTLFLAG_RD | CTLFLAG_LOCKED,
811     &bridge_hostfilter_stats, bridge_hostfilter_stats, "");
812 
813 #if BRIDGESTP
814 static int log_stp   = 0;   /* log STP state changes */
815 SYSCTL_INT(_net_link_bridge, OID_AUTO, log_stp, CTLFLAG_RW,
816     &log_stp, 0, "Log STP state changes");
817 #endif /* BRIDGESTP */
818 
819 struct bridge_control {
820 	int             (*bc_func)(struct bridge_softc *, void *);
821 	unsigned int    bc_argsize;
822 	unsigned int    bc_flags;
823 };
824 
825 #define VMNET_TAG               "com.apple.vmnet"
826 #define VMNET_LOCAL_TAG         VMNET_TAG ".local"
827 #define VMNET_BROADCAST_TAG     VMNET_TAG ".broadcast"
828 #define VMNET_MULTICAST_TAG     VMNET_TAG ".multicast"
829 
830 static u_int16_t vmnet_tag;
831 static u_int16_t vmnet_local_tag;
832 static u_int16_t vmnet_broadcast_tag;
833 static u_int16_t vmnet_multicast_tag;
834 
835 static u_int16_t
allocate_pf_tag(char * name)836 allocate_pf_tag(char * name)
837 {
838 	u_int16_t       tag;
839 
840 	tag = pf_tagname2tag_ext(name);
841 	BRIDGE_LOG(LOG_NOTICE, 0, "%s %d", name, tag);
842 	return tag;
843 }
844 
845 static void
allocate_vmnet_pf_tags(void)846 allocate_vmnet_pf_tags(void)
847 {
848 	/* allocate tags to use with PF */
849 	if (vmnet_tag == 0) {
850 		vmnet_tag = allocate_pf_tag(VMNET_TAG);
851 	}
852 	if (vmnet_local_tag == 0) {
853 		vmnet_local_tag = allocate_pf_tag(VMNET_LOCAL_TAG);
854 	}
855 	if (vmnet_broadcast_tag == 0) {
856 		vmnet_broadcast_tag = allocate_pf_tag(VMNET_BROADCAST_TAG);
857 	}
858 	if (vmnet_multicast_tag == 0) {
859 		vmnet_multicast_tag = allocate_pf_tag(VMNET_MULTICAST_TAG);
860 	}
861 }
862 
863 #define BC_F_COPYIN             0x01    /* copy arguments in */
864 #define BC_F_COPYOUT            0x02    /* copy arguments out */
865 #define BC_F_SUSER              0x04    /* do super-user check */
866 
867 static const struct bridge_control bridge_control_table32[] = {
868 	{ .bc_func = bridge_ioctl_add, .bc_argsize = sizeof(struct ifbreq),             /* 0 */
869 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
870 	{ .bc_func = bridge_ioctl_del, .bc_argsize = sizeof(struct ifbreq),
871 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
872 
873 	{ .bc_func = bridge_ioctl_gifflags, .bc_argsize = sizeof(struct ifbreq),
874 	  .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
875 	{ .bc_func = bridge_ioctl_sifflags, .bc_argsize = sizeof(struct ifbreq),
876 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
877 
878 	{ .bc_func = bridge_ioctl_scache, .bc_argsize = sizeof(struct ifbrparam),
879 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
880 	{ .bc_func = bridge_ioctl_gcache, .bc_argsize = sizeof(struct ifbrparam),
881 	  .bc_flags = BC_F_COPYOUT },
882 
883 	{ .bc_func = bridge_ioctl_gifs32, .bc_argsize = sizeof(struct ifbifconf32),
884 	  .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
885 	{ .bc_func = bridge_ioctl_rts32, .bc_argsize = sizeof(struct ifbaconf32),
886 	  .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
887 
888 	{ .bc_func = bridge_ioctl_saddr32, .bc_argsize = sizeof(struct ifbareq32),
889 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
890 
891 	{ .bc_func = bridge_ioctl_sto, .bc_argsize = sizeof(struct ifbrparam),
892 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
893 	{ .bc_func = bridge_ioctl_gto, .bc_argsize = sizeof(struct ifbrparam),           /* 10 */
894 	  .bc_flags = BC_F_COPYOUT },
895 
896 	{ .bc_func = bridge_ioctl_daddr32, .bc_argsize = sizeof(struct ifbareq32),
897 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
898 
899 	{ .bc_func = bridge_ioctl_flush, .bc_argsize = sizeof(struct ifbreq),
900 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
901 
902 	{ .bc_func = bridge_ioctl_gpri, .bc_argsize = sizeof(struct ifbrparam),
903 	  .bc_flags = BC_F_COPYOUT },
904 	{ .bc_func = bridge_ioctl_spri, .bc_argsize = sizeof(struct ifbrparam),
905 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
906 
907 	{ .bc_func = bridge_ioctl_ght, .bc_argsize = sizeof(struct ifbrparam),
908 	  .bc_flags = BC_F_COPYOUT },
909 	{ .bc_func = bridge_ioctl_sht, .bc_argsize = sizeof(struct ifbrparam),
910 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
911 
912 	{ .bc_func = bridge_ioctl_gfd, .bc_argsize = sizeof(struct ifbrparam),
913 	  .bc_flags = BC_F_COPYOUT },
914 	{ .bc_func = bridge_ioctl_sfd, .bc_argsize = sizeof(struct ifbrparam),
915 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
916 
917 	{ .bc_func = bridge_ioctl_gma, .bc_argsize = sizeof(struct ifbrparam),
918 	  .bc_flags = BC_F_COPYOUT },
919 	{ .bc_func = bridge_ioctl_sma, .bc_argsize = sizeof(struct ifbrparam),           /* 20 */
920 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
921 
922 	{ .bc_func = bridge_ioctl_sifprio, .bc_argsize = sizeof(struct ifbreq),
923 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
924 
925 	{ .bc_func = bridge_ioctl_sifcost, .bc_argsize = sizeof(struct ifbreq),
926 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
927 
928 	{ .bc_func = bridge_ioctl_gfilt, .bc_argsize = sizeof(struct ifbrparam),
929 	  .bc_flags = BC_F_COPYOUT },
930 	{ .bc_func = bridge_ioctl_sfilt, .bc_argsize = sizeof(struct ifbrparam),
931 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
932 
933 	{ .bc_func = bridge_ioctl_purge, .bc_argsize = sizeof(struct ifbreq),
934 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
935 
936 	{ .bc_func = bridge_ioctl_addspan, .bc_argsize = sizeof(struct ifbreq),
937 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
938 	{ .bc_func = bridge_ioctl_delspan, .bc_argsize = sizeof(struct ifbreq),
939 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
940 
941 	{ .bc_func = bridge_ioctl_gbparam32, .bc_argsize = sizeof(struct ifbropreq32),
942 	  .bc_flags = BC_F_COPYOUT },
943 
944 	{ .bc_func = bridge_ioctl_grte, .bc_argsize = sizeof(struct ifbrparam),
945 	  .bc_flags = BC_F_COPYOUT },
946 
947 	{ .bc_func = bridge_ioctl_gifsstp32, .bc_argsize = sizeof(struct ifbpstpconf32),     /* 30 */
948 	  .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
949 
950 	{ .bc_func = bridge_ioctl_sproto, .bc_argsize = sizeof(struct ifbrparam),
951 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
952 
953 	{ .bc_func = bridge_ioctl_stxhc, .bc_argsize = sizeof(struct ifbrparam),
954 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
955 
956 	{ .bc_func = bridge_ioctl_sifmaxaddr, .bc_argsize = sizeof(struct ifbreq),
957 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
958 
959 	{ .bc_func = bridge_ioctl_ghostfilter, .bc_argsize = sizeof(struct ifbrhostfilter),
960 	  .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
961 	{ .bc_func = bridge_ioctl_shostfilter, .bc_argsize = sizeof(struct ifbrhostfilter),
962 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
963 
964 	{ .bc_func = bridge_ioctl_gmnelist32,
965 	  .bc_argsize = sizeof(struct ifbrmnelist32),
966 	  .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
967 	{ .bc_func = bridge_ioctl_gifstats32,
968 	  .bc_argsize = sizeof(struct ifbrmreq32),
969 	  .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
970 };
971 
972 static const struct bridge_control bridge_control_table64[] = {
973 	{ .bc_func = bridge_ioctl_add, .bc_argsize = sizeof(struct ifbreq),           /* 0 */
974 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
975 	{ .bc_func = bridge_ioctl_del, .bc_argsize = sizeof(struct ifbreq),
976 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
977 
978 	{ .bc_func = bridge_ioctl_gifflags, .bc_argsize = sizeof(struct ifbreq),
979 	  .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
980 	{ .bc_func = bridge_ioctl_sifflags, .bc_argsize = sizeof(struct ifbreq),
981 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
982 
983 	{ .bc_func = bridge_ioctl_scache, .bc_argsize = sizeof(struct ifbrparam),
984 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
985 	{ .bc_func = bridge_ioctl_gcache, .bc_argsize = sizeof(struct ifbrparam),
986 	  .bc_flags = BC_F_COPYOUT },
987 
988 	{ .bc_func = bridge_ioctl_gifs64, .bc_argsize = sizeof(struct ifbifconf64),
989 	  .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
990 	{ .bc_func = bridge_ioctl_rts64, .bc_argsize = sizeof(struct ifbaconf64),
991 	  .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
992 
993 	{ .bc_func = bridge_ioctl_saddr64, .bc_argsize = sizeof(struct ifbareq64),
994 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
995 
996 	{ .bc_func = bridge_ioctl_sto, .bc_argsize = sizeof(struct ifbrparam),
997 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
998 	{ .bc_func = bridge_ioctl_gto, .bc_argsize = sizeof(struct ifbrparam),           /* 10 */
999 	  .bc_flags = BC_F_COPYOUT },
1000 
1001 	{ .bc_func = bridge_ioctl_daddr64, .bc_argsize = sizeof(struct ifbareq64),
1002 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1003 
1004 	{ .bc_func = bridge_ioctl_flush, .bc_argsize = sizeof(struct ifbreq),
1005 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1006 
1007 	{ .bc_func = bridge_ioctl_gpri, .bc_argsize = sizeof(struct ifbrparam),
1008 	  .bc_flags = BC_F_COPYOUT },
1009 	{ .bc_func = bridge_ioctl_spri, .bc_argsize = sizeof(struct ifbrparam),
1010 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1011 
1012 	{ .bc_func = bridge_ioctl_ght, .bc_argsize = sizeof(struct ifbrparam),
1013 	  .bc_flags = BC_F_COPYOUT },
1014 	{ .bc_func = bridge_ioctl_sht, .bc_argsize = sizeof(struct ifbrparam),
1015 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1016 
1017 	{ .bc_func = bridge_ioctl_gfd, .bc_argsize = sizeof(struct ifbrparam),
1018 	  .bc_flags = BC_F_COPYOUT },
1019 	{ .bc_func = bridge_ioctl_sfd, .bc_argsize = sizeof(struct ifbrparam),
1020 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1021 
1022 	{ .bc_func = bridge_ioctl_gma, .bc_argsize = sizeof(struct ifbrparam),
1023 	  .bc_flags = BC_F_COPYOUT },
1024 	{ .bc_func = bridge_ioctl_sma, .bc_argsize = sizeof(struct ifbrparam),           /* 20 */
1025 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1026 
1027 	{ .bc_func = bridge_ioctl_sifprio, .bc_argsize = sizeof(struct ifbreq),
1028 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1029 
1030 	{ .bc_func = bridge_ioctl_sifcost, .bc_argsize = sizeof(struct ifbreq),
1031 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1032 
1033 	{ .bc_func = bridge_ioctl_gfilt, .bc_argsize = sizeof(struct ifbrparam),
1034 	  .bc_flags = BC_F_COPYOUT },
1035 	{ .bc_func = bridge_ioctl_sfilt, .bc_argsize = sizeof(struct ifbrparam),
1036 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1037 
1038 	{ .bc_func = bridge_ioctl_purge, .bc_argsize = sizeof(struct ifbreq),
1039 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1040 
1041 	{ .bc_func = bridge_ioctl_addspan, .bc_argsize = sizeof(struct ifbreq),
1042 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1043 	{ .bc_func = bridge_ioctl_delspan, .bc_argsize = sizeof(struct ifbreq),
1044 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1045 
1046 	{ .bc_func = bridge_ioctl_gbparam64, .bc_argsize = sizeof(struct ifbropreq64),
1047 	  .bc_flags = BC_F_COPYOUT },
1048 
1049 	{ .bc_func = bridge_ioctl_grte, .bc_argsize = sizeof(struct ifbrparam),
1050 	  .bc_flags = BC_F_COPYOUT },
1051 
1052 	{ .bc_func = bridge_ioctl_gifsstp64, .bc_argsize = sizeof(struct ifbpstpconf64),     /* 30 */
1053 	  .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
1054 
1055 	{ .bc_func = bridge_ioctl_sproto, .bc_argsize = sizeof(struct ifbrparam),
1056 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1057 
1058 	{ .bc_func = bridge_ioctl_stxhc, .bc_argsize = sizeof(struct ifbrparam),
1059 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1060 
1061 	{ .bc_func = bridge_ioctl_sifmaxaddr, .bc_argsize = sizeof(struct ifbreq),
1062 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1063 
1064 	{ .bc_func = bridge_ioctl_ghostfilter, .bc_argsize = sizeof(struct ifbrhostfilter),
1065 	  .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
1066 	{ .bc_func = bridge_ioctl_shostfilter, .bc_argsize = sizeof(struct ifbrhostfilter),
1067 	  .bc_flags = BC_F_COPYIN | BC_F_SUSER },
1068 
1069 	{ .bc_func = bridge_ioctl_gmnelist64,
1070 	  .bc_argsize = sizeof(struct ifbrmnelist64),
1071 	  .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
1072 	{ .bc_func = bridge_ioctl_gifstats64,
1073 	  .bc_argsize = sizeof(struct ifbrmreq64),
1074 	  .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
1075 };
1076 
1077 static const unsigned int bridge_control_table_size =
1078     sizeof(bridge_control_table32) / sizeof(bridge_control_table32[0]);
1079 
1080 static LIST_HEAD(, bridge_softc) bridge_list =
1081     LIST_HEAD_INITIALIZER(bridge_list);
1082 
1083 #define BRIDGENAME      "bridge"
1084 #define BRIDGES_MAX     IF_MAXUNIT
1085 #define BRIDGE_ZONE_MAX_ELEM    MIN(IFNETS_MAX, BRIDGES_MAX)
1086 
1087 static struct if_clone bridge_cloner =
1088     IF_CLONE_INITIALIZER(BRIDGENAME, bridge_clone_create, bridge_clone_destroy,
1089     0, BRIDGES_MAX);
1090 
1091 static int if_bridge_txstart = 0;
1092 SYSCTL_INT(_net_link_bridge, OID_AUTO, txstart, CTLFLAG_RW | CTLFLAG_LOCKED,
1093     &if_bridge_txstart, 0, "Bridge interface uses TXSTART model");
1094 
1095 SYSCTL_INT(_net_link_bridge, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_LOCKED,
1096     &if_bridge_debug, 0, "Bridge debug flags");
1097 
1098 SYSCTL_INT(_net_link_bridge, OID_AUTO, log_level,
1099     CTLFLAG_RW | CTLFLAG_LOCKED,
1100     &if_bridge_log_level, 0, "Bridge log level");
1101 
1102 static int if_bridge_segmentation = 1;
1103 SYSCTL_INT(_net_link_bridge, OID_AUTO, segmentation,
1104     CTLFLAG_RW | CTLFLAG_LOCKED,
1105     &if_bridge_segmentation, 0, "Bridge interface enable segmentation");
1106 
1107 static int if_bridge_vmnet_pf_tagging = 1;
1108 SYSCTL_INT(_net_link_bridge, OID_AUTO, vmnet_pf_tagging,
1109     CTLFLAG_RW | CTLFLAG_LOCKED,
1110     &if_bridge_segmentation, 0, "Bridge interface enable vmnet PF tagging");
1111 
1112 #define BRIDGE_TSO_REDUCE_MSS_FORWARDING_MAX            256
1113 #define BRIDGE_TSO_REDUCE_MSS_FORWARDING_DEFAULT        110
1114 #define BRIDGE_TSO_REDUCE_MSS_TX_MAX                    256
1115 #define BRIDGE_TSO_REDUCE_MSS_TX_DEFAULT                0
1116 
1117 static u_int if_bridge_tso_reduce_mss_forwarding
1118         = BRIDGE_TSO_REDUCE_MSS_FORWARDING_DEFAULT;
1119 static u_int if_bridge_tso_reduce_mss_tx
1120         = BRIDGE_TSO_REDUCE_MSS_TX_DEFAULT;
1121 
1122 static int
bridge_tso_reduce_mss(struct sysctl_req * req,u_int * val,u_int val_max)1123 bridge_tso_reduce_mss(struct sysctl_req *req, u_int * val, u_int val_max)
1124 {
1125 	int     changed;
1126 	int     error;
1127 	u_int   new_value;
1128 
1129 	error = sysctl_io_number(req, *val, sizeof(*val), &new_value,
1130 	    &changed);
1131 	if (error == 0 && changed != 0) {
1132 		if (new_value > val_max) {
1133 			return EINVAL;
1134 		}
1135 		*val = new_value;
1136 	}
1137 	return error;
1138 }
1139 
1140 static int
1141 bridge_tso_reduce_mss_forwarding_sysctl SYSCTL_HANDLER_ARGS
1142 {
1143 	return bridge_tso_reduce_mss(req, &if_bridge_tso_reduce_mss_forwarding,
1144     BRIDGE_TSO_REDUCE_MSS_FORWARDING_MAX);
1145 }
1146 
1147 SYSCTL_PROC(_net_link_bridge, OID_AUTO, tso_reduce_mss_forwarding,
1148     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
1149     0, 0, bridge_tso_reduce_mss_forwarding_sysctl, "IU",
1150     "Bridge tso reduce mss when forwarding");
1151 
1152 static int
1153 bridge_tso_reduce_mss_tx_sysctl SYSCTL_HANDLER_ARGS
1154 {
1155 	return bridge_tso_reduce_mss(req, &if_bridge_tso_reduce_mss_tx,
1156     BRIDGE_TSO_REDUCE_MSS_TX_MAX);
1157 }
1158 
1159 SYSCTL_PROC(_net_link_bridge, OID_AUTO, tso_reduce_mss_tx,
1160     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
1161     0, 0, bridge_tso_reduce_mss_tx_sysctl, "IU",
1162     "Bridge tso reduce mss on transmit");
1163 
1164 
1165 #if DEBUG || DEVELOPMENT
1166 #define BRIDGE_FORCE_ONE        0x00000001
1167 #define BRIDGE_FORCE_TWO        0x00000002
1168 static u_int32_t if_bridge_force_errors = 0;
1169 SYSCTL_INT(_net_link_bridge, OID_AUTO, force_errors,
1170     CTLFLAG_RW | CTLFLAG_LOCKED,
1171     &if_bridge_force_errors, 0, "Bridge interface force errors");
1172 static inline bool
bridge_error_is_forced(u_int32_t flags)1173 bridge_error_is_forced(u_int32_t flags)
1174 {
1175 	return (if_bridge_force_errors & flags) != 0;
1176 }
1177 
1178 #define BRIDGE_ERROR_GET_FORCED(__is_forced, __flags)                   \
1179 	do {                                                            \
1180 	        __is_forced = bridge_error_is_forced(__flags);          \
1181 	        if (__is_forced) {                                      \
1182 	                BRIDGE_LOG(LOG_NOTICE, 0, "0x%x forced", __flags); \
1183 	        }                                                       \
1184 	} while (0)
1185 #endif /* DEBUG || DEVELOPMENT */
1186 
1187 
1188 static void brlog_ether_header(struct ether_header *);
1189 static void brlog_mbuf_data(mbuf_t, size_t, size_t);
1190 static void brlog_mbuf_pkthdr(mbuf_t, const char *, const char *);
1191 static void brlog_mbuf(mbuf_t, const char *, const char *);
1192 static void brlog_link(struct bridge_softc * sc);
1193 
1194 #if BRIDGE_LOCK_DEBUG
1195 static void bridge_lock(struct bridge_softc *);
1196 static void bridge_unlock(struct bridge_softc *);
1197 static int bridge_lock2ref(struct bridge_softc *);
1198 static void bridge_unref(struct bridge_softc *);
1199 static void bridge_xlock(struct bridge_softc *);
1200 static void bridge_xdrop(struct bridge_softc *);
1201 
1202 static void
bridge_lock(struct bridge_softc * sc)1203 bridge_lock(struct bridge_softc *sc)
1204 {
1205 	void *lr_saved = __builtin_return_address(0);
1206 
1207 	BRIDGE_LOCK_ASSERT_NOTHELD(sc);
1208 
1209 	_BRIDGE_LOCK(sc);
1210 
1211 	sc->lock_lr[sc->next_lock_lr] = lr_saved;
1212 	sc->next_lock_lr = (sc->next_lock_lr + 1) % SO_LCKDBG_MAX;
1213 }
1214 
1215 static void
bridge_unlock(struct bridge_softc * sc)1216 bridge_unlock(struct bridge_softc *sc)
1217 {
1218 	void *lr_saved = __builtin_return_address(0);
1219 
1220 	BRIDGE_LOCK_ASSERT_HELD(sc);
1221 
1222 	sc->unlock_lr[sc->next_unlock_lr] = lr_saved;
1223 	sc->next_unlock_lr = (sc->next_unlock_lr + 1) % SO_LCKDBG_MAX;
1224 
1225 	_BRIDGE_UNLOCK(sc);
1226 }
1227 
1228 static int
bridge_lock2ref(struct bridge_softc * sc)1229 bridge_lock2ref(struct bridge_softc *sc)
1230 {
1231 	int error = 0;
1232 	void *lr_saved = __builtin_return_address(0);
1233 
1234 	BRIDGE_LOCK_ASSERT_HELD(sc);
1235 
1236 	if (sc->sc_iflist_xcnt > 0) {
1237 		error = EBUSY;
1238 	} else {
1239 		sc->sc_iflist_ref++;
1240 	}
1241 
1242 	sc->unlock_lr[sc->next_unlock_lr] = lr_saved;
1243 	sc->next_unlock_lr = (sc->next_unlock_lr + 1) % SO_LCKDBG_MAX;
1244 
1245 	_BRIDGE_UNLOCK(sc);
1246 
1247 	return error;
1248 }
1249 
1250 static void
bridge_unref(struct bridge_softc * sc)1251 bridge_unref(struct bridge_softc *sc)
1252 {
1253 	void *lr_saved = __builtin_return_address(0);
1254 
1255 	BRIDGE_LOCK_ASSERT_NOTHELD(sc);
1256 
1257 	_BRIDGE_LOCK(sc);
1258 	sc->lock_lr[sc->next_lock_lr] = lr_saved;
1259 	sc->next_lock_lr = (sc->next_lock_lr + 1) % SO_LCKDBG_MAX;
1260 
1261 	sc->sc_iflist_ref--;
1262 
1263 	sc->unlock_lr[sc->next_unlock_lr] = lr_saved;
1264 	sc->next_unlock_lr = (sc->next_unlock_lr + 1) % SO_LCKDBG_MAX;
1265 	if ((sc->sc_iflist_xcnt > 0) && (sc->sc_iflist_ref == 0)) {
1266 		_BRIDGE_UNLOCK(sc);
1267 		wakeup(&sc->sc_cv);
1268 	} else {
1269 		_BRIDGE_UNLOCK(sc);
1270 	}
1271 }
1272 
1273 static void
bridge_xlock(struct bridge_softc * sc)1274 bridge_xlock(struct bridge_softc *sc)
1275 {
1276 	void *lr_saved = __builtin_return_address(0);
1277 
1278 	BRIDGE_LOCK_ASSERT_HELD(sc);
1279 
1280 	sc->sc_iflist_xcnt++;
1281 	while (sc->sc_iflist_ref > 0) {
1282 		sc->unlock_lr[sc->next_unlock_lr] = lr_saved;
1283 		sc->next_unlock_lr = (sc->next_unlock_lr + 1) % SO_LCKDBG_MAX;
1284 
1285 		msleep(&sc->sc_cv, &sc->sc_mtx, PZERO, "BRIDGE_XLOCK", NULL);
1286 
1287 		sc->lock_lr[sc->next_lock_lr] = lr_saved;
1288 		sc->next_lock_lr = (sc->next_lock_lr + 1) % SO_LCKDBG_MAX;
1289 	}
1290 }
1291 
1292 static void
bridge_xdrop(struct bridge_softc * sc)1293 bridge_xdrop(struct bridge_softc *sc)
1294 {
1295 	BRIDGE_LOCK_ASSERT_HELD(sc);
1296 
1297 	sc->sc_iflist_xcnt--;
1298 }
1299 
1300 #endif /* BRIDGE_LOCK_DEBUG */
1301 
1302 static void
brlog_mbuf_pkthdr(mbuf_t m,const char * prefix,const char * suffix)1303 brlog_mbuf_pkthdr(mbuf_t m, const char *prefix, const char *suffix)
1304 {
1305 	if (m) {
1306 		BRIDGE_LOG_SIMPLE(LOG_NOTICE, 0,
1307 		    "%spktlen: %u rcvif: 0x%llx header: 0x%llx nextpkt: 0x%llx%s",
1308 		    prefix ? prefix : "", (unsigned int)mbuf_pkthdr_len(m),
1309 		    (uint64_t)VM_KERNEL_ADDRPERM(mbuf_pkthdr_rcvif(m)),
1310 		    (uint64_t)VM_KERNEL_ADDRPERM(mbuf_pkthdr_header(m)),
1311 		    (uint64_t)VM_KERNEL_ADDRPERM(mbuf_nextpkt(m)),
1312 		    suffix ? suffix : "");
1313 	} else {
1314 		BRIDGE_LOG_SIMPLE(LOG_NOTICE, 0, "%s<NULL>%s", prefix, suffix);
1315 	}
1316 }
1317 
1318 static void
brlog_mbuf(mbuf_t m,const char * prefix,const char * suffix)1319 brlog_mbuf(mbuf_t m, const char *prefix, const char *suffix)
1320 {
1321 	if (m) {
1322 		BRIDGE_LOG_SIMPLE(LOG_NOTICE, 0,
1323 		    "%s0x%llx type: %u flags: 0x%x len: %u data: 0x%llx "
1324 		    "maxlen: %u datastart: 0x%llx next: 0x%llx%s",
1325 		    prefix ? prefix : "", (uint64_t)VM_KERNEL_ADDRPERM(m),
1326 		    mbuf_type(m), mbuf_flags(m), (unsigned int)mbuf_len(m),
1327 		    (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m)),
1328 		    (unsigned int)mbuf_maxlen(m),
1329 		    (uint64_t)VM_KERNEL_ADDRPERM(mbuf_datastart(m)),
1330 		    (uint64_t)VM_KERNEL_ADDRPERM(mbuf_next(m)),
1331 		    !suffix || (mbuf_flags(m) & MBUF_PKTHDR) ? "" : suffix);
1332 		if ((mbuf_flags(m) & MBUF_PKTHDR)) {
1333 			brlog_mbuf_pkthdr(m, "", suffix);
1334 		}
1335 	} else {
1336 		BRIDGE_LOG_SIMPLE(LOG_NOTICE, 0, "%s<NULL>%s", prefix, suffix);
1337 	}
1338 }
1339 
1340 static void
brlog_mbuf_data(mbuf_t m,size_t offset,size_t len)1341 brlog_mbuf_data(mbuf_t m, size_t offset, size_t len)
1342 {
1343 	mbuf_t                  n;
1344 	size_t                  i, j;
1345 	size_t                  pktlen, mlen, maxlen;
1346 	unsigned char   *ptr;
1347 
1348 	pktlen = mbuf_pkthdr_len(m);
1349 
1350 	if (offset > pktlen) {
1351 		return;
1352 	}
1353 
1354 	maxlen = (pktlen - offset > len) ? len : pktlen - offset;
1355 	n = m;
1356 	mlen = mbuf_len(n);
1357 	ptr = mbuf_data(n);
1358 	for (i = 0, j = 0; i < maxlen; i++, j++) {
1359 		if (j >= mlen) {
1360 			n = mbuf_next(n);
1361 			if (n == 0) {
1362 				break;
1363 			}
1364 			ptr = mbuf_data(n);
1365 			mlen = mbuf_len(n);
1366 			j = 0;
1367 		}
1368 		if (i >= offset) {
1369 			BRIDGE_LOG_SIMPLE(LOG_NOTICE, 0,
1370 			    "%02x%s", ptr[j], i % 2 ? " " : "");
1371 		}
1372 	}
1373 }
1374 
1375 static void
brlog_ether_header(struct ether_header * eh)1376 brlog_ether_header(struct ether_header *eh)
1377 {
1378 	BRIDGE_LOG_SIMPLE(LOG_NOTICE, 0,
1379 	    "%02x:%02x:%02x:%02x:%02x:%02x > "
1380 	    "%02x:%02x:%02x:%02x:%02x:%02x 0x%04x ",
1381 	    eh->ether_shost[0], eh->ether_shost[1], eh->ether_shost[2],
1382 	    eh->ether_shost[3], eh->ether_shost[4], eh->ether_shost[5],
1383 	    eh->ether_dhost[0], eh->ether_dhost[1], eh->ether_dhost[2],
1384 	    eh->ether_dhost[3], eh->ether_dhost[4], eh->ether_dhost[5],
1385 	    ntohs(eh->ether_type));
1386 }
1387 
1388 static char *
ether_ntop(char * buf,size_t len,const u_char * ap)1389 ether_ntop(char *buf, size_t len, const u_char *ap)
1390 {
1391 	snprintf(buf, len, "%02x:%02x:%02x:%02x:%02x:%02x",
1392 	    ap[0], ap[1], ap[2], ap[3], ap[4], ap[5]);
1393 
1394 	return buf;
1395 }
1396 
1397 static void
brlog_link(struct bridge_softc * sc)1398 brlog_link(struct bridge_softc * sc)
1399 {
1400 	int i;
1401 	uint32_t sdl_buffer[offsetof(struct sockaddr_dl, sdl_data) +
1402 	IFNAMSIZ + ETHER_ADDR_LEN];
1403 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)sdl_buffer;
1404 	const u_char * lladdr;
1405 	char lladdr_str[48];
1406 
1407 	memset(sdl, 0, sizeof(sdl_buffer));
1408 	sdl->sdl_family = AF_LINK;
1409 	sdl->sdl_nlen = strlen(sc->sc_if_xname);
1410 	sdl->sdl_alen = ETHER_ADDR_LEN;
1411 	sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data);
1412 	memcpy(sdl->sdl_data, sc->sc_if_xname, sdl->sdl_nlen);
1413 	memcpy(LLADDR(sdl), sc->sc_defaddr, ETHER_ADDR_LEN);
1414 	lladdr_str[0] = '\0';
1415 	for (i = 0, lladdr = CONST_LLADDR(sdl);
1416 	    i < sdl->sdl_alen;
1417 	    i++, lladdr++) {
1418 		char    byte_str[4];
1419 
1420 		snprintf(byte_str, sizeof(byte_str), "%s%x", i ? ":" : "",
1421 		    *lladdr);
1422 		strlcat(lladdr_str, byte_str, sizeof(lladdr_str));
1423 	}
1424 	BRIDGE_LOG_SIMPLE(LOG_NOTICE, 0,
1425 	    "%s sdl len %d index %d family %d type 0x%x nlen %d alen %d"
1426 	    " slen %d addr %s", sc->sc_if_xname,
1427 	    sdl->sdl_len, sdl->sdl_index,
1428 	    sdl->sdl_family, sdl->sdl_type, sdl->sdl_nlen,
1429 	    sdl->sdl_alen, sdl->sdl_slen, lladdr_str);
1430 }
1431 
1432 
1433 /*
1434  * bridgeattach:
1435  *
1436  *	Pseudo-device attach routine.
1437  */
1438 __private_extern__ int
bridgeattach(int n)1439 bridgeattach(int n)
1440 {
1441 #pragma unused(n)
1442 	int error;
1443 
1444 	LIST_INIT(&bridge_list);
1445 
1446 #if BRIDGESTP
1447 	bstp_sys_init();
1448 #endif /* BRIDGESTP */
1449 
1450 	error = if_clone_attach(&bridge_cloner);
1451 	if (error != 0) {
1452 		BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_clone_attach failed %d", error);
1453 	}
1454 	return error;
1455 }
1456 
1457 
1458 static errno_t
bridge_ifnet_set_attrs(struct ifnet * ifp)1459 bridge_ifnet_set_attrs(struct ifnet * ifp)
1460 {
1461 	errno_t         error;
1462 
1463 	error = ifnet_set_mtu(ifp, ETHERMTU);
1464 	if (error != 0) {
1465 		BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_mtu failed %d", error);
1466 		goto done;
1467 	}
1468 	error = ifnet_set_addrlen(ifp, ETHER_ADDR_LEN);
1469 	if (error != 0) {
1470 		BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_addrlen failed %d", error);
1471 		goto done;
1472 	}
1473 	error = ifnet_set_hdrlen(ifp, ETHER_HDR_LEN);
1474 	if (error != 0) {
1475 		BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_hdrlen failed %d", error);
1476 		goto done;
1477 	}
1478 	error = ifnet_set_flags(ifp,
1479 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST,
1480 	    0xffff);
1481 
1482 	if (error != 0) {
1483 		BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_flags failed %d", error);
1484 		goto done;
1485 	}
1486 done:
1487 	return error;
1488 }
1489 
1490 /*
1491  * bridge_clone_create:
1492  *
1493  *	Create a new bridge instance.
1494  */
1495 static int
bridge_clone_create(struct if_clone * ifc,uint32_t unit,void * params)1496 bridge_clone_create(struct if_clone *ifc, uint32_t unit, void *params)
1497 {
1498 #pragma unused(params)
1499 	struct ifnet *ifp = NULL;
1500 	struct bridge_softc *sc = NULL;
1501 	struct bridge_softc *sc2 = NULL;
1502 	struct ifnet_init_eparams init_params;
1503 	errno_t error = 0;
1504 	uint8_t eth_hostid[ETHER_ADDR_LEN];
1505 	int fb, retry, has_hostid;
1506 
1507 	sc = kalloc_type(struct bridge_softc, Z_WAITOK_ZERO_NOFAIL);
1508 	lck_mtx_init(&sc->sc_mtx, &bridge_lock_grp, &bridge_lock_attr);
1509 	sc->sc_brtmax = BRIDGE_RTABLE_MAX;
1510 	sc->sc_mne_max = BRIDGE_MAC_NAT_ENTRY_MAX;
1511 	sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
1512 	sc->sc_filter_flags = 0;
1513 
1514 	TAILQ_INIT(&sc->sc_iflist);
1515 
1516 	/* use the interface name as the unique id for ifp recycle */
1517 	snprintf(sc->sc_if_xname, sizeof(sc->sc_if_xname), "%s%d",
1518 	    ifc->ifc_name, unit);
1519 	bzero(&init_params, sizeof(init_params));
1520 	init_params.ver                 = IFNET_INIT_CURRENT_VERSION;
1521 	init_params.len                 = sizeof(init_params);
1522 	/* Initialize our routing table. */
1523 	error = bridge_rtable_init(sc);
1524 	if (error != 0) {
1525 		BRIDGE_LOG(LOG_NOTICE, 0, "bridge_rtable_init failed %d", error);
1526 		goto done;
1527 	}
1528 	TAILQ_INIT(&sc->sc_spanlist);
1529 	if (if_bridge_txstart) {
1530 		init_params.start = bridge_start;
1531 	} else {
1532 		init_params.flags = IFNET_INIT_LEGACY;
1533 		init_params.output = bridge_output;
1534 	}
1535 	init_params.set_bpf_tap = bridge_set_bpf_tap;
1536 	init_params.uniqueid            = sc->sc_if_xname;
1537 	init_params.uniqueid_len        = strlen(sc->sc_if_xname);
1538 	init_params.sndq_maxlen         = IFQ_MAXLEN;
1539 	init_params.name                = ifc->ifc_name;
1540 	init_params.unit                = unit;
1541 	init_params.family              = IFNET_FAMILY_ETHERNET;
1542 	init_params.type                = IFT_BRIDGE;
1543 	init_params.demux               = ether_demux;
1544 	init_params.add_proto           = ether_add_proto;
1545 	init_params.del_proto           = ether_del_proto;
1546 	init_params.check_multi         = ether_check_multi;
1547 	init_params.framer_extended     = ether_frameout_extended;
1548 	init_params.softc               = sc;
1549 	init_params.ioctl               = bridge_ioctl;
1550 	init_params.detach              = bridge_detach;
1551 	init_params.broadcast_addr      = etherbroadcastaddr;
1552 	init_params.broadcast_len       = ETHER_ADDR_LEN;
1553 
1554 	error = ifnet_allocate_extended(&init_params, &ifp);
1555 	if (error != 0) {
1556 		BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_allocate failed %d", error);
1557 		goto done;
1558 	}
1559 	LIST_INIT(&sc->sc_mne_list);
1560 	LIST_INIT(&sc->sc_mne_list_v6);
1561 	sc->sc_ifp = ifp;
1562 	error = bridge_ifnet_set_attrs(ifp);
1563 	if (error != 0) {
1564 		BRIDGE_LOG(LOG_NOTICE, 0, "bridge_ifnet_set_attrs failed %d",
1565 		    error);
1566 		goto done;
1567 	}
1568 	/*
1569 	 * Generate an ethernet address with a locally administered address.
1570 	 *
1571 	 * Since we are using random ethernet addresses for the bridge, it is
1572 	 * possible that we might have address collisions, so make sure that
1573 	 * this hardware address isn't already in use on another bridge.
1574 	 * The first try uses the "hostid" and falls back to read_frandom();
1575 	 * for "hostid", we use the MAC address of the first-encountered
1576 	 * Ethernet-type interface that is currently configured.
1577 	 */
1578 	fb = 0;
1579 	has_hostid = (uuid_get_ethernet(&eth_hostid[0]) == 0);
1580 	for (retry = 1; retry != 0;) {
1581 		if (fb || has_hostid == 0) {
1582 			read_frandom(&sc->sc_defaddr, ETHER_ADDR_LEN);
1583 			sc->sc_defaddr[0] &= ~1; /* clear multicast bit */
1584 			sc->sc_defaddr[0] |= 2;  /* set the LAA bit */
1585 		} else {
1586 			bcopy(&eth_hostid[0], &sc->sc_defaddr,
1587 			    ETHER_ADDR_LEN);
1588 			sc->sc_defaddr[0] &= ~1; /* clear multicast bit */
1589 			sc->sc_defaddr[0] |= 2;  /* set the LAA bit */
1590 			sc->sc_defaddr[3] =     /* stir it up a bit */
1591 			    ((sc->sc_defaddr[3] & 0x0f) << 4) |
1592 			    ((sc->sc_defaddr[3] & 0xf0) >> 4);
1593 			/*
1594 			 * Mix in the LSB as it's actually pretty significant,
1595 			 * see rdar://14076061
1596 			 */
1597 			sc->sc_defaddr[4] =
1598 			    (((sc->sc_defaddr[4] & 0x0f) << 4) |
1599 			    ((sc->sc_defaddr[4] & 0xf0) >> 4)) ^
1600 			    sc->sc_defaddr[5];
1601 			sc->sc_defaddr[5] = ifp->if_unit & 0xff;
1602 		}
1603 
1604 		fb = 1;
1605 		retry = 0;
1606 		lck_mtx_lock(&bridge_list_mtx);
1607 		LIST_FOREACH(sc2, &bridge_list, sc_list) {
1608 			if (_ether_cmp(sc->sc_defaddr,
1609 			    IF_LLADDR(sc2->sc_ifp)) == 0) {
1610 				retry = 1;
1611 			}
1612 		}
1613 		lck_mtx_unlock(&bridge_list_mtx);
1614 	}
1615 
1616 	sc->sc_flags &= ~SCF_MEDIA_ACTIVE;
1617 
1618 	if (BRIDGE_DBGF_ENABLED(BR_DBGF_LIFECYCLE)) {
1619 		brlog_link(sc);
1620 	}
1621 	error = ifnet_attach(ifp, NULL);
1622 	if (error != 0) {
1623 		BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_attach failed %d", error);
1624 		goto done;
1625 	}
1626 
1627 	error = ifnet_set_lladdr_and_type(ifp, sc->sc_defaddr, ETHER_ADDR_LEN,
1628 	    IFT_ETHER);
1629 	if (error != 0) {
1630 		BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_lladdr_and_type failed %d",
1631 		    error);
1632 		goto done;
1633 	}
1634 
1635 	ifnet_set_offload(ifp,
1636 	    IFNET_CSUM_IP | IFNET_CSUM_TCP | IFNET_CSUM_UDP |
1637 	    IFNET_CSUM_TCPIPV6 | IFNET_CSUM_UDPIPV6 | IFNET_MULTIPAGES);
1638 	error = bridge_set_tso(sc);
1639 	if (error != 0) {
1640 		BRIDGE_LOG(LOG_NOTICE, 0, "bridge_set_tso failed %d", error);
1641 		goto done;
1642 	}
1643 #if BRIDGESTP
1644 	bstp_attach(&sc->sc_stp, &bridge_ops);
1645 #endif /* BRIDGESTP */
1646 
1647 	lck_mtx_lock(&bridge_list_mtx);
1648 	LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
1649 	lck_mtx_unlock(&bridge_list_mtx);
1650 
1651 	/* attach as ethernet */
1652 	error = bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header),
1653 	    NULL, NULL);
1654 
1655 done:
1656 	if (error != 0) {
1657 		BRIDGE_LOG(LOG_NOTICE, 0, "failed error %d", error);
1658 		/* TBD: Clean up: sc, sc_rthash etc */
1659 	}
1660 
1661 	return error;
1662 }
1663 
1664 /*
1665  * bridge_clone_destroy:
1666  *
1667  *	Destroy a bridge instance.
1668  */
1669 static int
bridge_clone_destroy(struct ifnet * ifp)1670 bridge_clone_destroy(struct ifnet *ifp)
1671 {
1672 	struct bridge_softc *sc = ifp->if_softc;
1673 	struct bridge_iflist *bif;
1674 	errno_t error;
1675 
1676 	BRIDGE_LOCK(sc);
1677 	if ((sc->sc_flags & SCF_DETACHING)) {
1678 		BRIDGE_UNLOCK(sc);
1679 		return 0;
1680 	}
1681 	sc->sc_flags |= SCF_DETACHING;
1682 
1683 	bridge_ifstop(ifp, 1);
1684 
1685 	bridge_cancel_delayed_call(&sc->sc_resize_call);
1686 
1687 	bridge_cleanup_delayed_call(&sc->sc_resize_call);
1688 	bridge_cleanup_delayed_call(&sc->sc_aging_timer);
1689 
1690 	error = ifnet_set_flags(ifp, 0, IFF_UP);
1691 	if (error != 0) {
1692 		BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_flags failed %d", error);
1693 	}
1694 
1695 	while ((bif = TAILQ_FIRST(&sc->sc_iflist)) != NULL) {
1696 		bridge_delete_member(sc, bif);
1697 	}
1698 
1699 	while ((bif = TAILQ_FIRST(&sc->sc_spanlist)) != NULL) {
1700 		bridge_delete_span(sc, bif);
1701 	}
1702 	BRIDGE_UNLOCK(sc);
1703 
1704 	error = ifnet_detach(ifp);
1705 	if (error != 0) {
1706 		panic("%s (%d): ifnet_detach(%p) failed %d",
1707 		    __func__, __LINE__, ifp, error);
1708 	}
1709 	return 0;
1710 }
1711 
1712 #define DRVSPEC do { \
1713 	if (ifd->ifd_cmd >= bridge_control_table_size) {                \
1714 	        error = EINVAL;                                         \
1715 	        break;                                                  \
1716 	}                                                               \
1717 	bc = &bridge_control_table[ifd->ifd_cmd];                       \
1718                                                                         \
1719 	if (cmd == SIOCGDRVSPEC &&                                      \
1720 	    (bc->bc_flags & BC_F_COPYOUT) == 0) {                       \
1721 	        error = EINVAL;                                         \
1722 	        break;                                                  \
1723 	} else if (cmd == SIOCSDRVSPEC &&                               \
1724 	    (bc->bc_flags & BC_F_COPYOUT) != 0) {                       \
1725 	        error = EINVAL;                                         \
1726 	        break;                                                  \
1727 	}                                                               \
1728                                                                         \
1729 	if (bc->bc_flags & BC_F_SUSER) {                                \
1730 	        error = kauth_authorize_generic(kauth_cred_get(),       \
1731 	            KAUTH_GENERIC_ISSUSER);                             \
1732 	        if (error)                                              \
1733 	                break;                                          \
1734 	}                                                               \
1735                                                                         \
1736 	if (ifd->ifd_len != bc->bc_argsize ||                           \
1737 	    ifd->ifd_len > sizeof (args)) {                             \
1738 	        error = EINVAL;                                         \
1739 	        break;                                                  \
1740 	}                                                               \
1741                                                                         \
1742 	bzero(&args, sizeof (args));                                    \
1743 	if (bc->bc_flags & BC_F_COPYIN) {                               \
1744 	        error = copyin(ifd->ifd_data, &args, ifd->ifd_len);     \
1745 	        if (error)                                              \
1746 	                break;                                          \
1747 	}                                                               \
1748                                                                         \
1749 	BRIDGE_LOCK(sc);                                                \
1750 	error = (*bc->bc_func)(sc, &args);                              \
1751 	BRIDGE_UNLOCK(sc);                                              \
1752 	if (error)                                                      \
1753 	        break;                                                  \
1754                                                                         \
1755 	if (bc->bc_flags & BC_F_COPYOUT)                                \
1756 	        error = copyout(&args, ifd->ifd_data, ifd->ifd_len);    \
1757 } while (0)
1758 
1759 static boolean_t
interface_needs_input_broadcast(struct ifnet * ifp)1760 interface_needs_input_broadcast(struct ifnet * ifp)
1761 {
1762 	/*
1763 	 * Selectively enable input broadcast only when necessary.
1764 	 * The bridge interface itself attaches a fake protocol
1765 	 * so checking for at least two protocols means that the
1766 	 * interface is being used for something besides bridging
1767 	 * and needs to see broadcast packets from other members.
1768 	 */
1769 	return if_get_protolist(ifp, NULL, 0) >= 2;
1770 }
1771 
1772 static boolean_t
bif_set_input_broadcast(struct bridge_iflist * bif,boolean_t input_broadcast)1773 bif_set_input_broadcast(struct bridge_iflist * bif, boolean_t input_broadcast)
1774 {
1775 	boolean_t       old_input_broadcast;
1776 
1777 	old_input_broadcast = (bif->bif_flags & BIFF_INPUT_BROADCAST) != 0;
1778 	if (input_broadcast) {
1779 		bif->bif_flags |= BIFF_INPUT_BROADCAST;
1780 	} else {
1781 		bif->bif_flags &= ~BIFF_INPUT_BROADCAST;
1782 	}
1783 	return old_input_broadcast != input_broadcast;
1784 }
1785 
1786 /*
1787  * bridge_ioctl:
1788  *
1789  *	Handle a control request from the operator.
1790  */
1791 static errno_t
bridge_ioctl(struct ifnet * ifp,u_long cmd,void * data)1792 bridge_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1793 {
1794 	struct bridge_softc *sc = ifp->if_softc;
1795 	struct ifreq *ifr = (struct ifreq *)data;
1796 	struct bridge_iflist *bif;
1797 	int error = 0;
1798 
1799 	BRIDGE_LOCK_ASSERT_NOTHELD(sc);
1800 
1801 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_IOCTL,
1802 	    "ifp %s cmd 0x%08lx (%c%c [%lu] %c %lu)",
1803 	    ifp->if_xname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
1804 	    (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
1805 	    (char)IOCGROUP(cmd), cmd & 0xff);
1806 
1807 	switch (cmd) {
1808 	case SIOCSIFADDR:
1809 	case SIOCAIFADDR:
1810 		ifnet_set_flags(ifp, IFF_UP, IFF_UP);
1811 		break;
1812 
1813 	case SIOCGIFMEDIA32:
1814 	case SIOCGIFMEDIA64: {
1815 		struct ifmediareq *ifmr = (struct ifmediareq *)data;
1816 		user_addr_t user_addr;
1817 
1818 		user_addr = (cmd == SIOCGIFMEDIA64) ?
1819 		    ((struct ifmediareq64 *)ifmr)->ifmu_ulist :
1820 		    CAST_USER_ADDR_T(((struct ifmediareq32 *)ifmr)->ifmu_ulist);
1821 
1822 		ifmr->ifm_status = IFM_AVALID;
1823 		ifmr->ifm_mask = 0;
1824 		ifmr->ifm_count = 1;
1825 
1826 		BRIDGE_LOCK(sc);
1827 		if (!(sc->sc_flags & SCF_DETACHING) &&
1828 		    (sc->sc_flags & SCF_MEDIA_ACTIVE)) {
1829 			ifmr->ifm_status |= IFM_ACTIVE;
1830 			ifmr->ifm_active = ifmr->ifm_current =
1831 			    IFM_ETHER | IFM_AUTO;
1832 		} else {
1833 			ifmr->ifm_active = ifmr->ifm_current = IFM_NONE;
1834 		}
1835 		BRIDGE_UNLOCK(sc);
1836 
1837 		if (user_addr != USER_ADDR_NULL) {
1838 			error = copyout(&ifmr->ifm_current, user_addr,
1839 			    sizeof(int));
1840 		}
1841 		break;
1842 	}
1843 
1844 	case SIOCADDMULTI:
1845 	case SIOCDELMULTI:
1846 		break;
1847 
1848 	case SIOCSDRVSPEC32:
1849 	case SIOCGDRVSPEC32: {
1850 		union {
1851 			struct ifbreq ifbreq;
1852 			struct ifbifconf32 ifbifconf;
1853 			struct ifbareq32 ifbareq;
1854 			struct ifbaconf32 ifbaconf;
1855 			struct ifbrparam ifbrparam;
1856 			struct ifbropreq32 ifbropreq;
1857 		} args;
1858 		struct ifdrv32 *ifd = (struct ifdrv32 *)data;
1859 		const struct bridge_control *bridge_control_table =
1860 		    bridge_control_table32, *bc;
1861 
1862 		DRVSPEC;
1863 
1864 		break;
1865 	}
1866 	case SIOCSDRVSPEC64:
1867 	case SIOCGDRVSPEC64: {
1868 		union {
1869 			struct ifbreq ifbreq;
1870 			struct ifbifconf64 ifbifconf;
1871 			struct ifbareq64 ifbareq;
1872 			struct ifbaconf64 ifbaconf;
1873 			struct ifbrparam ifbrparam;
1874 			struct ifbropreq64 ifbropreq;
1875 		} args;
1876 		struct ifdrv64 *ifd = (struct ifdrv64 *)data;
1877 		const struct bridge_control *bridge_control_table =
1878 		    bridge_control_table64, *bc;
1879 
1880 		DRVSPEC;
1881 
1882 		break;
1883 	}
1884 
1885 	case SIOCSIFFLAGS:
1886 		if (!(ifp->if_flags & IFF_UP) &&
1887 		    (ifp->if_flags & IFF_RUNNING)) {
1888 			/*
1889 			 * If interface is marked down and it is running,
1890 			 * then stop and disable it.
1891 			 */
1892 			BRIDGE_LOCK(sc);
1893 			bridge_ifstop(ifp, 1);
1894 			BRIDGE_UNLOCK(sc);
1895 		} else if ((ifp->if_flags & IFF_UP) &&
1896 		    !(ifp->if_flags & IFF_RUNNING)) {
1897 			/*
1898 			 * If interface is marked up and it is stopped, then
1899 			 * start it.
1900 			 */
1901 			BRIDGE_LOCK(sc);
1902 			error = bridge_init(ifp);
1903 			BRIDGE_UNLOCK(sc);
1904 		}
1905 		break;
1906 
1907 	case SIOCSIFLLADDR:
1908 		error = ifnet_set_lladdr(ifp, ifr->ifr_addr.sa_data,
1909 		    ifr->ifr_addr.sa_len);
1910 		if (error != 0) {
1911 			BRIDGE_LOG(LOG_NOTICE, BR_DBGF_IOCTL,
1912 			    "%s SIOCSIFLLADDR error %d", ifp->if_xname,
1913 			    error);
1914 		}
1915 		break;
1916 
1917 	case SIOCSIFMTU:
1918 		if (ifr->ifr_mtu < 576) {
1919 			error = EINVAL;
1920 			break;
1921 		}
1922 		BRIDGE_LOCK(sc);
1923 		if (TAILQ_EMPTY(&sc->sc_iflist)) {
1924 			sc->sc_ifp->if_mtu = ifr->ifr_mtu;
1925 			BRIDGE_UNLOCK(sc);
1926 			break;
1927 		}
1928 		TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) {
1929 			if (bif->bif_ifp->if_mtu != (unsigned)ifr->ifr_mtu) {
1930 				BRIDGE_LOG(LOG_NOTICE, 0,
1931 				    "%s invalid MTU: %u(%s) != %d",
1932 				    sc->sc_ifp->if_xname,
1933 				    bif->bif_ifp->if_mtu,
1934 				    bif->bif_ifp->if_xname, ifr->ifr_mtu);
1935 				error = EINVAL;
1936 				break;
1937 			}
1938 		}
1939 		if (!error) {
1940 			sc->sc_ifp->if_mtu = ifr->ifr_mtu;
1941 		}
1942 		BRIDGE_UNLOCK(sc);
1943 		break;
1944 
1945 	default:
1946 		error = ether_ioctl(ifp, cmd, data);
1947 		if (error != 0 && error != EOPNOTSUPP) {
1948 			BRIDGE_LOG(LOG_NOTICE, BR_DBGF_IOCTL,
1949 			    "ifp %s cmd 0x%08lx "
1950 			    "(%c%c [%lu] %c %lu) failed error: %d",
1951 			    ifp->if_xname, cmd,
1952 			    (cmd & IOC_IN) ? 'I' : ' ',
1953 			    (cmd & IOC_OUT) ? 'O' : ' ',
1954 			    IOCPARM_LEN(cmd), (char)IOCGROUP(cmd),
1955 			    cmd & 0xff, error);
1956 		}
1957 		break;
1958 	}
1959 	BRIDGE_LOCK_ASSERT_NOTHELD(sc);
1960 
1961 	return error;
1962 }
1963 
1964 #if HAS_IF_CAP
1965 /*
1966  * bridge_mutecaps:
1967  *
1968  *	Clear or restore unwanted capabilities on the member interface
1969  */
1970 static void
bridge_mutecaps(struct bridge_softc * sc)1971 bridge_mutecaps(struct bridge_softc *sc)
1972 {
1973 	struct bridge_iflist *bif;
1974 	int enabled, mask;
1975 
1976 	/* Initial bitmask of capabilities to test */
1977 	mask = BRIDGE_IFCAPS_MASK;
1978 
1979 	TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) {
1980 		/* Every member must support it or its disabled */
1981 		mask &= bif->bif_savedcaps;
1982 	}
1983 
1984 	TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) {
1985 		enabled = bif->bif_ifp->if_capenable;
1986 		enabled &= ~BRIDGE_IFCAPS_STRIP;
1987 		/* strip off mask bits and enable them again if allowed */
1988 		enabled &= ~BRIDGE_IFCAPS_MASK;
1989 		enabled |= mask;
1990 
1991 		bridge_set_ifcap(sc, bif, enabled);
1992 	}
1993 }
1994 
1995 static void
bridge_set_ifcap(struct bridge_softc * sc,struct bridge_iflist * bif,int set)1996 bridge_set_ifcap(struct bridge_softc *sc, struct bridge_iflist *bif, int set)
1997 {
1998 	struct ifnet *ifp = bif->bif_ifp;
1999 	struct ifreq ifr;
2000 	int error;
2001 
2002 	bzero(&ifr, sizeof(ifr));
2003 	ifr.ifr_reqcap = set;
2004 
2005 	if (ifp->if_capenable != set) {
2006 		IFF_LOCKGIANT(ifp);
2007 		error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr);
2008 		IFF_UNLOCKGIANT(ifp);
2009 		if (error) {
2010 			BRIDGE_LOG(LOG_NOTICE, 0,
2011 			    "%s error setting interface capabilities on %s",
2012 			    sc->sc_ifp->if_xname, ifp->if_xname);
2013 		}
2014 	}
2015 }
2016 #endif /* HAS_IF_CAP */
2017 
2018 static errno_t
bridge_set_tso(struct bridge_softc * sc)2019 bridge_set_tso(struct bridge_softc *sc)
2020 {
2021 	struct bridge_iflist *bif;
2022 	u_int32_t tso_v4_mtu;
2023 	u_int32_t tso_v6_mtu;
2024 	ifnet_offload_t offload;
2025 	errno_t error = 0;
2026 
2027 	/* By default, support TSO */
2028 	offload = sc->sc_ifp->if_hwassist | IFNET_TSO_IPV4 | IFNET_TSO_IPV6;
2029 	tso_v4_mtu = IP_MAXPACKET;
2030 	tso_v6_mtu = IP_MAXPACKET;
2031 
2032 	/* Use the lowest common denominator of the members */
2033 	TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) {
2034 		ifnet_t ifp = bif->bif_ifp;
2035 
2036 		if (ifp == NULL) {
2037 			continue;
2038 		}
2039 
2040 		if (offload & IFNET_TSO_IPV4) {
2041 			if (ifp->if_hwassist & IFNET_TSO_IPV4) {
2042 				if (tso_v4_mtu > ifp->if_tso_v4_mtu) {
2043 					tso_v4_mtu = ifp->if_tso_v4_mtu;
2044 				}
2045 			} else {
2046 				offload &= ~IFNET_TSO_IPV4;
2047 				tso_v4_mtu = 0;
2048 			}
2049 		}
2050 		if (offload & IFNET_TSO_IPV6) {
2051 			if (ifp->if_hwassist & IFNET_TSO_IPV6) {
2052 				if (tso_v6_mtu > ifp->if_tso_v6_mtu) {
2053 					tso_v6_mtu = ifp->if_tso_v6_mtu;
2054 				}
2055 			} else {
2056 				offload &= ~IFNET_TSO_IPV6;
2057 				tso_v6_mtu = 0;
2058 			}
2059 		}
2060 	}
2061 
2062 	if (offload != sc->sc_ifp->if_hwassist) {
2063 		error = ifnet_set_offload(sc->sc_ifp, offload);
2064 		if (error != 0) {
2065 			BRIDGE_LOG(LOG_NOTICE, BR_DBGF_LIFECYCLE,
2066 			    "ifnet_set_offload(%s, 0x%x) failed %d",
2067 			    sc->sc_ifp->if_xname, offload, error);
2068 			goto done;
2069 		}
2070 		/*
2071 		 * For ifnet_set_tso_mtu() sake, the TSO MTU must be at least
2072 		 * as large as the interface MTU
2073 		 */
2074 		if (sc->sc_ifp->if_hwassist & IFNET_TSO_IPV4) {
2075 			if (tso_v4_mtu < sc->sc_ifp->if_mtu) {
2076 				tso_v4_mtu = sc->sc_ifp->if_mtu;
2077 			}
2078 			error = ifnet_set_tso_mtu(sc->sc_ifp, AF_INET,
2079 			    tso_v4_mtu);
2080 			if (error != 0) {
2081 				BRIDGE_LOG(LOG_NOTICE, BR_DBGF_LIFECYCLE,
2082 				    "ifnet_set_tso_mtu(%s, "
2083 				    "AF_INET, %u) failed %d",
2084 				    sc->sc_ifp->if_xname,
2085 				    tso_v4_mtu, error);
2086 				goto done;
2087 			}
2088 		}
2089 		if (sc->sc_ifp->if_hwassist & IFNET_TSO_IPV6) {
2090 			if (tso_v6_mtu < sc->sc_ifp->if_mtu) {
2091 				tso_v6_mtu = sc->sc_ifp->if_mtu;
2092 			}
2093 			error = ifnet_set_tso_mtu(sc->sc_ifp, AF_INET6,
2094 			    tso_v6_mtu);
2095 			if (error != 0) {
2096 				BRIDGE_LOG(LOG_NOTICE, BR_DBGF_LIFECYCLE,
2097 				    "ifnet_set_tso_mtu(%s, "
2098 				    "AF_INET6, %u) failed %d",
2099 				    sc->sc_ifp->if_xname,
2100 				    tso_v6_mtu, error);
2101 				goto done;
2102 			}
2103 		}
2104 	}
2105 done:
2106 	return error;
2107 }
2108 
2109 /*
2110  * bridge_lookup_member:
2111  *
2112  *	Lookup a bridge member interface.
2113  */
2114 static struct bridge_iflist *
bridge_lookup_member(struct bridge_softc * sc,const char * name)2115 bridge_lookup_member(struct bridge_softc *sc, const char *name)
2116 {
2117 	struct bridge_iflist *bif;
2118 	struct ifnet *ifp;
2119 
2120 	BRIDGE_LOCK_ASSERT_HELD(sc);
2121 
2122 	TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) {
2123 		ifp = bif->bif_ifp;
2124 		if (strcmp(ifp->if_xname, name) == 0) {
2125 			return bif;
2126 		}
2127 	}
2128 
2129 	return NULL;
2130 }
2131 
2132 /*
2133  * bridge_lookup_member_if:
2134  *
2135  *	Lookup a bridge member interface by ifnet*.
2136  */
2137 static struct bridge_iflist *
bridge_lookup_member_if(struct bridge_softc * sc,struct ifnet * member_ifp)2138 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
2139 {
2140 	struct bridge_iflist *bif;
2141 
2142 	BRIDGE_LOCK_ASSERT_HELD(sc);
2143 
2144 	TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) {
2145 		if (bif->bif_ifp == member_ifp) {
2146 			return bif;
2147 		}
2148 	}
2149 
2150 	return NULL;
2151 }
2152 
2153 static errno_t
bridge_iff_input(void * cookie,ifnet_t ifp,protocol_family_t protocol,mbuf_t * data,char ** frame_ptr)2154 bridge_iff_input(void *cookie, ifnet_t ifp, protocol_family_t protocol,
2155     mbuf_t *data, char **frame_ptr)
2156 {
2157 #pragma unused(protocol)
2158 	errno_t error = 0;
2159 	struct bridge_iflist *bif = (struct bridge_iflist *)cookie;
2160 	struct bridge_softc *sc = bif->bif_sc;
2161 	int included = 0;
2162 	size_t frmlen = 0;
2163 	mbuf_t m = *data;
2164 
2165 	if ((m->m_flags & M_PROTO1)) {
2166 		goto out;
2167 	}
2168 
2169 	if (*frame_ptr >= (char *)mbuf_datastart(m) &&
2170 	    *frame_ptr <= (char *)mbuf_data(m)) {
2171 		included = 1;
2172 		frmlen = (char *)mbuf_data(m) - *frame_ptr;
2173 	}
2174 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT,
2175 	    "%s from %s m 0x%llx data 0x%llx frame 0x%llx %s "
2176 	    "frmlen %lu", sc->sc_ifp->if_xname,
2177 	    ifp->if_xname, (uint64_t)VM_KERNEL_ADDRPERM(m),
2178 	    (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m)),
2179 	    (uint64_t)VM_KERNEL_ADDRPERM(*frame_ptr),
2180 	    included ? "inside" : "outside", frmlen);
2181 	if (BRIDGE_DBGF_ENABLED(BR_DBGF_MBUF)) {
2182 		brlog_mbuf(m, "bridge_iff_input[", "");
2183 		brlog_ether_header((struct ether_header *)
2184 		    (void *)*frame_ptr);
2185 		brlog_mbuf_data(m, 0, 20);
2186 	}
2187 	if (included == 0) {
2188 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT, "frame_ptr outside mbuf");
2189 		goto out;
2190 	}
2191 
2192 	/* Move data pointer to start of frame to the link layer header */
2193 	(void) mbuf_setdata(m, (char *)mbuf_data(m) - frmlen,
2194 	    mbuf_len(m) + frmlen);
2195 	(void) mbuf_pkthdr_adjustlen(m, frmlen);
2196 
2197 	/* make sure we can access the ethernet header */
2198 	if (mbuf_pkthdr_len(m) < sizeof(struct ether_header)) {
2199 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT,
2200 		    "short frame %lu < %lu",
2201 		    mbuf_pkthdr_len(m), sizeof(struct ether_header));
2202 		goto out;
2203 	}
2204 	if (mbuf_len(m) < sizeof(struct ether_header)) {
2205 		error = mbuf_pullup(data, sizeof(struct ether_header));
2206 		if (error != 0) {
2207 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT,
2208 			    "mbuf_pullup(%lu) failed %d",
2209 			    sizeof(struct ether_header),
2210 			    error);
2211 			error = EJUSTRETURN;
2212 			goto out;
2213 		}
2214 		if (m != *data) {
2215 			m = *data;
2216 			*frame_ptr = mbuf_data(m);
2217 		}
2218 	}
2219 
2220 	error = bridge_input(ifp, data);
2221 
2222 	/* Adjust packet back to original */
2223 	if (error == 0) {
2224 		/* bridge_input might have modified *data */
2225 		if (*data != m) {
2226 			m = *data;
2227 			*frame_ptr = mbuf_data(m);
2228 		}
2229 		(void) mbuf_setdata(m, (char *)mbuf_data(m) + frmlen,
2230 		    mbuf_len(m) - frmlen);
2231 		(void) mbuf_pkthdr_adjustlen(m, -frmlen);
2232 	}
2233 
2234 	if (BRIDGE_DBGF_ENABLED(BR_DBGF_MBUF) &&
2235 	    BRIDGE_DBGF_ENABLED(BR_DBGF_INPUT)) {
2236 		brlog_mbuf(m, "bridge_iff_input]", "");
2237 	}
2238 
2239 out:
2240 	BRIDGE_LOCK_ASSERT_NOTHELD(sc);
2241 
2242 	return error;
2243 }
2244 
2245 static errno_t
bridge_iff_output(void * cookie,ifnet_t ifp,protocol_family_t protocol,mbuf_t * data)2246 bridge_iff_output(void *cookie, ifnet_t ifp, protocol_family_t protocol,
2247     mbuf_t *data)
2248 {
2249 #pragma unused(protocol)
2250 	errno_t error = 0;
2251 	struct bridge_iflist *bif = (struct bridge_iflist *)cookie;
2252 	struct bridge_softc *sc = bif->bif_sc;
2253 	mbuf_t m = *data;
2254 
2255 	if ((m->m_flags & M_PROTO1)) {
2256 		goto out;
2257 	}
2258 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_OUTPUT,
2259 	    "%s from %s m 0x%llx data 0x%llx",
2260 	    sc->sc_ifp->if_xname, ifp->if_xname,
2261 	    (uint64_t)VM_KERNEL_ADDRPERM(m),
2262 	    (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m)));
2263 
2264 	error = bridge_member_output(sc, ifp, data);
2265 	if (error != 0 && error != EJUSTRETURN) {
2266 		BRIDGE_LOG(LOG_NOTICE, BR_DBGF_OUTPUT,
2267 		    "bridge_member_output failed error %d",
2268 		    error);
2269 	}
2270 out:
2271 	BRIDGE_LOCK_ASSERT_NOTHELD(sc);
2272 
2273 	return error;
2274 }
2275 
2276 static void
bridge_iff_event(void * cookie,ifnet_t ifp,protocol_family_t protocol,const struct kev_msg * event_msg)2277 bridge_iff_event(void *cookie, ifnet_t ifp, protocol_family_t protocol,
2278     const struct kev_msg *event_msg)
2279 {
2280 #pragma unused(protocol)
2281 	struct bridge_iflist *bif = (struct bridge_iflist *)cookie;
2282 	struct bridge_softc *sc = bif->bif_sc;
2283 
2284 	if (event_msg->vendor_code == KEV_VENDOR_APPLE &&
2285 	    event_msg->kev_class == KEV_NETWORK_CLASS &&
2286 	    event_msg->kev_subclass == KEV_DL_SUBCLASS) {
2287 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE,
2288 		    "%s event_code %u - %s",
2289 		    ifp->if_xname, event_msg->event_code,
2290 		    dlil_kev_dl_code_str(event_msg->event_code));
2291 
2292 		switch (event_msg->event_code) {
2293 		case KEV_DL_LINK_OFF:
2294 		case KEV_DL_LINK_ON: {
2295 			bridge_iflinkevent(ifp);
2296 #if BRIDGESTP
2297 			bstp_linkstate(ifp, event_msg->event_code);
2298 #endif /* BRIDGESTP */
2299 			break;
2300 		}
2301 		case KEV_DL_SIFFLAGS: {
2302 			if ((ifp->if_flags & IFF_UP) == 0) {
2303 				break;
2304 			}
2305 			if ((bif->bif_flags & BIFF_PROMISC) == 0) {
2306 				errno_t error;
2307 
2308 				error = ifnet_set_promiscuous(ifp, 1);
2309 				if (error != 0) {
2310 					BRIDGE_LOG(LOG_NOTICE, 0,
2311 					    "ifnet_set_promiscuous (%s)"
2312 					    " failed %d", ifp->if_xname,
2313 					    error);
2314 				} else {
2315 					bif->bif_flags |= BIFF_PROMISC;
2316 				}
2317 			}
2318 			if ((bif->bif_flags & BIFF_WIFI_INFRA) != 0 &&
2319 			    (bif->bif_flags & BIFF_ALL_MULTI) == 0) {
2320 				errno_t error;
2321 
2322 				error = if_allmulti(ifp, 1);
2323 				if (error != 0) {
2324 					BRIDGE_LOG(LOG_NOTICE, 0,
2325 					    "if_allmulti (%s)"
2326 					    " failed %d", ifp->if_xname,
2327 					    error);
2328 				} else {
2329 					bif->bif_flags |= BIFF_ALL_MULTI;
2330 #ifdef XNU_PLATFORM_AppleTVOS
2331 					ip6_forwarding = 1;
2332 #endif /* XNU_PLATFORM_AppleTVOS */
2333 				}
2334 			}
2335 			break;
2336 		}
2337 		case KEV_DL_IFCAP_CHANGED: {
2338 			BRIDGE_LOCK(sc);
2339 			bridge_set_tso(sc);
2340 			BRIDGE_UNLOCK(sc);
2341 			break;
2342 		}
2343 		case KEV_DL_PROTO_DETACHED:
2344 		case KEV_DL_PROTO_ATTACHED: {
2345 			bridge_proto_attach_changed(ifp);
2346 			break;
2347 		}
2348 		default:
2349 			break;
2350 		}
2351 	}
2352 }
2353 
2354 /*
2355  * bridge_iff_detached:
2356  *
2357  *      Called when our interface filter has been detached from a
2358  *      member interface.
2359  */
2360 static void
bridge_iff_detached(void * cookie,ifnet_t ifp)2361 bridge_iff_detached(void *cookie, ifnet_t ifp)
2362 {
2363 #pragma unused(cookie)
2364 	struct bridge_iflist *bif;
2365 	struct bridge_softc *sc = ifp->if_bridge;
2366 
2367 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE, "%s", ifp->if_xname);
2368 
2369 	/* Check if the interface is a bridge member */
2370 	if (sc != NULL) {
2371 		BRIDGE_LOCK(sc);
2372 		bif = bridge_lookup_member_if(sc, ifp);
2373 		if (bif != NULL) {
2374 			bridge_delete_member(sc, bif);
2375 		}
2376 		BRIDGE_UNLOCK(sc);
2377 		return;
2378 	}
2379 	/* Check if the interface is a span port */
2380 	lck_mtx_lock(&bridge_list_mtx);
2381 	LIST_FOREACH(sc, &bridge_list, sc_list) {
2382 		BRIDGE_LOCK(sc);
2383 		TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)
2384 		if (ifp == bif->bif_ifp) {
2385 			bridge_delete_span(sc, bif);
2386 			break;
2387 		}
2388 		BRIDGE_UNLOCK(sc);
2389 	}
2390 	lck_mtx_unlock(&bridge_list_mtx);
2391 }
2392 
2393 static errno_t
bridge_proto_input(ifnet_t ifp,protocol_family_t protocol,mbuf_t packet,char * header)2394 bridge_proto_input(ifnet_t ifp, protocol_family_t protocol, mbuf_t packet,
2395     char *header)
2396 {
2397 #pragma unused(protocol, packet, header)
2398 	BRIDGE_LOG(LOG_NOTICE, 0, "%s unexpected packet",
2399 	    ifp->if_xname);
2400 	return 0;
2401 }
2402 
2403 static int
bridge_attach_protocol(struct ifnet * ifp)2404 bridge_attach_protocol(struct ifnet *ifp)
2405 {
2406 	int     error;
2407 	struct ifnet_attach_proto_param reg;
2408 
2409 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE, "%s", ifp->if_xname);
2410 	bzero(&reg, sizeof(reg));
2411 	reg.input = bridge_proto_input;
2412 
2413 	error = ifnet_attach_protocol(ifp, PF_BRIDGE, &reg);
2414 	if (error) {
2415 		BRIDGE_LOG(LOG_NOTICE, 0,
2416 		    "ifnet_attach_protocol(%s) failed, %d",
2417 		    ifp->if_xname, error);
2418 	}
2419 
2420 	return error;
2421 }
2422 
2423 static int
bridge_detach_protocol(struct ifnet * ifp)2424 bridge_detach_protocol(struct ifnet *ifp)
2425 {
2426 	int     error;
2427 
2428 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE, "%s", ifp->if_xname);
2429 	error = ifnet_detach_protocol(ifp, PF_BRIDGE);
2430 	if (error) {
2431 		BRIDGE_LOG(LOG_NOTICE, 0,
2432 		    "ifnet_detach_protocol(%s) failed, %d",
2433 		    ifp->if_xname, error);
2434 	}
2435 
2436 	return error;
2437 }
2438 
2439 /*
2440  * bridge_delete_member:
2441  *
2442  *	Delete the specified member interface.
2443  */
2444 static void
bridge_delete_member(struct bridge_softc * sc,struct bridge_iflist * bif)2445 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif)
2446 {
2447 	uint32_t    bif_flags;
2448 	struct ifnet *ifs = bif->bif_ifp, *bifp = sc->sc_ifp;
2449 	int lladdr_changed = 0, error;
2450 	uint8_t eaddr[ETHER_ADDR_LEN];
2451 	u_int32_t event_code = 0;
2452 
2453 	BRIDGE_LOCK_ASSERT_HELD(sc);
2454 	VERIFY(ifs != NULL);
2455 
2456 	/*
2457 	 * Remove the member from the list first so it cannot be found anymore
2458 	 * when we release the bridge lock below
2459 	 */
2460 	if ((bif->bif_flags & BIFF_IN_MEMBER_LIST) != 0) {
2461 		BRIDGE_XLOCK(sc);
2462 		TAILQ_REMOVE(&sc->sc_iflist, bif, bif_next);
2463 		BRIDGE_XDROP(sc);
2464 	}
2465 	if (sc->sc_mac_nat_bif != NULL) {
2466 		if (bif == sc->sc_mac_nat_bif) {
2467 			bridge_mac_nat_disable(sc);
2468 		} else {
2469 			bridge_mac_nat_flush_entries(sc, bif);
2470 		}
2471 	}
2472 #if BRIDGESTP
2473 	if ((bif->bif_ifflags & IFBIF_STP) != 0) {
2474 		bstp_disable(&bif->bif_stp);
2475 	}
2476 #endif /* BRIDGESTP */
2477 
2478 	/*
2479 	 * If removing the interface that gave the bridge its mac address, set
2480 	 * the mac address of the bridge to the address of the next member, or
2481 	 * to its default address if no members are left.
2482 	 */
2483 	if (bridge_inherit_mac && sc->sc_ifaddr == ifs) {
2484 		ifnet_release(sc->sc_ifaddr);
2485 		if (TAILQ_EMPTY(&sc->sc_iflist)) {
2486 			bcopy(sc->sc_defaddr, eaddr, ETHER_ADDR_LEN);
2487 			sc->sc_ifaddr = NULL;
2488 		} else {
2489 			struct ifnet *fif =
2490 			    TAILQ_FIRST(&sc->sc_iflist)->bif_ifp;
2491 			bcopy(IF_LLADDR(fif), eaddr, ETHER_ADDR_LEN);
2492 			sc->sc_ifaddr = fif;
2493 			ifnet_reference(fif);   /* for sc_ifaddr */
2494 		}
2495 		lladdr_changed = 1;
2496 	}
2497 
2498 #if HAS_IF_CAP
2499 	bridge_mutecaps(sc);    /* recalculate now this interface is removed */
2500 #endif /* HAS_IF_CAP */
2501 
2502 	error = bridge_set_tso(sc);
2503 	if (error != 0) {
2504 		BRIDGE_LOG(LOG_NOTICE, 0, "bridge_set_tso failed %d", error);
2505 	}
2506 
2507 	bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
2508 
2509 	KASSERT(bif->bif_addrcnt == 0,
2510 	    ("%s: %d bridge routes referenced", __func__, bif->bif_addrcnt));
2511 
2512 	/*
2513 	 * Update link status of the bridge based on its remaining members
2514 	 */
2515 	event_code = bridge_updatelinkstatus(sc);
2516 	bif_flags = bif->bif_flags;
2517 	BRIDGE_UNLOCK(sc);
2518 
2519 	/* only perform these steps if the interface is still attached */
2520 	if (ifnet_is_attached(ifs, 1)) {
2521 #if SKYWALK
2522 		if ((bif_flags & BIFF_NETAGENT_REMOVED) != 0) {
2523 			ifnet_add_netagent(ifs);
2524 		}
2525 		if ((bif_flags & BIFF_FLOWSWITCH_ATTACHED) != 0) {
2526 			ifnet_detach_flowswitch_nexus(ifs);
2527 		}
2528 #endif /* SKYWALK */
2529 		/* disable promiscuous mode */
2530 		if ((bif_flags & BIFF_PROMISC) != 0) {
2531 			(void) ifnet_set_promiscuous(ifs, 0);
2532 		}
2533 		/* disable all multi */
2534 		if ((bif_flags & BIFF_ALL_MULTI) != 0) {
2535 			(void)if_allmulti(ifs, 0);
2536 		}
2537 #if HAS_IF_CAP
2538 		/* re-enable any interface capabilities */
2539 		bridge_set_ifcap(sc, bif, bif->bif_savedcaps);
2540 #endif
2541 		/* detach bridge "protocol" */
2542 		if ((bif_flags & BIFF_PROTO_ATTACHED) != 0) {
2543 			(void)bridge_detach_protocol(ifs);
2544 		}
2545 		/* detach interface filter */
2546 		if ((bif_flags & BIFF_FILTER_ATTACHED) != 0) {
2547 			iflt_detach(bif->bif_iff_ref);
2548 		}
2549 		ifnet_decr_iorefcnt(ifs);
2550 	}
2551 
2552 	if (lladdr_changed &&
2553 	    (error = ifnet_set_lladdr(bifp, eaddr, ETHER_ADDR_LEN)) != 0) {
2554 		BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_lladdr failed %d", error);
2555 	}
2556 
2557 	if (event_code != 0) {
2558 		bridge_link_event(bifp, event_code);
2559 	}
2560 
2561 #if BRIDGESTP
2562 	bstp_destroy(&bif->bif_stp);    /* prepare to free */
2563 #endif /* BRIDGESTP */
2564 
2565 	kfree_type(struct bridge_iflist, bif);
2566 	ifs->if_bridge = NULL;
2567 	ifnet_release(ifs);
2568 
2569 	BRIDGE_LOCK(sc);
2570 }
2571 
2572 /*
2573  * bridge_delete_span:
2574  *
2575  *	Delete the specified span interface.
2576  */
2577 static void
bridge_delete_span(struct bridge_softc * sc,struct bridge_iflist * bif)2578 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
2579 {
2580 	BRIDGE_LOCK_ASSERT_HELD(sc);
2581 
2582 	KASSERT(bif->bif_ifp->if_bridge == NULL,
2583 	    ("%s: not a span interface", __func__));
2584 
2585 	ifnet_release(bif->bif_ifp);
2586 
2587 	TAILQ_REMOVE(&sc->sc_spanlist, bif, bif_next);
2588 	kfree_type(struct bridge_iflist, bif);
2589 }
2590 
2591 static int
bridge_ioctl_add(struct bridge_softc * sc,void * arg)2592 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
2593 {
2594 	struct ifbreq *req = arg;
2595 	struct bridge_iflist *bif = NULL;
2596 	struct ifnet *ifs, *bifp = sc->sc_ifp;
2597 	int error = 0, lladdr_changed = 0;
2598 	uint8_t eaddr[ETHER_ADDR_LEN];
2599 	struct iff_filter iff;
2600 	u_int32_t event_code = 0;
2601 	boolean_t input_broadcast;
2602 	boolean_t wifi_infra = FALSE;
2603 
2604 	ifs = ifunit(req->ifbr_ifsname);
2605 	if (ifs == NULL) {
2606 		return ENOENT;
2607 	}
2608 	if (ifs->if_ioctl == NULL) {    /* must be supported */
2609 		return EINVAL;
2610 	}
2611 
2612 	if (IFNET_IS_INTCOPROC(ifs)) {
2613 		return EINVAL;
2614 	}
2615 
2616 	/* If it's in the span list, it can't be a member. */
2617 	TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2618 		if (ifs == bif->bif_ifp) {
2619 			return EBUSY;
2620 		}
2621 	}
2622 
2623 	if (ifs->if_bridge == sc) {
2624 		return EEXIST;
2625 	}
2626 
2627 	if (ifs->if_bridge != NULL) {
2628 		return EBUSY;
2629 	}
2630 
2631 	switch (ifs->if_type) {
2632 	case IFT_ETHER:
2633 		if (strcmp(ifs->if_name, "en") == 0 &&
2634 		    ifs->if_subfamily == IFNET_SUBFAMILY_WIFI &&
2635 		    (ifs->if_eflags & IFEF_IPV4_ROUTER) == 0) {
2636 			/* XXX is there a better way to identify Wi-Fi STA? */
2637 			wifi_infra = TRUE;
2638 		}
2639 		break;
2640 	case IFT_L2VLAN:
2641 	case IFT_IEEE8023ADLAG:
2642 		break;
2643 	case IFT_GIF:
2644 	/* currently not supported */
2645 	/* FALLTHRU */
2646 	default:
2647 		return EINVAL;
2648 	}
2649 
2650 	/* fail to add the interface if the MTU doesn't match */
2651 	if (!TAILQ_EMPTY(&sc->sc_iflist) && sc->sc_ifp->if_mtu != ifs->if_mtu) {
2652 		BRIDGE_LOG(LOG_NOTICE, 0, "%s invalid MTU for %s",
2653 		    sc->sc_ifp->if_xname,
2654 		    ifs->if_xname);
2655 		return EINVAL;
2656 	}
2657 
2658 	/* there's already an interface that's doing MAC NAT */
2659 	if (wifi_infra && sc->sc_mac_nat_bif != NULL) {
2660 		return EBUSY;
2661 	}
2662 
2663 	/* prevent the interface from detaching while we add the member */
2664 	if (!ifnet_is_attached(ifs, 1)) {
2665 		return ENXIO;
2666 	}
2667 
2668 	/* allocate a new member */
2669 	bif = kalloc_type(struct bridge_iflist, Z_WAITOK | Z_ZERO | Z_NOFAIL);
2670 	bif->bif_ifp = ifs;
2671 	ifnet_reference(ifs);
2672 	bif->bif_ifflags |= IFBIF_LEARNING | IFBIF_DISCOVER;
2673 #if HAS_IF_CAP
2674 	bif->bif_savedcaps = ifs->if_capenable;
2675 #endif /* HAS_IF_CAP */
2676 	bif->bif_sc = sc;
2677 	if (wifi_infra) {
2678 		(void)bridge_mac_nat_enable(sc, bif);
2679 	}
2680 
2681 	if (IFNET_IS_VMNET(ifs)) {
2682 		allocate_vmnet_pf_tags();
2683 	}
2684 	/* Allow the first Ethernet member to define the MTU */
2685 	if (TAILQ_EMPTY(&sc->sc_iflist)) {
2686 		sc->sc_ifp->if_mtu = ifs->if_mtu;
2687 	}
2688 
2689 	/*
2690 	 * Assign the interface's MAC address to the bridge if it's the first
2691 	 * member and the MAC address of the bridge has not been changed from
2692 	 * the default (randomly) generated one.
2693 	 */
2694 	if (bridge_inherit_mac && TAILQ_EMPTY(&sc->sc_iflist) &&
2695 	    _ether_cmp(IF_LLADDR(sc->sc_ifp), sc->sc_defaddr) == 0) {
2696 		bcopy(IF_LLADDR(ifs), eaddr, ETHER_ADDR_LEN);
2697 		sc->sc_ifaddr = ifs;
2698 		ifnet_reference(ifs);   /* for sc_ifaddr */
2699 		lladdr_changed = 1;
2700 	}
2701 
2702 	ifs->if_bridge = sc;
2703 #if BRIDGESTP
2704 	bstp_create(&sc->sc_stp, &bif->bif_stp, bif->bif_ifp);
2705 #endif /* BRIDGESTP */
2706 
2707 #if HAS_IF_CAP
2708 	/* Set interface capabilities to the intersection set of all members */
2709 	bridge_mutecaps(sc);
2710 #endif /* HAS_IF_CAP */
2711 
2712 
2713 	/*
2714 	 * Respect lock ordering with DLIL lock for the following operations
2715 	 */
2716 	BRIDGE_UNLOCK(sc);
2717 
2718 	/* enable promiscuous mode */
2719 	error = ifnet_set_promiscuous(ifs, 1);
2720 	switch (error) {
2721 	case 0:
2722 		bif->bif_flags |= BIFF_PROMISC;
2723 		break;
2724 	case ENETDOWN:
2725 	case EPWROFF:
2726 		BRIDGE_LOG(LOG_NOTICE, 0,
2727 		    "ifnet_set_promiscuous(%s) failed %d, ignoring",
2728 		    ifs->if_xname, error);
2729 		/* Ignore error when device is not up */
2730 		error = 0;
2731 		break;
2732 	default:
2733 		BRIDGE_LOG(LOG_NOTICE, 0,
2734 		    "ifnet_set_promiscuous(%s) failed %d",
2735 		    ifs->if_xname, error);
2736 		BRIDGE_LOCK(sc);
2737 		goto out;
2738 	}
2739 	if (wifi_infra) {
2740 		int this_error;
2741 
2742 		/* Wi-Fi doesn't really support promiscuous, set allmulti */
2743 		bif->bif_flags |= BIFF_WIFI_INFRA;
2744 		this_error = if_allmulti(ifs, 1);
2745 		if (this_error == 0) {
2746 			bif->bif_flags |= BIFF_ALL_MULTI;
2747 #ifdef XNU_PLATFORM_AppleTVOS
2748 			ip6_forwarding = 1;
2749 #endif /* XNU_PLATFORM_AppleTVOS */
2750 		} else {
2751 			BRIDGE_LOG(LOG_NOTICE, 0,
2752 			    "if_allmulti(%s) failed %d, ignoring",
2753 			    ifs->if_xname, this_error);
2754 		}
2755 	}
2756 #if SKYWALK
2757 	/* ensure that the flowswitch is present for native interface */
2758 	if (SKYWALK_NATIVE(ifs)) {
2759 		if (ifnet_attach_flowswitch_nexus(ifs)) {
2760 			bif->bif_flags |= BIFF_FLOWSWITCH_ATTACHED;
2761 		}
2762 	}
2763 	/* remove the netagent on the flowswitch (rdar://75050182) */
2764 	if (ifnet_remove_netagent(ifs)) {
2765 		bif->bif_flags |= BIFF_NETAGENT_REMOVED;
2766 	}
2767 #endif /* SKYWALK */
2768 
2769 	/*
2770 	 * install an interface filter
2771 	 */
2772 	memset(&iff, 0, sizeof(struct iff_filter));
2773 	iff.iff_cookie = bif;
2774 	iff.iff_name = "com.apple.kernel.bsd.net.if_bridge";
2775 	iff.iff_input = bridge_iff_input;
2776 	iff.iff_output = bridge_iff_output;
2777 	iff.iff_event = bridge_iff_event;
2778 	iff.iff_detached = bridge_iff_detached;
2779 	error = dlil_attach_filter(ifs, &iff, &bif->bif_iff_ref,
2780 	    DLIL_IFF_TSO | DLIL_IFF_INTERNAL);
2781 	if (error != 0) {
2782 		BRIDGE_LOG(LOG_NOTICE, 0, "iflt_attach failed %d", error);
2783 		BRIDGE_LOCK(sc);
2784 		goto out;
2785 	}
2786 	bif->bif_flags |= BIFF_FILTER_ATTACHED;
2787 
2788 	/*
2789 	 * install a dummy "bridge" protocol
2790 	 */
2791 	if ((error = bridge_attach_protocol(ifs)) != 0) {
2792 		if (error != 0) {
2793 			BRIDGE_LOG(LOG_NOTICE, 0,
2794 			    "bridge_attach_protocol failed %d", error);
2795 			BRIDGE_LOCK(sc);
2796 			goto out;
2797 		}
2798 	}
2799 	bif->bif_flags |= BIFF_PROTO_ATTACHED;
2800 
2801 	if (lladdr_changed &&
2802 	    (error = ifnet_set_lladdr(bifp, eaddr, ETHER_ADDR_LEN)) != 0) {
2803 		BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_lladdr failed %d", error);
2804 	}
2805 
2806 	/*
2807 	 * No failures past this point. Add the member to the list.
2808 	 */
2809 	BRIDGE_LOCK(sc);
2810 	bif->bif_flags |= BIFF_IN_MEMBER_LIST;
2811 	BRIDGE_XLOCK(sc);
2812 	TAILQ_INSERT_TAIL(&sc->sc_iflist, bif, bif_next);
2813 	BRIDGE_XDROP(sc);
2814 
2815 	/* cache the member link status */
2816 	if (interface_media_active(ifs)) {
2817 		bif->bif_flags |= BIFF_MEDIA_ACTIVE;
2818 	} else {
2819 		bif->bif_flags &= ~BIFF_MEDIA_ACTIVE;
2820 	}
2821 
2822 	/* the new member may change the link status of the bridge interface */
2823 	event_code = bridge_updatelinkstatus(sc);
2824 
2825 	/* check whether we need input broadcast or not */
2826 	input_broadcast = interface_needs_input_broadcast(ifs);
2827 	bif_set_input_broadcast(bif, input_broadcast);
2828 	BRIDGE_UNLOCK(sc);
2829 
2830 	if (event_code != 0) {
2831 		bridge_link_event(bifp, event_code);
2832 	}
2833 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE,
2834 	    "%s input broadcast %s", ifs->if_xname,
2835 	    input_broadcast ? "ENABLED" : "DISABLED");
2836 
2837 	BRIDGE_LOCK(sc);
2838 	bridge_set_tso(sc);
2839 
2840 out:
2841 	/* allow the interface to detach */
2842 	ifnet_decr_iorefcnt(ifs);
2843 
2844 	if (error != 0) {
2845 		if (bif != NULL) {
2846 			bridge_delete_member(sc, bif);
2847 		}
2848 	} else if (IFNET_IS_VMNET(ifs)) {
2849 		INC_ATOMIC_INT64_LIM(net_api_stats.nas_vmnet_total);
2850 	}
2851 
2852 	return error;
2853 }
2854 
2855 static int
bridge_ioctl_del(struct bridge_softc * sc,void * arg)2856 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
2857 {
2858 	struct ifbreq *req = arg;
2859 	struct bridge_iflist *bif;
2860 
2861 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
2862 	if (bif == NULL) {
2863 		return ENOENT;
2864 	}
2865 
2866 	bridge_delete_member(sc, bif);
2867 
2868 	return 0;
2869 }
2870 
2871 static int
bridge_ioctl_purge(struct bridge_softc * sc,void * arg)2872 bridge_ioctl_purge(struct bridge_softc *sc, void *arg)
2873 {
2874 #pragma unused(sc, arg)
2875 	return 0;
2876 }
2877 
2878 static int
bridge_ioctl_gifflags(struct bridge_softc * sc,void * arg)2879 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
2880 {
2881 	struct ifbreq *req = arg;
2882 	struct bridge_iflist *bif;
2883 
2884 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
2885 	if (bif == NULL) {
2886 		return ENOENT;
2887 	}
2888 
2889 	struct bstp_port *bp;
2890 
2891 	bp = &bif->bif_stp;
2892 	req->ifbr_state = bp->bp_state;
2893 	req->ifbr_priority = bp->bp_priority;
2894 	req->ifbr_path_cost = bp->bp_path_cost;
2895 	req->ifbr_proto = bp->bp_protover;
2896 	req->ifbr_role = bp->bp_role;
2897 	req->ifbr_stpflags = bp->bp_flags;
2898 	req->ifbr_ifsflags = bif->bif_ifflags;
2899 
2900 	/* Copy STP state options as flags */
2901 	if (bp->bp_operedge) {
2902 		req->ifbr_ifsflags |= IFBIF_BSTP_EDGE;
2903 	}
2904 	if (bp->bp_flags & BSTP_PORT_AUTOEDGE) {
2905 		req->ifbr_ifsflags |= IFBIF_BSTP_AUTOEDGE;
2906 	}
2907 	if (bp->bp_ptp_link) {
2908 		req->ifbr_ifsflags |= IFBIF_BSTP_PTP;
2909 	}
2910 	if (bp->bp_flags & BSTP_PORT_AUTOPTP) {
2911 		req->ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP;
2912 	}
2913 	if (bp->bp_flags & BSTP_PORT_ADMEDGE) {
2914 		req->ifbr_ifsflags |= IFBIF_BSTP_ADMEDGE;
2915 	}
2916 	if (bp->bp_flags & BSTP_PORT_ADMCOST) {
2917 		req->ifbr_ifsflags |= IFBIF_BSTP_ADMCOST;
2918 	}
2919 
2920 	req->ifbr_portno = bif->bif_ifp->if_index & 0xfff;
2921 	req->ifbr_addrcnt = bif->bif_addrcnt;
2922 	req->ifbr_addrmax = bif->bif_addrmax;
2923 	req->ifbr_addrexceeded = bif->bif_addrexceeded;
2924 
2925 	return 0;
2926 }
2927 
2928 static int
bridge_ioctl_sifflags(struct bridge_softc * sc,void * arg)2929 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
2930 {
2931 	struct ifbreq *req = arg;
2932 	struct bridge_iflist *bif;
2933 #if BRIDGESTP
2934 	struct bstp_port *bp;
2935 	int error;
2936 #endif /* BRIDGESTP */
2937 
2938 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
2939 	if (bif == NULL) {
2940 		return ENOENT;
2941 	}
2942 
2943 	if (req->ifbr_ifsflags & IFBIF_SPAN) {
2944 		/* SPAN is readonly */
2945 		return EINVAL;
2946 	}
2947 #define _EXCLUSIVE_FLAGS        (IFBIF_CHECKSUM_OFFLOAD | IFBIF_MAC_NAT)
2948 	if ((req->ifbr_ifsflags & _EXCLUSIVE_FLAGS) == _EXCLUSIVE_FLAGS) {
2949 		/* can't specify both MAC-NAT and checksum offload */
2950 		return EINVAL;
2951 	}
2952 	if ((req->ifbr_ifsflags & IFBIF_MAC_NAT) != 0) {
2953 		errno_t error;
2954 
2955 		error = bridge_mac_nat_enable(sc, bif);
2956 		if (error != 0) {
2957 			return error;
2958 		}
2959 	} else if (sc->sc_mac_nat_bif == bif) {
2960 		bridge_mac_nat_disable(sc);
2961 	}
2962 
2963 
2964 #if BRIDGESTP
2965 	if (req->ifbr_ifsflags & IFBIF_STP) {
2966 		if ((bif->bif_ifflags & IFBIF_STP) == 0) {
2967 			error = bstp_enable(&bif->bif_stp);
2968 			if (error) {
2969 				return error;
2970 			}
2971 		}
2972 	} else {
2973 		if ((bif->bif_ifflags & IFBIF_STP) != 0) {
2974 			bstp_disable(&bif->bif_stp);
2975 		}
2976 	}
2977 
2978 	/* Pass on STP flags */
2979 	bp = &bif->bif_stp;
2980 	bstp_set_edge(bp, req->ifbr_ifsflags & IFBIF_BSTP_EDGE ? 1 : 0);
2981 	bstp_set_autoedge(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOEDGE ? 1 : 0);
2982 	bstp_set_ptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_PTP ? 1 : 0);
2983 	bstp_set_autoptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOPTP ? 1 : 0);
2984 #else /* !BRIDGESTP */
2985 	if (req->ifbr_ifsflags & IFBIF_STP) {
2986 		return EOPNOTSUPP;
2987 	}
2988 #endif /* !BRIDGESTP */
2989 
2990 	/* Save the bits relating to the bridge */
2991 	bif->bif_ifflags = req->ifbr_ifsflags & IFBIFMASK;
2992 
2993 
2994 	return 0;
2995 }
2996 
2997 static int
bridge_ioctl_scache(struct bridge_softc * sc,void * arg)2998 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
2999 {
3000 	struct ifbrparam *param = arg;
3001 
3002 	sc->sc_brtmax = param->ifbrp_csize;
3003 	bridge_rttrim(sc);
3004 	return 0;
3005 }
3006 
3007 static int
bridge_ioctl_gcache(struct bridge_softc * sc,void * arg)3008 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
3009 {
3010 	struct ifbrparam *param = arg;
3011 
3012 	param->ifbrp_csize = sc->sc_brtmax;
3013 
3014 	return 0;
3015 }
3016 
3017 #define BRIDGE_IOCTL_GIFS do { \
3018 	struct bridge_iflist *bif;                                      \
3019 	struct ifbreq breq;                                             \
3020 	char *buf, *outbuf;                                             \
3021 	unsigned int count, buflen, len;                                \
3022                                                                         \
3023 	count = 0;                                                      \
3024 	TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next)                    \
3025 	        count++;                                                \
3026 	TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)                  \
3027 	        count++;                                                \
3028                                                                         \
3029 	buflen = sizeof (breq) * count;                                 \
3030 	if (bifc->ifbic_len == 0) {                                     \
3031 	        bifc->ifbic_len = buflen;                               \
3032 	        return (0);                                             \
3033 	}                                                               \
3034 	BRIDGE_UNLOCK(sc);                                              \
3035 	outbuf = (char *)kalloc_data(buflen, Z_WAITOK | Z_ZERO);        \
3036 	BRIDGE_LOCK(sc);                                                \
3037                                                                         \
3038 	count = 0;                                                      \
3039 	buf = outbuf;                                                   \
3040 	len = min(bifc->ifbic_len, buflen);                             \
3041 	bzero(&breq, sizeof (breq));                                    \
3042 	TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) {                  \
3043 	        if (len < sizeof (breq))                                \
3044 	                break;                                          \
3045                                                                         \
3046 	        snprintf(breq.ifbr_ifsname, sizeof (breq.ifbr_ifsname), \
3047 	            "%s", bif->bif_ifp->if_xname);                      \
3048 	/* Fill in the ifbreq structure */                      \
3049 	        error = bridge_ioctl_gifflags(sc, &breq);               \
3050 	        if (error)                                              \
3051 	                break;                                          \
3052 	        memcpy(buf, &breq, sizeof (breq));                      \
3053 	        count++;                                                \
3054 	        buf += sizeof (breq);                                   \
3055 	        len -= sizeof (breq);                                   \
3056 	}                                                               \
3057 	TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) {                \
3058 	        if (len < sizeof (breq))                                \
3059 	                break;                                          \
3060                                                                         \
3061 	        snprintf(breq.ifbr_ifsname,                             \
3062 	                 sizeof (breq.ifbr_ifsname),                    \
3063 	                 "%s", bif->bif_ifp->if_xname);                 \
3064 	        breq.ifbr_ifsflags = bif->bif_ifflags;                  \
3065 	        breq.ifbr_portno                                        \
3066 	                = bif->bif_ifp->if_index & 0xfff;               \
3067 	        memcpy(buf, &breq, sizeof (breq));                      \
3068 	        count++;                                                \
3069 	        buf += sizeof (breq);                                   \
3070 	        len -= sizeof (breq);                                   \
3071 	}                                                               \
3072                                                                         \
3073 	BRIDGE_UNLOCK(sc);                                              \
3074 	bifc->ifbic_len = sizeof (breq) * count;                        \
3075 	error = copyout(outbuf, bifc->ifbic_req, bifc->ifbic_len);      \
3076 	BRIDGE_LOCK(sc);                                                \
3077 	kfree_data(outbuf, buflen);                                     \
3078 } while (0)
3079 
3080 static int
bridge_ioctl_gifs64(struct bridge_softc * sc,void * arg)3081 bridge_ioctl_gifs64(struct bridge_softc *sc, void *arg)
3082 {
3083 	struct ifbifconf64 *bifc = arg;
3084 	int error = 0;
3085 
3086 	BRIDGE_IOCTL_GIFS;
3087 
3088 	return error;
3089 }
3090 
3091 static int
bridge_ioctl_gifs32(struct bridge_softc * sc,void * arg)3092 bridge_ioctl_gifs32(struct bridge_softc *sc, void *arg)
3093 {
3094 	struct ifbifconf32 *bifc = arg;
3095 	int error = 0;
3096 
3097 	BRIDGE_IOCTL_GIFS;
3098 
3099 	return error;
3100 }
3101 
3102 #define BRIDGE_IOCTL_RTS do {                                               \
3103 	struct bridge_rtnode *brt;                                          \
3104 	char *buf;                                                          \
3105 	char *outbuf = NULL;                                                \
3106 	unsigned int count, buflen, len;                                    \
3107 	unsigned long now;                                                  \
3108                                                                             \
3109 	if (bac->ifbac_len == 0)                                            \
3110 	        return (0);                                                 \
3111                                                                             \
3112 	bzero(&bareq, sizeof (bareq));                                      \
3113 	count = 0;                                                          \
3114 	LIST_FOREACH(brt, &sc->sc_rtlist, brt_list)                         \
3115 	        count++;                                                    \
3116 	buflen = sizeof (bareq) * count;                                    \
3117                                                                             \
3118 	BRIDGE_UNLOCK(sc);                                                  \
3119 	outbuf = (char *)kalloc_data(buflen, Z_WAITOK | Z_ZERO);            \
3120 	BRIDGE_LOCK(sc);                                                    \
3121                                                                             \
3122 	count = 0;                                                          \
3123 	buf = outbuf;                                                       \
3124 	len = min(bac->ifbac_len, buflen);                                  \
3125 	LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {                       \
3126 	        if (len < sizeof (bareq))                                   \
3127 	                goto out;                                           \
3128 	        snprintf(bareq.ifba_ifsname, sizeof (bareq.ifba_ifsname),   \
3129 	                 "%s", brt->brt_ifp->if_xname);                     \
3130 	        memcpy(bareq.ifba_dst, brt->brt_addr, sizeof (brt->brt_addr)); \
3131 	        bareq.ifba_vlan = brt->brt_vlan;                            \
3132 	        if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {   \
3133 	                now = (unsigned long) net_uptime();                 \
3134 	                if (now < brt->brt_expire)                          \
3135 	                        bareq.ifba_expire =                         \
3136 	                            brt->brt_expire - now;                  \
3137 	        } else                                                      \
3138 	                bareq.ifba_expire = 0;                              \
3139 	        bareq.ifba_flags = brt->brt_flags;                          \
3140                                                                             \
3141 	        memcpy(buf, &bareq, sizeof (bareq));                        \
3142 	        count++;                                                    \
3143 	        buf += sizeof (bareq);                                      \
3144 	        len -= sizeof (bareq);                                      \
3145 	}                                                                   \
3146 out:                                                                        \
3147 	bac->ifbac_len = sizeof (bareq) * count;                            \
3148 	if (outbuf != NULL) {                                               \
3149 	        BRIDGE_UNLOCK(sc);                                          \
3150 	        error = copyout(outbuf, bac->ifbac_req, bac->ifbac_len);    \
3151 	        kfree_data(outbuf, buflen);                                 \
3152 	        BRIDGE_LOCK(sc);                                            \
3153 	}                                                                   \
3154 	return (error);                                                     \
3155 } while (0)
3156 
3157 static int
bridge_ioctl_rts64(struct bridge_softc * sc,void * arg)3158 bridge_ioctl_rts64(struct bridge_softc *sc, void *arg)
3159 {
3160 	struct ifbaconf64 *bac = arg;
3161 	struct ifbareq64 bareq;
3162 	int error = 0;
3163 
3164 	BRIDGE_IOCTL_RTS;
3165 	return error;
3166 }
3167 
3168 static int
bridge_ioctl_rts32(struct bridge_softc * sc,void * arg)3169 bridge_ioctl_rts32(struct bridge_softc *sc, void *arg)
3170 {
3171 	struct ifbaconf32 *bac = arg;
3172 	struct ifbareq32 bareq;
3173 	int error = 0;
3174 
3175 	BRIDGE_IOCTL_RTS;
3176 	return error;
3177 }
3178 
3179 static int
bridge_ioctl_saddr32(struct bridge_softc * sc,void * arg)3180 bridge_ioctl_saddr32(struct bridge_softc *sc, void *arg)
3181 {
3182 	struct ifbareq32 *req = arg;
3183 	struct bridge_iflist *bif;
3184 	int error;
3185 
3186 	bif = bridge_lookup_member(sc, req->ifba_ifsname);
3187 	if (bif == NULL) {
3188 		return ENOENT;
3189 	}
3190 
3191 	error = bridge_rtupdate(sc, req->ifba_dst, req->ifba_vlan, bif, 1,
3192 	    req->ifba_flags);
3193 
3194 	return error;
3195 }
3196 
3197 static int
bridge_ioctl_saddr64(struct bridge_softc * sc,void * arg)3198 bridge_ioctl_saddr64(struct bridge_softc *sc, void *arg)
3199 {
3200 	struct ifbareq64 *req = arg;
3201 	struct bridge_iflist *bif;
3202 	int error;
3203 
3204 	bif = bridge_lookup_member(sc, req->ifba_ifsname);
3205 	if (bif == NULL) {
3206 		return ENOENT;
3207 	}
3208 
3209 	error = bridge_rtupdate(sc, req->ifba_dst, req->ifba_vlan, bif, 1,
3210 	    req->ifba_flags);
3211 
3212 	return error;
3213 }
3214 
3215 static int
bridge_ioctl_sto(struct bridge_softc * sc,void * arg)3216 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
3217 {
3218 	struct ifbrparam *param = arg;
3219 
3220 	sc->sc_brttimeout = param->ifbrp_ctime;
3221 	return 0;
3222 }
3223 
3224 static int
bridge_ioctl_gto(struct bridge_softc * sc,void * arg)3225 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
3226 {
3227 	struct ifbrparam *param = arg;
3228 
3229 	param->ifbrp_ctime = sc->sc_brttimeout;
3230 	return 0;
3231 }
3232 
3233 static int
bridge_ioctl_daddr32(struct bridge_softc * sc,void * arg)3234 bridge_ioctl_daddr32(struct bridge_softc *sc, void *arg)
3235 {
3236 	struct ifbareq32 *req = arg;
3237 
3238 	return bridge_rtdaddr(sc, req->ifba_dst, req->ifba_vlan);
3239 }
3240 
3241 static int
bridge_ioctl_daddr64(struct bridge_softc * sc,void * arg)3242 bridge_ioctl_daddr64(struct bridge_softc *sc, void *arg)
3243 {
3244 	struct ifbareq64 *req = arg;
3245 
3246 	return bridge_rtdaddr(sc, req->ifba_dst, req->ifba_vlan);
3247 }
3248 
3249 static int
bridge_ioctl_flush(struct bridge_softc * sc,void * arg)3250 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
3251 {
3252 	struct ifbreq *req = arg;
3253 
3254 	bridge_rtflush(sc, req->ifbr_ifsflags);
3255 	return 0;
3256 }
3257 
3258 static int
bridge_ioctl_gpri(struct bridge_softc * sc,void * arg)3259 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
3260 {
3261 	struct ifbrparam *param = arg;
3262 	struct bstp_state *bs = &sc->sc_stp;
3263 
3264 	param->ifbrp_prio = bs->bs_bridge_priority;
3265 	return 0;
3266 }
3267 
3268 static int
bridge_ioctl_spri(struct bridge_softc * sc,void * arg)3269 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
3270 {
3271 #if BRIDGESTP
3272 	struct ifbrparam *param = arg;
3273 
3274 	return bstp_set_priority(&sc->sc_stp, param->ifbrp_prio);
3275 #else /* !BRIDGESTP */
3276 #pragma unused(sc, arg)
3277 	return EOPNOTSUPP;
3278 #endif /* !BRIDGESTP */
3279 }
3280 
3281 static int
bridge_ioctl_ght(struct bridge_softc * sc,void * arg)3282 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
3283 {
3284 	struct ifbrparam *param = arg;
3285 	struct bstp_state *bs = &sc->sc_stp;
3286 
3287 	param->ifbrp_hellotime = bs->bs_bridge_htime >> 8;
3288 	return 0;
3289 }
3290 
3291 static int
bridge_ioctl_sht(struct bridge_softc * sc,void * arg)3292 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
3293 {
3294 #if BRIDGESTP
3295 	struct ifbrparam *param = arg;
3296 
3297 	return bstp_set_htime(&sc->sc_stp, param->ifbrp_hellotime);
3298 #else /* !BRIDGESTP */
3299 #pragma unused(sc, arg)
3300 	return EOPNOTSUPP;
3301 #endif /* !BRIDGESTP */
3302 }
3303 
3304 static int
bridge_ioctl_gfd(struct bridge_softc * sc,void * arg)3305 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
3306 {
3307 	struct ifbrparam *param;
3308 	struct bstp_state *bs;
3309 
3310 	param = arg;
3311 	bs = &sc->sc_stp;
3312 	param->ifbrp_fwddelay = bs->bs_bridge_fdelay >> 8;
3313 	return 0;
3314 }
3315 
3316 static int
bridge_ioctl_sfd(struct bridge_softc * sc,void * arg)3317 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
3318 {
3319 #if BRIDGESTP
3320 	struct ifbrparam *param = arg;
3321 
3322 	return bstp_set_fdelay(&sc->sc_stp, param->ifbrp_fwddelay);
3323 #else /* !BRIDGESTP */
3324 #pragma unused(sc, arg)
3325 	return EOPNOTSUPP;
3326 #endif /* !BRIDGESTP */
3327 }
3328 
3329 static int
bridge_ioctl_gma(struct bridge_softc * sc,void * arg)3330 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
3331 {
3332 	struct ifbrparam *param;
3333 	struct bstp_state *bs;
3334 
3335 	param = arg;
3336 	bs = &sc->sc_stp;
3337 	param->ifbrp_maxage = bs->bs_bridge_max_age >> 8;
3338 	return 0;
3339 }
3340 
3341 static int
bridge_ioctl_sma(struct bridge_softc * sc,void * arg)3342 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
3343 {
3344 #if BRIDGESTP
3345 	struct ifbrparam *param = arg;
3346 
3347 	return bstp_set_maxage(&sc->sc_stp, param->ifbrp_maxage);
3348 #else /* !BRIDGESTP */
3349 #pragma unused(sc, arg)
3350 	return EOPNOTSUPP;
3351 #endif /* !BRIDGESTP */
3352 }
3353 
3354 static int
bridge_ioctl_sifprio(struct bridge_softc * sc,void * arg)3355 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
3356 {
3357 #if BRIDGESTP
3358 	struct ifbreq *req = arg;
3359 	struct bridge_iflist *bif;
3360 
3361 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
3362 	if (bif == NULL) {
3363 		return ENOENT;
3364 	}
3365 
3366 	return bstp_set_port_priority(&bif->bif_stp, req->ifbr_priority);
3367 #else /* !BRIDGESTP */
3368 #pragma unused(sc, arg)
3369 	return EOPNOTSUPP;
3370 #endif /* !BRIDGESTP */
3371 }
3372 
3373 static int
bridge_ioctl_sifcost(struct bridge_softc * sc,void * arg)3374 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
3375 {
3376 #if BRIDGESTP
3377 	struct ifbreq *req = arg;
3378 	struct bridge_iflist *bif;
3379 
3380 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
3381 	if (bif == NULL) {
3382 		return ENOENT;
3383 	}
3384 
3385 	return bstp_set_path_cost(&bif->bif_stp, req->ifbr_path_cost);
3386 #else /* !BRIDGESTP */
3387 #pragma unused(sc, arg)
3388 	return EOPNOTSUPP;
3389 #endif /* !BRIDGESTP */
3390 }
3391 
3392 static int
bridge_ioctl_gfilt(struct bridge_softc * sc,void * arg)3393 bridge_ioctl_gfilt(struct bridge_softc *sc, void *arg)
3394 {
3395 	struct ifbrparam *param = arg;
3396 
3397 	param->ifbrp_filter = sc->sc_filter_flags;
3398 
3399 	return 0;
3400 }
3401 
3402 static int
bridge_ioctl_sfilt(struct bridge_softc * sc,void * arg)3403 bridge_ioctl_sfilt(struct bridge_softc *sc, void *arg)
3404 {
3405 	struct ifbrparam *param = arg;
3406 
3407 	if (param->ifbrp_filter & ~IFBF_FILT_MASK) {
3408 		return EINVAL;
3409 	}
3410 
3411 	if (param->ifbrp_filter & IFBF_FILT_USEIPF) {
3412 		return EINVAL;
3413 	}
3414 
3415 	sc->sc_filter_flags = param->ifbrp_filter;
3416 
3417 	return 0;
3418 }
3419 
3420 static int
bridge_ioctl_sifmaxaddr(struct bridge_softc * sc,void * arg)3421 bridge_ioctl_sifmaxaddr(struct bridge_softc *sc, void *arg)
3422 {
3423 	struct ifbreq *req = arg;
3424 	struct bridge_iflist *bif;
3425 
3426 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
3427 	if (bif == NULL) {
3428 		return ENOENT;
3429 	}
3430 
3431 	bif->bif_addrmax = req->ifbr_addrmax;
3432 	return 0;
3433 }
3434 
3435 static int
bridge_ioctl_addspan(struct bridge_softc * sc,void * arg)3436 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
3437 {
3438 	struct ifbreq *req = arg;
3439 	struct bridge_iflist *bif = NULL;
3440 	struct ifnet *ifs;
3441 
3442 	ifs = ifunit(req->ifbr_ifsname);
3443 	if (ifs == NULL) {
3444 		return ENOENT;
3445 	}
3446 
3447 	if (IFNET_IS_INTCOPROC(ifs)) {
3448 		return EINVAL;
3449 	}
3450 
3451 	TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)
3452 	if (ifs == bif->bif_ifp) {
3453 		return EBUSY;
3454 	}
3455 
3456 	if (ifs->if_bridge != NULL) {
3457 		return EBUSY;
3458 	}
3459 
3460 	switch (ifs->if_type) {
3461 	case IFT_ETHER:
3462 	case IFT_L2VLAN:
3463 	case IFT_IEEE8023ADLAG:
3464 		break;
3465 	case IFT_GIF:
3466 	/* currently not supported */
3467 	/* FALLTHRU */
3468 	default:
3469 		return EINVAL;
3470 	}
3471 
3472 	bif = kalloc_type(struct bridge_iflist, Z_WAITOK | Z_ZERO | Z_NOFAIL);
3473 
3474 	bif->bif_ifp = ifs;
3475 	bif->bif_ifflags = IFBIF_SPAN;
3476 
3477 	ifnet_reference(bif->bif_ifp);
3478 
3479 	TAILQ_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
3480 
3481 	return 0;
3482 }
3483 
3484 static int
bridge_ioctl_delspan(struct bridge_softc * sc,void * arg)3485 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
3486 {
3487 	struct ifbreq *req = arg;
3488 	struct bridge_iflist *bif;
3489 	struct ifnet *ifs;
3490 
3491 	ifs = ifunit(req->ifbr_ifsname);
3492 	if (ifs == NULL) {
3493 		return ENOENT;
3494 	}
3495 
3496 	TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)
3497 	if (ifs == bif->bif_ifp) {
3498 		break;
3499 	}
3500 
3501 	if (bif == NULL) {
3502 		return ENOENT;
3503 	}
3504 
3505 	bridge_delete_span(sc, bif);
3506 
3507 	return 0;
3508 }
3509 
3510 #define BRIDGE_IOCTL_GBPARAM do {                                       \
3511 	struct bstp_state *bs = &sc->sc_stp;                            \
3512 	struct bstp_port *root_port;                                    \
3513                                                                         \
3514 	req->ifbop_maxage = bs->bs_bridge_max_age >> 8;                 \
3515 	req->ifbop_hellotime = bs->bs_bridge_htime >> 8;                \
3516 	req->ifbop_fwddelay = bs->bs_bridge_fdelay >> 8;                \
3517                                                                         \
3518 	root_port = bs->bs_root_port;                                   \
3519 	if (root_port == NULL)                                          \
3520 	        req->ifbop_root_port = 0;                               \
3521 	else                                                            \
3522 	        req->ifbop_root_port = root_port->bp_ifp->if_index;     \
3523                                                                         \
3524 	req->ifbop_holdcount = bs->bs_txholdcount;                      \
3525 	req->ifbop_priority = bs->bs_bridge_priority;                   \
3526 	req->ifbop_protocol = bs->bs_protover;                          \
3527 	req->ifbop_root_path_cost = bs->bs_root_pv.pv_cost;             \
3528 	req->ifbop_bridgeid = bs->bs_bridge_pv.pv_dbridge_id;           \
3529 	req->ifbop_designated_root = bs->bs_root_pv.pv_root_id;         \
3530 	req->ifbop_designated_bridge = bs->bs_root_pv.pv_dbridge_id;    \
3531 	req->ifbop_last_tc_time.tv_sec = bs->bs_last_tc_time.tv_sec;    \
3532 	req->ifbop_last_tc_time.tv_usec = bs->bs_last_tc_time.tv_usec;  \
3533 } while (0)
3534 
3535 static int
bridge_ioctl_gbparam32(struct bridge_softc * sc,void * arg)3536 bridge_ioctl_gbparam32(struct bridge_softc *sc, void *arg)
3537 {
3538 	struct ifbropreq32 *req = arg;
3539 
3540 	BRIDGE_IOCTL_GBPARAM;
3541 	return 0;
3542 }
3543 
3544 static int
bridge_ioctl_gbparam64(struct bridge_softc * sc,void * arg)3545 bridge_ioctl_gbparam64(struct bridge_softc *sc, void *arg)
3546 {
3547 	struct ifbropreq64 *req = arg;
3548 
3549 	BRIDGE_IOCTL_GBPARAM;
3550 	return 0;
3551 }
3552 
3553 static int
bridge_ioctl_grte(struct bridge_softc * sc,void * arg)3554 bridge_ioctl_grte(struct bridge_softc *sc, void *arg)
3555 {
3556 	struct ifbrparam *param = arg;
3557 
3558 	param->ifbrp_cexceeded = sc->sc_brtexceeded;
3559 	return 0;
3560 }
3561 
3562 #define BRIDGE_IOCTL_GIFSSTP do {                                       \
3563 	struct bridge_iflist *bif;                                      \
3564 	struct bstp_port *bp;                                           \
3565 	struct ifbpstpreq bpreq;                                        \
3566 	char *buf, *outbuf;                                             \
3567 	unsigned int count, buflen, len;                                \
3568                                                                         \
3569 	count = 0;                                                      \
3570 	TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) {                  \
3571 	        if ((bif->bif_ifflags & IFBIF_STP) != 0)                \
3572 	                count++;                                        \
3573 	}                                                               \
3574                                                                         \
3575 	buflen = sizeof (bpreq) * count;                                \
3576 	if (bifstp->ifbpstp_len == 0) {                                 \
3577 	        bifstp->ifbpstp_len = buflen;                           \
3578 	        return (0);                                             \
3579 	}                                                               \
3580                                                                         \
3581 	BRIDGE_UNLOCK(sc);                                              \
3582 	outbuf = (char *)kalloc_data(buflen, Z_WAITOK | Z_ZERO);        \
3583 	BRIDGE_LOCK(sc);                                                \
3584                                                                         \
3585 	count = 0;                                                      \
3586 	buf = outbuf;                                                   \
3587 	len = min(bifstp->ifbpstp_len, buflen);                         \
3588 	bzero(&bpreq, sizeof (bpreq));                                  \
3589 	TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) {                  \
3590 	        if (len < sizeof (bpreq))                               \
3591 	                break;                                          \
3592                                                                         \
3593 	        if ((bif->bif_ifflags & IFBIF_STP) == 0)                \
3594 	                continue;                                       \
3595                                                                         \
3596 	        bp = &bif->bif_stp;                                     \
3597 	        bpreq.ifbp_portno = bif->bif_ifp->if_index & 0xfff;     \
3598 	        bpreq.ifbp_fwd_trans = bp->bp_forward_transitions;      \
3599 	        bpreq.ifbp_design_cost = bp->bp_desg_pv.pv_cost;        \
3600 	        bpreq.ifbp_design_port = bp->bp_desg_pv.pv_port_id;     \
3601 	        bpreq.ifbp_design_bridge = bp->bp_desg_pv.pv_dbridge_id; \
3602 	        bpreq.ifbp_design_root = bp->bp_desg_pv.pv_root_id;     \
3603                                                                         \
3604 	        memcpy(buf, &bpreq, sizeof (bpreq));                    \
3605 	        count++;                                                \
3606 	        buf += sizeof (bpreq);                                  \
3607 	        len -= sizeof (bpreq);                                  \
3608 	}                                                               \
3609                                                                         \
3610 	BRIDGE_UNLOCK(sc);                                              \
3611 	bifstp->ifbpstp_len = sizeof (bpreq) * count;                   \
3612 	error = copyout(outbuf, bifstp->ifbpstp_req, bifstp->ifbpstp_len); \
3613 	BRIDGE_LOCK(sc);                                                \
3614 	kfree_data(outbuf, buflen);                                     \
3615 	return (error);                                                 \
3616 } while (0)
3617 
3618 static int
bridge_ioctl_gifsstp32(struct bridge_softc * sc,void * arg)3619 bridge_ioctl_gifsstp32(struct bridge_softc *sc, void *arg)
3620 {
3621 	struct ifbpstpconf32 *bifstp = arg;
3622 	int error = 0;
3623 
3624 	BRIDGE_IOCTL_GIFSSTP;
3625 	return error;
3626 }
3627 
3628 static int
bridge_ioctl_gifsstp64(struct bridge_softc * sc,void * arg)3629 bridge_ioctl_gifsstp64(struct bridge_softc *sc, void *arg)
3630 {
3631 	struct ifbpstpconf64 *bifstp = arg;
3632 	int error = 0;
3633 
3634 	BRIDGE_IOCTL_GIFSSTP;
3635 	return error;
3636 }
3637 
3638 static int
bridge_ioctl_sproto(struct bridge_softc * sc,void * arg)3639 bridge_ioctl_sproto(struct bridge_softc *sc, void *arg)
3640 {
3641 #if BRIDGESTP
3642 	struct ifbrparam *param = arg;
3643 
3644 	return bstp_set_protocol(&sc->sc_stp, param->ifbrp_proto);
3645 #else /* !BRIDGESTP */
3646 #pragma unused(sc, arg)
3647 	return EOPNOTSUPP;
3648 #endif /* !BRIDGESTP */
3649 }
3650 
3651 static int
bridge_ioctl_stxhc(struct bridge_softc * sc,void * arg)3652 bridge_ioctl_stxhc(struct bridge_softc *sc, void *arg)
3653 {
3654 #if BRIDGESTP
3655 	struct ifbrparam *param = arg;
3656 
3657 	return bstp_set_holdcount(&sc->sc_stp, param->ifbrp_txhc);
3658 #else /* !BRIDGESTP */
3659 #pragma unused(sc, arg)
3660 	return EOPNOTSUPP;
3661 #endif /* !BRIDGESTP */
3662 }
3663 
3664 
3665 static int
bridge_ioctl_ghostfilter(struct bridge_softc * sc,void * arg)3666 bridge_ioctl_ghostfilter(struct bridge_softc *sc, void *arg)
3667 {
3668 	struct ifbrhostfilter *req = arg;
3669 	struct bridge_iflist *bif;
3670 
3671 	bif = bridge_lookup_member(sc, req->ifbrhf_ifsname);
3672 	if (bif == NULL) {
3673 		return ENOENT;
3674 	}
3675 
3676 	bzero(req, sizeof(struct ifbrhostfilter));
3677 	if (bif->bif_flags & BIFF_HOST_FILTER) {
3678 		req->ifbrhf_flags |= IFBRHF_ENABLED;
3679 		bcopy(bif->bif_hf_hwsrc, req->ifbrhf_hwsrca,
3680 		    ETHER_ADDR_LEN);
3681 		req->ifbrhf_ipsrc = bif->bif_hf_ipsrc.s_addr;
3682 	}
3683 	return 0;
3684 }
3685 
3686 static int
bridge_ioctl_shostfilter(struct bridge_softc * sc,void * arg)3687 bridge_ioctl_shostfilter(struct bridge_softc *sc, void *arg)
3688 {
3689 	struct ifbrhostfilter *req = arg;
3690 	struct bridge_iflist *bif;
3691 
3692 	bif = bridge_lookup_member(sc, req->ifbrhf_ifsname);
3693 	if (bif == NULL) {
3694 		return ENOENT;
3695 	}
3696 
3697 	if (req->ifbrhf_flags & IFBRHF_ENABLED) {
3698 		bif->bif_flags |= BIFF_HOST_FILTER;
3699 
3700 		if (req->ifbrhf_flags & IFBRHF_HWSRC) {
3701 			bcopy(req->ifbrhf_hwsrca, bif->bif_hf_hwsrc,
3702 			    ETHER_ADDR_LEN);
3703 			if (bcmp(req->ifbrhf_hwsrca, ethernulladdr,
3704 			    ETHER_ADDR_LEN) != 0) {
3705 				bif->bif_flags |= BIFF_HF_HWSRC;
3706 			} else {
3707 				bif->bif_flags &= ~BIFF_HF_HWSRC;
3708 			}
3709 		}
3710 		if (req->ifbrhf_flags & IFBRHF_IPSRC) {
3711 			bif->bif_hf_ipsrc.s_addr = req->ifbrhf_ipsrc;
3712 			if (bif->bif_hf_ipsrc.s_addr != INADDR_ANY) {
3713 				bif->bif_flags |= BIFF_HF_IPSRC;
3714 			} else {
3715 				bif->bif_flags &= ~BIFF_HF_IPSRC;
3716 			}
3717 		}
3718 	} else {
3719 		bif->bif_flags &= ~(BIFF_HOST_FILTER | BIFF_HF_HWSRC |
3720 		    BIFF_HF_IPSRC);
3721 		bzero(bif->bif_hf_hwsrc, ETHER_ADDR_LEN);
3722 		bif->bif_hf_ipsrc.s_addr = INADDR_ANY;
3723 	}
3724 
3725 	return 0;
3726 }
3727 
3728 static char *
bridge_mac_nat_entry_out(struct mac_nat_entry_list * list,unsigned int * count_p,char * buf,unsigned int * len_p)3729 bridge_mac_nat_entry_out(struct mac_nat_entry_list * list,
3730     unsigned int * count_p, char *buf, unsigned int *len_p)
3731 {
3732 	unsigned int            count = *count_p;
3733 	struct ifbrmne          ifbmne;
3734 	unsigned int            len = *len_p;
3735 	struct mac_nat_entry    *mne;
3736 	unsigned long           now;
3737 
3738 	bzero(&ifbmne, sizeof(ifbmne));
3739 	LIST_FOREACH(mne, list, mne_list) {
3740 		if (len < sizeof(ifbmne)) {
3741 			break;
3742 		}
3743 		snprintf(ifbmne.ifbmne_ifname, sizeof(ifbmne.ifbmne_ifname),
3744 		    "%s", mne->mne_bif->bif_ifp->if_xname);
3745 		memcpy(ifbmne.ifbmne_mac, mne->mne_mac,
3746 		    sizeof(ifbmne.ifbmne_mac));
3747 		now = (unsigned long) net_uptime();
3748 		if (now < mne->mne_expire) {
3749 			ifbmne.ifbmne_expire = mne->mne_expire - now;
3750 		} else {
3751 			ifbmne.ifbmne_expire = 0;
3752 		}
3753 		if ((mne->mne_flags & MNE_FLAGS_IPV6) != 0) {
3754 			ifbmne.ifbmne_af = AF_INET6;
3755 			ifbmne.ifbmne_ip6_addr = mne->mne_ip6;
3756 		} else {
3757 			ifbmne.ifbmne_af = AF_INET;
3758 			ifbmne.ifbmne_ip_addr = mne->mne_ip;
3759 		}
3760 		memcpy(buf, &ifbmne, sizeof(ifbmne));
3761 		count++;
3762 		buf += sizeof(ifbmne);
3763 		len -= sizeof(ifbmne);
3764 	}
3765 	*count_p = count;
3766 	*len_p = len;
3767 	return buf;
3768 }
3769 
3770 /*
3771  * bridge_ioctl_gmnelist()
3772  *   Perform the get mac_nat_entry list ioctl.
3773  *
3774  * Note:
3775  *   The struct ifbrmnelist32 and struct ifbrmnelist64 have the same
3776  *   field size/layout except for the last field ifbml_buf, the user-supplied
3777  *   buffer pointer. That is passed in separately via the 'user_addr'
3778  *   parameter from the respective 32-bit or 64-bit ioctl routine.
3779  */
3780 static int
bridge_ioctl_gmnelist(struct bridge_softc * sc,struct ifbrmnelist32 * mnl,user_addr_t user_addr)3781 bridge_ioctl_gmnelist(struct bridge_softc *sc, struct ifbrmnelist32 *mnl,
3782     user_addr_t user_addr)
3783 {
3784 	unsigned int            count;
3785 	char                    *buf;
3786 	int                     error = 0;
3787 	char                    *outbuf = NULL;
3788 	struct mac_nat_entry    *mne;
3789 	unsigned int            buflen;
3790 	unsigned int            len;
3791 
3792 	mnl->ifbml_elsize = sizeof(struct ifbrmne);
3793 	count = 0;
3794 	LIST_FOREACH(mne, &sc->sc_mne_list, mne_list) {
3795 		count++;
3796 	}
3797 	LIST_FOREACH(mne, &sc->sc_mne_list_v6, mne_list) {
3798 		count++;
3799 	}
3800 	buflen = sizeof(struct ifbrmne) * count;
3801 	if (buflen == 0 || mnl->ifbml_len == 0) {
3802 		mnl->ifbml_len = buflen;
3803 		return error;
3804 	}
3805 	BRIDGE_UNLOCK(sc);
3806 	outbuf = (char *)kalloc_data(buflen, Z_WAITOK | Z_ZERO);
3807 	BRIDGE_LOCK(sc);
3808 	count = 0;
3809 	buf = outbuf;
3810 	len = min(mnl->ifbml_len, buflen);
3811 	buf = bridge_mac_nat_entry_out(&sc->sc_mne_list, &count, buf, &len);
3812 	buf = bridge_mac_nat_entry_out(&sc->sc_mne_list_v6, &count, buf, &len);
3813 	mnl->ifbml_len = count * sizeof(struct ifbrmne);
3814 	BRIDGE_UNLOCK(sc);
3815 	error = copyout(outbuf, user_addr, mnl->ifbml_len);
3816 	kfree_data(outbuf, buflen);
3817 	BRIDGE_LOCK(sc);
3818 	return error;
3819 }
3820 
3821 static int
bridge_ioctl_gmnelist64(struct bridge_softc * sc,void * arg)3822 bridge_ioctl_gmnelist64(struct bridge_softc *sc, void *arg)
3823 {
3824 	struct ifbrmnelist64 *mnl = arg;
3825 
3826 	return bridge_ioctl_gmnelist(sc, arg, mnl->ifbml_buf);
3827 }
3828 
3829 static int
bridge_ioctl_gmnelist32(struct bridge_softc * sc,void * arg)3830 bridge_ioctl_gmnelist32(struct bridge_softc *sc, void *arg)
3831 {
3832 	struct ifbrmnelist32 *mnl = arg;
3833 
3834 	return bridge_ioctl_gmnelist(sc, arg,
3835 	           CAST_USER_ADDR_T(mnl->ifbml_buf));
3836 }
3837 
3838 /*
3839  * bridge_ioctl_gifstats()
3840  *   Return per-member stats.
3841  *
3842  * Note:
3843  *   The ifbrmreq32 and ifbrmreq64 structures have the same
3844  *   field size/layout except for the last field brmr_buf, the user-supplied
3845  *   buffer pointer. That is passed in separately via the 'user_addr'
3846  *   parameter from the respective 32-bit or 64-bit ioctl routine.
3847  */
3848 static int
bridge_ioctl_gifstats(struct bridge_softc * sc,struct ifbrmreq32 * mreq,user_addr_t user_addr)3849 bridge_ioctl_gifstats(struct bridge_softc *sc, struct ifbrmreq32 *mreq,
3850     user_addr_t user_addr)
3851 {
3852 	struct bridge_iflist    *bif;
3853 	int                     error = 0;
3854 	unsigned int            buflen;
3855 
3856 	bif = bridge_lookup_member(sc, mreq->brmr_ifname);
3857 	if (bif == NULL) {
3858 		error = ENOENT;
3859 		goto done;
3860 	}
3861 
3862 	buflen = mreq->brmr_elsize = sizeof(struct ifbrmstats);
3863 	if (buflen == 0 || mreq->brmr_len == 0) {
3864 		mreq->brmr_len = buflen;
3865 		goto done;
3866 	}
3867 	if (mreq->brmr_len != 0 && mreq->brmr_len < buflen) {
3868 		error = ENOBUFS;
3869 		goto done;
3870 	}
3871 	mreq->brmr_len = buflen;
3872 	error = copyout(&bif->bif_stats, user_addr, buflen);
3873 done:
3874 	return error;
3875 }
3876 
3877 static int
bridge_ioctl_gifstats32(struct bridge_softc * sc,void * arg)3878 bridge_ioctl_gifstats32(struct bridge_softc *sc, void *arg)
3879 {
3880 	struct ifbrmreq32 *mreq = arg;
3881 
3882 	return bridge_ioctl_gifstats(sc, arg, mreq->brmr_buf);
3883 }
3884 
3885 static int
bridge_ioctl_gifstats64(struct bridge_softc * sc,void * arg)3886 bridge_ioctl_gifstats64(struct bridge_softc *sc, void *arg)
3887 {
3888 	struct ifbrmreq64 *mreq = arg;
3889 
3890 	return bridge_ioctl_gifstats(sc, arg, mreq->brmr_buf);
3891 }
3892 
3893 /*
3894  * bridge_proto_attach_changed
3895  *
3896  *	Called when protocol attachment on the interface changes.
3897  */
3898 static void
bridge_proto_attach_changed(struct ifnet * ifp)3899 bridge_proto_attach_changed(struct ifnet *ifp)
3900 {
3901 	boolean_t changed = FALSE;
3902 	struct bridge_iflist *bif;
3903 	boolean_t input_broadcast;
3904 	struct bridge_softc *sc = ifp->if_bridge;
3905 
3906 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE, "%s", ifp->if_xname);
3907 	if (sc == NULL) {
3908 		return;
3909 	}
3910 	input_broadcast = interface_needs_input_broadcast(ifp);
3911 	BRIDGE_LOCK(sc);
3912 	bif = bridge_lookup_member_if(sc, ifp);
3913 	if (bif != NULL) {
3914 		changed = bif_set_input_broadcast(bif, input_broadcast);
3915 	}
3916 	BRIDGE_UNLOCK(sc);
3917 	if (changed) {
3918 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE,
3919 		    "%s input broadcast %s", ifp->if_xname,
3920 		    input_broadcast ? "ENABLED" : "DISABLED");
3921 	}
3922 	return;
3923 }
3924 
3925 /*
3926  * interface_media_active:
3927  *
3928  *	Tells if an interface media is active.
3929  */
3930 static int
interface_media_active(struct ifnet * ifp)3931 interface_media_active(struct ifnet *ifp)
3932 {
3933 	struct ifmediareq   ifmr;
3934 	int status = 0;
3935 
3936 	bzero(&ifmr, sizeof(ifmr));
3937 	if (ifnet_ioctl(ifp, 0, SIOCGIFMEDIA, &ifmr) == 0) {
3938 		if ((ifmr.ifm_status & IFM_AVALID) && ifmr.ifm_count > 0) {
3939 			status = ifmr.ifm_status & IFM_ACTIVE ? 1 : 0;
3940 		}
3941 	}
3942 
3943 	return status;
3944 }
3945 
3946 /*
3947  * bridge_updatelinkstatus:
3948  *
3949  *      Update the media active status of the bridge based on the
3950  *	media active status of its member.
3951  *	If changed, return the corresponding onf/off link event.
3952  */
3953 static u_int32_t
bridge_updatelinkstatus(struct bridge_softc * sc)3954 bridge_updatelinkstatus(struct bridge_softc *sc)
3955 {
3956 	struct bridge_iflist *bif;
3957 	int active_member = 0;
3958 	u_int32_t event_code = 0;
3959 
3960 	BRIDGE_LOCK_ASSERT_HELD(sc);
3961 
3962 	/*
3963 	 * Find out if we have an active interface
3964 	 */
3965 	TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) {
3966 		if (bif->bif_flags & BIFF_MEDIA_ACTIVE) {
3967 			active_member = 1;
3968 			break;
3969 		}
3970 	}
3971 
3972 	if (active_member && !(sc->sc_flags & SCF_MEDIA_ACTIVE)) {
3973 		sc->sc_flags |= SCF_MEDIA_ACTIVE;
3974 		event_code = KEV_DL_LINK_ON;
3975 	} else if (!active_member && (sc->sc_flags & SCF_MEDIA_ACTIVE)) {
3976 		sc->sc_flags &= ~SCF_MEDIA_ACTIVE;
3977 		event_code = KEV_DL_LINK_OFF;
3978 	}
3979 
3980 	return event_code;
3981 }
3982 
3983 /*
3984  * bridge_iflinkevent:
3985  */
3986 static void
bridge_iflinkevent(struct ifnet * ifp)3987 bridge_iflinkevent(struct ifnet *ifp)
3988 {
3989 	struct bridge_softc *sc = ifp->if_bridge;
3990 	struct bridge_iflist *bif;
3991 	u_int32_t event_code = 0;
3992 	int media_active;
3993 
3994 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE, "%s", ifp->if_xname);
3995 
3996 	/* Check if the interface is a bridge member */
3997 	if (sc == NULL) {
3998 		return;
3999 	}
4000 
4001 	media_active = interface_media_active(ifp);
4002 	BRIDGE_LOCK(sc);
4003 	bif = bridge_lookup_member_if(sc, ifp);
4004 	if (bif != NULL) {
4005 		if (media_active) {
4006 			bif->bif_flags |= BIFF_MEDIA_ACTIVE;
4007 		} else {
4008 			bif->bif_flags &= ~BIFF_MEDIA_ACTIVE;
4009 		}
4010 		if (sc->sc_mac_nat_bif != NULL) {
4011 			bridge_mac_nat_flush_entries(sc, bif);
4012 		}
4013 
4014 		event_code = bridge_updatelinkstatus(sc);
4015 	}
4016 	BRIDGE_UNLOCK(sc);
4017 
4018 	if (event_code != 0) {
4019 		bridge_link_event(sc->sc_ifp, event_code);
4020 	}
4021 }
4022 
4023 /*
4024  * bridge_delayed_callback:
4025  *
4026  *	Makes a delayed call
4027  */
4028 static void
bridge_delayed_callback(void * param,__unused void * param2)4029 bridge_delayed_callback(void *param, __unused void *param2)
4030 {
4031 	struct bridge_delayed_call *call = (struct bridge_delayed_call *)param;
4032 	struct bridge_softc *sc = call->bdc_sc;
4033 
4034 #if BRIDGE_DELAYED_CALLBACK_DEBUG
4035 	if (bridge_delayed_callback_delay > 0) {
4036 		struct timespec ts;
4037 
4038 		ts.tv_sec = bridge_delayed_callback_delay;
4039 		ts.tv_nsec = 0;
4040 
4041 		BRIDGE_LOG(LOG_NOTICE, 0,
4042 		    "sleeping for %d seconds",
4043 		    bridge_delayed_callback_delay);
4044 
4045 		msleep(&bridge_delayed_callback_delay, NULL, PZERO,
4046 		    __func__, &ts);
4047 
4048 		BRIDGE_LOG(LOG_NOTICE, 0, "awoken");
4049 	}
4050 #endif /* BRIDGE_DELAYED_CALLBACK_DEBUG */
4051 
4052 	BRIDGE_LOCK(sc);
4053 
4054 #if BRIDGE_DELAYED_CALLBACK_DEBUG
4055 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_DELAYED_CALL,
4056 	    "%s call 0x%llx flags 0x%x",
4057 	    sc->sc_if_xname, (uint64_t)VM_KERNEL_ADDRPERM(call),
4058 	    call->bdc_flags);
4059 }
4060 #endif /* BRIDGE_DELAYED_CALLBACK_DEBUG */
4061 
4062 	if (call->bdc_flags & BDCF_CANCELLING) {
4063 		wakeup(call);
4064 	} else {
4065 		if ((sc->sc_flags & SCF_DETACHING) == 0) {
4066 			(*call->bdc_func)(sc);
4067 		}
4068 	}
4069 	call->bdc_flags &= ~BDCF_OUTSTANDING;
4070 	BRIDGE_UNLOCK(sc);
4071 }
4072 
4073 /*
4074  * bridge_schedule_delayed_call:
4075  *
4076  *	Schedule a function to be called on a separate thread
4077  *      The actual call may be scheduled to run at a given time or ASAP.
4078  */
4079 static void
4080 bridge_schedule_delayed_call(struct bridge_delayed_call *call)
4081 {
4082 	uint64_t deadline = 0;
4083 	struct bridge_softc *sc = call->bdc_sc;
4084 
4085 	BRIDGE_LOCK_ASSERT_HELD(sc);
4086 
4087 	if ((sc->sc_flags & SCF_DETACHING) ||
4088 	    (call->bdc_flags & (BDCF_OUTSTANDING | BDCF_CANCELLING))) {
4089 		return;
4090 	}
4091 
4092 	if (call->bdc_ts.tv_sec || call->bdc_ts.tv_nsec) {
4093 		nanoseconds_to_absolutetime(
4094 			(uint64_t)call->bdc_ts.tv_sec * NSEC_PER_SEC +
4095 			call->bdc_ts.tv_nsec, &deadline);
4096 		clock_absolutetime_interval_to_deadline(deadline, &deadline);
4097 	}
4098 
4099 	call->bdc_flags = BDCF_OUTSTANDING;
4100 
4101 #if BRIDGE_DELAYED_CALLBACK_DEBUG
4102 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_DELAYED_CALL,
4103 	    "%s call 0x%llx flags 0x%x",
4104 	    sc->sc_if_xname, (uint64_t)VM_KERNEL_ADDRPERM(call),
4105 	    call->bdc_flags);
4106 }
4107 #endif /* BRIDGE_DELAYED_CALLBACK_DEBUG */
4108 
4109 	if (call->bdc_ts.tv_sec || call->bdc_ts.tv_nsec) {
4110 		thread_call_func_delayed(
4111 			(thread_call_func_t)bridge_delayed_callback,
4112 			call, deadline);
4113 	} else {
4114 		if (call->bdc_thread_call == NULL) {
4115 			call->bdc_thread_call = thread_call_allocate(
4116 				(thread_call_func_t)bridge_delayed_callback,
4117 				call);
4118 		}
4119 		thread_call_enter(call->bdc_thread_call);
4120 	}
4121 }
4122 
4123 /*
4124  * bridge_cancel_delayed_call:
4125  *
4126  *	Cancel a queued or running delayed call.
4127  *	If call is running, does not return until the call is done to
4128  *	prevent race condition with the brigde interface getting destroyed
4129  */
4130 static void
4131 bridge_cancel_delayed_call(struct bridge_delayed_call *call)
4132 {
4133 	boolean_t result;
4134 	struct bridge_softc *sc = call->bdc_sc;
4135 
4136 	/*
4137 	 * The call was never scheduled
4138 	 */
4139 	if (sc == NULL) {
4140 		return;
4141 	}
4142 
4143 	BRIDGE_LOCK_ASSERT_HELD(sc);
4144 
4145 	call->bdc_flags |= BDCF_CANCELLING;
4146 
4147 	while (call->bdc_flags & BDCF_OUTSTANDING) {
4148 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_DELAYED_CALL,
4149 		    "%s call 0x%llx flags 0x%x",
4150 		    sc->sc_if_xname, (uint64_t)VM_KERNEL_ADDRPERM(call),
4151 		    call->bdc_flags);
4152 		result = thread_call_func_cancel(
4153 			(thread_call_func_t)bridge_delayed_callback, call, FALSE);
4154 
4155 		if (result) {
4156 			/*
4157 			 * We managed to dequeue the delayed call
4158 			 */
4159 			call->bdc_flags &= ~BDCF_OUTSTANDING;
4160 		} else {
4161 			/*
4162 			 * Wait for delayed call do be done running
4163 			 */
4164 			msleep(call, &sc->sc_mtx, PZERO, __func__, NULL);
4165 		}
4166 	}
4167 	call->bdc_flags &= ~BDCF_CANCELLING;
4168 }
4169 
4170 /*
4171  * bridge_cleanup_delayed_call:
4172  *
4173  *	Dispose resource allocated for a delayed call
4174  *	Assume the delayed call is not queued or running .
4175  */
4176 static void
4177 bridge_cleanup_delayed_call(struct bridge_delayed_call *call)
4178 {
4179 	boolean_t result;
4180 	struct bridge_softc *sc = call->bdc_sc;
4181 
4182 	/*
4183 	 * The call was never scheduled
4184 	 */
4185 	if (sc == NULL) {
4186 		return;
4187 	}
4188 
4189 	BRIDGE_LOCK_ASSERT_HELD(sc);
4190 
4191 	VERIFY((call->bdc_flags & BDCF_OUTSTANDING) == 0);
4192 	VERIFY((call->bdc_flags & BDCF_CANCELLING) == 0);
4193 
4194 	if (call->bdc_thread_call != NULL) {
4195 		result = thread_call_free(call->bdc_thread_call);
4196 		if (result == FALSE) {
4197 			panic("%s thread_call_free() failed for call %p",
4198 			    __func__, call);
4199 		}
4200 		call->bdc_thread_call = NULL;
4201 	}
4202 }
4203 
4204 /*
4205  * bridge_init:
4206  *
4207  *	Initialize a bridge interface.
4208  */
4209 static int
4210 bridge_init(struct ifnet *ifp)
4211 {
4212 	struct bridge_softc *sc = (struct bridge_softc *)ifp->if_softc;
4213 	errno_t error;
4214 
4215 	BRIDGE_LOCK_ASSERT_HELD(sc);
4216 
4217 	if ((ifnet_flags(ifp) & IFF_RUNNING)) {
4218 		return 0;
4219 	}
4220 
4221 	error = ifnet_set_flags(ifp, IFF_RUNNING, IFF_RUNNING);
4222 
4223 	/*
4224 	 * Calling bridge_aging_timer() is OK as there are no entries to
4225 	 * age so we're just going to arm the timer
4226 	 */
4227 	bridge_aging_timer(sc);
4228 #if BRIDGESTP
4229 	if (error == 0) {
4230 		bstp_init(&sc->sc_stp); /* Initialize Spanning Tree */
4231 	}
4232 #endif /* BRIDGESTP */
4233 	return error;
4234 }
4235 
4236 /*
4237  * bridge_ifstop:
4238  *
4239  *	Stop the bridge interface.
4240  */
4241 static void
4242 bridge_ifstop(struct ifnet *ifp, int disable)
4243 {
4244 #pragma unused(disable)
4245 	struct bridge_softc *sc = ifp->if_softc;
4246 
4247 	BRIDGE_LOCK_ASSERT_HELD(sc);
4248 
4249 	if ((ifnet_flags(ifp) & IFF_RUNNING) == 0) {
4250 		return;
4251 	}
4252 
4253 	bridge_cancel_delayed_call(&sc->sc_aging_timer);
4254 
4255 #if BRIDGESTP
4256 	bstp_stop(&sc->sc_stp);
4257 #endif /* BRIDGESTP */
4258 
4259 	bridge_rtflush(sc, IFBF_FLUSHDYN);
4260 	(void) ifnet_set_flags(ifp, 0, IFF_RUNNING);
4261 }
4262 
4263 /*
4264  * bridge_compute_cksum:
4265  *
4266  *	If the packet has checksum flags, compare the hardware checksum
4267  *	capabilities of the source and destination interfaces. If they
4268  *	are the same, there's nothing to do. If they are different,
4269  *	finalize the checksum so that it can be sent on the destination
4270  *	interface.
4271  */
4272 static void
4273 bridge_compute_cksum(struct ifnet *src_if, struct ifnet *dst_if, struct mbuf *m)
4274 {
4275 	uint32_t csum_flags;
4276 	uint16_t dst_hw_csum;
4277 	uint32_t did_sw = 0;
4278 	struct ether_header *eh;
4279 	uint16_t src_hw_csum;
4280 
4281 	if (src_if == dst_if) {
4282 		return;
4283 	}
4284 	csum_flags = m->m_pkthdr.csum_flags & IF_HWASSIST_CSUM_MASK;
4285 	if (csum_flags == 0) {
4286 		/* no checksum offload */
4287 		return;
4288 	}
4289 
4290 	/*
4291 	 * if destination/source differ in checksum offload
4292 	 * capabilities, finalize/compute the checksum
4293 	 */
4294 	dst_hw_csum = IF_HWASSIST_CSUM_FLAGS(dst_if->if_hwassist);
4295 	src_hw_csum = IF_HWASSIST_CSUM_FLAGS(src_if->if_hwassist);
4296 	if (dst_hw_csum == src_hw_csum) {
4297 		return;
4298 	}
4299 	eh = mtod(m, struct ether_header *);
4300 	switch (ntohs(eh->ether_type)) {
4301 	case ETHERTYPE_IP:
4302 		did_sw = in_finalize_cksum(m, sizeof(*eh), csum_flags);
4303 		break;
4304 	case ETHERTYPE_IPV6:
4305 		did_sw = in6_finalize_cksum(m, sizeof(*eh), -1, -1, csum_flags);
4306 		break;
4307 	}
4308 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4309 	    "[%s -> %s] before 0x%x did 0x%x after 0x%x",
4310 	    src_if->if_xname, dst_if->if_xname, csum_flags, did_sw,
4311 	    m->m_pkthdr.csum_flags);
4312 }
4313 
4314 static errno_t
4315 bridge_transmit(struct ifnet * ifp, struct mbuf *m)
4316 {
4317 	struct flowadv  adv = { .code = FADV_SUCCESS };
4318 	errno_t         error;
4319 
4320 	error = dlil_output(ifp, 0, m, NULL, NULL, 1, &adv);
4321 	if (error == 0) {
4322 		if (adv.code == FADV_FLOW_CONTROLLED) {
4323 			error = EQFULL;
4324 		} else if (adv.code == FADV_SUSPENDED) {
4325 			error = EQSUSPENDED;
4326 		}
4327 	}
4328 	return error;
4329 }
4330 
4331 static int
4332 get_last_ip6_hdr(struct mbuf *m, int off, int proto, int * nxtp,
4333     bool *is_fragmented)
4334 {
4335 	int newoff;
4336 
4337 	*is_fragmented = false;
4338 	while (1) {
4339 		newoff = ip6_nexthdr(m, off, proto, nxtp);
4340 		if (newoff < 0) {
4341 			return off;
4342 		} else if (newoff < off) {
4343 			return -1;    /* invalid */
4344 		} else if (newoff == off) {
4345 			return newoff;
4346 		}
4347 		off = newoff;
4348 		proto = *nxtp;
4349 		if (proto == IPPROTO_FRAGMENT) {
4350 			*is_fragmented = true;
4351 		}
4352 	}
4353 }
4354 
4355 static int
4356 bridge_get_ip_proto(struct mbuf * * mp, u_int mac_hlen, bool is_ipv4,
4357     ip_packet_info_t info_p, struct bripstats * stats_p)
4358 {
4359 	int             error = 0;
4360 	u_int           hlen;
4361 	u_int           ip_hlen;
4362 	u_int           ip_pay_len;
4363 	struct mbuf *   m0 = *mp;
4364 	int             off;
4365 	int             opt_len = 0;
4366 	int             proto = 0;
4367 
4368 	bzero(info_p, sizeof(*info_p));
4369 	if (is_ipv4) {
4370 		struct ip *     ip;
4371 		u_int           ip_total_len;
4372 
4373 		/* IPv4 */
4374 		hlen = mac_hlen + sizeof(struct ip);
4375 		if (m0->m_pkthdr.len < hlen) {
4376 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4377 			    "Short IP packet %d < %d",
4378 			    m0->m_pkthdr.len, hlen);
4379 			error = _EBADIP;
4380 			stats_p->bips_bad_ip++;
4381 			goto done;
4382 		}
4383 		if (m0->m_len < hlen) {
4384 			*mp = m0 = m_pullup(m0, hlen);
4385 			if (m0 == NULL) {
4386 				BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4387 				    "m_pullup failed hlen %d",
4388 				    hlen);
4389 				error = ENOBUFS;
4390 				stats_p->bips_bad_ip++;
4391 				goto done;
4392 			}
4393 		}
4394 		ip = (struct ip *)(void *)(mtod(m0, uint8_t *) + mac_hlen);
4395 		if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
4396 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4397 			    "bad IP version");
4398 			error = _EBADIP;
4399 			stats_p->bips_bad_ip++;
4400 			goto done;
4401 		}
4402 		ip_hlen = IP_VHL_HL(ip->ip_vhl) << 2;
4403 		if (ip_hlen < sizeof(struct ip)) {
4404 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4405 			    "bad IP header length %d < %d",
4406 			    ip_hlen,
4407 			    (int)sizeof(struct ip));
4408 			error = _EBADIP;
4409 			stats_p->bips_bad_ip++;
4410 			goto done;
4411 		}
4412 		hlen = mac_hlen + ip_hlen;
4413 		if (m0->m_len < hlen) {
4414 			*mp = m0 = m_pullup(m0, hlen);
4415 			if (m0 == NULL) {
4416 				BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4417 				    "m_pullup failed hlen %d",
4418 				    hlen);
4419 				error = ENOBUFS;
4420 				stats_p->bips_bad_ip++;
4421 				goto done;
4422 			}
4423 		}
4424 
4425 		ip_total_len = ntohs(ip->ip_len);
4426 		if (ip_total_len < ip_hlen) {
4427 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4428 			    "IP total len %d < header len %d",
4429 			    ip_total_len, ip_hlen);
4430 			error = _EBADIP;
4431 			stats_p->bips_bad_ip++;
4432 			goto done;
4433 		}
4434 		if (ip_total_len > (m0->m_pkthdr.len - mac_hlen)) {
4435 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4436 			    "invalid IP payload length %d > %d",
4437 			    ip_total_len,
4438 			    (m0->m_pkthdr.len - mac_hlen));
4439 			error = _EBADIP;
4440 			stats_p->bips_bad_ip++;
4441 			goto done;
4442 		}
4443 		ip_pay_len = ip_total_len - ip_hlen;
4444 		info_p->ip_proto = ip->ip_p;
4445 		info_p->ip_hdr.ip = ip;
4446 #define FRAG_BITS       (IP_OFFMASK | IP_MF)
4447 		if ((ntohs(ip->ip_off) & FRAG_BITS) != 0) {
4448 			info_p->ip_is_fragmented = true;
4449 		}
4450 		stats_p->bips_ip++;
4451 	} else {
4452 		struct ip6_hdr *ip6;
4453 
4454 		/* IPv6 */
4455 		hlen = mac_hlen + sizeof(struct ip6_hdr);
4456 		if (m0->m_pkthdr.len < hlen) {
4457 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4458 			    "short IPv6 packet %d < %d",
4459 			    m0->m_pkthdr.len, hlen);
4460 			error = _EBADIPV6;
4461 			stats_p->bips_bad_ip6++;
4462 			goto done;
4463 		}
4464 		if (m0->m_len < hlen) {
4465 			*mp = m0 = m_pullup(m0, hlen);
4466 			if (m0 == NULL) {
4467 				BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4468 				    "m_pullup failed hlen %d",
4469 				    hlen);
4470 				error = ENOBUFS;
4471 				stats_p->bips_bad_ip6++;
4472 				goto done;
4473 			}
4474 		}
4475 		ip6 = (struct ip6_hdr *)(mtod(m0, uint8_t *) + mac_hlen);
4476 		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
4477 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4478 			    "bad IPv6 version");
4479 			error = _EBADIPV6;
4480 			stats_p->bips_bad_ip6++;
4481 			goto done;
4482 		}
4483 		off = get_last_ip6_hdr(m0, mac_hlen, IPPROTO_IPV6, &proto,
4484 		    &info_p->ip_is_fragmented);
4485 		if (off < 0 || m0->m_pkthdr.len < off) {
4486 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4487 			    "ip6_lasthdr() returned %d",
4488 			    off);
4489 			error = _EBADIPV6;
4490 			stats_p->bips_bad_ip6++;
4491 			goto done;
4492 		}
4493 		ip_hlen = sizeof(*ip6);
4494 		opt_len = off - mac_hlen - ip_hlen;
4495 		if (opt_len < 0) {
4496 			error = _EBADIPV6;
4497 			stats_p->bips_bad_ip6++;
4498 			goto done;
4499 		}
4500 		info_p->ip_proto = proto;
4501 		info_p->ip_hdr.ip6 = ip6;
4502 		ip_pay_len = ntohs(ip6->ip6_plen);
4503 		if (ip_pay_len > (m0->m_pkthdr.len - mac_hlen - ip_hlen)) {
4504 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4505 			    "invalid IPv6 payload length %d > %d",
4506 			    ip_pay_len,
4507 			    (m0->m_pkthdr.len - mac_hlen - ip_hlen));
4508 			error = _EBADIPV6;
4509 			stats_p->bips_bad_ip6++;
4510 			goto done;
4511 		}
4512 		stats_p->bips_ip6++;
4513 	}
4514 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4515 	    "IPv%c proto %d ip %u pay %u opt %u pkt %u%s",
4516 	    is_ipv4 ? '4' : '6',
4517 	    proto, ip_hlen, ip_pay_len, opt_len,
4518 	    m0->m_pkthdr.len, info_p->ip_is_fragmented ? " frag" : "");
4519 	info_p->ip_hlen = ip_hlen;
4520 	info_p->ip_pay_len = ip_pay_len;
4521 	info_p->ip_opt_len = opt_len;
4522 	info_p->ip_is_ipv4 = is_ipv4;
4523 done:
4524 	return error;
4525 }
4526 
4527 static int
4528 bridge_get_tcp_header(struct mbuf * * mp, u_int mac_hlen, bool is_ipv4,
4529     ip_packet_info_t info_p, struct bripstats * stats_p)
4530 {
4531 	int             error;
4532 	u_int           hlen;
4533 
4534 	error = bridge_get_ip_proto(mp, mac_hlen, is_ipv4, info_p, stats_p);
4535 	if (error != 0) {
4536 		goto done;
4537 	}
4538 	if (info_p->ip_proto != IPPROTO_TCP) {
4539 		/* not a TCP frame, not an error, just a bad guess */
4540 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4541 		    "non-TCP (%d) IPv%c frame %d bytes",
4542 		    info_p->ip_proto, is_ipv4 ? '4' : '6',
4543 		    (*mp)->m_pkthdr.len);
4544 		goto done;
4545 	}
4546 	if (info_p->ip_is_fragmented) {
4547 		/* both TSO and IP fragmentation don't make sense */
4548 		BRIDGE_LOG(LOG_NOTICE, BR_DBGF_CHECKSUM,
4549 		    "fragmented TSO packet?");
4550 		stats_p->bips_bad_tcp++;
4551 		error = _EBADTCP;
4552 		goto done;
4553 	}
4554 	hlen = mac_hlen + info_p->ip_hlen + sizeof(struct tcphdr) +
4555 	    info_p->ip_opt_len;
4556 	if ((*mp)->m_len < hlen) {
4557 		*mp = m_pullup(*mp, hlen);
4558 		if (*mp == NULL) {
4559 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4560 			    "m_pullup %d failed",
4561 			    hlen);
4562 			stats_p->bips_bad_tcp++;
4563 			error = _EBADTCP;
4564 			goto done;
4565 		}
4566 	}
4567 	info_p->ip_proto_hdr = ((caddr_t)info_p->ip_hdr.ptr) +
4568 	    info_p->ip_hlen + info_p->ip_opt_len;
4569 done:
4570 	return error;
4571 }
4572 
4573 static inline void
4574 proto_csum_stats_increment(uint8_t proto, struct brcsumstats * stats_p)
4575 {
4576 	if (proto == IPPROTO_TCP) {
4577 		stats_p->brcs_tcp_checksum++;
4578 	} else {
4579 		stats_p->brcs_udp_checksum++;
4580 	}
4581 	return;
4582 }
4583 
4584 static bool
4585 ether_header_type_is_ip(struct ether_header * eh, bool *is_ipv4)
4586 {
4587 	uint16_t        ether_type;
4588 	bool            is_ip = TRUE;
4589 
4590 	ether_type = ntohs(eh->ether_type);
4591 	switch (ether_type) {
4592 	case ETHERTYPE_IP:
4593 		*is_ipv4 = TRUE;
4594 		break;
4595 	case ETHERTYPE_IPV6:
4596 		*is_ipv4 = FALSE;
4597 		break;
4598 	default:
4599 		is_ip = FALSE;
4600 		break;
4601 	}
4602 	return is_ip;
4603 }
4604 
4605 static errno_t
4606 bridge_verify_checksum(struct mbuf * * mp, struct ifbrmstats *stats_p)
4607 {
4608 	struct brcsumstats *csum_stats_p;
4609 	struct ether_header     *eh;
4610 	errno_t         error = 0;
4611 	ip_packet_info  info;
4612 	bool            is_ipv4;
4613 	struct mbuf *   m;
4614 	u_int           mac_hlen = sizeof(struct ether_header);
4615 	uint16_t        sum;
4616 	bool            valid;
4617 
4618 	eh = mtod(*mp, struct ether_header *);
4619 	if (!ether_header_type_is_ip(eh, &is_ipv4)) {
4620 		goto done;
4621 	}
4622 	error = bridge_get_ip_proto(mp, mac_hlen, is_ipv4, &info,
4623 	    &stats_p->brms_out_ip);
4624 	m = *mp;
4625 	if (error != 0) {
4626 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4627 		    "bridge_get_ip_proto failed %d",
4628 		    error);
4629 		goto done;
4630 	}
4631 	if (is_ipv4) {
4632 		if ((m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) != 0) {
4633 			/* hardware offloaded IP header checksum */
4634 			valid = (m->m_pkthdr.csum_flags & CSUM_IP_VALID) != 0;
4635 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4636 			    "IP checksum HW %svalid",
4637 			    valid ? "" : "in");
4638 			if (!valid) {
4639 				stats_p->brms_out_cksum_bad_hw.brcs_ip_checksum++;
4640 				error = _EBADIPCHECKSUM;
4641 				goto done;
4642 			}
4643 			stats_p->brms_out_cksum_good_hw.brcs_ip_checksum++;
4644 		} else {
4645 			/* verify */
4646 			sum = inet_cksum(m, 0, mac_hlen, info.ip_hlen);
4647 			valid = (sum == 0);
4648 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4649 			    "IP checksum SW %svalid",
4650 			    valid ? "" : "in");
4651 			if (!valid) {
4652 				stats_p->brms_out_cksum_bad.brcs_ip_checksum++;
4653 				error = _EBADIPCHECKSUM;
4654 				goto done;
4655 			}
4656 			stats_p->brms_out_cksum_good.brcs_ip_checksum++;
4657 		}
4658 	}
4659 	if (info.ip_is_fragmented) {
4660 		/* can't verify checksum on fragmented packets */
4661 		goto done;
4662 	}
4663 	switch (info.ip_proto) {
4664 	case IPPROTO_TCP:
4665 		stats_p->brms_out_ip.bips_tcp++;
4666 		break;
4667 	case IPPROTO_UDP:
4668 		stats_p->brms_out_ip.bips_udp++;
4669 		break;
4670 	default:
4671 		goto done;
4672 	}
4673 	/* check for hardware offloaded UDP/TCP checksum */
4674 #define HW_CSUM         (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)
4675 	if ((m->m_pkthdr.csum_flags & HW_CSUM) == HW_CSUM) {
4676 		/* checksum verified by hardware */
4677 		valid = (m->m_pkthdr.csum_rx_val == 0xffff);
4678 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4679 		    "IPv%c %s checksum HW 0x%x %svalid",
4680 		    is_ipv4 ? '4' : '6',
4681 		    (info.ip_proto == IPPROTO_TCP)
4682 		    ? "TCP" : "UDP",
4683 		    m->m_pkthdr.csum_data,
4684 		    valid ? "" : "in" );
4685 		if (!valid) {
4686 			/* bad checksum */
4687 			csum_stats_p = &stats_p->brms_out_cksum_bad_hw;
4688 			error = (info.ip_proto == IPPROTO_TCP) ? _EBADTCPCHECKSUM
4689 			    : _EBADTCPCHECKSUM;
4690 		} else {
4691 			/* good checksum */
4692 			csum_stats_p = &stats_p->brms_out_cksum_good_hw;
4693 		}
4694 		proto_csum_stats_increment(info.ip_proto, csum_stats_p);
4695 		goto done;
4696 	}
4697 	m->m_data += mac_hlen;
4698 	m->m_len -= mac_hlen;
4699 	m->m_pkthdr.len -= mac_hlen;
4700 	if (is_ipv4) {
4701 		sum = inet_cksum(m, info.ip_proto,
4702 		    info.ip_hlen,
4703 		    info.ip_pay_len);
4704 	} else {
4705 		sum = inet6_cksum(m, info.ip_proto,
4706 		    info.ip_hlen + info.ip_opt_len,
4707 		    info.ip_pay_len - info.ip_opt_len);
4708 	}
4709 	valid = (sum == 0);
4710 	if (valid) {
4711 		csum_stats_p = &stats_p->brms_out_cksum_good;
4712 	} else {
4713 		csum_stats_p = &stats_p->brms_out_cksum_bad;
4714 		error = (info.ip_proto == IPPROTO_TCP)
4715 		    ? _EBADTCPCHECKSUM : _EBADUDPCHECKSUM;
4716 	}
4717 	proto_csum_stats_increment(info.ip_proto, csum_stats_p);
4718 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4719 	    "IPv%c %s checksum SW %svalid (0x%x) hlen %d paylen %d",
4720 	    is_ipv4 ? '4' : '6',
4721 	    (info.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
4722 	    valid ? "" : "in",
4723 	    sum, info.ip_hlen, info.ip_pay_len);
4724 	m->m_data -= mac_hlen;
4725 	m->m_len += mac_hlen;
4726 	m->m_pkthdr.len += mac_hlen;
4727 done:
4728 	return error;
4729 }
4730 
4731 static errno_t
4732 bridge_offload_checksum(struct mbuf * * mp, ip_packet_info * info_p,
4733     struct ifbrmstats * stats_p)
4734 {
4735 	uint16_t *      csum_p;
4736 	errno_t         error = 0;
4737 	u_int           hlen;
4738 	struct mbuf *   m0 = *mp;
4739 	u_int           mac_hlen = sizeof(struct ether_header);
4740 	u_int           pkt_hdr_len;
4741 	struct tcphdr * tcp;
4742 	u_int           tcp_hlen;
4743 	struct udphdr * udp;
4744 
4745 	if (info_p->ip_is_ipv4) {
4746 		/* compute IP header checksum */
4747 		info_p->ip_hdr.ip->ip_sum = 0;
4748 		info_p->ip_hdr.ip->ip_sum = inet_cksum(m0, 0, mac_hlen,
4749 		    info_p->ip_hlen);
4750 		stats_p->brms_in_computed_cksum.brcs_ip_checksum++;
4751 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4752 		    "IPv4 checksum 0x%x",
4753 		    ntohs(info_p->ip_hdr.ip->ip_sum));
4754 	}
4755 	if (info_p->ip_is_fragmented) {
4756 		/* can't compute checksum on fragmented packets */
4757 		goto done;
4758 	}
4759 	pkt_hdr_len = m0->m_pkthdr.len;
4760 	switch (info_p->ip_proto) {
4761 	case IPPROTO_TCP:
4762 		hlen = mac_hlen + info_p->ip_hlen + info_p->ip_opt_len
4763 		    + sizeof(struct tcphdr);
4764 		if (m0->m_len < hlen) {
4765 			*mp = m0 = m_pullup(m0, hlen);
4766 			if (m0 == NULL) {
4767 				stats_p->brms_in_ip.bips_bad_tcp++;
4768 				error = _EBADTCP;
4769 				goto done;
4770 			}
4771 		}
4772 		tcp = (struct tcphdr *)(void *)
4773 		    ((caddr_t)info_p->ip_hdr.ptr + info_p->ip_hlen
4774 		    + info_p->ip_opt_len);
4775 		tcp_hlen = tcp->th_off << 2;
4776 		hlen = mac_hlen + info_p->ip_hlen + info_p->ip_opt_len + tcp_hlen;
4777 		if (hlen > pkt_hdr_len) {
4778 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4779 			    "bad tcp header length %u",
4780 			    tcp_hlen);
4781 			stats_p->brms_in_ip.bips_bad_tcp++;
4782 			error = _EBADTCP;
4783 			goto done;
4784 		}
4785 		csum_p = &tcp->th_sum;
4786 		stats_p->brms_in_ip.bips_tcp++;
4787 		break;
4788 	case IPPROTO_UDP:
4789 		hlen = mac_hlen + info_p->ip_hlen + info_p->ip_opt_len + sizeof(*udp);
4790 		if (m0->m_len < hlen) {
4791 			*mp = m0 = m_pullup(m0, hlen);
4792 			if (m0 == NULL) {
4793 				stats_p->brms_in_ip.bips_bad_udp++;
4794 				error = ENOBUFS;
4795 				goto done;
4796 			}
4797 		}
4798 		udp = (struct udphdr *)(void *)
4799 		    ((caddr_t)info_p->ip_hdr.ptr + info_p->ip_hlen
4800 		    + info_p->ip_opt_len);
4801 		csum_p = &udp->uh_sum;
4802 		stats_p->brms_in_ip.bips_udp++;
4803 		break;
4804 	default:
4805 		/* not TCP or UDP */
4806 		goto done;
4807 	}
4808 	*csum_p = 0;
4809 	m0->m_data += mac_hlen;
4810 	m0->m_len -= mac_hlen;
4811 	m0->m_pkthdr.len -= mac_hlen;
4812 	if (info_p->ip_is_ipv4) {
4813 		*csum_p = inet_cksum(m0, info_p->ip_proto, info_p->ip_hlen,
4814 		    info_p->ip_pay_len);
4815 	} else {
4816 		*csum_p = inet6_cksum(m0, info_p->ip_proto,
4817 		    info_p->ip_hlen + info_p->ip_opt_len,
4818 		    info_p->ip_pay_len - info_p->ip_opt_len);
4819 	}
4820 	if (info_p->ip_proto == IPPROTO_UDP && *csum_p == 0) {
4821 		/* RFC 1122 4.1.3.4 */
4822 		*csum_p = 0xffff;
4823 	}
4824 	m0->m_data -= mac_hlen;
4825 	m0->m_len += mac_hlen;
4826 	m0->m_pkthdr.len += mac_hlen;
4827 	proto_csum_stats_increment(info_p->ip_proto,
4828 	    &stats_p->brms_in_computed_cksum);
4829 
4830 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4831 	    "IPv%c %s set checksum 0x%x",
4832 	    info_p->ip_is_ipv4 ? '4' : '6',
4833 	    (info_p->ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
4834 	    ntohs(*csum_p));
4835 done:
4836 	return error;
4837 }
4838 
4839 static errno_t
4840 bridge_send(struct ifnet *src_ifp,
4841     struct ifnet *dst_ifp, struct mbuf *m, ChecksumOperation cksum_op)
4842 {
4843 	switch (cksum_op) {
4844 	case CHECKSUM_OPERATION_CLEAR_OFFLOAD:
4845 		m->m_pkthdr.csum_flags &= ~CSUM_TX_FLAGS;
4846 		break;
4847 	case CHECKSUM_OPERATION_FINALIZE:
4848 		/* the checksum might not be correct, finalize now */
4849 		bridge_finalize_cksum(dst_ifp, m);
4850 		break;
4851 	case CHECKSUM_OPERATION_COMPUTE:
4852 		bridge_compute_cksum(src_ifp, dst_ifp, m);
4853 		break;
4854 	default:
4855 		break;
4856 	}
4857 #if HAS_IF_CAP
4858 	/*
4859 	 * If underlying interface can not do VLAN tag insertion itself
4860 	 * then attach a packet tag that holds it.
4861 	 */
4862 	if ((m->m_flags & M_VLANTAG) &&
4863 	    (dst_ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) {
4864 		m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
4865 		if (m == NULL) {
4866 			BRIDGE_LOG(LOG_NOTICE, BR_DBGF_CHECKSUM,
4867 			    "%s: unable to prepend VLAN header",
4868 			    dst_ifp->if_xname);
4869 			(void) ifnet_stat_increment_out(dst_ifp,
4870 			    0, 0, 1);
4871 			return 0;
4872 		}
4873 		m->m_flags &= ~M_VLANTAG;
4874 	}
4875 #endif /* HAS_IF_CAP */
4876 	return bridge_transmit(dst_ifp, m);
4877 }
4878 
4879 static errno_t
4880 bridge_send_tso(struct ifnet *dst_ifp, struct mbuf *m, bool is_ipv4)
4881 {
4882 	errno_t                 error;
4883 	u_int                   mac_hlen;
4884 
4885 	mac_hlen = sizeof(struct ether_header);
4886 
4887 #if HAS_IF_CAP
4888 	/*
4889 	 * If underlying interface can not do VLAN tag insertion itself
4890 	 * then attach a packet tag that holds it.
4891 	 */
4892 	if ((m->m_flags & M_VLANTAG) &&
4893 	    (dst_ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) {
4894 		m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
4895 		if (m == NULL) {
4896 			BRIDGE_LOG(LOG_NOTICE, BR_DBGF_CHECKSUM,
4897 			    "%s: unable to prepend VLAN header",
4898 			    dst_ifp->if_xname);
4899 			(void) ifnet_stat_increment_out(dst_ifp,
4900 			    0, 0, 1);
4901 			error = ENOBUFS;
4902 			goto done;
4903 		}
4904 		m->m_flags &= ~M_VLANTAG;
4905 		mac_hlen += ETHER_VLAN_ENCAP_LEN;
4906 	}
4907 #endif /* HAS_IF_CAP */
4908 	error = gso_tcp(dst_ifp, &m, mac_hlen, is_ipv4, TRUE);
4909 	return error;
4910 }
4911 
4912 /*
4913  * tso_hwassist:
4914  * - determine whether the destination interface supports TSO offload
4915  * - if the packet is already marked for offload and the hardware supports
4916  *   it, just allow the packet to continue on
4917  * - if not, parse the packet headers to verify that this is a large TCP
4918  *   packet requiring segmentation; if the hardware doesn't support it
4919  *   set need_sw_tso; otherwise, mark the packet for TSO offload
4920  */
4921 static int
4922 tso_hwassist(struct mbuf **mp, bool is_ipv4, struct ifnet * ifp, u_int mac_hlen,
4923     bool * need_sw_tso, bool * is_large_tcp)
4924 {
4925 	int             error = 0;
4926 	u_int32_t       if_csum;
4927 	u_int32_t       if_tso;
4928 	u_int32_t       mbuf_tso;
4929 	bool            supports_cksum = false;
4930 
4931 	*need_sw_tso = false;
4932 	*is_large_tcp = false;
4933 	if (is_ipv4) {
4934 		/*
4935 		 * Enable both TCP and IP offload if the hardware supports it.
4936 		 * If the hardware doesn't support TCP offload, supports_cksum
4937 		 * will be false so we won't set either offload.
4938 		 */
4939 		if_csum = ifp->if_hwassist & (CSUM_TCP | CSUM_IP);
4940 		supports_cksum = (if_csum & CSUM_TCP) != 0;
4941 		if_tso = IFNET_TSO_IPV4;
4942 		mbuf_tso = CSUM_TSO_IPV4;
4943 	} else {
4944 		supports_cksum = (ifp->if_hwassist & CSUM_TCPIPV6) != 0;
4945 		if_csum = CSUM_TCPIPV6;
4946 		if_tso = IFNET_TSO_IPV6;
4947 		mbuf_tso = CSUM_TSO_IPV6;
4948 	}
4949 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
4950 	    "%s: does%s support checksum 0x%x if_csum 0x%x",
4951 	    ifp->if_xname, supports_cksum ? "" : " not",
4952 	    ifp->if_hwassist, if_csum);
4953 	if ((ifp->if_hwassist & if_tso) != 0 &&
4954 	    ((*mp)->m_pkthdr.csum_flags & mbuf_tso) != 0) {
4955 		/* hardware TSO, mbuf already marked */
4956 	} else {
4957 		/* verify that this is a large TCP frame */
4958 		uint32_t                csum_flags;
4959 		ip_packet_info          info;
4960 		int                     mss;
4961 		struct bripstats        stats;
4962 		struct tcphdr *         tcp;
4963 
4964 		error = bridge_get_tcp_header(mp, mac_hlen, is_ipv4,
4965 		    &info, &stats);
4966 		if (error != 0) {
4967 			/* bad packet */
4968 			goto done;
4969 		}
4970 		if ((info.ip_hlen + info.ip_pay_len + info.ip_opt_len) <=
4971 		    ifp->if_mtu) {
4972 			/* not actually a large packet */
4973 			goto done;
4974 		}
4975 		if (info.ip_proto_hdr == NULL) {
4976 			/* not a TCP packet */
4977 			goto done;
4978 		}
4979 		if ((ifp->if_hwassist & if_tso) == 0) {
4980 			/* hardware does not support TSO, enable sw tso */
4981 			*need_sw_tso = if_bridge_segmentation != 0;
4982 			goto done;
4983 		}
4984 		/* use hardware TSO */
4985 		(*mp)->m_pkthdr.pkt_proto = IPPROTO_TCP;
4986 		tcp = (struct tcphdr *)info.ip_proto_hdr;
4987 		mss = ifp->if_mtu - info.ip_hlen - info.ip_opt_len
4988 		    - (tcp->th_off << 2) - if_bridge_tso_reduce_mss_tx;
4989 		assert(mss > 0);
4990 		csum_flags = mbuf_tso;
4991 		if (supports_cksum) {
4992 			csum_flags |= if_csum;
4993 		}
4994 		(*mp)->m_pkthdr.tso_segsz = mss;
4995 		(*mp)->m_pkthdr.csum_flags |= csum_flags;
4996 		(*mp)->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
4997 		*is_large_tcp = true;
4998 	}
4999 done:
5000 	return error;
5001 }
5002 
5003 /*
5004  * bridge_enqueue:
5005  *
5006  *	Enqueue a packet on a bridge member interface.
5007  *
5008  */
5009 static errno_t
5010 bridge_enqueue(ifnet_t bridge_ifp, struct ifnet *src_ifp,
5011     struct ifnet *dst_ifp, struct mbuf *m, ChecksumOperation cksum_op)
5012 {
5013 	errno_t         error = 0;
5014 	int             len;
5015 
5016 	VERIFY(dst_ifp != NULL);
5017 
5018 	/*
5019 	 * We may be sending a fragment so traverse the mbuf
5020 	 *
5021 	 * NOTE: bridge_fragment() is called only when PFIL_HOOKS is enabled.
5022 	 */
5023 	for (struct mbuf *next_m = NULL; m != NULL; m = next_m) {
5024 		bool            need_sw_tso = false;
5025 		bool            is_ipv4 = false;
5026 		bool            is_large_pkt;
5027 		errno_t         _error = 0;
5028 
5029 		len = m->m_pkthdr.len;
5030 		m->m_flags |= M_PROTO1; /* set to avoid loops */
5031 		next_m = m->m_nextpkt;
5032 		m->m_nextpkt = NULL;
5033 		/*
5034 		 * Need to segment the packet if it is a large frame
5035 		 * and the destination interface does not support TSO.
5036 		 *
5037 		 * Note that with trailers, it's possible for a packet to
5038 		 * be large but not actually require segmentation.
5039 		 */
5040 		is_large_pkt = (len > (bridge_ifp->if_mtu + ETHER_HDR_LEN));
5041 		if (is_large_pkt) {
5042 			struct ether_header     *eh;
5043 			bool                    is_large_tcp = false;
5044 
5045 			eh = mtod(m, struct ether_header *);
5046 			if (ether_header_type_is_ip(eh, &is_ipv4)) {
5047 				_error = tso_hwassist(&m, is_ipv4,
5048 				    dst_ifp, sizeof(struct ether_header),
5049 				    &need_sw_tso, &is_large_tcp);
5050 				if (is_large_tcp) {
5051 					cksum_op = CHECKSUM_OPERATION_NONE;
5052 				}
5053 			} else {
5054 				BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
5055 				    "large non IP packet");
5056 			}
5057 		}
5058 		if (_error != 0) {
5059 			if (m != NULL) {
5060 				m_freem(m);
5061 			}
5062 		} else if (need_sw_tso) {
5063 			_error = bridge_send_tso(dst_ifp, m, is_ipv4);
5064 		} else {
5065 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
5066 			    "%s bridge_send(%s) len %d op %d",
5067 			    bridge_ifp->if_xname,
5068 			    dst_ifp->if_xname,
5069 			    len, cksum_op);
5070 			_error = bridge_send(src_ifp, dst_ifp, m, cksum_op);
5071 		}
5072 
5073 		/* Preserve first error value */
5074 		if (error == 0 && _error != 0) {
5075 			error = _error;
5076 		}
5077 		if (_error == 0) {
5078 			(void) ifnet_stat_increment_out(bridge_ifp, 1, len, 0);
5079 		} else {
5080 			(void) ifnet_stat_increment_out(bridge_ifp, 0, 0, 1);
5081 		}
5082 	}
5083 
5084 	return error;
5085 }
5086 
5087 #if HAS_BRIDGE_DUMMYNET
5088 /*
5089  * bridge_dummynet:
5090  *
5091  *	Receive a queued packet from dummynet and pass it on to the output
5092  *	interface.
5093  *
5094  *	The mbuf has the Ethernet header already attached.
5095  */
5096 static void
5097 bridge_dummynet(struct mbuf *m, struct ifnet *ifp)
5098 {
5099 	struct bridge_softc *sc;
5100 
5101 	sc = ifp->if_bridge;
5102 
5103 	/*
5104 	 * The packet didn't originate from a member interface. This should only
5105 	 * ever happen if a member interface is removed while packets are
5106 	 * queued for it.
5107 	 */
5108 	if (sc == NULL) {
5109 		m_freem(m);
5110 		return;
5111 	}
5112 
5113 	if (PFIL_HOOKED(&inet_pfil_hook) || PFIL_HOOKED_INET6) {
5114 		if (bridge_pfil(&m, sc->sc_ifp, ifp, PFIL_OUT) != 0) {
5115 			return;
5116 		}
5117 		if (m == NULL) {
5118 			return;
5119 		}
5120 	}
5121 	(void) bridge_enqueue(sc->sc_ifp, NULL, ifp, m, CHECKSUM_OPERATION_NONE);
5122 }
5123 
5124 #endif /* HAS_BRIDGE_DUMMYNET */
5125 
5126 /*
5127  * bridge_member_output:
5128  *
5129  *	Send output from a bridge member interface.  This
5130  *	performs the bridging function for locally originated
5131  *	packets.
5132  *
5133  *	The mbuf has the Ethernet header already attached.
5134  */
5135 static errno_t
5136 bridge_member_output(struct bridge_softc *sc, ifnet_t ifp, mbuf_t *data)
5137 {
5138 	ifnet_t bridge_ifp;
5139 	struct ether_header *eh;
5140 	struct ifnet *dst_if;
5141 	uint16_t vlan;
5142 	struct bridge_iflist *mac_nat_bif;
5143 	ifnet_t mac_nat_ifp;
5144 	mbuf_t m = *data;
5145 
5146 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_OUTPUT,
5147 	    "ifp %s", ifp->if_xname);
5148 	if (m->m_len < ETHER_HDR_LEN) {
5149 		m = m_pullup(m, ETHER_HDR_LEN);
5150 		if (m == NULL) {
5151 			*data = NULL;
5152 			return EJUSTRETURN;
5153 		}
5154 	}
5155 
5156 	eh = mtod(m, struct ether_header *);
5157 	vlan = VLANTAGOF(m);
5158 
5159 	BRIDGE_LOCK(sc);
5160 	mac_nat_bif = sc->sc_mac_nat_bif;
5161 	mac_nat_ifp = (mac_nat_bif != NULL) ? mac_nat_bif->bif_ifp : NULL;
5162 	if (mac_nat_ifp == ifp) {
5163 		/* record the IP address used by the MAC NAT interface */
5164 		(void)bridge_mac_nat_output(sc, mac_nat_bif, data, NULL);
5165 		m = *data;
5166 		if (m == NULL) {
5167 			/* packet was deallocated */
5168 			BRIDGE_UNLOCK(sc);
5169 			return EJUSTRETURN;
5170 		}
5171 	}
5172 	bridge_ifp = sc->sc_ifp;
5173 
5174 	/*
5175 	 * APPLE MODIFICATION
5176 	 * If the packet is an 802.1X ethertype, then only send on the
5177 	 * original output interface.
5178 	 */
5179 	if (eh->ether_type == htons(ETHERTYPE_PAE)) {
5180 		dst_if = ifp;
5181 		goto sendunicast;
5182 	}
5183 
5184 	/*
5185 	 * If bridge is down, but the original output interface is up,
5186 	 * go ahead and send out that interface.  Otherwise, the packet
5187 	 * is dropped below.
5188 	 */
5189 	if ((bridge_ifp->if_flags & IFF_RUNNING) == 0) {
5190 		dst_if = ifp;
5191 		goto sendunicast;
5192 	}
5193 
5194 	/*
5195 	 * If the packet is a multicast, or we don't know a better way to
5196 	 * get there, send to all interfaces.
5197 	 */
5198 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
5199 		dst_if = NULL;
5200 	} else {
5201 		dst_if = bridge_rtlookup(sc, eh->ether_dhost, vlan);
5202 	}
5203 	if (dst_if == NULL) {
5204 		struct bridge_iflist *bif;
5205 		struct mbuf *mc;
5206 		errno_t error;
5207 
5208 
5209 		bridge_span(sc, m);
5210 
5211 		BRIDGE_LOCK2REF(sc, error);
5212 		if (error != 0) {
5213 			m_freem(m);
5214 			return EJUSTRETURN;
5215 		}
5216 
5217 		/*
5218 		 * Duplicate and send the packet across all member interfaces
5219 		 * except the originating interface.
5220 		 */
5221 		TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) {
5222 			dst_if = bif->bif_ifp;
5223 			if (dst_if == ifp) {
5224 				/* skip the originating interface */
5225 				continue;
5226 			}
5227 			/* skip interface with inactive link status */
5228 			if ((bif->bif_flags & BIFF_MEDIA_ACTIVE) == 0) {
5229 				continue;
5230 			}
5231 #if 0
5232 			if (dst_if->if_type == IFT_GIF) {
5233 				continue;
5234 			}
5235 #endif
5236 			/* skip interface that isn't running */
5237 			if ((dst_if->if_flags & IFF_RUNNING) == 0) {
5238 				continue;
5239 			}
5240 			/*
5241 			 * If the interface is participating in spanning
5242 			 * tree, make sure the port is in a state that
5243 			 * allows forwarding.
5244 			 */
5245 			if ((bif->bif_ifflags & IFBIF_STP) &&
5246 			    bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
5247 				continue;
5248 			}
5249 			/*
5250 			 * If the destination is the MAC NAT interface,
5251 			 * skip sending the packet. The packet can't be sent
5252 			 * if the source MAC is incorrect.
5253 			 */
5254 			if (dst_if == mac_nat_ifp) {
5255 				continue;
5256 			}
5257 
5258 			/* make a deep copy to send on this member interface */
5259 			mc = m_dup(m, M_DONTWAIT);
5260 			if (mc == NULL) {
5261 				(void)ifnet_stat_increment_out(bridge_ifp,
5262 				    0, 0, 1);
5263 				continue;
5264 			}
5265 			(void)bridge_enqueue(bridge_ifp, ifp, dst_if,
5266 			    mc, CHECKSUM_OPERATION_COMPUTE);
5267 		}
5268 		BRIDGE_UNREF(sc);
5269 
5270 		if ((ifp->if_flags & IFF_RUNNING) == 0) {
5271 			m_freem(m);
5272 			return EJUSTRETURN;
5273 		}
5274 		/* allow packet to continue on the originating interface */
5275 		return 0;
5276 	}
5277 
5278 sendunicast:
5279 	/*
5280 	 * XXX Spanning tree consideration here?
5281 	 */
5282 
5283 	bridge_span(sc, m);
5284 	if ((dst_if->if_flags & IFF_RUNNING) == 0) {
5285 		m_freem(m);
5286 		BRIDGE_UNLOCK(sc);
5287 		return EJUSTRETURN;
5288 	}
5289 
5290 	BRIDGE_UNLOCK(sc);
5291 	if (dst_if == ifp) {
5292 		/* allow packet to continue on the originating interface */
5293 		return 0;
5294 	}
5295 	if (dst_if != mac_nat_ifp) {
5296 		(void) bridge_enqueue(bridge_ifp, ifp, dst_if, m,
5297 		    CHECKSUM_OPERATION_COMPUTE);
5298 	} else {
5299 		/*
5300 		 * This is not the original output interface
5301 		 * and the destination is the MAC NAT interface.
5302 		 * Drop the packet because the packet can't be sent
5303 		 * if the source MAC is incorrect.
5304 		 */
5305 		m_freem(m);
5306 	}
5307 	return EJUSTRETURN;
5308 }
5309 
5310 /*
5311  * Output callback.
5312  *
5313  * This routine is called externally from above only when if_bridge_txstart
5314  * is disabled; otherwise it is called internally by bridge_start().
5315  */
5316 static int
5317 bridge_output(struct ifnet *ifp, struct mbuf *m)
5318 {
5319 	struct bridge_softc *sc = ifnet_softc(ifp);
5320 	struct ether_header *eh;
5321 	struct ifnet *dst_if = NULL;
5322 	int error = 0;
5323 
5324 	eh = mtod(m, struct ether_header *);
5325 
5326 	BRIDGE_LOCK(sc);
5327 
5328 	if (!(m->m_flags & (M_BCAST | M_MCAST))) {
5329 		dst_if = bridge_rtlookup(sc, eh->ether_dhost, 0);
5330 	}
5331 
5332 	(void) ifnet_stat_increment_out(ifp, 1, m->m_pkthdr.len, 0);
5333 
5334 #if NBPFILTER > 0
5335 	if (sc->sc_bpf_output) {
5336 		bridge_bpf_output(ifp, m);
5337 	}
5338 #endif
5339 
5340 	if (dst_if == NULL) {
5341 		/* callee will unlock */
5342 		bridge_broadcast(sc, NULL, m, 0);
5343 	} else {
5344 		ifnet_t bridge_ifp;
5345 
5346 		bridge_ifp = sc->sc_ifp;
5347 		BRIDGE_UNLOCK(sc);
5348 
5349 		error = bridge_enqueue(bridge_ifp, NULL, dst_if, m,
5350 		    CHECKSUM_OPERATION_FINALIZE);
5351 	}
5352 
5353 	return error;
5354 }
5355 
5356 static void
5357 bridge_finalize_cksum(struct ifnet *ifp, struct mbuf *m)
5358 {
5359 	struct ether_header *eh;
5360 	bool is_ipv4;
5361 	uint32_t sw_csum, hwcap;
5362 	uint32_t did_sw;
5363 	uint32_t csum_flags;
5364 
5365 	eh = mtod(m, struct ether_header *);
5366 	if (!ether_header_type_is_ip(eh, &is_ipv4)) {
5367 		return;
5368 	}
5369 
5370 	/* do in software what the hardware cannot */
5371 	hwcap = (ifp->if_hwassist | CSUM_DATA_VALID);
5372 	csum_flags = m->m_pkthdr.csum_flags;
5373 	sw_csum = csum_flags & ~IF_HWASSIST_CSUM_FLAGS(hwcap);
5374 	sw_csum &= IF_HWASSIST_CSUM_MASK;
5375 
5376 	if (is_ipv4) {
5377 		if ((hwcap & CSUM_PARTIAL) && !(sw_csum & CSUM_DELAY_DATA) &&
5378 		    (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)) {
5379 			if (m->m_pkthdr.csum_flags & CSUM_TCP) {
5380 				uint16_t start =
5381 				    sizeof(*eh) + sizeof(struct ip);
5382 				uint16_t ulpoff =
5383 				    m->m_pkthdr.csum_data & 0xffff;
5384 				m->m_pkthdr.csum_flags |=
5385 				    (CSUM_DATA_VALID | CSUM_PARTIAL);
5386 				m->m_pkthdr.csum_tx_stuff = (ulpoff + start);
5387 				m->m_pkthdr.csum_tx_start = start;
5388 			} else {
5389 				sw_csum |= (CSUM_DELAY_DATA &
5390 				    m->m_pkthdr.csum_flags);
5391 			}
5392 		}
5393 		did_sw = in_finalize_cksum(m, sizeof(*eh), sw_csum);
5394 	} else {
5395 		if ((hwcap & CSUM_PARTIAL) &&
5396 		    !(sw_csum & CSUM_DELAY_IPV6_DATA) &&
5397 		    (m->m_pkthdr.csum_flags & CSUM_DELAY_IPV6_DATA)) {
5398 			if (m->m_pkthdr.csum_flags & CSUM_TCPIPV6) {
5399 				uint16_t start =
5400 				    sizeof(*eh) + sizeof(struct ip6_hdr);
5401 				uint16_t ulpoff =
5402 				    m->m_pkthdr.csum_data & 0xffff;
5403 				m->m_pkthdr.csum_flags |=
5404 				    (CSUM_DATA_VALID | CSUM_PARTIAL);
5405 				m->m_pkthdr.csum_tx_stuff = (ulpoff + start);
5406 				m->m_pkthdr.csum_tx_start = start;
5407 			} else {
5408 				sw_csum |= (CSUM_DELAY_IPV6_DATA &
5409 				    m->m_pkthdr.csum_flags);
5410 			}
5411 		}
5412 		did_sw = in6_finalize_cksum(m, sizeof(*eh), -1, -1, sw_csum);
5413 	}
5414 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
5415 	    "[%s] before 0x%x hwcap 0x%x sw_csum 0x%x did 0x%x after 0x%x",
5416 	    ifp->if_xname, csum_flags, hwcap, sw_csum,
5417 	    did_sw, m->m_pkthdr.csum_flags);
5418 }
5419 
5420 /*
5421  * bridge_start:
5422  *
5423  *	Start output on a bridge.
5424  *
5425  * This routine is invoked by the start worker thread; because we never call
5426  * it directly, there is no need do deploy any serialization mechanism other
5427  * than what's already used by the worker thread, i.e. this is already single
5428  * threaded.
5429  *
5430  * This routine is called only when if_bridge_txstart is enabled.
5431  */
5432 static void
5433 bridge_start(struct ifnet *ifp)
5434 {
5435 	struct mbuf *m;
5436 
5437 	for (;;) {
5438 		if (ifnet_dequeue(ifp, &m) != 0) {
5439 			break;
5440 		}
5441 
5442 		(void) bridge_output(ifp, m);
5443 	}
5444 }
5445 
5446 /*
5447  * bridge_forward:
5448  *
5449  *	The forwarding function of the bridge.
5450  *
5451  *	NOTE: Releases the lock on return.
5452  */
5453 static void
5454 bridge_forward(struct bridge_softc *sc, struct bridge_iflist *sbif,
5455     struct mbuf *m)
5456 {
5457 	struct bridge_iflist *dbif;
5458 	ifnet_t bridge_ifp;
5459 	struct ifnet *src_if, *dst_if;
5460 	struct ether_header *eh;
5461 	uint16_t vlan;
5462 	uint8_t *dst;
5463 	int error;
5464 	struct mac_nat_record mnr;
5465 	bool translate_mac = FALSE;
5466 	uint32_t sc_filter_flags = 0;
5467 
5468 	BRIDGE_LOCK_ASSERT_HELD(sc);
5469 
5470 	bridge_ifp = sc->sc_ifp;
5471 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_OUTPUT,
5472 	    "%s m 0x%llx", bridge_ifp->if_xname,
5473 	    (uint64_t)VM_KERNEL_ADDRPERM(m));
5474 
5475 	src_if = m->m_pkthdr.rcvif;
5476 	if (src_if != sbif->bif_ifp) {
5477 		const char *    src_if_name;
5478 
5479 		src_if_name = (src_if != NULL) ? src_if->if_xname : "?";
5480 		BRIDGE_LOG(LOG_NOTICE, 0,
5481 		    "src_if %s != bif_ifp %s",
5482 		    src_if_name, sbif->bif_ifp->if_xname);
5483 		goto drop;
5484 	}
5485 
5486 	(void) ifnet_stat_increment_in(bridge_ifp, 1, m->m_pkthdr.len, 0);
5487 	vlan = VLANTAGOF(m);
5488 
5489 
5490 	if ((sbif->bif_ifflags & IFBIF_STP) &&
5491 	    sbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
5492 		goto drop;
5493 	}
5494 
5495 	eh = mtod(m, struct ether_header *);
5496 	dst = eh->ether_dhost;
5497 
5498 	/* If the interface is learning, record the address. */
5499 	if (sbif->bif_ifflags & IFBIF_LEARNING) {
5500 		error = bridge_rtupdate(sc, eh->ether_shost, vlan,
5501 		    sbif, 0, IFBAF_DYNAMIC);
5502 		/*
5503 		 * If the interface has addresses limits then deny any source
5504 		 * that is not in the cache.
5505 		 */
5506 		if (error && sbif->bif_addrmax) {
5507 			goto drop;
5508 		}
5509 	}
5510 
5511 	if ((sbif->bif_ifflags & IFBIF_STP) != 0 &&
5512 	    sbif->bif_stp.bp_state == BSTP_IFSTATE_LEARNING) {
5513 		goto drop;
5514 	}
5515 
5516 	/*
5517 	 * At this point, the port either doesn't participate
5518 	 * in spanning tree or it is in the forwarding state.
5519 	 */
5520 
5521 	/*
5522 	 * If the packet is unicast, destined for someone on
5523 	 * "this" side of the bridge, drop it.
5524 	 */
5525 	if ((m->m_flags & (M_BCAST | M_MCAST)) == 0) {
5526 		/* unicast */
5527 		dst_if = bridge_rtlookup(sc, dst, vlan);
5528 		if (src_if == dst_if) {
5529 			goto drop;
5530 		}
5531 	} else {
5532 		/* broadcast/multicast */
5533 
5534 		/*
5535 		 * Check if its a reserved multicast address, any address
5536 		 * listed in 802.1D section 7.12.6 may not be forwarded by the
5537 		 * bridge.
5538 		 * This is currently 01-80-C2-00-00-00 to 01-80-C2-00-00-0F
5539 		 */
5540 		if (dst[0] == 0x01 && dst[1] == 0x80 &&
5541 		    dst[2] == 0xc2 && dst[3] == 0x00 &&
5542 		    dst[4] == 0x00 && dst[5] <= 0x0f) {
5543 			goto drop;
5544 		}
5545 
5546 
5547 		/* ...forward it to all interfaces. */
5548 		atomic_add_64(&bridge_ifp->if_imcasts, 1);
5549 		dst_if = NULL;
5550 	}
5551 
5552 	/*
5553 	 * If we have a destination interface which is a member of our bridge,
5554 	 * OR this is a unicast packet, push it through the bpf(4) machinery.
5555 	 * For broadcast or multicast packets, don't bother because it will
5556 	 * be reinjected into ether_input. We do this before we pass the packets
5557 	 * through the pfil(9) framework, as it is possible that pfil(9) will
5558 	 * drop the packet, or possibly modify it, making it difficult to debug
5559 	 * firewall issues on the bridge.
5560 	 */
5561 #if NBPFILTER > 0
5562 	if (eh->ether_type == htons(ETHERTYPE_RSN_PREAUTH) ||
5563 	    dst_if != NULL || (m->m_flags & (M_BCAST | M_MCAST)) == 0) {
5564 		m->m_pkthdr.rcvif = bridge_ifp;
5565 		BRIDGE_BPF_MTAP_INPUT(sc, m);
5566 	}
5567 #endif /* NBPFILTER */
5568 
5569 	if (dst_if == NULL) {
5570 		/* bridge_broadcast will unlock */
5571 		bridge_broadcast(sc, sbif, m, 1);
5572 		return;
5573 	}
5574 
5575 	/*
5576 	 * Unicast.
5577 	 */
5578 	/*
5579 	 * At this point, we're dealing with a unicast frame
5580 	 * going to a different interface.
5581 	 */
5582 	if ((dst_if->if_flags & IFF_RUNNING) == 0) {
5583 		goto drop;
5584 	}
5585 
5586 	dbif = bridge_lookup_member_if(sc, dst_if);
5587 	if (dbif == NULL) {
5588 		/* Not a member of the bridge (anymore?) */
5589 		goto drop;
5590 	}
5591 
5592 	/* Private segments can not talk to each other */
5593 	if (sbif->bif_ifflags & dbif->bif_ifflags & IFBIF_PRIVATE) {
5594 		goto drop;
5595 	}
5596 
5597 	if ((dbif->bif_ifflags & IFBIF_STP) &&
5598 	    dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
5599 		goto drop;
5600 	}
5601 
5602 #if HAS_DHCPRA_MASK
5603 	/* APPLE MODIFICATION <rdar:6985737> */
5604 	if ((dst_if->if_extflags & IFEXTF_DHCPRA_MASK) != 0) {
5605 		m = ip_xdhcpra_output(dst_if, m);
5606 		if (!m) {
5607 			++bridge_ifp.if_xdhcpra;
5608 			BRIDGE_UNLOCK(sc);
5609 			return;
5610 		}
5611 	}
5612 #endif /* HAS_DHCPRA_MASK */
5613 
5614 	if (dbif == sc->sc_mac_nat_bif) {
5615 		/* determine how to translate the packet */
5616 		translate_mac
5617 		        = bridge_mac_nat_output(sc, sbif, &m, &mnr);
5618 		if (m == NULL) {
5619 			/* packet was deallocated */
5620 			BRIDGE_UNLOCK(sc);
5621 			return;
5622 		}
5623 	} else if (bif_has_checksum_offload(dbif) &&
5624 	    !bif_has_checksum_offload(sbif)) {
5625 		/*
5626 		 * If the destination interface has checksum offload enabled,
5627 		 * verify the checksum now, unless the source interface also has
5628 		 * checksum offload enabled. The checksum in that case has
5629 		 * already just been computed and verifying it is unnecessary.
5630 		 */
5631 		error = bridge_verify_checksum(&m, &dbif->bif_stats);
5632 		if (error != 0) {
5633 			BRIDGE_UNLOCK(sc);
5634 			if (m != NULL) {
5635 				m_freem(m);
5636 			}
5637 			return;
5638 		}
5639 	}
5640 
5641 	sc_filter_flags = sc->sc_filter_flags;
5642 
5643 	BRIDGE_UNLOCK(sc);
5644 	if (PF_IS_ENABLED && (sc_filter_flags & IFBF_FILT_MEMBER)) {
5645 		if (bridge_pf(&m, dst_if, sc_filter_flags, FALSE) != 0) {
5646 			return;
5647 		}
5648 		if (m == NULL) {
5649 			return;
5650 		}
5651 	}
5652 
5653 	/* if we need to, translate the MAC address */
5654 	if (translate_mac) {
5655 		bridge_mac_nat_translate(&m, &mnr, IF_LLADDR(dst_if));
5656 	}
5657 	/*
5658 	 * We're forwarding an inbound packet in which the checksum must
5659 	 * already have been computed and if required, verified.
5660 	 */
5661 	if (m != NULL) {
5662 		(void) bridge_enqueue(bridge_ifp, src_if, dst_if, m,
5663 		    CHECKSUM_OPERATION_CLEAR_OFFLOAD);
5664 	}
5665 	return;
5666 
5667 drop:
5668 	BRIDGE_UNLOCK(sc);
5669 	m_freem(m);
5670 }
5671 
5672 static void
5673 inject_input_packet(ifnet_t ifp, mbuf_t m)
5674 {
5675 	mbuf_pkthdr_setrcvif(m, ifp);
5676 	mbuf_pkthdr_setheader(m, mbuf_data(m));
5677 	mbuf_setdata(m, (char *)mbuf_data(m) + ETHER_HDR_LEN,
5678 	    mbuf_len(m) - ETHER_HDR_LEN);
5679 	mbuf_pkthdr_adjustlen(m, -ETHER_HDR_LEN);
5680 	m->m_flags |= M_PROTO1; /* set to avoid loops */
5681 	dlil_input_packet_list(ifp, m);
5682 	return;
5683 }
5684 
5685 static bool
5686 in_addr_is_ours(struct in_addr ip)
5687 {
5688 	struct in_ifaddr *ia;
5689 	bool             ours = false;
5690 
5691 	lck_rw_lock_shared(&in_ifaddr_rwlock);
5692 	TAILQ_FOREACH(ia, INADDR_HASH(ip.s_addr), ia_hash) {
5693 		if (IA_SIN(ia)->sin_addr.s_addr == ip.s_addr) {
5694 			ours = true;
5695 			break;
5696 		}
5697 	}
5698 	lck_rw_done(&in_ifaddr_rwlock);
5699 	return ours;
5700 }
5701 
5702 static bool
5703 in6_addr_is_ours(const struct in6_addr * ip6_p, uint32_t ifscope)
5704 {
5705 	struct in6_ifaddr       *ia6;
5706 	bool                    ours = false;
5707 
5708 	if (in6_embedded_scope && IN6_IS_ADDR_LINKLOCAL(ip6_p)) {
5709 		struct in6_addr         dst_ip;
5710 
5711 		/* need to embed scope ID for comparison */
5712 		bcopy(ip6_p, &dst_ip, sizeof(dst_ip));
5713 		dst_ip.s6_addr16[1] = htons(ifscope);
5714 		ip6_p = &dst_ip;
5715 	}
5716 	lck_rw_lock_shared(&in6_ifaddr_rwlock);
5717 	TAILQ_FOREACH(ia6, IN6ADDR_HASH(ip6_p), ia6_hash) {
5718 		if (in6_are_addr_equal_scoped(&ia6->ia_addr.sin6_addr, ip6_p,
5719 		    ia6->ia_addr.sin6_scope_id, ifscope)) {
5720 			ours = true;
5721 			break;
5722 		}
5723 	}
5724 	lck_rw_done(&in6_ifaddr_rwlock);
5725 	return ours;
5726 }
5727 
5728 static void
5729 bridge_interface_input(ifnet_t bridge_ifp, mbuf_t m,
5730     bpf_packet_func bpf_input_func)
5731 {
5732 	size_t                  byte_count;
5733 	struct ether_header     *eh;
5734 	errno_t                 error;
5735 	bool                    is_ipv4;
5736 	int                     len;
5737 	u_int                   mac_hlen;
5738 	int                     pkt_count;
5739 
5740 	/* segment large packets before sending them up */
5741 	if (if_bridge_segmentation == 0) {
5742 		goto done;
5743 	}
5744 	len = m->m_pkthdr.len;
5745 	if (len <= (bridge_ifp->if_mtu + ETHER_HDR_LEN)) {
5746 		goto done;
5747 	}
5748 	eh = mtod(m, struct ether_header *);
5749 	if (!ether_header_type_is_ip(eh, &is_ipv4)) {
5750 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
5751 		    "large non IPv4/IPv6 packet");
5752 		goto done;
5753 	}
5754 
5755 	/*
5756 	 * We have a large IPv4/IPv6 TCP packet. Segment it if required.
5757 	 *
5758 	 * If gso_tcp() returns success (0), the packet(s) are
5759 	 * ready to be passed up. If the destination is a local IP address,
5760 	 * the packet will be passed up as a large, single packet.
5761 	 *
5762 	 * If gso_tcp() returns an error, the packet has already
5763 	 * been freed.
5764 	 */
5765 	mac_hlen = sizeof(*eh);
5766 	error = gso_tcp(bridge_ifp, &m, mac_hlen, is_ipv4, FALSE);
5767 	if (error != 0) {
5768 		return;
5769 	}
5770 
5771 done:
5772 	pkt_count = 0;
5773 	byte_count = 0;
5774 	for (mbuf_t scan = m; scan != NULL; scan = scan->m_nextpkt) {
5775 		/* Mark the packet as arriving on the bridge interface */
5776 		mbuf_pkthdr_setrcvif(scan, bridge_ifp);
5777 		mbuf_pkthdr_setheader(scan, mbuf_data(scan));
5778 		if (bpf_input_func != NULL) {
5779 			(*bpf_input_func)(bridge_ifp, scan);
5780 		}
5781 		mbuf_setdata(scan, (char *)mbuf_data(scan) + ETHER_HDR_LEN,
5782 		    mbuf_len(scan) - ETHER_HDR_LEN);
5783 		mbuf_pkthdr_adjustlen(scan, -ETHER_HDR_LEN);
5784 		byte_count += mbuf_pkthdr_len(scan);
5785 		pkt_count++;
5786 	}
5787 	(void)ifnet_stat_increment_in(bridge_ifp, pkt_count, byte_count, 0);
5788 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT,
5789 	    "%s %d packet(s) %ld bytes",
5790 	    bridge_ifp->if_xname, pkt_count, byte_count);
5791 	dlil_input_packet_list(bridge_ifp, m);
5792 	return;
5793 }
5794 
5795 static bool
5796 is_our_ip(ip_packet_info_t info_p, uint32_t ifscope)
5797 {
5798 	bool    ours;
5799 
5800 	if (info_p->ip_is_ipv4) {
5801 		struct in_addr  dst_ip;
5802 
5803 		bcopy(&info_p->ip_hdr.ip->ip_dst, &dst_ip, sizeof(dst_ip));
5804 		ours = in_addr_is_ours(dst_ip);
5805 	} else {
5806 		ours = in6_addr_is_ours(&info_p->ip_hdr.ip6->ip6_dst, ifscope);
5807 	}
5808 	return ours;
5809 }
5810 
5811 static inline errno_t
5812 bridge_vmnet_tag_input(ifnet_t bridge_ifp, ifnet_t ifp,
5813     const u_char * ether_dhost, mbuf_t *mp,
5814     bool is_broadcast, bool is_ip, bool is_ipv4,
5815     ip_packet_info * info_p, struct bripstats * stats_p,
5816     bool *info_initialized)
5817 {
5818 	errno_t         error = 0;
5819 	bool            is_local = false;
5820 	struct pf_mtag *pf_mtag;
5821 	u_int16_t       tag = vmnet_tag;
5822 
5823 	*info_initialized = false;
5824 	if (is_broadcast) {
5825 		if (_ether_cmp(ether_dhost, etherbroadcastaddr) == 0) {
5826 			tag = vmnet_broadcast_tag;
5827 		} else {
5828 			tag = vmnet_multicast_tag;
5829 		}
5830 	} else if (is_ip) {
5831 		unsigned int    mac_hlen = sizeof(struct ether_header);
5832 
5833 		bzero(stats_p, sizeof(*stats_p));
5834 		*info_initialized = true;
5835 		error = bridge_get_ip_proto(mp, mac_hlen, is_ipv4, info_p,
5836 		    stats_p);
5837 		if (error != 0) {
5838 			BRIDGE_LOG(LOG_NOTICE, BR_DBGF_INPUT,
5839 			    "%s(%s) bridge_get_ip_proto failed %d",
5840 			    bridge_ifp->if_xname,
5841 			    ifp->if_xname, error);
5842 			if (*mp == NULL) {
5843 				return EJUSTRETURN;
5844 			}
5845 		} else {
5846 			is_local = is_our_ip(info_p, bridge_ifp->if_index);
5847 			if (is_local) {
5848 				tag = vmnet_local_tag;
5849 			}
5850 		}
5851 	}
5852 	pf_mtag = pf_get_mtag(*mp);
5853 	if (pf_mtag != NULL) {
5854 		pf_mtag->pftag_tag = tag;
5855 	}
5856 #if DEBUG || DEVELOPMENT
5857 	{
5858 		bool forced;
5859 
5860 		BRIDGE_ERROR_GET_FORCED(forced, BRIDGE_FORCE_ONE);
5861 		if (forced) {
5862 			m_freem(*mp);
5863 			*mp = NULL;
5864 			error = EJUSTRETURN;
5865 			goto done;
5866 		}
5867 		BRIDGE_ERROR_GET_FORCED(forced, BRIDGE_FORCE_TWO);
5868 		if (forced) {
5869 			error = _EBADIP;
5870 			goto done;
5871 		}
5872 	}
5873 done:
5874 #endif /* DEBUG || DEVELOPMENT */
5875 	return error;
5876 }
5877 
5878 static void
5879 bripstats_apply(struct bripstats *dst_p, const struct bripstats *src_p)
5880 {
5881 	dst_p->bips_ip += src_p->bips_ip;
5882 	dst_p->bips_ip6 += src_p->bips_ip6;
5883 	dst_p->bips_udp += src_p->bips_udp;
5884 	dst_p->bips_tcp += src_p->bips_tcp;
5885 
5886 	dst_p->bips_bad_ip += src_p->bips_bad_ip;
5887 	dst_p->bips_bad_ip6 += src_p->bips_bad_ip6;
5888 	dst_p->bips_bad_udp += src_p->bips_bad_udp;
5889 	dst_p->bips_bad_tcp += src_p->bips_bad_tcp;
5890 }
5891 
5892 static void
5893 bridge_bripstats_apply(ifnet_t ifp, const struct bripstats *stats_p)
5894 {
5895 	struct bridge_iflist *bif;
5896 	struct bridge_softc *sc = ifp->if_bridge;
5897 
5898 	BRIDGE_LOCK(sc);
5899 	bif = bridge_lookup_member_if(sc, ifp);
5900 	if (bif == NULL) {
5901 		goto done;
5902 	}
5903 	if (!bif_has_checksum_offload(bif)) {
5904 		goto done;
5905 	}
5906 	bripstats_apply(&bif->bif_stats.brms_in_ip, stats_p);
5907 
5908 done:
5909 	BRIDGE_UNLOCK(sc);
5910 	return;
5911 }
5912 
5913 /*
5914  * bridge_input:
5915  *
5916  *	Filter input from a member interface.  Queue the packet for
5917  *	bridging if it is not for us.
5918  */
5919 errno_t
5920 bridge_input(struct ifnet *ifp, mbuf_t *data)
5921 {
5922 	struct bridge_softc *sc = ifp->if_bridge;
5923 	struct bridge_iflist *bif, *bif2;
5924 	struct ether_header eh_in;
5925 	bool is_ip = false;
5926 	bool is_ipv4 = false;
5927 	ifnet_t bridge_ifp;
5928 	struct mbuf *mc, *mc2;
5929 	unsigned int mac_hlen = sizeof(struct ether_header);
5930 	uint16_t vlan;
5931 	errno_t error;
5932 	ip_packet_info info;
5933 	struct bripstats stats;
5934 	bool info_initialized = false;
5935 	errno_t ip_packet_error = 0;
5936 	bool is_broadcast;
5937 	bool is_ip_broadcast = false;
5938 	bool is_ifp_mac = false;
5939 	mbuf_t m = *data;
5940 	uint32_t sc_filter_flags = 0;
5941 
5942 	bridge_ifp = sc->sc_ifp;
5943 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT,
5944 	    "%s from %s m 0x%llx data 0x%llx",
5945 	    bridge_ifp->if_xname, ifp->if_xname,
5946 	    (uint64_t)VM_KERNEL_ADDRPERM(m),
5947 	    (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m)));
5948 	if ((sc->sc_ifp->if_flags & IFF_RUNNING) == 0) {
5949 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT,
5950 		    "%s not running passing along",
5951 		    bridge_ifp->if_xname);
5952 		return 0;
5953 	}
5954 
5955 	vlan = VLANTAGOF(m);
5956 
5957 #ifdef IFF_MONITOR
5958 	/*
5959 	 * Implement support for bridge monitoring. If this flag has been
5960 	 * set on this interface, discard the packet once we push it through
5961 	 * the bpf(4) machinery, but before we do, increment the byte and
5962 	 * packet counters associated with this interface.
5963 	 */
5964 	if ((bridge_ifp->if_flags & IFF_MONITOR) != 0) {
5965 		m->m_pkthdr.rcvif = bridge_ifp;
5966 		BRIDGE_BPF_MTAP_INPUT(sc, m);
5967 		(void) ifnet_stat_increment_in(bridge_ifp, 1, m->m_pkthdr.len, 0);
5968 		*data = NULL;
5969 		m_freem(m);
5970 		return EJUSTRETURN;
5971 	}
5972 #endif /* IFF_MONITOR */
5973 
5974 	is_broadcast = (m->m_flags & (M_BCAST | M_MCAST)) != 0;
5975 
5976 	/*
5977 	 * Need to clear the promiscuous flag otherwise it will be
5978 	 * dropped by DLIL after processing filters
5979 	 */
5980 	if ((mbuf_flags(m) & MBUF_PROMISC)) {
5981 		mbuf_setflags_mask(m, 0, MBUF_PROMISC);
5982 	}
5983 
5984 	/* copy the ethernet header */
5985 	eh_in = *(mtod(m, struct ether_header *));
5986 
5987 	is_ip = ether_header_type_is_ip(&eh_in, &is_ipv4);
5988 
5989 	if (if_bridge_vmnet_pf_tagging != 0 && IFNET_IS_VMNET(ifp)) {
5990 		/* tag packets coming from VMNET interfaces */
5991 		ip_packet_error = bridge_vmnet_tag_input(bridge_ifp, ifp,
5992 		    eh_in.ether_dhost, data, is_broadcast, is_ip, is_ipv4,
5993 		    &info, &stats, &info_initialized);
5994 		m = *data;
5995 		if (m == NULL) {
5996 			bridge_bripstats_apply(ifp, &stats);
5997 			return EJUSTRETURN;
5998 		}
5999 	}
6000 
6001 	sc_filter_flags = sc->sc_filter_flags;
6002 	if (PF_IS_ENABLED && (sc_filter_flags & IFBF_FILT_MEMBER)) {
6003 		error = bridge_pf(data, ifp, sc_filter_flags, TRUE);
6004 		m = *data;
6005 		if (error != 0 || m == NULL) {
6006 			return EJUSTRETURN;
6007 		}
6008 	}
6009 
6010 	BRIDGE_LOCK(sc);
6011 	bif = bridge_lookup_member_if(sc, ifp);
6012 	if (bif == NULL) {
6013 		BRIDGE_UNLOCK(sc);
6014 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT,
6015 		    "%s bridge_lookup_member_if failed",
6016 		    bridge_ifp->if_xname);
6017 		return 0;
6018 	}
6019 	if (is_ip && bif_has_checksum_offload(bif)) {
6020 		if (info_initialized) {
6021 			bripstats_apply(&bif->bif_stats.brms_in_ip, &stats);
6022 		} else {
6023 			error = bridge_get_ip_proto(data, mac_hlen, is_ipv4,
6024 			    &info, &bif->bif_stats.brms_in_ip);
6025 			if (error != 0) {
6026 				BRIDGE_LOG(LOG_NOTICE, BR_DBGF_CHECKSUM,
6027 				    "%s(%s) bridge_get_ip_proto failed %d",
6028 				    bridge_ifp->if_xname,
6029 				    bif->bif_ifp->if_xname, error);
6030 				ip_packet_error = error;
6031 			}
6032 		}
6033 		if (ip_packet_error == 0) {
6034 			/* need to compute IP/UDP/TCP/checksums */
6035 			error = bridge_offload_checksum(data, &info,
6036 			    &bif->bif_stats);
6037 			if (error != 0) {
6038 				BRIDGE_LOG(LOG_NOTICE, BR_DBGF_CHECKSUM,
6039 				    "%s(%s) bridge_offload_checksum failed %d",
6040 				    bridge_ifp->if_xname,
6041 				    bif->bif_ifp->if_xname, error);
6042 				ip_packet_error = error;
6043 			}
6044 		}
6045 		if (ip_packet_error != 0) {
6046 			BRIDGE_UNLOCK(sc);
6047 			if (*data != NULL) {
6048 				m_freem(*data);
6049 				*data = NULL;
6050 			}
6051 			return EJUSTRETURN;
6052 		}
6053 		m = *data;
6054 	}
6055 
6056 	if (bif->bif_flags & BIFF_HOST_FILTER) {
6057 		error = bridge_host_filter(bif, data);
6058 		if (error != 0) {
6059 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT,
6060 			    "%s bridge_host_filter failed",
6061 			    bif->bif_ifp->if_xname);
6062 			BRIDGE_UNLOCK(sc);
6063 			return EJUSTRETURN;
6064 		}
6065 		m = *data;
6066 	}
6067 
6068 	if (!is_broadcast &&
6069 	    _ether_cmp(eh_in.ether_dhost, IF_LLADDR(ifp)) == 0) {
6070 		/* the packet is unicast to the interface's MAC address */
6071 		if (is_ip && sc->sc_mac_nat_bif == bif) {
6072 			/* doing MAC-NAT, check if destination is IP broadcast */
6073 			is_ip_broadcast = is_broadcast_ip_packet(data);
6074 			if (*data == NULL) {
6075 				BRIDGE_UNLOCK(sc);
6076 				return EJUSTRETURN;
6077 			}
6078 			m = *data;
6079 		}
6080 		if (!is_ip_broadcast) {
6081 			is_ifp_mac = TRUE;
6082 		}
6083 	}
6084 
6085 	bridge_span(sc, m);
6086 
6087 	if (is_broadcast || is_ip_broadcast) {
6088 		if (is_broadcast && (m->m_flags & M_MCAST) != 0) {
6089 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MCAST,
6090 			    " multicast: "
6091 			    "%02x:%02x:%02x:%02x:%02x:%02x",
6092 			    eh_in.ether_dhost[0], eh_in.ether_dhost[1],
6093 			    eh_in.ether_dhost[2], eh_in.ether_dhost[3],
6094 			    eh_in.ether_dhost[4], eh_in.ether_dhost[5]);
6095 		}
6096 		/* Tap off 802.1D packets; they do not get forwarded. */
6097 		if (is_broadcast &&
6098 		    _ether_cmp(eh_in.ether_dhost, bstp_etheraddr) == 0) {
6099 #if BRIDGESTP
6100 			m = bstp_input(&bif->bif_stp, ifp, m);
6101 #else /* !BRIDGESTP */
6102 			m_freem(m);
6103 			m = NULL;
6104 #endif /* !BRIDGESTP */
6105 			if (m == NULL) {
6106 				BRIDGE_UNLOCK(sc);
6107 				return EJUSTRETURN;
6108 			}
6109 		}
6110 
6111 		if ((bif->bif_ifflags & IFBIF_STP) &&
6112 		    bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
6113 			BRIDGE_UNLOCK(sc);
6114 			return 0;
6115 		}
6116 
6117 		/*
6118 		 * Make a deep copy of the packet and enqueue the copy
6119 		 * for bridge processing.
6120 		 */
6121 		mc = m_dup(m, M_DONTWAIT);
6122 		if (mc == NULL) {
6123 			BRIDGE_UNLOCK(sc);
6124 			return 0;
6125 		}
6126 
6127 		/*
6128 		 * Perform the bridge forwarding function with the copy.
6129 		 *
6130 		 * Note that bridge_forward calls BRIDGE_UNLOCK
6131 		 */
6132 		if (is_ip_broadcast) {
6133 			struct ether_header *eh;
6134 
6135 			/* make the copy look like it is actually broadcast */
6136 			mc->m_flags |= M_BCAST;
6137 			eh = mtod(mc, struct ether_header *);
6138 			bcopy(etherbroadcastaddr, eh->ether_dhost,
6139 			    ETHER_ADDR_LEN);
6140 		}
6141 		bridge_forward(sc, bif, mc);
6142 
6143 		/*
6144 		 * Reinject the mbuf as arriving on the bridge so we have a
6145 		 * chance at claiming multicast packets. We can not loop back
6146 		 * here from ether_input as a bridge is never a member of a
6147 		 * bridge.
6148 		 */
6149 		VERIFY(bridge_ifp->if_bridge == NULL);
6150 		mc2 = m_dup(m, M_DONTWAIT);
6151 		if (mc2 != NULL) {
6152 			/* Keep the layer3 header aligned */
6153 			int i = min(mc2->m_pkthdr.len, max_protohdr);
6154 			mc2 = m_copyup(mc2, i, ETHER_ALIGN);
6155 		}
6156 		if (mc2 != NULL) {
6157 			/* mark packet as arriving on the bridge */
6158 			mc2->m_pkthdr.rcvif = bridge_ifp;
6159 			mc2->m_pkthdr.pkt_hdr = mbuf_data(mc2);
6160 			BRIDGE_BPF_MTAP_INPUT(sc, mc2);
6161 			(void) mbuf_setdata(mc2,
6162 			    (char *)mbuf_data(mc2) + ETHER_HDR_LEN,
6163 			    mbuf_len(mc2) - ETHER_HDR_LEN);
6164 			(void) mbuf_pkthdr_adjustlen(mc2, -ETHER_HDR_LEN);
6165 			(void) ifnet_stat_increment_in(bridge_ifp, 1,
6166 			    mbuf_pkthdr_len(mc2), 0);
6167 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MCAST,
6168 			    "%s mcast for us", bridge_ifp->if_xname);
6169 			dlil_input_packet_list(bridge_ifp, mc2);
6170 		}
6171 
6172 		/* Return the original packet for local processing. */
6173 		return 0;
6174 	}
6175 
6176 	if ((bif->bif_ifflags & IFBIF_STP) &&
6177 	    bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
6178 		BRIDGE_UNLOCK(sc);
6179 		return 0;
6180 	}
6181 
6182 #ifdef DEV_CARP
6183 #define CARP_CHECK_WE_ARE_DST(iface) \
6184 	((iface)->if_carp &&\
6185 	        carp_forus((iface)->if_carp, eh_in.ether_dhost))
6186 #define CARP_CHECK_WE_ARE_SRC(iface) \
6187 	((iface)->if_carp &&\
6188 	        carp_forus((iface)->if_carp, eh_in.ether_shost))
6189 #else
6190 #define CARP_CHECK_WE_ARE_DST(iface) 0
6191 #define CARP_CHECK_WE_ARE_SRC(iface) 0
6192 #endif
6193 
6194 #define PFIL_HOOKED_INET6 PFIL_HOOKED(&inet6_pfil_hook)
6195 
6196 #define PFIL_PHYS(sc, ifp, m)
6197 
6198 #define GRAB_OUR_PACKETS(iface)                                         \
6199 	if ((iface)->if_type == IFT_GIF)                                \
6200 	        continue;                                               \
6201 	/* It is destined for us. */                                    \
6202 	if (_ether_cmp(IF_LLADDR((iface)), eh_in.ether_dhost) == 0 ||   \
6203 	    CARP_CHECK_WE_ARE_DST((iface))) {                           \
6204 	        if ((iface)->if_type == IFT_BRIDGE) {                   \
6205 	                BRIDGE_BPF_MTAP_INPUT(sc, m);                   \
6206 	/* Filter on the physical interface. */         \
6207 	                PFIL_PHYS(sc, iface, m);                        \
6208 	        } else {                                                \
6209 	                bpf_tap_in(iface, DLT_EN10MB, m, NULL, 0);      \
6210 	        }                                                       \
6211 	        if (bif->bif_ifflags & IFBIF_LEARNING) {                \
6212 	                error = bridge_rtupdate(sc, eh_in.ether_shost,  \
6213 	                    vlan, bif, 0, IFBAF_DYNAMIC);               \
6214 	                if (error && bif->bif_addrmax) {                \
6215 	                        BRIDGE_UNLOCK(sc);                      \
6216 	                        m_freem(m);                             \
6217 	                        return (EJUSTRETURN);                   \
6218 	                }                                               \
6219 	        }                                                       \
6220 	        BRIDGE_UNLOCK(sc);                                      \
6221 	        inject_input_packet(iface, m);                          \
6222 	        return (EJUSTRETURN);                                   \
6223 	}                                                               \
6224                                                                         \
6225 	/* We just received a packet that we sent out. */               \
6226 	if (_ether_cmp(IF_LLADDR((iface)), eh_in.ether_shost) == 0 ||   \
6227 	    CARP_CHECK_WE_ARE_SRC((iface))) {                           \
6228 	        BRIDGE_UNLOCK(sc);                                      \
6229 	        m_freem(m);                                             \
6230 	        return (EJUSTRETURN);                                   \
6231 	}
6232 
6233 	/*
6234 	 * Unicast.
6235 	 */
6236 
6237 	/* handle MAC-NAT if enabled */
6238 	if (is_ifp_mac && sc->sc_mac_nat_bif == bif) {
6239 		ifnet_t dst_if;
6240 		boolean_t is_input = FALSE;
6241 
6242 		dst_if = bridge_mac_nat_input(sc, data, &is_input);
6243 		m = *data;
6244 		if (dst_if == ifp) {
6245 			/* our input packet */
6246 		} else if (dst_if != NULL || m == NULL) {
6247 			BRIDGE_UNLOCK(sc);
6248 			if (dst_if != NULL) {
6249 				ASSERT(m != NULL);
6250 				if (is_input) {
6251 					inject_input_packet(dst_if, m);
6252 				} else {
6253 					(void)bridge_enqueue(bridge_ifp, NULL,
6254 					    dst_if, m,
6255 					    CHECKSUM_OPERATION_CLEAR_OFFLOAD);
6256 				}
6257 			}
6258 			return EJUSTRETURN;
6259 		}
6260 	}
6261 
6262 	/*
6263 	 * If the packet is for the bridge, pass it up for local processing.
6264 	 */
6265 	if (_ether_cmp(eh_in.ether_dhost, IF_LLADDR(bridge_ifp)) == 0 ||
6266 	    CARP_CHECK_WE_ARE_DST(bridge_ifp)) {
6267 		bpf_packet_func     bpf_input_func = sc->sc_bpf_input;
6268 
6269 		/*
6270 		 * If the interface is learning, and the source
6271 		 * address is valid and not multicast, record
6272 		 * the address.
6273 		 */
6274 		if (bif->bif_ifflags & IFBIF_LEARNING) {
6275 			(void) bridge_rtupdate(sc, eh_in.ether_shost,
6276 			    vlan, bif, 0, IFBAF_DYNAMIC);
6277 		}
6278 		BRIDGE_UNLOCK(sc);
6279 
6280 		bridge_interface_input(bridge_ifp, m, bpf_input_func);
6281 		return EJUSTRETURN;
6282 	}
6283 
6284 	/*
6285 	 * if the destination of the packet is for the MAC address of
6286 	 * the member interface itself, then we don't need to forward
6287 	 * it -- just pass it back.  Note that it'll likely just be
6288 	 * dropped by the stack, but if something else is bound to
6289 	 * the interface directly (for example, the wireless stats
6290 	 * protocol -- although that actually uses BPF right now),
6291 	 * then it will consume the packet
6292 	 *
6293 	 * ALSO, note that we do this check AFTER checking for the
6294 	 * bridge's own MAC address, because the bridge may be
6295 	 * using the SAME MAC address as one of its interfaces
6296 	 */
6297 	if (is_ifp_mac) {
6298 
6299 #ifdef VERY_VERY_VERY_DIAGNOSTIC
6300 		BRIDGE_LOG(LOG_NOTICE, 0,
6301 		    "not forwarding packet bound for member interface");
6302 #endif
6303 
6304 		BRIDGE_UNLOCK(sc);
6305 		return 0;
6306 	}
6307 
6308 	/* Now check the remaining bridge members. */
6309 	TAILQ_FOREACH(bif2, &sc->sc_iflist, bif_next) {
6310 		if (bif2->bif_ifp != ifp) {
6311 			GRAB_OUR_PACKETS(bif2->bif_ifp);
6312 		}
6313 	}
6314 
6315 #undef CARP_CHECK_WE_ARE_DST
6316 #undef CARP_CHECK_WE_ARE_SRC
6317 #undef GRAB_OUR_PACKETS
6318 
6319 	/*
6320 	 * Perform the bridge forwarding function.
6321 	 *
6322 	 * Note that bridge_forward calls BRIDGE_UNLOCK
6323 	 */
6324 	bridge_forward(sc, bif, m);
6325 
6326 	return EJUSTRETURN;
6327 }
6328 
6329 /*
6330  * bridge_broadcast:
6331  *
6332  *	Send a frame to all interfaces that are members of
6333  *	the bridge, except for the one on which the packet
6334  *	arrived.
6335  *
6336  *	NOTE: Releases the lock on return.
6337  */
6338 static void
6339 bridge_broadcast(struct bridge_softc *sc, struct bridge_iflist * sbif,
6340     struct mbuf *m, int runfilt)
6341 {
6342 	ifnet_t bridge_ifp;
6343 	struct bridge_iflist *dbif;
6344 	struct ifnet * src_if;
6345 	struct mbuf *mc;
6346 	struct mbuf *mc_in;
6347 	struct ifnet *dst_if;
6348 	int error = 0, used = 0;
6349 	boolean_t bridge_if_out;
6350 	ChecksumOperation cksum_op;
6351 	struct mac_nat_record mnr;
6352 	struct bridge_iflist *mac_nat_bif = sc->sc_mac_nat_bif;
6353 	boolean_t translate_mac = FALSE;
6354 	uint32_t sc_filter_flags = 0;
6355 
6356 	bridge_ifp = sc->sc_ifp;
6357 	if (sbif != NULL) {
6358 		bridge_if_out = FALSE;
6359 		src_if = sbif->bif_ifp;
6360 		cksum_op = CHECKSUM_OPERATION_CLEAR_OFFLOAD;
6361 		if (mac_nat_bif != NULL && sbif != mac_nat_bif) {
6362 			/* get the translation record while holding the lock */
6363 			translate_mac
6364 			        = bridge_mac_nat_output(sc, sbif, &m, &mnr);
6365 			if (m == NULL) {
6366 				/* packet was deallocated */
6367 				BRIDGE_UNLOCK(sc);
6368 				return;
6369 			}
6370 		}
6371 	} else {
6372 		/*
6373 		 * sbif is NULL when the bridge interface calls
6374 		 * bridge_broadcast().
6375 		 */
6376 		bridge_if_out = TRUE;
6377 		cksum_op = CHECKSUM_OPERATION_FINALIZE;
6378 		sbif = NULL;
6379 		src_if = NULL;
6380 	}
6381 
6382 	BRIDGE_LOCK2REF(sc, error);
6383 	if (error) {
6384 		m_freem(m);
6385 		return;
6386 	}
6387 
6388 	TAILQ_FOREACH(dbif, &sc->sc_iflist, bif_next) {
6389 		dst_if = dbif->bif_ifp;
6390 		if (dst_if == src_if) {
6391 			/* skip the interface that the packet came in on */
6392 			continue;
6393 		}
6394 
6395 		/* Private segments can not talk to each other */
6396 		if (sbif != NULL &&
6397 		    (sbif->bif_ifflags & dbif->bif_ifflags & IFBIF_PRIVATE)) {
6398 			continue;
6399 		}
6400 
6401 		if ((dbif->bif_ifflags & IFBIF_STP) &&
6402 		    dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
6403 			continue;
6404 		}
6405 
6406 		if ((dbif->bif_ifflags & IFBIF_DISCOVER) == 0 &&
6407 		    (m->m_flags & (M_BCAST | M_MCAST)) == 0) {
6408 			continue;
6409 		}
6410 
6411 		if ((dst_if->if_flags & IFF_RUNNING) == 0) {
6412 			continue;
6413 		}
6414 
6415 		if (!(dbif->bif_flags & BIFF_MEDIA_ACTIVE)) {
6416 			continue;
6417 		}
6418 
6419 		if (TAILQ_NEXT(dbif, bif_next) == NULL) {
6420 			mc = m;
6421 			used = 1;
6422 		} else {
6423 			mc = m_dup(m, M_DONTWAIT);
6424 			if (mc == NULL) {
6425 				(void) ifnet_stat_increment_out(bridge_ifp,
6426 				    0, 0, 1);
6427 				continue;
6428 			}
6429 		}
6430 
6431 		/*
6432 		 * If broadcast input is enabled, do so only if this
6433 		 * is an input packet.
6434 		 */
6435 		if (!bridge_if_out &&
6436 		    (dbif->bif_flags & BIFF_INPUT_BROADCAST) != 0) {
6437 			mc_in = m_dup(mc, M_DONTWAIT);
6438 			/* this could fail, but we continue anyways */
6439 		} else {
6440 			mc_in = NULL;
6441 		}
6442 
6443 		/* out */
6444 		if (translate_mac && mac_nat_bif == dbif) {
6445 			/* translate the packet without holding the lock */
6446 			bridge_mac_nat_translate(&mc, &mnr, IF_LLADDR(dst_if));
6447 		}
6448 
6449 		sc_filter_flags = sc->sc_filter_flags;
6450 		if (runfilt &&
6451 		    PF_IS_ENABLED && (sc_filter_flags & IFBF_FILT_MEMBER)) {
6452 			if (used == 0) {
6453 				/* Keep the layer3 header aligned */
6454 				int i = min(mc->m_pkthdr.len, max_protohdr);
6455 				mc = m_copyup(mc, i, ETHER_ALIGN);
6456 				if (mc == NULL) {
6457 					(void) ifnet_stat_increment_out(
6458 						sc->sc_ifp, 0, 0, 1);
6459 					if (mc_in != NULL) {
6460 						m_freem(mc_in);
6461 						mc_in = NULL;
6462 					}
6463 					continue;
6464 				}
6465 			}
6466 			if (bridge_pf(&mc, dst_if, sc_filter_flags, FALSE) != 0) {
6467 				if (mc_in != NULL) {
6468 					m_freem(mc_in);
6469 					mc_in = NULL;
6470 				}
6471 				continue;
6472 			}
6473 			if (mc == NULL) {
6474 				if (mc_in != NULL) {
6475 					m_freem(mc_in);
6476 					mc_in = NULL;
6477 				}
6478 				continue;
6479 			}
6480 		}
6481 
6482 		if (mc != NULL) {
6483 			/* verify checksum if necessary */
6484 			if (bif_has_checksum_offload(dbif) && sbif != NULL &&
6485 			    !bif_has_checksum_offload(sbif)) {
6486 				error = bridge_verify_checksum(&mc,
6487 				    &dbif->bif_stats);
6488 				if (error != 0) {
6489 					if (mc != NULL) {
6490 						m_freem(mc);
6491 					}
6492 					mc = NULL;
6493 				}
6494 			}
6495 			if (mc != NULL) {
6496 				(void) bridge_enqueue(bridge_ifp,
6497 				    NULL, dst_if, mc, cksum_op);
6498 			}
6499 		}
6500 
6501 		/* in */
6502 		if (mc_in == NULL) {
6503 			continue;
6504 		}
6505 		bpf_tap_in(dst_if, DLT_EN10MB, mc_in, NULL, 0);
6506 		mbuf_pkthdr_setrcvif(mc_in, dst_if);
6507 		mbuf_pkthdr_setheader(mc_in, mbuf_data(mc_in));
6508 		mbuf_setdata(mc_in, (char *)mbuf_data(mc_in) + ETHER_HDR_LEN,
6509 		    mbuf_len(mc_in) - ETHER_HDR_LEN);
6510 		mbuf_pkthdr_adjustlen(mc_in, -ETHER_HDR_LEN);
6511 		mc_in->m_flags |= M_PROTO1; /* set to avoid loops */
6512 		dlil_input_packet_list(dst_if, mc_in);
6513 	}
6514 	if (used == 0) {
6515 		m_freem(m);
6516 	}
6517 
6518 
6519 	BRIDGE_UNREF(sc);
6520 }
6521 
6522 /*
6523  * bridge_span:
6524  *
6525  *	Duplicate a packet out one or more interfaces that are in span mode,
6526  *	the original mbuf is unmodified.
6527  */
6528 static void
6529 bridge_span(struct bridge_softc *sc, struct mbuf *m)
6530 {
6531 	struct bridge_iflist *bif;
6532 	struct ifnet *dst_if;
6533 	struct mbuf *mc;
6534 
6535 	if (TAILQ_EMPTY(&sc->sc_spanlist)) {
6536 		return;
6537 	}
6538 
6539 	TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) {
6540 		dst_if = bif->bif_ifp;
6541 
6542 		if ((dst_if->if_flags & IFF_RUNNING) == 0) {
6543 			continue;
6544 		}
6545 
6546 		mc = m_copypacket(m, M_DONTWAIT);
6547 		if (mc == NULL) {
6548 			(void) ifnet_stat_increment_out(sc->sc_ifp, 0, 0, 1);
6549 			continue;
6550 		}
6551 
6552 		(void) bridge_enqueue(sc->sc_ifp, NULL, dst_if, mc,
6553 		    CHECKSUM_OPERATION_NONE);
6554 	}
6555 }
6556 
6557 
6558 /*
6559  * bridge_rtupdate:
6560  *
6561  *	Add a bridge routing entry.
6562  */
6563 static int
6564 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst, uint16_t vlan,
6565     struct bridge_iflist *bif, int setflags, uint8_t flags)
6566 {
6567 	struct bridge_rtnode *brt;
6568 	int error;
6569 
6570 	BRIDGE_LOCK_ASSERT_HELD(sc);
6571 
6572 	/* Check the source address is valid and not multicast. */
6573 	if (ETHER_IS_MULTICAST(dst) ||
6574 	    (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
6575 	    dst[3] == 0 && dst[4] == 0 && dst[5] == 0) != 0) {
6576 		return EINVAL;
6577 	}
6578 
6579 
6580 	/* 802.1p frames map to vlan 1 */
6581 	if (vlan == 0) {
6582 		vlan = 1;
6583 	}
6584 
6585 	/*
6586 	 * A route for this destination might already exist.  If so,
6587 	 * update it, otherwise create a new one.
6588 	 */
6589 	if ((brt = bridge_rtnode_lookup(sc, dst, vlan)) == NULL) {
6590 		if (sc->sc_brtcnt >= sc->sc_brtmax) {
6591 			sc->sc_brtexceeded++;
6592 			return ENOSPC;
6593 		}
6594 		/* Check per interface address limits (if enabled) */
6595 		if (bif->bif_addrmax && bif->bif_addrcnt >= bif->bif_addrmax) {
6596 			bif->bif_addrexceeded++;
6597 			return ENOSPC;
6598 		}
6599 
6600 		/*
6601 		 * Allocate a new bridge forwarding node, and
6602 		 * initialize the expiration time and Ethernet
6603 		 * address.
6604 		 */
6605 		brt = zalloc_noblock(bridge_rtnode_pool);
6606 		if (brt == NULL) {
6607 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_RT_TABLE,
6608 			    "zalloc_nolock failed");
6609 			return ENOMEM;
6610 		}
6611 		bzero(brt, sizeof(struct bridge_rtnode));
6612 
6613 		if (bif->bif_ifflags & IFBIF_STICKY) {
6614 			brt->brt_flags = IFBAF_STICKY;
6615 		} else {
6616 			brt->brt_flags = IFBAF_DYNAMIC;
6617 		}
6618 
6619 		memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
6620 		brt->brt_vlan = vlan;
6621 
6622 
6623 		if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
6624 			zfree(bridge_rtnode_pool, brt);
6625 			return error;
6626 		}
6627 		brt->brt_dst = bif;
6628 		bif->bif_addrcnt++;
6629 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_RT_TABLE,
6630 		    "added %02x:%02x:%02x:%02x:%02x:%02x "
6631 		    "on %s count %u hashsize %u",
6632 		    dst[0], dst[1], dst[2], dst[3], dst[4], dst[5],
6633 		    sc->sc_ifp->if_xname, sc->sc_brtcnt,
6634 		    sc->sc_rthash_size);
6635 	}
6636 
6637 	if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
6638 	    brt->brt_dst != bif) {
6639 		brt->brt_dst->bif_addrcnt--;
6640 		brt->brt_dst = bif;
6641 		brt->brt_dst->bif_addrcnt++;
6642 	}
6643 
6644 	if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
6645 		unsigned long now;
6646 
6647 		now = (unsigned long) net_uptime();
6648 		brt->brt_expire = now + sc->sc_brttimeout;
6649 	}
6650 	if (setflags) {
6651 		brt->brt_flags = flags;
6652 	}
6653 
6654 
6655 	return 0;
6656 }
6657 
6658 /*
6659  * bridge_rtlookup:
6660  *
6661  *	Lookup the destination interface for an address.
6662  */
6663 static struct ifnet *
6664 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
6665 {
6666 	struct bridge_rtnode *brt;
6667 
6668 	BRIDGE_LOCK_ASSERT_HELD(sc);
6669 
6670 	if ((brt = bridge_rtnode_lookup(sc, addr, vlan)) == NULL) {
6671 		return NULL;
6672 	}
6673 
6674 	return brt->brt_ifp;
6675 }
6676 
6677 /*
6678  * bridge_rttrim:
6679  *
6680  *	Trim the routine table so that we have a number
6681  *	of routing entries less than or equal to the
6682  *	maximum number.
6683  */
6684 static void
6685 bridge_rttrim(struct bridge_softc *sc)
6686 {
6687 	struct bridge_rtnode *brt, *nbrt;
6688 
6689 	BRIDGE_LOCK_ASSERT_HELD(sc);
6690 
6691 	/* Make sure we actually need to do this. */
6692 	if (sc->sc_brtcnt <= sc->sc_brtmax) {
6693 		return;
6694 	}
6695 
6696 	/* Force an aging cycle; this might trim enough addresses. */
6697 	bridge_rtage(sc);
6698 	if (sc->sc_brtcnt <= sc->sc_brtmax) {
6699 		return;
6700 	}
6701 
6702 	LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
6703 		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
6704 			bridge_rtnode_destroy(sc, brt);
6705 			if (sc->sc_brtcnt <= sc->sc_brtmax) {
6706 				return;
6707 			}
6708 		}
6709 	}
6710 }
6711 
6712 /*
6713  * bridge_aging_timer:
6714  *
6715  *	Aging periodic timer for the bridge routing table.
6716  */
6717 static void
6718 bridge_aging_timer(struct bridge_softc *sc)
6719 {
6720 	BRIDGE_LOCK_ASSERT_HELD(sc);
6721 
6722 	bridge_rtage(sc);
6723 	if ((sc->sc_ifp->if_flags & IFF_RUNNING) &&
6724 	    (sc->sc_flags & SCF_DETACHING) == 0) {
6725 		sc->sc_aging_timer.bdc_sc = sc;
6726 		sc->sc_aging_timer.bdc_func = bridge_aging_timer;
6727 		sc->sc_aging_timer.bdc_ts.tv_sec = bridge_rtable_prune_period;
6728 		bridge_schedule_delayed_call(&sc->sc_aging_timer);
6729 	}
6730 }
6731 
6732 /*
6733  * bridge_rtage:
6734  *
6735  *	Perform an aging cycle.
6736  */
6737 static void
6738 bridge_rtage(struct bridge_softc *sc)
6739 {
6740 	struct bridge_rtnode *brt, *nbrt;
6741 	unsigned long now;
6742 
6743 	BRIDGE_LOCK_ASSERT_HELD(sc);
6744 
6745 	now = (unsigned long) net_uptime();
6746 
6747 	LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
6748 		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
6749 			if (now >= brt->brt_expire) {
6750 				bridge_rtnode_destroy(sc, brt);
6751 			}
6752 		}
6753 	}
6754 	if (sc->sc_mac_nat_bif != NULL) {
6755 		bridge_mac_nat_age_entries(sc, now);
6756 	}
6757 }
6758 
6759 /*
6760  * bridge_rtflush:
6761  *
6762  *	Remove all dynamic addresses from the bridge.
6763  */
6764 static void
6765 bridge_rtflush(struct bridge_softc *sc, int full)
6766 {
6767 	struct bridge_rtnode *brt, *nbrt;
6768 
6769 	BRIDGE_LOCK_ASSERT_HELD(sc);
6770 
6771 	LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
6772 		if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
6773 			bridge_rtnode_destroy(sc, brt);
6774 		}
6775 	}
6776 }
6777 
6778 /*
6779  * bridge_rtdaddr:
6780  *
6781  *	Remove an address from the table.
6782  */
6783 static int
6784 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
6785 {
6786 	struct bridge_rtnode *brt;
6787 	int found = 0;
6788 
6789 	BRIDGE_LOCK_ASSERT_HELD(sc);
6790 
6791 	/*
6792 	 * If vlan is zero then we want to delete for all vlans so the lookup
6793 	 * may return more than one.
6794 	 */
6795 	while ((brt = bridge_rtnode_lookup(sc, addr, vlan)) != NULL) {
6796 		bridge_rtnode_destroy(sc, brt);
6797 		found = 1;
6798 	}
6799 
6800 	return found ? 0 : ENOENT;
6801 }
6802 
6803 /*
6804  * bridge_rtdelete:
6805  *
6806  *	Delete routes to a specific member interface.
6807  */
6808 static void
6809 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
6810 {
6811 	struct bridge_rtnode *brt, *nbrt;
6812 
6813 	BRIDGE_LOCK_ASSERT_HELD(sc);
6814 
6815 	LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
6816 		if (brt->brt_ifp == ifp && (full ||
6817 		    (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)) {
6818 			bridge_rtnode_destroy(sc, brt);
6819 		}
6820 	}
6821 }
6822 
6823 /*
6824  * bridge_rtable_init:
6825  *
6826  *	Initialize the route table for this bridge.
6827  */
6828 static int
6829 bridge_rtable_init(struct bridge_softc *sc)
6830 {
6831 	u_int32_t i;
6832 
6833 	sc->sc_rthash = kalloc_type(struct _bridge_rtnode_list,
6834 	    BRIDGE_RTHASH_SIZE, Z_WAITOK_ZERO_NOFAIL);
6835 	sc->sc_rthash_size = BRIDGE_RTHASH_SIZE;
6836 
6837 	for (i = 0; i < sc->sc_rthash_size; i++) {
6838 		LIST_INIT(&sc->sc_rthash[i]);
6839 	}
6840 
6841 	sc->sc_rthash_key = RandomULong();
6842 
6843 	LIST_INIT(&sc->sc_rtlist);
6844 
6845 	return 0;
6846 }
6847 
6848 /*
6849  * bridge_rthash_delayed_resize:
6850  *
6851  *	Resize the routing table hash on a delayed thread call.
6852  */
6853 static void
6854 bridge_rthash_delayed_resize(struct bridge_softc *sc)
6855 {
6856 	u_int32_t new_rthash_size = 0;
6857 	u_int32_t old_rthash_size = 0;
6858 	struct _bridge_rtnode_list *new_rthash = NULL;
6859 	struct _bridge_rtnode_list *old_rthash = NULL;
6860 	u_int32_t i;
6861 	struct bridge_rtnode *brt;
6862 	int error = 0;
6863 
6864 	BRIDGE_LOCK_ASSERT_HELD(sc);
6865 
6866 	/*
6867 	 * Four entries per hash bucket is our ideal load factor
6868 	 */
6869 	if (sc->sc_brtcnt < sc->sc_rthash_size * 4) {
6870 		goto out;
6871 	}
6872 
6873 	/*
6874 	 * Doubling the number of hash buckets may be too simplistic
6875 	 * especially when facing a spike of new entries
6876 	 */
6877 	new_rthash_size = sc->sc_rthash_size * 2;
6878 
6879 	sc->sc_flags |= SCF_RESIZING;
6880 	BRIDGE_UNLOCK(sc);
6881 
6882 	new_rthash = kalloc_type(struct _bridge_rtnode_list, new_rthash_size,
6883 	    Z_WAITOK | Z_ZERO);
6884 
6885 	BRIDGE_LOCK(sc);
6886 	sc->sc_flags &= ~SCF_RESIZING;
6887 
6888 	if (new_rthash == NULL) {
6889 		error = ENOMEM;
6890 		goto out;
6891 	}
6892 	if ((sc->sc_flags & SCF_DETACHING)) {
6893 		error = ENODEV;
6894 		goto out;
6895 	}
6896 	/*
6897 	 * Fail safe from here on
6898 	 */
6899 	old_rthash = sc->sc_rthash;
6900 	old_rthash_size = sc->sc_rthash_size;
6901 	sc->sc_rthash = new_rthash;
6902 	sc->sc_rthash_size = new_rthash_size;
6903 
6904 	/*
6905 	 * Get a new key to force entries to be shuffled around to reduce
6906 	 * the likelihood they will land in the same buckets
6907 	 */
6908 	sc->sc_rthash_key = RandomULong();
6909 
6910 	for (i = 0; i < sc->sc_rthash_size; i++) {
6911 		LIST_INIT(&sc->sc_rthash[i]);
6912 	}
6913 
6914 	LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
6915 		LIST_REMOVE(brt, brt_hash);
6916 		(void) bridge_rtnode_hash(sc, brt);
6917 	}
6918 out:
6919 	if (error == 0) {
6920 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_RT_TABLE,
6921 		    "%s new size %u",
6922 		    sc->sc_ifp->if_xname, sc->sc_rthash_size);
6923 		kfree_type(struct _bridge_rtnode_list, old_rthash_size, old_rthash);
6924 	} else {
6925 		BRIDGE_LOG(LOG_NOTICE, BR_DBGF_RT_TABLE,
6926 		    "%s failed %d", sc->sc_ifp->if_xname, error);
6927 		kfree_type(struct _bridge_rtnode_list, new_rthash_size, new_rthash);
6928 	}
6929 }
6930 
6931 /*
6932  * Resize the number of hash buckets based on the load factor
6933  * Currently only grow
6934  * Failing to resize the hash table is not fatal
6935  */
6936 static void
6937 bridge_rthash_resize(struct bridge_softc *sc)
6938 {
6939 	BRIDGE_LOCK_ASSERT_HELD(sc);
6940 
6941 	if ((sc->sc_flags & SCF_DETACHING) || (sc->sc_flags & SCF_RESIZING)) {
6942 		return;
6943 	}
6944 
6945 	/*
6946 	 * Four entries per hash bucket is our ideal load factor
6947 	 */
6948 	if (sc->sc_brtcnt < sc->sc_rthash_size * 4) {
6949 		return;
6950 	}
6951 	/*
6952 	 * Hard limit on the size of the routing hash table
6953 	 */
6954 	if (sc->sc_rthash_size >= bridge_rtable_hash_size_max) {
6955 		return;
6956 	}
6957 
6958 	sc->sc_resize_call.bdc_sc = sc;
6959 	sc->sc_resize_call.bdc_func = bridge_rthash_delayed_resize;
6960 	bridge_schedule_delayed_call(&sc->sc_resize_call);
6961 }
6962 
6963 /*
6964  * bridge_rtable_fini:
6965  *
6966  *	Deconstruct the route table for this bridge.
6967  */
6968 static void
6969 bridge_rtable_fini(struct bridge_softc *sc)
6970 {
6971 	KASSERT(sc->sc_brtcnt == 0,
6972 	    ("%s: %d bridge routes referenced", __func__, sc->sc_brtcnt));
6973 	kfree_type(struct _bridge_rtnode_list, sc->sc_rthash_size,
6974 	    sc->sc_rthash);
6975 	sc->sc_rthash = NULL;
6976 	sc->sc_rthash_size = 0;
6977 }
6978 
6979 /*
6980  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
6981  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
6982  */
6983 #define mix(a, b, c)                                                    \
6984 do {                                                                    \
6985 	a -= b; a -= c; a ^= (c >> 13);                                 \
6986 	b -= c; b -= a; b ^= (a << 8);                                  \
6987 	c -= a; c -= b; c ^= (b >> 13);                                 \
6988 	a -= b; a -= c; a ^= (c >> 12);                                 \
6989 	b -= c; b -= a; b ^= (a << 16);                                 \
6990 	c -= a; c -= b; c ^= (b >> 5);                                  \
6991 	a -= b; a -= c; a ^= (c >> 3);                                  \
6992 	b -= c; b -= a; b ^= (a << 10);                                 \
6993 	c -= a; c -= b; c ^= (b >> 15);                                 \
6994 } while ( /*CONSTCOND*/ 0)
6995 
6996 static __inline uint32_t
6997 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
6998 {
6999 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
7000 
7001 	b += addr[5] << 8;
7002 	b += addr[4];
7003 	a += addr[3] << 24;
7004 	a += addr[2] << 16;
7005 	a += addr[1] << 8;
7006 	a += addr[0];
7007 
7008 	mix(a, b, c);
7009 
7010 	return c & BRIDGE_RTHASH_MASK(sc);
7011 }
7012 
7013 #undef mix
7014 
7015 static int
7016 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
7017 {
7018 	int i, d;
7019 
7020 	for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
7021 		d = ((int)a[i]) - ((int)b[i]);
7022 	}
7023 
7024 	return d;
7025 }
7026 
7027 /*
7028  * bridge_rtnode_lookup:
7029  *
7030  *	Look up a bridge route node for the specified destination. Compare the
7031  *	vlan id or if zero then just return the first match.
7032  */
7033 static struct bridge_rtnode *
7034 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr,
7035     uint16_t vlan)
7036 {
7037 	struct bridge_rtnode *brt;
7038 	uint32_t hash;
7039 	int dir;
7040 
7041 	BRIDGE_LOCK_ASSERT_HELD(sc);
7042 
7043 	hash = bridge_rthash(sc, addr);
7044 	LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
7045 		dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
7046 		if (dir == 0 && (brt->brt_vlan == vlan || vlan == 0)) {
7047 			return brt;
7048 		}
7049 		if (dir > 0) {
7050 			return NULL;
7051 		}
7052 	}
7053 
7054 	return NULL;
7055 }
7056 
7057 /*
7058  * bridge_rtnode_hash:
7059  *
7060  *	Insert the specified bridge node into the route hash table.
7061  *	This is used when adding a new node or to rehash when resizing
7062  *	the hash table
7063  */
7064 static int
7065 bridge_rtnode_hash(struct bridge_softc *sc, struct bridge_rtnode *brt)
7066 {
7067 	struct bridge_rtnode *lbrt;
7068 	uint32_t hash;
7069 	int dir;
7070 
7071 	BRIDGE_LOCK_ASSERT_HELD(sc);
7072 
7073 	hash = bridge_rthash(sc, brt->brt_addr);
7074 
7075 	lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
7076 	if (lbrt == NULL) {
7077 		LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
7078 		goto out;
7079 	}
7080 
7081 	do {
7082 		dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
7083 		if (dir == 0 && brt->brt_vlan == lbrt->brt_vlan) {
7084 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_RT_TABLE,
7085 			    "%s EEXIST %02x:%02x:%02x:%02x:%02x:%02x",
7086 			    sc->sc_ifp->if_xname,
7087 			    brt->brt_addr[0], brt->brt_addr[1],
7088 			    brt->brt_addr[2], brt->brt_addr[3],
7089 			    brt->brt_addr[4], brt->brt_addr[5]);
7090 			return EEXIST;
7091 		}
7092 		if (dir > 0) {
7093 			LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
7094 			goto out;
7095 		}
7096 		if (LIST_NEXT(lbrt, brt_hash) == NULL) {
7097 			LIST_INSERT_AFTER(lbrt, brt, brt_hash);
7098 			goto out;
7099 		}
7100 		lbrt = LIST_NEXT(lbrt, brt_hash);
7101 	} while (lbrt != NULL);
7102 
7103 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_RT_TABLE,
7104 	    "%s impossible %02x:%02x:%02x:%02x:%02x:%02x",
7105 	    sc->sc_ifp->if_xname,
7106 	    brt->brt_addr[0], brt->brt_addr[1], brt->brt_addr[2],
7107 	    brt->brt_addr[3], brt->brt_addr[4], brt->brt_addr[5]);
7108 out:
7109 	return 0;
7110 }
7111 
7112 /*
7113  * bridge_rtnode_insert:
7114  *
7115  *	Insert the specified bridge node into the route table.  We
7116  *	assume the entry is not already in the table.
7117  */
7118 static int
7119 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
7120 {
7121 	int error;
7122 
7123 	error = bridge_rtnode_hash(sc, brt);
7124 	if (error != 0) {
7125 		return error;
7126 	}
7127 
7128 	LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
7129 	sc->sc_brtcnt++;
7130 
7131 	bridge_rthash_resize(sc);
7132 
7133 	return 0;
7134 }
7135 
7136 /*
7137  * bridge_rtnode_destroy:
7138  *
7139  *	Destroy a bridge rtnode.
7140  */
7141 static void
7142 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
7143 {
7144 	BRIDGE_LOCK_ASSERT_HELD(sc);
7145 
7146 	LIST_REMOVE(brt, brt_hash);
7147 
7148 	LIST_REMOVE(brt, brt_list);
7149 	sc->sc_brtcnt--;
7150 	brt->brt_dst->bif_addrcnt--;
7151 	zfree(bridge_rtnode_pool, brt);
7152 }
7153 
7154 #if BRIDGESTP
7155 /*
7156  * bridge_rtable_expire:
7157  *
7158  *	Set the expiry time for all routes on an interface.
7159  */
7160 static void
7161 bridge_rtable_expire(struct ifnet *ifp, int age)
7162 {
7163 	struct bridge_softc *sc = ifp->if_bridge;
7164 	struct bridge_rtnode *brt;
7165 
7166 	BRIDGE_LOCK(sc);
7167 
7168 	/*
7169 	 * If the age is zero then flush, otherwise set all the expiry times to
7170 	 * age for the interface
7171 	 */
7172 	if (age == 0) {
7173 		bridge_rtdelete(sc, ifp, IFBF_FLUSHDYN);
7174 	} else {
7175 		unsigned long now;
7176 
7177 		now = (unsigned long) net_uptime();
7178 
7179 		LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
7180 			/* Cap the expiry time to 'age' */
7181 			if (brt->brt_ifp == ifp &&
7182 			    brt->brt_expire > now + age &&
7183 			    (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
7184 				brt->brt_expire = now + age;
7185 			}
7186 		}
7187 	}
7188 	BRIDGE_UNLOCK(sc);
7189 }
7190 
7191 /*
7192  * bridge_state_change:
7193  *
7194  *	Callback from the bridgestp code when a port changes states.
7195  */
7196 static void
7197 bridge_state_change(struct ifnet *ifp, int state)
7198 {
7199 	struct bridge_softc *sc = ifp->if_bridge;
7200 	static const char *stpstates[] = {
7201 		"disabled",
7202 		"listening",
7203 		"learning",
7204 		"forwarding",
7205 		"blocking",
7206 		"discarding"
7207 	};
7208 
7209 	if (log_stp) {
7210 		log(LOG_NOTICE, "%s: state changed to %s on %s",
7211 		    sc->sc_ifp->if_xname,
7212 		    stpstates[state], ifp->if_xname);
7213 	}
7214 }
7215 #endif /* BRIDGESTP */
7216 
7217 /*
7218  * bridge_set_bpf_tap:
7219  *
7220  *	Sets ups the BPF callbacks.
7221  */
7222 static errno_t
7223 bridge_set_bpf_tap(ifnet_t ifp, bpf_tap_mode mode, bpf_packet_func bpf_callback)
7224 {
7225 	struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp);
7226 
7227 	/* TBD locking */
7228 	if (sc == NULL || (sc->sc_flags & SCF_DETACHING)) {
7229 		return ENODEV;
7230 	}
7231 	switch (mode) {
7232 	case BPF_TAP_DISABLE:
7233 		sc->sc_bpf_input = sc->sc_bpf_output = NULL;
7234 		break;
7235 
7236 	case BPF_TAP_INPUT:
7237 		sc->sc_bpf_input = bpf_callback;
7238 		break;
7239 
7240 	case BPF_TAP_OUTPUT:
7241 		sc->sc_bpf_output = bpf_callback;
7242 		break;
7243 
7244 	case BPF_TAP_INPUT_OUTPUT:
7245 		sc->sc_bpf_input = sc->sc_bpf_output = bpf_callback;
7246 		break;
7247 
7248 	default:
7249 		break;
7250 	}
7251 
7252 	return 0;
7253 }
7254 
7255 /*
7256  * bridge_detach:
7257  *
7258  *	Callback when interface has been detached.
7259  */
7260 static void
7261 bridge_detach(ifnet_t ifp)
7262 {
7263 	struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp);
7264 
7265 #if BRIDGESTP
7266 	bstp_detach(&sc->sc_stp);
7267 #endif /* BRIDGESTP */
7268 
7269 	/* Tear down the routing table. */
7270 	bridge_rtable_fini(sc);
7271 
7272 	lck_mtx_lock(&bridge_list_mtx);
7273 	LIST_REMOVE(sc, sc_list);
7274 	lck_mtx_unlock(&bridge_list_mtx);
7275 
7276 	ifnet_release(ifp);
7277 
7278 	lck_mtx_destroy(&sc->sc_mtx, &bridge_lock_grp);
7279 	kfree_type(struct bridge_softc, sc);
7280 }
7281 
7282 /*
7283  * bridge_bpf_input:
7284  *
7285  *	Invoke the input BPF callback if enabled
7286  */
7287 static errno_t
7288 bridge_bpf_input(ifnet_t ifp, struct mbuf *m, const char * func, int line)
7289 {
7290 	struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp);
7291 	bpf_packet_func     input_func = sc->sc_bpf_input;
7292 
7293 	if (input_func != NULL) {
7294 		if (mbuf_pkthdr_rcvif(m) != ifp) {
7295 			BRIDGE_LOG(LOG_NOTICE, 0,
7296 			    "%s.%d: rcvif: 0x%llx != ifp 0x%llx", func, line,
7297 			    (uint64_t)VM_KERNEL_ADDRPERM(mbuf_pkthdr_rcvif(m)),
7298 			    (uint64_t)VM_KERNEL_ADDRPERM(ifp));
7299 		}
7300 		(*input_func)(ifp, m);
7301 	}
7302 	return 0;
7303 }
7304 
7305 /*
7306  * bridge_bpf_output:
7307  *
7308  *	Invoke the output BPF callback if enabled
7309  */
7310 static errno_t
7311 bridge_bpf_output(ifnet_t ifp, struct mbuf *m)
7312 {
7313 	struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp);
7314 	bpf_packet_func     output_func = sc->sc_bpf_output;
7315 
7316 	if (output_func != NULL) {
7317 		(*output_func)(ifp, m);
7318 	}
7319 	return 0;
7320 }
7321 
7322 /*
7323  * bridge_link_event:
7324  *
7325  *	Report a data link event on an interface
7326  */
7327 static void
7328 bridge_link_event(struct ifnet *ifp, u_int32_t event_code)
7329 {
7330 	struct event {
7331 		u_int32_t ifnet_family;
7332 		u_int32_t unit;
7333 		char if_name[IFNAMSIZ];
7334 	};
7335 	_Alignas(struct kern_event_msg) char message[sizeof(struct kern_event_msg) + sizeof(struct event)] = { 0 };
7336 	struct kern_event_msg *header = (struct kern_event_msg*)message;
7337 	struct event *data = (struct event *)(header + 1);
7338 
7339 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE,
7340 	    "%s event_code %u - %s", ifp->if_xname,
7341 	    event_code, dlil_kev_dl_code_str(event_code));
7342 	header->total_size   = sizeof(message);
7343 	header->vendor_code  = KEV_VENDOR_APPLE;
7344 	header->kev_class    = KEV_NETWORK_CLASS;
7345 	header->kev_subclass = KEV_DL_SUBCLASS;
7346 	header->event_code   = event_code;
7347 	data->ifnet_family   = ifnet_family(ifp);
7348 	data->unit           = (u_int32_t)ifnet_unit(ifp);
7349 	strlcpy(data->if_name, ifnet_name(ifp), IFNAMSIZ);
7350 	ifnet_event(ifp, header);
7351 }
7352 
7353 #define BRIDGE_HF_DROP(reason, func, line) {                            \
7354 	        bridge_hostfilter_stats.reason++;                       \
7355 	        BRIDGE_LOG(LOG_DEBUG, BR_DBGF_HOSTFILTER,               \
7356 	                   "%s.%d" #reason, func, line);                \
7357 	        error = EINVAL;                                         \
7358 	}
7359 
7360 /*
7361  * Make sure this is a DHCP or Bootp request that match the host filter
7362  */
7363 static int
7364 bridge_dhcp_filter(struct bridge_iflist *bif, struct mbuf *m, size_t offset)
7365 {
7366 	int error = EINVAL;
7367 	struct dhcp dhcp;
7368 
7369 	/*
7370 	 * Note: We use the dhcp structure because bootp structure definition
7371 	 * is larger and some vendors do not pad the request
7372 	 */
7373 	error = mbuf_copydata(m, offset, sizeof(struct dhcp), &dhcp);
7374 	if (error != 0) {
7375 		BRIDGE_HF_DROP(brhf_dhcp_too_small, __func__, __LINE__);
7376 		goto done;
7377 	}
7378 	if (dhcp.dp_op != BOOTREQUEST) {
7379 		BRIDGE_HF_DROP(brhf_dhcp_bad_op, __func__, __LINE__);
7380 		goto done;
7381 	}
7382 	/*
7383 	 * The hardware address must be an exact match
7384 	 */
7385 	if (dhcp.dp_htype != ARPHRD_ETHER) {
7386 		BRIDGE_HF_DROP(brhf_dhcp_bad_htype, __func__, __LINE__);
7387 		goto done;
7388 	}
7389 	if (dhcp.dp_hlen != ETHER_ADDR_LEN) {
7390 		BRIDGE_HF_DROP(brhf_dhcp_bad_hlen, __func__, __LINE__);
7391 		goto done;
7392 	}
7393 	if (bcmp(dhcp.dp_chaddr, bif->bif_hf_hwsrc,
7394 	    ETHER_ADDR_LEN) != 0) {
7395 		BRIDGE_HF_DROP(brhf_dhcp_bad_chaddr, __func__, __LINE__);
7396 		goto done;
7397 	}
7398 	/*
7399 	 * Client address must match the host address or be not specified
7400 	 */
7401 	if (dhcp.dp_ciaddr.s_addr != bif->bif_hf_ipsrc.s_addr &&
7402 	    dhcp.dp_ciaddr.s_addr != INADDR_ANY) {
7403 		BRIDGE_HF_DROP(brhf_dhcp_bad_ciaddr, __func__, __LINE__);
7404 		goto done;
7405 	}
7406 	error = 0;
7407 done:
7408 	return error;
7409 }
7410 
7411 static int
7412 bridge_host_filter(struct bridge_iflist *bif, mbuf_t *data)
7413 {
7414 	int error = EINVAL;
7415 	struct ether_header *eh;
7416 	static struct in_addr inaddr_any = { .s_addr = INADDR_ANY };
7417 	mbuf_t m = *data;
7418 
7419 	eh = mtod(m, struct ether_header *);
7420 
7421 	/*
7422 	 * Restrict the source hardware address
7423 	 */
7424 	if ((bif->bif_flags & BIFF_HF_HWSRC) == 0 ||
7425 	    bcmp(eh->ether_shost, bif->bif_hf_hwsrc,
7426 	    ETHER_ADDR_LEN) != 0) {
7427 		BRIDGE_HF_DROP(brhf_bad_ether_srchw_addr, __func__, __LINE__);
7428 		goto done;
7429 	}
7430 
7431 	/*
7432 	 * Restrict Ethernet protocols to ARP and IP
7433 	 */
7434 	if (eh->ether_type == htons(ETHERTYPE_ARP)) {
7435 		struct ether_arp *ea;
7436 		size_t minlen = sizeof(struct ether_header) +
7437 		    sizeof(struct ether_arp);
7438 
7439 		/*
7440 		 * Make the Ethernet and ARP headers contiguous
7441 		 */
7442 		if (mbuf_pkthdr_len(m) < minlen) {
7443 			BRIDGE_HF_DROP(brhf_arp_too_small, __func__, __LINE__);
7444 			goto done;
7445 		}
7446 		if (mbuf_len(m) < minlen && mbuf_pullup(data, minlen) != 0) {
7447 			BRIDGE_HF_DROP(brhf_arp_pullup_failed,
7448 			    __func__, __LINE__);
7449 			goto done;
7450 		}
7451 		m = *data;
7452 
7453 		/*
7454 		 * Verify this is an ethernet/ip arp
7455 		 */
7456 		eh = mtod(m, struct ether_header *);
7457 		ea = (struct ether_arp *)(eh + 1);
7458 		if (ea->arp_hrd != htons(ARPHRD_ETHER)) {
7459 			BRIDGE_HF_DROP(brhf_arp_bad_hw_type,
7460 			    __func__, __LINE__);
7461 			goto done;
7462 		}
7463 		if (ea->arp_pro != htons(ETHERTYPE_IP)) {
7464 			BRIDGE_HF_DROP(brhf_arp_bad_pro_type,
7465 			    __func__, __LINE__);
7466 			goto done;
7467 		}
7468 		/*
7469 		 * Verify the address lengths are correct
7470 		 */
7471 		if (ea->arp_hln != ETHER_ADDR_LEN) {
7472 			BRIDGE_HF_DROP(brhf_arp_bad_hw_len, __func__, __LINE__);
7473 			goto done;
7474 		}
7475 		if (ea->arp_pln != sizeof(struct in_addr)) {
7476 			BRIDGE_HF_DROP(brhf_arp_bad_pro_len,
7477 			    __func__, __LINE__);
7478 			goto done;
7479 		}
7480 
7481 		/*
7482 		 * Allow only ARP request or ARP reply
7483 		 */
7484 		if (ea->arp_op != htons(ARPOP_REQUEST) &&
7485 		    ea->arp_op != htons(ARPOP_REPLY)) {
7486 			BRIDGE_HF_DROP(brhf_arp_bad_op, __func__, __LINE__);
7487 			goto done;
7488 		}
7489 		/*
7490 		 * Verify source hardware address matches
7491 		 */
7492 		if (bcmp(ea->arp_sha, bif->bif_hf_hwsrc,
7493 		    ETHER_ADDR_LEN) != 0) {
7494 			BRIDGE_HF_DROP(brhf_arp_bad_sha, __func__, __LINE__);
7495 			goto done;
7496 		}
7497 		/*
7498 		 * Verify source protocol address:
7499 		 * May be null for an ARP probe
7500 		 */
7501 		if (bcmp(ea->arp_spa, &bif->bif_hf_ipsrc.s_addr,
7502 		    sizeof(struct in_addr)) != 0 &&
7503 		    bcmp(ea->arp_spa, &inaddr_any,
7504 		    sizeof(struct in_addr)) != 0) {
7505 			BRIDGE_HF_DROP(brhf_arp_bad_spa, __func__, __LINE__);
7506 			goto done;
7507 		}
7508 		bridge_hostfilter_stats.brhf_arp_ok += 1;
7509 		error = 0;
7510 	} else if (eh->ether_type == htons(ETHERTYPE_IP)) {
7511 		size_t minlen = sizeof(struct ether_header) + sizeof(struct ip);
7512 		struct ip iphdr;
7513 		size_t offset;
7514 
7515 		/*
7516 		 * Make the Ethernet and IP headers contiguous
7517 		 */
7518 		if (mbuf_pkthdr_len(m) < minlen) {
7519 			BRIDGE_HF_DROP(brhf_ip_too_small, __func__, __LINE__);
7520 			goto done;
7521 		}
7522 		offset = sizeof(struct ether_header);
7523 		error = mbuf_copydata(m, offset, sizeof(struct ip), &iphdr);
7524 		if (error != 0) {
7525 			BRIDGE_HF_DROP(brhf_ip_too_small, __func__, __LINE__);
7526 			goto done;
7527 		}
7528 		/*
7529 		 * Verify the source IP address
7530 		 */
7531 		if (iphdr.ip_p == IPPROTO_UDP) {
7532 			struct udphdr udp;
7533 
7534 			minlen += sizeof(struct udphdr);
7535 			if (mbuf_pkthdr_len(m) < minlen) {
7536 				BRIDGE_HF_DROP(brhf_ip_too_small,
7537 				    __func__, __LINE__);
7538 				goto done;
7539 			}
7540 
7541 			/*
7542 			 * Allow all zero addresses for DHCP requests
7543 			 */
7544 			if (iphdr.ip_src.s_addr != bif->bif_hf_ipsrc.s_addr &&
7545 			    iphdr.ip_src.s_addr != INADDR_ANY) {
7546 				BRIDGE_HF_DROP(brhf_ip_bad_srcaddr,
7547 				    __func__, __LINE__);
7548 				goto done;
7549 			}
7550 			offset = sizeof(struct ether_header) +
7551 			    (IP_VHL_HL(iphdr.ip_vhl) << 2);
7552 			error = mbuf_copydata(m, offset,
7553 			    sizeof(struct udphdr), &udp);
7554 			if (error != 0) {
7555 				BRIDGE_HF_DROP(brhf_ip_too_small,
7556 				    __func__, __LINE__);
7557 				goto done;
7558 			}
7559 			/*
7560 			 * Either it's a Bootp/DHCP packet that we like or
7561 			 * it's a UDP packet from the host IP as source address
7562 			 */
7563 			if (udp.uh_sport == htons(IPPORT_BOOTPC) &&
7564 			    udp.uh_dport == htons(IPPORT_BOOTPS)) {
7565 				minlen += sizeof(struct dhcp);
7566 				if (mbuf_pkthdr_len(m) < minlen) {
7567 					BRIDGE_HF_DROP(brhf_ip_too_small,
7568 					    __func__, __LINE__);
7569 					goto done;
7570 				}
7571 				offset += sizeof(struct udphdr);
7572 				error = bridge_dhcp_filter(bif, m, offset);
7573 				if (error != 0) {
7574 					goto done;
7575 				}
7576 			} else if (iphdr.ip_src.s_addr == INADDR_ANY) {
7577 				BRIDGE_HF_DROP(brhf_ip_bad_srcaddr,
7578 				    __func__, __LINE__);
7579 				goto done;
7580 			}
7581 		} else if (iphdr.ip_src.s_addr != bif->bif_hf_ipsrc.s_addr ||
7582 		    bif->bif_hf_ipsrc.s_addr == INADDR_ANY) {
7583 			BRIDGE_HF_DROP(brhf_ip_bad_srcaddr, __func__, __LINE__);
7584 			goto done;
7585 		}
7586 		/*
7587 		 * Allow only boring IP protocols
7588 		 */
7589 		if (iphdr.ip_p != IPPROTO_TCP &&
7590 		    iphdr.ip_p != IPPROTO_UDP &&
7591 		    iphdr.ip_p != IPPROTO_ICMP &&
7592 		    iphdr.ip_p != IPPROTO_ESP &&
7593 		    iphdr.ip_p != IPPROTO_AH &&
7594 		    iphdr.ip_p != IPPROTO_GRE) {
7595 			BRIDGE_HF_DROP(brhf_ip_bad_proto, __func__, __LINE__);
7596 			goto done;
7597 		}
7598 		bridge_hostfilter_stats.brhf_ip_ok += 1;
7599 		error = 0;
7600 	} else {
7601 		BRIDGE_HF_DROP(brhf_bad_ether_type, __func__, __LINE__);
7602 		goto done;
7603 	}
7604 done:
7605 	if (error != 0) {
7606 		if (BRIDGE_DBGF_ENABLED(BR_DBGF_HOSTFILTER)) {
7607 			if (m) {
7608 				brlog_mbuf_data(m, 0,
7609 				    sizeof(struct ether_header) +
7610 				    sizeof(struct ip));
7611 			}
7612 		}
7613 
7614 		if (m != NULL) {
7615 			m_freem(m);
7616 		}
7617 	}
7618 	return error;
7619 }
7620 
7621 /*
7622  * MAC NAT
7623  */
7624 
7625 static errno_t
7626 bridge_mac_nat_enable(struct bridge_softc *sc, struct bridge_iflist *bif)
7627 {
7628 	errno_t         error = 0;
7629 
7630 	BRIDGE_LOCK_ASSERT_HELD(sc);
7631 
7632 	if (IFNET_IS_VMNET(bif->bif_ifp)) {
7633 		error = EINVAL;
7634 		goto done;
7635 	}
7636 	if (sc->sc_mac_nat_bif != NULL) {
7637 		if (sc->sc_mac_nat_bif != bif) {
7638 			error = EBUSY;
7639 		}
7640 		goto done;
7641 	}
7642 	sc->sc_mac_nat_bif = bif;
7643 	bif->bif_ifflags |= IFBIF_MAC_NAT;
7644 	bridge_mac_nat_populate_entries(sc);
7645 
7646 done:
7647 	return error;
7648 }
7649 
7650 static void
7651 bridge_mac_nat_disable(struct bridge_softc *sc)
7652 {
7653 	struct bridge_iflist *mac_nat_bif = sc->sc_mac_nat_bif;
7654 
7655 	assert(mac_nat_bif != NULL);
7656 	bridge_mac_nat_flush_entries(sc, mac_nat_bif);
7657 	mac_nat_bif->bif_ifflags &= ~IFBIF_MAC_NAT;
7658 	sc->sc_mac_nat_bif = NULL;
7659 	return;
7660 }
7661 
7662 static void
7663 mac_nat_entry_print2(struct mac_nat_entry *mne,
7664     char *ifname, const char *msg1, const char *msg2)
7665 {
7666 	int             af;
7667 	char            etopbuf[24];
7668 	char            ntopbuf[MAX_IPv6_STR_LEN];
7669 	const char      *space;
7670 
7671 	af = ((mne->mne_flags & MNE_FLAGS_IPV6) != 0) ? AF_INET6 : AF_INET;
7672 	ether_ntop(etopbuf, sizeof(etopbuf), mne->mne_mac);
7673 	(void)inet_ntop(af, &mne->mne_u, ntopbuf, sizeof(ntopbuf));
7674 	if (msg2 == NULL) {
7675 		msg2 = "";
7676 		space = "";
7677 	} else {
7678 		space = " ";
7679 	}
7680 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
7681 	    "%s %s%s%s %p (%s, %s, %s)",
7682 	    ifname, msg1, space, msg2, mne, mne->mne_bif->bif_ifp->if_xname,
7683 	    ntopbuf, etopbuf);
7684 }
7685 
7686 static void
7687 mac_nat_entry_print(struct mac_nat_entry *mne,
7688     char *ifname, const char *msg)
7689 {
7690 	mac_nat_entry_print2(mne, ifname, msg, NULL);
7691 }
7692 
7693 static struct mac_nat_entry *
7694 bridge_lookup_mac_nat_entry(struct bridge_softc *sc, int af, void * ip)
7695 {
7696 	struct mac_nat_entry    *mne;
7697 	struct mac_nat_entry    *ret_mne = NULL;
7698 
7699 	if (af == AF_INET) {
7700 		in_addr_t s_addr = ((struct in_addr *)ip)->s_addr;
7701 
7702 		LIST_FOREACH(mne, &sc->sc_mne_list, mne_list) {
7703 			if (mne->mne_ip.s_addr == s_addr) {
7704 				if (BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) {
7705 					mac_nat_entry_print(mne, sc->sc_if_xname,
7706 					    "found");
7707 				}
7708 				ret_mne = mne;
7709 				break;
7710 			}
7711 		}
7712 	} else {
7713 		const struct in6_addr *ip6 = (const struct in6_addr *)ip;
7714 
7715 		LIST_FOREACH(mne, &sc->sc_mne_list_v6, mne_list) {
7716 			if (IN6_ARE_ADDR_EQUAL(&mne->mne_ip6, ip6)) {
7717 				if (BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) {
7718 					mac_nat_entry_print(mne, sc->sc_if_xname,
7719 					    "found");
7720 				}
7721 				ret_mne = mne;
7722 				break;
7723 			}
7724 		}
7725 	}
7726 	return ret_mne;
7727 }
7728 
7729 static void
7730 bridge_destroy_mac_nat_entry(struct bridge_softc *sc,
7731     struct mac_nat_entry *mne, const char *reason)
7732 {
7733 	LIST_REMOVE(mne, mne_list);
7734 	if (BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) {
7735 		mac_nat_entry_print(mne, sc->sc_if_xname, reason);
7736 	}
7737 	zfree(bridge_mne_pool, mne);
7738 	sc->sc_mne_count--;
7739 }
7740 
7741 static struct mac_nat_entry *
7742 bridge_create_mac_nat_entry(struct bridge_softc *sc,
7743     struct bridge_iflist *bif, int af, const void *ip, uint8_t *eaddr)
7744 {
7745 	struct mac_nat_entry_list *list;
7746 	struct mac_nat_entry *mne;
7747 
7748 	if (sc->sc_mne_count >= sc->sc_mne_max) {
7749 		sc->sc_mne_allocation_failures++;
7750 		return NULL;
7751 	}
7752 	mne = zalloc_noblock(bridge_mne_pool);
7753 	if (mne == NULL) {
7754 		sc->sc_mne_allocation_failures++;
7755 		return NULL;
7756 	}
7757 	sc->sc_mne_count++;
7758 	bzero(mne, sizeof(*mne));
7759 	bcopy(eaddr, mne->mne_mac, sizeof(mne->mne_mac));
7760 	mne->mne_bif = bif;
7761 	if (af == AF_INET) {
7762 		bcopy(ip, &mne->mne_ip, sizeof(mne->mne_ip));
7763 		list = &sc->sc_mne_list;
7764 	} else {
7765 		bcopy(ip, &mne->mne_ip6, sizeof(mne->mne_ip6));
7766 		mne->mne_flags |= MNE_FLAGS_IPV6;
7767 		list = &sc->sc_mne_list_v6;
7768 	}
7769 	LIST_INSERT_HEAD(list, mne, mne_list);
7770 	mne->mne_expire = (unsigned long)net_uptime() + sc->sc_brttimeout;
7771 	if (BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) {
7772 		mac_nat_entry_print(mne, sc->sc_if_xname, "created");
7773 	}
7774 	return mne;
7775 }
7776 
7777 static struct mac_nat_entry *
7778 bridge_update_mac_nat_entry(struct bridge_softc *sc,
7779     struct bridge_iflist *bif, int af, void *ip, uint8_t *eaddr)
7780 {
7781 	struct mac_nat_entry *mne;
7782 
7783 	mne = bridge_lookup_mac_nat_entry(sc, af, ip);
7784 	if (mne != NULL) {
7785 		struct bridge_iflist *mac_nat_bif = sc->sc_mac_nat_bif;
7786 
7787 		if (mne->mne_bif == mac_nat_bif) {
7788 			/* the MAC NAT interface takes precedence */
7789 			if (BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) {
7790 				if (mne->mne_bif != bif) {
7791 					mac_nat_entry_print2(mne,
7792 					    sc->sc_if_xname, "reject",
7793 					    bif->bif_ifp->if_xname);
7794 				}
7795 			}
7796 		} else if (mne->mne_bif != bif) {
7797 			const char *old_if = mne->mne_bif->bif_ifp->if_xname;
7798 
7799 			mne->mne_bif = bif;
7800 			if (BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) {
7801 				mac_nat_entry_print2(mne,
7802 				    sc->sc_if_xname, "replaced",
7803 				    old_if);
7804 			}
7805 			bcopy(eaddr, mne->mne_mac, sizeof(mne->mne_mac));
7806 		}
7807 		mne->mne_expire = (unsigned long)net_uptime() +
7808 		    sc->sc_brttimeout;
7809 	} else {
7810 		mne = bridge_create_mac_nat_entry(sc, bif, af, ip, eaddr);
7811 	}
7812 	return mne;
7813 }
7814 
7815 static void
7816 bridge_mac_nat_flush_entries_common(struct bridge_softc *sc,
7817     struct mac_nat_entry_list *list, struct bridge_iflist *bif)
7818 {
7819 	struct mac_nat_entry *mne;
7820 	struct mac_nat_entry *tmne;
7821 
7822 	LIST_FOREACH_SAFE(mne, list, mne_list, tmne) {
7823 		if (bif != NULL && mne->mne_bif != bif) {
7824 			continue;
7825 		}
7826 		bridge_destroy_mac_nat_entry(sc, mne, "flushed");
7827 	}
7828 }
7829 
7830 /*
7831  * bridge_mac_nat_flush_entries:
7832  *
7833  * Flush MAC NAT entries for the specified member. Flush all entries if
7834  * the member is the one that requires MAC NAT, otherwise just flush the
7835  * ones for the specified member.
7836  */
7837 static void
7838 bridge_mac_nat_flush_entries(struct bridge_softc *sc, struct bridge_iflist * bif)
7839 {
7840 	struct bridge_iflist *flush_bif;
7841 
7842 	flush_bif = (bif == sc->sc_mac_nat_bif) ? NULL : bif;
7843 	bridge_mac_nat_flush_entries_common(sc, &sc->sc_mne_list, flush_bif);
7844 	bridge_mac_nat_flush_entries_common(sc, &sc->sc_mne_list_v6, flush_bif);
7845 }
7846 
7847 static void
7848 bridge_mac_nat_populate_entries(struct bridge_softc *sc)
7849 {
7850 	errno_t                 error;
7851 	ifnet_t                 ifp;
7852 	ifaddr_t                *list;
7853 	struct bridge_iflist    *mac_nat_bif = sc->sc_mac_nat_bif;
7854 
7855 	assert(mac_nat_bif != NULL);
7856 	ifp = mac_nat_bif->bif_ifp;
7857 	error = ifnet_get_address_list(ifp, &list);
7858 	if (error != 0) {
7859 		BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT,
7860 		    "ifnet_get_address_list(%s) failed %d",
7861 		    ifp->if_xname, error);
7862 		return;
7863 	}
7864 	for (ifaddr_t *scan = list; *scan != NULL; scan++) {
7865 		sa_family_t     af;
7866 		void            *ip;
7867 
7868 		union {
7869 			struct sockaddr         sa;
7870 			struct sockaddr_in      sin;
7871 			struct sockaddr_in6     sin6;
7872 		} u;
7873 		af = ifaddr_address_family(*scan);
7874 		switch (af) {
7875 		case AF_INET:
7876 		case AF_INET6:
7877 			error = ifaddr_address(*scan, &u.sa, sizeof(u));
7878 			if (error != 0) {
7879 				BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT,
7880 				    "ifaddr_address failed %d",
7881 				    error);
7882 				break;
7883 			}
7884 			if (af == AF_INET) {
7885 				ip = (void *)&u.sin.sin_addr;
7886 			} else {
7887 				if (IN6_IS_ADDR_LINKLOCAL(&u.sin6.sin6_addr)) {
7888 					/* remove scope ID */
7889 					u.sin6.sin6_addr.s6_addr16[1] = 0;
7890 				}
7891 				ip = (void *)&u.sin6.sin6_addr;
7892 			}
7893 			bridge_create_mac_nat_entry(sc, mac_nat_bif, af, ip,
7894 			    (uint8_t *)IF_LLADDR(ifp));
7895 			break;
7896 		default:
7897 			break;
7898 		}
7899 	}
7900 	ifnet_free_address_list(list);
7901 	return;
7902 }
7903 
7904 static void
7905 bridge_mac_nat_age_entries_common(struct bridge_softc *sc,
7906     struct mac_nat_entry_list *list, unsigned long now)
7907 {
7908 	struct mac_nat_entry *mne;
7909 	struct mac_nat_entry *tmne;
7910 
7911 	LIST_FOREACH_SAFE(mne, list, mne_list, tmne) {
7912 		if (now >= mne->mne_expire) {
7913 			bridge_destroy_mac_nat_entry(sc, mne, "aged out");
7914 		}
7915 	}
7916 }
7917 
7918 static void
7919 bridge_mac_nat_age_entries(struct bridge_softc *sc, unsigned long now)
7920 {
7921 	if (sc->sc_mac_nat_bif == NULL) {
7922 		return;
7923 	}
7924 	bridge_mac_nat_age_entries_common(sc, &sc->sc_mne_list, now);
7925 	bridge_mac_nat_age_entries_common(sc, &sc->sc_mne_list_v6, now);
7926 }
7927 
7928 static const char *
7929 get_in_out_string(boolean_t is_output)
7930 {
7931 	return is_output ? "OUT" : "IN";
7932 }
7933 
7934 /*
7935  * is_valid_arp_packet:
7936  *	Verify that this is a valid ARP packet.
7937  *
7938  *	Returns TRUE if the packet is valid, FALSE otherwise.
7939  */
7940 static boolean_t
7941 is_valid_arp_packet(mbuf_t *data, boolean_t is_output,
7942     struct ether_header **eh_p, struct ether_arp **ea_p)
7943 {
7944 	struct ether_arp *ea;
7945 	struct ether_header *eh;
7946 	size_t minlen = sizeof(struct ether_header) + sizeof(struct ether_arp);
7947 	boolean_t is_valid = FALSE;
7948 	int flags = is_output ? BR_DBGF_OUTPUT : BR_DBGF_INPUT;
7949 
7950 	if (mbuf_pkthdr_len(*data) < minlen) {
7951 		BRIDGE_LOG(LOG_DEBUG, flags,
7952 		    "ARP %s short frame %lu < %lu",
7953 		    get_in_out_string(is_output),
7954 		    mbuf_pkthdr_len(*data), minlen);
7955 		goto done;
7956 	}
7957 	if (mbuf_len(*data) < minlen && mbuf_pullup(data, minlen) != 0) {
7958 		BRIDGE_LOG(LOG_DEBUG, flags,
7959 		    "ARP %s size %lu mbuf_pullup fail",
7960 		    get_in_out_string(is_output),
7961 		    minlen);
7962 		*data = NULL;
7963 		goto done;
7964 	}
7965 
7966 	/* validate ARP packet */
7967 	eh = mtod(*data, struct ether_header *);
7968 	ea = (struct ether_arp *)(eh + 1);
7969 	if (ntohs(ea->arp_hrd) != ARPHRD_ETHER) {
7970 		BRIDGE_LOG(LOG_DEBUG, flags,
7971 		    "ARP %s htype not ethernet",
7972 		    get_in_out_string(is_output));
7973 		goto done;
7974 	}
7975 	if (ea->arp_hln != ETHER_ADDR_LEN) {
7976 		BRIDGE_LOG(LOG_DEBUG, flags,
7977 		    "ARP %s hlen not ethernet",
7978 		    get_in_out_string(is_output));
7979 		goto done;
7980 	}
7981 	if (ntohs(ea->arp_pro) != ETHERTYPE_IP) {
7982 		BRIDGE_LOG(LOG_DEBUG, flags,
7983 		    "ARP %s ptype not IP",
7984 		    get_in_out_string(is_output));
7985 		goto done;
7986 	}
7987 	if (ea->arp_pln != sizeof(struct in_addr)) {
7988 		BRIDGE_LOG(LOG_DEBUG, flags,
7989 		    "ARP %s plen not IP",
7990 		    get_in_out_string(is_output));
7991 		goto done;
7992 	}
7993 	is_valid = TRUE;
7994 	*ea_p = ea;
7995 	*eh_p = eh;
7996 done:
7997 	return is_valid;
7998 }
7999 
8000 static struct mac_nat_entry *
8001 bridge_mac_nat_arp_input(struct bridge_softc *sc, mbuf_t *data)
8002 {
8003 	struct ether_arp        *ea;
8004 	struct ether_header     *eh;
8005 	struct mac_nat_entry    *mne = NULL;
8006 	u_short                 op;
8007 	struct in_addr          tpa;
8008 
8009 	if (!is_valid_arp_packet(data, FALSE, &eh, &ea)) {
8010 		goto done;
8011 	}
8012 	op = ntohs(ea->arp_op);
8013 	switch (op) {
8014 	case ARPOP_REQUEST:
8015 	case ARPOP_REPLY:
8016 		/* only care about REQUEST and REPLY */
8017 		break;
8018 	default:
8019 		goto done;
8020 	}
8021 
8022 	/* check the target IP address for a NAT entry */
8023 	bcopy(ea->arp_tpa, &tpa, sizeof(tpa));
8024 	if (tpa.s_addr != 0) {
8025 		mne = bridge_lookup_mac_nat_entry(sc, AF_INET, &tpa);
8026 	}
8027 	if (mne != NULL) {
8028 		if (op == ARPOP_REPLY) {
8029 			/* translate the MAC address */
8030 			if (BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) {
8031 				char    mac_src[24];
8032 				char    mac_dst[24];
8033 
8034 				ether_ntop(mac_src, sizeof(mac_src),
8035 				    ea->arp_tha);
8036 				ether_ntop(mac_dst, sizeof(mac_dst),
8037 				    mne->mne_mac);
8038 				BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8039 				    "%s %s ARP %s -> %s",
8040 				    sc->sc_if_xname,
8041 				    mne->mne_bif->bif_ifp->if_xname,
8042 				    mac_src, mac_dst);
8043 			}
8044 			bcopy(mne->mne_mac, ea->arp_tha, sizeof(ea->arp_tha));
8045 		}
8046 	} else {
8047 		/* handle conflicting ARP (sender matches mne) */
8048 		struct in_addr spa;
8049 
8050 		bcopy(ea->arp_spa, &spa, sizeof(spa));
8051 		if (spa.s_addr != 0 && spa.s_addr != tpa.s_addr) {
8052 			/* check the source IP for a NAT entry */
8053 			mne = bridge_lookup_mac_nat_entry(sc, AF_INET, &spa);
8054 		}
8055 	}
8056 
8057 done:
8058 	return mne;
8059 }
8060 
8061 static boolean_t
8062 bridge_mac_nat_arp_output(struct bridge_softc *sc,
8063     struct bridge_iflist *bif, mbuf_t *data, struct mac_nat_record *mnr)
8064 {
8065 	struct ether_arp        *ea;
8066 	struct ether_header     *eh;
8067 	struct in_addr          ip;
8068 	struct mac_nat_entry    *mne = NULL;
8069 	u_short                 op;
8070 	boolean_t               translate = FALSE;
8071 
8072 	if (!is_valid_arp_packet(data, TRUE, &eh, &ea)) {
8073 		goto done;
8074 	}
8075 	op = ntohs(ea->arp_op);
8076 	switch (op) {
8077 	case ARPOP_REQUEST:
8078 	case ARPOP_REPLY:
8079 		/* only care about REQUEST and REPLY */
8080 		break;
8081 	default:
8082 		goto done;
8083 	}
8084 
8085 	bcopy(ea->arp_spa, &ip, sizeof(ip));
8086 	if (ip.s_addr == 0) {
8087 		goto done;
8088 	}
8089 	/* XXX validate IP address: no multicast/broadcast */
8090 	mne = bridge_update_mac_nat_entry(sc, bif, AF_INET, &ip, ea->arp_sha);
8091 	if (mnr != NULL && mne != NULL) {
8092 		/* record the offset to do the replacement */
8093 		translate = TRUE;
8094 		mnr->mnr_arp_offset = (char *)ea->arp_sha - (char *)eh;
8095 	}
8096 
8097 done:
8098 	return translate;
8099 }
8100 
8101 #define ETHER_IPV4_HEADER_LEN   (sizeof(struct ether_header) +  \
8102 	                         + sizeof(struct ip))
8103 static struct ether_header *
8104 get_ether_ip_header(mbuf_t *data, boolean_t is_output)
8105 {
8106 	struct ether_header     *eh = NULL;
8107 	int             flags = is_output ? BR_DBGF_OUTPUT : BR_DBGF_INPUT;
8108 	size_t          minlen = ETHER_IPV4_HEADER_LEN;
8109 
8110 	if (mbuf_pkthdr_len(*data) < minlen) {
8111 		BRIDGE_LOG(LOG_DEBUG, flags,
8112 		    "IP %s short frame %lu < %lu",
8113 		    get_in_out_string(is_output),
8114 		    mbuf_pkthdr_len(*data), minlen);
8115 		goto done;
8116 	}
8117 	if (mbuf_len(*data) < minlen && mbuf_pullup(data, minlen) != 0) {
8118 		BRIDGE_LOG(LOG_DEBUG, flags,
8119 		    "IP %s size %lu mbuf_pullup fail",
8120 		    get_in_out_string(is_output),
8121 		    minlen);
8122 		*data = NULL;
8123 		goto done;
8124 	}
8125 	eh = mtod(*data, struct ether_header *);
8126 done:
8127 	return eh;
8128 }
8129 
8130 static bool
8131 is_broadcast_ip_packet(mbuf_t *data)
8132 {
8133 	struct ether_header     *eh;
8134 	uint16_t                ether_type;
8135 	bool                    is_broadcast = FALSE;
8136 
8137 	eh = mtod(*data, struct ether_header *);
8138 	ether_type = ntohs(eh->ether_type);
8139 	switch (ether_type) {
8140 	case ETHERTYPE_IP:
8141 		eh = get_ether_ip_header(data, FALSE);
8142 		if (eh != NULL) {
8143 			struct in_addr  dst;
8144 			struct ip       *iphdr;
8145 
8146 			iphdr = (struct ip *)(void *)(eh + 1);
8147 			bcopy(&iphdr->ip_dst, &dst, sizeof(dst));
8148 			is_broadcast = (dst.s_addr == INADDR_BROADCAST);
8149 		}
8150 		break;
8151 	default:
8152 		break;
8153 	}
8154 	return is_broadcast;
8155 }
8156 
8157 static struct mac_nat_entry *
8158 bridge_mac_nat_ip_input(struct bridge_softc *sc, mbuf_t *data)
8159 {
8160 	struct in_addr          dst;
8161 	struct ether_header     *eh;
8162 	struct ip               *iphdr;
8163 	struct mac_nat_entry    *mne = NULL;
8164 
8165 	eh = get_ether_ip_header(data, FALSE);
8166 	if (eh == NULL) {
8167 		goto done;
8168 	}
8169 	iphdr = (struct ip *)(void *)(eh + 1);
8170 	bcopy(&iphdr->ip_dst, &dst, sizeof(dst));
8171 	/* XXX validate IP address */
8172 	if (dst.s_addr == 0) {
8173 		goto done;
8174 	}
8175 	mne = bridge_lookup_mac_nat_entry(sc, AF_INET, &dst);
8176 done:
8177 	return mne;
8178 }
8179 
8180 static void
8181 bridge_mac_nat_udp_output(struct bridge_softc *sc,
8182     struct bridge_iflist *bif, mbuf_t m,
8183     uint8_t ip_header_len, struct mac_nat_record *mnr)
8184 {
8185 	uint16_t        dp_flags;
8186 	errno_t         error;
8187 	size_t          offset;
8188 	struct udphdr   udphdr;
8189 
8190 	/* copy the UDP header */
8191 	offset = sizeof(struct ether_header) + ip_header_len;
8192 	error = mbuf_copydata(m, offset, sizeof(struct udphdr), &udphdr);
8193 	if (error != 0) {
8194 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8195 		    "mbuf_copydata udphdr failed %d",
8196 		    error);
8197 		return;
8198 	}
8199 	if (ntohs(udphdr.uh_sport) != IPPORT_BOOTPC ||
8200 	    ntohs(udphdr.uh_dport) != IPPORT_BOOTPS) {
8201 		/* not a BOOTP/DHCP packet */
8202 		return;
8203 	}
8204 	/* check whether the broadcast bit is already set */
8205 	offset += sizeof(struct udphdr) + offsetof(struct dhcp, dp_flags);
8206 	error = mbuf_copydata(m, offset, sizeof(dp_flags), &dp_flags);
8207 	if (error != 0) {
8208 		BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8209 		    "mbuf_copydata dp_flags failed %d",
8210 		    error);
8211 		return;
8212 	}
8213 	if ((ntohs(dp_flags) & DHCP_FLAGS_BROADCAST) != 0) {
8214 		/* it's already set, nothing to do */
8215 		return;
8216 	}
8217 	/* broadcast bit needs to be set */
8218 	mnr->mnr_ip_dhcp_flags = dp_flags | htons(DHCP_FLAGS_BROADCAST);
8219 	mnr->mnr_ip_header_len = ip_header_len;
8220 	if (udphdr.uh_sum != 0) {
8221 		uint16_t        delta;
8222 
8223 		/* adjust checksum to take modified dp_flags into account */
8224 		delta = dp_flags - mnr->mnr_ip_dhcp_flags;
8225 		mnr->mnr_ip_udp_csum = udphdr.uh_sum + delta;
8226 	}
8227 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8228 	    "%s %s DHCP dp_flags 0x%x UDP cksum 0x%x",
8229 	    sc->sc_if_xname,
8230 	    bif->bif_ifp->if_xname,
8231 	    ntohs(mnr->mnr_ip_dhcp_flags),
8232 	    ntohs(mnr->mnr_ip_udp_csum));
8233 	return;
8234 }
8235 
8236 static boolean_t
8237 bridge_mac_nat_ip_output(struct bridge_softc *sc,
8238     struct bridge_iflist *bif, mbuf_t *data, struct mac_nat_record *mnr)
8239 {
8240 #pragma unused(mnr)
8241 	struct ether_header     *eh;
8242 	struct in_addr          ip;
8243 	struct ip               *iphdr;
8244 	uint8_t                 ip_header_len;
8245 	struct mac_nat_entry    *mne = NULL;
8246 	boolean_t               translate = FALSE;
8247 
8248 	eh = get_ether_ip_header(data, TRUE);
8249 	if (eh == NULL) {
8250 		goto done;
8251 	}
8252 	iphdr = (struct ip *)(void *)(eh + 1);
8253 	ip_header_len = IP_VHL_HL(iphdr->ip_vhl) << 2;
8254 	if (ip_header_len < sizeof(ip)) {
8255 		/* bogus IP header */
8256 		goto done;
8257 	}
8258 	bcopy(&iphdr->ip_src, &ip, sizeof(ip));
8259 	/* XXX validate the source address */
8260 	if (ip.s_addr != 0) {
8261 		mne = bridge_update_mac_nat_entry(sc, bif, AF_INET, &ip,
8262 		    eh->ether_shost);
8263 	}
8264 	if (mnr != NULL) {
8265 		if (iphdr->ip_p == IPPROTO_UDP) {
8266 			/* handle DHCP must broadcast */
8267 			bridge_mac_nat_udp_output(sc, bif, *data,
8268 			    ip_header_len, mnr);
8269 		}
8270 		translate = TRUE;
8271 	}
8272 done:
8273 	return translate;
8274 }
8275 
8276 #define ETHER_IPV6_HEADER_LEN   (sizeof(struct ether_header) +  \
8277 	                         + sizeof(struct ip6_hdr))
8278 static struct ether_header *
8279 get_ether_ipv6_header(mbuf_t *data, size_t plen, boolean_t is_output)
8280 {
8281 	struct ether_header     *eh = NULL;
8282 	int             flags = is_output ? BR_DBGF_OUTPUT : BR_DBGF_INPUT;
8283 	size_t          minlen = ETHER_IPV6_HEADER_LEN + plen;
8284 
8285 	if (mbuf_pkthdr_len(*data) < minlen) {
8286 		BRIDGE_LOG(LOG_DEBUG, flags,
8287 		    "IP %s short frame %lu < %lu",
8288 		    get_in_out_string(is_output),
8289 		    mbuf_pkthdr_len(*data), minlen);
8290 		goto done;
8291 	}
8292 	if (mbuf_len(*data) < minlen && mbuf_pullup(data, minlen) != 0) {
8293 		BRIDGE_LOG(LOG_DEBUG, flags,
8294 		    "IP %s size %lu mbuf_pullup fail",
8295 		    get_in_out_string(is_output),
8296 		    minlen);
8297 		*data = NULL;
8298 		goto done;
8299 	}
8300 	eh = mtod(*data, struct ether_header *);
8301 done:
8302 	return eh;
8303 }
8304 
8305 #include <netinet/icmp6.h>
8306 #include <netinet6/nd6.h>
8307 
8308 #define ETHER_ND_LLADDR_LEN     (ETHER_ADDR_LEN + sizeof(struct nd_opt_hdr))
8309 
8310 static void
8311 bridge_mac_nat_icmpv6_output(struct bridge_softc *sc,
8312     struct bridge_iflist *bif,
8313     mbuf_t *data, struct ip6_hdr *ip6h,
8314     struct in6_addr *saddrp,
8315     struct mac_nat_record *mnr)
8316 {
8317 	struct ether_header *eh;
8318 	struct icmp6_hdr *icmp6;
8319 	uint8_t         icmp6_type;
8320 	uint32_t        icmp6len;
8321 	int             lladdrlen = 0;
8322 	char            *lladdr = NULL;
8323 	unsigned int    off = sizeof(*ip6h);
8324 
8325 	icmp6len = (u_int32_t)ntohs(ip6h->ip6_plen);
8326 	if (icmp6len < sizeof(*icmp6)) {
8327 		BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT,
8328 		    "short IPv6 payload length %d < %lu",
8329 		    icmp6len, sizeof(*icmp6));
8330 		return;
8331 	}
8332 
8333 	/* pullup IP6 header + ICMPv6 header */
8334 	eh = get_ether_ipv6_header(data, sizeof(*icmp6), TRUE);
8335 	if (eh == NULL) {
8336 		BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT,
8337 		    "failed to pullup icmp6 header");
8338 		return;
8339 	}
8340 	ip6h = (struct ip6_hdr *)(void *)(eh + 1);
8341 	icmp6 = (struct icmp6_hdr *)((caddr_t)ip6h + off);
8342 	icmp6_type = icmp6->icmp6_type;
8343 	switch (icmp6_type) {
8344 	case ND_NEIGHBOR_SOLICIT:
8345 	case ND_NEIGHBOR_ADVERT:
8346 	case ND_ROUTER_ADVERT:
8347 	case ND_ROUTER_SOLICIT:
8348 		break;
8349 	default:
8350 		return;
8351 	}
8352 
8353 	/* pullup IP6 header + payload */
8354 	eh = get_ether_ipv6_header(data, icmp6len, TRUE);
8355 	if (eh == NULL) {
8356 		BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT,
8357 		    "failed to pullup icmp6 + payload");
8358 		return;
8359 	}
8360 	ip6h = (struct ip6_hdr *)(void *)(eh + 1);
8361 	icmp6 = (struct icmp6_hdr *)((caddr_t)ip6h + off);
8362 	switch (icmp6_type) {
8363 	case ND_NEIGHBOR_SOLICIT: {
8364 		struct nd_neighbor_solicit *nd_ns;
8365 		union nd_opts ndopts;
8366 		boolean_t is_dad_probe;
8367 		struct in6_addr taddr;
8368 
8369 		if (icmp6len < sizeof(*nd_ns)) {
8370 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8371 			    "short nd_ns %d < %lu",
8372 			    icmp6len, sizeof(*nd_ns));
8373 			return;
8374 		}
8375 
8376 		nd_ns = (struct nd_neighbor_solicit *)(void *)icmp6;
8377 		bcopy(&nd_ns->nd_ns_target, &taddr, sizeof(taddr));
8378 		if (IN6_IS_ADDR_MULTICAST(&taddr) ||
8379 		    IN6_IS_ADDR_UNSPECIFIED(&taddr)) {
8380 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8381 			    "invalid target ignored");
8382 			return;
8383 		}
8384 		/* parse options */
8385 		nd6_option_init(nd_ns + 1, icmp6len - sizeof(*nd_ns), &ndopts);
8386 		if (nd6_options(&ndopts) < 0) {
8387 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8388 			    "invalid ND6 NS option");
8389 			return;
8390 		}
8391 		if (ndopts.nd_opts_src_lladdr != NULL) {
8392 			lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
8393 			lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
8394 		}
8395 		is_dad_probe = IN6_IS_ADDR_UNSPECIFIED(saddrp);
8396 		if (lladdr != NULL) {
8397 			if (is_dad_probe) {
8398 				BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8399 				    "bad ND6 DAD packet");
8400 				return;
8401 			}
8402 			if (lladdrlen != ETHER_ND_LLADDR_LEN) {
8403 				BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8404 				    "source lladdrlen %d != %lu",
8405 				    lladdrlen, ETHER_ND_LLADDR_LEN);
8406 				return;
8407 			}
8408 		}
8409 		if (is_dad_probe) {
8410 			/* node is trying use taddr, create an mne for taddr */
8411 			*saddrp = taddr;
8412 		}
8413 		break;
8414 	}
8415 	case ND_NEIGHBOR_ADVERT: {
8416 		struct nd_neighbor_advert *nd_na;
8417 		union nd_opts ndopts;
8418 		struct in6_addr taddr;
8419 
8420 
8421 		nd_na = (struct nd_neighbor_advert *)(void *)icmp6;
8422 
8423 		if (icmp6len < sizeof(*nd_na)) {
8424 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8425 			    "short nd_na %d < %lu",
8426 			    icmp6len, sizeof(*nd_na));
8427 			return;
8428 		}
8429 
8430 		bcopy(&nd_na->nd_na_target, &taddr, sizeof(taddr));
8431 		if (IN6_IS_ADDR_MULTICAST(&taddr) ||
8432 		    IN6_IS_ADDR_UNSPECIFIED(&taddr)) {
8433 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8434 			    "invalid target ignored");
8435 			return;
8436 		}
8437 		/* parse options */
8438 		nd6_option_init(nd_na + 1, icmp6len - sizeof(*nd_na), &ndopts);
8439 		if (nd6_options(&ndopts) < 0) {
8440 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8441 			    "invalid ND6 NA option");
8442 			return;
8443 		}
8444 		if (ndopts.nd_opts_tgt_lladdr == NULL) {
8445 			/* target linklayer, nothing to do */
8446 			return;
8447 		}
8448 		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
8449 		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
8450 		if (lladdrlen != ETHER_ND_LLADDR_LEN) {
8451 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8452 			    "target lladdrlen %d != %lu",
8453 			    lladdrlen, ETHER_ND_LLADDR_LEN);
8454 			return;
8455 		}
8456 		break;
8457 	}
8458 	case ND_ROUTER_ADVERT:
8459 	case ND_ROUTER_SOLICIT: {
8460 		union nd_opts ndopts;
8461 		uint32_t type_length;
8462 		const char *description;
8463 
8464 		if (icmp6_type == ND_ROUTER_ADVERT) {
8465 			type_length = sizeof(struct nd_router_advert);
8466 			description = "RA";
8467 		} else {
8468 			type_length = sizeof(struct nd_router_solicit);
8469 			description = "RS";
8470 		}
8471 		if (icmp6len < type_length) {
8472 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8473 			    "short ND6 %s %d < %d",
8474 			    description, icmp6len, type_length);
8475 			return;
8476 		}
8477 		/* parse options */
8478 		nd6_option_init(((uint8_t *)icmp6) + type_length,
8479 		    icmp6len - type_length, &ndopts);
8480 		if (nd6_options(&ndopts) < 0) {
8481 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8482 			    "invalid ND6 %s option", description);
8483 			return;
8484 		}
8485 		if (ndopts.nd_opts_src_lladdr != NULL) {
8486 			lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
8487 			lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
8488 			if (lladdrlen != ETHER_ND_LLADDR_LEN) {
8489 				BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8490 				    "source lladdrlen %d != %lu",
8491 				    lladdrlen, ETHER_ND_LLADDR_LEN);
8492 				return;
8493 			}
8494 		}
8495 		break;
8496 	}
8497 	default:
8498 		break;
8499 	}
8500 	if (lladdr != NULL) {
8501 		mnr->mnr_ip6_lladdr_offset = (uint16_t)
8502 		    ((uintptr_t)lladdr - (uintptr_t)eh);
8503 		mnr->mnr_ip6_icmp6_len = icmp6len;
8504 		mnr->mnr_ip6_icmp6_type = icmp6_type;
8505 		mnr->mnr_ip6_header_len = off;
8506 		if (BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) {
8507 			const char *str;
8508 
8509 			switch (mnr->mnr_ip6_icmp6_type) {
8510 			case ND_ROUTER_ADVERT:
8511 				str = "ROUTER ADVERT";
8512 				break;
8513 			case ND_ROUTER_SOLICIT:
8514 				str = "ROUTER SOLICIT";
8515 				break;
8516 			case ND_NEIGHBOR_ADVERT:
8517 				str = "NEIGHBOR ADVERT";
8518 				break;
8519 			case ND_NEIGHBOR_SOLICIT:
8520 				str = "NEIGHBOR SOLICIT";
8521 				break;
8522 			default:
8523 				str = "";
8524 				break;
8525 			}
8526 			BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT,
8527 			    "%s %s %s ip6len %d icmp6len %d lladdr offset %d",
8528 			    sc->sc_if_xname, bif->bif_ifp->if_xname, str,
8529 			    mnr->mnr_ip6_header_len,
8530 			    mnr->mnr_ip6_icmp6_len, mnr->mnr_ip6_lladdr_offset);
8531 		}
8532 	}
8533 }
8534 
8535 static struct mac_nat_entry *
8536 bridge_mac_nat_ipv6_input(struct bridge_softc *sc, mbuf_t *data)
8537 {
8538 	struct in6_addr         dst;
8539 	struct ether_header     *eh;
8540 	struct ip6_hdr          *ip6h;
8541 	struct mac_nat_entry    *mne = NULL;
8542 
8543 	eh = get_ether_ipv6_header(data, 0, FALSE);
8544 	if (eh == NULL) {
8545 		goto done;
8546 	}
8547 	ip6h = (struct ip6_hdr *)(void *)(eh + 1);
8548 	bcopy(&ip6h->ip6_dst, &dst, sizeof(dst));
8549 	/* XXX validate IPv6 address */
8550 	if (IN6_IS_ADDR_UNSPECIFIED(&dst)) {
8551 		goto done;
8552 	}
8553 	mne = bridge_lookup_mac_nat_entry(sc, AF_INET6, &dst);
8554 
8555 done:
8556 	return mne;
8557 }
8558 
8559 static boolean_t
8560 bridge_mac_nat_ipv6_output(struct bridge_softc *sc,
8561     struct bridge_iflist *bif, mbuf_t *data, struct mac_nat_record *mnr)
8562 {
8563 	struct ether_header     *eh;
8564 	ether_addr_t            ether_shost;
8565 	struct ip6_hdr          *ip6h;
8566 	struct in6_addr         saddr;
8567 	boolean_t               translate;
8568 
8569 	translate = (bif == sc->sc_mac_nat_bif) ? FALSE : TRUE;
8570 	eh = get_ether_ipv6_header(data, 0, TRUE);
8571 	if (eh == NULL) {
8572 		translate = FALSE;
8573 		goto done;
8574 	}
8575 	bcopy(eh->ether_shost, &ether_shost, sizeof(ether_shost));
8576 	ip6h = (struct ip6_hdr *)(void *)(eh + 1);
8577 	bcopy(&ip6h->ip6_src, &saddr, sizeof(saddr));
8578 	if (mnr != NULL && ip6h->ip6_nxt == IPPROTO_ICMPV6) {
8579 		bridge_mac_nat_icmpv6_output(sc, bif, data, ip6h, &saddr, mnr);
8580 	}
8581 	if (IN6_IS_ADDR_UNSPECIFIED(&saddr)) {
8582 		goto done;
8583 	}
8584 	(void)bridge_update_mac_nat_entry(sc, bif, AF_INET6, &saddr,
8585 	    ether_shost.octet);
8586 
8587 done:
8588 	return translate;
8589 }
8590 
8591 /*
8592  * bridge_mac_nat_input:
8593  * Process a packet arriving on the MAC NAT interface (sc_mac_nat_bif).
8594  * This interface is the "external" interface with respect to NAT.
8595  * The interface is only capable of receiving a single MAC address
8596  * (e.g. a Wi-Fi STA interface).
8597  *
8598  * When a packet arrives on the external interface, look up the destination
8599  * IP address in the mac_nat_entry table. If there is a match, *is_input
8600  * is set to TRUE if it's for the MAC NAT interface, otherwise *is_input
8601  * is set to FALSE and translate the MAC address if necessary.
8602  *
8603  * Returns:
8604  * The internal interface to direct the packet to, or NULL if the packet
8605  * should not be redirected.
8606  *
8607  * *data may be updated to point at a different mbuf chain, or set to NULL
8608  * if the chain was deallocated during processing.
8609  */
8610 static ifnet_t
8611 bridge_mac_nat_input(struct bridge_softc *sc, mbuf_t *data,
8612     boolean_t *is_input)
8613 {
8614 	ifnet_t                 dst_if = NULL;
8615 	struct ether_header     *eh;
8616 	uint16_t                ether_type;
8617 	boolean_t               is_unicast;
8618 	mbuf_t                  m = *data;
8619 	struct mac_nat_entry    *mne = NULL;
8620 
8621 	BRIDGE_LOCK_ASSERT_HELD(sc);
8622 	*is_input = FALSE;
8623 	assert(sc->sc_mac_nat_bif != NULL);
8624 	is_unicast = ((m->m_flags & (M_BCAST | M_MCAST)) == 0);
8625 	eh = mtod(m, struct ether_header *);
8626 	ether_type = ntohs(eh->ether_type);
8627 	switch (ether_type) {
8628 	case ETHERTYPE_ARP:
8629 		mne = bridge_mac_nat_arp_input(sc, data);
8630 		break;
8631 	case ETHERTYPE_IP:
8632 		if (is_unicast) {
8633 			mne = bridge_mac_nat_ip_input(sc, data);
8634 		}
8635 		break;
8636 	case ETHERTYPE_IPV6:
8637 		if (is_unicast) {
8638 			mne = bridge_mac_nat_ipv6_input(sc, data);
8639 		}
8640 		break;
8641 	default:
8642 		break;
8643 	}
8644 	if (mne != NULL) {
8645 		if (is_unicast) {
8646 			if (m != *data) {
8647 				/* it may have changed */
8648 				eh = mtod(*data, struct ether_header *);
8649 			}
8650 			bcopy(mne->mne_mac, eh->ether_dhost,
8651 			    sizeof(eh->ether_dhost));
8652 		}
8653 		dst_if = mne->mne_bif->bif_ifp;
8654 		*is_input = (mne->mne_bif == sc->sc_mac_nat_bif);
8655 	}
8656 	return dst_if;
8657 }
8658 
8659 /*
8660  * bridge_mac_nat_output:
8661  * Process a packet destined to the MAC NAT interface (sc_mac_nat_bif)
8662  * from the interface 'bif'.
8663  *
8664  * Create a mac_nat_entry containing the source IP address and MAC address
8665  * from the packet. Populate a mac_nat_record with information detailing
8666  * how to translate the packet. Translation takes place later when
8667  * the bridge lock is no longer held.
8668  *
8669  * If 'bif' == sc_mac_nat_bif, the stack over the MAC NAT
8670  * interface is generating an output packet. No translation is required in this
8671  * case, we just record the IP address used to prevent another bif from
8672  * claiming our IP address.
8673  *
8674  * Returns:
8675  * TRUE if the packet should be translated (*mnr updated as well),
8676  * FALSE otherwise.
8677  *
8678  * *data may be updated to point at a different mbuf chain or NULL if
8679  * the chain was deallocated during processing.
8680  */
8681 
8682 static boolean_t
8683 bridge_mac_nat_output(struct bridge_softc *sc,
8684     struct bridge_iflist *bif, mbuf_t *data, struct mac_nat_record *mnr)
8685 {
8686 	struct ether_header     *eh;
8687 	uint16_t                ether_type;
8688 	boolean_t               translate = FALSE;
8689 
8690 	BRIDGE_LOCK_ASSERT_HELD(sc);
8691 	assert(sc->sc_mac_nat_bif != NULL);
8692 
8693 	eh = mtod(*data, struct ether_header *);
8694 	ether_type = ntohs(eh->ether_type);
8695 	if (mnr != NULL) {
8696 		bzero(mnr, sizeof(*mnr));
8697 		mnr->mnr_ether_type = ether_type;
8698 	}
8699 	switch (ether_type) {
8700 	case ETHERTYPE_ARP:
8701 		translate = bridge_mac_nat_arp_output(sc, bif, data, mnr);
8702 		break;
8703 	case ETHERTYPE_IP:
8704 		translate = bridge_mac_nat_ip_output(sc, bif, data, mnr);
8705 		break;
8706 	case ETHERTYPE_IPV6:
8707 		translate = bridge_mac_nat_ipv6_output(sc, bif, data, mnr);
8708 		break;
8709 	default:
8710 		break;
8711 	}
8712 	return translate;
8713 }
8714 
8715 static void
8716 bridge_mac_nat_arp_translate(mbuf_t *data, struct mac_nat_record *mnr,
8717     const caddr_t eaddr)
8718 {
8719 	errno_t                 error;
8720 
8721 	if (mnr->mnr_arp_offset == 0) {
8722 		return;
8723 	}
8724 	/* replace the source hardware address */
8725 	error = mbuf_copyback(*data, mnr->mnr_arp_offset,
8726 	    ETHER_ADDR_LEN, eaddr,
8727 	    MBUF_DONTWAIT);
8728 	if (error != 0) {
8729 		BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT,
8730 		    "mbuf_copyback failed");
8731 		m_freem(*data);
8732 		*data = NULL;
8733 	}
8734 	return;
8735 }
8736 
8737 static void
8738 bridge_mac_nat_ip_translate(mbuf_t *data, struct mac_nat_record *mnr)
8739 {
8740 	errno_t         error;
8741 	size_t          offset;
8742 
8743 	if (mnr->mnr_ip_header_len == 0) {
8744 		return;
8745 	}
8746 	/* update the UDP checksum */
8747 	offset = sizeof(struct ether_header) + mnr->mnr_ip_header_len;
8748 	error = mbuf_copyback(*data, offset + offsetof(struct udphdr, uh_sum),
8749 	    sizeof(mnr->mnr_ip_udp_csum),
8750 	    &mnr->mnr_ip_udp_csum,
8751 	    MBUF_DONTWAIT);
8752 	if (error != 0) {
8753 		BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT,
8754 		    "mbuf_copyback uh_sum failed");
8755 		m_freem(*data);
8756 		*data = NULL;
8757 	}
8758 	/* update the DHCP must broadcast flag */
8759 	offset += sizeof(struct udphdr);
8760 	error = mbuf_copyback(*data, offset + offsetof(struct dhcp, dp_flags),
8761 	    sizeof(mnr->mnr_ip_dhcp_flags),
8762 	    &mnr->mnr_ip_dhcp_flags,
8763 	    MBUF_DONTWAIT);
8764 	if (error != 0) {
8765 		BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT,
8766 		    "mbuf_copyback dp_flags failed");
8767 		m_freem(*data);
8768 		*data = NULL;
8769 	}
8770 }
8771 
8772 static void
8773 bridge_mac_nat_ipv6_translate(mbuf_t *data, struct mac_nat_record *mnr,
8774     const caddr_t eaddr)
8775 {
8776 	uint16_t        cksum;
8777 	errno_t         error;
8778 	mbuf_t          m = *data;
8779 
8780 	if (mnr->mnr_ip6_header_len == 0) {
8781 		return;
8782 	}
8783 	switch (mnr->mnr_ip6_icmp6_type) {
8784 	case ND_ROUTER_ADVERT:
8785 	case ND_ROUTER_SOLICIT:
8786 	case ND_NEIGHBOR_SOLICIT:
8787 	case ND_NEIGHBOR_ADVERT:
8788 		if (mnr->mnr_ip6_lladdr_offset == 0) {
8789 			/* nothing to do */
8790 			return;
8791 		}
8792 		break;
8793 	default:
8794 		return;
8795 	}
8796 
8797 	/*
8798 	 * replace the lladdr
8799 	 */
8800 	error = mbuf_copyback(m, mnr->mnr_ip6_lladdr_offset,
8801 	    ETHER_ADDR_LEN, eaddr,
8802 	    MBUF_DONTWAIT);
8803 	if (error != 0) {
8804 		BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT,
8805 		    "mbuf_copyback lladdr failed");
8806 		m_freem(m);
8807 		*data = NULL;
8808 		return;
8809 	}
8810 
8811 	/*
8812 	 * recompute the icmp6 checksum
8813 	 */
8814 
8815 	/* skip past the ethernet header */
8816 	mbuf_setdata(m, (char *)mbuf_data(m) + ETHER_HDR_LEN,
8817 	    mbuf_len(m) - ETHER_HDR_LEN);
8818 	mbuf_pkthdr_adjustlen(m, -ETHER_HDR_LEN);
8819 
8820 #define CKSUM_OFFSET_ICMP6      offsetof(struct icmp6_hdr, icmp6_cksum)
8821 	/* set the checksum to zero */
8822 	cksum = 0;
8823 	error = mbuf_copyback(m, mnr->mnr_ip6_header_len + CKSUM_OFFSET_ICMP6,
8824 	    sizeof(cksum), &cksum, MBUF_DONTWAIT);
8825 	if (error != 0) {
8826 		BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT,
8827 		    "mbuf_copyback cksum=0 failed");
8828 		m_freem(m);
8829 		*data = NULL;
8830 		return;
8831 	}
8832 	/* compute and set the new checksum */
8833 	cksum = in6_cksum(m, IPPROTO_ICMPV6, mnr->mnr_ip6_header_len,
8834 	    mnr->mnr_ip6_icmp6_len);
8835 	error = mbuf_copyback(m, mnr->mnr_ip6_header_len + CKSUM_OFFSET_ICMP6,
8836 	    sizeof(cksum), &cksum, MBUF_DONTWAIT);
8837 	if (error != 0) {
8838 		BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT,
8839 		    "mbuf_copyback cksum failed");
8840 		m_freem(m);
8841 		*data = NULL;
8842 		return;
8843 	}
8844 	/* restore the ethernet header */
8845 	mbuf_setdata(m, (char *)mbuf_data(m) - ETHER_HDR_LEN,
8846 	    mbuf_len(m) + ETHER_HDR_LEN);
8847 	mbuf_pkthdr_adjustlen(m, ETHER_HDR_LEN);
8848 	return;
8849 }
8850 
8851 static void
8852 bridge_mac_nat_translate(mbuf_t *data, struct mac_nat_record *mnr,
8853     const caddr_t eaddr)
8854 {
8855 	struct ether_header     *eh;
8856 
8857 	/* replace the source ethernet address with the single MAC */
8858 	eh = mtod(*data, struct ether_header *);
8859 	bcopy(eaddr, eh->ether_shost, sizeof(eh->ether_shost));
8860 	switch (mnr->mnr_ether_type) {
8861 	case ETHERTYPE_ARP:
8862 		bridge_mac_nat_arp_translate(data, mnr, eaddr);
8863 		break;
8864 
8865 	case ETHERTYPE_IP:
8866 		bridge_mac_nat_ip_translate(data, mnr);
8867 		break;
8868 
8869 	case ETHERTYPE_IPV6:
8870 		bridge_mac_nat_ipv6_translate(data, mnr, eaddr);
8871 		break;
8872 
8873 	default:
8874 		break;
8875 	}
8876 	return;
8877 }
8878 
8879 /*
8880  * bridge packet filtering
8881  */
8882 
8883 /*
8884  * Perform basic checks on header size since
8885  * pfil assumes ip_input has already processed
8886  * it for it.  Cut-and-pasted from ip_input.c.
8887  * Given how simple the IPv6 version is,
8888  * does the IPv4 version really need to be
8889  * this complicated?
8890  *
8891  * XXX Should we update ipstat here, or not?
8892  * XXX Right now we update ipstat but not
8893  * XXX csum_counter.
8894  */
8895 static int
8896 bridge_ip_checkbasic(struct mbuf **mp)
8897 {
8898 	struct mbuf *m = *mp;
8899 	struct ip *ip;
8900 	int len, hlen;
8901 	u_short sum;
8902 
8903 	if (*mp == NULL) {
8904 		return -1;
8905 	}
8906 
8907 	if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
8908 		/* max_linkhdr is already rounded up to nearest 4-byte */
8909 		if ((m = m_copyup(m, sizeof(struct ip),
8910 		    max_linkhdr)) == NULL) {
8911 			/* XXXJRT new stat, please */
8912 			ipstat.ips_toosmall++;
8913 			goto bad;
8914 		}
8915 	} else if (OS_EXPECT((size_t)m->m_len < sizeof(struct ip), 0)) {
8916 		if ((m = m_pullup(m, sizeof(struct ip))) == NULL) {
8917 			ipstat.ips_toosmall++;
8918 			goto bad;
8919 		}
8920 	}
8921 	ip = mtod(m, struct ip *);
8922 	if (ip == NULL) {
8923 		goto bad;
8924 	}
8925 
8926 	if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
8927 		ipstat.ips_badvers++;
8928 		goto bad;
8929 	}
8930 	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
8931 	if (hlen < (int)sizeof(struct ip)) {  /* minimum header length */
8932 		ipstat.ips_badhlen++;
8933 		goto bad;
8934 	}
8935 	if (hlen > m->m_len) {
8936 		if ((m = m_pullup(m, hlen)) == 0) {
8937 			ipstat.ips_badhlen++;
8938 			goto bad;
8939 		}
8940 		ip = mtod(m, struct ip *);
8941 		if (ip == NULL) {
8942 			goto bad;
8943 		}
8944 	}
8945 
8946 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
8947 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
8948 	} else {
8949 		if (hlen == sizeof(struct ip)) {
8950 			sum = in_cksum_hdr(ip);
8951 		} else {
8952 			sum = in_cksum(m, hlen);
8953 		}
8954 	}
8955 	if (sum) {
8956 		ipstat.ips_badsum++;
8957 		goto bad;
8958 	}
8959 
8960 	/* Retrieve the packet length. */
8961 	len = ntohs(ip->ip_len);
8962 
8963 	/*
8964 	 * Check for additional length bogosity
8965 	 */
8966 	if (len < hlen) {
8967 		ipstat.ips_badlen++;
8968 		goto bad;
8969 	}
8970 
8971 	/*
8972 	 * Check that the amount of data in the buffers
8973 	 * is as at least much as the IP header would have us expect.
8974 	 * Drop packet if shorter than we expect.
8975 	 */
8976 	if (m->m_pkthdr.len < len) {
8977 		ipstat.ips_tooshort++;
8978 		goto bad;
8979 	}
8980 
8981 	/* Checks out, proceed */
8982 	*mp = m;
8983 	return 0;
8984 
8985 bad:
8986 	*mp = m;
8987 	return -1;
8988 }
8989 
8990 /*
8991  * Same as above, but for IPv6.
8992  * Cut-and-pasted from ip6_input.c.
8993  * XXX Should we update ip6stat, or not?
8994  */
8995 static int
8996 bridge_ip6_checkbasic(struct mbuf **mp)
8997 {
8998 	struct mbuf *m = *mp;
8999 	struct ip6_hdr *ip6;
9000 
9001 	/*
9002 	 * If the IPv6 header is not aligned, slurp it up into a new
9003 	 * mbuf with space for link headers, in the event we forward
9004 	 * it.  Otherwise, if it is aligned, make sure the entire base
9005 	 * IPv6 header is in the first mbuf of the chain.
9006 	 */
9007 	if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
9008 		struct ifnet *inifp = m->m_pkthdr.rcvif;
9009 		/* max_linkhdr is already rounded up to nearest 4-byte */
9010 		if ((m = m_copyup(m, sizeof(struct ip6_hdr),
9011 		    max_linkhdr)) == NULL) {
9012 			/* XXXJRT new stat, please */
9013 			ip6stat.ip6s_toosmall++;
9014 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
9015 			goto bad;
9016 		}
9017 	} else if (OS_EXPECT((size_t)m->m_len < sizeof(struct ip6_hdr), 0)) {
9018 		struct ifnet *inifp = m->m_pkthdr.rcvif;
9019 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
9020 			ip6stat.ip6s_toosmall++;
9021 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
9022 			goto bad;
9023 		}
9024 	}
9025 
9026 	ip6 = mtod(m, struct ip6_hdr *);
9027 
9028 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
9029 		ip6stat.ip6s_badvers++;
9030 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
9031 		goto bad;
9032 	}
9033 
9034 	/* Checks out, proceed */
9035 	*mp = m;
9036 	return 0;
9037 
9038 bad:
9039 	*mp = m;
9040 	return -1;
9041 }
9042 
9043 /*
9044  * the PF routines expect to be called from ip_input, so we
9045  * need to do and undo here some of the same processing.
9046  *
9047  * XXX : this is heavily inspired on bridge_pfil()
9048  */
9049 static int
9050 bridge_pf(struct mbuf **mp, struct ifnet *ifp, uint32_t sc_filter_flags,
9051     int input)
9052 {
9053 	/*
9054 	 * XXX : mpetit : heavily inspired by bridge_pfil()
9055 	 */
9056 
9057 	int snap, error, i, hlen;
9058 	struct ether_header *eh1, eh2;
9059 	struct ip *ip;
9060 	struct llc llc1;
9061 	u_int16_t ether_type;
9062 
9063 	snap = 0;
9064 	error = -1;     /* Default error if not error == 0 */
9065 
9066 	if ((sc_filter_flags & IFBF_FILT_MEMBER) == 0) {
9067 		return 0; /* filtering is disabled */
9068 	}
9069 	i = min((*mp)->m_pkthdr.len, max_protohdr);
9070 	if ((*mp)->m_len < i) {
9071 		*mp = m_pullup(*mp, i);
9072 		if (*mp == NULL) {
9073 			BRIDGE_LOG(LOG_NOTICE, 0, "m_pullup failed");
9074 			return -1;
9075 		}
9076 	}
9077 
9078 	eh1 = mtod(*mp, struct ether_header *);
9079 	ether_type = ntohs(eh1->ether_type);
9080 
9081 	/*
9082 	 * Check for SNAP/LLC.
9083 	 */
9084 	if (ether_type < ETHERMTU) {
9085 		struct llc *llc2 = (struct llc *)(eh1 + 1);
9086 
9087 		if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
9088 		    llc2->llc_dsap == LLC_SNAP_LSAP &&
9089 		    llc2->llc_ssap == LLC_SNAP_LSAP &&
9090 		    llc2->llc_control == LLC_UI) {
9091 			ether_type = htons(llc2->llc_un.type_snap.ether_type);
9092 			snap = 1;
9093 		}
9094 	}
9095 
9096 	/*
9097 	 * If we're trying to filter bridge traffic, don't look at anything
9098 	 * other than IP and ARP traffic.  If the filter doesn't understand
9099 	 * IPv6, don't allow IPv6 through the bridge either.  This is lame
9100 	 * since if we really wanted, say, an AppleTalk filter, we are hosed,
9101 	 * but of course we don't have an AppleTalk filter to begin with.
9102 	 * (Note that since pfil doesn't understand ARP it will pass *ALL*
9103 	 * ARP traffic.)
9104 	 */
9105 	switch (ether_type) {
9106 	case ETHERTYPE_ARP:
9107 	case ETHERTYPE_REVARP:
9108 		return 0;         /* Automatically pass */
9109 
9110 	case ETHERTYPE_IP:
9111 	case ETHERTYPE_IPV6:
9112 		break;
9113 	default:
9114 		/*
9115 		 * Check to see if the user wants to pass non-ip
9116 		 * packets, these will not be checked by pf and
9117 		 * passed unconditionally so the default is to drop.
9118 		 */
9119 		if ((sc_filter_flags & IFBF_FILT_ONLYIP)) {
9120 			goto bad;
9121 		}
9122 		break;
9123 	}
9124 
9125 	/* Strip off the Ethernet header and keep a copy. */
9126 	m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t)&eh2);
9127 	m_adj(*mp, ETHER_HDR_LEN);
9128 
9129 	/* Strip off snap header, if present */
9130 	if (snap) {
9131 		m_copydata(*mp, 0, sizeof(struct llc), (caddr_t)&llc1);
9132 		m_adj(*mp, sizeof(struct llc));
9133 	}
9134 
9135 	/*
9136 	 * Check the IP header for alignment and errors
9137 	 */
9138 	switch (ether_type) {
9139 	case ETHERTYPE_IP:
9140 		error = bridge_ip_checkbasic(mp);
9141 		break;
9142 	case ETHERTYPE_IPV6:
9143 		error = bridge_ip6_checkbasic(mp);
9144 		break;
9145 	default:
9146 		error = 0;
9147 		break;
9148 	}
9149 	if (error) {
9150 		goto bad;
9151 	}
9152 
9153 	error = 0;
9154 
9155 	/*
9156 	 * Run the packet through pf rules
9157 	 */
9158 	switch (ether_type) {
9159 	case ETHERTYPE_IP:
9160 		/*
9161 		 * before calling the firewall, swap fields the same as
9162 		 * IP does. here we assume the header is contiguous
9163 		 */
9164 		ip = mtod(*mp, struct ip *);
9165 
9166 		ip->ip_len = ntohs(ip->ip_len);
9167 		ip->ip_off = ntohs(ip->ip_off);
9168 
9169 		if (ifp != NULL) {
9170 			error = pf_af_hook(ifp, 0, mp, AF_INET, input, NULL);
9171 		}
9172 
9173 		if (*mp == NULL || error != 0) { /* filter may consume */
9174 			break;
9175 		}
9176 
9177 		/* Recalculate the ip checksum and restore byte ordering */
9178 		ip = mtod(*mp, struct ip *);
9179 		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
9180 		if (hlen < (int)sizeof(struct ip)) {
9181 			goto bad;
9182 		}
9183 		if (hlen > (*mp)->m_len) {
9184 			if ((*mp = m_pullup(*mp, hlen)) == 0) {
9185 				goto bad;
9186 			}
9187 			ip = mtod(*mp, struct ip *);
9188 			if (ip == NULL) {
9189 				goto bad;
9190 			}
9191 		}
9192 		ip->ip_len = htons(ip->ip_len);
9193 		ip->ip_off = htons(ip->ip_off);
9194 		ip->ip_sum = 0;
9195 		if (hlen == sizeof(struct ip)) {
9196 			ip->ip_sum = in_cksum_hdr(ip);
9197 		} else {
9198 			ip->ip_sum = in_cksum(*mp, hlen);
9199 		}
9200 		break;
9201 
9202 	case ETHERTYPE_IPV6:
9203 		if (ifp != NULL) {
9204 			error = pf_af_hook(ifp, 0, mp, AF_INET6, input, NULL);
9205 		}
9206 
9207 		if (*mp == NULL || error != 0) { /* filter may consume */
9208 			break;
9209 		}
9210 		break;
9211 	default:
9212 		error = 0;
9213 		break;
9214 	}
9215 
9216 	if (*mp == NULL) {
9217 		return error;
9218 	}
9219 	if (error != 0) {
9220 		goto bad;
9221 	}
9222 
9223 	error = -1;
9224 
9225 	/*
9226 	 * Finally, put everything back the way it was and return
9227 	 */
9228 	if (snap) {
9229 		M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT, 0);
9230 		if (*mp == NULL) {
9231 			return error;
9232 		}
9233 		bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
9234 	}
9235 
9236 	M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT, 0);
9237 	if (*mp == NULL) {
9238 		return error;
9239 	}
9240 	bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
9241 
9242 	return 0;
9243 
9244 bad:
9245 	m_freem(*mp);
9246 	*mp = NULL;
9247 	return error;
9248 }
9249 
9250 /*
9251  * Copyright (C) 2014, Stefano Garzarella - Universita` di Pisa.
9252  * All rights reserved.
9253  *
9254  * Redistribution and use in source and binary forms, with or without
9255  * modification, are permitted provided that the following conditions
9256  * are met:
9257  *   1. Redistributions of source code must retain the above copyright
9258  *      notice, this list of conditions and the following disclaimer.
9259  *   2. Redistributions in binary form must reproduce the above copyright
9260  *      notice, this list of conditions and the following disclaimer in the
9261  *      documentation and/or other materials provided with the distribution.
9262  *
9263  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
9264  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9265  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9266  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
9267  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
9268  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
9269  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
9270  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
9271  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
9272  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
9273  * SUCH DAMAGE.
9274  */
9275 
9276 /*
9277  * XXX-ste: Maybe this function must be moved into kern/uipc_mbuf.c
9278  *
9279  * Create a queue of packets/segments which fit the given mss + hdr_len.
9280  * m0 points to mbuf chain to be segmented.
9281  * This function splits the payload (m0-> m_pkthdr.len - hdr_len)
9282  * into segments of length MSS bytes and then copy the first hdr_len bytes
9283  * from m0 at the top of each segment.
9284  * If hdr2_buf is not NULL (hdr2_len is the buf length), it is copied
9285  * in each segment after the first hdr_len bytes
9286  *
9287  * Return the new queue with the segments on success, NULL on failure.
9288  * (the mbuf queue is freed in this case).
9289  * nsegs contains the number of segments generated.
9290  */
9291 
9292 static struct mbuf *
9293 m_seg(struct mbuf *m0, int hdr_len, int mss, int *nsegs,
9294     char * hdr2_buf, int hdr2_len)
9295 {
9296 	int off = 0, n, firstlen;
9297 	struct mbuf **mnext, *mseg;
9298 	int total_len = m0->m_pkthdr.len;
9299 
9300 	/*
9301 	 * Segmentation useless
9302 	 */
9303 	if (total_len <= hdr_len + mss) {
9304 		return m0;
9305 	}
9306 
9307 	if (hdr2_buf == NULL || hdr2_len <= 0) {
9308 		hdr2_buf = NULL;
9309 		hdr2_len = 0;
9310 	}
9311 
9312 	off = hdr_len + mss;
9313 	firstlen = mss; /* first segment stored in the original mbuf */
9314 
9315 	mnext = &(m0->m_nextpkt); /* pointer to next packet */
9316 
9317 	for (n = 1; off < total_len; off += mss, n++) {
9318 		struct mbuf *m;
9319 		/*
9320 		 * Copy the header from the original packet
9321 		 * and create a new mbuf chain
9322 		 */
9323 		if (MHLEN < hdr_len) {
9324 			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
9325 		} else {
9326 			m = m_gethdr(M_NOWAIT, MT_DATA);
9327 		}
9328 
9329 		if (m == NULL) {
9330 #ifdef GSO_DEBUG
9331 			D("MGETHDR error\n");
9332 #endif
9333 			goto err;
9334 		}
9335 
9336 		m_copydata(m0, 0, hdr_len, mtod(m, caddr_t));
9337 
9338 		m->m_len = hdr_len;
9339 		/*
9340 		 * if the optional header is present, copy it
9341 		 */
9342 		if (hdr2_buf != NULL) {
9343 			m_copyback(m, hdr_len, hdr2_len, hdr2_buf);
9344 		}
9345 
9346 		m->m_flags |= (m0->m_flags & M_COPYFLAGS);
9347 		if (off + mss >= total_len) {           /* last segment */
9348 			mss = total_len - off;
9349 		}
9350 		/*
9351 		 * Copy the payload from original packet
9352 		 */
9353 		mseg = m_copym(m0, off, mss, M_NOWAIT);
9354 		if (mseg == NULL) {
9355 			m_freem(m);
9356 #ifdef GSO_DEBUG
9357 			D("m_copym error\n");
9358 #endif
9359 			goto err;
9360 		}
9361 		m_cat(m, mseg);
9362 
9363 		m->m_pkthdr.len = hdr_len + hdr2_len + mss;
9364 		m->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
9365 		/*
9366 		 * Copy the checksum flags and data (in_cksum() need this)
9367 		 */
9368 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
9369 		m->m_pkthdr.csum_data = m0->m_pkthdr.csum_data;
9370 		m->m_pkthdr.tso_segsz = m0->m_pkthdr.tso_segsz;
9371 
9372 		*mnext = m;
9373 		mnext = &(m->m_nextpkt);
9374 	}
9375 
9376 	/*
9377 	 * Update first segment.
9378 	 * If the optional header is present, is necessary
9379 	 * to insert it into the first segment.
9380 	 */
9381 	if (hdr2_buf == NULL) {
9382 		m_adj(m0, hdr_len + firstlen - total_len);
9383 		m0->m_pkthdr.len = hdr_len + firstlen;
9384 	} else {
9385 		mseg = m_copym(m0, hdr_len, firstlen, M_NOWAIT);
9386 		if (mseg == NULL) {
9387 #ifdef GSO_DEBUG
9388 			D("m_copym error\n");
9389 #endif
9390 			goto err;
9391 		}
9392 		m_adj(m0, hdr_len - total_len);
9393 		m_copyback(m0, hdr_len, hdr2_len, hdr2_buf);
9394 		m_cat(m0, mseg);
9395 		m0->m_pkthdr.len = hdr_len + hdr2_len + firstlen;
9396 	}
9397 
9398 	if (nsegs != NULL) {
9399 		*nsegs = n;
9400 	}
9401 	return m0;
9402 err:
9403 	while (m0 != NULL) {
9404 		mseg = m0->m_nextpkt;
9405 		m0->m_nextpkt = NULL;
9406 		m_freem(m0);
9407 		m0 = mseg;
9408 	}
9409 	return NULL;
9410 }
9411 
9412 /*
9413  * Wrappers of IPv4 checksum functions
9414  */
9415 static inline void
9416 gso_ipv4_data_cksum(struct mbuf *m, struct ip *ip, int mac_hlen)
9417 {
9418 	m->m_data += mac_hlen;
9419 	m->m_len -= mac_hlen;
9420 	m->m_pkthdr.len -= mac_hlen;
9421 #if __FreeBSD_version < 1000000
9422 	ip->ip_len = ntohs(ip->ip_len); /* needed for in_delayed_cksum() */
9423 #endif
9424 
9425 	in_delayed_cksum(m);
9426 
9427 #if __FreeBSD_version < 1000000
9428 	ip->ip_len = htons(ip->ip_len);
9429 #endif
9430 	m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
9431 	m->m_len += mac_hlen;
9432 	m->m_pkthdr.len += mac_hlen;
9433 	m->m_data -= mac_hlen;
9434 }
9435 
9436 static inline void
9437 gso_ipv4_hdr_cksum(struct mbuf *m, struct ip *ip, int mac_hlen, int ip_hlen)
9438 {
9439 	m->m_data += mac_hlen;
9440 
9441 	ip->ip_sum = in_cksum(m, ip_hlen);
9442 
9443 	m->m_pkthdr.csum_flags &= ~CSUM_IP;
9444 	m->m_data -= mac_hlen;
9445 }
9446 
9447 /*
9448  * Structure that contains the state during the TCP segmentation
9449  */
9450 struct gso_ip_tcp_state {
9451 	void    (*update)
9452 	(struct gso_ip_tcp_state*, struct mbuf*);
9453 	void    (*internal)
9454 	(struct gso_ip_tcp_state*, struct mbuf*);
9455 	union iphdr hdr;
9456 	struct tcphdr *tcp;
9457 	int mac_hlen;
9458 	int ip_hlen;
9459 	int tcp_hlen;
9460 	int hlen;
9461 	int pay_len;
9462 	int sw_csum;
9463 	uint32_t tcp_seq;
9464 	uint16_t ip_id;
9465 	boolean_t is_tx;
9466 };
9467 
9468 /*
9469  * Update the pointers to TCP and IPv4 headers
9470  */
9471 static inline void
9472 gso_ipv4_tcp_update(struct gso_ip_tcp_state *state, struct mbuf *m)
9473 {
9474 	state->hdr.ip = (struct ip *)(void *)(mtod(m, uint8_t *) + state->mac_hlen);
9475 	state->tcp = (struct tcphdr *)(void *)((caddr_t)(state->hdr.ip) + state->ip_hlen);
9476 	state->pay_len = m->m_pkthdr.len - state->hlen;
9477 }
9478 
9479 /*
9480  * Set properly the TCP and IPv4 headers
9481  */
9482 static inline void
9483 gso_ipv4_tcp_internal(struct gso_ip_tcp_state *state, struct mbuf *m)
9484 {
9485 	/*
9486 	 * Update IP header
9487 	 */
9488 	state->hdr.ip->ip_id = htons((state->ip_id)++);
9489 	state->hdr.ip->ip_len = htons(m->m_pkthdr.len - state->mac_hlen);
9490 	/*
9491 	 * TCP Checksum
9492 	 */
9493 	state->tcp->th_sum = 0;
9494 	state->tcp->th_sum = in_pseudo(state->hdr.ip->ip_src.s_addr,
9495 	    state->hdr.ip->ip_dst.s_addr,
9496 	    htons(state->tcp_hlen + IPPROTO_TCP + state->pay_len));
9497 	/*
9498 	 * Checksum HW not supported (TCP)
9499 	 */
9500 	if (state->sw_csum & CSUM_DELAY_DATA) {
9501 		gso_ipv4_data_cksum(m, state->hdr.ip, state->mac_hlen);
9502 	}
9503 
9504 	state->tcp_seq += state->pay_len;
9505 	/*
9506 	 * IP Checksum
9507 	 */
9508 	state->hdr.ip->ip_sum = 0;
9509 	/*
9510 	 * Checksum HW not supported (IP)
9511 	 */
9512 	if (state->sw_csum & CSUM_IP) {
9513 		gso_ipv4_hdr_cksum(m, state->hdr.ip, state->mac_hlen, state->ip_hlen);
9514 	}
9515 }
9516 
9517 
9518 /*
9519  * Updates the pointers to TCP and IPv6 headers
9520  */
9521 static inline void
9522 gso_ipv6_tcp_update(struct gso_ip_tcp_state *state, struct mbuf *m)
9523 {
9524 	state->hdr.ip6 = (struct ip6_hdr *)(mtod(m, uint8_t *) + state->mac_hlen);
9525 	state->tcp = (struct tcphdr *)(void *)((caddr_t)(state->hdr.ip6) + state->ip_hlen);
9526 	state->pay_len = m->m_pkthdr.len - state->hlen;
9527 }
9528 
9529 /*
9530  * Sets properly the TCP and IPv6 headers
9531  */
9532 static inline void
9533 gso_ipv6_tcp_internal(struct gso_ip_tcp_state *state, struct mbuf *m)
9534 {
9535 	state->hdr.ip6->ip6_plen = htons(m->m_pkthdr.len -
9536 	    state->mac_hlen - state->ip_hlen);
9537 	/*
9538 	 * TCP Checksum
9539 	 */
9540 	state->tcp->th_sum = 0;
9541 	state->tcp->th_sum = in6_pseudo(&state->hdr.ip6->ip6_src,
9542 	    &state->hdr.ip6->ip6_dst,
9543 	    htonl(state->tcp_hlen + state->pay_len + IPPROTO_TCP));
9544 	/*
9545 	 * Checksum HW not supported (TCP)
9546 	 */
9547 	if (state->sw_csum & CSUM_DELAY_IPV6_DATA) {
9548 		(void)in6_finalize_cksum(m, state->mac_hlen, -1, -1, state->sw_csum);
9549 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_IPV6_DATA;
9550 	}
9551 	state->tcp_seq += state->pay_len;
9552 }
9553 
9554 /*
9555  * Init the state during the TCP segmentation
9556  */
9557 static void
9558 gso_ip_tcp_init_state(struct gso_ip_tcp_state *state, struct ifnet *ifp,
9559     bool is_ipv4, int mac_hlen, int ip_hlen,
9560     void * ip_hdr, struct tcphdr * tcp_hdr)
9561 {
9562 #pragma unused(ifp)
9563 
9564 	state->hdr.ptr = ip_hdr;
9565 	state->tcp = tcp_hdr;
9566 	if (is_ipv4) {
9567 		state->ip_id = ntohs(state->hdr.ip->ip_id);
9568 		state->update = gso_ipv4_tcp_update;
9569 		state->internal = gso_ipv4_tcp_internal;
9570 		state->sw_csum = CSUM_DELAY_DATA | CSUM_IP; /* XXX */
9571 	} else {
9572 		state->update = gso_ipv6_tcp_update;
9573 		state->internal = gso_ipv6_tcp_internal;
9574 		state->sw_csum = CSUM_DELAY_IPV6_DATA; /* XXX */
9575 	}
9576 	state->mac_hlen = mac_hlen;
9577 	state->ip_hlen = ip_hlen;
9578 	state->tcp_hlen = state->tcp->th_off << 2;
9579 	state->hlen = mac_hlen + ip_hlen + state->tcp_hlen;
9580 	state->tcp_seq = ntohl(state->tcp->th_seq);
9581 	//state->sw_csum = m->m_pkthdr.csum_flags & ~IF_HWASSIST_CSUM_FLAGS(ifp->if_hwassist);
9582 	return;
9583 }
9584 
9585 /*
9586  * GSO on TCP/IP (v4 or v6)
9587  *
9588  * If is_tx is TRUE, segmented packets are transmitted after they are
9589  * segmented.
9590  *
9591  * If is_tx is FALSE, the segmented packets are returned as a chain in *mp.
9592  */
9593 static int
9594 gso_ip_tcp(struct ifnet *ifp, struct mbuf **mp, struct gso_ip_tcp_state *state,
9595     boolean_t is_tx)
9596 {
9597 	struct mbuf *m, *m_tx;
9598 	int error = 0;
9599 	int mss = 0;
9600 	int nsegs = 0;
9601 	struct mbuf *m0 = *mp;
9602 #ifdef GSO_STATS
9603 	int total_len = m0->m_pkthdr.len;
9604 #endif /* GSO_STATS */
9605 
9606 #if 1
9607 	u_int reduce_mss;
9608 
9609 	reduce_mss = is_tx ? if_bridge_tso_reduce_mss_tx
9610 	    : if_bridge_tso_reduce_mss_forwarding;
9611 	mss = ifp->if_mtu - state->ip_hlen - state->tcp_hlen - reduce_mss;
9612 	assert(mss > 0);
9613 #else
9614 	if (m0->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) {/* TSO with GSO */
9615 		mss = ifp->if_hw_tsomax - state->ip_hlen - state->tcp_hlen;
9616 	} else {
9617 		mss = m0->m_pkthdr.tso_segsz;
9618 	}
9619 #endif
9620 
9621 	*mp = m0 = m_seg(m0, state->hlen, mss, &nsegs, 0, 0);
9622 	if (m0 == NULL) {
9623 		return ENOBUFS; /* XXX ok? */
9624 	}
9625 	BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM,
9626 	    "%s %s mss %d nsegs %d",
9627 	    ifp->if_xname,
9628 	    is_tx ? "TX" : "RX",
9629 	    mss, nsegs);
9630 	/*
9631 	 * XXX-ste: can this happen?
9632 	 */
9633 	if (m0->m_nextpkt == NULL) {
9634 #ifdef GSO_DEBUG
9635 		D("only 1 segment");
9636 #endif
9637 		if (is_tx) {
9638 			error = bridge_transmit(ifp, m0);
9639 		}
9640 		return error;
9641 	}
9642 #ifdef GSO_STATS
9643 	GSOSTAT_SET_MAX(tcp.gsos_max_mss, mss);
9644 	GSOSTAT_SET_MIN(tcp.gsos_min_mss, mss);
9645 	GSOSTAT_ADD(tcp.gsos_osegments, nsegs);
9646 #endif /* GSO_STATS */
9647 
9648 	/* first pkt */
9649 	m = m0;
9650 
9651 	state->update(state, m);
9652 
9653 	do {
9654 		state->tcp->th_flags &= ~(TH_FIN | TH_PUSH);
9655 
9656 		state->internal(state, m);
9657 		m_tx = m;
9658 		m = m->m_nextpkt;
9659 		if (is_tx) {
9660 			m_tx->m_nextpkt = NULL;
9661 			if ((error = bridge_transmit(ifp, m_tx)) != 0) {
9662 				/*
9663 				 * XXX: If a segment can not be sent, discard the following
9664 				 * segments and propagate the error to the upper levels.
9665 				 * In this way the TCP retransmits all the initial packet.
9666 				 */
9667 #ifdef GSO_DEBUG
9668 				D("if_transmit error\n");
9669 #endif
9670 				goto err;
9671 			}
9672 		}
9673 		state->update(state, m);
9674 
9675 		state->tcp->th_flags &= ~TH_CWR;
9676 		state->tcp->th_seq = htonl(state->tcp_seq);
9677 	} while (m->m_nextpkt);
9678 
9679 	/* last pkt */
9680 	state->internal(state, m);
9681 
9682 	if (is_tx) {
9683 		error = bridge_transmit(ifp, m);
9684 #ifdef GSO_DEBUG
9685 		if (error) {
9686 			D("last if_transmit error\n");
9687 			D("error - type = %d \n", error);
9688 		}
9689 #endif
9690 	}
9691 #ifdef GSO_STATS
9692 	if (!error) {
9693 		GSOSTAT_INC(tcp.gsos_segmented);
9694 		GSOSTAT_SET_MAX(tcp.gsos_maxsegmented, total_len);
9695 		GSOSTAT_SET_MIN(tcp.gsos_minsegmented, total_len);
9696 		GSOSTAT_ADD(tcp.gsos_totalbyteseg, total_len);
9697 	}
9698 #endif /* GSO_STATS */
9699 	return error;
9700 
9701 err:
9702 #ifdef GSO_DEBUG
9703 	D("error - type = %d \n", error);
9704 #endif
9705 	while (m != NULL) {
9706 		m_tx = m->m_nextpkt;
9707 		m->m_nextpkt = NULL;
9708 		m_freem(m);
9709 		m = m_tx;
9710 	}
9711 	return error;
9712 }
9713 
9714 /*
9715  * GSO for TCP/IPv[46]
9716  */
9717 static int
9718 gso_tcp(struct ifnet *ifp, struct mbuf **mp, u_int mac_hlen, bool is_ipv4,
9719     boolean_t is_tx)
9720 {
9721 	int error;
9722 	ip_packet_info  info;
9723 	uint32_t csum_flags;
9724 	struct gso_ip_tcp_state state;
9725 	struct bripstats stats; /* XXX ignored */
9726 	struct tcphdr *tcp;
9727 
9728 	if (!is_tx && ipforwarding == 0) {
9729 		/* no need to segment if the packet will not be forwarded */
9730 		return 0;
9731 	}
9732 	error = bridge_get_tcp_header(mp, mac_hlen, is_ipv4, &info, &stats);
9733 	if (error != 0) {
9734 		if (*mp != NULL) {
9735 			m_freem(*mp);
9736 			*mp = NULL;
9737 		}
9738 		return error;
9739 	}
9740 	if (info.ip_proto_hdr == NULL) {
9741 		/* not a TCP packet */
9742 		return 0;
9743 	}
9744 	tcp = (struct tcphdr *)(void *)info.ip_proto_hdr;
9745 	gso_ip_tcp_init_state(&state, ifp, is_ipv4, mac_hlen,
9746 	    info.ip_hlen, info.ip_hdr.ptr, tcp);
9747 	if (is_ipv4) {
9748 		csum_flags = CSUM_DELAY_DATA; /* XXX */
9749 		if (!is_tx) {
9750 			/* if RX to our local IP address, don't segment */
9751 			struct in_addr  dst_ip;
9752 
9753 			bcopy(&state.hdr.ip->ip_dst, &dst_ip, sizeof(dst_ip));
9754 			if (in_addr_is_ours(dst_ip)) {
9755 				return 0;
9756 			}
9757 		}
9758 	} else {
9759 		csum_flags = CSUM_DELAY_IPV6_DATA; /* XXX */
9760 		if (!is_tx) {
9761 			/* if RX to our local IP address, don't segment */
9762 			if (in6_addr_is_ours(&state.hdr.ip6->ip6_dst,
9763 			    ifp->if_index)) {
9764 				/* local IP address, no need to segment */
9765 				return 0;
9766 			}
9767 		}
9768 	}
9769 	(*mp)->m_pkthdr.csum_flags = csum_flags;
9770 	(*mp)->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
9771 	return gso_ip_tcp(ifp, mp, &state, is_tx);
9772 }
9773