blob: 91b017016d5bcbdd240770f9528706439b91a5f7 [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 *
8 * $Id: br_fdb.c,v 1.6 2002/01/17 00:57:07 davem Exp $
9 *
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.
14 */
15
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>
Stephen Hemminger3f890922007-03-21 13:42:33 -070023#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/atomic.h>
Stephen Hemminger3f890922007-03-21 13:42:33 -070025#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "br_private.h"
27
Christoph Lametere18b8902006-12-06 20:33:20 -080028static struct kmem_cache *br_fdb_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
30 const unsigned char *addr);
31
Stephen Hemminger3f890922007-03-21 13:42:33 -070032static u32 fdb_salt __read_mostly;
33
Akinobu Mita87a596e2007-04-07 18:57:07 +090034int __init br_fdb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
37 sizeof(struct net_bridge_fdb_entry),
38 0,
39 SLAB_HWCACHE_ALIGN, NULL, NULL);
Akinobu Mita87a596e2007-04-07 18:57:07 +090040 if (!br_fdb_cache)
41 return -ENOMEM;
42
Stephen Hemminger3f890922007-03-21 13:42:33 -070043 get_random_bytes(&fdb_salt, sizeof(fdb_salt));
Akinobu Mita87a596e2007-04-07 18:57:07 +090044 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
47void __exit br_fdb_fini(void)
48{
49 kmem_cache_destroy(br_fdb_cache);
50}
51
52
53/* if topology_changing then use forward_delay (default 15 sec)
54 * otherwise keep longer (default 5 minutes)
55 */
Stephen Hemminger3f890922007-03-21 13:42:33 -070056static inline unsigned long hold_time(const struct net_bridge *br)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 return br->topology_change ? br->forward_delay : br->ageing_time;
59}
60
Stephen Hemminger3f890922007-03-21 13:42:33 -070061static inline int has_expired(const struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 const struct net_bridge_fdb_entry *fdb)
63{
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090064 return !fdb->is_static
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 && time_before_eq(fdb->ageing_timer + hold_time(br), jiffies);
66}
67
Stephen Hemminger3f890922007-03-21 13:42:33 -070068static inline int br_mac_hash(const unsigned char *mac)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Stephen Hemminger3f890922007-03-21 13:42:33 -070070 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Stephen Hemminger3f890922007-03-21 13:42:33 -070075static inline void fdb_delete(struct net_bridge_fdb_entry *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 hlist_del_rcu(&f->hlist);
78 br_fdb_put(f);
79}
80
81void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
82{
83 struct net_bridge *br = p->br;
84 int i;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 spin_lock_bh(&br->hash_lock);
87
88 /* Search all chains since old address/hash is unknown */
89 for (i = 0; i < BR_HASH_SIZE; i++) {
90 struct hlist_node *h;
91 hlist_for_each(h, &br->hash[i]) {
92 struct net_bridge_fdb_entry *f;
93
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) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090099 if (op != p &&
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700100 !compare_ether_addr(op->dev->dev_addr,
101 f->addr.addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 f->dst = op;
103 goto insert;
104 }
105 }
106
107 /* delete old one */
108 fdb_delete(f);
109 goto insert;
110 }
111 }
112 }
113 insert:
114 /* insert new address, may fail if invalid address or dup. */
115 fdb_insert(br, p, newaddr);
116
117 spin_unlock_bh(&br->hash_lock);
118}
119
120void br_fdb_cleanup(unsigned long _data)
121{
122 struct net_bridge *br = (struct net_bridge *)_data;
123 unsigned long delay = hold_time(br);
124 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) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900132 if (!f->is_static &&
133 time_before_eq(f->ageing_timer + delay, jiffies))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 fdb_delete(f);
135 }
136 }
137 spin_unlock_bh(&br->hash_lock);
138
139 mod_timer(&br->gc_timer, jiffies + HZ/10);
140}
141
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700142/* Completely flush all dynamic entries in forwarding database.*/
143void br_fdb_flush(struct net_bridge *br)
144{
145 int i;
Stephen Hemminger1a620692006-10-12 14:45:38 -0700146
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700147 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) {
152 if (!f->is_static)
153 fdb_delete(f);
154 }
155 }
156 spin_unlock_bh(&br->hash_lock);
157}
158
159/* Flush all entries refering to a specific port.
160 * if do_all is set also flush static entries
161 */
Stephen Hemminger1a620692006-10-12 14:45:38 -0700162void br_fdb_delete_by_port(struct net_bridge *br,
163 const struct net_bridge_port *p,
164 int do_all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 int i;
167
168 spin_lock_bh(&br->hash_lock);
169 for (i = 0; i < BR_HASH_SIZE; i++) {
170 struct hlist_node *h, *g;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 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);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900175 if (f->dst != p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 continue;
177
Stephen Hemminger1a620692006-10-12 14:45:38 -0700178 if (f->is_static && !do_all)
179 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /*
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
184 */
185 if (f->is_local) {
186 struct net_bridge_port *op;
187 list_for_each_entry(op, &br->port_list, list) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900188 if (op != p &&
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700189 !compare_ether_addr(op->dev->dev_addr,
190 f->addr.addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 f->dst = op;
192 goto skip_delete;
193 }
194 }
195 }
196
197 fdb_delete(f);
198 skip_delete: ;
199 }
200 }
201 spin_unlock_bh(&br->hash_lock);
202}
203
204/* No locking or refcounting, assumes caller has no preempt (rcu_read_lock) */
205struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
206 const unsigned char *addr)
207{
208 struct hlist_node *h;
209 struct net_bridge_fdb_entry *fdb;
210
211 hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700212 if (!compare_ether_addr(fdb->addr.addr, addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (unlikely(has_expired(br, fdb)))
214 break;
215 return fdb;
216 }
217 }
218
219 return NULL;
220}
221
222/* Interface used by ATM hook that keeps a ref count */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900223struct net_bridge_fdb_entry *br_fdb_get(struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 unsigned char *addr)
225{
226 struct net_bridge_fdb_entry *fdb;
227
228 rcu_read_lock();
229 fdb = __br_fdb_get(br, addr);
Patrick McHardyb19cbe22007-03-22 12:25:20 -0700230 if (fdb && !atomic_inc_not_zero(&fdb->use_count))
231 fdb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 rcu_read_unlock();
233 return fdb;
234}
235
236static void fdb_rcu_free(struct rcu_head *head)
237{
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);
241}
242
243/* Set entry up for deletion with RCU */
244void br_fdb_put(struct net_bridge_fdb_entry *ent)
245{
246 if (atomic_dec_and_test(&ent->use_count))
247 call_rcu(&ent->rcu, fdb_rcu_free);
248}
249
250/*
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900251 * Fill buffer with forwarding table records in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 * the API format.
253 */
254int br_fdb_fillbuf(struct net_bridge *br, void *buf,
255 unsigned long maxnum, unsigned long skip)
256{
257 struct __fdb_entry *fe = buf;
258 int i, num = 0;
259 struct hlist_node *h;
260 struct net_bridge_fdb_entry *f;
261
262 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
263
264 rcu_read_lock();
265 for (i = 0; i < BR_HASH_SIZE; i++) {
266 hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
267 if (num >= maxnum)
268 goto out;
269
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900270 if (has_expired(br, f))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 continue;
272
273 if (skip) {
274 --skip;
275 continue;
276 }
277
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;
282 if (!f->is_static)
283 fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer);
284 ++fe;
285 ++num;
286 }
287 }
288
289 out:
290 rcu_read_unlock();
291
292 return num;
293}
294
295static inline struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
296 const unsigned char *addr)
297{
298 struct hlist_node *h;
299 struct net_bridge_fdb_entry *fdb;
300
301 hlist_for_each_entry_rcu(fdb, h, head, hlist) {
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700302 if (!compare_ether_addr(fdb->addr.addr, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return fdb;
304 }
305 return NULL;
306}
307
308static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
309 struct net_bridge_port *source,
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900310 const unsigned char *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 int is_local)
312{
313 struct net_bridge_fdb_entry *fdb;
314
315 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
316 if (fdb) {
317 memcpy(fdb->addr.addr, addr, ETH_ALEN);
318 atomic_set(&fdb->use_count, 1);
319 hlist_add_head_rcu(&fdb->hlist, head);
320
321 fdb->dst = source;
322 fdb->is_local = is_local;
323 fdb->is_static = is_local;
324 fdb->ageing_timer = jiffies;
325 }
326 return fdb;
327}
328
329static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
330 const unsigned char *addr)
331{
332 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
333 struct net_bridge_fdb_entry *fdb;
334
335 if (!is_valid_ether_addr(addr))
336 return -EINVAL;
337
338 fdb = fdb_find(head, addr);
339 if (fdb) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900340 /* it is okay to have multiple ports with same
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * address, just use the first one.
342 */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900343 if (fdb->is_local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return 0;
345
346 printk(KERN_WARNING "%s adding interface with same address "
347 "as a received packet\n",
348 source->dev->name);
349 fdb_delete(fdb);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (!fdb_create(head, source, addr, 1))
353 return -ENOMEM;
354
355 return 0;
356}
357
358int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
359 const unsigned char *addr)
360{
361 int ret;
362
363 spin_lock_bh(&br->hash_lock);
364 ret = fdb_insert(br, source, addr);
365 spin_unlock_bh(&br->hash_lock);
366 return ret;
367}
368
369void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
370 const unsigned char *addr)
371{
372 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
373 struct net_bridge_fdb_entry *fdb;
374
375 /* some users want to always flood. */
376 if (hold_time(br) == 0)
377 return;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 fdb = fdb_find(head, addr);
380 if (likely(fdb)) {
381 /* attempt to update an entry for a local interface */
382 if (unlikely(fdb->is_local)) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900383 if (net_ratelimit())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 printk(KERN_WARNING "%s: received packet with "
385 " own address as source address\n",
386 source->dev->name);
387 } else {
388 /* fastpath: update of existing entry */
389 fdb->dst = source;
390 fdb->ageing_timer = jiffies;
391 }
392 } else {
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800393 spin_lock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 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
398 */
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800399 spin_unlock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}