blob: 88485cc74dc31c697d33ddc5eb2c5f2a0d3d0dee [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14#include <linux/kernel.h>
15#include <linux/init.h>
Franck Bui-Huu82524742008-05-12 21:21:05 +020016#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/spinlock.h>
18#include <linux/times.h>
19#include <linux/netdevice.h>
20#include <linux/etherdevice.h>
21#include <linux/jhash.h>
Stephen Hemminger3f890922007-03-21 13:42:33 -070022#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.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,
Paul Mundt20c2df82007-07-20 10:11:58 +090039 SLAB_HWCACHE_ALIGN, 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
Andrew Morton73afc902007-12-05 21:35:23 -080047void br_fdb_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
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{
Joe Perchesf64f9e72009-11-29 16:55:45 -080064 return !fdb->is_static &&
65 time_before_eq(fdb->ageing_timer + hold_time(br), jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
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
Michał Mirosławda678292009-06-05 05:35:28 +000075static void fdb_rcu_free(struct rcu_head *head)
76{
77 struct net_bridge_fdb_entry *ent
78 = container_of(head, struct net_bridge_fdb_entry, rcu);
79 kmem_cache_free(br_fdb_cache, ent);
80}
81
Stephen Hemminger3f890922007-03-21 13:42:33 -070082static inline void fdb_delete(struct net_bridge_fdb_entry *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 hlist_del_rcu(&f->hlist);
Michał Mirosławda678292009-06-05 05:35:28 +000085 call_rcu(&f->rcu, fdb_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
89{
90 struct net_bridge *br = p->br;
91 int i;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 spin_lock_bh(&br->hash_lock);
94
95 /* Search all chains since old address/hash is unknown */
96 for (i = 0; i < BR_HASH_SIZE; i++) {
97 struct hlist_node *h;
98 hlist_for_each(h, &br->hash[i]) {
99 struct net_bridge_fdb_entry *f;
100
101 f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
102 if (f->dst == p && f->is_local) {
103 /* maybe another port has same hw addr? */
104 struct net_bridge_port *op;
105 list_for_each_entry(op, &br->port_list, list) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900106 if (op != p &&
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700107 !compare_ether_addr(op->dev->dev_addr,
108 f->addr.addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 f->dst = op;
110 goto insert;
111 }
112 }
113
114 /* delete old one */
115 fdb_delete(f);
116 goto insert;
117 }
118 }
119 }
120 insert:
121 /* insert new address, may fail if invalid address or dup. */
122 fdb_insert(br, p, newaddr);
123
124 spin_unlock_bh(&br->hash_lock);
125}
126
127void br_fdb_cleanup(unsigned long _data)
128{
129 struct net_bridge *br = (struct net_bridge *)_data;
130 unsigned long delay = hold_time(br);
stephen hemminger25442e02010-06-15 06:14:12 +0000131 unsigned long next_timer = jiffies + br->ageing_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int i;
133
134 spin_lock_bh(&br->hash_lock);
135 for (i = 0; i < BR_HASH_SIZE; i++) {
136 struct net_bridge_fdb_entry *f;
137 struct hlist_node *h, *n;
138
139 hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
Baruch Even071f7722007-05-31 01:20:45 -0700140 unsigned long this_timer;
141 if (f->is_static)
142 continue;
143 this_timer = f->ageing_timer + delay;
144 if (time_before_eq(this_timer, jiffies))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 fdb_delete(f);
Fabio Checconi2bec0082008-03-20 15:54:58 -0700146 else if (time_before(this_timer, next_timer))
Baruch Even071f7722007-05-31 01:20:45 -0700147 next_timer = this_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149 }
150 spin_unlock_bh(&br->hash_lock);
151
stephen hemminger25442e02010-06-15 06:14:12 +0000152 mod_timer(&br->gc_timer, round_jiffies_up(next_timer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700155/* Completely flush all dynamic entries in forwarding database.*/
156void br_fdb_flush(struct net_bridge *br)
157{
158 int i;
Stephen Hemminger1a620692006-10-12 14:45:38 -0700159
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700160 spin_lock_bh(&br->hash_lock);
161 for (i = 0; i < BR_HASH_SIZE; i++) {
162 struct net_bridge_fdb_entry *f;
163 struct hlist_node *h, *n;
164 hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
165 if (!f->is_static)
166 fdb_delete(f);
167 }
168 }
169 spin_unlock_bh(&br->hash_lock);
170}
171
172/* Flush all entries refering to a specific port.
173 * if do_all is set also flush static entries
174 */
Stephen Hemminger1a620692006-10-12 14:45:38 -0700175void br_fdb_delete_by_port(struct net_bridge *br,
176 const struct net_bridge_port *p,
177 int do_all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 int i;
180
181 spin_lock_bh(&br->hash_lock);
182 for (i = 0; i < BR_HASH_SIZE; i++) {
183 struct hlist_node *h, *g;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 hlist_for_each_safe(h, g, &br->hash[i]) {
186 struct net_bridge_fdb_entry *f
187 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900188 if (f->dst != p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 continue;
190
Stephen Hemminger1a620692006-10-12 14:45:38 -0700191 if (f->is_static && !do_all)
192 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /*
194 * if multiple ports all have the same device address
195 * then when one port is deleted, assign
196 * the local entry to other port
197 */
198 if (f->is_local) {
199 struct net_bridge_port *op;
200 list_for_each_entry(op, &br->port_list, list) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900201 if (op != p &&
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700202 !compare_ether_addr(op->dev->dev_addr,
203 f->addr.addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 f->dst = op;
205 goto skip_delete;
206 }
207 }
208 }
209
210 fdb_delete(f);
211 skip_delete: ;
212 }
213 }
214 spin_unlock_bh(&br->hash_lock);
215}
216
stephen hemmingereeaf61d2010-07-27 08:26:30 +0000217/* No locking or refcounting, assumes caller has rcu_read_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
219 const unsigned char *addr)
220{
221 struct hlist_node *h;
222 struct net_bridge_fdb_entry *fdb;
223
224 hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700225 if (!compare_ether_addr(fdb->addr.addr, addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (unlikely(has_expired(br, fdb)))
227 break;
228 return fdb;
229 }
230 }
231
232 return NULL;
233}
234
Michał Mirosławda678292009-06-05 05:35:28 +0000235#if defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE)
236/* Interface used by ATM LANE hook to test
237 * if an addr is on some other bridge port */
238int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct net_bridge_fdb_entry *fdb;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000241 struct net_bridge_port *port;
Michał Mirosławda678292009-06-05 05:35:28 +0000242 int ret;
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 rcu_read_lock();
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000245 port = br_port_get_rcu(dev);
246 if (!port)
247 ret = 0;
248 else {
249 fdb = __br_fdb_get(port->br, addr);
250 ret = fdb && fdb->dst->dev != dev &&
251 fdb->dst->state == BR_STATE_FORWARDING;
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Michał Mirosławda678292009-06-05 05:35:28 +0000255 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
Michał Mirosławda678292009-06-05 05:35:28 +0000257#endif /* CONFIG_ATM_LANE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259/*
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900260 * Fill buffer with forwarding table records in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * the API format.
262 */
263int br_fdb_fillbuf(struct net_bridge *br, void *buf,
264 unsigned long maxnum, unsigned long skip)
265{
266 struct __fdb_entry *fe = buf;
267 int i, num = 0;
268 struct hlist_node *h;
269 struct net_bridge_fdb_entry *f;
270
271 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
272
273 rcu_read_lock();
274 for (i = 0; i < BR_HASH_SIZE; i++) {
275 hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
276 if (num >= maxnum)
277 goto out;
278
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900279 if (has_expired(br, f))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 continue;
281
282 if (skip) {
283 --skip;
284 continue;
285 }
286
287 /* convert from internal format to API */
288 memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
Stephen Hemmingerae4f8fc2008-05-02 16:53:33 -0700289
290 /* due to ABI compat need to split into hi/lo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 fe->port_no = f->dst->port_no;
Stephen Hemmingerae4f8fc2008-05-02 16:53:33 -0700292 fe->port_hi = f->dst->port_no >> 8;
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 fe->is_local = f->is_local;
295 if (!f->is_static)
296 fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer);
297 ++fe;
298 ++num;
299 }
300 }
301
302 out:
303 rcu_read_unlock();
304
305 return num;
306}
307
308static inline struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
309 const unsigned char *addr)
310{
311 struct hlist_node *h;
312 struct net_bridge_fdb_entry *fdb;
313
314 hlist_for_each_entry_rcu(fdb, h, head, hlist) {
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700315 if (!compare_ether_addr(fdb->addr.addr, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return fdb;
317 }
318 return NULL;
319}
320
321static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
322 struct net_bridge_port *source,
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900323 const unsigned char *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 int is_local)
325{
326 struct net_bridge_fdb_entry *fdb;
327
328 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
329 if (fdb) {
330 memcpy(fdb->addr.addr, addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 fdb->dst = source;
332 fdb->is_local = is_local;
333 fdb->is_static = is_local;
334 fdb->ageing_timer = jiffies;
Pavel Emelyanov1158f762011-02-04 13:02:36 -0800335
336 hlist_add_head_rcu(&fdb->hlist, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338 return fdb;
339}
340
341static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
342 const unsigned char *addr)
343{
344 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
345 struct net_bridge_fdb_entry *fdb;
346
347 if (!is_valid_ether_addr(addr))
348 return -EINVAL;
349
350 fdb = fdb_find(head, addr);
351 if (fdb) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900352 /* it is okay to have multiple ports with same
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * address, just use the first one.
354 */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900355 if (fdb->is_local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
stephen hemminger28a16c92010-05-10 09:31:09 +0000357 br_warn(br, "adding interface %s with same address "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 "as a received packet\n",
359 source->dev->name);
360 fdb_delete(fdb);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (!fdb_create(head, source, addr, 1))
364 return -ENOMEM;
365
366 return 0;
367}
368
369int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
370 const unsigned char *addr)
371{
372 int ret;
373
374 spin_lock_bh(&br->hash_lock);
375 ret = fdb_insert(br, source, addr);
376 spin_unlock_bh(&br->hash_lock);
377 return ret;
378}
379
380void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
381 const unsigned char *addr)
382{
383 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
384 struct net_bridge_fdb_entry *fdb;
385
386 /* some users want to always flood. */
387 if (hold_time(br) == 0)
388 return;
389
Stephen Hemmingerdf1c0b82007-08-30 22:15:35 -0700390 /* ignore packets unless we are using this port */
391 if (!(source->state == BR_STATE_LEARNING ||
392 source->state == BR_STATE_FORWARDING))
393 return;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 fdb = fdb_find(head, addr);
396 if (likely(fdb)) {
397 /* attempt to update an entry for a local interface */
398 if (unlikely(fdb->is_local)) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900399 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000400 br_warn(br, "received packet on %s with "
401 "own address as source address\n",
402 source->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 } else {
404 /* fastpath: update of existing entry */
405 fdb->dst = source;
406 fdb->ageing_timer = jiffies;
407 }
408 } else {
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800409 spin_lock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (!fdb_find(head, addr))
411 fdb_create(head, source, addr, 0);
412 /* else we lose race and someone else inserts
413 * it first, don't bother updating
414 */
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800415 spin_unlock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}