blob: 2dd2a23ce707bfeb4d84ec8e34223c14ade82458 [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{
Eric W. Biederman29a26a52015-09-15 20:04:16 -050070 struct net *net = dev_net(skb->dev);
71 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING,
72 net, sk, skb, NULL, skb->dev,
Stephen Hemminger9ef513b2006-05-25 15:58:54 -070073 br_dev_queue_push_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +020076EXPORT_SYMBOL_GPL(br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
79{
Vlad Yasevich78851982013-02-13 12:00:14 +000080 skb = br_handle_vlan(to->br, nbp_get_vlan_info(to), skb);
81 if (!skb)
82 return;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 skb->dev = to->dev;
Herbert Xu91d2c342010-06-10 16:12:50 +000085
Amerigo Wange15c3c222012-08-10 01:24:45 +000086 if (unlikely(netpoll_tx_running(to->br->dev))) {
Vlad Yasevichf6367b42014-03-27 17:32:30 -040087 if (!is_skb_forwardable(skb->dev, skb))
Herbert Xu91d2c342010-06-10 16:12:50 +000088 kfree_skb(skb);
89 else {
90 skb_push(skb, ETH_HLEN);
91 br_netpoll_send_skb(to, skb);
92 }
93 return;
94 }
95
Eric W. Biederman29a26a52015-09-15 20:04:16 -050096 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
97 dev_net(skb->dev), NULL, skb,NULL, skb->dev,
Jan Engelhardt713aefa2010-03-23 04:07:21 +010098 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
102{
103 struct net_device *indev;
104
Herbert Xu4906f992009-02-09 15:07:18 -0800105 if (skb_warn_if_lro(skb)) {
106 kfree_skb(skb);
107 return;
108 }
109
Vlad Yasevich78851982013-02-13 12:00:14 +0000110 skb = br_handle_vlan(to->br, nbp_get_vlan_info(to), skb);
111 if (!skb)
112 return;
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 indev = skb->dev;
115 skb->dev = to->dev;
Herbert Xu35fc92a2007-03-26 23:22:20 -0700116 skb_forward_csum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500118 NF_HOOK(NFPROTO_BRIDGE, NF_BR_FORWARD,
119 dev_net(indev), NULL, skb, indev, skb->dev,
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100120 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123/* called with rcu_read_lock */
124void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
125{
stephen hemminger43598812011-12-08 07:17:49 +0000126 if (to && should_deliver(to, skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 __br_deliver(to, skb);
128 return;
129 }
130
131 kfree_skb(skb);
132}
Pablo Neira Ayuso523b9292014-10-25 18:40:26 +0200133EXPORT_SYMBOL_GPL(br_deliver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/* called with rcu_read_lock */
Michael Braun7f7708f2010-03-16 00:26:22 -0700136void br_forward(const struct net_bridge_port *to, struct sk_buff *skb, struct sk_buff *skb0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Herbert Xu4906f992009-02-09 15:07:18 -0800138 if (should_deliver(to, skb)) {
Michael Braun7f7708f2010-03-16 00:26:22 -0700139 if (skb0)
140 deliver_clone(to, skb, __br_forward);
141 else
142 __br_forward(to, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return;
144 }
145
Michael Braun7f7708f2010-03-16 00:26:22 -0700146 if (!skb0)
147 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
David S. Miller87faf3c2010-03-16 14:37:47 -0700150static int deliver_clone(const struct net_bridge_port *prev,
151 struct sk_buff *skb,
Herbert Xu025d89c2010-02-27 19:41:43 +0000152 void (*__packet_hook)(const struct net_bridge_port *p,
153 struct sk_buff *skb))
154{
Herbert Xufed396a2010-06-15 21:43:07 -0700155 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
156
Herbert Xu025d89c2010-02-27 19:41:43 +0000157 skb = skb_clone(skb, GFP_ATOMIC);
158 if (!skb) {
Herbert Xu025d89c2010-02-27 19:41:43 +0000159 dev->stats.tx_dropped++;
160 return -ENOMEM;
161 }
162
163 __packet_hook(prev, skb);
164 return 0;
165}
166
167static struct net_bridge_port *maybe_deliver(
168 struct net_bridge_port *prev, struct net_bridge_port *p,
169 struct sk_buff *skb,
170 void (*__packet_hook)(const struct net_bridge_port *p,
171 struct sk_buff *skb))
172{
173 int err;
174
175 if (!should_deliver(p, skb))
176 return prev;
177
178 if (!prev)
179 goto out;
180
181 err = deliver_clone(prev, skb, __packet_hook);
182 if (err)
183 return ERR_PTR(err);
184
185out:
186 return p;
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/* called under bridge lock */
Herbert Xue081e1e2007-09-16 16:20:48 -0700190static void br_flood(struct net_bridge *br, struct sk_buff *skb,
Herbert Xub33084b2010-02-27 19:41:41 +0000191 struct sk_buff *skb0,
192 void (*__packet_hook)(const struct net_bridge_port *p,
Vlad Yasevich867a5942013-06-05 10:08:01 -0400193 struct sk_buff *skb),
194 bool unicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 struct net_bridge_port *p;
197 struct net_bridge_port *prev;
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 prev = NULL;
200
201 list_for_each_entry_rcu(p, &br->port_list, list) {
Vlad Yasevich867a5942013-06-05 10:08:01 -0400202 /* Do not flood unicast traffic to ports that turn it off */
203 if (unicast && !(p->flags & BR_FLOOD))
204 continue;
Kyeyoon Park95850112014-10-23 14:49:17 -0700205
206 /* Do not flood to ports that enable proxy ARP */
207 if (p->flags & BR_PROXYARP)
208 continue;
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200209 if ((p->flags & BR_PROXYARP_WIFI) &&
210 BR_INPUT_SKB_CB(skb)->proxyarp_replied)
211 continue;
Kyeyoon Park95850112014-10-23 14:49:17 -0700212
Herbert Xu025d89c2010-02-27 19:41:43 +0000213 prev = maybe_deliver(prev, p, skb, __packet_hook);
214 if (IS_ERR(prev))
215 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
Herbert Xub33084b2010-02-27 19:41:41 +0000218 if (!prev)
219 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Herbert Xu025d89c2010-02-27 19:41:43 +0000221 if (skb0)
222 deliver_clone(prev, skb, __packet_hook);
223 else
224 __packet_hook(prev, skb);
Herbert Xub33084b2010-02-27 19:41:41 +0000225 return;
226
227out:
228 if (!skb0)
229 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232
233/* called with rcu_read_lock */
Vlad Yasevich867a5942013-06-05 10:08:01 -0400234void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, bool unicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Vlad Yasevich867a5942013-06-05 10:08:01 -0400236 br_flood(br, skb, NULL, __br_deliver, unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239/* called under bridge lock */
Herbert Xub33084b2010-02-27 19:41:41 +0000240void br_flood_forward(struct net_bridge *br, struct sk_buff *skb,
Vlad Yasevich867a5942013-06-05 10:08:01 -0400241 struct sk_buff *skb2, bool unicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Vlad Yasevich867a5942013-06-05 10:08:01 -0400243 br_flood(br, skb, skb2, __br_forward, unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
Herbert Xu5cb5e942010-02-27 19:41:46 +0000245
246#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
247/* called with rcu_read_lock */
248static void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
249 struct sk_buff *skb, struct sk_buff *skb0,
250 void (*__packet_hook)(
251 const struct net_bridge_port *p,
252 struct sk_buff *skb))
253{
254 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
255 struct net_bridge *br = netdev_priv(dev);
stephen hemmingerafe01592010-04-27 15:01:07 +0000256 struct net_bridge_port *prev = NULL;
Herbert Xu5cb5e942010-02-27 19:41:46 +0000257 struct net_bridge_port_group *p;
258 struct hlist_node *rp;
259
Eric Dumazete8051682010-11-15 06:38:10 +0000260 rp = rcu_dereference(hlist_first_rcu(&br->router_list));
stephen hemminger83f6a742010-04-27 15:01:06 +0000261 p = mdst ? rcu_dereference(mdst->ports) : NULL;
Herbert Xu5cb5e942010-02-27 19:41:46 +0000262 while (p || rp) {
stephen hemmingerafe01592010-04-27 15:01:07 +0000263 struct net_bridge_port *port, *lport, *rport;
264
Herbert Xu5cb5e942010-02-27 19:41:46 +0000265 lport = p ? p->port : NULL;
266 rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) :
267 NULL;
268
269 port = (unsigned long)lport > (unsigned long)rport ?
270 lport : rport;
271
272 prev = maybe_deliver(prev, port, skb, __packet_hook);
273 if (IS_ERR(prev))
274 goto out;
275
276 if ((unsigned long)lport >= (unsigned long)port)
stephen hemminger83f6a742010-04-27 15:01:06 +0000277 p = rcu_dereference(p->next);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000278 if ((unsigned long)rport >= (unsigned long)port)
Eric Dumazete8051682010-11-15 06:38:10 +0000279 rp = rcu_dereference(hlist_next_rcu(rp));
Herbert Xu5cb5e942010-02-27 19:41:46 +0000280 }
281
282 if (!prev)
283 goto out;
284
285 if (skb0)
286 deliver_clone(prev, skb, __packet_hook);
287 else
288 __packet_hook(prev, skb);
289 return;
290
291out:
292 if (!skb0)
293 kfree_skb(skb);
294}
295
296/* called with rcu_read_lock */
297void br_multicast_deliver(struct net_bridge_mdb_entry *mdst,
298 struct sk_buff *skb)
299{
300 br_multicast_flood(mdst, skb, NULL, __br_deliver);
301}
302
303/* called with rcu_read_lock */
304void br_multicast_forward(struct net_bridge_mdb_entry *mdst,
305 struct sk_buff *skb, struct sk_buff *skb2)
306{
307 br_multicast_flood(mdst, skb, skb2, __br_forward);
308}
309#endif