ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 #ifdef CONFIG_BRIDGE_NETFILTER
35         /* ip_refrag calls ip_fragment, which doesn't copy the MAC header. */
36         nf_bridge_maybe_copy_header(skb);
37 #endif
38         skb_push(skb, ETH_HLEN);
39
40         dev_queue_xmit(skb);
41
42         return 0;
43 }
44
45 int br_forward_finish(struct sk_buff *skb)
46 {
47         NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
48                         br_dev_queue_push_xmit);
49
50         return 0;
51 }
52
53 static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
54 {
55         skb->dev = to->dev;
56 #ifdef CONFIG_NETFILTER_DEBUG
57         skb->nf_debug = 0;
58 #endif
59         NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
60                         br_forward_finish);
61 }
62
63 static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
64 {
65         struct net_device *indev;
66
67         indev = skb->dev;
68         skb->dev = to->dev;
69         skb->ip_summed = CHECKSUM_NONE;
70
71         NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
72                         br_forward_finish);
73 }
74
75 /* called with rcu_read_lock */
76 void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
77 {
78         if (should_deliver(to, skb)) {
79                 __br_deliver(to, skb);
80                 return;
81         }
82
83         kfree_skb(skb);
84 }
85
86 /* called with rcu_read_lock */
87 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
88 {
89         if (should_deliver(to, skb)) {
90                 __br_forward(to, skb);
91                 return;
92         }
93
94         kfree_skb(skb);
95 }
96
97 /* called under bridge lock */
98 static void br_flood(struct net_bridge *br, struct sk_buff *skb, int clone,
99         void (*__packet_hook)(const struct net_bridge_port *p, 
100                               struct sk_buff *skb))
101 {
102         struct net_bridge_port *p;
103         struct net_bridge_port *prev;
104
105         if (clone) {
106                 struct sk_buff *skb2;
107
108                 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
109                         br->statistics.tx_dropped++;
110                         return;
111                 }
112
113                 skb = skb2;
114         }
115
116         prev = NULL;
117
118         list_for_each_entry_rcu(p, &br->port_list, list) {
119                 if (should_deliver(p, skb)) {
120                         if (prev != NULL) {
121                                 struct sk_buff *skb2;
122
123                                 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
124                                         br->statistics.tx_dropped++;
125                                         kfree_skb(skb);
126                                         return;
127                                 }
128
129                                 __packet_hook(prev, skb2);
130                         }
131
132                         prev = p;
133                 }
134         }
135
136         if (prev != NULL) {
137                 __packet_hook(prev, skb);
138                 return;
139         }
140
141         kfree_skb(skb);
142 }
143
144
145 /* called with rcu_read_lock */
146 void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, int clone)
147 {
148         br_flood(br, skb, clone, __br_deliver);
149 }
150
151 /* called under bridge lock */
152 void br_flood_forward(struct net_bridge *br, struct sk_buff *skb, int clone)
153 {
154         br_flood(br, skb, clone, __br_forward);
155 }