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