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 <asm/atomic.h>
23 #include "br_private.h"
25 static kmem_cache_t *br_fdb_cache;
26 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
27 const unsigned char *addr, int is_local);
29 void __init br_fdb_init(void)
31 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
32 sizeof(struct net_bridge_fdb_entry),
34 SLAB_HWCACHE_ALIGN, NULL, NULL);
37 void __exit br_fdb_fini(void)
39 kmem_cache_destroy(br_fdb_cache);
43 /* if topology_changing then use forward_delay (default 15 sec)
44 * otherwise keep longer (default 5 minutes)
46 static __inline__ unsigned long hold_time(const struct net_bridge *br)
48 return br->topology_change ? br->forward_delay : br->ageing_time;
51 static __inline__ int has_expired(const struct net_bridge *br,
52 const struct net_bridge_fdb_entry *fdb)
54 return !fdb->is_static
55 && time_before_eq(fdb->ageing_timer + hold_time(br), jiffies);
58 static __inline__ int br_mac_hash(const unsigned char *mac)
63 x = (x << 2) ^ mac[1];
64 x = (x << 2) ^ mac[2];
65 x = (x << 2) ^ mac[3];
66 x = (x << 2) ^ mac[4];
67 x = (x << 2) ^ mac[5];
71 return x & (BR_HASH_SIZE - 1);
74 static __inline__ void fdb_delete(struct net_bridge_fdb_entry *f)
76 hlist_del_rcu(&f->hlist);
78 list_del(&f->u.age_list);
83 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
85 struct net_bridge *br = p->br;
88 spin_lock_bh(&br->hash_lock);
90 /* Search all chains since old address/hash is unknown */
91 for (i = 0; i < BR_HASH_SIZE; i++) {
93 hlist_for_each(h, &br->hash[i]) {
94 struct net_bridge_fdb_entry *f;
96 f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
97 if (f->dst == p && f->is_local) {
98 /* maybe another port has same hw addr? */
99 struct net_bridge_port *op;
100 list_for_each_entry(op, &br->port_list, list) {
102 !memcmp(op->dev->dev_addr,
103 f->addr.addr, ETH_ALEN)) {
116 /* insert new address, may fail if invalid address or dup. */
117 fdb_insert(br, p, newaddr, 1);
120 spin_unlock_bh(&br->hash_lock);
123 void br_fdb_cleanup(unsigned long _data)
125 struct net_bridge *br = (struct net_bridge *)_data;
126 struct list_head *l, *n;
129 spin_lock_bh(&br->hash_lock);
130 delay = hold_time(br);
132 list_for_each_safe(l, n, &br->age_list) {
133 struct net_bridge_fdb_entry *f;
134 unsigned long expires;
136 f = list_entry(l, struct net_bridge_fdb_entry, u.age_list);
137 expires = f->ageing_timer + delay;
139 if (time_before_eq(expires, jiffies)) {
140 WARN_ON(f->is_static);
141 pr_debug("expire age %lu jiffies %lu\n",
142 f->ageing_timer, jiffies);
145 mod_timer(&br->gc_timer, expires);
149 spin_unlock_bh(&br->hash_lock);
152 void br_fdb_delete_by_port(struct net_bridge *br, struct net_bridge_port *p)
156 spin_lock_bh(&br->hash_lock);
157 for (i = 0; i < BR_HASH_SIZE; i++) {
158 struct hlist_node *h, *g;
160 hlist_for_each_safe(h, g, &br->hash[i]) {
161 struct net_bridge_fdb_entry *f
162 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
167 * if multiple ports all have the same device address
168 * then when one port is deleted, assign
169 * the local entry to other port
172 struct net_bridge_port *op;
173 list_for_each_entry(op, &br->port_list, list) {
175 !memcmp(op->dev->dev_addr,
176 f->addr.addr, ETH_ALEN)) {
187 spin_unlock_bh(&br->hash_lock);
190 /* No locking or refcounting, assumes caller has no preempt (rcu_read_lock) */
191 struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
192 const unsigned char *addr)
194 struct hlist_node *h;
195 struct net_bridge_fdb_entry *fdb;
197 hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
198 if (!memcmp(fdb->addr.addr, addr, ETH_ALEN)) {
199 if (unlikely(has_expired(br, fdb)))
208 /* Interface used by ATM hook that keeps a ref count */
209 struct net_bridge_fdb_entry *br_fdb_get(struct net_bridge *br,
212 struct net_bridge_fdb_entry *fdb;
215 fdb = __br_fdb_get(br, addr);
217 atomic_inc(&fdb->use_count);
222 static void fdb_rcu_free(struct rcu_head *head)
224 struct net_bridge_fdb_entry *ent
225 = container_of(head, struct net_bridge_fdb_entry, u.rcu);
226 kmem_cache_free(br_fdb_cache, ent);
229 /* Set entry up for deletion with RCU */
230 void br_fdb_put(struct net_bridge_fdb_entry *ent)
232 if (atomic_dec_and_test(&ent->use_count))
233 call_rcu(&ent->u.rcu, fdb_rcu_free);
237 * Fill buffer with forwarding table records in
240 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
241 unsigned long maxnum, unsigned long skip)
243 struct __fdb_entry *fe = buf;
245 struct hlist_node *h;
246 struct net_bridge_fdb_entry *f;
248 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
251 for (i = 0; i < BR_HASH_SIZE; i++) {
252 hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
256 if (has_expired(br, f))
264 /* convert from internal format to API */
265 memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
266 fe->port_no = f->dst->port_no;
267 fe->is_local = f->is_local;
269 fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer);
281 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
282 const unsigned char *addr, int is_local)
284 struct hlist_node *h;
285 struct net_bridge_fdb_entry *fdb;
286 int hash = br_mac_hash(addr);
288 if (!is_valid_ether_addr(addr))
289 return -EADDRNOTAVAIL;
291 hlist_for_each_entry(fdb, h, &br->hash[hash], hlist) {
292 if (!memcmp(fdb->addr.addr, addr, ETH_ALEN)) {
293 /* attempt to update an entry for a local interface */
295 /* it is okay to have multiple ports with same
296 * address, just don't allow to be spoofed.
302 printk(KERN_WARNING "%s: received packet with "
303 " own address as source address\n",
309 printk(KERN_WARNING "%s adding interface with same address "
310 "as a received packet\n",
318 /* move to end of age list */
319 list_del(&fdb->u.age_list);
324 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
328 memcpy(fdb->addr.addr, addr, ETH_ALEN);
329 atomic_set(&fdb->use_count, 1);
330 hlist_add_head_rcu(&fdb->hlist, &br->hash[hash]);
332 if (!timer_pending(&br->gc_timer)) {
333 br->gc_timer.expires = jiffies + hold_time(br);
334 add_timer(&br->gc_timer);
339 fdb->is_local = is_local;
340 fdb->is_static = is_local;
341 fdb->ageing_timer = jiffies;
343 list_add_tail(&fdb->u.age_list, &br->age_list);
348 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
349 const unsigned char *addr, int is_local)
353 spin_lock_bh(&br->hash_lock);
354 ret = fdb_insert(br, source, addr, is_local);
355 spin_unlock_bh(&br->hash_lock);