bridge: add sysfs hook to flush forwarding table
[linux-2.6] / 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 <linux/jhash.h>
23 #include <linux/random.h>
24 #include <asm/atomic.h>
25 #include <asm/unaligned.h>
26 #include "br_private.h"
27
28 static struct kmem_cache *br_fdb_cache __read_mostly;
29 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
30                       const unsigned char *addr);
31
32 static u32 fdb_salt __read_mostly;
33
34 void __init br_fdb_init(void)
35 {
36         br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
37                                          sizeof(struct net_bridge_fdb_entry),
38                                          0,
39                                          SLAB_HWCACHE_ALIGN, NULL, NULL);
40         get_random_bytes(&fdb_salt, sizeof(fdb_salt));
41 }
42
43 void __exit br_fdb_fini(void)
44 {
45         kmem_cache_destroy(br_fdb_cache);
46 }
47
48
49 /* if topology_changing then use forward_delay (default 15 sec)
50  * otherwise keep longer (default 5 minutes)
51  */
52 static inline unsigned long hold_time(const struct net_bridge *br)
53 {
54         return br->topology_change ? br->forward_delay : br->ageing_time;
55 }
56
57 static inline int has_expired(const struct net_bridge *br,
58                                   const struct net_bridge_fdb_entry *fdb)
59 {
60         return !fdb->is_static
61                 && time_before_eq(fdb->ageing_timer + hold_time(br), jiffies);
62 }
63
64 static inline int br_mac_hash(const unsigned char *mac)
65 {
66         /* use 1 byte of OUI cnd 3 bytes of NIC */
67         u32 key = get_unaligned((u32 *)(mac + 2));
68         return jhash_1word(key, fdb_salt) & (BR_HASH_SIZE - 1);
69 }
70
71 static inline void fdb_delete(struct net_bridge_fdb_entry *f)
72 {
73         hlist_del_rcu(&f->hlist);
74         br_fdb_put(f);
75 }
76
77 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
78 {
79         struct net_bridge *br = p->br;
80         int i;
81
82         spin_lock_bh(&br->hash_lock);
83
84         /* Search all chains since old address/hash is unknown */
85         for (i = 0; i < BR_HASH_SIZE; i++) {
86                 struct hlist_node *h;
87                 hlist_for_each(h, &br->hash[i]) {
88                         struct net_bridge_fdb_entry *f;
89
90                         f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
91                         if (f->dst == p && f->is_local) {
92                                 /* maybe another port has same hw addr? */
93                                 struct net_bridge_port *op;
94                                 list_for_each_entry(op, &br->port_list, list) {
95                                         if (op != p &&
96                                             !compare_ether_addr(op->dev->dev_addr,
97                                                                 f->addr.addr)) {
98                                                 f->dst = op;
99                                                 goto insert;
100                                         }
101                                 }
102
103                                 /* delete old one */
104                                 fdb_delete(f);
105                                 goto insert;
106                         }
107                 }
108         }
109  insert:
110         /* insert new address,  may fail if invalid address or dup. */
111         fdb_insert(br, p, newaddr);
112
113         spin_unlock_bh(&br->hash_lock);
114 }
115
116 void br_fdb_cleanup(unsigned long _data)
117 {
118         struct net_bridge *br = (struct net_bridge *)_data;
119         unsigned long delay = hold_time(br);
120         int i;
121
122         spin_lock_bh(&br->hash_lock);
123         for (i = 0; i < BR_HASH_SIZE; i++) {
124                 struct net_bridge_fdb_entry *f;
125                 struct hlist_node *h, *n;
126
127                 hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
128                         if (!f->is_static &&
129                             time_before_eq(f->ageing_timer + delay, jiffies))
130                                 fdb_delete(f);
131                 }
132         }
133         spin_unlock_bh(&br->hash_lock);
134
135         mod_timer(&br->gc_timer, jiffies + HZ/10);
136 }
137
138 /* Completely flush all dynamic entries in forwarding database.*/
139 void br_fdb_flush(struct net_bridge *br)
140 {
141         int i;
142
143         spin_lock_bh(&br->hash_lock);
144         for (i = 0; i < BR_HASH_SIZE; i++) {
145                 struct net_bridge_fdb_entry *f;
146                 struct hlist_node *h, *n;
147                 hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
148                         if (!f->is_static)
149                                 fdb_delete(f);
150                 }
151         }
152         spin_unlock_bh(&br->hash_lock);
153 }
154
155 /* Flush all entries refering to a specific port.
156  * if do_all is set also flush static entries
157  */
158 void br_fdb_delete_by_port(struct net_bridge *br,
159                            const struct net_bridge_port *p,
160                            int do_all)
161 {
162         int i;
163
164         spin_lock_bh(&br->hash_lock);
165         for (i = 0; i < BR_HASH_SIZE; i++) {
166                 struct hlist_node *h, *g;
167
168                 hlist_for_each_safe(h, g, &br->hash[i]) {
169                         struct net_bridge_fdb_entry *f
170                                 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
171                         if (f->dst != p)
172                                 continue;
173
174                         if (f->is_static && !do_all)
175                                 continue;
176                         /*
177                          * if multiple ports all have the same device address
178                          * then when one port is deleted, assign
179                          * the local entry to other port
180                          */
181                         if (f->is_local) {
182                                 struct net_bridge_port *op;
183                                 list_for_each_entry(op, &br->port_list, list) {
184                                         if (op != p &&
185                                             !compare_ether_addr(op->dev->dev_addr,
186                                                                 f->addr.addr)) {
187                                                 f->dst = op;
188                                                 goto skip_delete;
189                                         }
190                                 }
191                         }
192
193                         fdb_delete(f);
194                 skip_delete: ;
195                 }
196         }
197         spin_unlock_bh(&br->hash_lock);
198 }
199
200 /* No locking or refcounting, assumes caller has no preempt (rcu_read_lock) */
201 struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
202                                           const unsigned char *addr)
203 {
204         struct hlist_node *h;
205         struct net_bridge_fdb_entry *fdb;
206
207         hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
208                 if (!compare_ether_addr(fdb->addr.addr, addr)) {
209                         if (unlikely(has_expired(br, fdb)))
210                                 break;
211                         return fdb;
212                 }
213         }
214
215         return NULL;
216 }
217
218 /* Interface used by ATM hook that keeps a ref count */
219 struct net_bridge_fdb_entry *br_fdb_get(struct net_bridge *br,
220                                         unsigned char *addr)
221 {
222         struct net_bridge_fdb_entry *fdb;
223
224         rcu_read_lock();
225         fdb = __br_fdb_get(br, addr);
226         if (fdb && !atomic_inc_not_zero(&fdb->use_count))
227                 fdb = NULL;
228         rcu_read_unlock();
229         return fdb;
230 }
231
232 static void fdb_rcu_free(struct rcu_head *head)
233 {
234         struct net_bridge_fdb_entry *ent
235                 = container_of(head, struct net_bridge_fdb_entry, rcu);
236         kmem_cache_free(br_fdb_cache, ent);
237 }
238
239 /* Set entry up for deletion with RCU  */
240 void br_fdb_put(struct net_bridge_fdb_entry *ent)
241 {
242         if (atomic_dec_and_test(&ent->use_count))
243                 call_rcu(&ent->rcu, fdb_rcu_free);
244 }
245
246 /*
247  * Fill buffer with forwarding table records in
248  * the API format.
249  */
250 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
251                    unsigned long maxnum, unsigned long skip)
252 {
253         struct __fdb_entry *fe = buf;
254         int i, num = 0;
255         struct hlist_node *h;
256         struct net_bridge_fdb_entry *f;
257
258         memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
259
260         rcu_read_lock();
261         for (i = 0; i < BR_HASH_SIZE; i++) {
262                 hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
263                         if (num >= maxnum)
264                                 goto out;
265
266                         if (has_expired(br, f))
267                                 continue;
268
269                         if (skip) {
270                                 --skip;
271                                 continue;
272                         }
273
274                         /* convert from internal format to API */
275                         memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
276                         fe->port_no = f->dst->port_no;
277                         fe->is_local = f->is_local;
278                         if (!f->is_static)
279                                 fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer);
280                         ++fe;
281                         ++num;
282                 }
283         }
284
285  out:
286         rcu_read_unlock();
287
288         return num;
289 }
290
291 static inline struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
292                                                     const unsigned char *addr)
293 {
294         struct hlist_node *h;
295         struct net_bridge_fdb_entry *fdb;
296
297         hlist_for_each_entry_rcu(fdb, h, head, hlist) {
298                 if (!compare_ether_addr(fdb->addr.addr, addr))
299                         return fdb;
300         }
301         return NULL;
302 }
303
304 static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
305                                                struct net_bridge_port *source,
306                                                const unsigned char *addr,
307                                                int is_local)
308 {
309         struct net_bridge_fdb_entry *fdb;
310
311         fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
312         if (fdb) {
313                 memcpy(fdb->addr.addr, addr, ETH_ALEN);
314                 atomic_set(&fdb->use_count, 1);
315                 hlist_add_head_rcu(&fdb->hlist, head);
316
317                 fdb->dst = source;
318                 fdb->is_local = is_local;
319                 fdb->is_static = is_local;
320                 fdb->ageing_timer = jiffies;
321         }
322         return fdb;
323 }
324
325 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
326                   const unsigned char *addr)
327 {
328         struct hlist_head *head = &br->hash[br_mac_hash(addr)];
329         struct net_bridge_fdb_entry *fdb;
330
331         if (!is_valid_ether_addr(addr))
332                 return -EINVAL;
333
334         fdb = fdb_find(head, addr);
335         if (fdb) {
336                 /* it is okay to have multiple ports with same
337                  * address, just use the first one.
338                  */
339                 if (fdb->is_local)
340                         return 0;
341
342                 printk(KERN_WARNING "%s adding interface with same address "
343                        "as a received packet\n",
344                        source->dev->name);
345                 fdb_delete(fdb);
346         }
347
348         if (!fdb_create(head, source, addr, 1))
349                 return -ENOMEM;
350
351         return 0;
352 }
353
354 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
355                   const unsigned char *addr)
356 {
357         int ret;
358
359         spin_lock_bh(&br->hash_lock);
360         ret = fdb_insert(br, source, addr);
361         spin_unlock_bh(&br->hash_lock);
362         return ret;
363 }
364
365 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
366                    const unsigned char *addr)
367 {
368         struct hlist_head *head = &br->hash[br_mac_hash(addr)];
369         struct net_bridge_fdb_entry *fdb;
370
371         /* some users want to always flood. */
372         if (hold_time(br) == 0)
373                 return;
374
375         fdb = fdb_find(head, addr);
376         if (likely(fdb)) {
377                 /* attempt to update an entry for a local interface */
378                 if (unlikely(fdb->is_local)) {
379                         if (net_ratelimit())
380                                 printk(KERN_WARNING "%s: received packet with "
381                                        " own address as source address\n",
382                                        source->dev->name);
383                 } else {
384                         /* fastpath: update of existing entry */
385                         fdb->dst = source;
386                         fdb->ageing_timer = jiffies;
387                 }
388         } else {
389                 spin_lock(&br->hash_lock);
390                 if (!fdb_find(head, addr))
391                         fdb_create(head, source, addr, 0);
392                 /* else  we lose race and someone else inserts
393                  * it first, don't bother updating
394                  */
395                 spin_unlock(&br->hash_lock);
396         }
397 }