3 * Linux ethernet bridge
6 * Lennert Buytenhek <buytenh@gnu.org>
8 * $Id: br_fdb.c,v 1.6 2002/01/17 00:57:07 davem Exp $
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.
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"
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);
32 static u32 fdb_salt __read_mostly;
34 int __init br_fdb_init(void)
36 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
37 sizeof(struct net_bridge_fdb_entry),
39 SLAB_HWCACHE_ALIGN, NULL, NULL);
43 get_random_bytes(&fdb_salt, sizeof(fdb_salt));
47 void __exit br_fdb_fini(void)
49 kmem_cache_destroy(br_fdb_cache);
53 /* if topology_changing then use forward_delay (default 15 sec)
54 * otherwise keep longer (default 5 minutes)
56 static inline unsigned long hold_time(const struct net_bridge *br)
58 return br->topology_change ? br->forward_delay : br->ageing_time;
61 static inline int has_expired(const struct net_bridge *br,
62 const struct net_bridge_fdb_entry *fdb)
64 return !fdb->is_static
65 && time_before_eq(fdb->ageing_timer + hold_time(br), jiffies);
68 static inline int br_mac_hash(const unsigned char *mac)
70 /* use 1 byte of OUI cnd 3 bytes of NIC */
71 u32 key = get_unaligned((u32 *)(mac + 2));
72 return jhash_1word(key, fdb_salt) & (BR_HASH_SIZE - 1);
75 static inline void fdb_delete(struct net_bridge_fdb_entry *f)
77 hlist_del_rcu(&f->hlist);
81 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
83 struct net_bridge *br = p->br;
86 spin_lock_bh(&br->hash_lock);
88 /* Search all chains since old address/hash is unknown */
89 for (i = 0; i < BR_HASH_SIZE; i++) {
91 hlist_for_each(h, &br->hash[i]) {
92 struct net_bridge_fdb_entry *f;
94 f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
95 if (f->dst == p && f->is_local) {
96 /* maybe another port has same hw addr? */
97 struct net_bridge_port *op;
98 list_for_each_entry(op, &br->port_list, list) {
100 !compare_ether_addr(op->dev->dev_addr,
114 /* insert new address, may fail if invalid address or dup. */
115 fdb_insert(br, p, newaddr);
117 spin_unlock_bh(&br->hash_lock);
120 void br_fdb_cleanup(unsigned long _data)
122 struct net_bridge *br = (struct net_bridge *)_data;
123 unsigned long delay = hold_time(br);
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;
131 hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
133 time_before_eq(f->ageing_timer + delay, jiffies))
137 spin_unlock_bh(&br->hash_lock);
139 mod_timer(&br->gc_timer, jiffies + HZ/10);
142 /* Completely flush all dynamic entries in forwarding database.*/
143 void br_fdb_flush(struct net_bridge *br)
147 spin_lock_bh(&br->hash_lock);
148 for (i = 0; i < BR_HASH_SIZE; i++) {
149 struct net_bridge_fdb_entry *f;
150 struct hlist_node *h, *n;
151 hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
156 spin_unlock_bh(&br->hash_lock);
159 /* Flush all entries refering to a specific port.
160 * if do_all is set also flush static entries
162 void br_fdb_delete_by_port(struct net_bridge *br,
163 const struct net_bridge_port *p,
168 spin_lock_bh(&br->hash_lock);
169 for (i = 0; i < BR_HASH_SIZE; i++) {
170 struct hlist_node *h, *g;
172 hlist_for_each_safe(h, g, &br->hash[i]) {
173 struct net_bridge_fdb_entry *f
174 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
178 if (f->is_static && !do_all)
181 * if multiple ports all have the same device address
182 * then when one port is deleted, assign
183 * the local entry to other port
186 struct net_bridge_port *op;
187 list_for_each_entry(op, &br->port_list, list) {
189 !compare_ether_addr(op->dev->dev_addr,
201 spin_unlock_bh(&br->hash_lock);
204 /* No locking or refcounting, assumes caller has no preempt (rcu_read_lock) */
205 struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
206 const unsigned char *addr)
208 struct hlist_node *h;
209 struct net_bridge_fdb_entry *fdb;
211 hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
212 if (!compare_ether_addr(fdb->addr.addr, addr)) {
213 if (unlikely(has_expired(br, fdb)))
222 /* Interface used by ATM hook that keeps a ref count */
223 struct net_bridge_fdb_entry *br_fdb_get(struct net_bridge *br,
226 struct net_bridge_fdb_entry *fdb;
229 fdb = __br_fdb_get(br, addr);
230 if (fdb && !atomic_inc_not_zero(&fdb->use_count))
236 static void fdb_rcu_free(struct rcu_head *head)
238 struct net_bridge_fdb_entry *ent
239 = container_of(head, struct net_bridge_fdb_entry, rcu);
240 kmem_cache_free(br_fdb_cache, ent);
243 /* Set entry up for deletion with RCU */
244 void br_fdb_put(struct net_bridge_fdb_entry *ent)
246 if (atomic_dec_and_test(&ent->use_count))
247 call_rcu(&ent->rcu, fdb_rcu_free);
251 * Fill buffer with forwarding table records in
254 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
255 unsigned long maxnum, unsigned long skip)
257 struct __fdb_entry *fe = buf;
259 struct hlist_node *h;
260 struct net_bridge_fdb_entry *f;
262 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
265 for (i = 0; i < BR_HASH_SIZE; i++) {
266 hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
270 if (has_expired(br, f))
278 /* convert from internal format to API */
279 memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
280 fe->port_no = f->dst->port_no;
281 fe->is_local = f->is_local;
283 fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer);
295 static inline struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
296 const unsigned char *addr)
298 struct hlist_node *h;
299 struct net_bridge_fdb_entry *fdb;
301 hlist_for_each_entry_rcu(fdb, h, head, hlist) {
302 if (!compare_ether_addr(fdb->addr.addr, addr))
308 static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
309 struct net_bridge_port *source,
310 const unsigned char *addr,
313 struct net_bridge_fdb_entry *fdb;
315 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
317 memcpy(fdb->addr.addr, addr, ETH_ALEN);
318 atomic_set(&fdb->use_count, 1);
319 hlist_add_head_rcu(&fdb->hlist, head);
322 fdb->is_local = is_local;
323 fdb->is_static = is_local;
324 fdb->ageing_timer = jiffies;
329 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
330 const unsigned char *addr)
332 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
333 struct net_bridge_fdb_entry *fdb;
335 if (!is_valid_ether_addr(addr))
338 fdb = fdb_find(head, addr);
340 /* it is okay to have multiple ports with same
341 * address, just use the first one.
346 printk(KERN_WARNING "%s adding interface with same address "
347 "as a received packet\n",
352 if (!fdb_create(head, source, addr, 1))
358 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
359 const unsigned char *addr)
363 spin_lock_bh(&br->hash_lock);
364 ret = fdb_insert(br, source, addr);
365 spin_unlock_bh(&br->hash_lock);
369 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
370 const unsigned char *addr)
372 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
373 struct net_bridge_fdb_entry *fdb;
375 /* some users want to always flood. */
376 if (hold_time(br) == 0)
379 fdb = fdb_find(head, addr);
381 /* attempt to update an entry for a local interface */
382 if (unlikely(fdb->is_local)) {
384 printk(KERN_WARNING "%s: received packet with "
385 " own address as source address\n",
388 /* fastpath: update of existing entry */
390 fdb->ageing_timer = jiffies;
393 spin_lock(&br->hash_lock);
394 if (!fdb_find(head, addr))
395 fdb_create(head, source, addr, 0);
396 /* else we lose race and someone else inserts
397 * it first, don't bother updating
399 spin_unlock(&br->hash_lock);