Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Forwarding database |
| 3 | * Linux ethernet bridge |
| 4 | * |
| 5 | * Authors: |
| 6 | * Lennert Buytenhek <buytenh@gnu.org> |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * 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-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 16 | #include <linux/rculist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #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 Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 22 | #include <linux/random.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/atomic.h> |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 25 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "br_private.h" |
| 27 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 28 | static struct kmem_cache *br_fdb_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source, |
| 30 | const unsigned char *addr); |
| 31 | |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 32 | static u32 fdb_salt __read_mostly; |
| 33 | |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 34 | int __init br_fdb_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
| 36 | br_fdb_cache = kmem_cache_create("bridge_fdb_cache", |
| 37 | sizeof(struct net_bridge_fdb_entry), |
| 38 | 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 39 | SLAB_HWCACHE_ALIGN, NULL); |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 40 | if (!br_fdb_cache) |
| 41 | return -ENOMEM; |
| 42 | |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 43 | get_random_bytes(&fdb_salt, sizeof(fdb_salt)); |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 44 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Andrew Morton | 73afc90 | 2007-12-05 21:35:23 -0800 | [diff] [blame] | 47 | void br_fdb_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 56 | static inline unsigned long hold_time(const struct net_bridge *br) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
| 58 | return br->topology_change ? br->forward_delay : br->ageing_time; |
| 59 | } |
| 60 | |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 61 | static inline int has_expired(const struct net_bridge *br, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | const struct net_bridge_fdb_entry *fdb) |
| 63 | { |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 64 | return !fdb->is_static && |
| 65 | time_before_eq(fdb->ageing_timer + hold_time(br), jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 68 | static inline int br_mac_hash(const unsigned char *mac) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 70 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 75 | static 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 Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 82 | static inline void fdb_delete(struct net_bridge_fdb_entry *f) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | { |
| 84 | hlist_del_rcu(&f->hlist); |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 85 | call_rcu(&f->rcu, fdb_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr) |
| 89 | { |
| 90 | struct net_bridge *br = p->br; |
| 91 | int i; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | 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 Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 106 | if (op != p && |
Stephen Hemminger | 6ede246 | 2005-10-25 15:04:59 -0700 | [diff] [blame] | 107 | !compare_ether_addr(op->dev->dev_addr, |
| 108 | f->addr.addr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | 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 | |
| 127 | void br_fdb_cleanup(unsigned long _data) |
| 128 | { |
| 129 | struct net_bridge *br = (struct net_bridge *)_data; |
| 130 | unsigned long delay = hold_time(br); |
Baruch Even | 071f772 | 2007-05-31 01:20:45 -0700 | [diff] [blame] | 131 | unsigned long next_timer = jiffies + br->forward_delay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | 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 Even | 071f772 | 2007-05-31 01:20:45 -0700 | [diff] [blame] | 140 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | fdb_delete(f); |
Fabio Checconi | 2bec008 | 2008-03-20 15:54:58 -0700 | [diff] [blame] | 146 | else if (time_before(this_timer, next_timer)) |
Baruch Even | 071f772 | 2007-05-31 01:20:45 -0700 | [diff] [blame] | 147 | next_timer = this_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | spin_unlock_bh(&br->hash_lock); |
| 151 | |
Baruch Even | 071f772 | 2007-05-31 01:20:45 -0700 | [diff] [blame] | 152 | /* Add HZ/4 to ensure we round the jiffies upwards to be after the next |
| 153 | * timer, otherwise we might round down and will have no-op run. */ |
| 154 | mod_timer(&br->gc_timer, round_jiffies(next_timer + HZ/4)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 157 | /* Completely flush all dynamic entries in forwarding database.*/ |
| 158 | void br_fdb_flush(struct net_bridge *br) |
| 159 | { |
| 160 | int i; |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 161 | |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 162 | spin_lock_bh(&br->hash_lock); |
| 163 | for (i = 0; i < BR_HASH_SIZE; i++) { |
| 164 | struct net_bridge_fdb_entry *f; |
| 165 | struct hlist_node *h, *n; |
| 166 | hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) { |
| 167 | if (!f->is_static) |
| 168 | fdb_delete(f); |
| 169 | } |
| 170 | } |
| 171 | spin_unlock_bh(&br->hash_lock); |
| 172 | } |
| 173 | |
| 174 | /* Flush all entries refering to a specific port. |
| 175 | * if do_all is set also flush static entries |
| 176 | */ |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 177 | void br_fdb_delete_by_port(struct net_bridge *br, |
| 178 | const struct net_bridge_port *p, |
| 179 | int do_all) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
| 181 | int i; |
| 182 | |
| 183 | spin_lock_bh(&br->hash_lock); |
| 184 | for (i = 0; i < BR_HASH_SIZE; i++) { |
| 185 | struct hlist_node *h, *g; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | hlist_for_each_safe(h, g, &br->hash[i]) { |
| 188 | struct net_bridge_fdb_entry *f |
| 189 | = hlist_entry(h, struct net_bridge_fdb_entry, hlist); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 190 | if (f->dst != p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | continue; |
| 192 | |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 193 | if (f->is_static && !do_all) |
| 194 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | /* |
| 196 | * if multiple ports all have the same device address |
| 197 | * then when one port is deleted, assign |
| 198 | * the local entry to other port |
| 199 | */ |
| 200 | if (f->is_local) { |
| 201 | struct net_bridge_port *op; |
| 202 | list_for_each_entry(op, &br->port_list, list) { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 203 | if (op != p && |
Stephen Hemminger | 6ede246 | 2005-10-25 15:04:59 -0700 | [diff] [blame] | 204 | !compare_ether_addr(op->dev->dev_addr, |
| 205 | f->addr.addr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | f->dst = op; |
| 207 | goto skip_delete; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | fdb_delete(f); |
| 213 | skip_delete: ; |
| 214 | } |
| 215 | } |
| 216 | spin_unlock_bh(&br->hash_lock); |
| 217 | } |
| 218 | |
| 219 | /* No locking or refcounting, assumes caller has no preempt (rcu_read_lock) */ |
| 220 | struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br, |
| 221 | const unsigned char *addr) |
| 222 | { |
| 223 | struct hlist_node *h; |
| 224 | struct net_bridge_fdb_entry *fdb; |
| 225 | |
| 226 | hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) { |
Stephen Hemminger | 6ede246 | 2005-10-25 15:04:59 -0700 | [diff] [blame] | 227 | if (!compare_ether_addr(fdb->addr.addr, addr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | if (unlikely(has_expired(br, fdb))) |
| 229 | break; |
| 230 | return fdb; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return NULL; |
| 235 | } |
| 236 | |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 237 | #if defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE) |
| 238 | /* Interface used by ATM LANE hook to test |
| 239 | * if an addr is on some other bridge port */ |
| 240 | int br_fdb_test_addr(struct net_device *dev, unsigned char *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
| 242 | struct net_bridge_fdb_entry *fdb; |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 243 | int ret; |
| 244 | |
| 245 | if (!dev->br_port) |
| 246 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | rcu_read_lock(); |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 249 | fdb = __br_fdb_get(dev->br_port->br, addr); |
| 250 | ret = fdb && fdb->dst->dev != dev && |
| 251 | fdb->dst->state == BR_STATE_FORWARDING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 254 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 256 | #endif /* CONFIG_ATM_LANE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | /* |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 259 | * Fill buffer with forwarding table records in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | * the API format. |
| 261 | */ |
| 262 | int br_fdb_fillbuf(struct net_bridge *br, void *buf, |
| 263 | unsigned long maxnum, unsigned long skip) |
| 264 | { |
| 265 | struct __fdb_entry *fe = buf; |
| 266 | int i, num = 0; |
| 267 | struct hlist_node *h; |
| 268 | struct net_bridge_fdb_entry *f; |
| 269 | |
| 270 | memset(buf, 0, maxnum*sizeof(struct __fdb_entry)); |
| 271 | |
| 272 | rcu_read_lock(); |
| 273 | for (i = 0; i < BR_HASH_SIZE; i++) { |
| 274 | hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) { |
| 275 | if (num >= maxnum) |
| 276 | goto out; |
| 277 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 278 | if (has_expired(br, f)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | continue; |
| 280 | |
| 281 | if (skip) { |
| 282 | --skip; |
| 283 | continue; |
| 284 | } |
| 285 | |
| 286 | /* convert from internal format to API */ |
| 287 | memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN); |
Stephen Hemminger | ae4f8fc | 2008-05-02 16:53:33 -0700 | [diff] [blame] | 288 | |
| 289 | /* due to ABI compat need to split into hi/lo */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | fe->port_no = f->dst->port_no; |
Stephen Hemminger | ae4f8fc | 2008-05-02 16:53:33 -0700 | [diff] [blame] | 291 | fe->port_hi = f->dst->port_no >> 8; |
| 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | fe->is_local = f->is_local; |
| 294 | if (!f->is_static) |
| 295 | fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer); |
| 296 | ++fe; |
| 297 | ++num; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | out: |
| 302 | rcu_read_unlock(); |
| 303 | |
| 304 | return num; |
| 305 | } |
| 306 | |
| 307 | static inline struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head, |
| 308 | const unsigned char *addr) |
| 309 | { |
| 310 | struct hlist_node *h; |
| 311 | struct net_bridge_fdb_entry *fdb; |
| 312 | |
| 313 | hlist_for_each_entry_rcu(fdb, h, head, hlist) { |
Stephen Hemminger | 6ede246 | 2005-10-25 15:04:59 -0700 | [diff] [blame] | 314 | if (!compare_ether_addr(fdb->addr.addr, addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | return fdb; |
| 316 | } |
| 317 | return NULL; |
| 318 | } |
| 319 | |
| 320 | static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head, |
| 321 | struct net_bridge_port *source, |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 322 | const unsigned char *addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | int is_local) |
| 324 | { |
| 325 | struct net_bridge_fdb_entry *fdb; |
| 326 | |
| 327 | fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC); |
| 328 | if (fdb) { |
| 329 | memcpy(fdb->addr.addr, addr, ETH_ALEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | hlist_add_head_rcu(&fdb->hlist, head); |
| 331 | |
| 332 | fdb->dst = source; |
| 333 | fdb->is_local = is_local; |
| 334 | fdb->is_static = is_local; |
| 335 | fdb->ageing_timer = jiffies; |
| 336 | } |
| 337 | return fdb; |
| 338 | } |
| 339 | |
| 340 | static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source, |
| 341 | const unsigned char *addr) |
| 342 | { |
| 343 | struct hlist_head *head = &br->hash[br_mac_hash(addr)]; |
| 344 | struct net_bridge_fdb_entry *fdb; |
| 345 | |
| 346 | if (!is_valid_ether_addr(addr)) |
| 347 | return -EINVAL; |
| 348 | |
| 349 | fdb = fdb_find(head, addr); |
| 350 | if (fdb) { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 351 | /* it is okay to have multiple ports with same |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | * address, just use the first one. |
| 353 | */ |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 354 | if (fdb->is_local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | return 0; |
| 356 | |
| 357 | printk(KERN_WARNING "%s adding interface with same address " |
| 358 | "as a received packet\n", |
| 359 | source->dev->name); |
| 360 | fdb_delete(fdb); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
| 363 | if (!fdb_create(head, source, addr, 1)) |
| 364 | return -ENOMEM; |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | int 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 | |
| 380 | void 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 Hemminger | df1c0b8 | 2007-08-30 22:15:35 -0700 | [diff] [blame] | 390 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | 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 Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 399 | if (net_ratelimit()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | printk(KERN_WARNING "%s: received packet with " |
Jiri Pirko | 0bb3241 | 2009-05-27 00:55:08 +0000 | [diff] [blame] | 401 | "own address as source address\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | source->dev->name); |
| 403 | } else { |
| 404 | /* fastpath: update of existing entry */ |
| 405 | fdb->dst = source; |
| 406 | fdb->ageing_timer = jiffies; |
| 407 | } |
| 408 | } else { |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 409 | spin_lock(&br->hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | 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 Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 415 | spin_unlock(&br->hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |