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