import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / net / bridge / br_input.c
1 /*
2  *      Handle incoming frames
3  *      Linux ethernet bridge
4  *
5  *      Authors:
6  *      Lennert Buytenhek               <buytenh@gnu.org>
7  *
8  *      $Id: br_input.c,v 1.9.2.1 2001/12/24 04:50:05 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/etherdevice.h>
19 #include <linux/if_bridge.h>
20 #include <linux/netfilter_bridge.h>
21 #include "br_private.h"
22
23 unsigned char bridge_ula[6] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
24
25 static int br_pass_frame_up_finish(struct sk_buff *skb)
26 {
27         netif_rx(skb);
28
29         return 0;
30 }
31
32 static void br_pass_frame_up(struct net_bridge *br, struct sk_buff *skb)
33 {
34         struct net_device *indev;
35
36         br->statistics.rx_packets++;
37         br->statistics.rx_bytes += skb->len;
38
39         indev = skb->dev;
40         skb->dev = &br->dev;
41         skb->pkt_type = PACKET_HOST;
42         skb_push(skb, ETH_HLEN);
43         skb->protocol = eth_type_trans(skb, &br->dev);
44
45         NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_IN, skb, indev, NULL,
46                         br_pass_frame_up_finish);
47 }
48
49 static int br_handle_frame_finish(struct sk_buff *skb)
50 {
51         struct net_bridge *br;
52         unsigned char *dest;
53         struct net_bridge_fdb_entry *dst;
54         struct net_bridge_port *p;
55         int passedup;
56
57         dest = skb->mac.ethernet->h_dest;
58
59         p = skb->dev->br_port;
60         if (p == NULL)
61                 goto err_nolock;
62
63         br = p->br;
64         read_lock(&br->lock);
65         if (skb->dev->br_port == NULL)
66                 goto err;
67
68         passedup = 0;
69         if (br->dev.flags & IFF_PROMISC) {
70                 struct sk_buff *skb2;
71
72                 skb2 = skb_clone(skb, GFP_ATOMIC);
73                 if (skb2 != NULL) {
74                         passedup = 1;
75                         br_pass_frame_up(br, skb2);
76                 }
77         }
78
79         if (dest[0] & 1) {
80                 br_flood_forward(br, skb, !passedup);
81                 if (!passedup)
82                         br_pass_frame_up(br, skb);
83                 goto out;
84         }
85
86         dst = br_fdb_get(br, dest);
87         if (dst != NULL && dst->is_local) {
88                 if (!passedup)
89                         br_pass_frame_up(br, skb);
90                 else
91                         kfree_skb(skb);
92                 br_fdb_put(dst);
93                 goto out;
94         }
95
96         if (dst != NULL) {
97                 br_forward(dst->dst, skb);
98                 br_fdb_put(dst);
99                 goto out;
100         }
101
102         br_flood_forward(br, skb, 0);
103
104 out:
105         read_unlock(&br->lock);
106         return 0;
107
108 err:
109         read_unlock(&br->lock);
110 err_nolock:
111         kfree_skb(skb);
112         return 0;
113 }
114
115 void br_handle_frame(struct sk_buff *skb)
116 {
117         struct net_bridge *br;
118         unsigned char *dest;
119         struct net_bridge_port *p;
120
121         dest = skb->mac.ethernet->h_dest;
122
123         p = skb->dev->br_port;
124         if (p == NULL)
125                 goto err_nolock;
126
127         br = p->br;
128         read_lock(&br->lock);
129         if (skb->dev->br_port == NULL)
130                 goto err;
131
132         if (!(br->dev.flags & IFF_UP) ||
133             p->state == BR_STATE_DISABLED)
134                 goto err;
135
136         if (skb->mac.ethernet->h_source[0] & 1)
137                 goto err;
138
139         if (p->state == BR_STATE_LEARNING ||
140             p->state == BR_STATE_FORWARDING)
141                 br_fdb_insert(br, p, skb->mac.ethernet->h_source, 0);
142
143         if (br->stp_enabled &&
144             !memcmp(dest, bridge_ula, 5) &&
145             !(dest[5] & 0xF0))
146                 goto handle_special_frame;
147
148         if (p->state == BR_STATE_FORWARDING) {
149                 NF_HOOK(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
150                         br_handle_frame_finish);
151                 read_unlock(&br->lock);
152                 return;
153         }
154
155 err:
156         read_unlock(&br->lock);
157 err_nolock:
158         kfree_skb(skb);
159         return;
160
161 handle_special_frame:
162         if (!dest[5]) {
163                 NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_IN, skb, skb->dev,NULL,
164                         br_stp_handle_bpdu);
165                 read_unlock(&br->lock);
166                 return;
167         }
168
169         read_unlock(&br->lock);
170         kfree_skb(skb);
171 }