Merge branch 'perf-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / bridge / br_forward.c
blobbc1704ac6cd9f68f2b73e36443a3c8162b5f5fe1
1 /*
2 * Forwarding decision
3 * Linux ethernet bridge
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
8 * 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.
14 #include <linux/kernel.h>
15 #include <linux/netdevice.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_vlan.h>
18 #include <linux/netfilter_bridge.h>
19 #include "br_private.h"
21 /* Don't forward packets to originating port or forwarding diasabled */
22 static inline int should_deliver(const struct net_bridge_port *p,
23 const struct sk_buff *skb)
25 return (((p->flags & BR_HAIRPIN_MODE) || skb->dev != p->dev) &&
26 p->state == BR_STATE_FORWARDING);
29 static inline unsigned packet_length(const struct sk_buff *skb)
31 return skb->len - (skb->protocol == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
34 int br_dev_queue_push_xmit(struct sk_buff *skb)
36 /* drop mtu oversized packets except gso */
37 if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb))
38 kfree_skb(skb);
39 else {
40 /* ip_refrag calls ip_fragment, doesn't copy the MAC header. */
41 if (nf_bridge_maybe_copy_header(skb))
42 kfree_skb(skb);
43 else {
44 skb_push(skb, ETH_HLEN);
46 dev_queue_xmit(skb);
50 return 0;
53 int br_forward_finish(struct sk_buff *skb)
55 return NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
56 br_dev_queue_push_xmit);
60 static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
62 skb->dev = to->dev;
63 NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
64 br_forward_finish);
67 static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
69 struct net_device *indev;
71 if (skb_warn_if_lro(skb)) {
72 kfree_skb(skb);
73 return;
76 indev = skb->dev;
77 skb->dev = to->dev;
78 skb_forward_csum(skb);
80 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
81 br_forward_finish);
84 /* called with rcu_read_lock */
85 void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
87 if (should_deliver(to, skb)) {
88 __br_deliver(to, skb);
89 return;
92 kfree_skb(skb);
95 /* called with rcu_read_lock */
96 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
98 if (should_deliver(to, skb)) {
99 __br_forward(to, skb);
100 return;
103 kfree_skb(skb);
106 /* called under bridge lock */
107 static void br_flood(struct net_bridge *br, struct sk_buff *skb,
108 void (*__packet_hook)(const struct net_bridge_port *p,
109 struct sk_buff *skb))
111 struct net_bridge_port *p;
112 struct net_bridge_port *prev;
114 prev = NULL;
116 list_for_each_entry_rcu(p, &br->port_list, list) {
117 if (should_deliver(p, skb)) {
118 if (prev != NULL) {
119 struct sk_buff *skb2;
121 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
122 br->dev->stats.tx_dropped++;
123 kfree_skb(skb);
124 return;
127 __packet_hook(prev, skb2);
130 prev = p;
134 if (prev != NULL) {
135 __packet_hook(prev, skb);
136 return;
139 kfree_skb(skb);
143 /* called with rcu_read_lock */
144 void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb)
146 br_flood(br, skb, __br_deliver);
149 /* called under bridge lock */
150 void br_flood_forward(struct net_bridge *br, struct sk_buff *skb)
152 br_flood(br, skb, __br_forward);