blob: fa7bfced888ec0f3b7d8a5ceff17b597cbe9b79e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Forwarding decision
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
Herbert Xu025d89c2010-02-27 19:41:43 +000014#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
17#include <linux/netdevice.h>
WANG Congc06ee962010-05-06 00:48:24 -070018#include <linux/netpoll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/skbuff.h>
Stephen Hemminger85ca7192006-04-26 02:39:19 -070020#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/netfilter_bridge.h>
22#include "br_private.h"
23
David S. Miller87faf3c2010-03-16 14:37:47 -070024static int deliver_clone(const struct net_bridge_port *prev,
25 struct sk_buff *skb,
Michael Braun7f7708f2010-03-16 00:26:22 -070026 void (*__packet_hook)(const struct net_bridge_port *p,
27 struct sk_buff *skb));
28
tanxiaojun1a81a2e2013-12-16 21:32:46 +080029/* Don't forward packets to originating port or forwarding disabled */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090030static inline int should_deliver(const struct net_bridge_port *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 const struct sk_buff *skb)
32{
tanxiaojuna97bfc12013-12-19 13:28:11 +080033 return ((p->flags & BR_HAIRPIN_MODE) || skb->dev != p->dev) &&
Vlad Yasevich85f46c62013-02-13 12:00:11 +000034 br_allowed_egress(p->br, nbp_get_vlan_info(p), skb) &&
tanxiaojuna97bfc12013-12-19 13:28:11 +080035 p->state == BR_STATE_FORWARDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
David Miller7026b1d2015-04-05 22:19:04 -040038int br_dev_queue_push_xmit(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Toshiaki Makitadf356d52015-07-28 19:05:37 +090040 if (!is_skb_forwardable(skb->dev, skb))
41 goto drop;
42
43 skb_push(skb, ETH_HLEN);
44 br_drop_fake_rtable(skb);
45 skb_sender_cpu_clear(skb);
46
47 if (skb->ip_summed == CHECKSUM_PARTIAL &&
48 (skb->protocol == htons(ETH_P_8021Q) ||
49 skb->protocol == htons(ETH_P_8021AD))) {
50 int depth;
51
52 if (!__vlan_get_protocol(skb, skb->protocol, &depth))
53 goto drop;
54
55 skb_set_network_header(skb, depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 }
57
Toshiaki Makitadf356d52015-07-28 19:05:37 +090058 dev_queue_xmit(skb);
59
60 return 0;
61
62drop:
63 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return 0;
65}
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +020066EXPORT_SYMBOL_GPL(br_dev_queue_push_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
David Miller7026b1d2015-04-05 22:19:04 -040068int br_forward_finish(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
David Miller7026b1d2015-04-05 22:19:04 -040070 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING, sk, skb,
71 NULL, skb->dev,
Stephen Hemminger9ef513b2006-05-25 15:58:54 -070072 br_dev_queue_push_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +020075EXPORT_SYMBOL_GPL(br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
78{
Vlad Yasevich78851982013-02-13 12:00:14 +000079 skb = br_handle_vlan(to->br, nbp_get_vlan_info(to), skb);
80 if (!skb)
81 return;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 skb->dev = to->dev;
Herbert Xu91d2c342010-06-10 16:12:50 +000084
Amerigo Wange15c3c222012-08-10 01:24:45 +000085 if (unlikely(netpoll_tx_running(to->br->dev))) {
Vlad Yasevichf6367b42014-03-27 17:32:30 -040086 if (!is_skb_forwardable(skb->dev, skb))
Herbert Xu91d2c342010-06-10 16:12:50 +000087 kfree_skb(skb);
88 else {
89 skb_push(skb, ETH_HLEN);
90 br_netpoll_send_skb(to, skb);
91 }
92 return;
93 }
94
David Miller7026b1d2015-04-05 22:19:04 -040095 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
96 NULL, skb->dev,
Jan Engelhardt713aefa2010-03-23 04:07:21 +010097 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
101{
102 struct net_device *indev;
103
Herbert Xu4906f992009-02-09 15:07:18 -0800104 if (skb_warn_if_lro(skb)) {
105 kfree_skb(skb);
106 return;
107 }
108
Vlad Yasevich78851982013-02-13 12:00:14 +0000109 skb = br_handle_vlan(to->br, nbp_get_vlan_info(to), skb);
110 if (!skb)
111 return;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 indev = skb->dev;
114 skb->dev = to->dev;
Herbert Xu35fc92a2007-03-26 23:22:20 -0700115 skb_forward_csum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
David Miller7026b1d2015-04-05 22:19:04 -0400117 NF_HOOK(NFPROTO_BRIDGE, NF_BR_FORWARD, NULL, skb,
118 indev, skb->dev,
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100119 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122/* called with rcu_read_lock */
123void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
124{
stephen hemminger43598812011-12-08 07:17:49 +0000125 if (to && should_deliver(to, skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 __br_deliver(to, skb);
127 return;
128 }
129
130 kfree_skb(skb);
131}
Pablo Neira Ayuso523b9292014-10-25 18:40:26 +0200132EXPORT_SYMBOL_GPL(br_deliver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/* called with rcu_read_lock */
Michael Braun7f7708f2010-03-16 00:26:22 -0700135void br_forward(const struct net_bridge_port *to, struct sk_buff *skb, struct sk_buff *skb0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Herbert Xu4906f992009-02-09 15:07:18 -0800137 if (should_deliver(to, skb)) {
Michael Braun7f7708f2010-03-16 00:26:22 -0700138 if (skb0)
139 deliver_clone(to, skb, __br_forward);
140 else
141 __br_forward(to, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return;
143 }
144
Michael Braun7f7708f2010-03-16 00:26:22 -0700145 if (!skb0)
146 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
David S. Miller87faf3c2010-03-16 14:37:47 -0700149static int deliver_clone(const struct net_bridge_port *prev,
150 struct sk_buff *skb,
Herbert Xu025d89c2010-02-27 19:41:43 +0000151 void (*__packet_hook)(const struct net_bridge_port *p,
152 struct sk_buff *skb))
153{
Herbert Xufed396a2010-06-15 21:43:07 -0700154 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
155
Herbert Xu025d89c2010-02-27 19:41:43 +0000156 skb = skb_clone(skb, GFP_ATOMIC);
157 if (!skb) {
Herbert Xu025d89c2010-02-27 19:41:43 +0000158 dev->stats.tx_dropped++;
159 return -ENOMEM;
160 }
161
162 __packet_hook(prev, skb);
163 return 0;
164}
165
166static struct net_bridge_port *maybe_deliver(
167 struct net_bridge_port *prev, struct net_bridge_port *p,
168 struct sk_buff *skb,
169 void (*__packet_hook)(const struct net_bridge_port *p,
170 struct sk_buff *skb))
171{
172 int err;
173
174 if (!should_deliver(p, skb))
175 return prev;
176
177 if (!prev)
178 goto out;
179
180 err = deliver_clone(prev, skb, __packet_hook);
181 if (err)
182 return ERR_PTR(err);
183
184out:
185 return p;
186}
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/* called under bridge lock */
Herbert Xue081e1e2007-09-16 16:20:48 -0700189static void br_flood(struct net_bridge *br, struct sk_buff *skb,
Herbert Xub33084b2010-02-27 19:41:41 +0000190 struct sk_buff *skb0,
191 void (*__packet_hook)(const struct net_bridge_port *p,
Vlad Yasevich867a5942013-06-05 10:08:01 -0400192 struct sk_buff *skb),
193 bool unicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 struct net_bridge_port *p;
196 struct net_bridge_port *prev;
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 prev = NULL;
199
200 list_for_each_entry_rcu(p, &br->port_list, list) {
Vlad Yasevich867a5942013-06-05 10:08:01 -0400201 /* Do not flood unicast traffic to ports that turn it off */
202 if (unicast && !(p->flags & BR_FLOOD))
203 continue;
Kyeyoon Park95850112014-10-23 14:49:17 -0700204
205 /* Do not flood to ports that enable proxy ARP */
206 if (p->flags & BR_PROXYARP)
207 continue;
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200208 if ((p->flags & BR_PROXYARP_WIFI) &&
209 BR_INPUT_SKB_CB(skb)->proxyarp_replied)
210 continue;
Kyeyoon Park95850112014-10-23 14:49:17 -0700211
Herbert Xu025d89c2010-02-27 19:41:43 +0000212 prev = maybe_deliver(prev, p, skb, __packet_hook);
213 if (IS_ERR(prev))
214 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
Herbert Xub33084b2010-02-27 19:41:41 +0000217 if (!prev)
218 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Herbert Xu025d89c2010-02-27 19:41:43 +0000220 if (skb0)
221 deliver_clone(prev, skb, __packet_hook);
222 else
223 __packet_hook(prev, skb);
Herbert Xub33084b2010-02-27 19:41:41 +0000224 return;
225
226out:
227 if (!skb0)
228 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231
232/* called with rcu_read_lock */
Vlad Yasevich867a5942013-06-05 10:08:01 -0400233void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, bool unicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Vlad Yasevich867a5942013-06-05 10:08:01 -0400235 br_flood(br, skb, NULL, __br_deliver, unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/* called under bridge lock */
Herbert Xub33084b2010-02-27 19:41:41 +0000239void br_flood_forward(struct net_bridge *br, struct sk_buff *skb,
Vlad Yasevich867a5942013-06-05 10:08:01 -0400240 struct sk_buff *skb2, bool unicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Vlad Yasevich867a5942013-06-05 10:08:01 -0400242 br_flood(br, skb, skb2, __br_forward, unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
Herbert Xu5cb5e942010-02-27 19:41:46 +0000244
245#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
246/* called with rcu_read_lock */
247static void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
248 struct sk_buff *skb, struct sk_buff *skb0,
249 void (*__packet_hook)(
250 const struct net_bridge_port *p,
251 struct sk_buff *skb))
252{
253 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
254 struct net_bridge *br = netdev_priv(dev);
stephen hemmingerafe01592010-04-27 15:01:07 +0000255 struct net_bridge_port *prev = NULL;
Herbert Xu5cb5e942010-02-27 19:41:46 +0000256 struct net_bridge_port_group *p;
257 struct hlist_node *rp;
258
Eric Dumazete8051682010-11-15 06:38:10 +0000259 rp = rcu_dereference(hlist_first_rcu(&br->router_list));
stephen hemminger83f6a742010-04-27 15:01:06 +0000260 p = mdst ? rcu_dereference(mdst->ports) : NULL;
Herbert Xu5cb5e942010-02-27 19:41:46 +0000261 while (p || rp) {
stephen hemmingerafe01592010-04-27 15:01:07 +0000262 struct net_bridge_port *port, *lport, *rport;
263
Herbert Xu5cb5e942010-02-27 19:41:46 +0000264 lport = p ? p->port : NULL;
265 rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) :
266 NULL;
267
268 port = (unsigned long)lport > (unsigned long)rport ?
269 lport : rport;
270
271 prev = maybe_deliver(prev, port, skb, __packet_hook);
272 if (IS_ERR(prev))
273 goto out;
274
275 if ((unsigned long)lport >= (unsigned long)port)
stephen hemminger83f6a742010-04-27 15:01:06 +0000276 p = rcu_dereference(p->next);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000277 if ((unsigned long)rport >= (unsigned long)port)
Eric Dumazete8051682010-11-15 06:38:10 +0000278 rp = rcu_dereference(hlist_next_rcu(rp));
Herbert Xu5cb5e942010-02-27 19:41:46 +0000279 }
280
281 if (!prev)
282 goto out;
283
284 if (skb0)
285 deliver_clone(prev, skb, __packet_hook);
286 else
287 __packet_hook(prev, skb);
288 return;
289
290out:
291 if (!skb0)
292 kfree_skb(skb);
293}
294
295/* called with rcu_read_lock */
296void br_multicast_deliver(struct net_bridge_mdb_entry *mdst,
297 struct sk_buff *skb)
298{
299 br_multicast_flood(mdst, skb, NULL, __br_deliver);
300}
301
302/* called with rcu_read_lock */
303void br_multicast_forward(struct net_bridge_mdb_entry *mdst,
304 struct sk_buff *skb, struct sk_buff *skb2)
305{
306 br_multicast_flood(mdst, skb, skb2, __br_forward);
307}
308#endif