]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_bridge.c
80bc27d78cf753afc7a0e944c6e9dcbd974be84b
[apple/xnu.git] / bsd / net / if_bridge.c
1 /*
2 * Copyright (c) 2004-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /* $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $ */
30 /*
31 * Copyright 2001 Wasabi Systems, Inc.
32 * All rights reserved.
33 *
34 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed for the NetBSD Project by
47 * Wasabi Systems, Inc.
48 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
49 * or promote products derived from this software without specific prior
50 * written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
54 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
56 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62 * POSSIBILITY OF SUCH DAMAGE.
63 */
64
65 /*
66 * Copyright (c) 1999, 2000 Jason L. Wright (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).
102 *
103 * - GIF isn't handled due to the lack of IPPROTO_ETHERIP support.
104 */
105
106 #include <sys/cdefs.h>
107
108 #define BRIDGE_DEBUG 1
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>
118 #include <sys/kernel.h>
119 #include <sys/random.h>
120 #include <sys/syslog.h>
121 #include <sys/sysctl.h>
122 #include <sys/proc.h>
123 #include <sys/lock.h>
124 #include <sys/mcache.h>
125
126 #include <sys/kauth.h>
127
128 #include <kern/thread_call.h>
129
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>
138 #include <net/if_dl.h>
139 #include <net/if_types.h>
140 #include <net/if_var.h>
141 #include <net/if_media.h>
142 #include <net/net_api_stats.h>
143
144 #include <netinet/in.h> /* for struct arpcom */
145 #include <netinet/in_systm.h>
146 #include <netinet/in_var.h>
147 #define _IP_VHL
148 #include <netinet/ip.h>
149 #include <netinet/ip_var.h>
150 #if INET6
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
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>
161 #if NVLAN > 0
162 #include <net/if_vlan_var.h>
163 #endif /* NVLAN > 0 */
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 */
174 #include <dev/random/randomdev.h>
175
176 #include <netinet/bootp.h>
177 #include <netinet/dhcp.h>
178
179
180 #if BRIDGE_DEBUG
181 #define BR_DBGF_LIFECYCLE 0x0001
182 #define BR_DBGF_INPUT 0x0002
183 #define BR_DBGF_OUTPUT 0x0004
184 #define BR_DBGF_RT_TABLE 0x0008
185 #define BR_DBGF_DELAYED_CALL 0x0010
186 #define BR_DBGF_IOCTL 0x0020
187 #define BR_DBGF_MBUF 0x0040
188 #define BR_DBGF_MCAST 0x0080
189 #define BR_DBGF_HOSTFILTER 0x0100
190 #define BR_DBGF_CHECKSUM 0x0200
191 #define BR_DBGF_MAC_NAT 0x0400
192 #endif /* BRIDGE_DEBUG */
193
194 #define _BRIDGE_LOCK(_sc) lck_mtx_lock(&(_sc)->sc_mtx)
195 #define _BRIDGE_UNLOCK(_sc) lck_mtx_unlock(&(_sc)->sc_mtx)
196 #define BRIDGE_LOCK_ASSERT_HELD(_sc) \
197 LCK_MTX_ASSERT(&(_sc)->sc_mtx, LCK_MTX_ASSERT_OWNED)
198 #define BRIDGE_LOCK_ASSERT_NOTHELD(_sc) \
199 LCK_MTX_ASSERT(&(_sc)->sc_mtx, LCK_MTX_ASSERT_NOTOWNED)
200
201 #if BRIDGE_DEBUG
202
203 #define BR_LCKDBG_MAX 4
204
205 #define BRIDGE_LOCK(_sc) bridge_lock(_sc)
206 #define BRIDGE_UNLOCK(_sc) bridge_unlock(_sc)
207 #define BRIDGE_LOCK2REF(_sc, _err) _err = bridge_lock2ref(_sc)
208 #define BRIDGE_UNREF(_sc) bridge_unref(_sc)
209 #define BRIDGE_XLOCK(_sc) bridge_xlock(_sc)
210 #define BRIDGE_XDROP(_sc) bridge_xdrop(_sc)
211 #define IF_BRIDGE_DEBUG(f) bridge_debug_flag_is_set(f)
212
213 #else /* !BRIDGE_DEBUG */
214
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); \
224 } while (0)
225 #define BRIDGE_UNREF(_sc) do { \
226 _BRIDGE_LOCK(_sc); \
227 (_sc)->sc_iflist_ref--; \
228 if (((_sc)->sc_iflist_xcnt > 0) && ((_sc)->sc_iflist_ref == 0)) { \
229 _BRIDGE_UNLOCK(_sc); \
230 wakeup(&(_sc)->sc_cv); \
231 } else \
232 _BRIDGE_UNLOCK(_sc); \
233 } while (0)
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); \
240 } while (0)
241 #define BRIDGE_XDROP(_sc) do { \
242 BRIDGE_LOCK_ASSERT_HELD(_sc); \
243 (_sc)->sc_iflist_xcnt--; \
244 } while (0)
245
246 #define IF_BRIDGE_DEBUG(f) FALSE
247
248 #endif /* BRIDGE_DEBUG */
249
250 #if NBPFILTER > 0
251 #define BRIDGE_BPF_MTAP_INPUT(sc, m) \
252 if (sc->sc_bpf_input != NULL) \
253 bridge_bpf_input(sc->sc_ifp, m, __func__, __LINE__)
254 #else /* NBPFILTER */
255 #define BRIDGE_BPF_MTAP_INPUT(ifp, m)
256 #endif /* NBPFILTER */
257
258 /*
259 * Initial size of the route hash table. Must be a power of two.
260 */
261 #ifndef BRIDGE_RTHASH_SIZE
262 #define BRIDGE_RTHASH_SIZE 16
263 #endif
264
265 /*
266 * Maximum size of the routing hash table
267 */
268 #define BRIDGE_RTHASH_SIZE_MAX 2048
269
270 #define BRIDGE_RTHASH_MASK(sc) ((sc)->sc_rthash_size - 1)
271
272 /*
273 * Maximum number of addresses to cache.
274 */
275 #ifndef BRIDGE_RTABLE_MAX
276 #define BRIDGE_RTABLE_MAX 100
277 #endif
278
279
280 /*
281 * Timeout (in seconds) for entries learned dynamically.
282 */
283 #ifndef BRIDGE_RTABLE_TIMEOUT
284 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
285 #endif
286
287 /*
288 * Number of seconds between walks of the route list.
289 */
290 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
291 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
292 #endif
293
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
303 /*
304 * List of capabilities to possibly mask on the member interface.
305 */
306 #define BRIDGE_IFCAPS_MASK (IFCAP_TOE|IFCAP_TSO|IFCAP_TXCSUM)
307 /*
308 * List of capabilities to disable on the member interface.
309 */
310 #define BRIDGE_IFCAPS_STRIP IFCAP_LRO
311
312 /*
313 * Bridge interface list entry.
314 */
315 struct bridge_iflist {
316 TAILQ_ENTRY(bridge_iflist) bif_next;
317 struct ifnet *bif_ifp; /* member if */
318 struct bstp_port bif_stp; /* STP state */
319 uint32_t bif_ifflags; /* member if flags */
320 int bif_savedcaps; /* saved capabilities */
321 uint32_t bif_addrmax; /* max # of addresses */
322 uint32_t bif_addrcnt; /* cur. # of addresses */
323 uint32_t bif_addrexceeded; /* # of address violations */
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];
331 };
332
333 #define BIFF_PROMISC 0x01 /* promiscuous mode set */
334 #define BIFF_PROTO_ATTACHED 0x02 /* protocol attached */
335 #define BIFF_FILTER_ATTACHED 0x04 /* interface filter attached */
336 #define BIFF_MEDIA_ACTIVE 0x08 /* interface media active */
337 #define BIFF_HOST_FILTER 0x10 /* host filter enabled */
338 #define BIFF_HF_HWSRC 0x20 /* host filter source MAC is set */
339 #define BIFF_HF_IPSRC 0x40 /* host filter source IP is set */
340 #define BIFF_INPUT_BROADCAST 0x80 /* send broadcast packets in */
341
342 /*
343 * mac_nat_entry
344 * - translates between an IP address and MAC address on a specific
345 * bridge interface member
346 */
347 struct mac_nat_entry {
348 LIST_ENTRY(mac_nat_entry) mne_list; /* list linkage */
349 struct bridge_iflist *mne_bif; /* originating interface */
350 unsigned long mne_expire; /* expiration time */
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
364 LIST_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 */
373 struct 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
402 /*
403 * Bridge route node.
404 */
405 struct bridge_rtnode {
406 LIST_ENTRY(bridge_rtnode) brt_hash; /* hash table linkage */
407 LIST_ENTRY(bridge_rtnode) brt_list; /* list linkage */
408 struct bridge_iflist *brt_dst; /* destination if */
409 unsigned long brt_expire; /* expiration time */
410 uint8_t brt_flags; /* address flags */
411 uint8_t brt_addr[ETHER_ADDR_LEN];
412 uint16_t brt_vlan; /* vlan id */
413
414 };
415 #define brt_ifp brt_dst->bif_ifp
416
417 /*
418 * Bridge delayed function call context
419 */
420 typedef void (*bridge_delayed_func_t)(struct bridge_softc *);
421
422 struct bridge_delayed_call {
423 struct bridge_softc *bdc_sc;
424 bridge_delayed_func_t bdc_func; /* Function to call */
425 struct timespec bdc_ts; /* Time to call */
426 u_int32_t bdc_flags;
427 thread_call_t bdc_thread_call;
428 };
429
430 #define BDCF_OUTSTANDING 0x01 /* Delayed call has been scheduled */
431 #define BDCF_CANCELLING 0x02 /* May be waiting for call completion */
432
433 /*
434 * Software state for each bridge.
435 */
436 LIST_HEAD(_bridge_rtnode_list, bridge_rtnode);
437
438 struct bridge_softc {
439 struct ifnet *sc_ifp; /* make this an interface */
440 u_int32_t sc_flags;
441 LIST_ENTRY(bridge_softc) sc_list;
442 decl_lck_mtx_data(, sc_mtx);
443 struct _bridge_rtnode_list *sc_rthash; /* our forwarding table */
444 struct _bridge_rtnode_list sc_rtlist; /* list version of above */
445 uint32_t sc_rthash_key; /* key for hash */
446 uint32_t sc_rthash_size; /* size of the hash table */
447 struct bridge_delayed_call sc_aging_timer;
448 struct bridge_delayed_call sc_resize_call;
449 TAILQ_HEAD(, bridge_iflist) sc_spanlist; /* span ports list */
450 struct bstp_state sc_stp; /* STP state */
451 bpf_packet_func sc_bpf_input;
452 bpf_packet_func sc_bpf_output;
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];
465
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;
472 #if BRIDGE_DEBUG
473 /*
474 * Locking and unlocking calling history
475 */
476 void *lock_lr[BR_LCKDBG_MAX];
477 int next_lock_lr;
478 void *unlock_lr[BR_LCKDBG_MAX];
479 int next_unlock_lr;
480 #endif /* BRIDGE_DEBUG */
481 };
482
483 #define SCF_DETACHING 0x01
484 #define SCF_RESIZING 0x02
485 #define SCF_MEDIA_ACTIVE 0x04
486
487 typedef enum {
488 kChecksumOperationNone = 0,
489 kChecksumOperationClear = 1,
490 kChecksumOperationFinalize = 2,
491 kChecksumOperationCompute = 3,
492 } ChecksumOperation;
493
494 struct bridge_hostfilter_stats bridge_hostfilter_stats;
495
496 decl_lck_mtx_data(static, bridge_list_mtx);
497
498 static int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
499
500 static zone_t bridge_rtnode_pool = NULL;
501 static zone_t bridge_mne_pool = NULL;
502
503 static int bridge_clone_create(struct if_clone *, uint32_t, void *);
504 static int bridge_clone_destroy(struct ifnet *);
505
506 static errno_t bridge_ioctl(struct ifnet *, u_long, void *);
507 #if HAS_IF_CAP
508 static void bridge_mutecaps(struct bridge_softc *);
509 static void bridge_set_ifcap(struct bridge_softc *, struct bridge_iflist *,
510 int);
511 #endif
512 static errno_t bridge_set_tso(struct bridge_softc *);
513 static void bridge_ifdetach(struct ifnet *);
514 static void bridge_proto_attach_changed(struct ifnet *);
515 static int bridge_init(struct ifnet *);
516 #if HAS_BRIDGE_DUMMYNET
517 static void bridge_dummynet(struct mbuf *, struct ifnet *);
518 #endif
519 static void bridge_ifstop(struct ifnet *, int);
520 static int bridge_output(struct ifnet *, struct mbuf *);
521 static void bridge_finalize_cksum(struct ifnet *, struct mbuf *);
522 static void bridge_start(struct ifnet *);
523 static errno_t bridge_input(struct ifnet *, mbuf_t *);
524 static errno_t bridge_iff_input(void *, ifnet_t, protocol_family_t,
525 mbuf_t *, char **);
526 static errno_t bridge_iff_output(void *, ifnet_t, protocol_family_t,
527 mbuf_t *);
528 static errno_t bridge_member_output(struct bridge_softc *sc, ifnet_t ifp,
529 mbuf_t *m);
530
531 static int bridge_enqueue(ifnet_t, struct ifnet *,
532 struct ifnet *, struct mbuf *, ChecksumOperation);
533 static void bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp, int);
534
535 static void bridge_forward(struct bridge_softc *, struct bridge_iflist *,
536 struct mbuf *);
537
538 static void bridge_aging_timer(struct bridge_softc *sc);
539
540 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
541 struct mbuf *, int);
542 static void bridge_span(struct bridge_softc *, struct mbuf *);
543
544 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
545 uint16_t, struct bridge_iflist *, int, uint8_t);
546 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *,
547 uint16_t);
548 static void bridge_rttrim(struct bridge_softc *);
549 static void bridge_rtage(struct bridge_softc *);
550 static void bridge_rtflush(struct bridge_softc *, int);
551 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *,
552 uint16_t);
553
554 static int bridge_rtable_init(struct bridge_softc *);
555 static void bridge_rtable_fini(struct bridge_softc *);
556
557 static void bridge_rthash_resize(struct bridge_softc *);
558
559 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
560 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
561 const uint8_t *, uint16_t);
562 static int bridge_rtnode_hash(struct bridge_softc *,
563 struct bridge_rtnode *);
564 static int bridge_rtnode_insert(struct bridge_softc *,
565 struct bridge_rtnode *);
566 static void bridge_rtnode_destroy(struct bridge_softc *,
567 struct bridge_rtnode *);
568 #if BRIDGESTP
569 static void bridge_rtable_expire(struct ifnet *, int);
570 static void bridge_state_change(struct ifnet *, int);
571 #endif /* BRIDGESTP */
572
573 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
574 const char *name);
575 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
576 struct ifnet *ifp);
577 static void bridge_delete_member(struct bridge_softc *,
578 struct bridge_iflist *, int);
579 static void bridge_delete_span(struct bridge_softc *,
580 struct bridge_iflist *);
581
582 static int bridge_ioctl_add(struct bridge_softc *, void *);
583 static int bridge_ioctl_del(struct bridge_softc *, void *);
584 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
585 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
586 static int bridge_ioctl_scache(struct bridge_softc *, void *);
587 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
588 static int bridge_ioctl_gifs32(struct bridge_softc *, void *);
589 static int bridge_ioctl_gifs64(struct bridge_softc *, void *);
590 static int bridge_ioctl_rts32(struct bridge_softc *, void *);
591 static int bridge_ioctl_rts64(struct bridge_softc *, void *);
592 static int bridge_ioctl_saddr32(struct bridge_softc *, void *);
593 static int bridge_ioctl_saddr64(struct bridge_softc *, void *);
594 static int bridge_ioctl_sto(struct bridge_softc *, void *);
595 static int bridge_ioctl_gto(struct bridge_softc *, void *);
596 static int bridge_ioctl_daddr32(struct bridge_softc *, void *);
597 static int bridge_ioctl_daddr64(struct bridge_softc *, void *);
598 static int bridge_ioctl_flush(struct bridge_softc *, void *);
599 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
600 static int bridge_ioctl_spri(struct bridge_softc *, void *);
601 static int bridge_ioctl_ght(struct bridge_softc *, void *);
602 static int bridge_ioctl_sht(struct bridge_softc *, void *);
603 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
604 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
605 static int bridge_ioctl_gma(struct bridge_softc *, void *);
606 static int bridge_ioctl_sma(struct bridge_softc *, void *);
607 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
608 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
609 static int bridge_ioctl_sifmaxaddr(struct bridge_softc *, void *);
610 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
611 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
612 static int bridge_ioctl_gbparam32(struct bridge_softc *, void *);
613 static int bridge_ioctl_gbparam64(struct bridge_softc *, void *);
614 static int bridge_ioctl_grte(struct bridge_softc *, void *);
615 static int bridge_ioctl_gifsstp32(struct bridge_softc *, void *);
616 static int bridge_ioctl_gifsstp64(struct bridge_softc *, void *);
617 static int bridge_ioctl_sproto(struct bridge_softc *, void *);
618 static int bridge_ioctl_stxhc(struct bridge_softc *, void *);
619 static int bridge_ioctl_purge(struct bridge_softc *sc, void *);
620 static int bridge_ioctl_gfilt(struct bridge_softc *, void *);
621 static int bridge_ioctl_sfilt(struct bridge_softc *, void *);
622 static int bridge_ioctl_ghostfilter(struct bridge_softc *, void *);
623 static int bridge_ioctl_shostfilter(struct bridge_softc *, void *);
624 static int bridge_ioctl_gmnelist32(struct bridge_softc *, void *);
625 static int bridge_ioctl_gmnelist64(struct bridge_softc *, void *);
626 #ifdef PFIL_HOOKS
627 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
628 int);
629 static int bridge_fragment(struct ifnet *, struct mbuf *,
630 struct ether_header *, int, struct llc *);
631 #endif /* PFIL_HOOKS */
632 static int bridge_ip_checkbasic(struct mbuf **);
633 #ifdef INET6
634 static int bridge_ip6_checkbasic(struct mbuf **);
635 #endif /* INET6 */
636
637 static int bridge_pf(struct mbuf **, struct ifnet *, uint32_t sc_filter_flags, int input);
638
639 static errno_t bridge_set_bpf_tap(ifnet_t, bpf_tap_mode, bpf_packet_func);
640 static errno_t bridge_bpf_input(ifnet_t, struct mbuf *, const char *, int);
641 static errno_t bridge_bpf_output(ifnet_t, struct mbuf *);
642
643 static void bridge_detach(ifnet_t);
644 static void bridge_link_event(struct ifnet *, u_int32_t);
645 static void bridge_iflinkevent(struct ifnet *);
646 static u_int32_t bridge_updatelinkstatus(struct bridge_softc *);
647 static int interface_media_active(struct ifnet *);
648 static void bridge_schedule_delayed_call(struct bridge_delayed_call *);
649 static void bridge_cancel_delayed_call(struct bridge_delayed_call *);
650 static void bridge_cleanup_delayed_call(struct bridge_delayed_call *);
651 static int bridge_host_filter(struct bridge_iflist *, mbuf_t *);
652
653 static errno_t bridge_mac_nat_enable(struct bridge_softc *,
654 struct bridge_iflist *);
655 static void bridge_mac_nat_disable(struct bridge_softc *sc);
656 static void bridge_mac_nat_age_entries(struct bridge_softc *sc, unsigned long);
657 static void bridge_mac_nat_populate_entries(struct bridge_softc *sc);
658 static void bridge_mac_nat_flush_entries(struct bridge_softc *sc,
659 struct bridge_iflist *);
660 static ifnet_t bridge_mac_nat_input(struct bridge_softc *, mbuf_t *,
661 boolean_t *);
662 static boolean_t bridge_mac_nat_output(struct bridge_softc *,
663 struct bridge_iflist *, mbuf_t *, struct mac_nat_record *);
664 static void bridge_mac_nat_translate(mbuf_t *, struct mac_nat_record *,
665 const caddr_t);
666
667 #define m_copypacket(m, how) m_copym(m, 0, M_COPYALL, how)
668
669 /* The default bridge vlan is 1 (IEEE 802.1Q-2003 Table 9-2) */
670 #define VLANTAGOF(_m) 0
671
672 u_int8_t bstp_etheraddr[ETHER_ADDR_LEN] =
673 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
674
675 static u_int8_t ethernulladdr[ETHER_ADDR_LEN] =
676 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
677
678 #if BRIDGESTP
679 static struct bstp_cb_ops bridge_ops = {
680 .bcb_state = bridge_state_change,
681 .bcb_rtage = bridge_rtable_expire
682 };
683 #endif /* BRIDGESTP */
684
685 SYSCTL_DECL(_net_link);
686 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
687 "Bridge");
688
689 static int bridge_inherit_mac = 0; /* share MAC with first bridge member */
690 SYSCTL_INT(_net_link_bridge, OID_AUTO, inherit_mac,
691 CTLFLAG_RW | CTLFLAG_LOCKED,
692 &bridge_inherit_mac, 0,
693 "Inherit MAC address from the first bridge member");
694
695 SYSCTL_INT(_net_link_bridge, OID_AUTO, rtable_prune_period,
696 CTLFLAG_RW | CTLFLAG_LOCKED,
697 &bridge_rtable_prune_period, 0,
698 "Interval between pruning of routing table");
699
700 static unsigned int bridge_rtable_hash_size_max = BRIDGE_RTHASH_SIZE_MAX;
701 SYSCTL_UINT(_net_link_bridge, OID_AUTO, rtable_hash_size_max,
702 CTLFLAG_RW | CTLFLAG_LOCKED,
703 &bridge_rtable_hash_size_max, 0,
704 "Maximum size of the routing hash table");
705
706 #if BRIDGE_DEBUG_DELAYED_CALLBACK
707 static int bridge_delayed_callback_delay = 0;
708 SYSCTL_INT(_net_link_bridge, OID_AUTO, delayed_callback_delay,
709 CTLFLAG_RW | CTLFLAG_LOCKED,
710 &bridge_delayed_callback_delay, 0,
711 "Delay before calling delayed function");
712 #endif
713
714 SYSCTL_STRUCT(_net_link_bridge, OID_AUTO,
715 hostfilterstats, CTLFLAG_RD | CTLFLAG_LOCKED,
716 &bridge_hostfilter_stats, bridge_hostfilter_stats, "");
717
718 #if defined(PFIL_HOOKS)
719 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
720 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
721 static int pfil_member = 1; /* run pfil hooks on the member interface */
722 static int pfil_ipfw = 0; /* layer2 filter with ipfw */
723 static int pfil_ipfw_arp = 0; /* layer2 filter with ipfw */
724 static int pfil_local_phys = 0; /* run pfil hooks on the physical interface */
725 /* for locally destined packets */
726 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW | CTLFLAG_LOCKED,
727 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
728 SYSCTL_INT(_net_link_bridge, OID_AUTO, ipfw_arp, CTLFLAG_RW | CTLFLAG_LOCKED,
729 &pfil_ipfw_arp, 0, "Filter ARP packets through IPFW layer2");
730 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW | CTLFLAG_LOCKED,
731 &pfil_bridge, 0, "Packet filter on the bridge interface");
732 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW | CTLFLAG_LOCKED,
733 &pfil_member, 0, "Packet filter on the member interface");
734 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_local_phys,
735 CTLFLAG_RW | CTLFLAG_LOCKED, &pfil_local_phys, 0,
736 "Packet filter on the physical interface for locally destined packets");
737 #endif /* PFIL_HOOKS */
738
739 #if BRIDGESTP
740 static int log_stp = 0; /* log STP state changes */
741 SYSCTL_INT(_net_link_bridge, OID_AUTO, log_stp, CTLFLAG_RW,
742 &log_stp, 0, "Log STP state changes");
743 #endif /* BRIDGESTP */
744
745 struct bridge_control {
746 int (*bc_func)(struct bridge_softc *, void *);
747 unsigned int bc_argsize;
748 unsigned int bc_flags;
749 };
750
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 */
754
755 static const struct bridge_control bridge_control_table32[] = {
756 { .bc_func = bridge_ioctl_add, .bc_argsize = sizeof(struct ifbreq), /* 0 */
757 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
758 { .bc_func = bridge_ioctl_del, .bc_argsize = sizeof(struct ifbreq),
759 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
760
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 },
765
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 },
770
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 },
775
776 { .bc_func = bridge_ioctl_saddr32, .bc_argsize = sizeof(struct ifbareq32),
777 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
778
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 },
783
784 { .bc_func = bridge_ioctl_daddr32, .bc_argsize = sizeof(struct ifbareq32),
785 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
786
787 { .bc_func = bridge_ioctl_flush, .bc_argsize = sizeof(struct ifbreq),
788 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
789
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 },
794
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 },
799
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 },
804
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 },
809
810 { .bc_func = bridge_ioctl_sifprio, .bc_argsize = sizeof(struct ifbreq),
811 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
812
813 { .bc_func = bridge_ioctl_sifcost, .bc_argsize = sizeof(struct ifbreq),
814 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
815
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 },
820
821 { .bc_func = bridge_ioctl_purge, .bc_argsize = sizeof(struct ifbreq),
822 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
823
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 },
828
829 { .bc_func = bridge_ioctl_gbparam32, .bc_argsize = sizeof(struct ifbropreq32),
830 .bc_flags = BC_F_COPYOUT },
831
832 { .bc_func = bridge_ioctl_grte, .bc_argsize = sizeof(struct ifbrparam),
833 .bc_flags = BC_F_COPYOUT },
834
835 { .bc_func = bridge_ioctl_gifsstp32, .bc_argsize = sizeof(struct ifbpstpconf32), /* 30 */
836 .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
837
838 { .bc_func = bridge_ioctl_sproto, .bc_argsize = sizeof(struct ifbrparam),
839 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
840
841 { .bc_func = bridge_ioctl_stxhc, .bc_argsize = sizeof(struct ifbrparam),
842 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
843
844 { .bc_func = bridge_ioctl_sifmaxaddr, .bc_argsize = sizeof(struct ifbreq),
845 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
846
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 },
851
852 { .bc_func = bridge_ioctl_gmnelist32, .bc_argsize = sizeof(struct ifbrmnelist32),
853 .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
854 };
855
856 static const struct bridge_control bridge_control_table64[] = {
857 { .bc_func = bridge_ioctl_add, .bc_argsize = sizeof(struct ifbreq), /* 0 */
858 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
859 { .bc_func = bridge_ioctl_del, .bc_argsize = sizeof(struct ifbreq),
860 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
861
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 },
866
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 },
871
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 },
876
877 { .bc_func = bridge_ioctl_saddr64, .bc_argsize = sizeof(struct ifbareq64),
878 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
879
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 },
884
885 { .bc_func = bridge_ioctl_daddr64, .bc_argsize = sizeof(struct ifbareq64),
886 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
887
888 { .bc_func = bridge_ioctl_flush, .bc_argsize = sizeof(struct ifbreq),
889 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
890
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 },
895
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 },
900
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 },
905
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 },
910
911 { .bc_func = bridge_ioctl_sifprio, .bc_argsize = sizeof(struct ifbreq),
912 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
913
914 { .bc_func = bridge_ioctl_sifcost, .bc_argsize = sizeof(struct ifbreq),
915 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
916
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 },
921
922 { .bc_func = bridge_ioctl_purge, .bc_argsize = sizeof(struct ifbreq),
923 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
924
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 },
929
930 { .bc_func = bridge_ioctl_gbparam64, .bc_argsize = sizeof(struct ifbropreq64),
931 .bc_flags = BC_F_COPYOUT },
932
933 { .bc_func = bridge_ioctl_grte, .bc_argsize = sizeof(struct ifbrparam),
934 .bc_flags = BC_F_COPYOUT },
935
936 { .bc_func = bridge_ioctl_gifsstp64, .bc_argsize = sizeof(struct ifbpstpconf64), /* 30 */
937 .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
938
939 { .bc_func = bridge_ioctl_sproto, .bc_argsize = sizeof(struct ifbrparam),
940 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
941
942 { .bc_func = bridge_ioctl_stxhc, .bc_argsize = sizeof(struct ifbrparam),
943 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
944
945 { .bc_func = bridge_ioctl_sifmaxaddr, .bc_argsize = sizeof(struct ifbreq),
946 .bc_flags = BC_F_COPYIN | BC_F_SUSER },
947
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 },
952
953 { .bc_func = bridge_ioctl_gmnelist64, .bc_argsize = sizeof(struct ifbrmnelist64),
954 .bc_flags = BC_F_COPYIN | BC_F_COPYOUT },
955 };
956
957 static const unsigned int bridge_control_table_size =
958 sizeof(bridge_control_table32) / sizeof(bridge_control_table32[0]);
959
960 static LIST_HEAD(, bridge_softc) bridge_list =
961 LIST_HEAD_INITIALIZER(bridge_list);
962
963 static lck_grp_t *bridge_lock_grp = NULL;
964 static lck_attr_t *bridge_lock_attr = NULL;
965
966 #define BRIDGENAME "bridge"
967 #define BRIDGES_MAX IF_MAXUNIT
968 #define BRIDGE_ZONE_MAX_ELEM MIN(IFNETS_MAX, BRIDGES_MAX)
969
970 static struct if_clone bridge_cloner =
971 IF_CLONE_INITIALIZER(BRIDGENAME, bridge_clone_create, bridge_clone_destroy,
972 0, BRIDGES_MAX, BRIDGE_ZONE_MAX_ELEM, sizeof(struct bridge_softc));
973
974 static int if_bridge_txstart = 0;
975 SYSCTL_INT(_net_link_bridge, OID_AUTO, txstart, CTLFLAG_RW | CTLFLAG_LOCKED,
976 &if_bridge_txstart, 0, "Bridge interface uses TXSTART model");
977
978 #if BRIDGE_DEBUG
979 static int if_bridge_debug = 0;
980 SYSCTL_INT(_net_link_bridge, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_LOCKED,
981 &if_bridge_debug, 0, "Bridge debug");
982
983 static void printf_ether_header(struct ether_header *);
984 static void printf_mbuf_data(mbuf_t, size_t, size_t);
985 static void printf_mbuf_pkthdr(mbuf_t, const char *, const char *);
986 static void printf_mbuf(mbuf_t, const char *, const char *);
987 static void link_print(struct bridge_softc * sc);
988
989 static void bridge_lock(struct bridge_softc *);
990 static void bridge_unlock(struct bridge_softc *);
991 static int bridge_lock2ref(struct bridge_softc *);
992 static void bridge_unref(struct bridge_softc *);
993 static void bridge_xlock(struct bridge_softc *);
994 static void bridge_xdrop(struct bridge_softc *);
995
996 static void
997 bridge_lock(struct bridge_softc *sc)
998 {
999 void *lr_saved = __builtin_return_address(0);
1000
1001 BRIDGE_LOCK_ASSERT_NOTHELD(sc);
1002
1003 _BRIDGE_LOCK(sc);
1004
1005 sc->lock_lr[sc->next_lock_lr] = lr_saved;
1006 sc->next_lock_lr = (sc->next_lock_lr + 1) % SO_LCKDBG_MAX;
1007 }
1008
1009 static void
1010 bridge_unlock(struct bridge_softc *sc)
1011 {
1012 void *lr_saved = __builtin_return_address(0);
1013
1014 BRIDGE_LOCK_ASSERT_HELD(sc);
1015
1016 sc->unlock_lr[sc->next_unlock_lr] = lr_saved;
1017 sc->next_unlock_lr = (sc->next_unlock_lr + 1) % SO_LCKDBG_MAX;
1018
1019 _BRIDGE_UNLOCK(sc);
1020 }
1021
1022 static int
1023 bridge_lock2ref(struct bridge_softc *sc)
1024 {
1025 int error = 0;
1026 void *lr_saved = __builtin_return_address(0);
1027
1028 BRIDGE_LOCK_ASSERT_HELD(sc);
1029
1030 if (sc->sc_iflist_xcnt > 0) {
1031 error = EBUSY;
1032 } else {
1033 sc->sc_iflist_ref++;
1034 }
1035
1036 sc->unlock_lr[sc->next_unlock_lr] = lr_saved;
1037 sc->next_unlock_lr = (sc->next_unlock_lr + 1) % SO_LCKDBG_MAX;
1038
1039 _BRIDGE_UNLOCK(sc);
1040
1041 return error;
1042 }
1043
1044 static void
1045 bridge_unref(struct bridge_softc *sc)
1046 {
1047 void *lr_saved = __builtin_return_address(0);
1048
1049 BRIDGE_LOCK_ASSERT_NOTHELD(sc);
1050
1051 _BRIDGE_LOCK(sc);
1052 sc->lock_lr[sc->next_lock_lr] = lr_saved;
1053 sc->next_lock_lr = (sc->next_lock_lr + 1) % SO_LCKDBG_MAX;
1054
1055 sc->sc_iflist_ref--;
1056
1057 sc->unlock_lr[sc->next_unlock_lr] = lr_saved;
1058 sc->next_unlock_lr = (sc->next_unlock_lr + 1) % SO_LCKDBG_MAX;
1059 if ((sc->sc_iflist_xcnt > 0) && (sc->sc_iflist_ref == 0)) {
1060 _BRIDGE_UNLOCK(sc);
1061 wakeup(&sc->sc_cv);
1062 } else {
1063 _BRIDGE_UNLOCK(sc);
1064 }
1065 }
1066
1067 static void
1068 bridge_xlock(struct bridge_softc *sc)
1069 {
1070 void *lr_saved = __builtin_return_address(0);
1071
1072 BRIDGE_LOCK_ASSERT_HELD(sc);
1073
1074 sc->sc_iflist_xcnt++;
1075 while (sc->sc_iflist_ref > 0) {
1076 sc->unlock_lr[sc->next_unlock_lr] = lr_saved;
1077 sc->next_unlock_lr = (sc->next_unlock_lr + 1) % SO_LCKDBG_MAX;
1078
1079 msleep(&sc->sc_cv, &sc->sc_mtx, PZERO, "BRIDGE_XLOCK", NULL);
1080
1081 sc->lock_lr[sc->next_lock_lr] = lr_saved;
1082 sc->next_lock_lr = (sc->next_lock_lr + 1) % SO_LCKDBG_MAX;
1083 }
1084 }
1085
1086 static void
1087 bridge_xdrop(struct bridge_softc *sc)
1088 {
1089 BRIDGE_LOCK_ASSERT_HELD(sc);
1090
1091 sc->sc_iflist_xcnt--;
1092 }
1093
1094 void
1095 printf_mbuf_pkthdr(mbuf_t m, const char *prefix, const char *suffix)
1096 {
1097 if (m) {
1098 printf("%spktlen: %u rcvif: 0x%llx header: 0x%llx "
1099 "nextpkt: 0x%llx%s",
1100 prefix ? prefix : "", (unsigned int)mbuf_pkthdr_len(m),
1101 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_pkthdr_rcvif(m)),
1102 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_pkthdr_header(m)),
1103 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_nextpkt(m)),
1104 suffix ? suffix : "");
1105 } else {
1106 printf("%s<NULL>%s\n", prefix, suffix);
1107 }
1108 }
1109
1110 void
1111 printf_mbuf(mbuf_t m, const char *prefix, const char *suffix)
1112 {
1113 if (m) {
1114 printf("%s0x%llx type: %u flags: 0x%x len: %u data: 0x%llx "
1115 "maxlen: %u datastart: 0x%llx next: 0x%llx%s",
1116 prefix ? prefix : "", (uint64_t)VM_KERNEL_ADDRPERM(m),
1117 mbuf_type(m), mbuf_flags(m), (unsigned int)mbuf_len(m),
1118 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m)),
1119 (unsigned int)mbuf_maxlen(m),
1120 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_datastart(m)),
1121 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_next(m)),
1122 !suffix || (mbuf_flags(m) & MBUF_PKTHDR) ? "" : suffix);
1123 if ((mbuf_flags(m) & MBUF_PKTHDR)) {
1124 printf_mbuf_pkthdr(m, " ", suffix);
1125 }
1126 } else {
1127 printf("%s<NULL>%s\n", prefix, suffix);
1128 }
1129 }
1130
1131 void
1132 printf_mbuf_data(mbuf_t m, size_t offset, size_t len)
1133 {
1134 mbuf_t n;
1135 size_t i, j;
1136 size_t pktlen, mlen, maxlen;
1137 unsigned char *ptr;
1138
1139 pktlen = mbuf_pkthdr_len(m);
1140
1141 if (offset > pktlen) {
1142 return;
1143 }
1144
1145 maxlen = (pktlen - offset > len) ? len : pktlen - offset;
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);
1152 if (n == 0) {
1153 break;
1154 }
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 }
1163 }
1164
1165 static void
1166 printf_ether_header(struct ether_header *eh)
1167 {
1168 printf("%02x:%02x:%02x:%02x:%02x:%02x > "
1169 "%02x:%02x:%02x:%02x:%02x:%02x 0x%04x ",
1170 eh->ether_shost[0], eh->ether_shost[1], eh->ether_shost[2],
1171 eh->ether_shost[3], eh->ether_shost[4], eh->ether_shost[5],
1172 eh->ether_dhost[0], eh->ether_dhost[1], eh->ether_dhost[2],
1173 eh->ether_dhost[3], eh->ether_dhost[4], eh->ether_dhost[5],
1174 ntohs(eh->ether_type));
1175 }
1176
1177 static void
1178 link_print(struct bridge_softc * sc)
1179 {
1180 int i;
1181 uint32_t sdl_buffer[offsetof(struct sockaddr_dl, sdl_data) +
1182 IFNAMSIZ + ETHER_ADDR_LEN];
1183 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sdl_buffer;
1184
1185 memset(sdl, 0, sizeof(sdl_buffer));
1186 sdl->sdl_family = AF_LINK;
1187 sdl->sdl_nlen = strlen(sc->sc_if_xname);
1188 sdl->sdl_alen = ETHER_ADDR_LEN;
1189 sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data);
1190 memcpy(sdl->sdl_data, sc->sc_if_xname, sdl->sdl_nlen);
1191 memcpy(LLADDR(sdl), sc->sc_defaddr, ETHER_ADDR_LEN);
1192
1193 #if 1
1194 printf("sdl len %d index %d family %d type 0x%x nlen %d alen %d"
1195 " slen %d addr ", sdl->sdl_len, sdl->sdl_index,
1196 sdl->sdl_family, sdl->sdl_type, sdl->sdl_nlen,
1197 sdl->sdl_alen, sdl->sdl_slen);
1198 #endif
1199 for (i = 0; i < sdl->sdl_alen; i++) {
1200 printf("%s%x", i ? ":" : "", (CONST_LLADDR(sdl))[i]);
1201 }
1202 printf("\n");
1203 }
1204
1205 static boolean_t
1206 bridge_debug_flag_is_set(uint32_t flag)
1207 {
1208 return (if_bridge_debug & flag) != 0;
1209 }
1210
1211 #endif /* BRIDGE_DEBUG */
1212
1213 /*
1214 * bridgeattach:
1215 *
1216 * Pseudo-device attach routine.
1217 */
1218 __private_extern__ int
1219 bridgeattach(int n)
1220 {
1221 #pragma unused(n)
1222 int error;
1223 lck_grp_attr_t *lck_grp_attr = NULL;
1224
1225 bridge_rtnode_pool = zinit(sizeof(struct bridge_rtnode),
1226 1024 * sizeof(struct bridge_rtnode), 0, "bridge_rtnode");
1227 zone_change(bridge_rtnode_pool, Z_CALLERACCT, FALSE);
1228
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
1233 lck_grp_attr = lck_grp_attr_alloc_init();
1234
1235 bridge_lock_grp = lck_grp_alloc_init("if_bridge", lck_grp_attr);
1236
1237 bridge_lock_attr = lck_attr_alloc_init();
1238
1239 #if BRIDGE_DEBUG
1240 lck_attr_setdebug(bridge_lock_attr);
1241 #endif
1242
1243 lck_mtx_init(&bridge_list_mtx, bridge_lock_grp, bridge_lock_attr);
1244
1245 /* can free the attributes once we've allocated the group lock */
1246 lck_grp_attr_free(lck_grp_attr);
1247
1248 LIST_INIT(&bridge_list);
1249
1250 #if BRIDGESTP
1251 bstp_sys_init();
1252 #endif /* BRIDGESTP */
1253
1254 error = if_clone_attach(&bridge_cloner);
1255 if (error != 0) {
1256 printf("%s: ifnet_clone_attach failed %d\n", __func__, error);
1257 }
1258
1259 return error;
1260 }
1261
1262 #if defined(PFIL_HOOKS)
1263 /*
1264 * handler for net.link.bridge.pfil_ipfw
1265 */
1266 static int
1267 sysctl_pfil_ipfw SYSCTL_HANDLER_ARGS
1268 {
1269 #pragma unused(arg1, arg2)
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
1292 return error;
1293 }
1294
1295 SYSCTL_PROC(_net_link_bridge, OID_AUTO, ipfw, CTLTYPE_INT | CTLFLAG_RW,
1296 &pfil_ipfw, 0, &sysctl_pfil_ipfw, "I", "Layer2 filter with IPFW");
1297 #endif /* PFIL_HOOKS */
1298
1299 static errno_t
1300 bridge_ifnet_set_attrs(struct ifnet * ifp)
1301 {
1302 errno_t error;
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 }
1327 done:
1328 return error;
1329 }
1330
1331 /*
1332 * bridge_clone_create:
1333 *
1334 * Create a new bridge instance.
1335 */
1336 static int
1337 bridge_clone_create(struct if_clone *ifc, uint32_t unit, void *params)
1338 {
1339 #pragma unused(params)
1340 struct ifnet *ifp = NULL;
1341 struct bridge_softc *sc = NULL;
1342 struct bridge_softc *sc2 = NULL;
1343 struct ifnet_init_eparams init_params;
1344 errno_t error = 0;
1345 uint8_t eth_hostid[ETHER_ADDR_LEN];
1346 int fb, retry, has_hostid;
1347
1348 sc = if_clone_softc_allocate(&bridge_cloner);
1349 if (sc == NULL) {
1350 error = ENOMEM;
1351 goto done;
1352 }
1353
1354 lck_mtx_init(&sc->sc_mtx, bridge_lock_grp, bridge_lock_attr);
1355 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
1356 sc->sc_mne_max = BRIDGE_MAC_NAT_ENTRY_MAX;
1357 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
1358 sc->sc_filter_flags = 0;
1359
1360 TAILQ_INIT(&sc->sc_iflist);
1361
1362 /* use the interface name as the unique id for ifp recycle */
1363 snprintf(sc->sc_if_xname, sizeof(sc->sc_if_xname), "%s%d",
1364 ifc->ifc_name, unit);
1365 bzero(&init_params, sizeof(init_params));
1366 init_params.ver = IFNET_INIT_CURRENT_VERSION;
1367 init_params.len = sizeof(init_params);
1368 /* Initialize our routing table. */
1369 error = bridge_rtable_init(sc);
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;
1381 }
1382 init_params.set_bpf_tap = bridge_set_bpf_tap;
1383 init_params.uniqueid = sc->sc_if_xname;
1384 init_params.uniqueid_len = strlen(sc->sc_if_xname);
1385 init_params.sndq_maxlen = IFQ_MAXLEN;
1386 init_params.name = ifc->ifc_name;
1387 init_params.unit = unit;
1388 init_params.family = IFNET_FAMILY_ETHERNET;
1389 init_params.type = IFT_BRIDGE;
1390 init_params.demux = ether_demux;
1391 init_params.add_proto = ether_add_proto;
1392 init_params.del_proto = ether_del_proto;
1393 init_params.check_multi = ether_check_multi;
1394 init_params.framer_extended = ether_frameout_extended;
1395 init_params.softc = sc;
1396 init_params.ioctl = bridge_ioctl;
1397 init_params.detach = bridge_detach;
1398 init_params.broadcast_addr = etherbroadcastaddr;
1399 init_params.broadcast_len = ETHER_ADDR_LEN;
1400
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 }
1407 LIST_INIT(&sc->sc_mne_list);
1408 LIST_INIT(&sc->sc_mne_list_v6);
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;
1415 }
1416 /*
1417 * Generate an ethernet address with a locally administered address.
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.
1422 * The first try uses the "hostid" and falls back to read_frandom();
1423 * for "hostid", we use the MAC address of the first-encountered
1424 * Ethernet-type interface that is currently configured.
1425 */
1426 fb = 0;
1427 has_hostid = (uuid_get_ethernet(&eth_hostid[0]) == 0);
1428 for (retry = 1; retry != 0;) {
1429 if (fb || has_hostid == 0) {
1430 read_frandom(&sc->sc_defaddr, ETHER_ADDR_LEN);
1431 sc->sc_defaddr[0] &= ~1; /* clear multicast bit */
1432 sc->sc_defaddr[0] |= 2; /* set the LAA bit */
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 */
1438 sc->sc_defaddr[3] = /* stir it up a bit */
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) |
1447 ((sc->sc_defaddr[4] & 0xf0) >> 4)) ^
1448 sc->sc_defaddr[5];
1449 sc->sc_defaddr[5] = ifp->if_unit & 0xff;
1450 }
1451
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,
1457 IF_LLADDR(sc2->sc_ifp), ETHER_ADDR_LEN) == 0) {
1458 retry = 1;
1459 }
1460 }
1461 lck_mtx_unlock(&bridge_list_mtx);
1462 }
1463
1464 sc->sc_flags &= ~SCF_MEDIA_ACTIVE;
1465
1466 #if BRIDGE_DEBUG
1467 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE)) {
1468 link_print(sc);
1469 }
1470 #endif
1471 error = ifnet_attach(ifp, NULL);
1472 if (error != 0) {
1473 printf("%s: ifnet_attach failed %d\n", __func__, error);
1474 goto done;
1475 }
1476
1477 error = ifnet_set_lladdr_and_type(ifp, sc->sc_defaddr, ETHER_ADDR_LEN,
1478 IFT_ETHER);
1479 if (error != 0) {
1480 printf("%s: ifnet_set_lladdr_and_type failed %d\n", __func__,
1481 error);
1482 goto done;
1483 }
1484
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 }
1494 #if BRIDGESTP
1495 bstp_attach(&sc->sc_stp, &bridge_ops);
1496 #endif /* BRIDGESTP */
1497
1498 lck_mtx_lock(&bridge_list_mtx);
1499 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
1500 lck_mtx_unlock(&bridge_list_mtx);
1501
1502 /* attach as ethernet */
1503 error = bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header),
1504 NULL, NULL);
1505
1506 done:
1507 if (error != 0) {
1508 printf("%s failed error %d\n", __func__, error);
1509 /* TBD: Clean up: sc, sc_rthash etc */
1510 }
1511
1512 return error;
1513 }
1514
1515 /*
1516 * bridge_clone_destroy:
1517 *
1518 * Destroy a bridge instance.
1519 */
1520 static int
1521 bridge_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);
1530 return 0;
1531 }
1532 sc->sc_flags |= SCF_DETACHING;
1533
1534 bridge_ifstop(ifp, 1);
1535
1536 bridge_cancel_delayed_call(&sc->sc_resize_call);
1537
1538 bridge_cleanup_delayed_call(&sc->sc_resize_call);
1539 bridge_cleanup_delayed_call(&sc->sc_aging_timer);
1540
1541 error = ifnet_set_flags(ifp, 0, IFF_UP);
1542 if (error != 0) {
1543 printf("%s: ifnet_set_flags failed %d\n", __func__, error);
1544 }
1545
1546 while ((bif = TAILQ_FIRST(&sc->sc_iflist)) != NULL) {
1547 bridge_delete_member(sc, bif, 0);
1548 }
1549
1550 while ((bif = TAILQ_FIRST(&sc->sc_spanlist)) != NULL) {
1551 bridge_delete_span(sc, bif);
1552 }
1553 BRIDGE_UNLOCK(sc);
1554
1555 error = ifnet_detach(ifp);
1556 if (error != 0) {
1557 panic("%s: ifnet_detach(%p) failed %d\n",
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); \
1608 } while (0)
1609
1610 /*
1611 * bridge_ioctl:
1612 *
1613 * Handle a control request from the operator.
1614 */
1615 static errno_t
1616 bridge_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1617 {
1618 struct bridge_softc *sc = ifp->if_softc;
1619 struct ifreq *ifr = (struct ifreq *)data;
1620 struct bridge_iflist *bif;
1621 int error = 0;
1622
1623 BRIDGE_LOCK_ASSERT_NOTHELD(sc);
1624
1625 #if BRIDGE_DEBUG
1626 if (IF_BRIDGE_DEBUG(BR_DBGF_IOCTL)) {
1627 printf("%s: ifp %s cmd 0x%08lx (%c%c [%lu] %c %lu)\n",
1628 __func__, ifp->if_xname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
1629 (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
1630 (char)IOCGROUP(cmd), cmd & 0xff);
1631 }
1632 #endif /* BRIDGE_DEBUG */
1633
1634 switch (cmd) {
1635 case SIOCSIFADDR:
1636 case SIOCAIFADDR:
1637 ifnet_set_flags(ifp, IFF_UP, IFF_UP);
1638 break;
1639
1640 case SIOCGIFMEDIA32:
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,
1666 sizeof(int));
1667 }
1668 break;
1669 }
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;
1685 struct ifdrv32 *ifd = (struct ifdrv32 *)data;
1686 const struct bridge_control *bridge_control_table =
1687 bridge_control_table32, *bc;
1688
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;
1703 struct ifdrv64 *ifd = (struct ifdrv64 *)data;
1704 const struct bridge_control *bridge_control_table =
1705 bridge_control_table64, *bc;
1706
1707 DRVSPEC;
1708
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);
1720 bridge_ifstop(ifp, 1);
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:
1735 error = ifnet_set_lladdr(ifp, ifr->ifr_addr.sa_data,
1736 ifr->ifr_addr.sa_len);
1737 if (error != 0) {
1738 printf("%s: SIOCSIFLLADDR error %d\n", ifp->if_xname,
1739 error);
1740 }
1741 break;
1742
1743 case SIOCSIFMTU:
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 }
1764 if (!error) {
1765 sc->sc_ifp->if_mtu = ifr->ifr_mtu;
1766 }
1767 BRIDGE_UNLOCK(sc);
1768 break;
1769
1770 default:
1771 error = ether_ioctl(ifp, cmd, data);
1772 #if BRIDGE_DEBUG
1773 if (error != 0 && error != EOPNOTSUPP) {
1774 printf("%s: ifp %s cmd 0x%08lx "
1775 "(%c%c [%lu] %c %lu) failed error: %d\n",
1776 __func__, ifp->if_xname, cmd,
1777 (cmd & IOC_IN) ? 'I' : ' ',
1778 (cmd & IOC_OUT) ? 'O' : ' ',
1779 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd),
1780 cmd & 0xff, error);
1781 }
1782 #endif /* BRIDGE_DEBUG */
1783 break;
1784 }
1785 BRIDGE_LOCK_ASSERT_NOTHELD(sc);
1786
1787 return error;
1788 }
1789
1790 #if HAS_IF_CAP
1791 /*
1792 * bridge_mutecaps:
1793 *
1794 * Clear or restore unwanted capabilities on the member interface
1795 */
1796 static void
1797 bridge_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 }
1819 }
1820
1821 static void
1822 bridge_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
1828 bzero(&ifr, sizeof(ifr));
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);
1835 if (error) {
1836 printf("%s: %s error setting interface capabilities "
1837 "on %s\n", __func__, sc->sc_ifp->if_xname,
1838 ifp->if_xname);
1839 }
1840 }
1841 }
1842 #endif /* HAS_IF_CAP */
1843
1844 static errno_t
1845 bridge_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
1862 if (ifp == NULL) {
1863 continue;
1864 }
1865
1866 if (offload & IFNET_TSO_IPV4) {
1867 if (ifp->if_hwassist & IFNET_TSO_IPV4) {
1868 if (tso_v4_mtu > ifp->if_tso_v4_mtu) {
1869 tso_v4_mtu = ifp->if_tso_v4_mtu;
1870 }
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) {
1878 if (tso_v6_mtu > ifp->if_tso_v6_mtu) {
1879 tso_v6_mtu = ifp->if_tso_v6_mtu;
1880 }
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
1892 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE)) {
1893 printf("%s: ifnet_set_offload(%s, 0x%x) "
1894 "failed %d\n", __func__,
1895 sc->sc_ifp->if_xname, offload, error);
1896 }
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) {
1905 if (tso_v4_mtu < sc->sc_ifp->if_mtu) {
1906 tso_v4_mtu = sc->sc_ifp->if_mtu;
1907 }
1908 error = ifnet_set_tso_mtu(sc->sc_ifp, AF_INET,
1909 tso_v4_mtu);
1910 if (error != 0) {
1911 #if BRIDGE_DEBUG
1912 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE)) {
1913 printf("%s: ifnet_set_tso_mtu(%s, "
1914 "AF_INET, %u) failed %d\n",
1915 __func__, sc->sc_ifp->if_xname,
1916 tso_v4_mtu, error);
1917 }
1918 #endif /* BRIDGE_DEBUG */
1919 goto done;
1920 }
1921 }
1922 if (sc->sc_ifp->if_hwassist & IFNET_TSO_IPV6) {
1923 if (tso_v6_mtu < sc->sc_ifp->if_mtu) {
1924 tso_v6_mtu = sc->sc_ifp->if_mtu;
1925 }
1926 error = ifnet_set_tso_mtu(sc->sc_ifp, AF_INET6,
1927 tso_v6_mtu);
1928 if (error != 0) {
1929 #if BRIDGE_DEBUG
1930 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE)) {
1931 printf("%s: ifnet_set_tso_mtu(%s, "
1932 "AF_INET6, %u) failed %d\n",
1933 __func__, sc->sc_ifp->if_xname,
1934 tso_v6_mtu, error);
1935 }
1936 #endif /* BRIDGE_DEBUG */
1937 goto done;
1938 }
1939 }
1940 }
1941 done:
1942 return error;
1943 }
1944
1945 /*
1946 * bridge_lookup_member:
1947 *
1948 * Lookup a bridge member interface.
1949 */
1950 static struct bridge_iflist *
1951 bridge_lookup_member(struct bridge_softc *sc, const char *name)
1952 {
1953 struct bridge_iflist *bif;
1954 struct ifnet *ifp;
1955
1956 BRIDGE_LOCK_ASSERT_HELD(sc);
1957
1958 TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) {
1959 ifp = bif->bif_ifp;
1960 if (strcmp(ifp->if_xname, name) == 0) {
1961 return bif;
1962 }
1963 }
1964
1965 return NULL;
1966 }
1967
1968 /*
1969 * bridge_lookup_member_if:
1970 *
1971 * Lookup a bridge member interface by ifnet*.
1972 */
1973 static struct bridge_iflist *
1974 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
1975 {
1976 struct bridge_iflist *bif;
1977
1978 BRIDGE_LOCK_ASSERT_HELD(sc);
1979
1980 TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) {
1981 if (bif->bif_ifp == member_ifp) {
1982 return bif;
1983 }
1984 }
1985
1986 return NULL;
1987 }
1988
1989 static errno_t
1990 bridge_iff_input(void *cookie, ifnet_t ifp, protocol_family_t protocol,
1991 mbuf_t *data, char **frame_ptr)
1992 {
1993 #pragma unused(protocol)
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
2001 if ((m->m_flags & M_PROTO1)) {
2002 goto out;
2003 }
2004
2005 if (*frame_ptr >= (char *)mbuf_datastart(m) &&
2006 *frame_ptr <= (char *)mbuf_data(m)) {
2007 included = 1;
2008 frmlen = (char *)mbuf_data(m) - *frame_ptr;
2009 }
2010 #if BRIDGE_DEBUG
2011 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT)) {
2012 printf("%s: %s from %s m 0x%llx data 0x%llx frame 0x%llx %s "
2013 "frmlen %lu\n", __func__, sc->sc_ifp->if_xname,
2014 ifp->if_xname, (uint64_t)VM_KERNEL_ADDRPERM(m),
2015 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m)),
2016 (uint64_t)VM_KERNEL_ADDRPERM(*frame_ptr),
2017 included ? "inside" : "outside", frmlen);
2018
2019 if (IF_BRIDGE_DEBUG(BR_DBGF_MBUF)) {
2020 printf_mbuf(m, "bridge_iff_input[", "\n");
2021 printf_ether_header((struct ether_header *)
2022 (void *)*frame_ptr);
2023 printf_mbuf_data(m, 0, 20);
2024 printf("\n");
2025 }
2026 }
2027 #endif /* BRIDGE_DEBUG */
2028 if (included == 0) {
2029 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT)) {
2030 printf("%s: frame_ptr outside mbuf\n", __func__);
2031 }
2032 goto out;
2033 }
2034
2035 /* Move data pointer to start of frame to the link layer header */
2036 (void) mbuf_setdata(m, (char *)mbuf_data(m) - frmlen,
2037 mbuf_len(m) + frmlen);
2038 (void) mbuf_pkthdr_adjustlen(m, frmlen);
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 }
2046 goto out;
2047 }
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 }
2064
2065 error = bridge_input(ifp, data);
2066
2067 /* Adjust packet back to original */
2068 if (error == 0) {
2069 /* bridge_input might have modified *data */
2070 if (*data != m) {
2071 m = *data;
2072 *frame_ptr = mbuf_data(m);
2073 }
2074 (void) mbuf_setdata(m, (char *)mbuf_data(m) + frmlen,
2075 mbuf_len(m) - frmlen);
2076 (void) mbuf_pkthdr_adjustlen(m, -frmlen);
2077 }
2078 #if BRIDGE_DEBUG
2079 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT) &&
2080 IF_BRIDGE_DEBUG(BR_DBGF_MBUF)) {
2081 printf("\n");
2082 printf_mbuf(m, "bridge_iff_input]", "\n");
2083 }
2084 #endif /* BRIDGE_DEBUG */
2085
2086 out:
2087 BRIDGE_LOCK_ASSERT_NOTHELD(sc);
2088
2089 return error;
2090 }
2091
2092 static errno_t
2093 bridge_iff_output(void *cookie, ifnet_t ifp, protocol_family_t protocol,
2094 mbuf_t *data)
2095 {
2096 #pragma unused(protocol)
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;
2101
2102 if ((m->m_flags & M_PROTO1)) {
2103 goto out;
2104 }
2105
2106 #if BRIDGE_DEBUG
2107 if (IF_BRIDGE_DEBUG(BR_DBGF_OUTPUT)) {
2108 printf("%s: %s from %s m 0x%llx data 0x%llx\n", __func__,
2109 sc->sc_ifp->if_xname, ifp->if_xname,
2110 (uint64_t)VM_KERNEL_ADDRPERM(m),
2111 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m)));
2112 }
2113 #endif /* BRIDGE_DEBUG */
2114
2115 error = bridge_member_output(sc, ifp, data);
2116 if (error != 0 && error != EJUSTRETURN) {
2117 printf("%s: bridge_member_output failed error %d\n", __func__,
2118 error);
2119 }
2120 out:
2121 BRIDGE_LOCK_ASSERT_NOTHELD(sc);
2122
2123 return error;
2124 }
2125
2126 static void
2127 bridge_iff_event(void *cookie, ifnet_t ifp, protocol_family_t protocol,
2128 const struct kev_msg *event_msg)
2129 {
2130 #pragma unused(protocol)
2131 struct bridge_iflist *bif = (struct bridge_iflist *)cookie;
2132 struct bridge_softc *sc = bif->bif_sc;
2133
2134 if (event_msg->vendor_code == KEV_VENDOR_APPLE &&
2135 event_msg->kev_class == KEV_NETWORK_CLASS &&
2136 event_msg->kev_subclass == KEV_DL_SUBCLASS) {
2137 #if BRIDGE_DEBUG
2138 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE)) {
2139 printf("%s: %s event_code %u - %s\n", __func__,
2140 ifp->if_xname, event_msg->event_code,
2141 dlil_kev_dl_code_str(event_msg->event_code));
2142 }
2143 #endif /* BRIDGE_DEBUG */
2144
2145 switch (event_msg->event_code) {
2146 case KEV_DL_IF_DETACHING:
2147 case KEV_DL_IF_DETACHED: {
2148 bridge_ifdetach(ifp);
2149 break;
2150 }
2151 case KEV_DL_LINK_OFF:
2152 case KEV_DL_LINK_ON: {
2153 bridge_iflinkevent(ifp);
2154 #if BRIDGESTP
2155 bstp_linkstate(ifp, event_msg->event_code);
2156 #endif /* BRIDGESTP */
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;
2173 }
2174 }
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 }
2183 case KEV_DL_PROTO_DETACHED:
2184 case KEV_DL_PROTO_ATTACHED: {
2185 bridge_proto_attach_changed(ifp);
2186 break;
2187 }
2188 default:
2189 break;
2190 }
2191 }
2192 }
2193
2194 /*
2195 * bridge_iff_detached:
2196 *
2197 * Detach an interface from a bridge. Called when a member
2198 * interface is detaching.
2199 */
2200 static void
2201 bridge_iff_detached(void *cookie, ifnet_t ifp)
2202 {
2203 struct bridge_iflist *bif = (struct bridge_iflist *)cookie;
2204
2205 #if BRIDGE_DEBUG
2206 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE)) {
2207 printf("%s: %s\n", __func__, ifp->if_xname);
2208 }
2209 #endif /* BRIDGE_DEBUG */
2210
2211 bridge_ifdetach(ifp);
2212
2213 _FREE(bif, M_DEVBUF);
2214 }
2215
2216 static errno_t
2217 bridge_proto_input(ifnet_t ifp, protocol_family_t protocol, mbuf_t packet,
2218 char *header)
2219 {
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 */
2225 return 0;
2226 }
2227
2228 static int
2229 bridge_attach_protocol(struct ifnet *ifp)
2230 {
2231 int error;
2232 struct ifnet_attach_proto_param reg;
2233
2234 #if BRIDGE_DEBUG
2235 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE)) {
2236 printf("%s: %s\n", __func__, ifp->if_xname);
2237 }
2238 #endif /* BRIDGE_DEBUG */
2239
2240 bzero(&reg, sizeof(reg));
2241 reg.input = bridge_proto_input;
2242
2243 error = ifnet_attach_protocol(ifp, PF_BRIDGE, &reg);
2244 if (error) {
2245 printf("%s: ifnet_attach_protocol(%s) failed, %d\n",
2246 __func__, ifp->if_xname, error);
2247 }
2248
2249 return error;
2250 }
2251
2252 static int
2253 bridge_detach_protocol(struct ifnet *ifp)
2254 {
2255 int error;
2256
2257 #if BRIDGE_DEBUG
2258 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE)) {
2259 printf("%s: %s\n", __func__, ifp->if_xname);
2260 }
2261 #endif /* BRIDGE_DEBUG */
2262 error = ifnet_detach_protocol(ifp, PF_BRIDGE);
2263 if (error) {
2264 printf("%s: ifnet_detach_protocol(%s) failed, %d\n",
2265 __func__, ifp->if_xname, error);
2266 }
2267
2268 return error;
2269 }
2270
2271 /*
2272 * bridge_delete_member:
2273 *
2274 * Delete the specified member interface.
2275 */
2276 static void
2277 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
2278 int gone)
2279 {
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;
2284
2285 BRIDGE_LOCK_ASSERT_HELD(sc);
2286 VERIFY(ifs != NULL);
2287
2288 /*
2289 * Remove the member from the list first so it cannot be found anymore
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
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
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 */
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);
2317 (void) ifnet_set_promiscuous(ifs, 0);
2318 BRIDGE_LOCK(sc);
2319 }
2320 break;
2321
2322 case IFT_GIF:
2323 /* currently not supported */
2324 /* FALLTHRU */
2325 default:
2326 VERIFY(0);
2327 /* NOTREACHED */
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
2336 if (bif->bif_flags & BIFF_PROTO_ATTACHED) {
2337 /* Respect lock ordering with DLIL lock */
2338 BRIDGE_UNLOCK(sc);
2339 (void) bridge_detach_protocol(ifs);
2340 BRIDGE_LOCK(sc);
2341 }
2342 #if BRIDGESTP
2343 if ((bif->bif_ifflags & IFBIF_STP) != 0) {
2344 bstp_disable(&bif->bif_stp);
2345 }
2346 #endif /* BRIDGESTP */
2347
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;
2363 ifnet_reference(fif); /* for sc_ifaddr */
2364 }
2365 lladdr_changed = 1;
2366 }
2367
2368 #if HAS_IF_CAP
2369 bridge_mutecaps(sc); /* recalculate now this interface is removed */
2370 #endif /* HAS_IF_CAP */
2371
2372 error = bridge_set_tso(sc);
2373 if (error != 0) {
2374 printf("%s: bridge_set_tso failed %d\n", __func__, error);
2375 }
2376
2377 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
2378
2379 KASSERT(bif->bif_addrcnt == 0,
2380 ("%s: %d bridge routes referenced", __func__, bif->bif_addrcnt));
2381
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
2389 BRIDGE_UNLOCK(sc);
2390
2391
2392 if (lladdr_changed &&
2393 (error = ifnet_set_lladdr(bifp, eaddr, ETHER_ADDR_LEN)) != 0) {
2394 printf("%s: ifnet_set_lladdr failed %d\n", __func__, error);
2395 }
2396
2397 if (event_code != 0) {
2398 bridge_link_event(bifp, event_code);
2399 }
2400
2401 #if BRIDGESTP
2402 bstp_destroy(&bif->bif_stp); /* prepare to free */
2403 #endif /* BRIDGESTP */
2404
2405 if (filt_attached) {
2406 iflt_detach(bif->bif_iff_ref);
2407 } else {
2408 _FREE(bif, M_DEVBUF);
2409 }
2410
2411 ifs->if_bridge = NULL;
2412 ifnet_release(ifs);
2413
2414 BRIDGE_LOCK(sc);
2415 }
2416
2417 /*
2418 * bridge_delete_span:
2419 *
2420 * Delete the specified span interface.
2421 */
2422 static void
2423 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
2424 {
2425 BRIDGE_LOCK_ASSERT_HELD(sc);
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
2436 static int
2437 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
2438 {
2439 struct ifbreq *req = arg;
2440 struct bridge_iflist *bif = NULL;
2441 struct ifnet *ifs, *bifp = sc->sc_ifp;
2442 int error = 0, lladdr_changed = 0;
2443 uint8_t eaddr[ETHER_ADDR_LEN];
2444 struct iff_filter iff;
2445 u_int32_t event_code = 0;
2446 boolean_t mac_nat = FALSE;
2447
2448 ifs = ifunit(req->ifbr_ifsname);
2449 if (ifs == NULL) {
2450 return ENOENT;
2451 }
2452 if (ifs->if_ioctl == NULL) { /* must be supported */
2453 return EINVAL;
2454 }
2455
2456 if (IFNET_IS_INTCOPROC(ifs)) {
2457 return EINVAL;
2458 }
2459
2460 /* If it's in the span list, it can't be a member. */
2461 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2462 if (ifs == bif->bif_ifp) {
2463 return EBUSY;
2464 }
2465 }
2466
2467 if (ifs->if_bridge == sc) {
2468 return EEXIST;
2469 }
2470
2471 if (ifs->if_bridge != NULL) {
2472 return EBUSY;
2473 }
2474
2475 switch (ifs->if_type) {
2476 case IFT_ETHER:
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 }
2482 case IFT_L2VLAN:
2483 /* permitted interface types */
2484 break;
2485 case IFT_GIF:
2486 /* currently not supported */
2487 /* FALLTHRU */
2488 default:
2489 return EINVAL;
2490 }
2491
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 }
2504 bif = _MALLOC(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO);
2505 if (bif == NULL) {
2506 return ENOMEM;
2507 }
2508 bif->bif_ifp = ifs;
2509 ifnet_reference(ifs);
2510 bif->bif_ifflags |= IFBIF_LEARNING | IFBIF_DISCOVER;
2511 #if HAS_IF_CAP
2512 bif->bif_savedcaps = ifs->if_capenable;
2513 #endif /* HAS_IF_CAP */
2514 bif->bif_sc = sc;
2515 if (mac_nat) {
2516 (void)bridge_mac_nat_enable(sc, bif);
2517 }
2518
2519 /* Allow the first Ethernet member to define the MTU */
2520 if (TAILQ_EMPTY(&sc->sc_iflist)) {
2521 sc->sc_ifp->if_mtu = ifs->if_mtu;
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;
2533 ifnet_reference(ifs); /* for sc_ifaddr */
2534 lladdr_changed = 1;
2535 }
2536
2537 ifs->if_bridge = sc;
2538 #if BRIDGESTP
2539 bstp_create(&sc->sc_stp, &bif->bif_stp, bif->bif_ifp);
2540 #endif /* BRIDGESTP */
2541
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
2552 bridge_set_tso(sc);
2553
2554
2555 /*
2556 * Place the interface into promiscuous mode.
2557 */
2558 switch (ifs->if_type) {
2559 case IFT_ETHER:
2560 case IFT_L2VLAN:
2561 error = ifnet_set_promiscuous(ifs, 1);
2562 if (error) {
2563 /* Ignore error when device is not up */
2564 if (error != ENETDOWN) {
2565 goto out;
2566 }
2567 error = 0;
2568 } else {
2569 bif->bif_flags |= BIFF_PROMISC;
2570 }
2571 break;
2572
2573 default:
2574 break;
2575 }
2576
2577 /*
2578 * The new member may change the link status of the bridge interface
2579 */
2580 if (interface_media_active(ifs)) {
2581 bif->bif_flags |= BIFF_MEDIA_ACTIVE;
2582 } else {
2583 bif->bif_flags &= ~BIFF_MEDIA_ACTIVE;
2584 }
2585
2586 event_code = bridge_updatelinkstatus(sc);
2587
2588 /*
2589 * Respect lock ordering with DLIL lock for the following operations
2590 */
2591 BRIDGE_UNLOCK(sc);
2592
2593
2594 /*
2595 * install an interface filter
2596 */
2597 memset(&iff, 0, sizeof(struct iff_filter));
2598 iff.iff_cookie = bif;
2599 iff.iff_name = "com.apple.kernel.bsd.net.if_bridge";
2600 iff.iff_input = bridge_iff_input;
2601 iff.iff_output = bridge_iff_output;
2602 iff.iff_event = bridge_iff_event;
2603 iff.iff_detached = bridge_iff_detached;
2604 error = dlil_attach_filter(ifs, &iff, &bif->bif_iff_ref,
2605 DLIL_IFF_TSO | DLIL_IFF_INTERNAL);
2606 if (error != 0) {
2607 printf("%s: iflt_attach failed %d\n", __func__, error);
2608 BRIDGE_LOCK(sc);
2609 goto out;
2610 }
2611 BRIDGE_LOCK(sc);
2612 bif->bif_flags |= BIFF_FILTER_ATTACHED;
2613 BRIDGE_UNLOCK(sc);
2614
2615 /*
2616 * install a dummy "bridge" protocol
2617 */
2618 if ((error = bridge_attach_protocol(ifs)) != 0) {
2619 if (error != 0) {
2620 printf("%s: bridge_attach_protocol failed %d\n",
2621 __func__, error);
2622 BRIDGE_LOCK(sc);
2623 goto out;
2624 }
2625 }
2626 BRIDGE_LOCK(sc);
2627 bif->bif_flags |= BIFF_PROTO_ATTACHED;
2628 BRIDGE_UNLOCK(sc);
2629
2630 if (lladdr_changed &&
2631 (error = ifnet_set_lladdr(bifp, eaddr, ETHER_ADDR_LEN)) != 0) {
2632 printf("%s: ifnet_set_lladdr failed %d\n", __func__, error);
2633 }
2634
2635 if (event_code != 0) {
2636 bridge_link_event(bifp, event_code);
2637 }
2638
2639 BRIDGE_LOCK(sc);
2640
2641 out:
2642 if (error && bif != NULL) {
2643 bridge_delete_member(sc, bif, 1);
2644 }
2645
2646 return error;
2647 }
2648
2649 static int
2650 bridge_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);
2656 if (bif == NULL) {
2657 return ENOENT;
2658 }
2659
2660 bridge_delete_member(sc, bif, 0);
2661
2662 return 0;
2663 }
2664
2665 static int
2666 bridge_ioctl_purge(struct bridge_softc *sc, void *arg)
2667 {
2668 #pragma unused(sc, arg)
2669 return 0;
2670 }
2671
2672 static int
2673 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
2674 {
2675 struct ifbreq *req = arg;
2676 struct bridge_iflist *bif;
2677
2678 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
2679 if (bif == NULL) {
2680 return ENOENT;
2681 }
2682
2683 struct bstp_port *bp;
2684
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;
2692 req->ifbr_ifsflags = bif->bif_ifflags;
2693
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;
2703 }
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
2714 req->ifbr_portno = bif->bif_ifp->if_index & 0xfff;
2715 req->ifbr_addrcnt = bif->bif_addrcnt;
2716 req->ifbr_addrmax = bif->bif_addrmax;
2717 req->ifbr_addrexceeded = bif->bif_addrexceeded;
2718
2719 return 0;
2720 }
2721
2722 static int
2723 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
2724 {
2725 struct ifbreq *req = arg;
2726 struct bridge_iflist *bif;
2727 #if BRIDGESTP
2728 struct bstp_port *bp;
2729 int error;
2730 #endif /* BRIDGESTP */
2731
2732 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
2733 if (bif == NULL) {
2734 return ENOENT;
2735 }
2736
2737 if (req->ifbr_ifsflags & IFBIF_SPAN) {
2738 /* SPAN is readonly */
2739 return EINVAL;
2740 }
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 }
2750
2751
2752 #if BRIDGESTP
2753 if (req->ifbr_ifsflags & IFBIF_STP) {
2754 if ((bif->bif_ifflags & IFBIF_STP) == 0) {
2755 error = bstp_enable(&bif->bif_stp);
2756 if (error) {
2757 return error;
2758 }
2759 }
2760 } else {
2761 if ((bif->bif_ifflags & IFBIF_STP) != 0) {
2762 bstp_disable(&bif->bif_stp);
2763 }
2764 }
2765
2766 /* Pass on STP flags */
2767 bp = &bif->bif_stp;
2768 bstp_set_edge(bp, req->ifbr_ifsflags & IFBIF_BSTP_EDGE ? 1 : 0);
2769 bstp_set_autoedge(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOEDGE ? 1 : 0);
2770 bstp_set_ptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_PTP ? 1 : 0);
2771 bstp_set_autoptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOPTP ? 1 : 0);
2772 #else /* !BRIDGESTP */
2773 if (req->ifbr_ifsflags & IFBIF_STP) {
2774 return EOPNOTSUPP;
2775 }
2776 #endif /* !BRIDGESTP */
2777
2778 /* Save the bits relating to the bridge */
2779 bif->bif_ifflags = req->ifbr_ifsflags & IFBIFMASK;
2780
2781
2782 return 0;
2783 }
2784
2785 static int
2786 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
2787 {
2788 struct ifbrparam *param = arg;
2789
2790 sc->sc_brtmax = param->ifbrp_csize;
2791 bridge_rttrim(sc);
2792 return 0;
2793 }
2794
2795 static int
2796 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
2797 {
2798 struct ifbrparam *param = arg;
2799
2800 param->ifbrp_csize = sc->sc_brtmax;
2801
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++; \
2814 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) \
2815 count++; \
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 } \
2845 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) { \
2846 if (len < sizeof (breq)) \
2847 break; \
2848 \
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); \
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); \
2866 } while (0)
2867
2868 static int
2869 bridge_ioctl_gifs64(struct bridge_softc *sc, void *arg)
2870 {
2871 struct ifbifconf64 *bifc = arg;
2872 int error = 0;
2873
2874 BRIDGE_IOCTL_GIFS;
2875
2876 return error;
2877 }
2878
2879 static int
2880 bridge_ioctl_gifs32(struct bridge_softc *sc, void *arg)
2881 {
2882 struct ifbifconf32 *bifc = arg;
2883 int error = 0;
2884
2885 BRIDGE_IOCTL_GIFS;
2886
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; \
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 } \
2934 out: \
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); \
2943 } while (0)
2944
2945 static int
2946 bridge_ioctl_rts64(struct bridge_softc *sc, void *arg)
2947 {
2948 struct ifbaconf64 *bac = arg;
2949 struct ifbareq64 bareq;
2950 int error = 0;
2951
2952 BRIDGE_IOCTL_RTS;
2953 return error;
2954 }
2955
2956 static int
2957 bridge_ioctl_rts32(struct bridge_softc *sc, void *arg)
2958 {
2959 struct ifbaconf32 *bac = arg;
2960 struct ifbareq32 bareq;
2961 int error = 0;
2962
2963 BRIDGE_IOCTL_RTS;
2964 return error;
2965 }
2966
2967 static int
2968 bridge_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);
2975 if (bif == NULL) {
2976 return ENOENT;
2977 }
2978
2979 error = bridge_rtupdate(sc, req->ifba_dst, req->ifba_vlan, bif, 1,
2980 req->ifba_flags);
2981
2982 return error;
2983 }
2984
2985 static int
2986 bridge_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);
2993 if (bif == NULL) {
2994 return ENOENT;
2995 }
2996
2997 error = bridge_rtupdate(sc, req->ifba_dst, req->ifba_vlan, bif, 1,
2998 req->ifba_flags);
2999
3000 return error;
3001 }
3002
3003 static int
3004 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
3005 {
3006 struct ifbrparam *param = arg;
3007
3008 sc->sc_brttimeout = param->ifbrp_ctime;
3009 return 0;
3010 }
3011
3012 static int
3013 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
3014 {
3015 struct ifbrparam *param = arg;
3016
3017 param->ifbrp_ctime = sc->sc_brttimeout;
3018 return 0;
3019 }
3020
3021 static int
3022 bridge_ioctl_daddr32(struct bridge_softc *sc, void *arg)
3023 {
3024 struct ifbareq32 *req = arg;
3025
3026 return bridge_rtdaddr(sc, req->ifba_dst, req->ifba_vlan);
3027 }
3028
3029 static int
3030 bridge_ioctl_daddr64(struct bridge_softc *sc, void *arg)
3031 {
3032 struct ifbareq64 *req = arg;
3033
3034 return bridge_rtdaddr(sc, req->ifba_dst, req->ifba_vlan);
3035 }
3036
3037 static int
3038 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
3039 {
3040 struct ifbreq *req = arg;
3041
3042 bridge_rtflush(sc, req->ifbr_ifsflags);
3043 return 0;
3044 }
3045
3046 static int
3047 bridge_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;
3053 return 0;
3054 }
3055
3056 static int
3057 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
3058 {
3059 #if BRIDGESTP
3060 struct ifbrparam *param = arg;
3061
3062 return bstp_set_priority(&sc->sc_stp, param->ifbrp_prio);
3063 #else /* !BRIDGESTP */
3064 #pragma unused(sc, arg)
3065 return EOPNOTSUPP;
3066 #endif /* !BRIDGESTP */
3067 }
3068
3069 static int
3070 bridge_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;
3076 return 0;
3077 }
3078
3079 static int
3080 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
3081 {
3082 #if BRIDGESTP
3083 struct ifbrparam *param = arg;
3084
3085 return bstp_set_htime(&sc->sc_stp, param->ifbrp_hellotime);
3086 #else /* !BRIDGESTP */
3087 #pragma unused(sc, arg)
3088 return EOPNOTSUPP;
3089 #endif /* !BRIDGESTP */
3090 }
3091
3092 static int
3093 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
3094 {
3095 struct ifbrparam *param;
3096 struct bstp_state *bs;
3097
3098 param = arg;
3099 bs = &sc->sc_stp;
3100 param->ifbrp_fwddelay = bs->bs_bridge_fdelay >> 8;
3101 return 0;
3102 }
3103
3104 static int
3105 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
3106 {
3107 #if BRIDGESTP
3108 struct ifbrparam *param = arg;
3109
3110 return bstp_set_fdelay(&sc->sc_stp, param->ifbrp_fwddelay);
3111 #else /* !BRIDGESTP */
3112 #pragma unused(sc, arg)
3113 return EOPNOTSUPP;
3114 #endif /* !BRIDGESTP */
3115 }
3116
3117 static int
3118 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
3119 {
3120 struct ifbrparam *param;
3121 struct bstp_state *bs;
3122
3123 param = arg;
3124 bs = &sc->sc_stp;
3125 param->ifbrp_maxage = bs->bs_bridge_max_age >> 8;
3126 return 0;
3127 }
3128
3129 static int
3130 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
3131 {
3132 #if BRIDGESTP
3133 struct ifbrparam *param = arg;
3134
3135 return bstp_set_maxage(&sc->sc_stp, param->ifbrp_maxage);
3136 #else /* !BRIDGESTP */
3137 #pragma unused(sc, arg)
3138 return EOPNOTSUPP;
3139 #endif /* !BRIDGESTP */
3140 }
3141
3142 static int
3143 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
3144 {
3145 #if BRIDGESTP
3146 struct ifbreq *req = arg;
3147 struct bridge_iflist *bif;
3148
3149 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
3150 if (bif == NULL) {
3151 return ENOENT;
3152 }
3153
3154 return bstp_set_port_priority(&bif->bif_stp, req->ifbr_priority);
3155 #else /* !BRIDGESTP */
3156 #pragma unused(sc, arg)
3157 return EOPNOTSUPP;
3158 #endif /* !BRIDGESTP */
3159 }
3160
3161 static int
3162 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
3163 {
3164 #if BRIDGESTP
3165 struct ifbreq *req = arg;
3166 struct bridge_iflist *bif;
3167
3168 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
3169 if (bif == NULL) {
3170 return ENOENT;
3171 }
3172
3173 return bstp_set_path_cost(&bif->bif_stp, req->ifbr_path_cost);
3174 #else /* !BRIDGESTP */
3175 #pragma unused(sc, arg)
3176 return EOPNOTSUPP;
3177 #endif /* !BRIDGESTP */
3178 }
3179
3180 static int
3181 bridge_ioctl_gfilt(struct bridge_softc *sc, void *arg)
3182 {
3183 struct ifbrparam *param = arg;
3184
3185 param->ifbrp_filter = sc->sc_filter_flags;
3186
3187 return 0;
3188 }
3189
3190 static int
3191 bridge_ioctl_sfilt(struct bridge_softc *sc, void *arg)
3192 {
3193 struct ifbrparam *param = arg;
3194
3195 if (param->ifbrp_filter & ~IFBF_FILT_MASK) {
3196 return EINVAL;
3197 }
3198
3199 if (param->ifbrp_filter & IFBF_FILT_USEIPF) {
3200 return EINVAL;
3201 }
3202
3203 sc->sc_filter_flags = param->ifbrp_filter;
3204
3205 return 0;
3206 }
3207
3208 static int
3209 bridge_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);
3215 if (bif == NULL) {
3216 return ENOENT;
3217 }
3218
3219 bif->bif_addrmax = req->ifbr_addrmax;
3220 return 0;
3221 }
3222
3223 static int
3224 bridge_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);
3231 if (ifs == NULL) {
3232 return ENOENT;
3233 }
3234
3235 if (IFNET_IS_INTCOPROC(ifs)) {
3236 return EINVAL;
3237 }
3238
3239 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)
3240 if (ifs == bif->bif_ifp) {
3241 return EBUSY;
3242 }
3243
3244 if (ifs->if_bridge != NULL) {
3245 return EBUSY;
3246 }
3247
3248 switch (ifs->if_type) {
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;
3257 }
3258
3259 bif = _MALLOC(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO);
3260 if (bif == NULL) {
3261 return ENOMEM;
3262 }
3263
3264 bif->bif_ifp = ifs;
3265 bif->bif_ifflags = IFBIF_SPAN;
3266
3267 ifnet_reference(bif->bif_ifp);
3268
3269 TAILQ_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
3270
3271 return 0;
3272 }
3273
3274 static int
3275 bridge_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);
3282 if (ifs == NULL) {
3283 return ENOENT;
3284 }
3285
3286 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)
3287 if (ifs == bif->bif_ifp) {
3288 break;
3289 }
3290
3291 if (bif == NULL) {
3292 return ENOENT;
3293 }
3294
3295 bridge_delete_span(sc, bif);
3296
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; \
3323 } while (0)
3324
3325 static int
3326 bridge_ioctl_gbparam32(struct bridge_softc *sc, void *arg)
3327 {
3328 struct ifbropreq32 *req = arg;
3329
3330 BRIDGE_IOCTL_GBPARAM;
3331 return 0;
3332 }
3333
3334 static int
3335 bridge_ioctl_gbparam64(struct bridge_softc *sc, void *arg)
3336 {
3337 struct ifbropreq64 *req = arg;
3338
3339 BRIDGE_IOCTL_GBPARAM;
3340 return 0;
3341 }
3342
3343 static int
3344 bridge_ioctl_grte(struct bridge_softc *sc, void *arg)
3345 {
3346 struct ifbrparam *param = arg;
3347
3348 param->ifbrp_cexceeded = sc->sc_brtexceeded;
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; \
3402 error = copyout(outbuf, bifstp->ifbpstp_req, bifstp->ifbpstp_len); \
3403 BRIDGE_LOCK(sc); \
3404 _FREE(outbuf, M_TEMP); \
3405 return (error); \
3406 } while (0)
3407
3408 static int
3409 bridge_ioctl_gifsstp32(struct bridge_softc *sc, void *arg)
3410 {
3411 struct ifbpstpconf32 *bifstp = arg;
3412 int error = 0;
3413
3414 BRIDGE_IOCTL_GIFSSTP;
3415 return error;
3416 }
3417
3418 static int
3419 bridge_ioctl_gifsstp64(struct bridge_softc *sc, void *arg)
3420 {
3421 struct ifbpstpconf64 *bifstp = arg;
3422 int error = 0;
3423
3424 BRIDGE_IOCTL_GIFSSTP;
3425 return error;
3426 }
3427
3428 static int
3429 bridge_ioctl_sproto(struct bridge_softc *sc, void *arg)
3430 {
3431 #if BRIDGESTP
3432 struct ifbrparam *param = arg;
3433
3434 return bstp_set_protocol(&sc->sc_stp, param->ifbrp_proto);
3435 #else /* !BRIDGESTP */
3436 #pragma unused(sc, arg)
3437 return EOPNOTSUPP;
3438 #endif /* !BRIDGESTP */
3439 }
3440
3441 static int
3442 bridge_ioctl_stxhc(struct bridge_softc *sc, void *arg)
3443 {
3444 #if BRIDGESTP
3445 struct ifbrparam *param = arg;
3446
3447 return bstp_set_holdcount(&sc->sc_stp, param->ifbrp_txhc);
3448 #else /* !BRIDGESTP */
3449 #pragma unused(sc, arg)
3450 return EOPNOTSUPP;
3451 #endif /* !BRIDGESTP */
3452 }
3453
3454
3455 static int
3456 bridge_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);
3462 if (bif == NULL) {
3463 return ENOENT;
3464 }
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 }
3473 return 0;
3474 }
3475
3476 static int
3477 bridge_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);
3483 if (bif == NULL) {
3484 return ENOENT;
3485 }
3486
3487 INC_ATOMIC_INT64_LIM(net_api_stats.nas_vmnet_total);
3488
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,
3496 ETHER_ADDR_LEN) != 0) {
3497 bif->bif_flags |= BIFF_HF_HWSRC;
3498 } else {
3499 bif->bif_flags &= ~BIFF_HF_HWSRC;
3500 }
3501 }
3502 if (req->ifbrhf_flags & IFBRHF_IPSRC) {
3503 bif->bif_hf_ipsrc.s_addr = req->ifbrhf_ipsrc;
3504 if (bif->bif_hf_ipsrc.s_addr != INADDR_ANY) {
3505 bif->bif_flags |= BIFF_HF_IPSRC;
3506 } else {
3507 bif->bif_flags &= ~BIFF_HF_IPSRC;
3508 }
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
3517 return 0;
3518 }
3519
3520 static char *
3521 bridge_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 */
3572 static int
3573 bridge_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
3611 static int
3612 bridge_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
3619 static int
3620 bridge_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 }
3627
3628 /*
3629 * bridge_ifdetach:
3630 *
3631 * Detach an interface from a bridge. Called when a member
3632 * interface is detaching.
3633 */
3634 static void
3635 bridge_ifdetach(struct ifnet *ifp)
3636 {
3637 struct bridge_iflist *bif;
3638 struct bridge_softc *sc = ifp->if_bridge;
3639
3640 #if BRIDGE_DEBUG
3641 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE)) {
3642 printf("%s: %s\n", __func__, ifp->if_xname);
3643 }
3644 #endif /* BRIDGE_DEBUG */
3645
3646 /* Check if the interface is a bridge member */
3647 if (sc != NULL) {
3648 BRIDGE_LOCK(sc);
3649 bif = bridge_lookup_member_if(sc, ifp);
3650 if (bif != NULL) {
3651 bridge_delete_member(sc, bif, 1);
3652 }
3653 BRIDGE_UNLOCK(sc);
3654 return;
3655 }
3656 /* Check if the interface is a span port */
3657 lck_mtx_lock(&bridge_list_mtx);
3658 LIST_FOREACH(sc, &bridge_list, sc_list) {
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;
3664 }
3665 BRIDGE_UNLOCK(sc);
3666 }
3667 lck_mtx_unlock(&bridge_list_mtx);
3668 }
3669
3670 /*
3671 * bridge_proto_attach_changed
3672 *
3673 * Called when protocol attachment on the interface changes.
3674 */
3675 static void
3676 bridge_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
3684 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE)) {
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
3713 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE)) {
3714 printf("%s: input broadcast %s", ifp->if_xname,
3715 input_broadcast ? "ENABLED" : "DISABLED");
3716 }
3717 #endif /* BRIDGE_DEBUG */
3718 return;
3719 }
3720
3721 /*
3722 * interface_media_active:
3723 *
3724 * Tells if an interface media is active.
3725 */
3726 static int
3727 interface_media_active(struct ifnet *ifp)
3728 {
3729 struct ifmediareq ifmr;
3730 int status = 0;
3731
3732 bzero(&ifmr, sizeof(ifmr));
3733 if (ifnet_ioctl(ifp, 0, SIOCGIFMEDIA, &ifmr) == 0) {
3734 if ((ifmr.ifm_status & IFM_AVALID) && ifmr.ifm_count > 0) {
3735 status = ifmr.ifm_status & IFM_ACTIVE ? 1 : 0;
3736 }
3737 }
3738
3739 return status;
3740 }
3741
3742 /*
3743 * bridge_updatelinkstatus:
3744 *
3745 * Update the media active status of the bridge based on the
3746 * media active status of its member.
3747 * If changed, return the corresponding onf/off link event.
3748 */
3749 static u_int32_t
3750 bridge_updatelinkstatus(struct bridge_softc *sc)
3751 {
3752 struct bridge_iflist *bif;
3753 int active_member = 0;
3754 u_int32_t event_code = 0;
3755
3756 BRIDGE_LOCK_ASSERT_HELD(sc);
3757
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
3776 return event_code;
3777 }
3778
3779 /*
3780 * bridge_iflinkevent:
3781 */
3782 static void
3783 bridge_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
3790 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE)) {
3791 printf("%s: %s\n", __func__, ifp->if_xname);
3792 }
3793 #endif /* BRIDGE_DEBUG */
3794
3795 /* Check if the interface is a bridge member */
3796 if (sc == NULL) {
3797 return;
3798 }
3799
3800 BRIDGE_LOCK(sc);
3801 bif = bridge_lookup_member_if(sc, ifp);
3802 if (bif != NULL) {
3803 if (interface_media_active(ifp)) {
3804 bif->bif_flags |= BIFF_MEDIA_ACTIVE;
3805 } else {
3806 bif->bif_flags &= ~BIFF_MEDIA_ACTIVE;
3807 }
3808 if (sc->sc_mac_nat_bif != NULL) {
3809 bridge_mac_nat_flush_entries(sc, bif);
3810 }
3811
3812 event_code = bridge_updatelinkstatus(sc);
3813 }
3814 BRIDGE_UNLOCK(sc);
3815
3816 if (event_code != 0) {
3817 bridge_link_event(sc->sc_ifp, event_code);
3818 }
3819 }
3820
3821 /*
3822 * bridge_delayed_callback:
3823 *
3824 * Makes a delayed call
3825 */
3826 static void
3827 bridge_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
3851 #if BRIDGE_DEBUG_DELAYED_CALLBACK
3852 if (IF_BRIDGE_DEBUG(BR_DBGF_DELAYED_CALL)) {
3853 printf("%s: %s call 0x%llx flags 0x%x\n", __func__,
3854 sc->sc_if_xname, (uint64_t)VM_KERNEL_ADDRPERM(call),
3855 call->bdc_flags);
3856 }
3857 #endif /* BRIDGE_DEBUG_DELAYED_CALLBACK */
3858
3859 if (call->bdc_flags & BDCF_CANCELLING) {
3860 wakeup(call);
3861 } else {
3862 if ((sc->sc_flags & SCF_DETACHING) == 0) {
3863 (*call->bdc_func)(sc);
3864 }
3865 }
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
3874 * The actual call may be scheduled to run at a given time or ASAP.
3875 */
3876 static void
3877 bridge_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) ||
3885 (call->bdc_flags & (BDCF_OUTSTANDING | BDCF_CANCELLING))) {
3886 return;
3887 }
3888
3889 if (call->bdc_ts.tv_sec || call->bdc_ts.tv_nsec) {
3890 nanoseconds_to_absolutetime(
3891 (uint64_t)call->bdc_ts.tv_sec * NSEC_PER_SEC +
3892 call->bdc_ts.tv_nsec, &deadline);
3893 clock_absolutetime_interval_to_deadline(deadline, &deadline);
3894 }
3895
3896 call->bdc_flags = BDCF_OUTSTANDING;
3897
3898 #if BRIDGE_DEBUG_DELAYED_CALLBACK
3899 if (IF_BRIDGE_DEBUG(BR_DBGF_DELAYED_CALL)) {
3900 printf("%s: %s call 0x%llx flags 0x%x\n", __func__,
3901 sc->sc_if_xname, (uint64_t)VM_KERNEL_ADDRPERM(call),
3902 call->bdc_flags);
3903 }
3904 #endif /* BRIDGE_DEBUG_DELAYED_CALLBACK */
3905
3906 if (call->bdc_ts.tv_sec || call->bdc_ts.tv_nsec) {
3907 thread_call_func_delayed(
3908 (thread_call_func_t)bridge_delayed_callback,
3909 call, deadline);
3910 } else {
3911 if (call->bdc_thread_call == NULL) {
3912 call->bdc_thread_call = thread_call_allocate(
3913 (thread_call_func_t)bridge_delayed_callback,
3914 call);
3915 }
3916 thread_call_enter(call->bdc_thread_call);
3917 }
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 */
3927 static void
3928 bridge_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 */
3936 if (sc == NULL) {
3937 return;
3938 }
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
3946 if (IF_BRIDGE_DEBUG(BR_DBGF_DELAYED_CALL)) {
3947 printf("%s: %s call 0x%llx flags 0x%x\n", __func__,
3948 sc->sc_if_xname, (uint64_t)VM_KERNEL_ADDRPERM(call),
3949 call->bdc_flags);
3950 }
3951 #endif /* BRIDGE_DEBUG */
3952 result = thread_call_func_cancel(
3953 (thread_call_func_t)bridge_delayed_callback, call, FALSE);
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
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 */
3976 static void
3977 bridge_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 */
3985 if (sc == NULL) {
3986 return;
3987 }
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);
3996 if (result == FALSE) {
3997 panic("%s thread_call_free() failed for call %p",
3998 __func__, call);
3999 }
4000 call->bdc_thread_call = NULL;
4001 }
4002 }
4003
4004 /*
4005 * bridge_init:
4006 *
4007 * Initialize a bridge interface.
4008 */
4009 static int
4010 bridge_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
4017 if ((ifnet_flags(ifp) & IFF_RUNNING)) {
4018 return 0;
4019 }
4020
4021 error = ifnet_set_flags(ifp, IFF_RUNNING, IFF_RUNNING);
4022
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);
4028 #if BRIDGESTP
4029 if (error == 0) {
4030 bstp_init(&sc->sc_stp); /* Initialize Spanning Tree */
4031 }
4032 #endif /* BRIDGESTP */
4033 return error;
4034 }
4035
4036 /*
4037 * bridge_ifstop:
4038 *
4039 * Stop the bridge interface.
4040 */
4041 static void
4042 bridge_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
4049 if ((ifnet_flags(ifp) & IFF_RUNNING) == 0) {
4050 return;
4051 }
4052
4053 bridge_cancel_delayed_call(&sc->sc_aging_timer);
4054
4055 #if BRIDGESTP
4056 bstp_stop(&sc->sc_stp);
4057 #endif /* BRIDGESTP */
4058
4059 bridge_rtflush(sc, IFBF_FLUSHDYN);
4060 (void) ifnet_set_flags(ifp, 0, IFF_RUNNING);
4061 }
4062
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 */
4072 static void
4073 bridge_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
4108 if (IF_BRIDGE_DEBUG(BR_DBGF_CHECKSUM)) {
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
4117 /*
4118 * bridge_enqueue:
4119 *
4120 * Enqueue a packet on a bridge member interface.
4121 *
4122 */
4123 static int
4124 bridge_enqueue(ifnet_t bridge_ifp, struct ifnet *src_ifp,
4125 struct ifnet *dst_ifp, struct mbuf *m, ChecksumOperation cksum_op)
4126 {
4127 int len, error = 0;
4128 struct mbuf *next_m;
4129
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 */
4137 for (; m; m = next_m) {
4138 errno_t _error;
4139 struct flowadv adv = { .code = FADV_SUCCESS };
4140
4141 next_m = m->m_nextpkt;
4142 m->m_nextpkt = NULL;
4143
4144 len = m->m_pkthdr.len;
4145 m->m_flags |= M_PROTO1; /* set to avoid loops */
4146
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 }
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) {
4170 printf("%s: %s: unable to prepend VLAN "
4171 "header\n", __func__, dst_ifp->if_xname);
4172 (void) ifnet_stat_increment_out(dst_ifp,
4173 0, 0, 1);
4174 continue;
4175 }
4176 m->m_flags &= ~M_VLANTAG;
4177 }
4178 #endif /* HAS_IF_CAP */
4179
4180 _error = dlil_output(dst_ifp, 0, m, NULL, NULL, 1, &adv);
4181
4182 /* Preserve existing error value */
4183 if (error == 0) {
4184 if (_error != 0) {
4185 error = _error;
4186 } else if (adv.code == FADV_FLOW_CONTROLLED) {
4187 error = EQFULL;
4188 } else if (adv.code == FADV_SUSPENDED) {
4189 error = EQSUSPENDED;
4190 }
4191 }
4192
4193 if (_error == 0) {
4194 (void) ifnet_stat_increment_out(bridge_ifp, 1, len, 0);
4195 } else {
4196 (void) ifnet_stat_increment_out(bridge_ifp, 0, 0, 1);
4197 }
4198 }
4199
4200 return error;
4201 }
4202
4203 #if HAS_BRIDGE_DUMMYNET
4204 /*
4205 * bridge_dummynet:
4206 *
4207 * Receive a queued packet from dummynet and pass it on to the output
4208 * interface.
4209 *
4210 * The mbuf has the Ethernet header already attached.
4211 */
4212 static void
4213 bridge_dummynet(struct mbuf *m, struct ifnet *ifp)
4214 {
4215 struct bridge_softc *sc;
4216
4217 sc = ifp->if_bridge;
4218
4219 /*
4220 * The packet didn't originate from a member interface. This should only
4221 * ever happen if a member interface is removed while packets are
4222 * queued for it.
4223 */
4224 if (sc == NULL) {
4225 m_freem(m);
4226 return;
4227 }
4228
4229 if (PFIL_HOOKED(&inet_pfil_hook) || PFIL_HOOKED_INET6) {
4230 if (bridge_pfil(&m, sc->sc_ifp, ifp, PFIL_OUT) != 0) {
4231 return;
4232 }
4233 if (m == NULL) {
4234 return;
4235 }
4236 }
4237 (void) bridge_enqueue(sc->sc_ifp, NULL, ifp, m, kChecksumOperationNone);
4238 }
4239 #endif /* HAS_BRIDGE_DUMMYNET */
4240
4241 /*
4242 * bridge_member_output:
4243 *
4244 * Send output from a bridge member interface. This
4245 * performs the bridging function for locally originated
4246 * packets.
4247 *
4248 * The mbuf has the Ethernet header already attached.
4249 */
4250 static errno_t
4251 bridge_member_output(struct bridge_softc *sc, ifnet_t ifp, mbuf_t *data)
4252 {
4253 ifnet_t bridge_ifp;
4254 struct ether_header *eh;
4255 struct ifnet *dst_if;
4256 uint16_t vlan;
4257 struct bridge_iflist *mac_nat_bif;
4258 ifnet_t mac_nat_ifp;
4259 mbuf_t m = *data;
4260
4261 #if BRIDGE_DEBUG
4262 if (IF_BRIDGE_DEBUG(BR_DBGF_OUTPUT)) {
4263 printf("%s: ifp %s\n", __func__, ifp->if_xname);
4264 }
4265 #endif /* BRIDGE_DEBUG */
4266
4267 if (m->m_len < ETHER_HDR_LEN) {
4268 m = m_pullup(m, ETHER_HDR_LEN);
4269 if (m == NULL) {
4270 *data = NULL;
4271 return EJUSTRETURN;
4272 }
4273 }
4274
4275 eh = mtod(m, struct ether_header *);
4276 vlan = VLANTAGOF(m);
4277
4278 BRIDGE_LOCK(sc);
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;
4292
4293 /*
4294 * APPLE MODIFICATION
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 }
4302
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 */
4308 if ((bridge_ifp->if_flags & IFF_RUNNING) == 0) {
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 */
4317 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
4318 dst_if = NULL;
4319 } else {
4320 dst_if = bridge_rtlookup(sc, eh->ether_dhost, vlan);
4321 }
4322 if (dst_if == NULL) {
4323 struct bridge_iflist *bif;
4324 struct mbuf *mc;
4325 int used = 0;
4326 errno_t error;
4327
4328
4329 bridge_span(sc, m);
4330
4331 BRIDGE_LOCK2REF(sc, error);
4332 if (error != 0) {
4333 m_freem(m);
4334 return EJUSTRETURN;
4335 }
4336
4337 TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) {
4338 /* skip interface with inactive link status */
4339 if ((bif->bif_flags & BIFF_MEDIA_ACTIVE) == 0) {
4340 continue;
4341 }
4342 dst_if = bif->bif_ifp;
4343
4344 if (dst_if->if_type == IFT_GIF) {
4345 continue;
4346 }
4347 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
4348 continue;
4349 }
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 }
4370 }
4371 if (TAILQ_NEXT(bif, bif_next) == NULL) {
4372 used = 1;
4373 mc = m;
4374 } else {
4375 mc = m_dup(m, M_DONTWAIT);
4376 if (mc == NULL) {
4377 (void) ifnet_stat_increment_out(
4378 bridge_ifp, 0, 0, 1);
4379 continue;
4380 }
4381 }
4382 (void) bridge_enqueue(bridge_ifp, ifp, dst_if,
4383 mc, kChecksumOperationCompute);
4384 }
4385 if (used == 0) {
4386 m_freem(m);
4387 }
4388 BRIDGE_UNREF(sc);
4389 return EJUSTRETURN;
4390 }
4391
4392 sendunicast:
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);
4401 return EJUSTRETURN;
4402 }
4403
4404 BRIDGE_UNLOCK(sc);
4405 if (dst_if == ifp) {
4406 /* just let the packet continue on its way */
4407 return 0;
4408 }
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 }
4421 return EJUSTRETURN;
4422 }
4423
4424 /*
4425 * Output callback.
4426 *
4427 * This routine is called externally from above only when if_bridge_txstart
4428 * is disabled; otherwise it is called internally by bridge_start().
4429 */
4430 static int
4431 bridge_output(struct ifnet *ifp, struct mbuf *m)
4432 {
4433 struct bridge_softc *sc = ifnet_softc(ifp);
4434 struct ether_header *eh;
4435 struct ifnet *dst_if = NULL;
4436 int error = 0;
4437
4438 eh = mtod(m, struct ether_header *);
4439
4440 BRIDGE_LOCK(sc);
4441
4442 if (!(m->m_flags & (M_BCAST | M_MCAST))) {
4443 dst_if = bridge_rtlookup(sc, eh->ether_dhost, 0);
4444 }
4445
4446 (void) ifnet_stat_increment_out(ifp, 1, m->m_pkthdr.len, 0);
4447
4448 #if NBPFILTER > 0
4449 if (sc->sc_bpf_output) {
4450 bridge_bpf_output(ifp, m);
4451 }
4452 #endif
4453
4454 if (dst_if == NULL) {
4455 /* callee will unlock */
4456 bridge_broadcast(sc, NULL, m, 0);
4457 } else {
4458 ifnet_t bridge_ifp;
4459
4460 bridge_ifp = sc->sc_ifp;
4461 BRIDGE_UNLOCK(sc);
4462 error = bridge_enqueue(bridge_ifp, NULL, dst_if, m,
4463 kChecksumOperationFinalize);
4464 }
4465
4466 return error;
4467 }
4468
4469 static void
4470 bridge_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
4475
4476 if (ifp != NULL) {
4477 hwcap = (ifp->if_hwassist | CSUM_DATA_VALID);
4478 } else {
4479 hwcap = 0;
4480 }
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 =
4492 sizeof(*eh) + sizeof(struct ip);
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 }
4504 (void) in_finalize_cksum(m, sizeof(*eh), sw_csum);
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 =
4514 sizeof(*eh) + sizeof(struct ip6_hdr);
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 }
4526 (void) in6_finalize_cksum(m, sizeof(*eh), -1, -1, sw_csum);
4527 break;
4528 #endif /* INET6 */
4529 }
4530 }
4531
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 */
4544 static void
4545 bridge_start(struct ifnet *ifp)
4546 {
4547 struct mbuf *m;
4548
4549 for (;;) {
4550 if (ifnet_dequeue(ifp, &m) != 0) {
4551 break;
4552 }
4553
4554 (void) bridge_output(ifp, m);
4555 }
4556 }
4557
4558 /*
4559 * bridge_forward:
4560 *
4561 * The forwarding function of the bridge.
4562 *
4563 * NOTE: Releases the lock on return.
4564 */
4565 static void
4566 bridge_forward(struct bridge_softc *sc, struct bridge_iflist *sbif,
4567 struct mbuf *m)
4568 {
4569 struct bridge_iflist *dbif;
4570 ifnet_t bridge_ifp;
4571 struct ifnet *src_if, *dst_if;
4572 struct ether_header *eh;
4573 uint16_t vlan;
4574 uint8_t *dst;
4575 int error;
4576 struct mac_nat_record mnr;
4577 boolean_t translate_mac = FALSE;
4578 uint32_t sc_filter_flags = 0;
4579
4580 BRIDGE_LOCK_ASSERT_HELD(sc);
4581
4582 bridge_ifp = sc->sc_ifp;
4583 #if BRIDGE_DEBUG
4584 if (IF_BRIDGE_DEBUG(BR_DBGF_OUTPUT)) {
4585 printf("%s: %s m 0x%llx\n", __func__, bridge_ifp->if_xname,
4586 (uint64_t)VM_KERNEL_ADDRPERM(m));
4587 }
4588 #endif /* BRIDGE_DEBUG */
4589
4590 src_if = m->m_pkthdr.rcvif;
4591
4592 (void) ifnet_stat_increment_in(bridge_ifp, 1, m->m_pkthdr.len, 0);
4593 vlan = VLANTAGOF(m);
4594
4595
4596 if ((sbif->bif_ifflags & IFBIF_STP) &&
4597 sbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
4598 goto drop;
4599 }
4600
4601 eh = mtod(m, struct ether_header *);
4602 dst = eh->ether_dhost;
4603
4604 /* If the interface is learning, record the address. */
4605 if (sbif->bif_ifflags & IFBIF_LEARNING) {
4606 error = bridge_rtupdate(sc, eh->ether_shost, vlan,
4607 sbif, 0, IFBAF_DYNAMIC);
4608 /*
4609 * If the interface has addresses limits then deny any source
4610 * that is not in the cache.
4611 */
4612 if (error && sbif->bif_addrmax) {
4613 goto drop;
4614 }
4615 }
4616
4617 if ((sbif->bif_ifflags & IFBIF_STP) != 0 &&
4618 sbif->bif_stp.bp_state == BSTP_IFSTATE_LEARNING) {
4619 goto drop;
4620 }
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 */
4631 if ((m->m_flags & (M_BCAST | M_MCAST)) == 0) {
4632 /* unicast */
4633 dst_if = bridge_rtlookup(sc, dst, vlan);
4634 if (src_if == dst_if) {
4635 goto drop;
4636 }
4637 } else {
4638 /* broadcast/multicast */
4639
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 &&
4648 dst[4] == 0x00 && dst[5] <= 0x0f) {
4649 goto drop;
4650 }
4651
4652
4653 /* ...forward it to all interfaces. */
4654 atomic_add_64(&bridge_ifp->if_imcasts, 1);
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) ||
4669 dst_if != NULL || (m->m_flags & (M_BCAST | M_MCAST)) == 0) {
4670 m->m_pkthdr.rcvif = bridge_ifp;
4671 BRIDGE_BPF_MTAP_INPUT(sc, m);
4672 }
4673 #endif /* NBPFILTER */
4674
4675 #if defined(PFIL_HOOKS)
4676 /* run the packet filter */
4677 if (PFIL_HOOKED(&inet_pfil_hook) || PFIL_HOOKED_INET6) {
4678 BRIDGE_UNLOCK(sc);
4679 if (bridge_pfil(&m, bridge_ifp, src_if, PFIL_IN) != 0) {
4680 return;
4681 }
4682 if (m == NULL) {
4683 return;
4684 }
4685 BRIDGE_LOCK(sc);
4686 }
4687 #endif /* PFIL_HOOKS */
4688
4689 if (dst_if == NULL) {
4690 /* bridge_broadcast will unlock */
4691 bridge_broadcast(sc, src_if, m, 1);
4692 return;
4693 }
4694
4695 /*
4696 * Unicast.
4697 */
4698 /*
4699 * At this point, we're dealing with a unicast frame
4700 * going to a different interface.
4701 */
4702 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
4703 goto drop;
4704 }
4705
4706 dbif = bridge_lookup_member_if(sc, dst_if);
4707 if (dbif == NULL) {
4708 /* Not a member of the bridge (anymore?) */
4709 goto drop;
4710 }
4711
4712 /* Private segments can not talk to each other */
4713 if (sbif->bif_ifflags & dbif->bif_ifflags & IFBIF_PRIVATE) {
4714 goto drop;
4715 }
4716
4717 if ((dbif->bif_ifflags & IFBIF_STP) &&
4718 dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
4719 goto drop;
4720 }
4721
4722 #if HAS_DHCPRA_MASK
4723 /* APPLE MODIFICATION <rdar:6985737> */
4724 if ((dst_if->if_extflags & IFEXTF_DHCPRA_MASK) != 0) {
4725 m = ip_xdhcpra_output(dst_if, m);
4726 if (!m) {
4727 ++bridge_ifp.if_xdhcpra;
4728 BRIDGE_UNLOCK(sc);
4729 return;
4730 }
4731 }
4732 #endif /* HAS_DHCPRA_MASK */
4733
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 }
4744
4745 #if defined(PFIL_HOOKS)
4746 if (PFIL_HOOKED(&inet_pfil_hook) || PFIL_HOOKED_INET6) {
4747 if (bridge_pfil(&m, bridge_ifp, dst_if, PFIL_OUT) != 0) {
4748 return;
4749 }
4750 if (m == NULL) {
4751 return;
4752 }
4753 }
4754 #endif /* PFIL_HOOKS */
4755
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 }
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 */
4778 if (m != NULL) {
4779 (void) bridge_enqueue(bridge_ifp, NULL, dst_if, m,
4780 kChecksumOperationClear);
4781 }
4782 return;
4783
4784 drop:
4785 BRIDGE_UNLOCK(sc);
4786 m_freem(m);
4787 }
4788
4789 #if BRIDGE_DEBUG
4790
4791 static char *
4792 ether_ntop(char *buf, size_t len, const u_char *ap)
4793 {
4794 snprintf(buf, len, "%02x:%02x:%02x:%02x:%02x:%02x",
4795 ap[0], ap[1], ap[2], ap[3], ap[4], ap[5]);
4796
4797 return buf;
4798 }
4799
4800 #endif /* BRIDGE_DEBUG */
4801
4802 static void
4803 inject_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
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 */
4821 errno_t
4822 bridge_input(struct ifnet *ifp, mbuf_t *data)
4823 {
4824 struct bridge_softc *sc = ifp->if_bridge;
4825 struct bridge_iflist *bif, *bif2;
4826 ifnet_t bridge_ifp;
4827 struct ether_header *eh;
4828 struct mbuf *mc, *mc2;
4829 uint16_t vlan;
4830 errno_t error;
4831 boolean_t is_ifp_mac = FALSE;
4832 mbuf_t m = *data;
4833 uint32_t sc_filter_flags = 0;
4834
4835 bridge_ifp = sc->sc_ifp;
4836 #if BRIDGE_DEBUG
4837 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT)) {
4838 printf("%s: %s from %s m 0x%llx data 0x%llx\n", __func__,
4839 bridge_ifp->if_xname, ifp->if_xname,
4840 (uint64_t)VM_KERNEL_ADDRPERM(m),
4841 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m)));
4842 }
4843 #endif /* BRIDGE_DEBUG */
4844
4845 if ((sc->sc_ifp->if_flags & IFF_RUNNING) == 0) {
4846 #if BRIDGE_DEBUG
4847 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT)) {
4848 printf("%s: %s not running passing along\n",
4849 __func__, bridge_ifp->if_xname);
4850 }
4851 #endif /* BRIDGE_DEBUG */
4852 return 0;
4853 }
4854
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 */
4864 if ((bridge_ifp->if_flags & IFF_MONITOR) != 0) {
4865 m->m_pkthdr.rcvif = bridge_ifp;
4866 BRIDGE_BPF_MTAP_INPUT(sc, m);
4867 (void) ifnet_stat_increment_in(bridge_ifp, 1, m->m_pkthdr.len, 0);
4868 m_freem(m);
4869 return EJUSTRETURN;
4870 }
4871 #endif /* IFF_MONITOR */
4872
4873 /*
4874 * Need to clear the promiscous flags otherwise it will be
4875 * dropped by DLIL after processing filters
4876 */
4877 if ((mbuf_flags(m) & MBUF_PROMISC)) {
4878 mbuf_setflags_mask(m, 0, MBUF_PROMISC);
4879 }
4880
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
4898 BRIDGE_LOCK(sc);
4899 bif = bridge_lookup_member_if(sc, ifp);
4900 if (bif == NULL) {
4901 BRIDGE_UNLOCK(sc);
4902 #if BRIDGE_DEBUG
4903 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT)) {
4904 printf("%s: %s bridge_lookup_member_if failed\n",
4905 __func__, bridge_ifp->if_xname);
4906 }
4907 #endif /* BRIDGE_DEBUG */
4908 return 0;
4909 }
4910
4911 if (bif->bif_flags & BIFF_HOST_FILTER) {
4912 error = bridge_host_filter(bif, data);
4913 if (error != 0) {
4914 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT)) {
4915 printf("%s: %s bridge_host_filter failed\n",
4916 __func__, bif->bif_ifp->if_xname);
4917 }
4918 BRIDGE_UNLOCK(sc);
4919 return EJUSTRETURN;
4920 }
4921 m = *data;
4922 }
4923
4924 eh = mtod(m, struct ether_header *);
4925
4926 bridge_span(sc, m);
4927
4928 if (m->m_flags & (M_BCAST | M_MCAST)) {
4929 #if BRIDGE_DEBUG
4930 if (IF_BRIDGE_DEBUG(BR_DBGF_MCAST)) {
4931 if ((m->m_flags & M_MCAST)) {
4932 printf("%s: multicast: "
4933 "%02x:%02x:%02x:%02x:%02x:%02x\n",
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]);
4938 }
4939 }
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) {
4945 #if BRIDGESTP
4946 m = bstp_input(&bif->bif_stp, ifp, m);
4947 #else /* !BRIDGESTP */
4948 m_freem(m);
4949 m = NULL;
4950 #endif /* !BRIDGESTP */
4951 if (m == NULL) {
4952 BRIDGE_UNLOCK(sc);
4953 return EJUSTRETURN;
4954 }
4955 }
4956
4957 if ((bif->bif_ifflags & IFBIF_STP) &&
4958 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
4959 BRIDGE_UNLOCK(sc);
4960 return 0;
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);
4971 return 0;
4972 }
4973
4974 /*
4975 * Perform the bridge forwarding function with the copy.
4976 *
4977 * Note that bridge_forward calls BRIDGE_UNLOCK
4978 */
4979 bridge_forward(sc, bif, mc);
4980
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 */
4987 VERIFY(bridge_ifp->if_bridge == NULL);
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) {
4995 /* mark packet as arriving on the bridge */
4996 mc2->m_pkthdr.rcvif = bridge_ifp;
4997 mc2->m_pkthdr.pkt_hdr = mbuf_data(mc2);
4998
4999 BRIDGE_BPF_MTAP_INPUT(sc, m);
5000
5001 (void) mbuf_setdata(mc2,
5002 (char *)mbuf_data(mc2) + ETHER_HDR_LEN,
5003 mbuf_len(mc2) - ETHER_HDR_LEN);
5004 (void) mbuf_pkthdr_adjustlen(mc2, -ETHER_HDR_LEN);
5005
5006 (void) ifnet_stat_increment_in(bridge_ifp, 1,
5007 mbuf_pkthdr_len(mc2), 0);
5008
5009 #if BRIDGE_DEBUG
5010 if (IF_BRIDGE_DEBUG(BR_DBGF_MCAST)) {
5011 printf("%s: %s mcast for us\n", __func__,
5012 bridge_ifp->if_xname);
5013 }
5014 #endif /* BRIDGE_DEBUG */
5015
5016 dlil_input_packet_list(bridge_ifp, mc2);
5017 }
5018
5019 /* Return the original packet for local processing. */
5020 return 0;
5021 }
5022
5023 if ((bif->bif_ifflags & IFBIF_STP) &&
5024 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
5025 BRIDGE_UNLOCK(sc);
5026 return 0;
5027 }
5028
5029 #ifdef DEV_CARP
5030 #define CARP_CHECK_WE_ARE_DST(iface) \
5031 ((iface)->if_carp &&\
5032 carp_forus((iface)->if_carp, eh->ether_dhost))
5033 #define CARP_CHECK_WE_ARE_SRC(iface) \
5034 ((iface)->if_carp &&\
5035 carp_forus((iface)->if_carp, eh->ether_shost))
5036 #else
5037 #define CARP_CHECK_WE_ARE_DST(iface) 0
5038 #define CARP_CHECK_WE_ARE_SRC(iface) 0
5039 #endif
5040
5041 #ifdef INET6
5042 #define PFIL_HOOKED_INET6 PFIL_HOOKED(&inet6_pfil_hook)
5043 #else
5044 #define PFIL_HOOKED_INET6 0
5045 #endif
5046
5047 #if defined(PFIL_HOOKS)
5048 #define PFIL_PHYS(sc, ifp, m) do { \
5049 if (pfil_local_phys && \
5050 (PFIL_HOOKED(&inet_pfil_hook) || PFIL_HOOKED_INET6)) { \
5051 if (bridge_pfil(&m, NULL, ifp, \
5052 PFIL_IN) != 0 || m == NULL) { \
5053 BRIDGE_UNLOCK(sc); \
5054 return (NULL); \
5055 } \
5056 } \
5057 } while (0)
5058 #else /* PFIL_HOOKS */
5059 #define PFIL_PHYS(sc, ifp, m)
5060 #endif /* PFIL_HOOKS */
5061
5062 #define GRAB_OUR_PACKETS(iface) \
5063 if ((iface)->if_type == IFT_GIF) \
5064 continue; \
5065 /* It is destined for us. */ \
5066 if (memcmp(IF_LLADDR((iface)), eh->ether_dhost, \
5067 ETHER_ADDR_LEN) == 0 || CARP_CHECK_WE_ARE_DST((iface))) { \
5068 if ((iface)->if_type == IFT_BRIDGE) { \
5069 BRIDGE_BPF_MTAP_INPUT(sc, m); \
5070 /* Filter on the physical interface. */ \
5071 PFIL_PHYS(sc, iface, m); \
5072 } else { \
5073 bpf_tap_in(iface, DLT_EN10MB, m, NULL, 0); \
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); \
5080 m_freem(m); \
5081 return (EJUSTRETURN); \
5082 } \
5083 } \
5084 BRIDGE_UNLOCK(sc); \
5085 inject_input_packet(iface, m); \
5086 return (EJUSTRETURN); \
5087 } \
5088 \
5089 /* We just received a packet that we sent out. */ \
5090 if (memcmp(IF_LLADDR((iface)), eh->ether_shost, \
5091 ETHER_ADDR_LEN) == 0 || CARP_CHECK_WE_ARE_SRC((iface))) { \
5092 BRIDGE_UNLOCK(sc); \
5093 m_freem(m); \
5094 return (EJUSTRETURN); \
5095 }
5096
5097 /*
5098 * Unicast.
5099 */
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
5129 /*
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.
5132 */
5133 if (memcmp(eh->ether_dhost, IF_LLADDR(bridge_ifp),
5134 ETHER_ADDR_LEN) == 0 || CARP_CHECK_WE_ARE_DST(bridge_ifp)) {
5135 /* Mark the packet as arriving on the bridge interface */
5136 (void) mbuf_pkthdr_setrcvif(m, bridge_ifp);
5137 mbuf_pkthdr_setheader(m, mbuf_data(m));
5138
5139 /*
5140 * If the interface is learning, and the source
5141 * address is valid and not multicast, record
5142 * the address.
5143 */
5144 if (bif->bif_ifflags & IFBIF_LEARNING) {
5145 (void) bridge_rtupdate(sc, eh->ether_shost,
5146 vlan, bif, 0, IFBAF_DYNAMIC);
5147 }
5148
5149 BRIDGE_BPF_MTAP_INPUT(sc, m);
5150
5151 (void) mbuf_setdata(m, (char *)mbuf_data(m) + ETHER_HDR_LEN,
5152 mbuf_len(m) - ETHER_HDR_LEN);
5153 (void) mbuf_pkthdr_adjustlen(m, -ETHER_HDR_LEN);
5154
5155 (void) ifnet_stat_increment_in(bridge_ifp, 1, mbuf_pkthdr_len(m), 0);
5156
5157 BRIDGE_UNLOCK(sc);
5158
5159 #if BRIDGE_DEBUG
5160 if (IF_BRIDGE_DEBUG(BR_DBGF_INPUT)) {
5161 printf("%s: %s packet for bridge\n", __func__,
5162 bridge_ifp->if_xname);
5163 }
5164 #endif /* BRIDGE_DEBUG */
5165
5166 dlil_input_packet_list(bridge_ifp, m);
5167
5168 return EJUSTRETURN;
5169 }
5170
5171 /*
5172 * if the destination of the packet is for the MAC address of
5173 * the member interface itself, then we don't need to forward
5174 * it -- just pass it back. Note that it'll likely just be
5175 * dropped by the stack, but if something else is bound to
5176 * the interface directly (for example, the wireless stats
5177 * protocol -- although that actually uses BPF right now),
5178 * then it will consume the packet
5179 *
5180 * ALSO, note that we do this check AFTER checking for the
5181 * bridge's own MAC address, because the bridge may be
5182 * using the SAME MAC address as one of its interfaces
5183 */
5184 if (is_ifp_mac) {
5185
5186 #ifdef VERY_VERY_VERY_DIAGNOSTIC
5187 printf("%s: not forwarding packet bound for member "
5188 "interface\n", __func__);
5189 #endif
5190
5191 BRIDGE_UNLOCK(sc);
5192 return 0;
5193 }
5194
5195 /* Now check the remaining bridge members. */
5196 TAILQ_FOREACH(bif2, &sc->sc_iflist, bif_next) {
5197 if (bif2->bif_ifp != ifp) {
5198 GRAB_OUR_PACKETS(bif2->bif_ifp);
5199 }
5200 }
5201
5202 #undef CARP_CHECK_WE_ARE_DST
5203 #undef CARP_CHECK_WE_ARE_SRC
5204 #undef GRAB_OUR_PACKETS
5205
5206 /*
5207 * Perform the bridge forwarding function.
5208 *
5209 * Note that bridge_forward calls BRIDGE_UNLOCK
5210 */
5211 bridge_forward(sc, bif, m);
5212
5213 return EJUSTRETURN;
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 */
5225 static void
5226 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
5227 struct mbuf *m, int runfilt)
5228 {
5229 ifnet_t bridge_ifp;
5230 struct bridge_iflist *dbif, *sbif;
5231 struct mbuf *mc;
5232 struct mbuf *mc_in;
5233 struct ifnet *dst_if;
5234 int error = 0, used = 0;
5235 boolean_t bridge_if_out;
5236 ChecksumOperation cksum_op;
5237 struct mac_nat_record mnr;
5238 struct bridge_iflist *mac_nat_bif = sc->sc_mac_nat_bif;
5239 boolean_t translate_mac = FALSE;
5240 uint32_t sc_filter_flags = 0;
5241
5242 bridge_ifp = sc->sc_ifp;
5243 if (src_if != NULL) {
5244 bridge_if_out = FALSE;
5245 cksum_op = kChecksumOperationClear;
5246 sbif = bridge_lookup_member_if(sc, src_if);
5247 if (sbif != NULL && mac_nat_bif != NULL && sbif != mac_nat_bif) {
5248 /* get the translation record while holding the lock */
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 }
5257 } else {
5258 /*
5259 * src_if is NULL when the bridge interface calls
5260 * bridge_broadcast().
5261 */
5262 bridge_if_out = TRUE;
5263 cksum_op = kChecksumOperationFinalize;
5264 sbif = NULL;
5265 }
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 */
5275 if (runfilt && (PFIL_HOOKED(&inet_pfil_hook) || PFIL_HOOKED_INET6)) {
5276 if (bridge_pfil(&m, bridge_ifp, NULL, PFIL_OUT) != 0) {
5277 goto out;
5278 }
5279 if (m == NULL) {
5280 goto out;
5281 }
5282 }
5283 #endif /* PFIL_HOOKS */
5284 TAILQ_FOREACH(dbif, &sc->sc_iflist, bif_next) {
5285 dst_if = dbif->bif_ifp;
5286 if (dst_if == src_if) {
5287 /* skip the interface that the packet came in on */
5288 continue;
5289 }
5290
5291 /* Private segments can not talk to each other */
5292 if (sbif != NULL &&
5293 (sbif->bif_ifflags & dbif->bif_ifflags & IFBIF_PRIVATE)) {
5294 continue;
5295 }
5296
5297 if ((dbif->bif_ifflags & IFBIF_STP) &&
5298 dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
5299 continue;
5300 }
5301
5302 if ((dbif->bif_ifflags & IFBIF_DISCOVER) == 0 &&
5303 (m->m_flags & (M_BCAST | M_MCAST)) == 0) {
5304 continue;
5305 }
5306
5307 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
5308 continue;
5309 }
5310
5311 if (!(dbif->bif_flags & BIFF_MEDIA_ACTIVE)) {
5312 continue;
5313 }
5314
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) {
5321 (void) ifnet_stat_increment_out(bridge_ifp,
5322 0, 0, 1);
5323 continue;
5324 }
5325 }
5326
5327 /*
5328 * If broadcast input is enabled, do so only if this
5329 * is an input packet.
5330 */
5331 if (!bridge_if_out &&
5332 (dbif->bif_flags & BIFF_INPUT_BROADCAST) != 0) {
5333 mc_in = m_dup(mc, M_DONTWAIT);
5334 /* this could fail, but we continue anyways */
5335 } else {
5336 mc_in = NULL;
5337 }
5338
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 */
5345 if (runfilt &&
5346 (PFIL_HOOKED(&inet_pfil_hook) || PFIL_HOOKED_INET6)) {
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) {
5352 (void) ifnet_stat_increment_out(
5353 bridge_ifp, 0, 0, 1);
5354 if (mc_in != NULL) {
5355 m_freem(mc_in);
5356 }
5357 continue;
5358 }
5359 }
5360 if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0) {
5361 if (mc_in != NULL) {
5362 m_freem(mc_in);
5363 }
5364 continue;
5365 }
5366 if (mc == NULL) {
5367 if (mc_in != NULL) {
5368 m_freem(mc_in);
5369 }
5370 continue;
5371 }
5372 }
5373 #endif /* PFIL_HOOKS */
5374
5375 /* out */
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 }
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);
5431 }
5432 if (used == 0) {
5433 m_freem(m);
5434 }
5435
5436 #ifdef PFIL_HOOKS
5437 out:
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 */
5449 static void
5450 bridge_span(struct bridge_softc *sc, struct mbuf *m)
5451 {
5452 struct bridge_iflist *bif;
5453 struct ifnet *dst_if;
5454 struct mbuf *mc;
5455
5456 if (TAILQ_EMPTY(&sc->sc_spanlist)) {
5457 return;
5458 }
5459
5460 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) {
5461 dst_if = bif->bif_ifp;
5462
5463 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
5464 continue;
5465 }
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
5473 (void) bridge_enqueue(sc->sc_ifp, NULL, dst_if, mc,
5474 kChecksumOperationNone);
5475 }
5476 }
5477
5478
5479 /*
5480 * bridge_rtupdate:
5481 *
5482 * Add a bridge routing entry.
5483 */
5484 static int
5485 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst, uint16_t vlan,
5486 struct bridge_iflist *bif, int setflags, uint8_t flags)
5487 {
5488 struct bridge_rtnode *brt;
5489 int error;
5490
5491 BRIDGE_LOCK_ASSERT_HELD(sc);
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 &&
5496 dst[3] == 0 && dst[4] == 0 && dst[5] == 0) != 0) {
5497 return EINVAL;
5498 }
5499
5500
5501 /* 802.1p frames map to vlan 1 */
5502 if (vlan == 0) {
5503 vlan = 1;
5504 }
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++;
5513 return ENOSPC;
5514 }
5515 /* Check per interface address limits (if enabled) */
5516 if (bif->bif_addrmax && bif->bif_addrcnt >= bif->bif_addrmax) {
5517 bif->bif_addrexceeded++;
5518 return ENOSPC;
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);
5527 if (brt == NULL) {
5528 if (IF_BRIDGE_DEBUG(BR_DBGF_RT_TABLE)) {
5529 printf("%s: zalloc_nolock failed", __func__);
5530 }
5531 return ENOMEM;
5532 }
5533 bzero(brt, sizeof(struct bridge_rtnode));
5534
5535 if (bif->bif_ifflags & IFBIF_STICKY) {
5536 brt->brt_flags = IFBAF_STICKY;
5537 } else {
5538 brt->brt_flags = IFBAF_DYNAMIC;
5539 }
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);
5547 return error;
5548 }
5549 brt->brt_dst = bif;
5550 bif->bif_addrcnt++;
5551 #if BRIDGE_DEBUG
5552 if (IF_BRIDGE_DEBUG(BR_DBGF_RT_TABLE)) {
5553 printf("%s: added %02x:%02x:%02x:%02x:%02x:%02x "
5554 "on %s count %u hashsize %u\n", __func__,
5555 dst[0], dst[1], dst[2], dst[3], dst[4], dst[5],
5556 sc->sc_ifp->if_xname, sc->sc_brtcnt,
5557 sc->sc_rthash_size);
5558 }
5559 #endif
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) {
5570 unsigned long now;
5571
5572 now = (unsigned long) net_uptime();
5573 brt->brt_expire = now + sc->sc_brttimeout;
5574 }
5575 if (setflags) {
5576 brt->brt_flags = flags;
5577 }
5578
5579
5580 return 0;
5581 }
5582
5583 /*
5584 * bridge_rtlookup:
5585 *
5586 * Lookup the destination interface for an address.
5587 */
5588 static struct ifnet *
5589 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
5590 {
5591 struct bridge_rtnode *brt;
5592
5593 BRIDGE_LOCK_ASSERT_HELD(sc);
5594
5595 if ((brt = bridge_rtnode_lookup(sc, addr, vlan)) == NULL) {
5596 return NULL;
5597 }
5598
5599 return brt->brt_ifp;
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 */
5609 static void
5610 bridge_rttrim(struct bridge_softc *sc)
5611 {
5612 struct bridge_rtnode *brt, *nbrt;
5613
5614 BRIDGE_LOCK_ASSERT_HELD(sc);
5615
5616 /* Make sure we actually need to do this. */
5617 if (sc->sc_brtcnt <= sc->sc_brtmax) {
5618 return;
5619 }
5620
5621 /* Force an aging cycle; this might trim enough addresses. */
5622 bridge_rtage(sc);
5623 if (sc->sc_brtcnt <= sc->sc_brtmax) {
5624 return;
5625 }
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);
5630 if (sc->sc_brtcnt <= sc->sc_brtmax) {
5631 return;
5632 }
5633 }
5634 }
5635 }
5636
5637 /*
5638 * bridge_aging_timer:
5639 *
5640 * Aging periodic timer for the bridge routing table.
5641 */
5642 static void
5643 bridge_aging_timer(struct bridge_softc *sc)
5644 {
5645 BRIDGE_LOCK_ASSERT_HELD(sc);
5646
5647 bridge_rtage(sc);
5648 if ((sc->sc_ifp->if_flags & IFF_RUNNING) &&
5649 (sc->sc_flags & SCF_DETACHING) == 0) {
5650 sc->sc_aging_timer.bdc_sc = sc;
5651 sc->sc_aging_timer.bdc_func = bridge_aging_timer;
5652 sc->sc_aging_timer.bdc_ts.tv_sec = bridge_rtable_prune_period;
5653 bridge_schedule_delayed_call(&sc->sc_aging_timer);
5654 }
5655 }
5656
5657 /*
5658 * bridge_rtage:
5659 *
5660 * Perform an aging cycle.
5661 */
5662 static void
5663 bridge_rtage(struct bridge_softc *sc)
5664 {
5665 struct bridge_rtnode *brt, *nbrt;
5666 unsigned long now;
5667
5668 BRIDGE_LOCK_ASSERT_HELD(sc);
5669
5670 now = (unsigned long) net_uptime();
5671
5672 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
5673 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
5674 if (now >= brt->brt_expire) {
5675 bridge_rtnode_destroy(sc, brt);
5676 }
5677 }
5678 }
5679 if (sc->sc_mac_nat_bif != NULL) {
5680 bridge_mac_nat_age_entries(sc, now);
5681 }
5682 }
5683
5684 /*
5685 * bridge_rtflush:
5686 *
5687 * Remove all dynamic addresses from the bridge.
5688 */
5689 static void
5690 bridge_rtflush(struct bridge_softc *sc, int full)
5691 {
5692 struct bridge_rtnode *brt, *nbrt;
5693
5694 BRIDGE_LOCK_ASSERT_HELD(sc);
5695
5696 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
5697 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
5698 bridge_rtnode_destroy(sc, brt);
5699 }
5700 }
5701 }
5702
5703 /*
5704 * bridge_rtdaddr:
5705 *
5706 * Remove an address from the table.
5707 */
5708 static int
5709 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan)
5710 {
5711 struct bridge_rtnode *brt;
5712 int found = 0;
5713
5714 BRIDGE_LOCK_ASSERT_HELD(sc);
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
5725 return found ? 0 : ENOENT;
5726 }
5727
5728 /*
5729 * bridge_rtdelete:
5730 *
5731 * Delete routes to a specific member interface.
5732 */
5733 static void
5734 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
5735 {
5736 struct bridge_rtnode *brt, *nbrt;
5737
5738 BRIDGE_LOCK_ASSERT_HELD(sc);
5739
5740 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
5741 if (brt->brt_ifp == ifp && (full ||
5742 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)) {
5743 bridge_rtnode_destroy(sc, brt);
5744 }
5745 }
5746 }
5747
5748 /*
5749 * bridge_rtable_init:
5750 *
5751 * Initialize the route table for this bridge.
5752 */
5753 static int
5754 bridge_rtable_init(struct bridge_softc *sc)
5755 {
5756 u_int32_t i;
5757
5758 sc->sc_rthash = _MALLOC(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
5759 M_DEVBUF, M_WAITOK | M_ZERO);
5760 if (sc->sc_rthash == NULL) {
5761 printf("%s: no memory\n", __func__);
5762 return ENOMEM;
5763 }
5764 sc->sc_rthash_size = BRIDGE_RTHASH_SIZE;
5765
5766 for (i = 0; i < sc->sc_rthash_size; i++) {
5767 LIST_INIT(&sc->sc_rthash[i]);
5768 }
5769
5770 sc->sc_rthash_key = RandomULong();
5771
5772 LIST_INIT(&sc->sc_rtlist);
5773
5774 return 0;
5775 }
5776
5777 /*
5778 * bridge_rthash_delayed_resize:
5779 *
5780 * Resize the routing table hash on a delayed thread call.
5781 */
5782 static void
5783 bridge_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 */
5797 if (sc->sc_brtcnt < sc->sc_rthash_size * 4) {
5798 goto out;
5799 }
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
5810 new_rthash = _MALLOC(sizeof(*sc->sc_rthash) * new_rthash_size,
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
5837 for (i = 0; i < sc->sc_rthash_size; i++) {
5838 LIST_INIT(&sc->sc_rthash[i]);
5839 }
5840
5841 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
5842 LIST_REMOVE(brt, brt_hash);
5843 (void) bridge_rtnode_hash(sc, brt);
5844 }
5845 out:
5846 if (error == 0) {
5847 #if BRIDGE_DEBUG
5848 if (IF_BRIDGE_DEBUG(BR_DBGF_RT_TABLE)) {
5849 printf("%s: %s new size %u\n", __func__,
5850 sc->sc_ifp->if_xname, sc->sc_rthash_size);
5851 }
5852 #endif /* BRIDGE_DEBUG */
5853 if (old_rthash) {
5854 _FREE(old_rthash, M_DEVBUF);
5855 }
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 */
5861 if (new_rthash != NULL) {
5862 _FREE(new_rthash, M_DEVBUF);
5863 }
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 */
5872 static void
5873 bridge_rthash_resize(struct bridge_softc *sc)
5874 {
5875 BRIDGE_LOCK_ASSERT_HELD(sc);
5876
5877 if ((sc->sc_flags & SCF_DETACHING) || (sc->sc_flags & SCF_RESIZING)) {
5878 return;
5879 }
5880
5881 /*
5882 * Four entries per hash bucket is our ideal load factor
5883 */
5884 if (sc->sc_brtcnt < sc->sc_rthash_size * 4) {
5885 return;
5886 }
5887 /*
5888 * Hard limit on the size of the routing hash table
5889 */
5890 if (sc->sc_rthash_size >= bridge_rtable_hash_size_max) {
5891 return;
5892 }
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
5899 /*
5900 * bridge_rtable_fini:
5901 *
5902 * Deconstruct the route table for this bridge.
5903 */
5904 static void
5905 bridge_rtable_fini(struct bridge_softc *sc)
5906 {
5907 KASSERT(sc->sc_brtcnt == 0,
5908 ("%s: %d bridge routes referenced", __func__, sc->sc_brtcnt));
5909 if (sc->sc_rthash) {
5910 _FREE(sc->sc_rthash, M_DEVBUF);
5911 sc->sc_rthash = NULL;
5912 }
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 */
5919 #define mix(a, b, c) \
5920 do { \
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)
5931
5932 static __inline uint32_t
5933 bridge_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
5946 return c & BRIDGE_RTHASH_MASK(sc);
5947 }
5948
5949 #undef mix
5950
5951 static int
5952 bridge_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
5960 return d;
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 */
5969 static struct bridge_rtnode *
5970 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr,
5971 uint16_t vlan)
5972 {
5973 struct bridge_rtnode *brt;
5974 uint32_t hash;
5975 int dir;
5976
5977 BRIDGE_LOCK_ASSERT_HELD(sc);
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);
5982 if (dir == 0 && (brt->brt_vlan == vlan || vlan == 0)) {
5983 return brt;
5984 }
5985 if (dir > 0) {
5986 return NULL;
5987 }
5988 }
5989
5990 return NULL;
5991 }
5992
5993 /*
5994 * bridge_rtnode_hash:
5995 *
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
5999 */
6000 static int
6001 bridge_rtnode_hash(struct bridge_softc *sc, struct bridge_rtnode *brt)
6002 {
6003 struct bridge_rtnode *lbrt;
6004 uint32_t hash;
6005 int dir;
6006
6007 BRIDGE_LOCK_ASSERT_HELD(sc);
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);
6019 if (dir == 0 && brt->brt_vlan == lbrt->brt_vlan) {
6020 #if BRIDGE_DEBUG
6021 if (IF_BRIDGE_DEBUG(BR_DBGF_RT_TABLE)) {
6022 printf("%s: %s EEXIST "
6023 "%02x:%02x:%02x:%02x:%02x:%02x\n",
6024 __func__, sc->sc_ifp->if_xname,
6025 brt->brt_addr[0], brt->brt_addr[1],
6026 brt->brt_addr[2], brt->brt_addr[3],
6027 brt->brt_addr[4], brt->brt_addr[5]);
6028 }
6029 #endif
6030 return EEXIST;
6031 }
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
6043 #if BRIDGE_DEBUG
6044 if (IF_BRIDGE_DEBUG(BR_DBGF_RT_TABLE)) {
6045 printf("%s: %s impossible %02x:%02x:%02x:%02x:%02x:%02x\n",
6046 __func__, sc->sc_ifp->if_xname,
6047 brt->brt_addr[0], brt->brt_addr[1], brt->brt_addr[2],
6048 brt->brt_addr[3], brt->brt_addr[4], brt->brt_addr[5]);
6049 }
6050 #endif
6051
6052 out:
6053 return 0;
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 */
6062 static int
6063 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
6064 {
6065 int error;
6066
6067 error = bridge_rtnode_hash(sc, brt);
6068 if (error != 0) {
6069 return error;
6070 }
6071
6072 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
6073 sc->sc_brtcnt++;
6074
6075 bridge_rthash_resize(sc);
6076
6077 return 0;
6078 }
6079
6080 /*
6081 * bridge_rtnode_destroy:
6082 *
6083 * Destroy a bridge rtnode.
6084 */
6085 static void
6086 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
6087 {
6088 BRIDGE_LOCK_ASSERT_HELD(sc);
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
6098 #if BRIDGESTP
6099 /*
6100 * bridge_rtable_expire:
6101 *
6102 * Set the expiry time for all routes on an interface.
6103 */
6104 static void
6105 bridge_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 */
6116 if (age == 0) {
6117 bridge_rtdelete(sc, ifp, IFBF_FLUSHDYN);
6118 } else {
6119 unsigned long now;
6120
6121 now = (unsigned long) net_uptime();
6122
6123 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
6124 /* Cap the expiry time to 'age' */
6125 if (brt->brt_ifp == ifp &&
6126 brt->brt_expire > now + age &&
6127 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
6128 brt->brt_expire = now + age;
6129 }
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 */
6140 static void
6141 bridge_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
6153 if (log_stp) {
6154 log(LOG_NOTICE, "%s: state changed to %s on %s\n",
6155 sc->sc_ifp->if_xname,
6156 stpstates[state], ifp->if_xname);
6157 }
6158 }
6159 #endif /* BRIDGESTP */
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 */
6168 static int
6169 bridge_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;
6179 error = -1; /* Default error if not error == 0 */
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
6186 if (pfil_bridge == 0 && pfil_member == 0 && pfil_ipfw == 0) {
6187 return 0; /* filtering is disabled */
6188 }
6189 i = min((*mp)->m_pkthdr.len, max_protohdr);
6190 if ((*mp)->m_len < i) {
6191 *mp = m_pullup(*mp, i);
6192 if (*mp == NULL) {
6193 printf("%s: m_pullup failed\n", __func__);
6194 return -1;
6195 }
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) {
6226 case ETHERTYPE_ARP:
6227 case ETHERTYPE_REVARP:
6228 if (pfil_ipfw_arp == 0) {
6229 return 0; /* Automatically pass */
6230 }
6231 break;
6232
6233 case ETHERTYPE_IP:
6234 #if INET6
6235 case ETHERTYPE_IPV6:
6236 #endif /* INET6 */
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 }
6247 }
6248
6249 /* Strip off the Ethernet header and keep a copy. */
6250 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t)&eh2);
6251 m_adj(*mp, ETHER_HDR_LEN);
6252
6253 /* Strip off snap header, if present */
6254 if (snap) {
6255 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t)&llc1);
6256 m_adj(*mp, sizeof(struct llc));
6257 }
6258
6259 /*
6260 * Check the IP header for alignment and errors
6261 */
6262 if (dir == PFIL_IN) {
6263 switch (ether_type) {
6264 case ETHERTYPE_IP:
6265 error = bridge_ip_checkbasic(mp);
6266 break;
6267 #if INET6
6268 case ETHERTYPE_IPV6:
6269 error = bridge_ip6_checkbasic(mp);
6270 break;
6271 #endif /* INET6 */
6272 default:
6273 error = 0;
6274 }
6275 if (error) {
6276 goto bad;
6277 }
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);
6283 if (args.rule != NULL && fw_one_pass) {
6284 goto ipfwpass; /* packet already partially processed */
6285 }
6286 args.m = *mp;
6287 args.oif = ifp;
6288 args.next_hop = NULL;
6289 args.eh = &eh2;
6290 args.inp = NULL; /* used by ipfw uid/gid/jail rules */
6291 i = ip_fw_chk_ptr(&args);
6292 *mp = args.m;
6293
6294 if (*mp == NULL) {
6295 return error;
6296 }
6297
6298 if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
6299 /* put the Ethernet header back on */
6300 M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT, 0);
6301 if (*mp == NULL) {
6302 return error;
6303 }
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;
6311 ip_dn_io_ptr(mp, DN_TO_IFB_FWD, &args, DN_CLIENT_IPFW);
6312 return error;
6313 }
6314
6315 if (i != IP_FW_PASS) { /* drop */
6316 goto bad;
6317 }
6318 }
6319
6320 ipfwpass:
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 */
6344 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL) {
6345 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
6346 dir, NULL);
6347 }
6348
6349 if (*mp == NULL || error != 0) { /* filter may consume */
6350 break;
6351 }
6352
6353 if (pfil_member && ifp != NULL) {
6354 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp,
6355 dir, NULL);
6356 }
6357
6358 if (*mp == NULL || error != 0) { /* filter may consume */
6359 break;
6360 }
6361
6362 if (pfil_bridge && dir == PFIL_IN && bifp != NULL) {
6363 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
6364 dir, NULL);
6365 }
6366
6367 if (*mp == NULL || error != 0) { /* filter may consume */
6368 break;
6369 }
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,
6376 &llc1);
6377 return error;
6378 }
6379 }
6380
6381 /* Recalculate the ip checksum and restore byte ordering */
6382 ip = mtod(*mp, struct ip *);
6383 hlen = ip->ip_hl << 2;
6384 if (hlen < sizeof(struct ip)) {
6385 goto bad;
6386 }
6387 if (hlen > (*mp)->m_len) {
6388 if ((*mp = m_pullup(*mp, hlen)) == 0) {
6389 goto bad;
6390 }
6391 ip = mtod(*mp, struct ip *);
6392 if (ip == NULL) {
6393 goto bad;
6394 }
6395 }
6396 ip->ip_len = htons(ip->ip_len);
6397 ip->ip_off = htons(ip->ip_off);
6398 ip->ip_sum = 0;
6399 if (hlen == sizeof(struct ip)) {
6400 ip->ip_sum = in_cksum_hdr(ip);
6401 } else {
6402 ip->ip_sum = in_cksum(*mp, hlen);
6403 }
6404
6405 break;
6406 #if INET6
6407 case ETHERTYPE_IPV6:
6408 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL) {
6409 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
6410 dir, NULL);
6411 }
6412
6413 if (*mp == NULL || error != 0) { /* filter may consume */
6414 break;
6415 }
6416
6417 if (pfil_member && ifp != NULL) {
6418 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
6419 dir, NULL);
6420 }
6421
6422 if (*mp == NULL || error != 0) { /* filter may consume */
6423 break;
6424 }
6425
6426 if (pfil_bridge && dir == PFIL_IN && bifp != NULL) {
6427 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
6428 dir, NULL);
6429 }
6430 break;
6431 #endif
6432 default:
6433 error = 0;
6434 break;
6435 }
6436
6437 if (*mp == NULL) {
6438 return error;
6439 }
6440 if (error != 0) {
6441 goto bad;
6442 }
6443
6444 error = -1;
6445
6446 /*
6447 * Finally, put everything back the way it was and return
6448 */
6449 if (snap) {
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));
6455 }
6456
6457 M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT, 0);
6458 if (*mp == NULL) {
6459 return error;
6460 }
6461 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
6462
6463 return 0;
6464
6465 bad:
6466 m_freem(*mp);
6467 *mp = NULL;
6468 return error;
6469 }
6470 #endif /* PFIL_HOOKS */
6471
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 */
6484 static int
6485 bridge_ip_checkbasic(struct mbuf **mp)
6486 {
6487 struct mbuf *m = *mp;
6488 struct ip *ip;
6489 int len, hlen;
6490 u_short sum;
6491
6492 if (*mp == NULL) {
6493 return -1;
6494 }
6495
6496 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
6497 /* max_linkhdr is already rounded up to nearest 4-byte */
6498 if ((m = m_copyup(m, sizeof(struct ip),
6499 max_linkhdr)) == NULL) {
6500 /* XXXJRT new stat, please */
6501 ipstat.ips_toosmall++;
6502 goto bad;
6503 }
6504 } else if (OS_EXPECT((size_t)m->m_len < sizeof(struct ip), 0)) {
6505 if ((m = m_pullup(m, sizeof(struct ip))) == NULL) {
6506 ipstat.ips_toosmall++;
6507 goto bad;
6508 }
6509 }
6510 ip = mtod(m, struct ip *);
6511 if (ip == NULL) {
6512 goto bad;
6513 }
6514
6515 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
6516 ipstat.ips_badvers++;
6517 goto bad;
6518 }
6519 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
6520 if (hlen < (int)sizeof(struct ip)) { /* minimum header length */
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 *);
6530 if (ip == NULL) {
6531 goto bad;
6532 }
6533 }
6534
6535 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
6536 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
6537 } else {
6538 if (hlen == sizeof(struct ip)) {
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;
6572 return 0;
6573
6574 bad:
6575 *mp = m;
6576 return -1;
6577 }
6578
6579 #if INET6
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 */
6585 static int
6586 bridge_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;
6599 /* max_linkhdr is already rounded up to nearest 4-byte */
6600 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
6601 max_linkhdr)) == NULL) {
6602 /* XXXJRT new stat, please */
6603 ip6stat.ip6s_toosmall++;
6604 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
6605 goto bad;
6606 }
6607 } else if (OS_EXPECT((size_t)m->m_len < sizeof(struct ip6_hdr), 0)) {
6608 struct ifnet *inifp = m->m_pkthdr.rcvif;
6609 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
6610 ip6stat.ip6s_toosmall++;
6611 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
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;
6626 return 0;
6627
6628 bad:
6629 *mp = m;
6630 return -1;
6631 }
6632 #endif /* INET6 */
6633
6634 #ifdef PFIL_HOOKS
6635 /*
6636 * bridge_fragment:
6637 *
6638 * Return a fragmented mbuf chain.
6639 */
6640 static int
6641 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
6642 int snap, struct llc *llc)
6643 {
6644 struct mbuf *m0;
6645 struct ip *ip;
6646 int error = -1;
6647
6648 if (m->m_len < sizeof(struct ip) &&
6649 (m = m_pullup(m, sizeof(struct ip))) == NULL) {
6650 goto out;
6651 }
6652 ip = mtod(m, struct ip *);
6653
6654 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
6655 CSUM_DELAY_IP);
6656 if (error) {
6657 goto out;
6658 }
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) {
6664 M_PREPEND(m0, sizeof(struct llc), M_DONTWAIT, 0);
6665 if (m0 == NULL) {
6666 error = ENOBUFS;
6667 continue;
6668 }
6669 bcopy(llc, mtod(m0, caddr_t),
6670 sizeof(struct llc));
6671 }
6672 M_PREPEND(m0, ETHER_HDR_LEN, M_DONTWAIT, 0);
6673 if (m0 == NULL) {
6674 error = ENOBUFS;
6675 continue;
6676 }
6677 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
6678 } else {
6679 m_freem(m);
6680 }
6681 }
6682
6683 if (error == 0) {
6684 ipstat.ips_fragmented++;
6685 }
6686
6687 return error;
6688
6689 out:
6690 if (m != NULL) {
6691 m_freem(m);
6692 }
6693 return error;
6694 }
6695 #endif /* PFIL_HOOKS */
6696
6697 /*
6698 * bridge_set_bpf_tap:
6699 *
6700 * Sets ups the BPF callbacks.
6701 */
6702 static errno_t
6703 bridge_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);
6706
6707 /* TBD locking */
6708 if (sc == NULL || (sc->sc_flags & SCF_DETACHING)) {
6709 return ENODEV;
6710 }
6711 switch (mode) {
6712 case BPF_TAP_DISABLE:
6713 sc->sc_bpf_input = sc->sc_bpf_output = NULL;
6714 break;
6715
6716 case BPF_TAP_INPUT:
6717 sc->sc_bpf_input = bpf_callback;
6718 break;
6719
6720 case BPF_TAP_OUTPUT:
6721 sc->sc_bpf_output = bpf_callback;
6722 break;
6723
6724 case BPF_TAP_INPUT_OUTPUT:
6725 sc->sc_bpf_input = sc->sc_bpf_output = bpf_callback;
6726 break;
6727
6728 default:
6729 break;
6730 }
6731
6732 return 0;
6733 }
6734
6735 /*
6736 * bridge_detach:
6737 *
6738 * Callback when interface has been detached.
6739 */
6740 static void
6741 bridge_detach(ifnet_t ifp)
6742 {
6743 struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp);
6744
6745 #if BRIDGESTP
6746 bstp_detach(&sc->sc_stp);
6747 #endif /* BRIDGESTP */
6748
6749 /* Tear down the routing table. */
6750 bridge_rtable_fini(sc);
6751
6752 lck_mtx_lock(&bridge_list_mtx);
6753 LIST_REMOVE(sc, sc_list);
6754 lck_mtx_unlock(&bridge_list_mtx);
6755
6756 ifnet_release(ifp);
6757
6758 lck_mtx_destroy(&sc->sc_mtx, bridge_lock_grp);
6759 if_clone_softc_deallocate(&bridge_cloner, sc);
6760 }
6761
6762 /*
6763 * bridge_bpf_input:
6764 *
6765 * Invoke the input BPF callback if enabled
6766 */
6767 static errno_t
6768 bridge_bpf_input(ifnet_t ifp, struct mbuf *m, const char * func, int line)
6769 {
6770 struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp);
6771 bpf_packet_func input_func = sc->sc_bpf_input;
6772
6773 if (input_func != NULL) {
6774 if (mbuf_pkthdr_rcvif(m) != ifp) {
6775 printf("%s.%d: rcvif: 0x%llx != ifp 0x%llx\n", func, line,
6776 (uint64_t)VM_KERNEL_ADDRPERM(mbuf_pkthdr_rcvif(m)),
6777 (uint64_t)VM_KERNEL_ADDRPERM(ifp));
6778 }
6779 (*input_func)(ifp, m);
6780 }
6781 return 0;
6782 }
6783
6784 /*
6785 * bridge_bpf_output:
6786 *
6787 * Invoke the output BPF callback if enabled
6788 */
6789 static errno_t
6790 bridge_bpf_output(ifnet_t ifp, struct mbuf *m)
6791 {
6792 struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp);
6793 bpf_packet_func output_func = sc->sc_bpf_output;
6794
6795 if (output_func != NULL) {
6796 (*output_func)(ifp, m);
6797 }
6798 return 0;
6799 }
6800
6801 /*
6802 * bridge_link_event:
6803 *
6804 * Report a data link event on an interface
6805 */
6806 static void
6807 bridge_link_event(struct ifnet *ifp, u_int32_t event_code)
6808 {
6809 struct {
6810 struct kern_event_msg header;
6811 u_int32_t unit;
6812 char if_name[IFNAMSIZ];
6813 } event;
6814
6815 #if BRIDGE_DEBUG
6816 if (IF_BRIDGE_DEBUG(BR_DBGF_LIFECYCLE)) {
6817 printf("%s: %s event_code %u - %s\n", __func__, ifp->if_xname,
6818 event_code, dlil_kev_dl_code_str(event_code));
6819 }
6820 #endif /* BRIDGE_DEBUG */
6821
6822 bzero(&event, sizeof(event));
6823 event.header.total_size = sizeof(event);
6824 event.header.vendor_code = KEV_VENDOR_APPLE;
6825 event.header.kev_class = KEV_NETWORK_CLASS;
6826 event.header.kev_subclass = KEV_DL_SUBCLASS;
6827 event.header.event_code = event_code;
6828 event.header.event_data[0] = ifnet_family(ifp);
6829 event.unit = (u_int32_t)ifnet_unit(ifp);
6830 strlcpy(event.if_name, ifnet_name(ifp), IFNAMSIZ);
6831 ifnet_event(ifp, &event.header);
6832 }
6833
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 }
6841
6842 /*
6843 * Make sure this is a DHCP or Bootp request that match the host filter
6844 */
6845 static int
6846 bridge_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;
6889 done:
6890 return error;
6891 }
6892
6893 static int
6894 bridge_host_filter(struct bridge_iflist *bif, mbuf_t *data)
6895 {
6896 int error = EINVAL;
6897 struct ether_header *eh;
6898 static struct in_addr inaddr_any = { .s_addr = INADDR_ANY };
6899 mbuf_t m = *data;
6900
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) +
6919 sizeof(struct ether_arp);
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 }
6928 if (mbuf_len(m) < minlen && mbuf_pullup(data, minlen) != 0) {
6929 BRIDGE_HF_DROP(brhf_arp_pullup_failed,
6930 __func__, __LINE__);
6931 goto done;
6932 }
6933 m = *data;
6934
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,
6942 __func__, __LINE__);
6943 goto done;
6944 }
6945 if (ea->arp_pro != htons(ETHERTYPE_IP)) {
6946 BRIDGE_HF_DROP(brhf_arp_bad_pro_type,
6947 __func__, __LINE__);
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,
6959 __func__, __LINE__);
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,
6984 sizeof(struct in_addr)) != 0 &&
6985 bcmp(ea->arp_spa, &inaddr_any,
6986 sizeof(struct in_addr)) != 0) {
6987 BRIDGE_HF_DROP(brhf_arp_bad_spa, __func__, __LINE__);
6988 goto done;
6989 }
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,
7019 __func__, __LINE__);
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,
7029 __func__, __LINE__);
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,
7038 __func__, __LINE__);
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,
7050 __func__, __LINE__);
7051 goto done;
7052 }
7053 offset += sizeof(struct udphdr);
7054 error = bridge_dhcp_filter(bif, m, offset);
7055 if (error != 0) {
7056 goto done;
7057 }
7058 } else if (iphdr.ip_src.s_addr == INADDR_ANY) {
7059 BRIDGE_HF_DROP(brhf_ip_bad_srcaddr,
7060 __func__, __LINE__);
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) {
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 }
7086 done:
7087 if (error != 0) {
7088 if (IF_BRIDGE_DEBUG(BR_DBGF_HOSTFILTER)) {
7089 if (m) {
7090 printf_mbuf_data(m, 0,
7091 sizeof(struct ether_header) +
7092 sizeof(struct ip));
7093 }
7094 printf("\n");
7095 }
7096
7097 if (m != NULL) {
7098 m_freem(m);
7099 }
7100 }
7101 return error;
7102 }
7103
7104 /*
7105 * MAC NAT
7106 */
7107
7108 static errno_t
7109 bridge_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
7125 done:
7126 return error;
7127 }
7128
7129 static void
7130 bridge_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
7141 static void
7142 mac_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
7164 static void
7165 mac_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
7171 static struct mac_nat_entry *
7172 bridge_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
7207 static void
7208 bridge_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
7219 static struct mac_nat_entry *
7220 bridge_create_mac_nat_entry(struct bridge_softc *sc,
7221 struct bridge_iflist *bif, int af, const void *ip, uint8_t *eaddr)
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
7255 static struct mac_nat_entry *
7256 bridge_update_mac_nat_entry(struct bridge_softc *sc,
7257 struct bridge_iflist *bif, int af, void *ip, uint8_t *eaddr)
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
7293 static void
7294 bridge_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 */
7315 static void
7316 bridge_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
7325 static void
7326 bridge_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
7380 static void
7381 bridge_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
7394 static void
7395 bridge_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
7404 static const char *
7405 get_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 */
7416 static boolean_t
7417 is_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;
7484 done:
7485 return is_valid;
7486 }
7487
7488 static struct mac_nat_entry *
7489 bridge_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
7544 done:
7545 return mne;
7546 }
7547
7548 static boolean_t
7549 bridge_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
7584 done:
7585 return translate;
7586 }
7587
7588 #define ETHER_IPV4_HEADER_LEN (sizeof(struct ether_header) + \
7589 + sizeof(struct ip))
7590 static struct ether_header *
7591 get_ether_ip_header(mbuf_t *data, boolean_t is_output)
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 *);
7617 done:
7618 return eh;
7619 }
7620
7621 static struct mac_nat_entry *
7622 bridge_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);
7640 done:
7641 return mne;
7642 }
7643
7644 static void
7645 bridge_mac_nat_udp_output(struct bridge_softc *sc,
7646 struct bridge_iflist *bif, mbuf_t m,
7647 uint8_t ip_header_len, struct mac_nat_record *mnr)
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
7703 static boolean_t
7704 bridge_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 }
7739 done:
7740 return translate;
7741 }
7742
7743 #define ETHER_IPV6_HEADER_LEN (sizeof(struct ether_header) + \
7744 + sizeof(struct ip6_hdr))
7745 static struct ether_header *
7746 get_ether_ipv6_header(mbuf_t *data, boolean_t is_output)
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 *);
7772 done:
7773 return eh;
7774 }
7775
7776 #if 0
7777 static void
7778 bridge_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
7794 static void
7795 bridge_mac_nat_icmpv6_output(struct bridge_softc *sc, struct bridge_iflist *bif,
7796 mbuf_t *data, struct ether_header *eh,
7797 struct ip6_hdr *ip6h, struct in6_addr *saddrp, struct mac_nat_record *mnr)
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
7996 static struct mac_nat_entry *
7997 bridge_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
8021 done:
8022 return mne;
8023 }
8024
8025 static boolean_t
8026 bridge_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
8052 done:
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 */
8075 static ifnet_t
8076 bridge_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
8147 static boolean_t
8148 bridge_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
8180 static void
8181 bridge_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
8202 static void
8203 bridge_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
8237 static void
8238 bridge_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
8315 static void
8316 bridge_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 */
8353 static
8354 int
8355 bridge_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
8548 bad:
8549 m_freem(*mp);
8550 *mp = NULL;
8551 return error;
8552 }