blob: 15abef7349f3e7836793ca45366ec70cb6d8e41b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/netdevice.h>
17#include <linux/skbuff.h>
Stephen Hemminger85ca7192006-04-26 02:39:19 -070018#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/netfilter_bridge.h>
20#include "br_private.h"
21
David S. Miller87faf3c2010-03-16 14:37:47 -070022static int deliver_clone(const struct net_bridge_port *prev,
23 struct sk_buff *skb,
Michael Braun7f7708f2010-03-16 00:26:22 -070024 void (*__packet_hook)(const struct net_bridge_port *p,
25 struct sk_buff *skb));
26
Stephen Hemminger9ef513b2006-05-25 15:58:54 -070027/* Don't forward packets to originating port or forwarding diasabled */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090028static inline int should_deliver(const struct net_bridge_port *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 const struct sk_buff *skb)
30{
Fischer, Anna3982d3d2009-08-13 06:55:16 +000031 return (((p->flags & BR_HAIRPIN_MODE) || skb->dev != p->dev) &&
32 p->state == BR_STATE_FORWARDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
Stephen Hemminger85ca7192006-04-26 02:39:19 -070035static inline unsigned packet_length(const struct sk_buff *skb)
36{
37 return skb->len - (skb->protocol == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
38}
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040int br_dev_queue_push_xmit(struct sk_buff *skb)
41{
Herbert Xu79671682006-06-22 02:40:14 -070042 /* drop mtu oversized packets except gso */
Herbert Xu89114af2006-07-08 13:34:32 -070043 if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 kfree_skb(skb);
45 else {
Bart De Schuymere26c28e2010-04-13 11:41:39 +020046 /* ip_fragment doesn't copy the MAC header */
Stephen Hemminger3a138132006-08-26 20:28:30 -070047 if (nf_bridge_maybe_copy_header(skb))
48 kfree_skb(skb);
Stephen Hemminger07317622006-08-29 17:48:17 -070049 else {
Stephen Hemminger3a138132006-08-26 20:28:30 -070050 skb_push(skb, ETH_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Stephen Hemminger3a138132006-08-26 20:28:30 -070052 dev_queue_xmit(skb);
53 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 }
55
56 return 0;
57}
58
59int br_forward_finish(struct sk_buff *skb)
60{
Jan Engelhardt713aefa2010-03-23 04:07:21 +010061 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
Stephen Hemminger9ef513b2006-05-25 15:58:54 -070062 br_dev_queue_push_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
66static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
67{
68 skb->dev = to->dev;
Jan Engelhardt713aefa2010-03-23 04:07:21 +010069 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
70 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
74{
75 struct net_device *indev;
76
Herbert Xu4906f992009-02-09 15:07:18 -080077 if (skb_warn_if_lro(skb)) {
78 kfree_skb(skb);
79 return;
80 }
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 indev = skb->dev;
83 skb->dev = to->dev;
Herbert Xu35fc92a2007-03-26 23:22:20 -070084 skb_forward_csum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Jan Engelhardt713aefa2010-03-23 04:07:21 +010086 NF_HOOK(NFPROTO_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
87 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90/* called with rcu_read_lock */
91void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
92{
93 if (should_deliver(to, skb)) {
94 __br_deliver(to, skb);
95 return;
96 }
97
98 kfree_skb(skb);
99}
100
101/* called with rcu_read_lock */
Michael Braun7f7708f2010-03-16 00:26:22 -0700102void br_forward(const struct net_bridge_port *to, struct sk_buff *skb, struct sk_buff *skb0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Herbert Xu4906f992009-02-09 15:07:18 -0800104 if (should_deliver(to, skb)) {
Michael Braun7f7708f2010-03-16 00:26:22 -0700105 if (skb0)
106 deliver_clone(to, skb, __br_forward);
107 else
108 __br_forward(to, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return;
110 }
111
Michael Braun7f7708f2010-03-16 00:26:22 -0700112 if (!skb0)
113 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
David S. Miller87faf3c2010-03-16 14:37:47 -0700116static int deliver_clone(const struct net_bridge_port *prev,
117 struct sk_buff *skb,
Herbert Xu025d89c2010-02-27 19:41:43 +0000118 void (*__packet_hook)(const struct net_bridge_port *p,
119 struct sk_buff *skb))
120{
121 skb = skb_clone(skb, GFP_ATOMIC);
122 if (!skb) {
123 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
124
125 dev->stats.tx_dropped++;
126 return -ENOMEM;
127 }
128
129 __packet_hook(prev, skb);
130 return 0;
131}
132
133static struct net_bridge_port *maybe_deliver(
134 struct net_bridge_port *prev, struct net_bridge_port *p,
135 struct sk_buff *skb,
136 void (*__packet_hook)(const struct net_bridge_port *p,
137 struct sk_buff *skb))
138{
139 int err;
140
141 if (!should_deliver(p, skb))
142 return prev;
143
144 if (!prev)
145 goto out;
146
147 err = deliver_clone(prev, skb, __packet_hook);
148 if (err)
149 return ERR_PTR(err);
150
151out:
152 return p;
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* called under bridge lock */
Herbert Xue081e1e2007-09-16 16:20:48 -0700156static void br_flood(struct net_bridge *br, struct sk_buff *skb,
Herbert Xub33084b2010-02-27 19:41:41 +0000157 struct sk_buff *skb0,
158 void (*__packet_hook)(const struct net_bridge_port *p,
159 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 struct net_bridge_port *p;
162 struct net_bridge_port *prev;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 prev = NULL;
165
166 list_for_each_entry_rcu(p, &br->port_list, list) {
Herbert Xu025d89c2010-02-27 19:41:43 +0000167 prev = maybe_deliver(prev, p, skb, __packet_hook);
168 if (IS_ERR(prev))
169 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171
Herbert Xub33084b2010-02-27 19:41:41 +0000172 if (!prev)
173 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Herbert Xu025d89c2010-02-27 19:41:43 +0000175 if (skb0)
176 deliver_clone(prev, skb, __packet_hook);
177 else
178 __packet_hook(prev, skb);
Herbert Xub33084b2010-02-27 19:41:41 +0000179 return;
180
181out:
182 if (!skb0)
183 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186
187/* called with rcu_read_lock */
Herbert Xue081e1e2007-09-16 16:20:48 -0700188void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Herbert Xub33084b2010-02-27 19:41:41 +0000190 br_flood(br, skb, NULL, __br_deliver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193/* called under bridge lock */
Herbert Xub33084b2010-02-27 19:41:41 +0000194void br_flood_forward(struct net_bridge *br, struct sk_buff *skb,
195 struct sk_buff *skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Herbert Xub33084b2010-02-27 19:41:41 +0000197 br_flood(br, skb, skb2, __br_forward);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
Herbert Xu5cb5e942010-02-27 19:41:46 +0000199
200#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
201/* called with rcu_read_lock */
202static void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
203 struct sk_buff *skb, struct sk_buff *skb0,
204 void (*__packet_hook)(
205 const struct net_bridge_port *p,
206 struct sk_buff *skb))
207{
208 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
209 struct net_bridge *br = netdev_priv(dev);
210 struct net_bridge_port *port;
211 struct net_bridge_port *lport, *rport;
212 struct net_bridge_port *prev;
213 struct net_bridge_port_group *p;
214 struct hlist_node *rp;
215
216 prev = NULL;
217
218 rp = br->router_list.first;
219 p = mdst ? mdst->ports : NULL;
220 while (p || rp) {
221 lport = p ? p->port : NULL;
222 rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) :
223 NULL;
224
225 port = (unsigned long)lport > (unsigned long)rport ?
226 lport : rport;
227
228 prev = maybe_deliver(prev, port, skb, __packet_hook);
229 if (IS_ERR(prev))
230 goto out;
231
232 if ((unsigned long)lport >= (unsigned long)port)
233 p = p->next;
234 if ((unsigned long)rport >= (unsigned long)port)
235 rp = rp->next;
236 }
237
238 if (!prev)
239 goto out;
240
241 if (skb0)
242 deliver_clone(prev, skb, __packet_hook);
243 else
244 __packet_hook(prev, skb);
245 return;
246
247out:
248 if (!skb0)
249 kfree_skb(skb);
250}
251
252/* called with rcu_read_lock */
253void br_multicast_deliver(struct net_bridge_mdb_entry *mdst,
254 struct sk_buff *skb)
255{
256 br_multicast_flood(mdst, skb, NULL, __br_deliver);
257}
258
259/* called with rcu_read_lock */
260void br_multicast_forward(struct net_bridge_mdb_entry *mdst,
261 struct sk_buff *skb, struct sk_buff *skb2)
262{
263 br_multicast_flood(mdst, skb, skb2, __br_forward);
264}
265#endif