1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Handle incoming frames
4 * Linux ethernet bridge
7 * Lennert Buytenhek <buytenh@gnu.org>
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/netfilter_bridge.h>
15 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
16 #include <net/netfilter/nf_queue.h>
18 #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"
27 br_netif_receive_skb(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
29 br_drop_fake_rtable(skb
);
30 return netif_receive_skb(skb
);
33 static int br_pass_frame_up(struct sk_buff
*skb
, bool promisc
)
35 struct net_device
*indev
, *brdev
= BR_INPUT_SKB_CB(skb
)->brdev
;
36 struct net_bridge
*br
= netdev_priv(brdev
);
37 struct net_bridge_vlan_group
*vg
;
39 dev_sw_netstats_rx_add(brdev
, skb
->len
);
41 vg
= br_vlan_group_rcu(br
);
43 /* Reset the offload_fwd_mark because there could be a stacked
44 * bridge above, and it should not think this bridge it doing
45 * that bridge's work forwarding out its ports.
47 br_switchdev_frame_unmark(skb
);
49 /* Bridge is just like any other port. Make sure the
50 * packet is allowed except in promisc mode when someone
51 * may be running packet capture.
53 if (!(brdev
->flags
& IFF_PROMISC
) &&
54 !br_allowed_egress(vg
, skb
)) {
61 skb
= br_handle_vlan(br
, NULL
, vg
, skb
);
64 /* update the multicast stats if the packet is IGMP/MLD */
65 br_multicast_count(br
, NULL
, skb
, br_multicast_igmp_type(skb
),
68 BR_INPUT_SKB_CB(skb
)->promisc
= promisc
;
70 return NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_IN
,
71 dev_net(indev
), NULL
, skb
, indev
, NULL
,
72 br_netif_receive_skb
);
75 /* note: already called with rcu_read_lock */
76 int br_handle_frame_finish(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
78 struct net_bridge_port
*p
= br_port_get_rcu(skb
->dev
);
79 enum br_pkt_type pkt_type
= BR_PKT_UNICAST
;
80 struct net_bridge_fdb_entry
*dst
= NULL
;
81 struct net_bridge_mcast_port
*pmctx
;
82 struct net_bridge_mdb_entry
*mdst
;
83 bool local_rcv
, mcast_hit
= false;
84 struct net_bridge_mcast
*brmctx
;
85 struct net_bridge_vlan
*vlan
;
86 struct net_bridge
*br
;
96 if (br_mst_is_enabled(br
)) {
97 state
= BR_STATE_FORWARDING
;
99 if (p
->state
== BR_STATE_DISABLED
)
105 brmctx
= &p
->br
->multicast_ctx
;
106 pmctx
= &p
->multicast_ctx
;
107 if (!br_allowed_ingress(p
->br
, nbp_vlan_group_rcu(p
), skb
, &vid
,
111 if (p
->flags
& BR_PORT_LOCKED
) {
112 struct net_bridge_fdb_entry
*fdb_src
=
113 br_fdb_find_rcu(br
, eth_hdr(skb
)->h_source
, vid
);
116 /* FDB miss. Create locked FDB entry if MAB is enabled
117 * and drop the packet.
119 if (p
->flags
& BR_PORT_MAB
)
120 br_fdb_update(br
, p
, eth_hdr(skb
)->h_source
,
121 vid
, BIT(BR_FDB_LOCKED
));
123 } else if (READ_ONCE(fdb_src
->dst
) != p
||
124 test_bit(BR_FDB_LOCAL
, &fdb_src
->flags
)) {
125 /* FDB mismatch. Drop the packet without roaming. */
127 } else if (test_bit(BR_FDB_LOCKED
, &fdb_src
->flags
)) {
128 /* FDB match, but entry is locked. Refresh it and drop
131 br_fdb_update(br
, p
, eth_hdr(skb
)->h_source
, vid
,
137 nbp_switchdev_frame_mark(p
, skb
);
139 /* insert into forwarding database after filtering to avoid spoofing */
140 if (p
->flags
& BR_LEARNING
)
141 br_fdb_update(br
, p
, eth_hdr(skb
)->h_source
, vid
, 0);
143 promisc
= !!(br
->dev
->flags
& IFF_PROMISC
);
146 if (is_multicast_ether_addr(eth_hdr(skb
)->h_dest
)) {
147 /* by definition the broadcast is also a multicast address */
148 if (is_broadcast_ether_addr(eth_hdr(skb
)->h_dest
)) {
149 pkt_type
= BR_PKT_BROADCAST
;
152 pkt_type
= BR_PKT_MULTICAST
;
153 if (br_multicast_rcv(&brmctx
, &pmctx
, vlan
, skb
, vid
))
158 if (state
== BR_STATE_LEARNING
)
161 BR_INPUT_SKB_CB(skb
)->brdev
= br
->dev
;
162 BR_INPUT_SKB_CB(skb
)->src_port_isolated
= !!(p
->flags
& BR_ISOLATED
);
164 if (IS_ENABLED(CONFIG_INET
) &&
165 (skb
->protocol
== htons(ETH_P_ARP
) ||
166 skb
->protocol
== htons(ETH_P_RARP
))) {
167 br_do_proxy_suppress_arp(skb
, br
, vid
, p
);
168 } else if (IS_ENABLED(CONFIG_IPV6
) &&
169 skb
->protocol
== htons(ETH_P_IPV6
) &&
170 br_opt_get(br
, BROPT_NEIGH_SUPPRESS_ENABLED
) &&
171 pskb_may_pull(skb
, sizeof(struct ipv6hdr
) +
172 sizeof(struct nd_msg
)) &&
173 ipv6_hdr(skb
)->nexthdr
== IPPROTO_ICMPV6
) {
174 struct nd_msg
*msg
, _msg
;
176 msg
= br_is_nd_neigh_msg(skb
, &_msg
);
178 br_do_suppress_nd(skb
, br
, vid
, p
, msg
);
182 case BR_PKT_MULTICAST
:
183 mdst
= br_mdb_entry_skb_get(brmctx
, skb
, vid
);
184 if ((mdst
|| BR_INPUT_SKB_CB_MROUTERS_ONLY(skb
)) &&
185 br_multicast_querier_exists(brmctx
, eth_hdr(skb
), mdst
)) {
186 if ((mdst
&& mdst
->host_joined
) ||
187 br_multicast_is_router(brmctx
, skb
)) {
189 DEV_STATS_INC(br
->dev
, multicast
);
194 DEV_STATS_INC(br
->dev
, multicast
);
198 dst
= br_fdb_find_rcu(br
, eth_hdr(skb
)->h_dest
, vid
);
205 unsigned long now
= jiffies
;
207 if (test_bit(BR_FDB_LOCAL
, &dst
->flags
))
208 return br_pass_frame_up(skb
, false);
210 if (now
!= dst
->used
)
212 br_forward(dst
->dst
, skb
, local_rcv
, false);
215 br_flood(br
, skb
, pkt_type
, local_rcv
, false, vid
);
217 br_multicast_flood(mdst
, skb
, brmctx
, local_rcv
, false);
221 return br_pass_frame_up(skb
, promisc
);
229 EXPORT_SYMBOL_GPL(br_handle_frame_finish
);
231 static void __br_handle_local_finish(struct sk_buff
*skb
)
233 struct net_bridge_port
*p
= br_port_get_rcu(skb
->dev
);
236 /* check if vlan is allowed, to avoid spoofing */
237 if ((p
->flags
& BR_LEARNING
) &&
238 nbp_state_should_learn(p
) &&
239 !br_opt_get(p
->br
, BROPT_NO_LL_LEARN
) &&
240 br_should_learn(p
, skb
, &vid
))
241 br_fdb_update(p
->br
, p
, eth_hdr(skb
)->h_source
, vid
, 0);
244 /* note: already called with rcu_read_lock */
245 static int br_handle_local_finish(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
247 __br_handle_local_finish(skb
);
249 /* return 1 to signal the okfn() was called so it's ok to use the skb */
253 static int nf_hook_bridge_pre(struct sk_buff
*skb
, struct sk_buff
**pskb
)
255 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
256 struct nf_hook_entries
*e
= NULL
;
257 struct nf_hook_state state
;
258 unsigned int verdict
, i
;
262 net
= dev_net(skb
->dev
);
263 #ifdef HAVE_JUMP_LABEL
264 if (!static_key_false(&nf_hooks_needed
[NFPROTO_BRIDGE
][NF_BR_PRE_ROUTING
]))
268 e
= rcu_dereference(net
->nf
.hooks_bridge
[NF_BR_PRE_ROUTING
]);
272 nf_hook_state_init(&state
, NF_BR_PRE_ROUTING
,
273 NFPROTO_BRIDGE
, skb
->dev
, NULL
, NULL
,
274 net
, br_handle_frame_finish
);
276 for (i
= 0; i
< e
->num_hook_entries
; i
++) {
277 verdict
= nf_hook_entry_hookfn(&e
->hooks
[i
], skb
, &state
);
278 switch (verdict
& NF_VERDICT_MASK
) {
280 if (BR_INPUT_SKB_CB(skb
)->br_netfilter_broute
) {
282 return RX_HANDLER_PASS
;
287 return RX_HANDLER_CONSUMED
;
289 ret
= nf_queue(skb
, &state
, i
, verdict
);
292 return RX_HANDLER_CONSUMED
;
293 default: /* STOLEN */
294 return RX_HANDLER_CONSUMED
;
298 net
= dev_net(skb
->dev
);
299 br_handle_frame_finish(net
, NULL
, skb
);
301 br_handle_frame_finish(dev_net(skb
->dev
), NULL
, skb
);
303 return RX_HANDLER_CONSUMED
;
306 /* Return 0 if the frame was not processed otherwise 1
307 * note: already called with rcu_read_lock
309 static int br_process_frame_type(struct net_bridge_port
*p
,
312 struct br_frame_type
*tmp
;
314 hlist_for_each_entry_rcu(tmp
, &p
->br
->frame_type_list
, list
)
315 if (unlikely(tmp
->type
== skb
->protocol
))
316 return tmp
->frame_handler(p
, skb
);
322 * Return NULL if skb is handled
323 * note: already called with rcu_read_lock
325 static rx_handler_result_t
br_handle_frame(struct sk_buff
**pskb
)
327 struct net_bridge_port
*p
;
328 struct sk_buff
*skb
= *pskb
;
329 const unsigned char *dest
= eth_hdr(skb
)->h_dest
;
331 if (unlikely(skb
->pkt_type
== PACKET_LOOPBACK
))
332 return RX_HANDLER_PASS
;
334 if (!is_valid_ether_addr(eth_hdr(skb
)->h_source
))
337 skb
= skb_share_check(skb
, GFP_ATOMIC
);
339 return RX_HANDLER_CONSUMED
;
341 memset(skb
->cb
, 0, sizeof(struct br_input_skb_cb
));
342 br_tc_skb_miss_set(skb
, false);
344 p
= br_port_get_rcu(skb
->dev
);
345 if (p
->flags
& BR_VLAN_TUNNEL
)
346 br_handle_ingress_vlan_tunnel(skb
, p
, nbp_vlan_group_rcu(p
));
348 if (unlikely(is_link_local_ether_addr(dest
))) {
349 u16 fwd_mask
= p
->br
->group_fwd_mask_required
;
352 * See IEEE 802.1D Table 7-10 Reserved addresses
355 * Bridge Group Address 01-80-C2-00-00-00
356 * (MAC Control) 802.3 01-80-C2-00-00-01
357 * (Link Aggregation) 802.3 01-80-C2-00-00-02
358 * 802.1X PAE address 01-80-C2-00-00-03
360 * 802.1AB LLDP 01-80-C2-00-00-0E
362 * Others reserved for future standardization
364 fwd_mask
|= p
->group_fwd_mask
;
366 case 0x00: /* Bridge Group Address */
367 /* If STP is turned off,
368 then must forward to keep loop detection */
369 if (p
->br
->stp_enabled
== BR_NO_STP
||
370 fwd_mask
& (1u << dest
[5]))
373 __br_handle_local_finish(skb
);
374 return RX_HANDLER_PASS
;
376 case 0x01: /* IEEE MAC (Pause) */
379 case 0x0E: /* 802.1AB LLDP */
380 fwd_mask
|= p
->br
->group_fwd_mask
;
381 if (fwd_mask
& (1u << dest
[5]))
384 __br_handle_local_finish(skb
);
385 return RX_HANDLER_PASS
;
388 /* Allow selective forwarding for most other protocols */
389 fwd_mask
|= p
->br
->group_fwd_mask
;
390 if (fwd_mask
& (1u << dest
[5]))
394 BR_INPUT_SKB_CB(skb
)->promisc
= false;
396 /* The else clause should be hit when nf_hook():
397 * - returns < 0 (drop/error)
398 * - returns = 0 (stolen/nf_queue)
399 * Thus return 1 from the okfn() to signal the skb is ok to pass
401 if (NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_IN
,
402 dev_net(skb
->dev
), NULL
, skb
, skb
->dev
, NULL
,
403 br_handle_local_finish
) == 1) {
404 return RX_HANDLER_PASS
;
406 return RX_HANDLER_CONSUMED
;
410 if (unlikely(br_process_frame_type(p
, skb
)))
411 return RX_HANDLER_PASS
;
414 if (br_mst_is_enabled(p
->br
))
415 goto defer_stp_filtering
;
418 case BR_STATE_FORWARDING
:
419 case BR_STATE_LEARNING
:
421 if (ether_addr_equal(p
->br
->dev
->dev_addr
, dest
))
422 skb
->pkt_type
= PACKET_HOST
;
424 return nf_hook_bridge_pre(skb
, pskb
);
429 return RX_HANDLER_CONSUMED
;
432 /* This function has no purpose other than to appease the br_port_get_rcu/rtnl
433 * helpers which identify bridged ports according to the rx_handler installed
434 * on them (so there _needs_ to be a bridge rx_handler even if we don't need it
435 * to do anything useful). This bridge won't support traffic to/from the stack,
436 * but only hardware bridging. So return RX_HANDLER_PASS so we don't steal
437 * frames from the ETH_P_XDSA packet_type handler.
439 static rx_handler_result_t
br_handle_frame_dummy(struct sk_buff
**pskb
)
441 return RX_HANDLER_PASS
;
444 rx_handler_func_t
*br_get_rx_handler(const struct net_device
*dev
)
446 if (netdev_uses_dsa(dev
))
447 return br_handle_frame_dummy
;
449 return br_handle_frame
;
452 void br_add_frame(struct net_bridge
*br
, struct br_frame_type
*ft
)
454 hlist_add_head_rcu(&ft
->list
, &br
->frame_type_list
);
457 void br_del_frame(struct net_bridge
*br
, struct br_frame_type
*ft
)
459 struct br_frame_type
*tmp
;
461 hlist_for_each_entry(tmp
, &br
->frame_type_list
, list
)
463 hlist_del_rcu(&ft
->list
);