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> |
Arun Sharma | 6006349 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 24 | #include <linux/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); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 31 | static void fdb_notify(const struct net_bridge_fdb_entry *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 33 | static u32 fdb_salt __read_mostly; |
| 34 | |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 35 | int __init br_fdb_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
| 37 | br_fdb_cache = kmem_cache_create("bridge_fdb_cache", |
| 38 | sizeof(struct net_bridge_fdb_entry), |
| 39 | 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 40 | SLAB_HWCACHE_ALIGN, NULL); |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 41 | if (!br_fdb_cache) |
| 42 | return -ENOMEM; |
| 43 | |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 44 | get_random_bytes(&fdb_salt, sizeof(fdb_salt)); |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 45 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Andrew Morton | 73afc90 | 2007-12-05 21:35:23 -0800 | [diff] [blame] | 48 | void br_fdb_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
| 50 | kmem_cache_destroy(br_fdb_cache); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | /* if topology_changing then use forward_delay (default 15 sec) |
| 55 | * otherwise keep longer (default 5 minutes) |
| 56 | */ |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 57 | static inline unsigned long hold_time(const struct net_bridge *br) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
| 59 | return br->topology_change ? br->forward_delay : br->ageing_time; |
| 60 | } |
| 61 | |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 62 | static inline int has_expired(const struct net_bridge *br, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | const struct net_bridge_fdb_entry *fdb) |
| 64 | { |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 65 | return !fdb->is_static && |
stephen hemminger | 7cd8861 | 2011-04-04 14:03:28 +0000 | [diff] [blame] | 66 | time_before_eq(fdb->updated + hold_time(br), jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 69 | static inline int br_mac_hash(const unsigned char *mac) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 71 | /* use 1 byte of OUI cnd 3 bytes of NIC */ |
| 72 | u32 key = get_unaligned((u32 *)(mac + 2)); |
| 73 | return jhash_1word(key, fdb_salt) & (BR_HASH_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 76 | static void fdb_rcu_free(struct rcu_head *head) |
| 77 | { |
| 78 | struct net_bridge_fdb_entry *ent |
| 79 | = container_of(head, struct net_bridge_fdb_entry, rcu); |
| 80 | kmem_cache_free(br_fdb_cache, ent); |
| 81 | } |
| 82 | |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 83 | static inline void fdb_delete(struct net_bridge_fdb_entry *f) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 85 | fdb_notify(f, RTM_DELNEIGH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | hlist_del_rcu(&f->hlist); |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 87 | call_rcu(&f->rcu, fdb_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr) |
| 91 | { |
| 92 | struct net_bridge *br = p->br; |
| 93 | int i; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | spin_lock_bh(&br->hash_lock); |
| 96 | |
| 97 | /* Search all chains since old address/hash is unknown */ |
| 98 | for (i = 0; i < BR_HASH_SIZE; i++) { |
| 99 | struct hlist_node *h; |
| 100 | hlist_for_each(h, &br->hash[i]) { |
| 101 | struct net_bridge_fdb_entry *f; |
| 102 | |
| 103 | f = hlist_entry(h, struct net_bridge_fdb_entry, hlist); |
| 104 | if (f->dst == p && f->is_local) { |
| 105 | /* maybe another port has same hw addr? */ |
| 106 | struct net_bridge_port *op; |
| 107 | list_for_each_entry(op, &br->port_list, list) { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 108 | if (op != p && |
Stephen Hemminger | 6ede246 | 2005-10-25 15:04:59 -0700 | [diff] [blame] | 109 | !compare_ether_addr(op->dev->dev_addr, |
| 110 | f->addr.addr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | f->dst = op; |
| 112 | goto insert; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /* delete old one */ |
| 117 | fdb_delete(f); |
| 118 | goto insert; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | insert: |
| 123 | /* insert new address, may fail if invalid address or dup. */ |
| 124 | fdb_insert(br, p, newaddr); |
| 125 | |
| 126 | spin_unlock_bh(&br->hash_lock); |
| 127 | } |
| 128 | |
| 129 | void br_fdb_cleanup(unsigned long _data) |
| 130 | { |
| 131 | struct net_bridge *br = (struct net_bridge *)_data; |
| 132 | unsigned long delay = hold_time(br); |
stephen hemminger | 25442e0 | 2010-06-15 06:14:12 +0000 | [diff] [blame] | 133 | unsigned long next_timer = jiffies + br->ageing_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | int i; |
| 135 | |
| 136 | spin_lock_bh(&br->hash_lock); |
| 137 | for (i = 0; i < BR_HASH_SIZE; i++) { |
| 138 | struct net_bridge_fdb_entry *f; |
| 139 | struct hlist_node *h, *n; |
| 140 | |
| 141 | hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) { |
Baruch Even | 071f772 | 2007-05-31 01:20:45 -0700 | [diff] [blame] | 142 | unsigned long this_timer; |
| 143 | if (f->is_static) |
| 144 | continue; |
stephen hemminger | 7cd8861 | 2011-04-04 14:03:28 +0000 | [diff] [blame] | 145 | this_timer = f->updated + delay; |
Baruch Even | 071f772 | 2007-05-31 01:20:45 -0700 | [diff] [blame] | 146 | if (time_before_eq(this_timer, jiffies)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | fdb_delete(f); |
Fabio Checconi | 2bec008 | 2008-03-20 15:54:58 -0700 | [diff] [blame] | 148 | else if (time_before(this_timer, next_timer)) |
Baruch Even | 071f772 | 2007-05-31 01:20:45 -0700 | [diff] [blame] | 149 | next_timer = this_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | spin_unlock_bh(&br->hash_lock); |
| 153 | |
stephen hemminger | 25442e0 | 2010-06-15 06:14:12 +0000 | [diff] [blame] | 154 | mod_timer(&br->gc_timer, round_jiffies_up(next_timer)); |
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 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 174 | /* Flush all entries referring to a specific port. |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 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 | |
stephen hemminger | eeaf61d | 2010-07-27 08:26:30 +0000 | [diff] [blame] | 219 | /* No locking or refcounting, assumes caller has rcu_read_lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 243 | struct net_bridge_port *port; |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 244 | int ret; |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | rcu_read_lock(); |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 247 | port = br_port_get_rcu(dev); |
| 248 | if (!port) |
| 249 | ret = 0; |
| 250 | else { |
| 251 | fdb = __br_fdb_get(port->br, addr); |
| 252 | ret = fdb && fdb->dst->dev != dev && |
| 253 | fdb->dst->state == BR_STATE_FORWARDING; |
| 254 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 257 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 259 | #endif /* CONFIG_ATM_LANE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
| 261 | /* |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 262 | * Fill buffer with forwarding table records in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | * the API format. |
| 264 | */ |
| 265 | int br_fdb_fillbuf(struct net_bridge *br, void *buf, |
| 266 | unsigned long maxnum, unsigned long skip) |
| 267 | { |
| 268 | struct __fdb_entry *fe = buf; |
| 269 | int i, num = 0; |
| 270 | struct hlist_node *h; |
| 271 | struct net_bridge_fdb_entry *f; |
| 272 | |
| 273 | memset(buf, 0, maxnum*sizeof(struct __fdb_entry)); |
| 274 | |
| 275 | rcu_read_lock(); |
| 276 | for (i = 0; i < BR_HASH_SIZE; i++) { |
| 277 | hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) { |
| 278 | if (num >= maxnum) |
| 279 | goto out; |
| 280 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 281 | if (has_expired(br, f)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | continue; |
| 283 | |
| 284 | if (skip) { |
| 285 | --skip; |
| 286 | continue; |
| 287 | } |
| 288 | |
| 289 | /* convert from internal format to API */ |
| 290 | memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN); |
Stephen Hemminger | ae4f8fc | 2008-05-02 16:53:33 -0700 | [diff] [blame] | 291 | |
| 292 | /* due to ABI compat need to split into hi/lo */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | fe->port_no = f->dst->port_no; |
Stephen Hemminger | ae4f8fc | 2008-05-02 16:53:33 -0700 | [diff] [blame] | 294 | fe->port_hi = f->dst->port_no >> 8; |
| 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | fe->is_local = f->is_local; |
| 297 | if (!f->is_static) |
stephen hemminger | 7cd8861 | 2011-04-04 14:03:28 +0000 | [diff] [blame] | 298 | fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->updated); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | ++fe; |
| 300 | ++num; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | out: |
| 305 | rcu_read_unlock(); |
| 306 | |
| 307 | return num; |
| 308 | } |
| 309 | |
stephen hemminger | 664de48 | 2011-04-04 14:03:29 +0000 | [diff] [blame] | 310 | static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head, |
| 311 | const unsigned char *addr) |
| 312 | { |
| 313 | struct hlist_node *h; |
| 314 | struct net_bridge_fdb_entry *fdb; |
| 315 | |
| 316 | hlist_for_each_entry(fdb, h, head, hlist) { |
| 317 | if (!compare_ether_addr(fdb->addr.addr, addr)) |
| 318 | return fdb; |
| 319 | } |
| 320 | return NULL; |
| 321 | } |
| 322 | |
| 323 | static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head, |
| 324 | const unsigned char *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
| 326 | struct hlist_node *h; |
| 327 | struct net_bridge_fdb_entry *fdb; |
| 328 | |
| 329 | hlist_for_each_entry_rcu(fdb, h, head, hlist) { |
Stephen Hemminger | 6ede246 | 2005-10-25 15:04:59 -0700 | [diff] [blame] | 330 | if (!compare_ether_addr(fdb->addr.addr, addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | return fdb; |
| 332 | } |
| 333 | return NULL; |
| 334 | } |
| 335 | |
| 336 | static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head, |
| 337 | struct net_bridge_port *source, |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 338 | const unsigned char *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | { |
| 340 | struct net_bridge_fdb_entry *fdb; |
| 341 | |
| 342 | fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC); |
| 343 | if (fdb) { |
| 344 | memcpy(fdb->addr.addr, addr, ETH_ALEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | fdb->dst = source; |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 346 | fdb->is_local = 0; |
| 347 | fdb->is_static = 0; |
stephen hemminger | 7cd8861 | 2011-04-04 14:03:28 +0000 | [diff] [blame] | 348 | fdb->updated = fdb->used = jiffies; |
Pavel Emelyanov | 1158f76 | 2011-02-04 13:02:36 -0800 | [diff] [blame] | 349 | hlist_add_head_rcu(&fdb->hlist, head); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 350 | fdb_notify(fdb, RTM_NEWNEIGH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } |
| 352 | return fdb; |
| 353 | } |
| 354 | |
| 355 | static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source, |
| 356 | const unsigned char *addr) |
| 357 | { |
| 358 | struct hlist_head *head = &br->hash[br_mac_hash(addr)]; |
| 359 | struct net_bridge_fdb_entry *fdb; |
| 360 | |
| 361 | if (!is_valid_ether_addr(addr)) |
| 362 | return -EINVAL; |
| 363 | |
| 364 | fdb = fdb_find(head, addr); |
| 365 | if (fdb) { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 366 | /* it is okay to have multiple ports with same |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | * address, just use the first one. |
| 368 | */ |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 369 | if (fdb->is_local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | return 0; |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 371 | br_warn(br, "adding interface %s with same address " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | "as a received packet\n", |
| 373 | source->dev->name); |
| 374 | fdb_delete(fdb); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 375 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 377 | fdb = fdb_create(head, source, addr); |
| 378 | if (!fdb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | return -ENOMEM; |
| 380 | |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 381 | fdb->is_local = fdb->is_static = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 385 | /* Add entry for local address of interface */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source, |
| 387 | const unsigned char *addr) |
| 388 | { |
| 389 | int ret; |
| 390 | |
| 391 | spin_lock_bh(&br->hash_lock); |
| 392 | ret = fdb_insert(br, source, addr); |
| 393 | spin_unlock_bh(&br->hash_lock); |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source, |
| 398 | const unsigned char *addr) |
| 399 | { |
| 400 | struct hlist_head *head = &br->hash[br_mac_hash(addr)]; |
| 401 | struct net_bridge_fdb_entry *fdb; |
| 402 | |
| 403 | /* some users want to always flood. */ |
| 404 | if (hold_time(br) == 0) |
| 405 | return; |
| 406 | |
Stephen Hemminger | df1c0b8 | 2007-08-30 22:15:35 -0700 | [diff] [blame] | 407 | /* ignore packets unless we are using this port */ |
| 408 | if (!(source->state == BR_STATE_LEARNING || |
| 409 | source->state == BR_STATE_FORWARDING)) |
| 410 | return; |
| 411 | |
stephen hemminger | 664de48 | 2011-04-04 14:03:29 +0000 | [diff] [blame] | 412 | fdb = fdb_find_rcu(head, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | if (likely(fdb)) { |
| 414 | /* attempt to update an entry for a local interface */ |
| 415 | if (unlikely(fdb->is_local)) { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 416 | if (net_ratelimit()) |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 417 | br_warn(br, "received packet on %s with " |
| 418 | "own address as source address\n", |
| 419 | source->dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } else { |
| 421 | /* fastpath: update of existing entry */ |
| 422 | fdb->dst = source; |
stephen hemminger | 7cd8861 | 2011-04-04 14:03:28 +0000 | [diff] [blame] | 423 | fdb->updated = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | } else { |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 426 | spin_lock(&br->hash_lock); |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 427 | if (likely(!fdb_find(head, addr))) |
| 428 | fdb_create(head, source, addr); |
| 429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | /* else we lose race and someone else inserts |
| 431 | * it first, don't bother updating |
| 432 | */ |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 433 | spin_unlock(&br->hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 436 | |
| 437 | static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb) |
| 438 | { |
| 439 | if (fdb->is_local) |
| 440 | return NUD_PERMANENT; |
| 441 | else if (fdb->is_static) |
| 442 | return NUD_NOARP; |
| 443 | else if (has_expired(fdb->dst->br, fdb)) |
| 444 | return NUD_STALE; |
| 445 | else |
| 446 | return NUD_REACHABLE; |
| 447 | } |
| 448 | |
| 449 | static int fdb_fill_info(struct sk_buff *skb, |
| 450 | const struct net_bridge_fdb_entry *fdb, |
| 451 | u32 pid, u32 seq, int type, unsigned int flags) |
| 452 | { |
| 453 | unsigned long now = jiffies; |
| 454 | struct nda_cacheinfo ci; |
| 455 | struct nlmsghdr *nlh; |
| 456 | struct ndmsg *ndm; |
| 457 | |
| 458 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags); |
| 459 | if (nlh == NULL) |
| 460 | return -EMSGSIZE; |
| 461 | |
| 462 | |
| 463 | ndm = nlmsg_data(nlh); |
| 464 | ndm->ndm_family = AF_BRIDGE; |
| 465 | ndm->ndm_pad1 = 0; |
| 466 | ndm->ndm_pad2 = 0; |
| 467 | ndm->ndm_flags = 0; |
| 468 | ndm->ndm_type = 0; |
| 469 | ndm->ndm_ifindex = fdb->dst->dev->ifindex; |
| 470 | ndm->ndm_state = fdb_to_nud(fdb); |
| 471 | |
| 472 | NLA_PUT(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr); |
| 473 | |
| 474 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); |
| 475 | ci.ndm_confirmed = 0; |
| 476 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); |
| 477 | ci.ndm_refcnt = 0; |
| 478 | NLA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci); |
| 479 | |
| 480 | return nlmsg_end(skb, nlh); |
| 481 | |
| 482 | nla_put_failure: |
| 483 | nlmsg_cancel(skb, nlh); |
| 484 | return -EMSGSIZE; |
| 485 | } |
| 486 | |
| 487 | static inline size_t fdb_nlmsg_size(void) |
| 488 | { |
| 489 | return NLMSG_ALIGN(sizeof(struct ndmsg)) |
| 490 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ |
| 491 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
| 492 | } |
| 493 | |
| 494 | static void fdb_notify(const struct net_bridge_fdb_entry *fdb, int type) |
| 495 | { |
| 496 | struct net *net = dev_net(fdb->dst->dev); |
| 497 | struct sk_buff *skb; |
| 498 | int err = -ENOBUFS; |
| 499 | |
| 500 | skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC); |
| 501 | if (skb == NULL) |
| 502 | goto errout; |
| 503 | |
| 504 | err = fdb_fill_info(skb, fdb, 0, 0, type, 0); |
| 505 | if (err < 0) { |
| 506 | /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */ |
| 507 | WARN_ON(err == -EMSGSIZE); |
| 508 | kfree_skb(skb); |
| 509 | goto errout; |
| 510 | } |
| 511 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 512 | return; |
| 513 | errout: |
| 514 | if (err < 0) |
| 515 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
| 516 | } |
| 517 | |
| 518 | /* Dump information about entries, in response to GETNEIGH */ |
| 519 | int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 520 | { |
| 521 | struct net *net = sock_net(skb->sk); |
| 522 | struct net_device *dev; |
| 523 | int idx = 0; |
| 524 | |
| 525 | rcu_read_lock(); |
| 526 | for_each_netdev_rcu(net, dev) { |
| 527 | struct net_bridge *br = netdev_priv(dev); |
| 528 | int i; |
| 529 | |
| 530 | if (!(dev->priv_flags & IFF_EBRIDGE)) |
| 531 | continue; |
| 532 | |
| 533 | for (i = 0; i < BR_HASH_SIZE; i++) { |
| 534 | struct hlist_node *h; |
| 535 | struct net_bridge_fdb_entry *f; |
| 536 | |
| 537 | hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) { |
| 538 | if (idx < cb->args[0]) |
| 539 | goto skip; |
| 540 | |
| 541 | if (fdb_fill_info(skb, f, |
| 542 | NETLINK_CB(cb->skb).pid, |
| 543 | cb->nlh->nlmsg_seq, |
| 544 | RTM_NEWNEIGH, |
| 545 | NLM_F_MULTI) < 0) |
| 546 | break; |
| 547 | skip: |
| 548 | ++idx; |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | rcu_read_unlock(); |
| 553 | |
| 554 | cb->args[0] = idx; |
| 555 | |
| 556 | return skb->len; |
| 557 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 558 | |
| 559 | /* Create new static fdb entry */ |
| 560 | static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr, |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame^] | 561 | __u16 state, __u16 flags) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 562 | { |
| 563 | struct net_bridge *br = source->br; |
| 564 | struct hlist_head *head = &br->hash[br_mac_hash(addr)]; |
| 565 | struct net_bridge_fdb_entry *fdb; |
| 566 | |
| 567 | fdb = fdb_find(head, addr); |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame^] | 568 | if (fdb == NULL) { |
| 569 | if (!(flags & NLM_F_CREATE)) |
| 570 | return -ENOENT; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 571 | |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame^] | 572 | fdb = fdb_create(head, source, addr); |
| 573 | if (!fdb) |
| 574 | return -ENOMEM; |
| 575 | } else { |
| 576 | if (flags & NLM_F_EXCL) |
| 577 | return -EEXIST; |
| 578 | |
| 579 | if (flags & NLM_F_REPLACE) |
| 580 | fdb->updated = fdb->used = jiffies; |
| 581 | fdb->is_local = fdb->is_static = 0; |
| 582 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 583 | |
| 584 | if (state & NUD_PERMANENT) |
| 585 | fdb->is_local = fdb->is_static = 1; |
| 586 | else if (state & NUD_NOARP) |
| 587 | fdb->is_static = 1; |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | /* Add new permanent fdb entry with RTM_NEWNEIGH */ |
| 592 | int br_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
| 593 | { |
| 594 | struct net *net = sock_net(skb->sk); |
| 595 | struct ndmsg *ndm; |
| 596 | struct nlattr *tb[NDA_MAX+1]; |
| 597 | struct net_device *dev; |
| 598 | struct net_bridge_port *p; |
| 599 | const __u8 *addr; |
| 600 | int err; |
| 601 | |
| 602 | ASSERT_RTNL(); |
| 603 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); |
| 604 | if (err < 0) |
| 605 | return err; |
| 606 | |
| 607 | ndm = nlmsg_data(nlh); |
| 608 | if (ndm->ndm_ifindex == 0) { |
| 609 | pr_info("bridge: RTM_NEWNEIGH with invalid ifindex\n"); |
| 610 | return -EINVAL; |
| 611 | } |
| 612 | |
| 613 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
| 614 | if (dev == NULL) { |
| 615 | pr_info("bridge: RTM_NEWNEIGH with unknown ifindex\n"); |
| 616 | return -ENODEV; |
| 617 | } |
| 618 | |
| 619 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
| 620 | pr_info("bridge: RTM_NEWNEIGH with invalid address\n"); |
| 621 | return -EINVAL; |
| 622 | } |
| 623 | |
| 624 | addr = nla_data(tb[NDA_LLADDR]); |
| 625 | if (!is_valid_ether_addr(addr)) { |
| 626 | pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n"); |
| 627 | return -EINVAL; |
| 628 | } |
| 629 | |
| 630 | p = br_port_get_rtnl(dev); |
| 631 | if (p == NULL) { |
| 632 | pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n", |
| 633 | dev->name); |
| 634 | return -EINVAL; |
| 635 | } |
| 636 | |
| 637 | spin_lock_bh(&p->br->hash_lock); |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame^] | 638 | err = fdb_add_entry(p, addr, ndm->ndm_state, nlh->nlmsg_flags); |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 639 | spin_unlock_bh(&p->br->hash_lock); |
| 640 | |
| 641 | return err; |
| 642 | } |
| 643 | |
| 644 | static int fdb_delete_by_addr(struct net_bridge_port *p, const u8 *addr) |
| 645 | { |
| 646 | struct net_bridge *br = p->br; |
| 647 | struct hlist_head *head = &br->hash[br_mac_hash(addr)]; |
| 648 | struct net_bridge_fdb_entry *fdb; |
| 649 | |
| 650 | fdb = fdb_find(head, addr); |
| 651 | if (!fdb) |
| 652 | return -ENOENT; |
| 653 | |
| 654 | fdb_delete(fdb); |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | /* Remove neighbor entry with RTM_DELNEIGH */ |
| 659 | int br_fdb_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
| 660 | { |
| 661 | struct net *net = sock_net(skb->sk); |
| 662 | struct ndmsg *ndm; |
| 663 | struct net_bridge_port *p; |
| 664 | struct nlattr *llattr; |
| 665 | const __u8 *addr; |
| 666 | struct net_device *dev; |
| 667 | int err; |
| 668 | |
| 669 | ASSERT_RTNL(); |
| 670 | if (nlmsg_len(nlh) < sizeof(*ndm)) |
| 671 | return -EINVAL; |
| 672 | |
| 673 | ndm = nlmsg_data(nlh); |
| 674 | if (ndm->ndm_ifindex == 0) { |
| 675 | pr_info("bridge: RTM_DELNEIGH with invalid ifindex\n"); |
| 676 | return -EINVAL; |
| 677 | } |
| 678 | |
| 679 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
| 680 | if (dev == NULL) { |
| 681 | pr_info("bridge: RTM_DELNEIGH with unknown ifindex\n"); |
| 682 | return -ENODEV; |
| 683 | } |
| 684 | |
| 685 | llattr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_LLADDR); |
| 686 | if (llattr == NULL || nla_len(llattr) != ETH_ALEN) { |
| 687 | pr_info("bridge: RTM_DELNEIGH with invalid address\n"); |
| 688 | return -EINVAL; |
| 689 | } |
| 690 | |
| 691 | addr = nla_data(llattr); |
| 692 | |
| 693 | p = br_port_get_rtnl(dev); |
| 694 | if (p == NULL) { |
| 695 | pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n", |
| 696 | dev->name); |
| 697 | return -EINVAL; |
| 698 | } |
| 699 | |
| 700 | spin_lock_bh(&p->br->hash_lock); |
| 701 | err = fdb_delete_by_addr(p, addr); |
| 702 | spin_unlock_bh(&p->br->hash_lock); |
| 703 | |
| 704 | return err; |
| 705 | } |