blob: bece03bf83c49fe9355f9972a195c49da8e934eb [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;
Li RongQing8f849852014-01-04 13:57:59 +080038 struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Eric Dumazet406818f2010-06-23 13:00:48 -070040 u64_stats_update_begin(&brstats->syncp);
stephen hemminger14bb4782010-03-02 13:32:09 +000041 brstats->rx_packets++;
42 brstats->rx_bytes += skb->len;
Eric Dumazet406818f2010-06-23 13:00:48 -070043 u64_stats_update_end(&brstats->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020045 vg = br_vlan_group_rcu(br);
Vlad Yasevich85f46c62013-02-13 12:00:11 +000046 /* Bridge is just like any other port. Make sure the
47 * packet is allowed except in promisc modue when someone
48 * may be running packet capture.
49 */
50 if (!(brdev->flags & IFF_PROMISC) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020051 !br_allowed_egress(vg, skb)) {
Vlad Yasevich85f46c62013-02-13 12:00:11 +000052 kfree_skb(skb);
53 return NET_RX_DROP;
54 }
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 indev = skb->dev;
Pavel Emelyanova339f1c2008-05-21 14:13:47 -070057 skb->dev = brdev;
Roopa Prabhu11538d02017-01-31 22:59:55 -080058 skb = br_handle_vlan(br, NULL, vg, skb);
Vlad Yasevichfc92f742014-03-27 21:51:18 -040059 if (!skb)
60 return NET_RX_DROP;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +020061 /* update the multicast stats if the packet is IGMP/MLD */
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -070062 br_multicast_count(br, NULL, skb, br_multicast_igmp_type(skb),
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +020063 BR_MCAST_DIR_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Eric W. Biederman29a26a52015-09-15 20:04:16 -050065 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
66 dev_net(indev), NULL, skb, indev, NULL,
Eric W. Biederman04eb4482015-09-15 20:04:15 -050067 br_netif_receive_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
stephen hemmingereeaf61d2010-07-27 08:26:30 +000070/* note: already called with rcu_read_lock */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050071int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Jiri Pirkof350a0a82010-06-15 06:50:45 +000073 struct net_bridge_port *p = br_port_get_rcu(skb->dev);
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +020074 enum br_pkt_type pkt_type = BR_PKT_UNICAST;
Nikolay Aleksandrov46c07722016-07-14 06:09:59 +030075 struct net_bridge_fdb_entry *dst = NULL;
Herbert Xuc4fcb782010-02-27 19:41:48 +000076 struct net_bridge_mdb_entry *mdst;
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +020077 bool local_rcv, mcast_hit = false;
Nikolay Aleksandrov46c07722016-07-14 06:09:59 +030078 struct net_bridge *br;
Vlad Yasevich78851982013-02-13 12:00:14 +000079 u16 vid = 0;
Nikolay Aleksandrova580c762020-01-24 13:40:22 +020080 u8 state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Stephen Hemmingerb3f1be42006-02-09 17:08:52 -080082 if (!p || p->state == BR_STATE_DISABLED)
83 goto drop;
Stephen Hemminger85967bb2005-05-29 14:15:55 -070084
Nikolay Aleksandrova580c762020-01-24 13:40:22 +020085 state = p->state;
86 if (!br_allowed_ingress(p->br, nbp_vlan_group_rcu(p), skb, &vid,
87 &state))
Toshiaki Makitaeb707612014-04-09 17:00:30 +090088 goto out;
Vlad Yasevicha37b85c2013-02-13 12:00:10 +000089
Ido Schimmel6bc506b2016-08-25 18:42:37 +020090 nbp_switchdev_frame_mark(p, skb);
91
Stephen Hemmingerb3f1be42006-02-09 17:08:52 -080092 /* insert into forwarding database after filtering to avoid spoofing */
93 br = p->br;
Vlad Yasevich9ba18892013-06-05 10:08:00 -040094 if (p->flags & BR_LEARNING)
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +020095 br_fdb_update(br, p, eth_hdr(skb)->h_source, vid, 0);
Stephen Hemmingerb3f1be42006-02-09 17:08:52 -080096
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +020097 local_rcv = !!(br->dev->flags & IFF_PROMISC);
Nikolay Aleksandrov3d26eb82019-07-02 15:00:20 +030098 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) {
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +020099 /* by definition the broadcast is also a multicast address */
Nikolay Aleksandrov3d26eb82019-07-02 15:00:20 +0300100 if (is_broadcast_ether_addr(eth_hdr(skb)->h_dest)) {
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200101 pkt_type = BR_PKT_BROADCAST;
102 local_rcv = true;
103 } else {
104 pkt_type = BR_PKT_MULTICAST;
105 if (br_multicast_rcv(br, p, skb, vid))
106 goto drop;
107 }
108 }
Herbert Xuc4fcb782010-02-27 19:41:48 +0000109
Nikolay Aleksandrova580c762020-01-24 13:40:22 +0200110 if (state == BR_STATE_LEARNING)
Stephen Hemmingerb3f1be42006-02-09 17:08:52 -0800111 goto drop;
Stephen Hemminger0e5eaba2005-12-21 19:00:18 -0800112
Herbert Xu68b7c892010-02-27 19:41:40 +0000113 BR_INPUT_SKB_CB(skb)->brdev = br->dev;
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300114 BR_INPUT_SKB_CB(skb)->src_port_isolated = !!(p->flags & BR_ISOLATED);
Herbert Xu68b7c892010-02-27 19:41:40 +0000115
Roopa Prabhu057658c2017-10-06 22:12:38 -0700116 if (IS_ENABLED(CONFIG_INET) &&
117 (skb->protocol == htons(ETH_P_ARP) ||
118 skb->protocol == htons(ETH_P_RARP))) {
119 br_do_proxy_suppress_arp(skb, br, vid, p);
Roopa Prabhued842fa2017-10-06 22:12:39 -0700120 } else if (IS_ENABLED(CONFIG_IPV6) &&
121 skb->protocol == htons(ETH_P_IPV6) &&
Nikolay Aleksandrovc69c2cd2018-09-26 17:01:05 +0300122 br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) &&
Roopa Prabhued842fa2017-10-06 22:12:39 -0700123 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
124 sizeof(struct nd_msg)) &&
125 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
126 struct nd_msg *msg, _msg;
127
128 msg = br_is_nd_neigh_msg(skb, &_msg);
129 if (msg)
130 br_do_suppress_nd(skb, br, vid, p, msg);
Roopa Prabhu057658c2017-10-06 22:12:38 -0700131 }
Kyeyoon Park95850112014-10-23 14:49:17 -0700132
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200133 switch (pkt_type) {
134 case BR_PKT_MULTICAST:
Cong Wangfbca58a2013-03-07 03:05:33 +0000135 mdst = br_mdb_get(br, skb, vid);
Linus Lüssingb00589a2013-08-01 01:06:20 +0200136 if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200137 br_multicast_querier_exists(br, eth_hdr(skb))) {
Andrew Lunnff0fd342017-11-09 23:10:57 +0100138 if ((mdst && mdst->host_joined) ||
Nikolay Aleksandrove151aab2016-07-14 06:10:00 +0300139 br_multicast_is_router(br)) {
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300140 local_rcv = true;
Nikolay Aleksandrove151aab2016-07-14 06:10:00 +0300141 br->dev->stats.multicast++;
142 }
143 mcast_hit = true;
Nikolay Aleksandrov46c07722016-07-14 06:09:59 +0300144 } else {
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300145 local_rcv = true;
Nikolay Aleksandrove151aab2016-07-14 06:10:00 +0300146 br->dev->stats.multicast++;
Nikolay Aleksandrov46c07722016-07-14 06:09:59 +0300147 }
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200148 break;
149 case BR_PKT_UNICAST:
Nikolay Aleksandrov3d26eb82019-07-02 15:00:20 +0300150 dst = br_fdb_find_rcu(br, eth_hdr(skb)->h_dest, vid);
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200151 default:
152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
Nikolay Aleksandrove151aab2016-07-14 06:10:00 +0300155 if (dst) {
stephen hemmingerca6d4482017-02-07 08:46:46 -0800156 unsigned long now = jiffies;
157
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200158 if (test_bit(BR_FDB_LOCAL, &dst->flags))
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200159 return br_pass_frame_up(skb);
160
stephen hemmingerca6d4482017-02-07 08:46:46 -0800161 if (now != dst->used)
162 dst->used = now;
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300163 br_forward(dst->dst, skb, local_rcv, false);
Nikolay Aleksandrove151aab2016-07-14 06:10:00 +0300164 } else {
165 if (!mcast_hit)
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200166 br_flood(br, skb, pkt_type, local_rcv, false);
Nikolay Aleksandrove151aab2016-07-14 06:10:00 +0300167 else
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300168 br_multicast_flood(mdst, skb, local_rcv, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300171 if (local_rcv)
172 return br_pass_frame_up(skb);
Herbert Xu87557c12010-02-27 19:41:39 +0000173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174out:
175 return 0;
Stephen Hemmingerb3f1be42006-02-09 17:08:52 -0800176drop:
177 kfree_skb(skb);
178 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +0200180EXPORT_SYMBOL_GPL(br_handle_frame_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Ido Schimmel56fae4042016-06-07 12:06:58 +0300182static void __br_handle_local_finish(struct sk_buff *skb)
Stephen Hemmingercf0f02d2006-03-20 22:59:06 -0800183{
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000184 struct net_bridge_port *p = br_port_get_rcu(skb->dev);
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000185 u16 vid = 0;
Stephen Hemmingercf0f02d2006-03-20 22:59:06 -0800186
Toshiaki Makitae0d79682014-05-26 15:15:53 +0900187 /* check if vlan is allowed, to avoid spoofing */
Nikolay Aleksandrov70e42722018-11-24 04:34:21 +0200188 if ((p->flags & BR_LEARNING) &&
Nikolay Aleksandrov5d1fcaf2019-11-04 11:36:51 +0200189 nbp_state_should_learn(p) &&
Nikolay Aleksandrov70e42722018-11-24 04:34:21 +0200190 !br_opt_get(p->br, BROPT_NO_LL_LEARN) &&
191 br_should_learn(p, skb, &vid))
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +0200192 br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, 0);
Ido Schimmel56fae4042016-06-07 12:06:58 +0300193}
194
195/* note: already called with rcu_read_lock */
196static int br_handle_local_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
197{
Ido Schimmel56fae4042016-06-07 12:06:58 +0300198 __br_handle_local_finish(skb);
Florian Westphal8626c562016-03-12 11:14:42 +0100199
Nikolay Aleksandrov3b2e2902019-04-11 13:56:39 +0300200 /* return 1 to signal the okfn() was called so it's ok to use the skb */
201 return 1;
Stephen Hemmingercf0f02d2006-03-20 22:59:06 -0800202}
203
Florian Westphal971502d2019-04-11 16:36:41 +0200204static int nf_hook_bridge_pre(struct sk_buff *skb, struct sk_buff **pskb)
205{
206#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
207 struct nf_hook_entries *e = NULL;
208 struct nf_hook_state state;
209 unsigned int verdict, i;
210 struct net *net;
211 int ret;
212
213 net = dev_net(skb->dev);
214#ifdef HAVE_JUMP_LABEL
215 if (!static_key_false(&nf_hooks_needed[NFPROTO_BRIDGE][NF_BR_PRE_ROUTING]))
216 goto frame_finish;
217#endif
218
219 e = rcu_dereference(net->nf.hooks_bridge[NF_BR_PRE_ROUTING]);
220 if (!e)
221 goto frame_finish;
222
223 nf_hook_state_init(&state, NF_BR_PRE_ROUTING,
224 NFPROTO_BRIDGE, skb->dev, NULL, NULL,
225 net, br_handle_frame_finish);
226
227 for (i = 0; i < e->num_hook_entries; i++) {
228 verdict = nf_hook_entry_hookfn(&e->hooks[i], skb, &state);
229 switch (verdict & NF_VERDICT_MASK) {
230 case NF_ACCEPT:
Florian Westphal223fd0a2019-04-11 16:36:42 +0200231 if (BR_INPUT_SKB_CB(skb)->br_netfilter_broute) {
232 *pskb = skb;
233 return RX_HANDLER_PASS;
234 }
Florian Westphal971502d2019-04-11 16:36:41 +0200235 break;
236 case NF_DROP:
237 kfree_skb(skb);
238 return RX_HANDLER_CONSUMED;
239 case NF_QUEUE:
Florian Westphal0d9cb302019-07-02 20:41:14 +0200240 ret = nf_queue(skb, &state, i, verdict);
Florian Westphal971502d2019-04-11 16:36:41 +0200241 if (ret == 1)
242 continue;
243 return RX_HANDLER_CONSUMED;
244 default: /* STOLEN */
245 return RX_HANDLER_CONSUMED;
246 }
247 }
248frame_finish:
249 net = dev_net(skb->dev);
250 br_handle_frame_finish(net, NULL, skb);
251#else
252 br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
253#endif
254 return RX_HANDLER_CONSUMED;
255}
256
Henrik Bjoernlund90c628d2020-10-27 10:02:42 +0000257/* Return 0 if the frame was not processed otherwise 1
258 * note: already called with rcu_read_lock
259 */
260static int br_process_frame_type(struct net_bridge_port *p,
261 struct sk_buff *skb)
262{
263 struct br_frame_type *tmp;
264
265 hlist_for_each_entry_rcu(tmp, &p->br->frame_type_list, list)
266 if (unlikely(tmp->type == skb->protocol))
267 return tmp->frame_handler(p, skb);
268
269 return 0;
270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/*
Stephen Hemminger6229e362007-03-21 13:38:47 -0700273 * Return NULL if skb is handled
stephen hemmingereeaf61d2010-07-27 08:26:30 +0000274 * note: already called with rcu_read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 */
Vladimir Oltean9eb8eff2020-05-10 19:37:40 +0300276static rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000278 struct net_bridge_port *p;
Jiri Pirko8a4eb572011-03-12 03:14:39 +0000279 struct sk_buff *skb = *pskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 const unsigned char *dest = eth_hdr(skb)->h_dest;
281
Simon Hormanc2368e72010-08-22 17:35:32 +0000282 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
Jiri Pirko8a4eb572011-03-12 03:14:39 +0000283 return RX_HANDLER_PASS;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
Stephen Hemminger467aea02007-03-21 13:42:06 -0700286 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Herbert Xu7b995652007-10-14 00:39:01 -0700288 skb = skb_share_check(skb, GFP_ATOMIC);
289 if (!skb)
Jiri Pirko8a4eb572011-03-12 03:14:39 +0000290 return RX_HANDLER_CONSUMED;
Herbert Xu7b995652007-10-14 00:39:01 -0700291
Florian Westphalf12064d2019-04-11 16:36:40 +0200292 memset(skb->cb, 0, sizeof(struct br_input_skb_cb));
293
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000294 p = br_port_get_rcu(skb->dev);
Roopa Prabhu11538d02017-01-31 22:59:55 -0800295 if (p->flags & BR_VLAN_TUNNEL) {
296 if (br_handle_ingress_vlan_tunnel(skb, p,
297 nbp_vlan_group_rcu(p)))
298 goto drop;
299 }
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000300
Ben Hutchings46acc462012-11-01 09:11:11 +0000301 if (unlikely(is_link_local_ether_addr(dest))) {
Toshiaki Makitaf2808d22014-06-10 20:59:24 +0900302 u16 fwd_mask = p->br->group_fwd_mask_required;
303
stephen hemminger515853c2011-10-03 18:14:46 +0000304 /*
305 * See IEEE 802.1D Table 7-10 Reserved addresses
306 *
307 * Assignment Value
308 * Bridge Group Address 01-80-C2-00-00-00
309 * (MAC Control) 802.3 01-80-C2-00-00-01
310 * (Link Aggregation) 802.3 01-80-C2-00-00-02
311 * 802.1X PAE address 01-80-C2-00-00-03
312 *
313 * 802.1AB LLDP 01-80-C2-00-00-0E
314 *
315 * Others reserved for future standardization
316 */
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300317 fwd_mask |= p->group_fwd_mask;
stephen hemminger515853c2011-10-03 18:14:46 +0000318 switch (dest[5]) {
319 case 0x00: /* Bridge Group Address */
320 /* If STP is turned off,
321 then must forward to keep loop detection */
Toshiaki Makitaf2808d22014-06-10 20:59:24 +0900322 if (p->br->stp_enabled == BR_NO_STP ||
323 fwd_mask & (1u << dest[5]))
stephen hemminger515853c2011-10-03 18:14:46 +0000324 goto forward;
Ido Schimmel56fae4042016-06-07 12:06:58 +0300325 *pskb = skb;
326 __br_handle_local_finish(skb);
327 return RX_HANDLER_PASS;
stephen hemminger515853c2011-10-03 18:14:46 +0000328
329 case 0x01: /* IEEE MAC (Pause) */
Stephen Hemminger2111f8b2007-04-25 22:05:55 -0700330 goto drop;
331
Ido Schimmelbaedbe52016-07-22 14:56:20 +0300332 case 0x0E: /* 802.1AB LLDP */
333 fwd_mask |= p->br->group_fwd_mask;
334 if (fwd_mask & (1u << dest[5]))
335 goto forward;
336 *pskb = skb;
337 __br_handle_local_finish(skb);
338 return RX_HANDLER_PASS;
339
stephen hemminger515853c2011-10-03 18:14:46 +0000340 default:
341 /* Allow selective forwarding for most other protocols */
Toshiaki Makitaf2808d22014-06-10 20:59:24 +0900342 fwd_mask |= p->br->group_fwd_mask;
343 if (fwd_mask & (1u << dest[5]))
stephen hemminger515853c2011-10-03 18:14:46 +0000344 goto forward;
345 }
Stephen Hemmingera598f6a2009-05-15 06:10:13 +0000346
Nikolay Aleksandrov3b2e2902019-04-11 13:56:39 +0300347 /* The else clause should be hit when nf_hook():
348 * - returns < 0 (drop/error)
349 * - returns = 0 (stolen/nf_queue)
350 * Thus return 1 from the okfn() to signal the skb is ok to pass
351 */
352 if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
353 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
354 br_handle_local_finish) == 1) {
355 return RX_HANDLER_PASS;
356 } else {
357 return RX_HANDLER_CONSUMED;
358 }
Stephen Hemminger2111f8b2007-04-25 22:05:55 -0700359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Henrik Bjoernlund90c628d2020-10-27 10:02:42 +0000361 if (unlikely(br_process_frame_type(p, skb)))
Horatiu Vultur65369932020-04-26 15:22:07 +0200362 return RX_HANDLER_PASS;
363
Stephen Hemmingera598f6a2009-05-15 06:10:13 +0000364forward:
Stephen Hemminger467aea02007-03-21 13:42:06 -0700365 switch (p->state) {
366 case BR_STATE_FORWARDING:
Stephen Hemminger467aea02007-03-21 13:42:06 -0700367 case BR_STATE_LEARNING:
Joe Perches9a7b6ef92012-05-08 18:56:49 +0000368 if (ether_addr_equal(p->br->dev->dev_addr, dest))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 skb->pkt_type = PACKET_HOST;
370
Florian Westphal971502d2019-04-11 16:36:41 +0200371 return nf_hook_bridge_pre(skb, pskb);
Stephen Hemminger467aea02007-03-21 13:42:06 -0700372 default:
373drop:
374 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Jiri Pirko8a4eb572011-03-12 03:14:39 +0000376 return RX_HANDLER_CONSUMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
Vladimir Oltean9eb8eff2020-05-10 19:37:40 +0300378
379/* This function has no purpose other than to appease the br_port_get_rcu/rtnl
380 * helpers which identify bridged ports according to the rx_handler installed
381 * on them (so there _needs_ to be a bridge rx_handler even if we don't need it
382 * to do anything useful). This bridge won't support traffic to/from the stack,
383 * but only hardware bridging. So return RX_HANDLER_PASS so we don't steal
384 * frames from the ETH_P_XDSA packet_type handler.
385 */
386static rx_handler_result_t br_handle_frame_dummy(struct sk_buff **pskb)
387{
388 return RX_HANDLER_PASS;
389}
390
391rx_handler_func_t *br_get_rx_handler(const struct net_device *dev)
392{
393 if (netdev_uses_dsa(dev))
394 return br_handle_frame_dummy;
395
396 return br_handle_frame;
397}
Henrik Bjoernlund90c628d2020-10-27 10:02:42 +0000398
399void br_add_frame(struct net_bridge *br, struct br_frame_type *ft)
400{
401 hlist_add_head_rcu(&ft->list, &br->frame_type_list);
402}
403
404void br_del_frame(struct net_bridge *br, struct br_frame_type *ft)
405{
406 struct br_frame_type *tmp;
407
408 hlist_for_each_entry(tmp, &br->frame_type_list, list)
409 if (ft == tmp) {
410 hlist_del_rcu(&ft->list);
411 return;
412 }
413}