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