blob: 8a0c0cc55cb4e59a83cf20d3f9b4de8e46b3a044 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Handle incoming frames
4 * Linux ethernet bridge
5 *
6 * Authors:
7 * Lennert Buytenhek <buytenh@gnu.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/netdevice.h>
13#include <linux/etherdevice.h>
14#include <linux/netfilter_bridge.h>
Stephen Rothwelldc2f4182019-04-13 14:03:36 +100015#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
Florian Westphal971502d2019-04-11 16:36:41 +020016#include <net/netfilter/nf_queue.h>
Stephen Rothwelldc2f4182019-04-13 14:03:36 +100017#endif
Kyeyoon Park95850112014-10-23 14:49:17 -070018#include <linux/neighbour.h>
19#include <net/arp.h>
Vladimir Oltean9eb8eff2020-05-10 19:37:40 +030020#include <net/dsa.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040021#include <linux/export.h>
Vlad Yasevicha37b85c2013-02-13 12:00:10 +000022#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "br_private.h"
Roopa Prabhu11538d02017-01-31 22:59:55 -080024#include "br_private_tunnel.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050026static int
27br_netif_receive_skb(struct net *net, struct sock *sk, struct sk_buff *skb)
Eric W. Biederman04eb4482015-09-15 20:04:15 -050028{
Florian Westphala13b2082017-03-13 17:38:17 +010029 br_drop_fake_rtable(skb);
Eric W. Biederman04eb4482015-09-15 20:04:15 -050030 return netif_receive_skb(skb);
31}
32
Herbert Xu68b7c892010-02-27 19:41:40 +000033static int br_pass_frame_up(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Herbert Xu68b7c892010-02-27 19:41:40 +000035 struct net_device *indev, *brdev = BR_INPUT_SKB_CB(skb)->brdev;
stephen hemminger14bb4782010-03-02 13:32:09 +000036 struct net_bridge *br = netdev_priv(brdev);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020037 struct net_bridge_vlan_group *vg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Heiner Kallweit7609ecb2020-11-20 12:22:23 +010039 dev_sw_netstats_rx_add(brdev, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020041 vg = br_vlan_group_rcu(br);
Vlad Yasevich85f46c62013-02-13 12:00:11 +000042 /* Bridge is just like any other port. Make sure the
Menglong Dongefb5b332021-01-07 18:53:32 -080043 * packet is allowed except in promisc mode when someone
Vlad Yasevich85f46c62013-02-13 12:00:11 +000044 * may be running packet capture.
45 */
46 if (!(brdev->flags & IFF_PROMISC) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020047 !br_allowed_egress(vg, skb)) {
Vlad Yasevich85f46c62013-02-13 12:00:11 +000048 kfree_skb(skb);
49 return NET_RX_DROP;
50 }
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 indev = skb->dev;
Pavel Emelyanova339f1c2008-05-21 14:13:47 -070053 skb->dev = brdev;
Roopa Prabhu11538d02017-01-31 22:59:55 -080054 skb = br_handle_vlan(br, NULL, vg, skb);
Vlad Yasevichfc92f742014-03-27 21:51:18 -040055 if (!skb)
56 return NET_RX_DROP;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +020057 /* update the multicast stats if the packet is IGMP/MLD */
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -070058 br_multicast_count(br, NULL, skb, br_multicast_igmp_type(skb),
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +020059 BR_MCAST_DIR_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Eric W. Biederman29a26a52015-09-15 20:04:16 -050061 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
62 dev_net(indev), NULL, skb, indev, NULL,
Eric W. Biederman04eb4482015-09-15 20:04:15 -050063 br_netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
stephen hemmingereeaf61d2010-07-27 08:26:30 +000066/* note: already called with rcu_read_lock */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050067int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Jiri Pirkof350a0a82010-06-15 06:50:45 +000069 struct net_bridge_port *p = br_port_get_rcu(skb->dev);
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +020070 enum br_pkt_type pkt_type = BR_PKT_UNICAST;
Nikolay Aleksandrov46c07722016-07-14 06:09:59 +030071 struct net_bridge_fdb_entry *dst = NULL;
Nikolay Aleksandrovadc47032021-07-19 20:06:25 +030072 struct net_bridge_mcast_port *pmctx;
Herbert Xuc4fcb782010-02-27 19:41:48 +000073 struct net_bridge_mdb_entry *mdst;
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +020074 bool local_rcv, mcast_hit = false;
Nikolay Aleksandrovadc47032021-07-19 20:06:25 +030075 struct net_bridge_mcast *brmctx;
Nikolay Aleksandrovf4b70022021-07-19 20:06:28 +030076 struct net_bridge_vlan *vlan;
Nikolay Aleksandrov46c07722016-07-14 06:09:59 +030077 struct net_bridge *br;
Vlad Yasevich78851982013-02-13 12:00:14 +000078 u16 vid = 0;
Nikolay Aleksandrova580c762020-01-24 13:40:22 +020079 u8 state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Stephen Hemmingerb3f1be42006-02-09 17:08:52 -080081 if (!p || p->state == BR_STATE_DISABLED)
82 goto drop;
Stephen Hemminger85967bb2005-05-29 14:15:55 -070083
Nikolay Aleksandrovadc47032021-07-19 20:06:25 +030084 brmctx = &p->br->multicast_ctx;
85 pmctx = &p->multicast_ctx;
Nikolay Aleksandrova580c762020-01-24 13:40:22 +020086 state = p->state;
87 if (!br_allowed_ingress(p->br, nbp_vlan_group_rcu(p), skb, &vid,
Nikolay Aleksandrovf4b70022021-07-19 20:06:28 +030088 &state, &vlan))
Toshiaki Makitaeb707612014-04-09 17:00:30 +090089 goto out;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +000090
Ido Schimmel6bc506b2016-08-25 18:42:37 +020091 nbp_switchdev_frame_mark(p, skb);
92
Stephen Hemmingerb3f1be42006-02-09 17:08:52 -080093 /* insert into forwarding database after filtering to avoid spoofing */
94 br = p->br;
Vlad Yasevich9ba18892013-06-05 10:08:00 -040095 if (p->flags & BR_LEARNING)
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +020096 br_fdb_update(br, p, eth_hdr(skb)->h_source, vid, 0);
Stephen Hemmingerb3f1be42006-02-09 17:08:52 -080097
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +020098 local_rcv = !!(br->dev->flags & IFF_PROMISC);
Nikolay Aleksandrov3d26eb82019-07-02 15:00:20 +030099 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) {
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200100 /* by definition the broadcast is also a multicast address */
Nikolay Aleksandrov3d26eb82019-07-02 15:00:20 +0300101 if (is_broadcast_ether_addr(eth_hdr(skb)->h_dest)) {
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200102 pkt_type = BR_PKT_BROADCAST;
103 local_rcv = true;
104 } else {
105 pkt_type = BR_PKT_MULTICAST;
Nikolay Aleksandrovf4b70022021-07-19 20:06:28 +0300106 if (br_multicast_rcv(&brmctx, &pmctx, vlan, skb, vid))
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200107 goto drop;
108 }
109 }
Herbert Xuc4fcb782010-02-27 19:41:48 +0000110
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200111 if (state == BR_STATE_LEARNING)
Stephen Hemmingerb3f1be42006-02-09 17:08:52 -0800112 goto drop;
Stephen Hemminger0e5eaba2005-12-21 19:00:18 -0800113
Herbert Xu68b7c892010-02-27 19:41:40 +0000114 BR_INPUT_SKB_CB(skb)->brdev = br->dev;
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300115 BR_INPUT_SKB_CB(skb)->src_port_isolated = !!(p->flags & BR_ISOLATED);
Herbert Xu68b7c892010-02-27 19:41:40 +0000116
Roopa Prabhu057658c2017-10-06 22:12:38 -0700117 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);
Roopa Prabhued842fa2017-10-06 22:12:39 -0700121 } else if (IS_ENABLED(CONFIG_IPV6) &&
122 skb->protocol == htons(ETH_P_IPV6) &&
Nikolay Aleksandrovc69c2cd2018-09-26 17:01:05 +0300123 br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) &&
Roopa Prabhued842fa2017-10-06 22:12:39 -0700124 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;
128
129 msg = br_is_nd_neigh_msg(skb, &_msg);
130 if (msg)
131 br_do_suppress_nd(skb, br, vid, p, msg);
Roopa Prabhu057658c2017-10-06 22:12:38 -0700132 }
Kyeyoon Park95850112014-10-23 14:49:17 -0700133
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200134 switch (pkt_type) {
135 case BR_PKT_MULTICAST:
Nikolay Aleksandrovadc47032021-07-19 20:06:25 +0300136 mdst = br_mdb_get(brmctx, skb, vid);
Linus Lüssingb00589a2013-08-01 01:06:20 +0200137 if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
Nikolay Aleksandrovadc47032021-07-19 20:06:25 +0300138 br_multicast_querier_exists(brmctx, eth_hdr(skb), mdst)) {
Andrew Lunnff0fd342017-11-09 23:10:57 +0100139 if ((mdst && mdst->host_joined) ||
Nikolay Aleksandrovadc47032021-07-19 20:06:25 +0300140 br_multicast_is_router(brmctx, skb)) {
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300141 local_rcv = true;
Nikolay Aleksandrove151aab2016-07-14 06:10:00 +0300142 br->dev->stats.multicast++;
143 }
144 mcast_hit = true;
Nikolay Aleksandrov46c07722016-07-14 06:09:59 +0300145 } else {
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300146 local_rcv = true;
Nikolay Aleksandrove151aab2016-07-14 06:10:00 +0300147 br->dev->stats.multicast++;
Nikolay Aleksandrov46c07722016-07-14 06:09:59 +0300148 }
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200149 break;
150 case BR_PKT_UNICAST:
Nikolay Aleksandrov3d26eb82019-07-02 15:00:20 +0300151 dst = br_fdb_find_rcu(br, eth_hdr(skb)->h_dest, vid);
Gustavo A. R. Silvaecd1c6a2021-03-09 23:41:15 -0600152 break;
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200153 default:
154 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156
Nikolay Aleksandrove151aab2016-07-14 06:10:00 +0300157 if (dst) {
stephen hemmingerca6d4482017-02-07 08:46:46 -0800158 unsigned long now = jiffies;
159
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200160 if (test_bit(BR_FDB_LOCAL, &dst->flags))
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200161 return br_pass_frame_up(skb);
162
stephen hemmingerca6d4482017-02-07 08:46:46 -0800163 if (now != dst->used)
164 dst->used = now;
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300165 br_forward(dst->dst, skb, local_rcv, false);
Nikolay Aleksandrove151aab2016-07-14 06:10:00 +0300166 } else {
167 if (!mcast_hit)
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200168 br_flood(br, skb, pkt_type, local_rcv, false);
Nikolay Aleksandrove151aab2016-07-14 06:10:00 +0300169 else
Nikolay Aleksandrovadc47032021-07-19 20:06:25 +0300170 br_multicast_flood(mdst, skb, brmctx, local_rcv, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300173 if (local_rcv)
174 return br_pass_frame_up(skb);
Herbert Xu87557c12010-02-27 19:41:39 +0000175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176out:
177 return 0;
Stephen Hemmingerb3f1be42006-02-09 17:08:52 -0800178drop:
179 kfree_skb(skb);
180 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +0200182EXPORT_SYMBOL_GPL(br_handle_frame_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Ido Schimmel56fae4042016-06-07 12:06:58 +0300184static void __br_handle_local_finish(struct sk_buff *skb)
Stephen Hemmingercf0f02d2006-03-20 22:59:06 -0800185{
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000186 struct net_bridge_port *p = br_port_get_rcu(skb->dev);
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000187 u16 vid = 0;
Stephen Hemmingercf0f02d2006-03-20 22:59:06 -0800188
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900189 /* check if vlan is allowed, to avoid spoofing */
Nikolay Aleksandrov70e42722018-11-24 04:34:21 +0200190 if ((p->flags & BR_LEARNING) &&
Nikolay Aleksandrov5d1fcaf2019-11-04 11:36:51 +0200191 nbp_state_should_learn(p) &&
Nikolay Aleksandrov70e42722018-11-24 04:34:21 +0200192 !br_opt_get(p->br, BROPT_NO_LL_LEARN) &&
193 br_should_learn(p, skb, &vid))
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +0200194 br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, 0);
Ido Schimmel56fae4042016-06-07 12:06:58 +0300195}
196
197/* note: already called with rcu_read_lock */
198static int br_handle_local_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
199{
Ido Schimmel56fae4042016-06-07 12:06:58 +0300200 __br_handle_local_finish(skb);
Florian Westphal8626c562016-03-12 11:14:42 +0100201
Nikolay Aleksandrov3b2e2902019-04-11 13:56:39 +0300202 /* return 1 to signal the okfn() was called so it's ok to use the skb */
203 return 1;
Stephen Hemmingercf0f02d2006-03-20 22:59:06 -0800204}
205
Florian Westphal971502d2019-04-11 16:36:41 +0200206static int nf_hook_bridge_pre(struct sk_buff *skb, struct sk_buff **pskb)
207{
208#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
209 struct nf_hook_entries *e = NULL;
210 struct nf_hook_state state;
211 unsigned int verdict, i;
212 struct net *net;
213 int ret;
214
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]))
218 goto frame_finish;
219#endif
220
221 e = rcu_dereference(net->nf.hooks_bridge[NF_BR_PRE_ROUTING]);
222 if (!e)
223 goto frame_finish;
224
225 nf_hook_state_init(&state, NF_BR_PRE_ROUTING,
226 NFPROTO_BRIDGE, skb->dev, NULL, NULL,
227 net, br_handle_frame_finish);
228
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) {
232 case NF_ACCEPT:
Florian Westphal223fd0a2019-04-11 16:36:42 +0200233 if (BR_INPUT_SKB_CB(skb)->br_netfilter_broute) {
234 *pskb = skb;
235 return RX_HANDLER_PASS;
236 }
Florian Westphal971502d2019-04-11 16:36:41 +0200237 break;
238 case NF_DROP:
239 kfree_skb(skb);
240 return RX_HANDLER_CONSUMED;
241 case NF_QUEUE:
Florian Westphal0d9cb302019-07-02 20:41:14 +0200242 ret = nf_queue(skb, &state, i, verdict);
Florian Westphal971502d2019-04-11 16:36:41 +0200243 if (ret == 1)
244 continue;
245 return RX_HANDLER_CONSUMED;
246 default: /* STOLEN */
247 return RX_HANDLER_CONSUMED;
248 }
249 }
250frame_finish:
251 net = dev_net(skb->dev);
252 br_handle_frame_finish(net, NULL, skb);
253#else
254 br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
255#endif
256 return RX_HANDLER_CONSUMED;
257}
258
Henrik Bjoernlund90c628d2020-10-27 10:02:42 +0000259/* Return 0 if the frame was not processed otherwise 1
260 * note: already called with rcu_read_lock
261 */
262static int br_process_frame_type(struct net_bridge_port *p,
263 struct sk_buff *skb)
264{
265 struct br_frame_type *tmp;
266
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);
270
271 return 0;
272}
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/*
Stephen Hemminger6229e362007-03-21 13:38:47 -0700275 * Return NULL if skb is handled
stephen hemmingereeaf61d2010-07-27 08:26:30 +0000276 * note: already called with rcu_read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
Vladimir Oltean9eb8eff2020-05-10 19:37:40 +0300278static rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000280 struct net_bridge_port *p;
Jiri Pirko8a4eb572011-03-12 03:14:39 +0000281 struct sk_buff *skb = *pskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 const unsigned char *dest = eth_hdr(skb)->h_dest;
283
Simon Hormanc2368e72010-08-22 17:35:32 +0000284 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
Jiri Pirko8a4eb572011-03-12 03:14:39 +0000285 return RX_HANDLER_PASS;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
Stephen Hemminger467aea02007-03-21 13:42:06 -0700288 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Herbert Xu7b995652007-10-14 00:39:01 -0700290 skb = skb_share_check(skb, GFP_ATOMIC);
291 if (!skb)
Jiri Pirko8a4eb572011-03-12 03:14:39 +0000292 return RX_HANDLER_CONSUMED;
Herbert Xu7b995652007-10-14 00:39:01 -0700293
Florian Westphalf12064d2019-04-11 16:36:40 +0200294 memset(skb->cb, 0, sizeof(struct br_input_skb_cb));
295
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000296 p = br_port_get_rcu(skb->dev);
Roopa Prabhu11538d02017-01-31 22:59:55 -0800297 if (p->flags & BR_VLAN_TUNNEL) {
298 if (br_handle_ingress_vlan_tunnel(skb, p,
299 nbp_vlan_group_rcu(p)))
300 goto drop;
301 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000302
Ben Hutchings46acc462012-11-01 09:11:11 +0000303 if (unlikely(is_link_local_ether_addr(dest))) {
Toshiaki Makitaf2808d22014-06-10 20:59:24 +0900304 u16 fwd_mask = p->br->group_fwd_mask_required;
305
stephen hemminger515853c2011-10-03 18:14:46 +0000306 /*
307 * See IEEE 802.1D Table 7-10 Reserved addresses
308 *
309 * Assignment Value
310 * Bridge Group Address 01-80-C2-00-00-00
311 * (MAC Control) 802.3 01-80-C2-00-00-01
312 * (Link Aggregation) 802.3 01-80-C2-00-00-02
313 * 802.1X PAE address 01-80-C2-00-00-03
314 *
315 * 802.1AB LLDP 01-80-C2-00-00-0E
316 *
317 * Others reserved for future standardization
318 */
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300319 fwd_mask |= p->group_fwd_mask;
stephen hemminger515853c2011-10-03 18:14:46 +0000320 switch (dest[5]) {
321 case 0x00: /* Bridge Group Address */
322 /* If STP is turned off,
323 then must forward to keep loop detection */
Toshiaki Makitaf2808d22014-06-10 20:59:24 +0900324 if (p->br->stp_enabled == BR_NO_STP ||
325 fwd_mask & (1u << dest[5]))
stephen hemminger515853c2011-10-03 18:14:46 +0000326 goto forward;
Ido Schimmel56fae4042016-06-07 12:06:58 +0300327 *pskb = skb;
328 __br_handle_local_finish(skb);
329 return RX_HANDLER_PASS;
stephen hemminger515853c2011-10-03 18:14:46 +0000330
331 case 0x01: /* IEEE MAC (Pause) */
Stephen Hemminger2111f8b2007-04-25 22:05:55 -0700332 goto drop;
333
Ido Schimmelbaedbe52016-07-22 14:56:20 +0300334 case 0x0E: /* 802.1AB LLDP */
335 fwd_mask |= p->br->group_fwd_mask;
336 if (fwd_mask & (1u << dest[5]))
337 goto forward;
338 *pskb = skb;
339 __br_handle_local_finish(skb);
340 return RX_HANDLER_PASS;
341
stephen hemminger515853c2011-10-03 18:14:46 +0000342 default:
343 /* Allow selective forwarding for most other protocols */
Toshiaki Makitaf2808d22014-06-10 20:59:24 +0900344 fwd_mask |= p->br->group_fwd_mask;
345 if (fwd_mask & (1u << dest[5]))
stephen hemminger515853c2011-10-03 18:14:46 +0000346 goto forward;
347 }
Stephen Hemmingera598f6a2009-05-15 06:10:13 +0000348
Nikolay Aleksandrov3b2e2902019-04-11 13:56:39 +0300349 /* The else clause should be hit when nf_hook():
350 * - returns < 0 (drop/error)
351 * - returns = 0 (stolen/nf_queue)
352 * Thus return 1 from the okfn() to signal the skb is ok to pass
353 */
354 if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
355 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
356 br_handle_local_finish) == 1) {
357 return RX_HANDLER_PASS;
358 } else {
359 return RX_HANDLER_CONSUMED;
360 }
Stephen Hemminger2111f8b2007-04-25 22:05:55 -0700361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Henrik Bjoernlund90c628d2020-10-27 10:02:42 +0000363 if (unlikely(br_process_frame_type(p, skb)))
Horatiu Vultur65369932020-04-26 15:22:07 +0200364 return RX_HANDLER_PASS;
365
Stephen Hemmingera598f6a2009-05-15 06:10:13 +0000366forward:
Stephen Hemminger467aea02007-03-21 13:42:06 -0700367 switch (p->state) {
368 case BR_STATE_FORWARDING:
Stephen Hemminger467aea02007-03-21 13:42:06 -0700369 case BR_STATE_LEARNING:
Joe Perches9a7b6ef92012-05-08 18:56:49 +0000370 if (ether_addr_equal(p->br->dev->dev_addr, dest))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 skb->pkt_type = PACKET_HOST;
372
Florian Westphal971502d2019-04-11 16:36:41 +0200373 return nf_hook_bridge_pre(skb, pskb);
Stephen Hemminger467aea02007-03-21 13:42:06 -0700374 default:
375drop:
376 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +0000378 return RX_HANDLER_CONSUMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
Vladimir Oltean9eb8eff2020-05-10 19:37:40 +0300380
381/* This function has no purpose other than to appease the br_port_get_rcu/rtnl
382 * helpers which identify bridged ports according to the rx_handler installed
383 * on them (so there _needs_ to be a bridge rx_handler even if we don't need it
384 * to do anything useful). This bridge won't support traffic to/from the stack,
385 * but only hardware bridging. So return RX_HANDLER_PASS so we don't steal
386 * frames from the ETH_P_XDSA packet_type handler.
387 */
388static rx_handler_result_t br_handle_frame_dummy(struct sk_buff **pskb)
389{
390 return RX_HANDLER_PASS;
391}
392
393rx_handler_func_t *br_get_rx_handler(const struct net_device *dev)
394{
395 if (netdev_uses_dsa(dev))
396 return br_handle_frame_dummy;
397
398 return br_handle_frame;
399}
Henrik Bjoernlund90c628d2020-10-27 10:02:42 +0000400
401void br_add_frame(struct net_bridge *br, struct br_frame_type *ft)
402{
403 hlist_add_head_rcu(&ft->list, &br->frame_type_list);
404}
405
406void br_del_frame(struct net_bridge *br, struct br_frame_type *ft)
407{
408 struct br_frame_type *tmp;
409
410 hlist_for_each_entry(tmp, &br->frame_type_list, list)
411 if (ft == tmp) {
412 hlist_del_rcu(&ft->list);
413 return;
414 }
415}