VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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_rcu(&f->hlist);
77         if (!f->is_static)
78                 list_del(&f->u.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         spin_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         spin_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         spin_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                 unsigned long expires;
135
136                 f = list_entry(l, struct net_bridge_fdb_entry, u.age_list);
137                 expires = f->ageing_timer + delay;
138
139                 if (time_before_eq(expires, jiffies)) {
140                         WARN_ON(f->is_static);
141                         pr_debug("expire age %lu jiffies %lu\n",
142                                  f->ageing_timer, jiffies);
143                         fdb_delete(f);
144                 } else {
145                         mod_timer(&br->gc_timer, expires);
146                         break;
147                 }
148         }
149         spin_unlock_bh(&br->hash_lock);
150 }
151
152 void br_fdb_delete_by_port(struct net_bridge *br, struct net_bridge_port *p)
153 {
154         int i;
155
156         spin_lock_bh(&br->hash_lock);
157         for (i = 0; i < BR_HASH_SIZE; i++) {
158                 struct hlist_node *h, *g;
159                 
160                 hlist_for_each_safe(h, g, &br->hash[i]) {
161                         struct net_bridge_fdb_entry *f
162                                 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
163                         if (f->dst != p) 
164                                 continue;
165
166                         /*
167                          * if multiple ports all have the same device address
168                          * then when one port is deleted, assign
169                          * the local entry to other port
170                          */
171                         if (f->is_local) {
172                                 struct net_bridge_port *op;
173                                 list_for_each_entry(op, &br->port_list, list) {
174                                         if (op != p && 
175                                             !memcmp(op->dev->dev_addr,
176                                                     f->addr.addr, ETH_ALEN)) {
177                                                 f->dst = op;
178                                                 goto skip_delete;
179                                         }
180                                 }
181                         }
182
183                         fdb_delete(f);
184                 skip_delete: ;
185                 }
186         }
187         spin_unlock_bh(&br->hash_lock);
188 }
189
190 /* No locking or refcounting, assumes caller has no preempt (rcu_read_lock) */
191 struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
192                                           const unsigned char *addr)
193 {
194         struct hlist_node *h;
195         struct net_bridge_fdb_entry *fdb;
196
197         hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
198                 if (!memcmp(fdb->addr.addr, addr, ETH_ALEN)) {
199                         if (unlikely(has_expired(br, fdb)))
200                                 break;
201                         return fdb;
202                 }
203         }
204
205         return NULL;
206 }
207
208 /* Interface used by ATM hook that keeps a ref count */
209 struct net_bridge_fdb_entry *br_fdb_get(struct net_bridge *br, 
210                                         unsigned char *addr)
211 {
212         struct net_bridge_fdb_entry *fdb;
213
214         rcu_read_lock();
215         fdb = __br_fdb_get(br, addr);
216         if (fdb) 
217                 atomic_inc(&fdb->use_count);
218         rcu_read_unlock();
219         return fdb;
220 }
221
222 static void fdb_rcu_free(struct rcu_head *head)
223 {
224         struct net_bridge_fdb_entry *ent
225                 = container_of(head, struct net_bridge_fdb_entry, u.rcu);
226         kmem_cache_free(br_fdb_cache, ent);
227 }
228
229 /* Set entry up for deletion with RCU  */
230 void br_fdb_put(struct net_bridge_fdb_entry *ent)
231 {
232         if (atomic_dec_and_test(&ent->use_count))
233                 call_rcu(&ent->u.rcu, fdb_rcu_free);
234 }
235
236 /*
237  * Fill buffer with forwarding table records in 
238  * the API format.
239  */
240 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
241                    unsigned long maxnum, unsigned long skip)
242 {
243         struct __fdb_entry *fe = buf;
244         int i, num = 0;
245         struct hlist_node *h;
246         struct net_bridge_fdb_entry *f;
247
248         memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
249
250         rcu_read_lock();
251         for (i = 0; i < BR_HASH_SIZE; i++) {
252                 hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
253                         if (num >= maxnum)
254                                 goto out;
255
256                         if (has_expired(br, f)) 
257                                 continue;
258
259                         if (skip) {
260                                 --skip;
261                                 continue;
262                         }
263
264                         /* convert from internal format to API */
265                         memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
266                         fe->port_no = f->dst->port_no;
267                         fe->is_local = f->is_local;
268                         if (!f->is_static)
269                                 fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer);
270                         ++fe;
271                         ++num;
272                 }
273         }
274
275  out:
276         rcu_read_unlock();
277
278         return num;
279 }
280
281 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
282                   const unsigned char *addr, int is_local)
283 {
284         struct hlist_node *h;
285         struct net_bridge_fdb_entry *fdb;
286         int hash = br_mac_hash(addr);
287
288         if (!is_valid_ether_addr(addr))
289                 return -EADDRNOTAVAIL;
290
291         hlist_for_each_entry(fdb, h, &br->hash[hash], hlist) {
292                 if (!memcmp(fdb->addr.addr, addr, ETH_ALEN)) {
293                         /* attempt to update an entry for a local interface */
294                         if (fdb->is_local) {
295                                 /* it is okay to have multiple ports with same 
296                                  * address, just don't allow to be spoofed.
297                                  */
298                                 if (is_local) 
299                                         return 0;
300
301                                 if (net_ratelimit()) 
302                                         printk(KERN_WARNING "%s: received packet with "
303                                                " own address as source address\n",
304                                                source->dev->name);
305                                 return -EEXIST;
306                         }
307
308                         if (is_local) {
309                                 printk(KERN_WARNING "%s adding interface with same address "
310                                        "as a received packet\n",
311                                        source->dev->name);
312                                 goto update;
313                         }
314
315                         if (fdb->is_static)
316                                 return 0;
317
318                         /* move to end of age list */
319                         list_del(&fdb->u.age_list);
320                         goto update;
321                 }
322         }
323
324         fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
325         if (!fdb)
326                 return ENOMEM;
327
328         memcpy(fdb->addr.addr, addr, ETH_ALEN);
329         atomic_set(&fdb->use_count, 1);
330         hlist_add_head_rcu(&fdb->hlist, &br->hash[hash]);
331
332         if (!timer_pending(&br->gc_timer)) {
333                 br->gc_timer.expires = jiffies + hold_time(br);
334                 add_timer(&br->gc_timer);
335         }
336
337  update:
338         fdb->dst = source;
339         fdb->is_local = is_local;
340         fdb->is_static = is_local;
341         fdb->ageing_timer = jiffies;
342         if (!is_local) 
343                 list_add_tail(&fdb->u.age_list, &br->age_list);
344
345         return 0;
346 }
347
348 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
349                   const unsigned char *addr, int is_local)
350 {
351         int ret;
352
353         spin_lock_bh(&br->hash_lock);
354         ret = fdb_insert(br, source, addr, is_local);
355         spin_unlock_bh(&br->hash_lock);
356         return ret;
357 }