blob: ec646656dbf14dfe9a00f46a5646515598b506df [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) &&
Nikolay Aleksandrovac0e9322020-01-24 13:40:19 +020028 p->state == BR_STATE_FORWARDING && br_allowed_egress(vg, skb) &&
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 &&
Menglong Donga98c0c42021-01-17 16:09:50 +080042 eth_type_vlan(skb->protocol)) {
Toshiaki Makitadf356d52015-07-28 19:05:37 +090043 int depth;
44
45 if (!__vlan_get_protocol(skb, skb->protocol, &depth))
46 goto drop;
47
48 skb_set_network_header(skb, depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
50
Vladimir Olteanc5381152021-07-23 23:49:11 +030051 br_switchdev_frame_set_offload_fwd_mark(skb);
Tobias Waldekranz47211192021-07-22 18:55:38 +030052
Toshiaki Makitadf356d52015-07-28 19:05:37 +090053 dev_queue_xmit(skb);
54
55 return 0;
56
57drop:
58 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return 0;
60}
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +020061EXPORT_SYMBOL_GPL(br_dev_queue_push_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050063int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Paolo Abeni41d1c882019-01-08 18:45:05 +010065 skb->tstamp = 0;
Eric W. Biederman29a26a52015-09-15 20:04:16 -050066 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING,
67 net, sk, skb, NULL, skb->dev,
Stephen Hemminger9ef513b2006-05-25 15:58:54 -070068 br_dev_queue_push_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +020071EXPORT_SYMBOL_GPL(br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +030073static void __br_forward(const struct net_bridge_port *to,
74 struct sk_buff *skb, bool local_orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020076 struct net_bridge_vlan_group *vg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct net_device *indev;
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +030078 struct net *net;
79 int br_hook;
Herbert Xu4906f992009-02-09 15:07:18 -080080
Tobias Waldekranz47211192021-07-22 18:55:38 +030081 /* Mark the skb for forwarding offload early so that br_handle_vlan()
82 * can know whether to pop the VLAN header on egress or keep it.
83 */
84 nbp_switchdev_frame_mark_tx_fwd_offload(to, skb);
85
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020086 vg = nbp_vlan_group_rcu(to);
Roopa Prabhu11538d02017-01-31 22:59:55 -080087 skb = br_handle_vlan(to->br, to, vg, skb);
Vlad Yasevich78851982013-02-13 12:00:14 +000088 if (!skb)
89 return;
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 indev = skb->dev;
92 skb->dev = to->dev;
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +030093 if (!local_orig) {
94 if (skb_warn_if_lro(skb)) {
95 kfree_skb(skb);
96 return;
97 }
98 br_hook = NF_BR_FORWARD;
99 skb_forward_csum(skb);
100 net = dev_net(indev);
101 } else {
102 if (unlikely(netpoll_tx_running(to->br->dev))) {
Yunjian Wang28c13822019-01-17 09:46:41 +0800103 skb_push(skb, ETH_HLEN);
104 if (!is_skb_forwardable(skb->dev, skb))
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300105 kfree_skb(skb);
Yunjian Wang28c13822019-01-17 09:46:41 +0800106 else
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300107 br_netpoll_send_skb(to, skb);
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300108 return;
109 }
110 br_hook = NF_BR_LOCAL_OUT;
111 net = dev_net(skb->dev);
112 indev = NULL;
113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300115 NF_HOOK(NFPROTO_BRIDGE, br_hook,
116 net, NULL, skb, indev, skb->dev,
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100117 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
David S. Miller87faf3c2010-03-16 14:37:47 -0700120static int deliver_clone(const struct net_bridge_port *prev,
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300121 struct sk_buff *skb, bool local_orig)
Herbert Xu025d89c2010-02-27 19:41:43 +0000122{
Herbert Xufed396a2010-06-15 21:43:07 -0700123 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
124
Herbert Xu025d89c2010-02-27 19:41:43 +0000125 skb = skb_clone(skb, GFP_ATOMIC);
126 if (!skb) {
Herbert Xu025d89c2010-02-27 19:41:43 +0000127 dev->stats.tx_dropped++;
128 return -ENOMEM;
129 }
130
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300131 __br_forward(prev, skb, local_orig);
Herbert Xu025d89c2010-02-27 19:41:43 +0000132 return 0;
133}
134
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300135/**
136 * br_forward - forward a packet to a specific port
137 * @to: destination port
138 * @skb: packet being forwarded
139 * @local_rcv: packet will be received locally after forwarding
140 * @local_orig: packet is locally originated
141 *
142 * Should be called with rcu_read_lock.
143 */
144void br_forward(const struct net_bridge_port *to,
145 struct sk_buff *skb, bool local_rcv, bool local_orig)
146{
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300147 if (unlikely(!to))
148 goto out;
149
150 /* redirect to backup link if the destination port is down */
151 if (rcu_access_pointer(to->backup_port) && !netif_carrier_ok(to->dev)) {
152 struct net_bridge_port *backup_port;
153
154 backup_port = rcu_dereference(to->backup_port);
155 if (unlikely(!backup_port))
156 goto out;
157 to = backup_port;
158 }
159
160 if (should_deliver(to, skb)) {
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300161 if (local_rcv)
162 deliver_clone(to, skb, local_orig);
163 else
164 __br_forward(to, skb, local_orig);
165 return;
166 }
167
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300168out:
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300169 if (!local_rcv)
170 kfree_skb(skb);
171}
172EXPORT_SYMBOL_GPL(br_forward);
173
Herbert Xu025d89c2010-02-27 19:41:43 +0000174static struct net_bridge_port *maybe_deliver(
175 struct net_bridge_port *prev, struct net_bridge_port *p,
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300176 struct sk_buff *skb, bool local_orig)
Herbert Xu025d89c2010-02-27 19:41:43 +0000177{
Pablo Neira Ayuso847d44e2019-04-04 13:56:38 +0200178 u8 igmp_type = br_multicast_igmp_type(skb);
Herbert Xu025d89c2010-02-27 19:41:43 +0000179 int err;
180
181 if (!should_deliver(p, skb))
182 return prev;
183
Tobias Waldekranz47211192021-07-22 18:55:38 +0300184 nbp_switchdev_frame_mark_tx_fwd_to_hwdom(p, skb);
185
Herbert Xu025d89c2010-02-27 19:41:43 +0000186 if (!prev)
187 goto out;
188
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300189 err = deliver_clone(prev, skb, local_orig);
Herbert Xu025d89c2010-02-27 19:41:43 +0000190 if (err)
191 return ERR_PTR(err);
Herbert Xu025d89c2010-02-27 19:41:43 +0000192out:
Pablo Neira Ayuso847d44e2019-04-04 13:56:38 +0200193 br_multicast_count(p->br, p, skb, igmp_type, BR_MCAST_DIR_TX);
194
Herbert Xu025d89c2010-02-27 19:41:43 +0000195 return p;
196}
197
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300198/* called under rcu_read_lock */
199void br_flood(struct net_bridge *br, struct sk_buff *skb,
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200200 enum br_pkt_type pkt_type, bool local_rcv, bool local_orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300202 struct net_bridge_port *prev = NULL;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200203 struct net_bridge_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 list_for_each_entry_rcu(p, &br->port_list, list) {
Mike Manning99f906e2017-04-26 14:48:09 +0100206 /* Do not flood unicast traffic to ports that turn it off, nor
207 * other traffic if flood off, except for traffic we originate
208 */
209 switch (pkt_type) {
210 case BR_PKT_UNICAST:
211 if (!(p->flags & BR_FLOOD))
212 continue;
213 break;
214 case BR_PKT_MULTICAST:
215 if (!(p->flags & BR_MCAST_FLOOD) && skb->dev != br->dev)
216 continue;
217 break;
218 case BR_PKT_BROADCAST:
219 if (!(p->flags & BR_BCAST_FLOOD) && skb->dev != br->dev)
220 continue;
221 break;
222 }
Kyeyoon Park95850112014-10-23 14:49:17 -0700223
224 /* Do not flood to ports that enable proxy ARP */
225 if (p->flags & BR_PROXYARP)
226 continue;
Roopa Prabhu821f1b22017-10-06 22:12:37 -0700227 if ((p->flags & (BR_PROXYARP_WIFI | BR_NEIGH_SUPPRESS)) &&
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200228 BR_INPUT_SKB_CB(skb)->proxyarp_replied)
229 continue;
Kyeyoon Park95850112014-10-23 14:49:17 -0700230
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300231 prev = maybe_deliver(prev, p, skb, local_orig);
Herbert Xu025d89c2010-02-27 19:41:43 +0000232 if (IS_ERR(prev))
233 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235
Herbert Xub33084b2010-02-27 19:41:41 +0000236 if (!prev)
237 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300239 if (local_rcv)
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300240 deliver_clone(prev, skb, local_orig);
Herbert Xu025d89c2010-02-27 19:41:43 +0000241 else
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300242 __br_forward(prev, skb, local_orig);
Herbert Xub33084b2010-02-27 19:41:41 +0000243 return;
244
245out:
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300246 if (!local_rcv)
Herbert Xub33084b2010-02-27 19:41:41 +0000247 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Herbert Xu5cb5e942010-02-27 19:41:46 +0000250#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
Arnd Bergmann5b9d6b12017-01-25 23:29:33 +0100251static void maybe_deliver_addr(struct net_bridge_port *p, struct sk_buff *skb,
252 const unsigned char *addr, bool local_orig)
253{
254 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
255 const unsigned char *src = eth_hdr(skb)->h_source;
256
257 if (!should_deliver(p, skb))
258 return;
259
260 /* Even with hairpin, no soliloquies - prevent breaking IPv6 DAD */
261 if (skb->dev == p->dev && ether_addr_equal(src, addr))
262 return;
263
264 skb = skb_copy(skb, GFP_ATOMIC);
265 if (!skb) {
266 dev->stats.tx_dropped++;
267 return;
268 }
269
270 if (!is_broadcast_ether_addr(addr))
271 memcpy(eth_hdr(skb)->h_dest, addr, ETH_ALEN);
272
273 __br_forward(p, skb, local_orig);
274}
275
Herbert Xu5cb5e942010-02-27 19:41:46 +0000276/* called with rcu_read_lock */
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300277void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
278 struct sk_buff *skb,
Nikolay Aleksandrovadc47032021-07-19 20:06:25 +0300279 struct net_bridge_mcast *brmctx,
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300280 bool local_rcv, bool local_orig)
Herbert Xu5cb5e942010-02-27 19:41:46 +0000281{
stephen hemmingerafe01592010-04-27 15:01:07 +0000282 struct net_bridge_port *prev = NULL;
Herbert Xu5cb5e942010-02-27 19:41:46 +0000283 struct net_bridge_port_group *p;
Nikolay Aleksandrov36cfec72020-09-22 10:30:27 +0300284 bool allow_mode_include = true;
Herbert Xu5cb5e942010-02-27 19:41:46 +0000285 struct hlist_node *rp;
286
Nikolay Aleksandrovadc47032021-07-19 20:06:25 +0300287 rp = br_multicast_get_first_rport_node(brmctx, skb);
Linus Lüssing44ebb082021-05-13 15:20:44 +0200288
Nikolay Aleksandrov36cfec72020-09-22 10:30:27 +0300289 if (mdst) {
290 p = rcu_dereference(mdst->ports);
Nikolay Aleksandrovadc47032021-07-19 20:06:25 +0300291 if (br_multicast_should_handle_mode(brmctx, mdst->addr.proto) &&
Nikolay Aleksandrov36cfec72020-09-22 10:30:27 +0300292 br_multicast_is_star_g(&mdst->addr))
293 allow_mode_include = false;
294 } else {
295 p = NULL;
296 }
297
Herbert Xu5cb5e942010-02-27 19:41:46 +0000298 while (p || rp) {
stephen hemmingerafe01592010-04-27 15:01:07 +0000299 struct net_bridge_port *port, *lport, *rport;
300
Nikolay Aleksandrov085b53c2020-09-22 10:30:22 +0300301 lport = p ? p->key.port : NULL;
Linus Lüssing44ebb082021-05-13 15:20:44 +0200302 rport = br_multicast_rport_from_node_skb(rp, skb);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000303
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100304 if ((unsigned long)lport > (unsigned long)rport) {
305 port = lport;
306
307 if (port->flags & BR_MULTICAST_TO_UNICAST) {
308 maybe_deliver_addr(lport, skb, p->eth_addr,
309 local_orig);
310 goto delivered;
311 }
Nikolay Aleksandrov36cfec72020-09-22 10:30:27 +0300312 if ((!allow_mode_include &&
313 p->filter_mode == MCAST_INCLUDE) ||
314 (p->flags & MDB_PG_FLAGS_BLOCKED))
315 goto delivered;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100316 } else {
317 port = rport;
318 }
Herbert Xu5cb5e942010-02-27 19:41:46 +0000319
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300320 prev = maybe_deliver(prev, port, skb, local_orig);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000321 if (IS_ERR(prev))
322 goto out;
Pablo Neira Ayuso847d44e2019-04-04 13:56:38 +0200323delivered:
Herbert Xu5cb5e942010-02-27 19:41:46 +0000324 if ((unsigned long)lport >= (unsigned long)port)
stephen hemminger83f6a742010-04-27 15:01:06 +0000325 p = rcu_dereference(p->next);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000326 if ((unsigned long)rport >= (unsigned long)port)
Eric Dumazete8051682010-11-15 06:38:10 +0000327 rp = rcu_dereference(hlist_next_rcu(rp));
Herbert Xu5cb5e942010-02-27 19:41:46 +0000328 }
329
330 if (!prev)
331 goto out;
332
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300333 if (local_rcv)
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300334 deliver_clone(prev, skb, local_orig);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000335 else
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300336 __br_forward(prev, skb, local_orig);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000337 return;
338
339out:
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300340 if (!local_rcv)
Herbert Xu5cb5e942010-02-27 19:41:46 +0000341 kfree_skb(skb);
342}
Herbert Xu5cb5e942010-02-27 19:41:46 +0000343#endif