patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / net / bridge / br_fdb.c
1 /*
2  *      Forwarding database
3  *      Linux ethernet bridge
4  *
5  *      Authors:
6  *      Lennert Buytenhek               <buytenh@gnu.org>
7  *
8  *      $Id: br_fdb.c,v 1.6 2002/01/17 00:57:07 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/init.h>
18 #include <linux/spinlock.h>
19 #include <linux/times.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <asm/atomic.h>
23 #include "br_private.h"
24
25 static kmem_cache_t *br_fdb_cache;
26 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
27                       const unsigned char *addr, int is_local);
28
29 void __init br_fdb_init(void)
30 {
31         br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
32                                          sizeof(struct net_bridge_fdb_entry),
33                                          0,
34                                          SLAB_HWCACHE_ALIGN, NULL, NULL);
35 }
36
37 void __exit br_fdb_fini(void)
38 {
39         kmem_cache_destroy(br_fdb_cache);
40 }
41
42
43 /* if topology_changing then use forward_delay (default 15 sec)
44  * otherwise keep longer (default 5 minutes)
45  */
46 static __inline__ unsigned long hold_time(const struct net_bridge *br)
47 {
48         return br->topology_change ? br->forward_delay : br->ageing_time;
49 }
50
51 static __inline__ int has_expired(const struct net_bridge *br,
52                                   const struct net_bridge_fdb_entry *fdb)
53 {
54         return !fdb->is_static 
55                 && time_before_eq(fdb->ageing_timer + hold_time(br), jiffies);
56 }
57
58 static __inline__ int br_mac_hash(const unsigned char *mac)
59 {
60         unsigned long x;
61
62         x = mac[0];
63         x = (x << 2) ^ mac[1];
64         x = (x << 2) ^ mac[2];
65         x = (x << 2) ^ mac[3];
66         x = (x << 2) ^ mac[4];
67         x = (x << 2) ^ mac[5];
68
69         x ^= x >> 8;
70
71         return x & (BR_HASH_SIZE - 1);
72 }
73
74 static __inline__ void fdb_delete(struct net_bridge_fdb_entry *f)
75 {
76         hlist_del(&f->hlist);
77         if (!f->is_static)
78                 list_del(&f->age_list);
79
80         br_fdb_put(f);
81 }
82
83 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
84 {
85         struct net_bridge *br = p->br;
86         int i;
87         
88         write_lock_bh(&br->hash_lock);
89
90         /* Search all chains since old address/hash is unknown */
91         for (i = 0; i < BR_HASH_SIZE; i++) {
92                 struct hlist_node *h;
93                 hlist_for_each(h, &br->hash[i]) {
94                         struct net_bridge_fdb_entry *f;
95
96                         f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
97                         if (f->dst == p && f->is_local) {
98                                 /* maybe another port has same hw addr? */
99                                 struct net_bridge_port *op;
100                                 list_for_each_entry(op, &br->port_list, list) {
101                                         if (op != p && 
102                                             !memcmp(op->dev->dev_addr,
103                                                     f->addr.addr, ETH_ALEN)) {
104                                                 f->dst = op;
105                                                 goto insert;
106                                         }
107                                 }
108
109                                 /* delete old one */
110                                 fdb_delete(f);
111                                 goto insert;
112                         }
113                 }
114         }
115  insert:
116         /* insert new address,  may fail if invalid address or dup. */
117         fdb_insert(br, p, newaddr, 1);
118
119
120         write_unlock_bh(&br->hash_lock);
121 }
122
123 void br_fdb_cleanup(unsigned long _data)
124 {
125         struct net_bridge *br = (struct net_bridge *)_data;
126         struct list_head *l, *n;
127         unsigned long delay;
128
129         write_lock_bh(&br->hash_lock);
130         delay = hold_time(br);
131
132         list_for_each_safe(l, n, &br->age_list) {
133                 struct net_bridge_fdb_entry *f
134                         = list_entry(l, struct net_bridge_fdb_entry, age_list);
135                 unsigned long expires = f->ageing_timer + delay;
136
137                 if (time_before_eq(expires, jiffies)) {
138                         WARN_ON(f->is_static);
139                         pr_debug("expire age %lu jiffies %lu\n",
140                                  f->ageing_timer, jiffies);
141                         fdb_delete(f);
142                 } else {
143                         mod_timer(&br->gc_timer, expires);
144                         break;
145                 }
146         }
147         write_unlock_bh(&br->hash_lock);
148 }
149
150 void br_fdb_delete_by_port(struct net_bridge *br, struct net_bridge_port *p)
151 {
152         int i;
153
154         write_lock_bh(&br->hash_lock);
155         for (i = 0; i < BR_HASH_SIZE; i++) {
156                 struct hlist_node *h, *g;
157                 
158                 hlist_for_each_safe(h, g, &br->hash[i]) {
159                         struct net_bridge_fdb_entry *f
160                                 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
161                         if (f->dst != p) 
162                                 continue;
163
164                         /*
165                          * if multiple ports all have the same device address
166                          * then when one port is deleted, assign
167                          * the local entry to other port
168                          */
169                         if (f->is_local) {
170                                 struct net_bridge_port *op;
171                                 list_for_each_entry(op, &br->port_list, list) {
172                                         if (op != p && 
173                                             !memcmp(op->dev->dev_addr,
174                                                     f->addr.addr, ETH_ALEN)) {
175                                                 f->dst = op;
176                                                 goto skip_delete;
177                                         }
178                                 }
179                         }
180
181                         fdb_delete(f);
182                 skip_delete: ;
183                 }
184         }
185         write_unlock_bh(&br->hash_lock);
186 }
187
188 struct net_bridge_fdb_entry *br_fdb_get(struct net_bridge *br, unsigned char *addr)
189 {
190         struct hlist_node *h;
191
192         read_lock_bh(&br->hash_lock);
193                 
194         hlist_for_each(h, &br->hash[br_mac_hash(addr)]) {
195                 struct net_bridge_fdb_entry *fdb
196                         = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
197
198                 if (!memcmp(fdb->addr.addr, addr, ETH_ALEN)) {
199                         if (has_expired(br, fdb))
200                                 goto ret_null;
201
202                         atomic_inc(&fdb->use_count);
203                         read_unlock_bh(&br->hash_lock);
204                         return fdb;
205                 }
206         }
207  ret_null:
208         read_unlock_bh(&br->hash_lock);
209         return NULL;
210 }
211
212 void br_fdb_put(struct net_bridge_fdb_entry *ent)
213 {
214         if (atomic_dec_and_test(&ent->use_count))
215                 kmem_cache_free(br_fdb_cache, ent);
216 }
217
218 /*
219  * Fill buffer with forwarding table records in 
220  * the API format.
221  */
222 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
223                    unsigned long maxnum, unsigned long skip)
224 {
225         struct __fdb_entry *fe = buf;
226         int i, num = 0;
227         struct hlist_node *h;
228         struct net_bridge_fdb_entry *f;
229
230         memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
231
232         read_lock_bh(&br->hash_lock);
233         for (i = 0; i < BR_HASH_SIZE; i++) {
234                 hlist_for_each_entry(f, h, &br->hash[i], hlist) {
235                         if (num >= maxnum)
236                                 goto out;
237
238                         if (has_expired(br, f)) 
239                                 continue;
240
241                         if (skip) {
242                                 --skip;
243                                 continue;
244                         }
245
246                         /* convert from internal format to API */
247                         memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
248                         fe->port_no = f->dst->port_no;
249                         fe->is_local = f->is_local;
250                         if (!f->is_static)
251                                 fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer);
252                         ++fe;
253                         ++num;
254                 }
255         }
256
257  out:
258         read_unlock_bh(&br->hash_lock);
259
260         return num;
261 }
262
263 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
264                   const unsigned char *addr, int is_local)
265 {
266         struct hlist_node *h;
267         struct net_bridge_fdb_entry *fdb;
268         int hash = br_mac_hash(addr);
269
270         if (!is_valid_ether_addr(addr))
271                 return -EADDRNOTAVAIL;
272
273         hlist_for_each_entry(fdb, h, &br->hash[hash], hlist) {
274                 if (!memcmp(fdb->addr.addr, addr, ETH_ALEN)) {
275                         /* attempt to update an entry for a local interface */
276                         if (fdb->is_local) {
277                                 /* it is okay to have multiple ports with same 
278                                  * address, just don't allow to be spoofed.
279                                  */
280                                 if (is_local) 
281                                         return 0;
282
283                                 if (net_ratelimit()) 
284                                         printk(KERN_WARNING "%s: received packet with "
285                                                " own address as source address\n",
286                                                source->dev->name);
287                                 return -EEXIST;
288                         }
289
290                         if (is_local) {
291                                 printk(KERN_WARNING "%s adding interface with same address "
292                                        "as a received packet\n",
293                                        source->dev->name);
294                                 goto update;
295                         }
296
297                         if (fdb->is_static)
298                                 return 0;
299
300                         /* move to end of age list */
301                         list_del(&fdb->age_list);
302                         goto update;
303                 }
304         }
305
306         fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
307         if (!fdb)
308                 return ENOMEM;
309
310         memcpy(fdb->addr.addr, addr, ETH_ALEN);
311         atomic_set(&fdb->use_count, 1);
312         hlist_add_head(&fdb->hlist, &br->hash[hash]);
313
314         if (!timer_pending(&br->gc_timer)) {
315                 br->gc_timer.expires = jiffies + hold_time(br);
316                 add_timer(&br->gc_timer);
317         }
318
319  update:
320         fdb->dst = source;
321         fdb->is_local = is_local;
322         fdb->is_static = is_local;
323         fdb->ageing_timer = jiffies;
324         if (!is_local) 
325                 list_add_tail(&fdb->age_list, &br->age_list);
326
327         return 0;
328 }
329
330 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
331                   const unsigned char *addr, int is_local)
332 {
333         int ret;
334
335         write_lock_bh(&br->hash_lock);
336         ret = fdb_insert(br, source, addr, is_local);
337         write_unlock_bh(&br->hash_lock);
338         return ret;
339 }