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)
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);
42 /* Bridge is just like any other port. Make sure the
43 * packet is allowed except in promisc mode when someone
44 * may be running packet capture.
46 if (!(brdev->flags & IFF_PROMISC) &&
47 !br_allowed_egress(vg, skb)) {
54 skb = br_handle_vlan(br, NULL, vg, skb);
57 /* update the multicast stats if the packet is IGMP/MLD */
58 br_multicast_count(br, NULL, skb, br_multicast_igmp_type(skb),
61 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
62 dev_net(indev), NULL, skb, indev, NULL,
63 br_netif_receive_skb);
66 /* note: already called with rcu_read_lock */
67 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
69 struct net_bridge_port *p = br_port_get_rcu(skb->dev);
70 enum br_pkt_type pkt_type = BR_PKT_UNICAST;
71 struct net_bridge_fdb_entry *dst = NULL;
72 struct net_bridge_mcast_port *pmctx;
73 struct net_bridge_mdb_entry *mdst;
74 bool local_rcv, mcast_hit = false;
75 struct net_bridge_mcast *brmctx;
76 struct net_bridge_vlan *vlan;
77 struct net_bridge *br;
81 if (!p || p->state == BR_STATE_DISABLED)
84 brmctx = &p->br->multicast_ctx;
85 pmctx = &p->multicast_ctx;
87 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, 0);
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(&brmctx, &pmctx, vlan, skb, vid))
111 if (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_opt_get(br, BROPT_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(brmctx, skb, vid);
137 if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
138 br_multicast_querier_exists(brmctx, eth_hdr(skb), mdst)) {
139 if ((mdst && mdst->host_joined) ||
140 br_multicast_is_router(brmctx, skb)) {
142 br->dev->stats.multicast++;
147 br->dev->stats.multicast++;
151 dst = br_fdb_find_rcu(br, eth_hdr(skb)->h_dest, vid);
158 unsigned long now = jiffies;
160 if (test_bit(BR_FDB_LOCAL, &dst->flags))
161 return br_pass_frame_up(skb);
163 if (now != dst->used)
165 br_forward(dst->dst, skb, local_rcv, false);
168 br_flood(br, skb, pkt_type, local_rcv, false);
170 br_multicast_flood(mdst, skb, brmctx, local_rcv, false);
174 return br_pass_frame_up(skb);
182 EXPORT_SYMBOL_GPL(br_handle_frame_finish);
184 static void __br_handle_local_finish(struct sk_buff *skb)
186 struct net_bridge_port *p = br_port_get_rcu(skb->dev);
189 /* check if vlan is allowed, to avoid spoofing */
190 if ((p->flags & BR_LEARNING) &&
191 nbp_state_should_learn(p) &&
192 !br_opt_get(p->br, BROPT_NO_LL_LEARN) &&
193 br_should_learn(p, skb, &vid))
194 br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, 0);
197 /* note: already called with rcu_read_lock */
198 static int br_handle_local_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
200 __br_handle_local_finish(skb);
202 /* return 1 to signal the okfn() was called so it's ok to use the skb */
206 static int nf_hook_bridge_pre(struct sk_buff *skb, struct sk_buff **pskb)
208 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
209 struct nf_hook_entries *e = NULL;
210 struct nf_hook_state state;
211 unsigned int verdict, i;
215 net = dev_net(skb->dev);
216 #ifdef HAVE_JUMP_LABEL
217 if (!static_key_false(&nf_hooks_needed[NFPROTO_BRIDGE][NF_BR_PRE_ROUTING]))
221 e = rcu_dereference(net->nf.hooks_bridge[NF_BR_PRE_ROUTING]);
225 nf_hook_state_init(&state, NF_BR_PRE_ROUTING,
226 NFPROTO_BRIDGE, skb->dev, NULL, NULL,
227 net, br_handle_frame_finish);
229 for (i = 0; i < e->num_hook_entries; i++) {
230 verdict = nf_hook_entry_hookfn(&e->hooks[i], skb, &state);
231 switch (verdict & NF_VERDICT_MASK) {
233 if (BR_INPUT_SKB_CB(skb)->br_netfilter_broute) {
235 return RX_HANDLER_PASS;
240 return RX_HANDLER_CONSUMED;
242 ret = nf_queue(skb, &state, i, verdict);
245 return RX_HANDLER_CONSUMED;
246 default: /* STOLEN */
247 return RX_HANDLER_CONSUMED;
251 net = dev_net(skb->dev);
252 br_handle_frame_finish(net, NULL, skb);
254 br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
256 return RX_HANDLER_CONSUMED;
259 /* Return 0 if the frame was not processed otherwise 1
260 * note: already called with rcu_read_lock
262 static int br_process_frame_type(struct net_bridge_port *p,
265 struct br_frame_type *tmp;
267 hlist_for_each_entry_rcu(tmp, &p->br->frame_type_list, list)
268 if (unlikely(tmp->type == skb->protocol))
269 return tmp->frame_handler(p, skb);
275 * Return NULL if skb is handled
276 * note: already called with rcu_read_lock
278 static rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
280 struct net_bridge_port *p;
281 struct sk_buff *skb = *pskb;
282 const unsigned char *dest = eth_hdr(skb)->h_dest;
284 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
285 return RX_HANDLER_PASS;
287 if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
290 skb = skb_share_check(skb, GFP_ATOMIC);
292 return RX_HANDLER_CONSUMED;
294 memset(skb->cb, 0, sizeof(struct br_input_skb_cb));
296 p = br_port_get_rcu(skb->dev);
297 if (p->flags & BR_VLAN_TUNNEL)
298 br_handle_ingress_vlan_tunnel(skb, p, nbp_vlan_group_rcu(p));
300 if (unlikely(is_link_local_ether_addr(dest))) {
301 u16 fwd_mask = p->br->group_fwd_mask_required;
304 * See IEEE 802.1D Table 7-10 Reserved addresses
307 * Bridge Group Address 01-80-C2-00-00-00
308 * (MAC Control) 802.3 01-80-C2-00-00-01
309 * (Link Aggregation) 802.3 01-80-C2-00-00-02
310 * 802.1X PAE address 01-80-C2-00-00-03
312 * 802.1AB LLDP 01-80-C2-00-00-0E
314 * Others reserved for future standardization
316 fwd_mask |= p->group_fwd_mask;
318 case 0x00: /* Bridge Group Address */
319 /* If STP is turned off,
320 then must forward to keep loop detection */
321 if (p->br->stp_enabled == BR_NO_STP ||
322 fwd_mask & (1u << dest[5]))
325 __br_handle_local_finish(skb);
326 return RX_HANDLER_PASS;
328 case 0x01: /* IEEE MAC (Pause) */
331 case 0x0E: /* 802.1AB LLDP */
332 fwd_mask |= p->br->group_fwd_mask;
333 if (fwd_mask & (1u << dest[5]))
336 __br_handle_local_finish(skb);
337 return RX_HANDLER_PASS;
340 /* Allow selective forwarding for most other protocols */
341 fwd_mask |= p->br->group_fwd_mask;
342 if (fwd_mask & (1u << dest[5]))
346 /* The else clause should be hit when nf_hook():
347 * - returns < 0 (drop/error)
348 * - returns = 0 (stolen/nf_queue)
349 * Thus return 1 from the okfn() to signal the skb is ok to pass
351 if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
352 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
353 br_handle_local_finish) == 1) {
354 return RX_HANDLER_PASS;
356 return RX_HANDLER_CONSUMED;
360 if (unlikely(br_process_frame_type(p, skb)))
361 return RX_HANDLER_PASS;
365 case BR_STATE_FORWARDING:
366 case BR_STATE_LEARNING:
367 if (ether_addr_equal(p->br->dev->dev_addr, dest))
368 skb->pkt_type = PACKET_HOST;
370 return nf_hook_bridge_pre(skb, pskb);
375 return RX_HANDLER_CONSUMED;
378 /* This function has no purpose other than to appease the br_port_get_rcu/rtnl
379 * helpers which identify bridged ports according to the rx_handler installed
380 * on them (so there _needs_ to be a bridge rx_handler even if we don't need it
381 * to do anything useful). This bridge won't support traffic to/from the stack,
382 * but only hardware bridging. So return RX_HANDLER_PASS so we don't steal
383 * frames from the ETH_P_XDSA packet_type handler.
385 static rx_handler_result_t br_handle_frame_dummy(struct sk_buff **pskb)
387 return RX_HANDLER_PASS;
390 rx_handler_func_t *br_get_rx_handler(const struct net_device *dev)
392 if (netdev_uses_dsa(dev))
393 return br_handle_frame_dummy;
395 return br_handle_frame;
398 void br_add_frame(struct net_bridge *br, struct br_frame_type *ft)
400 hlist_add_head_rcu(&ft->list, &br->frame_type_list);
403 void br_del_frame(struct net_bridge *br, struct br_frame_type *ft)
405 struct br_frame_type *tmp;
407 hlist_for_each_entry(tmp, &br->frame_type_list, list)
409 hlist_del_rcu(&ft->list);