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