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)
78 list_del(&f->age_list);
83 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
85 struct net_bridge *br = p->br;
88 write_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 write_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 write_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 = list_entry(l, struct net_bridge_fdb_entry, age_list);
135 unsigned long expires = f->ageing_timer + delay;
137 if (time_before_eq(expires, jiffies)) {
138 WARN_ON(f->is_static);
139 pr_debug("expire age %lu jiffies %lu\n",
140 f->ageing_timer, jiffies);
143 mod_timer(&br->gc_timer, expires);
147 write_unlock_bh(&br->hash_lock);
150 void br_fdb_delete_by_port(struct net_bridge *br, struct net_bridge_port *p)
154 write_lock_bh(&br->hash_lock);
155 for (i = 0; i < BR_HASH_SIZE; i++) {
156 struct hlist_node *h, *g;
158 hlist_for_each_safe(h, g, &br->hash[i]) {
159 struct net_bridge_fdb_entry *f
160 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
165 * if multiple ports all have the same device address
166 * then when one port is deleted, assign
167 * the local entry to other port
170 struct net_bridge_port *op;
171 list_for_each_entry(op, &br->port_list, list) {
173 !memcmp(op->dev->dev_addr,
174 f->addr.addr, ETH_ALEN)) {
185 write_unlock_bh(&br->hash_lock);
188 struct net_bridge_fdb_entry *br_fdb_get(struct net_bridge *br, unsigned char *addr)
190 struct hlist_node *h;
192 read_lock_bh(&br->hash_lock);
194 hlist_for_each(h, &br->hash[br_mac_hash(addr)]) {
195 struct net_bridge_fdb_entry *fdb
196 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
198 if (!memcmp(fdb->addr.addr, addr, ETH_ALEN)) {
199 if (has_expired(br, fdb))
202 atomic_inc(&fdb->use_count);
203 read_unlock_bh(&br->hash_lock);
208 read_unlock_bh(&br->hash_lock);
212 void br_fdb_put(struct net_bridge_fdb_entry *ent)
214 if (atomic_dec_and_test(&ent->use_count))
215 kmem_cache_free(br_fdb_cache, ent);
219 * Fill buffer with forwarding table records in
222 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
223 unsigned long maxnum, unsigned long skip)
225 struct __fdb_entry *fe = buf;
227 struct hlist_node *h;
228 struct net_bridge_fdb_entry *f;
230 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
232 read_lock_bh(&br->hash_lock);
233 for (i = 0; i < BR_HASH_SIZE; i++) {
234 hlist_for_each_entry(f, h, &br->hash[i], hlist) {
238 if (has_expired(br, f))
246 /* convert from internal format to API */
247 memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
248 fe->port_no = f->dst->port_no;
249 fe->is_local = f->is_local;
251 fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer);
258 read_unlock_bh(&br->hash_lock);
263 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
264 const unsigned char *addr, int is_local)
266 struct hlist_node *h;
267 struct net_bridge_fdb_entry *fdb;
268 int hash = br_mac_hash(addr);
270 if (!is_valid_ether_addr(addr))
271 return -EADDRNOTAVAIL;
273 hlist_for_each_entry(fdb, h, &br->hash[hash], hlist) {
274 if (!memcmp(fdb->addr.addr, addr, ETH_ALEN)) {
275 /* attempt to update an entry for a local interface */
277 /* it is okay to have multiple ports with same
278 * address, just don't allow to be spoofed.
284 printk(KERN_WARNING "%s: received packet with "
285 " own address as source address\n",
291 printk(KERN_WARNING "%s adding interface with same address "
292 "as a received packet\n",
300 /* move to end of age list */
301 list_del(&fdb->age_list);
306 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
310 memcpy(fdb->addr.addr, addr, ETH_ALEN);
311 atomic_set(&fdb->use_count, 1);
312 hlist_add_head(&fdb->hlist, &br->hash[hash]);
314 if (!timer_pending(&br->gc_timer)) {
315 br->gc_timer.expires = jiffies + hold_time(br);
316 add_timer(&br->gc_timer);
321 fdb->is_local = is_local;
322 fdb->is_static = is_local;
323 fdb->ageing_timer = jiffies;
325 list_add_tail(&fdb->age_list, &br->age_list);
330 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
331 const unsigned char *addr, int is_local)
335 write_lock_bh(&br->hash_lock);
336 ret = fdb_insert(br, source, addr, is_local);
337 write_unlock_bh(&br->hash_lock);