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 void __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);
40 get_random_bytes(&fdb_salt, sizeof(fdb_salt));
43 void __exit br_fdb_fini(void)
45 kmem_cache_destroy(br_fdb_cache);
49 /* if topology_changing then use forward_delay (default 15 sec)
50 * otherwise keep longer (default 5 minutes)
52 static inline unsigned long hold_time(const struct net_bridge *br)
54 return br->topology_change ? br->forward_delay : br->ageing_time;
57 static inline int has_expired(const struct net_bridge *br,
58 const struct net_bridge_fdb_entry *fdb)
60 return !fdb->is_static
61 && time_before_eq(fdb->ageing_timer + hold_time(br), jiffies);
64 static inline int br_mac_hash(const unsigned char *mac)
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);
71 static inline void fdb_delete(struct net_bridge_fdb_entry *f)
73 hlist_del_rcu(&f->hlist);
77 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
79 struct net_bridge *br = p->br;
82 spin_lock_bh(&br->hash_lock);
84 /* Search all chains since old address/hash is unknown */
85 for (i = 0; i < BR_HASH_SIZE; i++) {
87 hlist_for_each(h, &br->hash[i]) {
88 struct net_bridge_fdb_entry *f;
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) {
96 !compare_ether_addr(op->dev->dev_addr,
110 /* insert new address, may fail if invalid address or dup. */
111 fdb_insert(br, p, newaddr);
113 spin_unlock_bh(&br->hash_lock);
116 void br_fdb_cleanup(unsigned long _data)
118 struct net_bridge *br = (struct net_bridge *)_data;
119 unsigned long delay = hold_time(br);
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;
127 hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
129 time_before_eq(f->ageing_timer + delay, jiffies))
133 spin_unlock_bh(&br->hash_lock);
135 mod_timer(&br->gc_timer, jiffies + HZ/10);
138 /* Completely flush all dynamic entries in forwarding database.*/
139 void br_fdb_flush(struct net_bridge *br)
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) {
152 spin_unlock_bh(&br->hash_lock);
155 /* Flush all entries refering to a specific port.
156 * if do_all is set also flush static entries
158 void br_fdb_delete_by_port(struct net_bridge *br,
159 const struct net_bridge_port *p,
164 spin_lock_bh(&br->hash_lock);
165 for (i = 0; i < BR_HASH_SIZE; i++) {
166 struct hlist_node *h, *g;
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);
174 if (f->is_static && !do_all)
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
182 struct net_bridge_port *op;
183 list_for_each_entry(op, &br->port_list, list) {
185 !compare_ether_addr(op->dev->dev_addr,
197 spin_unlock_bh(&br->hash_lock);
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)
204 struct hlist_node *h;
205 struct net_bridge_fdb_entry *fdb;
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)))
218 /* Interface used by ATM hook that keeps a ref count */
219 struct net_bridge_fdb_entry *br_fdb_get(struct net_bridge *br,
222 struct net_bridge_fdb_entry *fdb;
225 fdb = __br_fdb_get(br, addr);
226 if (fdb && !atomic_inc_not_zero(&fdb->use_count))
232 static void fdb_rcu_free(struct rcu_head *head)
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);
239 /* Set entry up for deletion with RCU */
240 void br_fdb_put(struct net_bridge_fdb_entry *ent)
242 if (atomic_dec_and_test(&ent->use_count))
243 call_rcu(&ent->rcu, fdb_rcu_free);
247 * Fill buffer with forwarding table records in
250 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
251 unsigned long maxnum, unsigned long skip)
253 struct __fdb_entry *fe = buf;
255 struct hlist_node *h;
256 struct net_bridge_fdb_entry *f;
258 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
261 for (i = 0; i < BR_HASH_SIZE; i++) {
262 hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
266 if (has_expired(br, f))
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;
279 fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer);
291 static inline struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
292 const unsigned char *addr)
294 struct hlist_node *h;
295 struct net_bridge_fdb_entry *fdb;
297 hlist_for_each_entry_rcu(fdb, h, head, hlist) {
298 if (!compare_ether_addr(fdb->addr.addr, addr))
304 static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
305 struct net_bridge_port *source,
306 const unsigned char *addr,
309 struct net_bridge_fdb_entry *fdb;
311 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
313 memcpy(fdb->addr.addr, addr, ETH_ALEN);
314 atomic_set(&fdb->use_count, 1);
315 hlist_add_head_rcu(&fdb->hlist, head);
318 fdb->is_local = is_local;
319 fdb->is_static = is_local;
320 fdb->ageing_timer = jiffies;
325 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
326 const unsigned char *addr)
328 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
329 struct net_bridge_fdb_entry *fdb;
331 if (!is_valid_ether_addr(addr))
334 fdb = fdb_find(head, addr);
336 /* it is okay to have multiple ports with same
337 * address, just use the first one.
342 printk(KERN_WARNING "%s adding interface with same address "
343 "as a received packet\n",
348 if (!fdb_create(head, source, addr, 1))
354 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
355 const unsigned char *addr)
359 spin_lock_bh(&br->hash_lock);
360 ret = fdb_insert(br, source, addr);
361 spin_unlock_bh(&br->hash_lock);
365 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
366 const unsigned char *addr)
368 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
369 struct net_bridge_fdb_entry *fdb;
371 /* some users want to always flood. */
372 if (hold_time(br) == 0)
375 fdb = fdb_find(head, addr);
377 /* attempt to update an entry for a local interface */
378 if (unlikely(fdb->is_local)) {
380 printk(KERN_WARNING "%s: received packet with "
381 " own address as source address\n",
384 /* fastpath: update of existing entry */
386 fdb->ageing_timer = jiffies;
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
395 spin_unlock(&br->hash_lock);