2 * Handle incoming frames
3 * Linux ethernet bridge
6 * Lennert Buytenhek <buytenh@gnu.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 #include <linux/slab.h>
15 #include <linux/kernel.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/netfilter_bridge.h>
19 #include <linux/neighbour.h>
21 #include <linux/export.h>
22 #include <linux/rculist.h>
23 #include "br_private.h"
24 #include "br_private_tunnel.h"
26 /* Hook for brouter */
27 br_should_route_hook_t __rcu
*br_should_route_hook __read_mostly
;
28 EXPORT_SYMBOL(br_should_route_hook
);
31 br_netif_receive_skb(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
33 br_drop_fake_rtable(skb
);
34 return netif_receive_skb(skb
);
37 static int br_pass_frame_up(struct sk_buff
*skb
)
39 struct net_device
*indev
, *brdev
= BR_INPUT_SKB_CB(skb
)->brdev
;
40 struct net_bridge
*br
= netdev_priv(brdev
);
41 struct net_bridge_vlan_group
*vg
;
42 struct pcpu_sw_netstats
*brstats
= this_cpu_ptr(br
->stats
);
44 u64_stats_update_begin(&brstats
->syncp
);
45 brstats
->rx_packets
++;
46 brstats
->rx_bytes
+= skb
->len
;
47 u64_stats_update_end(&brstats
->syncp
);
49 vg
= br_vlan_group_rcu(br
);
50 /* Bridge is just like any other port. Make sure the
51 * packet is allowed except in promisc modue when someone
52 * may be running packet capture.
54 if (!(brdev
->flags
& IFF_PROMISC
) &&
55 !br_allowed_egress(vg
, skb
)) {
62 skb
= br_handle_vlan(br
, NULL
, vg
, skb
);
65 /* update the multicast stats if the packet is IGMP/MLD */
66 br_multicast_count(br
, NULL
, skb
, br_multicast_igmp_type(skb
),
69 return NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_IN
,
70 dev_net(indev
), NULL
, skb
, indev
, NULL
,
71 br_netif_receive_skb
);
74 /* note: already called with rcu_read_lock */
75 int br_handle_frame_finish(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
77 struct net_bridge_port
*p
= br_port_get_rcu(skb
->dev
);
78 enum br_pkt_type pkt_type
= BR_PKT_UNICAST
;
79 struct net_bridge_fdb_entry
*dst
= NULL
;
80 struct net_bridge_mdb_entry
*mdst
;
81 bool local_rcv
, mcast_hit
= false;
82 struct net_bridge
*br
;
85 if (!p
|| p
->state
== BR_STATE_DISABLED
)
88 if (!br_allowed_ingress(p
->br
, nbp_vlan_group_rcu(p
), skb
, &vid
))
91 nbp_switchdev_frame_mark(p
, skb
);
93 /* insert into forwarding database after filtering to avoid spoofing */
95 if (p
->flags
& BR_LEARNING
)
96 br_fdb_update(br
, p
, eth_hdr(skb
)->h_source
, vid
, false);
98 local_rcv
= !!(br
->dev
->flags
& IFF_PROMISC
);
99 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
)) {
100 /* by definition the broadcast is also a multicast address */
101 if (is_broadcast_ether_addr(eth_hdr(skb
)->h_dest
)) {
102 pkt_type
= BR_PKT_BROADCAST
;
105 pkt_type
= BR_PKT_MULTICAST
;
106 if (br_multicast_rcv(br
, p
, skb
, vid
))
111 if (p
->state
== BR_STATE_LEARNING
)
114 BR_INPUT_SKB_CB(skb
)->brdev
= br
->dev
;
115 BR_INPUT_SKB_CB(skb
)->src_port_isolated
= !!(p
->flags
& BR_ISOLATED
);
117 if (IS_ENABLED(CONFIG_INET
) &&
118 (skb
->protocol
== htons(ETH_P_ARP
) ||
119 skb
->protocol
== htons(ETH_P_RARP
))) {
120 br_do_proxy_suppress_arp(skb
, br
, vid
, p
);
121 } else if (IS_ENABLED(CONFIG_IPV6
) &&
122 skb
->protocol
== htons(ETH_P_IPV6
) &&
123 br
->neigh_suppress_enabled
&&
124 pskb_may_pull(skb
, sizeof(struct ipv6hdr
) +
125 sizeof(struct nd_msg
)) &&
126 ipv6_hdr(skb
)->nexthdr
== IPPROTO_ICMPV6
) {
127 struct nd_msg
*msg
, _msg
;
129 msg
= br_is_nd_neigh_msg(skb
, &_msg
);
131 br_do_suppress_nd(skb
, br
, vid
, p
, msg
);
135 case BR_PKT_MULTICAST
:
136 mdst
= br_mdb_get(br
, skb
, vid
);
137 if ((mdst
|| BR_INPUT_SKB_CB_MROUTERS_ONLY(skb
)) &&
138 br_multicast_querier_exists(br
, eth_hdr(skb
))) {
139 if ((mdst
&& mdst
->host_joined
) ||
140 br_multicast_is_router(br
)) {
142 br
->dev
->stats
.multicast
++;
147 br
->dev
->stats
.multicast
++;
151 dst
= br_fdb_find_rcu(br
, eth_hdr(skb
)->h_dest
, vid
);
157 unsigned long now
= jiffies
;
160 return br_pass_frame_up(skb
);
162 if (now
!= dst
->used
)
164 br_forward(dst
->dst
, skb
, local_rcv
, false);
167 br_flood(br
, skb
, pkt_type
, local_rcv
, false);
169 br_multicast_flood(mdst
, skb
, local_rcv
, false);
173 return br_pass_frame_up(skb
);
181 EXPORT_SYMBOL_GPL(br_handle_frame_finish
);
183 static void __br_handle_local_finish(struct sk_buff
*skb
)
185 struct net_bridge_port
*p
= br_port_get_rcu(skb
->dev
);
188 /* check if vlan is allowed, to avoid spoofing */
189 if (p
->flags
& BR_LEARNING
&& br_should_learn(p
, skb
, &vid
))
190 br_fdb_update(p
->br
, p
, eth_hdr(skb
)->h_source
, vid
, false);
193 /* note: already called with rcu_read_lock */
194 static int br_handle_local_finish(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
196 __br_handle_local_finish(skb
);
198 /* return 1 to signal the okfn() was called so it's ok to use the skb */
203 * Return NULL if skb is handled
204 * note: already called with rcu_read_lock
206 rx_handler_result_t
br_handle_frame(struct sk_buff
**pskb
)
208 struct net_bridge_port
*p
;
209 struct sk_buff
*skb
= *pskb
;
210 const unsigned char *dest
= eth_hdr(skb
)->h_dest
;
211 br_should_route_hook_t
*rhook
;
213 if (unlikely(skb
->pkt_type
== PACKET_LOOPBACK
))
214 return RX_HANDLER_PASS
;
216 if (!is_valid_ether_addr(eth_hdr(skb
)->h_source
))
219 skb
= skb_share_check(skb
, GFP_ATOMIC
);
221 return RX_HANDLER_CONSUMED
;
223 p
= br_port_get_rcu(skb
->dev
);
224 if (p
->flags
& BR_VLAN_TUNNEL
) {
225 if (br_handle_ingress_vlan_tunnel(skb
, p
,
226 nbp_vlan_group_rcu(p
)))
230 if (unlikely(is_link_local_ether_addr(dest
))) {
231 u16 fwd_mask
= p
->br
->group_fwd_mask_required
;
234 * See IEEE 802.1D Table 7-10 Reserved addresses
237 * Bridge Group Address 01-80-C2-00-00-00
238 * (MAC Control) 802.3 01-80-C2-00-00-01
239 * (Link Aggregation) 802.3 01-80-C2-00-00-02
240 * 802.1X PAE address 01-80-C2-00-00-03
242 * 802.1AB LLDP 01-80-C2-00-00-0E
244 * Others reserved for future standardization
246 fwd_mask
|= p
->group_fwd_mask
;
248 case 0x00: /* Bridge Group Address */
249 /* If STP is turned off,
250 then must forward to keep loop detection */
251 if (p
->br
->stp_enabled
== BR_NO_STP
||
252 fwd_mask
& (1u << dest
[5]))
255 __br_handle_local_finish(skb
);
256 return RX_HANDLER_PASS
;
258 case 0x01: /* IEEE MAC (Pause) */
261 case 0x0E: /* 802.1AB LLDP */
262 fwd_mask
|= p
->br
->group_fwd_mask
;
263 if (fwd_mask
& (1u << dest
[5]))
266 __br_handle_local_finish(skb
);
267 return RX_HANDLER_PASS
;
270 /* Allow selective forwarding for most other protocols */
271 fwd_mask
|= p
->br
->group_fwd_mask
;
272 if (fwd_mask
& (1u << dest
[5]))
276 /* The else clause should be hit when nf_hook():
277 * - returns < 0 (drop/error)
278 * - returns = 0 (stolen/nf_queue)
279 * Thus return 1 from the okfn() to signal the skb is ok to pass
281 if (NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_IN
,
282 dev_net(skb
->dev
), NULL
, skb
, skb
->dev
, NULL
,
283 br_handle_local_finish
) == 1) {
284 return RX_HANDLER_PASS
;
286 return RX_HANDLER_CONSUMED
;
292 case BR_STATE_FORWARDING
:
293 rhook
= rcu_dereference(br_should_route_hook
);
297 return RX_HANDLER_PASS
;
299 dest
= eth_hdr(skb
)->h_dest
;
302 case BR_STATE_LEARNING
:
303 if (ether_addr_equal(p
->br
->dev
->dev_addr
, dest
))
304 skb
->pkt_type
= PACKET_HOST
;
306 NF_HOOK(NFPROTO_BRIDGE
, NF_BR_PRE_ROUTING
,
307 dev_net(skb
->dev
), NULL
, skb
, skb
->dev
, NULL
,
308 br_handle_frame_finish
);
314 return RX_HANDLER_CONSUMED
;