blob: ee64287f1290d5a1f35ca9f7b64aeb4bac21eedf [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
Stephen Hemminger9ef513b2006-05-25 15:58:54 -070029/* Don't forward packets to originating port or forwarding diasabled */
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{
Fischer, Anna3982d3d2009-08-13 06:55:16 +000033 return (((p->flags & BR_HAIRPIN_MODE) || skb->dev != p->dev) &&
34 p->state == BR_STATE_FORWARDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
Stephen Hemminger85ca7192006-04-26 02:39:19 -070037static inline unsigned packet_length(const struct sk_buff *skb)
38{
39 return skb->len - (skb->protocol == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
40}
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042int br_dev_queue_push_xmit(struct sk_buff *skb)
43{
Changli Gaof88de8d2010-12-25 03:41:30 +000044 /* ip_fragment doesn't copy the MAC header */
45 if (nf_bridge_maybe_copy_header(skb) ||
46 (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 kfree_skb(skb);
Changli Gaof88de8d2010-12-25 03:41:30 +000048 } else {
49 skb_push(skb, ETH_HLEN);
50 dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
52
53 return 0;
54}
55
56int br_forward_finish(struct sk_buff *skb)
57{
Jan Engelhardt713aefa2010-03-23 04:07:21 +010058 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
Stephen Hemminger9ef513b2006-05-25 15:58:54 -070059 br_dev_queue_push_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
64{
65 skb->dev = to->dev;
Herbert Xu91d2c342010-06-10 16:12:50 +000066
67 if (unlikely(netpoll_tx_running(to->dev))) {
68 if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb))
69 kfree_skb(skb);
70 else {
71 skb_push(skb, ETH_HLEN);
72 br_netpoll_send_skb(to, skb);
73 }
74 return;
75 }
76
Jan Engelhardt713aefa2010-03-23 04:07:21 +010077 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
78 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
82{
83 struct net_device *indev;
84
Herbert Xu4906f992009-02-09 15:07:18 -080085 if (skb_warn_if_lro(skb)) {
86 kfree_skb(skb);
87 return;
88 }
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 indev = skb->dev;
91 skb->dev = to->dev;
Herbert Xu35fc92a2007-03-26 23:22:20 -070092 skb_forward_csum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Jan Engelhardt713aefa2010-03-23 04:07:21 +010094 NF_HOOK(NFPROTO_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
95 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98/* called with rcu_read_lock */
99void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
100{
101 if (should_deliver(to, skb)) {
102 __br_deliver(to, skb);
103 return;
104 }
105
106 kfree_skb(skb);
107}
108
109/* called with rcu_read_lock */
Michael Braun7f7708f2010-03-16 00:26:22 -0700110void br_forward(const struct net_bridge_port *to, struct sk_buff *skb, struct sk_buff *skb0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Herbert Xu4906f992009-02-09 15:07:18 -0800112 if (should_deliver(to, skb)) {
Michael Braun7f7708f2010-03-16 00:26:22 -0700113 if (skb0)
114 deliver_clone(to, skb, __br_forward);
115 else
116 __br_forward(to, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return;
118 }
119
Michael Braun7f7708f2010-03-16 00:26:22 -0700120 if (!skb0)
121 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
David S. Miller87faf3c2010-03-16 14:37:47 -0700124static int deliver_clone(const struct net_bridge_port *prev,
125 struct sk_buff *skb,
Herbert Xu025d89c2010-02-27 19:41:43 +0000126 void (*__packet_hook)(const struct net_bridge_port *p,
127 struct sk_buff *skb))
128{
Herbert Xufed396a2010-06-15 21:43:07 -0700129 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
130
Herbert Xu025d89c2010-02-27 19:41:43 +0000131 skb = skb_clone(skb, GFP_ATOMIC);
132 if (!skb) {
Herbert Xu025d89c2010-02-27 19:41:43 +0000133 dev->stats.tx_dropped++;
134 return -ENOMEM;
135 }
136
137 __packet_hook(prev, skb);
138 return 0;
139}
140
141static struct net_bridge_port *maybe_deliver(
142 struct net_bridge_port *prev, struct net_bridge_port *p,
143 struct sk_buff *skb,
144 void (*__packet_hook)(const struct net_bridge_port *p,
145 struct sk_buff *skb))
146{
147 int err;
148
149 if (!should_deliver(p, skb))
150 return prev;
151
152 if (!prev)
153 goto out;
154
155 err = deliver_clone(prev, skb, __packet_hook);
156 if (err)
157 return ERR_PTR(err);
158
159out:
160 return p;
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* called under bridge lock */
Herbert Xue081e1e2007-09-16 16:20:48 -0700164static void br_flood(struct net_bridge *br, struct sk_buff *skb,
Herbert Xub33084b2010-02-27 19:41:41 +0000165 struct sk_buff *skb0,
166 void (*__packet_hook)(const struct net_bridge_port *p,
167 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 struct net_bridge_port *p;
170 struct net_bridge_port *prev;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 prev = NULL;
173
174 list_for_each_entry_rcu(p, &br->port_list, list) {
Herbert Xu025d89c2010-02-27 19:41:43 +0000175 prev = maybe_deliver(prev, p, skb, __packet_hook);
176 if (IS_ERR(prev))
177 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179
Herbert Xub33084b2010-02-27 19:41:41 +0000180 if (!prev)
181 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Herbert Xu025d89c2010-02-27 19:41:43 +0000183 if (skb0)
184 deliver_clone(prev, skb, __packet_hook);
185 else
186 __packet_hook(prev, skb);
Herbert Xub33084b2010-02-27 19:41:41 +0000187 return;
188
189out:
190 if (!skb0)
191 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194
195/* called with rcu_read_lock */
Herbert Xue081e1e2007-09-16 16:20:48 -0700196void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Herbert Xub33084b2010-02-27 19:41:41 +0000198 br_flood(br, skb, NULL, __br_deliver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201/* called under bridge lock */
Herbert Xub33084b2010-02-27 19:41:41 +0000202void br_flood_forward(struct net_bridge *br, struct sk_buff *skb,
203 struct sk_buff *skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Herbert Xub33084b2010-02-27 19:41:41 +0000205 br_flood(br, skb, skb2, __br_forward);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
Herbert Xu5cb5e942010-02-27 19:41:46 +0000207
208#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
209/* called with rcu_read_lock */
210static void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
211 struct sk_buff *skb, struct sk_buff *skb0,
212 void (*__packet_hook)(
213 const struct net_bridge_port *p,
214 struct sk_buff *skb))
215{
216 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
217 struct net_bridge *br = netdev_priv(dev);
stephen hemmingerafe01592010-04-27 15:01:07 +0000218 struct net_bridge_port *prev = NULL;
Herbert Xu5cb5e942010-02-27 19:41:46 +0000219 struct net_bridge_port_group *p;
220 struct hlist_node *rp;
221
Eric Dumazete8051682010-11-15 06:38:10 +0000222 rp = rcu_dereference(hlist_first_rcu(&br->router_list));
stephen hemminger83f6a742010-04-27 15:01:06 +0000223 p = mdst ? rcu_dereference(mdst->ports) : NULL;
Herbert Xu5cb5e942010-02-27 19:41:46 +0000224 while (p || rp) {
stephen hemmingerafe01592010-04-27 15:01:07 +0000225 struct net_bridge_port *port, *lport, *rport;
226
Herbert Xu5cb5e942010-02-27 19:41:46 +0000227 lport = p ? p->port : NULL;
228 rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) :
229 NULL;
230
231 port = (unsigned long)lport > (unsigned long)rport ?
232 lport : rport;
233
234 prev = maybe_deliver(prev, port, skb, __packet_hook);
235 if (IS_ERR(prev))
236 goto out;
237
238 if ((unsigned long)lport >= (unsigned long)port)
stephen hemminger83f6a742010-04-27 15:01:06 +0000239 p = rcu_dereference(p->next);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000240 if ((unsigned long)rport >= (unsigned long)port)
Eric Dumazete8051682010-11-15 06:38:10 +0000241 rp = rcu_dereference(hlist_next_rcu(rp));
Herbert Xu5cb5e942010-02-27 19:41:46 +0000242 }
243
244 if (!prev)
245 goto out;
246
247 if (skb0)
248 deliver_clone(prev, skb, __packet_hook);
249 else
250 __packet_hook(prev, skb);
251 return;
252
253out:
254 if (!skb0)
255 kfree_skb(skb);
256}
257
258/* called with rcu_read_lock */
259void br_multicast_deliver(struct net_bridge_mdb_entry *mdst,
260 struct sk_buff *skb)
261{
262 br_multicast_flood(mdst, skb, NULL, __br_deliver);
263}
264
265/* called with rcu_read_lock */
266void br_multicast_forward(struct net_bridge_mdb_entry *mdst,
267 struct sk_buff *skb, struct sk_buff *skb2)
268{
269 br_multicast_flood(mdst, skb, skb2, __br_forward);
270}
271#endif