dac7f0698193f3804c98353ad6990ea39a767dbb
[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/netfilter_bridge.h>
20 #include "br_private.h"
21
22 static inline int should_deliver(const struct net_bridge_port *p, 
23                                  const struct sk_buff *skb)
24 {
25         if (skb->dev == p->dev ||
26             p->state != BR_STATE_FORWARDING)
27                 return 0;
28
29         return 1;
30 }
31
32 int br_dev_queue_push_xmit(struct sk_buff *skb)
33 {
34         /* drop mtu oversized packets except tso */
35         if (skb->len > skb->dev->mtu && !skb_shinfo(skb)->tso_size)
36                 kfree_skb(skb);
37         else {
38 #ifdef CONFIG_BRIDGE_NETFILTER
39                 /* ip_refrag calls ip_fragment, doesn't copy the MAC header. */
40                 if (nf_bridge_maybe_copy_header(skb))
41                         kfree_skb(skb);
42                 else
43 #endif
44                 {
45                         skb_push(skb, ETH_HLEN);
46
47                         dev_queue_xmit(skb);
48                 }
49         }
50
51         return 0;
52 }
53
54 int br_forward_finish(struct sk_buff *skb)
55 {
56         NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
57                         br_dev_queue_push_xmit);
58
59         return 0;
60 }
61
62 static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
63 {
64         skb->dev = to->dev;
65         NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
66                         br_forward_finish);
67 }
68
69 static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
70 {
71         struct net_device *indev;
72
73         indev = skb->dev;
74         skb->dev = to->dev;
75         skb->ip_summed = CHECKSUM_NONE;
76
77         NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
78                         br_forward_finish);
79 }
80
81 /* called with rcu_read_lock */
82 void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
83 {
84         if (should_deliver(to, skb)) {
85                 __br_deliver(to, skb);
86                 return;
87         }
88
89         kfree_skb(skb);
90 }
91
92 /* called with rcu_read_lock */
93 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
94 {
95         if (should_deliver(to, skb)) {
96                 __br_forward(to, skb);
97                 return;
98         }
99
100         kfree_skb(skb);
101 }
102
103 /* called under bridge lock */
104 static void br_flood(struct net_bridge *br, struct sk_buff *skb, int clone,
105         void (*__packet_hook)(const struct net_bridge_port *p, 
106                               struct sk_buff *skb))
107 {
108         struct net_bridge_port *p;
109         struct net_bridge_port *prev;
110
111         if (clone) {
112                 struct sk_buff *skb2;
113
114                 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
115                         br->statistics.tx_dropped++;
116                         return;
117                 }
118
119                 skb = skb2;
120         }
121
122         prev = NULL;
123
124         list_for_each_entry_rcu(p, &br->port_list, list) {
125                 if (should_deliver(p, skb)) {
126                         if (prev != NULL) {
127                                 struct sk_buff *skb2;
128
129                                 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
130                                         br->statistics.tx_dropped++;
131                                         kfree_skb(skb);
132                                         return;
133                                 }
134
135                                 __packet_hook(prev, skb2);
136                         }
137
138                         prev = p;
139                 }
140         }
141
142         if (prev != NULL) {
143                 __packet_hook(prev, skb);
144                 return;
145         }
146
147         kfree_skb(skb);
148 }
149
150
151 /* called with rcu_read_lock */
152 void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, int clone)
153 {
154         br_flood(br, skb, clone, __br_deliver);
155 }
156
157 /* called under bridge lock */
158 void br_flood_forward(struct net_bridge *br, struct sk_buff *skb, int clone)
159 {
160         br_flood(br, skb, clone, __br_forward);
161 }