blob: 7a241c396981eebb9ee1357daf88e7d18f3f8db8 [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>
18#include <linux/skbuff.h>
Stephen Hemminger85ca7192006-04-26 02:39:19 -070019#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/netfilter_bridge.h>
21#include "br_private.h"
22
David S. Miller87faf3c2010-03-16 14:37:47 -070023static int deliver_clone(const struct net_bridge_port *prev,
24 struct sk_buff *skb,
Michael Braun7f7708f2010-03-16 00:26:22 -070025 void (*__packet_hook)(const struct net_bridge_port *p,
26 struct sk_buff *skb));
27
Stephen Hemminger9ef513b2006-05-25 15:58:54 -070028/* Don't forward packets to originating port or forwarding diasabled */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090029static inline int should_deliver(const struct net_bridge_port *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 const struct sk_buff *skb)
31{
Fischer, Anna3982d3d2009-08-13 06:55:16 +000032 return (((p->flags & BR_HAIRPIN_MODE) || skb->dev != p->dev) &&
33 p->state == BR_STATE_FORWARDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
35
Stephen Hemminger85ca7192006-04-26 02:39:19 -070036static inline unsigned packet_length(const struct sk_buff *skb)
37{
38 return skb->len - (skb->protocol == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
39}
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041int br_dev_queue_push_xmit(struct sk_buff *skb)
42{
Herbert Xu79671682006-06-22 02:40:14 -070043 /* drop mtu oversized packets except gso */
Herbert Xu89114af2006-07-08 13:34:32 -070044 if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 kfree_skb(skb);
46 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 /* ip_refrag calls ip_fragment, doesn't copy the MAC header. */
Stephen Hemminger3a138132006-08-26 20:28:30 -070048 if (nf_bridge_maybe_copy_header(skb))
49 kfree_skb(skb);
Stephen Hemminger073176212006-08-29 17:48:17 -070050 else {
Stephen Hemminger3a138132006-08-26 20:28:30 -070051 skb_push(skb, ETH_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Stephen Hemminger3a138132006-08-26 20:28:30 -070053 dev_queue_xmit(skb);
54 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
56
57 return 0;
58}
59
60int br_forward_finish(struct sk_buff *skb)
61{
Stephen Hemminger9ef513b2006-05-25 15:58:54 -070062 return NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
63 br_dev_queue_push_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
68{
69 skb->dev = to->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
71 br_forward_finish);
72}
73
74static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
75{
76 struct net_device *indev;
77
Herbert Xu4906f992009-02-09 15:07:18 -080078 if (skb_warn_if_lro(skb)) {
79 kfree_skb(skb);
80 return;
81 }
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 indev = skb->dev;
84 skb->dev = to->dev;
Herbert Xu35fc92a2007-03-26 23:22:20 -070085 skb_forward_csum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
88 br_forward_finish);
89}
90
91/* called with rcu_read_lock */
92void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
93{
94 if (should_deliver(to, skb)) {
95 __br_deliver(to, skb);
96 return;
97 }
98
99 kfree_skb(skb);
100}
101
102/* called with rcu_read_lock */
Michael Braun7f7708f2010-03-16 00:26:22 -0700103void br_forward(const struct net_bridge_port *to, struct sk_buff *skb, struct sk_buff *skb0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Herbert Xu4906f992009-02-09 15:07:18 -0800105 if (should_deliver(to, skb)) {
Michael Braun7f7708f2010-03-16 00:26:22 -0700106 if (skb0)
107 deliver_clone(to, skb, __br_forward);
108 else
109 __br_forward(to, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return;
111 }
112
Michael Braun7f7708f2010-03-16 00:26:22 -0700113 if (!skb0)
114 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
David S. Miller87faf3c2010-03-16 14:37:47 -0700117static int deliver_clone(const struct net_bridge_port *prev,
118 struct sk_buff *skb,
Herbert Xu025d89c2010-02-27 19:41:43 +0000119 void (*__packet_hook)(const struct net_bridge_port *p,
120 struct sk_buff *skb))
121{
122 skb = skb_clone(skb, GFP_ATOMIC);
123 if (!skb) {
124 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
125
126 dev->stats.tx_dropped++;
127 return -ENOMEM;
128 }
129
130 __packet_hook(prev, skb);
131 return 0;
132}
133
134static struct net_bridge_port *maybe_deliver(
135 struct net_bridge_port *prev, struct net_bridge_port *p,
136 struct sk_buff *skb,
137 void (*__packet_hook)(const struct net_bridge_port *p,
138 struct sk_buff *skb))
139{
140 int err;
141
142 if (!should_deliver(p, skb))
143 return prev;
144
145 if (!prev)
146 goto out;
147
148 err = deliver_clone(prev, skb, __packet_hook);
149 if (err)
150 return ERR_PTR(err);
151
152out:
153 return p;
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* called under bridge lock */
Herbert Xue081e1e2007-09-16 16:20:48 -0700157static void br_flood(struct net_bridge *br, struct sk_buff *skb,
Herbert Xub33084b2010-02-27 19:41:41 +0000158 struct sk_buff *skb0,
159 void (*__packet_hook)(const struct net_bridge_port *p,
160 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct net_bridge_port *p;
163 struct net_bridge_port *prev;
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 prev = NULL;
166
167 list_for_each_entry_rcu(p, &br->port_list, list) {
Herbert Xu025d89c2010-02-27 19:41:43 +0000168 prev = maybe_deliver(prev, p, skb, __packet_hook);
169 if (IS_ERR(prev))
170 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172
Herbert Xub33084b2010-02-27 19:41:41 +0000173 if (!prev)
174 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Herbert Xu025d89c2010-02-27 19:41:43 +0000176 if (skb0)
177 deliver_clone(prev, skb, __packet_hook);
178 else
179 __packet_hook(prev, skb);
Herbert Xub33084b2010-02-27 19:41:41 +0000180 return;
181
182out:
183 if (!skb0)
184 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187
188/* called with rcu_read_lock */
Herbert Xue081e1e2007-09-16 16:20:48 -0700189void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Herbert Xub33084b2010-02-27 19:41:41 +0000191 br_flood(br, skb, NULL, __br_deliver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/* called under bridge lock */
Herbert Xub33084b2010-02-27 19:41:41 +0000195void br_flood_forward(struct net_bridge *br, struct sk_buff *skb,
196 struct sk_buff *skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Herbert Xub33084b2010-02-27 19:41:41 +0000198 br_flood(br, skb, skb2, __br_forward);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
Herbert Xu5cb5e942010-02-27 19:41:46 +0000200
201#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
202/* called with rcu_read_lock */
203static void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
204 struct sk_buff *skb, struct sk_buff *skb0,
205 void (*__packet_hook)(
206 const struct net_bridge_port *p,
207 struct sk_buff *skb))
208{
209 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
210 struct net_bridge *br = netdev_priv(dev);
211 struct net_bridge_port *port;
212 struct net_bridge_port *lport, *rport;
213 struct net_bridge_port *prev;
214 struct net_bridge_port_group *p;
215 struct hlist_node *rp;
216
217 prev = NULL;
218
219 rp = br->router_list.first;
220 p = mdst ? mdst->ports : NULL;
221 while (p || rp) {
222 lport = p ? p->port : NULL;
223 rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) :
224 NULL;
225
226 port = (unsigned long)lport > (unsigned long)rport ?
227 lport : rport;
228
229 prev = maybe_deliver(prev, port, skb, __packet_hook);
230 if (IS_ERR(prev))
231 goto out;
232
233 if ((unsigned long)lport >= (unsigned long)port)
234 p = p->next;
235 if ((unsigned long)rport >= (unsigned long)port)
236 rp = rp->next;
237 }
238
239 if (!prev)
240 goto out;
241
242 if (skb0)
243 deliver_clone(prev, skb, __packet_hook);
244 else
245 __packet_hook(prev, skb);
246 return;
247
248out:
249 if (!skb0)
250 kfree_skb(skb);
251}
252
253/* called with rcu_read_lock */
254void br_multicast_deliver(struct net_bridge_mdb_entry *mdst,
255 struct sk_buff *skb)
256{
257 br_multicast_flood(mdst, skb, NULL, __br_deliver);
258}
259
260/* called with rcu_read_lock */
261void br_multicast_forward(struct net_bridge_mdb_entry *mdst,
262 struct sk_buff *skb, struct sk_buff *skb2)
263{
264 br_multicast_flood(mdst, skb, skb2, __br_forward);
265}
266#endif