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