clean
[linux-2.4.21-pre4.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.1.1.1 2005/04/11 02:51:12 jack 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/inetdevice.h>
19 #include <linux/skbuff.h>
20 #include <linux/if_bridge.h>
21 #include <linux/netfilter_bridge.h>
22 #include "br_private.h"
23
24 static inline int should_deliver(struct net_bridge_port *p, 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 int __dev_queue_push_xmit(struct sk_buff *skb)
34 {
35         skb_push(skb, ETH_HLEN);
36         dev_queue_xmit(skb);
37
38         return 0;
39 }
40
41 static int __br_forward_finish(struct sk_buff *skb)
42 {
43         NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
44                         __dev_queue_push_xmit);
45
46         return 0;
47 }
48
49 static void __br_deliver(struct net_bridge_port *to, struct sk_buff *skb)
50 {
51         skb->dev = to->dev;
52 #ifdef CONFIG_NETFILTER_DEBUG
53         skb->nf_debug = 0;
54 #endif
55         NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
56                         __br_forward_finish);
57 }
58
59 static void __br_forward(struct net_bridge_port *to, struct sk_buff *skb)
60 {
61         struct net_device *indev;
62
63         indev = skb->dev;
64         skb->dev = to->dev;
65
66         NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
67                         __br_forward_finish);
68 }
69
70 /* called under bridge lock */
71 void br_deliver(struct net_bridge_port *to, struct sk_buff *skb)
72 {
73         if (should_deliver(to, skb)) {
74                 __br_deliver(to, skb);
75                 return;
76         }
77
78         kfree_skb(skb);
79 }
80
81 /* called under bridge lock */
82 void br_forward(struct net_bridge_port *to, struct sk_buff *skb)
83 {
84         if (should_deliver(to, skb)) {
85                 __br_forward(to, skb);
86                 return;
87         }
88
89         kfree_skb(skb);
90 }
91
92 /* called under bridge lock */
93 static void br_flood(struct net_bridge *br, struct sk_buff *skb, int clone,
94         void (*__packet_hook)(struct net_bridge_port *p, struct sk_buff *skb))
95 {
96         struct net_bridge_port *p;
97         struct net_bridge_port *prev;
98
99         if (clone) {
100                 struct sk_buff *skb2;
101
102                 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
103                         br->statistics.tx_dropped++;
104                         return;
105                 }
106
107                 skb = skb2;
108         }
109
110         prev = NULL;
111
112         p = br->port_list;
113         while (p != NULL) {
114                 if (should_deliver(p, skb)) {
115                         if (prev != NULL) {
116                                 struct sk_buff *skb2;
117
118                                 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
119                                         br->statistics.tx_dropped++;
120                                         kfree_skb(skb);
121                                         return;
122                                 }
123
124                                 __packet_hook(prev, skb2);
125                         }
126
127                         prev = p;
128                 }
129
130                 p = p->next;
131         }
132
133         if (prev != NULL) {
134                 __packet_hook(prev, skb);
135                 return;
136         }
137
138         kfree_skb(skb);
139 }
140
141 /* called under bridge lock */
142 void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, int clone)
143 {
144         br_flood(br, skb, clone, __br_deliver);
145 }
146
147 /* called under bridge lock */
148 void br_flood_forward(struct net_bridge *br, struct sk_buff *skb, int clone)
149 {
150         br_flood(br, skb, clone, __br_forward);
151 }