Merge to Fedora kernel-2.6.17-1.2187_FC5 patched with stable patch-2.6.17.13-vs2...
[linux-2.6.git] / net / bridge / br_forward.c
1 /*
2  *      Forwarding decision
3  *      Linux ethernet bridge
4  *
5  *      Authors:
6  *      Lennert Buytenhek               <buytenh@gnu.org>
7  *
8  *      $Id: br_forward.c,v 1.4 2001/08/14 22:05:57 davem Exp $
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/netdevice.h>
18 #include <linux/skbuff.h>
19 #include <linux/if_vlan.h>
20 #include <linux/netfilter_bridge.h>
21 #include "br_private.h"
22
23 static inline int should_deliver(const struct net_bridge_port *p, 
24                                  const struct sk_buff *skb)
25 {
26         if (skb->dev == p->dev ||
27             p->state != BR_STATE_FORWARDING)
28                 return 0;
29
30         return 1;
31 }
32
33 static inline unsigned packet_length(const struct sk_buff *skb)
34 {
35         return skb->len - (skb->protocol == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
36 }
37
38 int br_dev_queue_push_xmit(struct sk_buff *skb)
39 {
40         /* drop mtu oversized packets except tso */
41         if (skb->len > skb->dev->mtu && !skb_is_gso(skb))
42                 kfree_skb(skb);
43         else {
44 #ifdef CONFIG_BRIDGE_NETFILTER
45                 /* ip_refrag calls ip_fragment, doesn't copy the MAC header. */
46                 if (nf_bridge_maybe_copy_header(skb))
47                         kfree_skb(skb);
48                 else
49 #endif
50                 {
51                         skb_push(skb, ETH_HLEN);
52
53                         dev_queue_xmit(skb);
54                 }
55         }
56
57         return 0;
58 }
59
60 int br_forward_finish(struct sk_buff *skb)
61 {
62         NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
63                         br_dev_queue_push_xmit);
64
65         return 0;
66 }
67
68 static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
69 {
70         skb->dev = to->dev;
71         NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
72                         br_forward_finish);
73 }
74
75 static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
76 {
77         struct net_device *indev;
78
79         indev = skb->dev;
80         skb->dev = to->dev;
81         skb->ip_summed = CHECKSUM_NONE;
82
83         NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
84                         br_forward_finish);
85 }
86
87 /* called with rcu_read_lock */
88 void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
89 {
90         if (should_deliver(to, skb)) {
91                 __br_deliver(to, skb);
92                 return;
93         }
94
95         kfree_skb(skb);
96 }
97
98 /* called with rcu_read_lock */
99 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
100 {
101         if (should_deliver(to, skb)) {
102                 __br_forward(to, skb);
103                 return;
104         }
105
106         kfree_skb(skb);
107 }
108
109 /* called under bridge lock */
110 static void br_flood(struct net_bridge *br, struct sk_buff *skb, int clone,
111         void (*__packet_hook)(const struct net_bridge_port *p, 
112                               struct sk_buff *skb))
113 {
114         struct net_bridge_port *p;
115         struct net_bridge_port *prev;
116
117         if (clone) {
118                 struct sk_buff *skb2;
119
120                 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
121                         br->statistics.tx_dropped++;
122                         return;
123                 }
124
125                 skb = skb2;
126         }
127
128         prev = NULL;
129
130         list_for_each_entry_rcu(p, &br->port_list, list) {
131                 if (should_deliver(p, skb)) {
132                         if (prev != NULL) {
133                                 struct sk_buff *skb2;
134
135                                 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
136                                         br->statistics.tx_dropped++;
137                                         kfree_skb(skb);
138                                         return;
139                                 }
140
141                                 __packet_hook(prev, skb2);
142                         }
143
144                         prev = p;
145                 }
146         }
147
148         if (prev != NULL) {
149                 __packet_hook(prev, skb);
150                 return;
151         }
152
153         kfree_skb(skb);
154 }
155
156
157 /* called with rcu_read_lock */
158 void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, int clone)
159 {
160         br_flood(br, skb, clone, __br_deliver);
161 }
162
163 /* called under bridge lock */
164 void br_flood_forward(struct net_bridge *br, struct sk_buff *skb, int clone)
165 {
166         br_flood(br, skb, clone, __br_forward);
167 }