blob: 86637000f275dbb44e3d6abc073ec254f1708480 [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 * Forwarding decision
4 * Linux ethernet bridge
5 *
6 * Authors:
7 * Lennert Buytenhek <buytenh@gnu.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Herbert Xu025d89c2010-02-27 19:41:43 +000010#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
13#include <linux/netdevice.h>
WANG Congc06ee962010-05-06 00:48:24 -070014#include <linux/netpoll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/skbuff.h>
Stephen Hemminger85ca7192006-04-26 02:39:19 -070016#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/netfilter_bridge.h>
18#include "br_private.h"
19
tanxiaojun1a81a2e2013-12-16 21:32:46 +080020/* Don't forward packets to originating port or forwarding disabled */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090021static inline int should_deliver(const struct net_bridge_port *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 const struct sk_buff *skb)
23{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020024 struct net_bridge_vlan_group *vg;
25
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020026 vg = nbp_vlan_group_rcu(p);
tanxiaojuna97bfc12013-12-19 13:28:11 +080027 return ((p->flags & BR_HAIRPIN_MODE) || skb->dev != p->dev) &&
Ido Schimmel6bc506b2016-08-25 18:42:37 +020028 br_allowed_egress(vg, skb) && p->state == BR_STATE_FORWARDING &&
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +030029 nbp_switchdev_allowed_egress(p, skb) &&
30 !br_skb_isolated(p, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031}
32
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050033int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Yunjian Wang28c13822019-01-17 09:46:41 +080035 skb_push(skb, ETH_HLEN);
Toshiaki Makitadf356d52015-07-28 19:05:37 +090036 if (!is_skb_forwardable(skb->dev, skb))
37 goto drop;
38
Toshiaki Makitadf356d52015-07-28 19:05:37 +090039 br_drop_fake_rtable(skb);
Toshiaki Makitadf356d52015-07-28 19:05:37 +090040
41 if (skb->ip_summed == CHECKSUM_PARTIAL &&
42 (skb->protocol == htons(ETH_P_8021Q) ||
43 skb->protocol == htons(ETH_P_8021AD))) {
44 int depth;
45
46 if (!__vlan_get_protocol(skb, skb->protocol, &depth))
47 goto drop;
48
49 skb_set_network_header(skb, depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 }
51
Toshiaki Makitadf356d52015-07-28 19:05:37 +090052 dev_queue_xmit(skb);
53
54 return 0;
55
56drop:
57 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return 0;
59}
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +020060EXPORT_SYMBOL_GPL(br_dev_queue_push_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050062int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Paolo Abeni41d1c882019-01-08 18:45:05 +010064 skb->tstamp = 0;
Eric W. Biederman29a26a52015-09-15 20:04:16 -050065 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING,
66 net, sk, skb, NULL, skb->dev,
Stephen Hemminger9ef513b2006-05-25 15:58:54 -070067 br_dev_queue_push_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +020070EXPORT_SYMBOL_GPL(br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +030072static void __br_forward(const struct net_bridge_port *to,
73 struct sk_buff *skb, bool local_orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020075 struct net_bridge_vlan_group *vg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct net_device *indev;
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +030077 struct net *net;
78 int br_hook;
Herbert Xu4906f992009-02-09 15:07:18 -080079
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020080 vg = nbp_vlan_group_rcu(to);
Roopa Prabhu11538d02017-01-31 22:59:55 -080081 skb = br_handle_vlan(to->br, to, vg, skb);
Vlad Yasevich78851982013-02-13 12:00:14 +000082 if (!skb)
83 return;
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 indev = skb->dev;
86 skb->dev = to->dev;
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +030087 if (!local_orig) {
88 if (skb_warn_if_lro(skb)) {
89 kfree_skb(skb);
90 return;
91 }
92 br_hook = NF_BR_FORWARD;
93 skb_forward_csum(skb);
94 net = dev_net(indev);
95 } else {
96 if (unlikely(netpoll_tx_running(to->br->dev))) {
Yunjian Wang28c13822019-01-17 09:46:41 +080097 skb_push(skb, ETH_HLEN);
98 if (!is_skb_forwardable(skb->dev, skb))
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +030099 kfree_skb(skb);
Yunjian Wang28c13822019-01-17 09:46:41 +0800100 else
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300101 br_netpoll_send_skb(to, skb);
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300102 return;
103 }
104 br_hook = NF_BR_LOCAL_OUT;
105 net = dev_net(skb->dev);
106 indev = NULL;
107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300109 NF_HOOK(NFPROTO_BRIDGE, br_hook,
110 net, NULL, skb, indev, skb->dev,
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100111 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
David S. Miller87faf3c2010-03-16 14:37:47 -0700114static int deliver_clone(const struct net_bridge_port *prev,
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300115 struct sk_buff *skb, bool local_orig)
Herbert Xu025d89c2010-02-27 19:41:43 +0000116{
Herbert Xufed396a2010-06-15 21:43:07 -0700117 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
118
Herbert Xu025d89c2010-02-27 19:41:43 +0000119 skb = skb_clone(skb, GFP_ATOMIC);
120 if (!skb) {
Herbert Xu025d89c2010-02-27 19:41:43 +0000121 dev->stats.tx_dropped++;
122 return -ENOMEM;
123 }
124
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300125 __br_forward(prev, skb, local_orig);
Herbert Xu025d89c2010-02-27 19:41:43 +0000126 return 0;
127}
128
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300129/**
130 * br_forward - forward a packet to a specific port
131 * @to: destination port
132 * @skb: packet being forwarded
133 * @local_rcv: packet will be received locally after forwarding
134 * @local_orig: packet is locally originated
135 *
136 * Should be called with rcu_read_lock.
137 */
138void br_forward(const struct net_bridge_port *to,
139 struct sk_buff *skb, bool local_rcv, bool local_orig)
140{
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300141 if (unlikely(!to))
142 goto out;
143
144 /* redirect to backup link if the destination port is down */
145 if (rcu_access_pointer(to->backup_port) && !netif_carrier_ok(to->dev)) {
146 struct net_bridge_port *backup_port;
147
148 backup_port = rcu_dereference(to->backup_port);
149 if (unlikely(!backup_port))
150 goto out;
151 to = backup_port;
152 }
153
154 if (should_deliver(to, skb)) {
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300155 if (local_rcv)
156 deliver_clone(to, skb, local_orig);
157 else
158 __br_forward(to, skb, local_orig);
159 return;
160 }
161
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300162out:
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300163 if (!local_rcv)
164 kfree_skb(skb);
165}
166EXPORT_SYMBOL_GPL(br_forward);
167
Herbert Xu025d89c2010-02-27 19:41:43 +0000168static struct net_bridge_port *maybe_deliver(
169 struct net_bridge_port *prev, struct net_bridge_port *p,
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300170 struct sk_buff *skb, bool local_orig)
Herbert Xu025d89c2010-02-27 19:41:43 +0000171{
Pablo Neira Ayuso847d44e2019-04-04 13:56:38 +0200172 u8 igmp_type = br_multicast_igmp_type(skb);
Herbert Xu025d89c2010-02-27 19:41:43 +0000173 int err;
174
175 if (!should_deliver(p, skb))
176 return prev;
177
178 if (!prev)
179 goto out;
180
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300181 err = deliver_clone(prev, skb, local_orig);
Herbert Xu025d89c2010-02-27 19:41:43 +0000182 if (err)
183 return ERR_PTR(err);
Herbert Xu025d89c2010-02-27 19:41:43 +0000184out:
Pablo Neira Ayuso847d44e2019-04-04 13:56:38 +0200185 br_multicast_count(p->br, p, skb, igmp_type, BR_MCAST_DIR_TX);
186
Herbert Xu025d89c2010-02-27 19:41:43 +0000187 return p;
188}
189
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300190/* called under rcu_read_lock */
191void br_flood(struct net_bridge *br, struct sk_buff *skb,
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200192 enum br_pkt_type pkt_type, bool local_rcv, bool local_orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300194 struct net_bridge_port *prev = NULL;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200195 struct net_bridge_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 list_for_each_entry_rcu(p, &br->port_list, list) {
Mike Manning99f906e2017-04-26 14:48:09 +0100198 /* Do not flood unicast traffic to ports that turn it off, nor
199 * other traffic if flood off, except for traffic we originate
200 */
201 switch (pkt_type) {
202 case BR_PKT_UNICAST:
203 if (!(p->flags & BR_FLOOD))
204 continue;
205 break;
206 case BR_PKT_MULTICAST:
207 if (!(p->flags & BR_MCAST_FLOOD) && skb->dev != br->dev)
208 continue;
209 break;
210 case BR_PKT_BROADCAST:
211 if (!(p->flags & BR_BCAST_FLOOD) && skb->dev != br->dev)
212 continue;
213 break;
214 }
Kyeyoon Park95850112014-10-23 14:49:17 -0700215
216 /* Do not flood to ports that enable proxy ARP */
217 if (p->flags & BR_PROXYARP)
218 continue;
Roopa Prabhu821f1b22017-10-06 22:12:37 -0700219 if ((p->flags & (BR_PROXYARP_WIFI | BR_NEIGH_SUPPRESS)) &&
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200220 BR_INPUT_SKB_CB(skb)->proxyarp_replied)
221 continue;
Kyeyoon Park95850112014-10-23 14:49:17 -0700222
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300223 prev = maybe_deliver(prev, p, skb, local_orig);
Herbert Xu025d89c2010-02-27 19:41:43 +0000224 if (IS_ERR(prev))
225 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227
Herbert Xub33084b2010-02-27 19:41:41 +0000228 if (!prev)
229 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300231 if (local_rcv)
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300232 deliver_clone(prev, skb, local_orig);
Herbert Xu025d89c2010-02-27 19:41:43 +0000233 else
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300234 __br_forward(prev, skb, local_orig);
Herbert Xub33084b2010-02-27 19:41:41 +0000235 return;
236
237out:
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300238 if (!local_rcv)
Herbert Xub33084b2010-02-27 19:41:41 +0000239 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Herbert Xu5cb5e942010-02-27 19:41:46 +0000242#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
Arnd Bergmann5b9d6b12017-01-25 23:29:33 +0100243static void maybe_deliver_addr(struct net_bridge_port *p, struct sk_buff *skb,
244 const unsigned char *addr, bool local_orig)
245{
246 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
247 const unsigned char *src = eth_hdr(skb)->h_source;
248
249 if (!should_deliver(p, skb))
250 return;
251
252 /* Even with hairpin, no soliloquies - prevent breaking IPv6 DAD */
253 if (skb->dev == p->dev && ether_addr_equal(src, addr))
254 return;
255
256 skb = skb_copy(skb, GFP_ATOMIC);
257 if (!skb) {
258 dev->stats.tx_dropped++;
259 return;
260 }
261
262 if (!is_broadcast_ether_addr(addr))
263 memcpy(eth_hdr(skb)->h_dest, addr, ETH_ALEN);
264
265 __br_forward(p, skb, local_orig);
266}
267
Herbert Xu5cb5e942010-02-27 19:41:46 +0000268/* called with rcu_read_lock */
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300269void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
270 struct sk_buff *skb,
271 bool local_rcv, bool local_orig)
Herbert Xu5cb5e942010-02-27 19:41:46 +0000272{
273 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
274 struct net_bridge *br = netdev_priv(dev);
stephen hemmingerafe01592010-04-27 15:01:07 +0000275 struct net_bridge_port *prev = NULL;
Herbert Xu5cb5e942010-02-27 19:41:46 +0000276 struct net_bridge_port_group *p;
277 struct hlist_node *rp;
278
Eric Dumazete8051682010-11-15 06:38:10 +0000279 rp = rcu_dereference(hlist_first_rcu(&br->router_list));
stephen hemminger83f6a742010-04-27 15:01:06 +0000280 p = mdst ? rcu_dereference(mdst->ports) : NULL;
Herbert Xu5cb5e942010-02-27 19:41:46 +0000281 while (p || rp) {
stephen hemmingerafe01592010-04-27 15:01:07 +0000282 struct net_bridge_port *port, *lport, *rport;
283
Herbert Xu5cb5e942010-02-27 19:41:46 +0000284 lport = p ? p->port : NULL;
YueHaibingd8fb1642018-04-26 11:07:05 +0800285 rport = hlist_entry_safe(rp, struct net_bridge_port, rlist);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000286
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100287 if ((unsigned long)lport > (unsigned long)rport) {
288 port = lport;
289
290 if (port->flags & BR_MULTICAST_TO_UNICAST) {
291 maybe_deliver_addr(lport, skb, p->eth_addr,
292 local_orig);
293 goto delivered;
294 }
295 } else {
296 port = rport;
297 }
Herbert Xu5cb5e942010-02-27 19:41:46 +0000298
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300299 prev = maybe_deliver(prev, port, skb, local_orig);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000300 if (IS_ERR(prev))
301 goto out;
Pablo Neira Ayuso847d44e2019-04-04 13:56:38 +0200302delivered:
Herbert Xu5cb5e942010-02-27 19:41:46 +0000303 if ((unsigned long)lport >= (unsigned long)port)
stephen hemminger83f6a742010-04-27 15:01:06 +0000304 p = rcu_dereference(p->next);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000305 if ((unsigned long)rport >= (unsigned long)port)
Eric Dumazete8051682010-11-15 06:38:10 +0000306 rp = rcu_dereference(hlist_next_rcu(rp));
Herbert Xu5cb5e942010-02-27 19:41:46 +0000307 }
308
309 if (!prev)
310 goto out;
311
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300312 if (local_rcv)
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300313 deliver_clone(prev, skb, local_orig);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000314 else
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300315 __br_forward(prev, skb, local_orig);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000316 return;
317
318out:
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300319 if (!local_rcv)
Herbert Xu5cb5e942010-02-27 19:41:46 +0000320 kfree_skb(skb);
321}
Herbert Xu5cb5e942010-02-27 19:41:46 +0000322#endif