blob: a48f5efdb6bfa9b44fb0149136c169857a6e0931 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Forwarding database
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14#include <linux/kernel.h>
15#include <linux/init.h>
Franck Bui-Huu82524742008-05-12 21:21:05 +020016#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/spinlock.h>
18#include <linux/times.h>
19#include <linux/netdevice.h>
20#include <linux/etherdevice.h>
21#include <linux/jhash.h>
Stephen Hemminger3f890922007-03-21 13:42:33 -070022#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/atomic.h>
Stephen Hemminger3f890922007-03-21 13:42:33 -070024#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "br_private.h"
26
Christoph Lametere18b8902006-12-06 20:33:20 -080027static struct kmem_cache *br_fdb_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
29 const unsigned char *addr);
30
Stephen Hemminger3f890922007-03-21 13:42:33 -070031static u32 fdb_salt __read_mostly;
32
Akinobu Mita87a596e2007-04-07 18:57:07 +090033int __init br_fdb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
36 sizeof(struct net_bridge_fdb_entry),
37 0,
Paul Mundt20c2df82007-07-20 10:11:58 +090038 SLAB_HWCACHE_ALIGN, NULL);
Akinobu Mita87a596e2007-04-07 18:57:07 +090039 if (!br_fdb_cache)
40 return -ENOMEM;
41
Stephen Hemminger3f890922007-03-21 13:42:33 -070042 get_random_bytes(&fdb_salt, sizeof(fdb_salt));
Akinobu Mita87a596e2007-04-07 18:57:07 +090043 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
Andrew Morton73afc902007-12-05 21:35:23 -080046void br_fdb_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 kmem_cache_destroy(br_fdb_cache);
49}
50
51
52/* if topology_changing then use forward_delay (default 15 sec)
53 * otherwise keep longer (default 5 minutes)
54 */
Stephen Hemminger3f890922007-03-21 13:42:33 -070055static inline unsigned long hold_time(const struct net_bridge *br)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 return br->topology_change ? br->forward_delay : br->ageing_time;
58}
59
Stephen Hemminger3f890922007-03-21 13:42:33 -070060static inline int has_expired(const struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 const struct net_bridge_fdb_entry *fdb)
62{
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090063 return !fdb->is_static
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 && time_before_eq(fdb->ageing_timer + hold_time(br), jiffies);
65}
66
Stephen Hemminger3f890922007-03-21 13:42:33 -070067static inline int br_mac_hash(const unsigned char *mac)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Stephen Hemminger3f890922007-03-21 13:42:33 -070069 /* use 1 byte of OUI cnd 3 bytes of NIC */
70 u32 key = get_unaligned((u32 *)(mac + 2));
71 return jhash_1word(key, fdb_salt) & (BR_HASH_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
Stephen Hemminger3f890922007-03-21 13:42:33 -070074static inline void fdb_delete(struct net_bridge_fdb_entry *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 hlist_del_rcu(&f->hlist);
77 br_fdb_put(f);
78}
79
80void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
81{
82 struct net_bridge *br = p->br;
83 int i;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 spin_lock_bh(&br->hash_lock);
86
87 /* Search all chains since old address/hash is unknown */
88 for (i = 0; i < BR_HASH_SIZE; i++) {
89 struct hlist_node *h;
90 hlist_for_each(h, &br->hash[i]) {
91 struct net_bridge_fdb_entry *f;
92
93 f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
94 if (f->dst == p && f->is_local) {
95 /* maybe another port has same hw addr? */
96 struct net_bridge_port *op;
97 list_for_each_entry(op, &br->port_list, list) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090098 if (op != p &&
Stephen Hemminger6ede2462005-10-25 15:04:59 -070099 !compare_ether_addr(op->dev->dev_addr,
100 f->addr.addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 f->dst = op;
102 goto insert;
103 }
104 }
105
106 /* delete old one */
107 fdb_delete(f);
108 goto insert;
109 }
110 }
111 }
112 insert:
113 /* insert new address, may fail if invalid address or dup. */
114 fdb_insert(br, p, newaddr);
115
116 spin_unlock_bh(&br->hash_lock);
117}
118
119void br_fdb_cleanup(unsigned long _data)
120{
121 struct net_bridge *br = (struct net_bridge *)_data;
122 unsigned long delay = hold_time(br);
Baruch Even071f7722007-05-31 01:20:45 -0700123 unsigned long next_timer = jiffies + br->forward_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int i;
125
126 spin_lock_bh(&br->hash_lock);
127 for (i = 0; i < BR_HASH_SIZE; i++) {
128 struct net_bridge_fdb_entry *f;
129 struct hlist_node *h, *n;
130
131 hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
Baruch Even071f7722007-05-31 01:20:45 -0700132 unsigned long this_timer;
133 if (f->is_static)
134 continue;
135 this_timer = f->ageing_timer + delay;
136 if (time_before_eq(this_timer, jiffies))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 fdb_delete(f);
Fabio Checconi2bec0082008-03-20 15:54:58 -0700138 else if (time_before(this_timer, next_timer))
Baruch Even071f7722007-05-31 01:20:45 -0700139 next_timer = this_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141 }
142 spin_unlock_bh(&br->hash_lock);
143
Baruch Even071f7722007-05-31 01:20:45 -0700144 /* Add HZ/4 to ensure we round the jiffies upwards to be after the next
145 * timer, otherwise we might round down and will have no-op run. */
146 mod_timer(&br->gc_timer, round_jiffies(next_timer + HZ/4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700149/* Completely flush all dynamic entries in forwarding database.*/
150void br_fdb_flush(struct net_bridge *br)
151{
152 int i;
Stephen Hemminger1a620692006-10-12 14:45:38 -0700153
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700154 spin_lock_bh(&br->hash_lock);
155 for (i = 0; i < BR_HASH_SIZE; i++) {
156 struct net_bridge_fdb_entry *f;
157 struct hlist_node *h, *n;
158 hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
159 if (!f->is_static)
160 fdb_delete(f);
161 }
162 }
163 spin_unlock_bh(&br->hash_lock);
164}
165
166/* Flush all entries refering to a specific port.
167 * if do_all is set also flush static entries
168 */
Stephen Hemminger1a620692006-10-12 14:45:38 -0700169void br_fdb_delete_by_port(struct net_bridge *br,
170 const struct net_bridge_port *p,
171 int do_all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 int i;
174
175 spin_lock_bh(&br->hash_lock);
176 for (i = 0; i < BR_HASH_SIZE; i++) {
177 struct hlist_node *h, *g;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 hlist_for_each_safe(h, g, &br->hash[i]) {
180 struct net_bridge_fdb_entry *f
181 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900182 if (f->dst != p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 continue;
184
Stephen Hemminger1a620692006-10-12 14:45:38 -0700185 if (f->is_static && !do_all)
186 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /*
188 * if multiple ports all have the same device address
189 * then when one port is deleted, assign
190 * the local entry to other port
191 */
192 if (f->is_local) {
193 struct net_bridge_port *op;
194 list_for_each_entry(op, &br->port_list, list) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900195 if (op != p &&
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700196 !compare_ether_addr(op->dev->dev_addr,
197 f->addr.addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 f->dst = op;
199 goto skip_delete;
200 }
201 }
202 }
203
204 fdb_delete(f);
205 skip_delete: ;
206 }
207 }
208 spin_unlock_bh(&br->hash_lock);
209}
210
211/* No locking or refcounting, assumes caller has no preempt (rcu_read_lock) */
212struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
213 const unsigned char *addr)
214{
215 struct hlist_node *h;
216 struct net_bridge_fdb_entry *fdb;
217
218 hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700219 if (!compare_ether_addr(fdb->addr.addr, addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (unlikely(has_expired(br, fdb)))
221 break;
222 return fdb;
223 }
224 }
225
226 return NULL;
227}
228
229/* Interface used by ATM hook that keeps a ref count */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900230struct net_bridge_fdb_entry *br_fdb_get(struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 unsigned char *addr)
232{
233 struct net_bridge_fdb_entry *fdb;
234
235 rcu_read_lock();
236 fdb = __br_fdb_get(br, addr);
Patrick McHardyb19cbe22007-03-22 12:25:20 -0700237 if (fdb && !atomic_inc_not_zero(&fdb->use_count))
238 fdb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 rcu_read_unlock();
240 return fdb;
241}
242
243static void fdb_rcu_free(struct rcu_head *head)
244{
245 struct net_bridge_fdb_entry *ent
246 = container_of(head, struct net_bridge_fdb_entry, rcu);
247 kmem_cache_free(br_fdb_cache, ent);
248}
249
250/* Set entry up for deletion with RCU */
251void br_fdb_put(struct net_bridge_fdb_entry *ent)
252{
253 if (atomic_dec_and_test(&ent->use_count))
254 call_rcu(&ent->rcu, fdb_rcu_free);
255}
256
257/*
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900258 * Fill buffer with forwarding table records in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * the API format.
260 */
261int br_fdb_fillbuf(struct net_bridge *br, void *buf,
262 unsigned long maxnum, unsigned long skip)
263{
264 struct __fdb_entry *fe = buf;
265 int i, num = 0;
266 struct hlist_node *h;
267 struct net_bridge_fdb_entry *f;
268
269 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
270
271 rcu_read_lock();
272 for (i = 0; i < BR_HASH_SIZE; i++) {
273 hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
274 if (num >= maxnum)
275 goto out;
276
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900277 if (has_expired(br, f))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 continue;
279
280 if (skip) {
281 --skip;
282 continue;
283 }
284
285 /* convert from internal format to API */
286 memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
Stephen Hemmingerae4f8fc2008-05-02 16:53:33 -0700287
288 /* due to ABI compat need to split into hi/lo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 fe->port_no = f->dst->port_no;
Stephen Hemmingerae4f8fc2008-05-02 16:53:33 -0700290 fe->port_hi = f->dst->port_no >> 8;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 fe->is_local = f->is_local;
293 if (!f->is_static)
294 fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer);
295 ++fe;
296 ++num;
297 }
298 }
299
300 out:
301 rcu_read_unlock();
302
303 return num;
304}
305
306static inline struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
307 const unsigned char *addr)
308{
309 struct hlist_node *h;
310 struct net_bridge_fdb_entry *fdb;
311
312 hlist_for_each_entry_rcu(fdb, h, head, hlist) {
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700313 if (!compare_ether_addr(fdb->addr.addr, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return fdb;
315 }
316 return NULL;
317}
318
319static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
320 struct net_bridge_port *source,
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900321 const unsigned char *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 int is_local)
323{
324 struct net_bridge_fdb_entry *fdb;
325
326 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
327 if (fdb) {
328 memcpy(fdb->addr.addr, addr, ETH_ALEN);
329 atomic_set(&fdb->use_count, 1);
330 hlist_add_head_rcu(&fdb->hlist, head);
331
332 fdb->dst = source;
333 fdb->is_local = is_local;
334 fdb->is_static = is_local;
335 fdb->ageing_timer = jiffies;
336 }
337 return fdb;
338}
339
340static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
341 const unsigned char *addr)
342{
343 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
344 struct net_bridge_fdb_entry *fdb;
345
346 if (!is_valid_ether_addr(addr))
347 return -EINVAL;
348
349 fdb = fdb_find(head, addr);
350 if (fdb) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900351 /* it is okay to have multiple ports with same
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 * address, just use the first one.
353 */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900354 if (fdb->is_local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return 0;
356
357 printk(KERN_WARNING "%s adding interface with same address "
358 "as a received packet\n",
359 source->dev->name);
360 fdb_delete(fdb);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (!fdb_create(head, source, addr, 1))
364 return -ENOMEM;
365
366 return 0;
367}
368
369int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
370 const unsigned char *addr)
371{
372 int ret;
373
374 spin_lock_bh(&br->hash_lock);
375 ret = fdb_insert(br, source, addr);
376 spin_unlock_bh(&br->hash_lock);
377 return ret;
378}
379
380void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
381 const unsigned char *addr)
382{
383 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
384 struct net_bridge_fdb_entry *fdb;
385
386 /* some users want to always flood. */
387 if (hold_time(br) == 0)
388 return;
389
Stephen Hemmingerdf1c0b82007-08-30 22:15:35 -0700390 /* ignore packets unless we are using this port */
391 if (!(source->state == BR_STATE_LEARNING ||
392 source->state == BR_STATE_FORWARDING))
393 return;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 fdb = fdb_find(head, addr);
396 if (likely(fdb)) {
397 /* attempt to update an entry for a local interface */
398 if (unlikely(fdb->is_local)) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900399 if (net_ratelimit())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 printk(KERN_WARNING "%s: received packet with "
401 " own address as source address\n",
402 source->dev->name);
403 } else {
404 /* fastpath: update of existing entry */
405 fdb->dst = source;
406 fdb->ageing_timer = jiffies;
407 }
408 } else {
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800409 spin_lock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (!fdb_find(head, addr))
411 fdb_create(head, source, addr, 0);
412 /* else we lose race and someone else inserts
413 * it first, don't bother updating
414 */
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800415 spin_unlock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}