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> |
Scott Feldman | b4ad7ba | 2015-06-14 11:33:11 -0700 | [diff] [blame] | 27 | #include <net/switchdev.h> |
Roopa Prabhu | b74fd30 | 2017-08-29 13:16:57 -0700 | [diff] [blame] | 28 | #include <trace/events/bridge.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "br_private.h" |
| 30 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 31 | static const struct rhashtable_params br_fdb_rht_params = { |
| 32 | .head_offset = offsetof(struct net_bridge_fdb_entry, rhnode), |
| 33 | .key_offset = offsetof(struct net_bridge_fdb_entry, key), |
| 34 | .key_len = sizeof(struct net_bridge_fdb_key), |
| 35 | .automatic_shrinking = true, |
| 36 | .locks_mul = 1, |
| 37 | }; |
| 38 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 39 | static struct kmem_cache *br_fdb_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | 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] | 41 | const unsigned char *addr, u16 vid); |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 42 | static void fdb_notify(struct net_bridge *br, |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 43 | const struct net_bridge_fdb_entry *, int, bool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 45 | int __init br_fdb_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
| 47 | br_fdb_cache = kmem_cache_create("bridge_fdb_cache", |
| 48 | sizeof(struct net_bridge_fdb_entry), |
| 49 | 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 50 | SLAB_HWCACHE_ALIGN, NULL); |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 51 | if (!br_fdb_cache) |
| 52 | return -ENOMEM; |
| 53 | |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 54 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Andrew Morton | 73afc90 | 2007-12-05 21:35:23 -0800 | [diff] [blame] | 57 | void br_fdb_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
| 59 | kmem_cache_destroy(br_fdb_cache); |
| 60 | } |
| 61 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 62 | int br_fdb_hash_init(struct net_bridge *br) |
| 63 | { |
| 64 | return rhashtable_init(&br->fdb_hash_tbl, &br_fdb_rht_params); |
| 65 | } |
| 66 | |
| 67 | void br_fdb_hash_fini(struct net_bridge *br) |
| 68 | { |
| 69 | rhashtable_destroy(&br->fdb_hash_tbl); |
| 70 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | /* if topology_changing then use forward_delay (default 15 sec) |
| 73 | * otherwise keep longer (default 5 minutes) |
| 74 | */ |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 75 | static inline unsigned long hold_time(const struct net_bridge *br) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | return br->topology_change ? br->forward_delay : br->ageing_time; |
| 78 | } |
| 79 | |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 80 | static inline int has_expired(const struct net_bridge *br, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | const struct net_bridge_fdb_entry *fdb) |
| 82 | { |
Roopa Prabhu | eda7a5e | 2017-02-16 13:38:04 -0800 | [diff] [blame] | 83 | return !fdb->is_static && !fdb->added_by_external_learn && |
stephen hemminger | 7cd8861 | 2011-04-04 14:03:28 +0000 | [diff] [blame] | 84 | time_before_eq(fdb->updated + hold_time(br), jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 87 | static void fdb_rcu_free(struct rcu_head *head) |
| 88 | { |
| 89 | struct net_bridge_fdb_entry *ent |
| 90 | = container_of(head, struct net_bridge_fdb_entry, rcu); |
| 91 | kmem_cache_free(br_fdb_cache, ent); |
| 92 | } |
| 93 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 94 | static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl, |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 95 | const unsigned char *addr, |
| 96 | __u16 vid) |
| 97 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 98 | struct net_bridge_fdb_key key; |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 99 | |
Nikolay Aleksandrov | 410b3d4 | 2017-02-13 14:59:10 +0100 | [diff] [blame] | 100 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 101 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 102 | key.vlan_id = vid; |
| 103 | memcpy(key.addr.addr, addr, sizeof(key.addr.addr)); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 104 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 105 | return rhashtable_lookup(tbl, &key, br_fdb_rht_params); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* requires bridge hash_lock */ |
| 109 | static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br, |
| 110 | const unsigned char *addr, |
| 111 | __u16 vid) |
| 112 | { |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 113 | struct net_bridge_fdb_entry *fdb; |
| 114 | |
WANG Cong | d12c917 | 2017-03-16 10:32:42 -0700 | [diff] [blame] | 115 | lockdep_assert_held_once(&br->hash_lock); |
Nikolay Aleksandrov | 410b3d4 | 2017-02-13 14:59:10 +0100 | [diff] [blame] | 116 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 117 | rcu_read_lock(); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 118 | fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 119 | rcu_read_unlock(); |
| 120 | |
| 121 | return fdb; |
| 122 | } |
| 123 | |
Petr Machata | 4d4fd36 | 2018-04-29 10:56:08 +0300 | [diff] [blame] | 124 | struct net_device *br_fdb_find_port(const struct net_device *br_dev, |
| 125 | const unsigned char *addr, |
| 126 | __u16 vid) |
| 127 | { |
| 128 | struct net_bridge_fdb_entry *f; |
| 129 | struct net_device *dev = NULL; |
| 130 | struct net_bridge *br; |
| 131 | |
| 132 | ASSERT_RTNL(); |
| 133 | |
| 134 | if (!netif_is_bridge_master(br_dev)) |
| 135 | return NULL; |
| 136 | |
| 137 | br = netdev_priv(br_dev); |
Petr Machata | 873aca2 | 2018-06-08 15:11:47 +0200 | [diff] [blame] | 138 | rcu_read_lock(); |
| 139 | f = br_fdb_find_rcu(br, addr, vid); |
Petr Machata | 4d4fd36 | 2018-04-29 10:56:08 +0300 | [diff] [blame] | 140 | if (f && f->dst) |
| 141 | dev = f->dst->dev; |
Petr Machata | 873aca2 | 2018-06-08 15:11:47 +0200 | [diff] [blame] | 142 | rcu_read_unlock(); |
Petr Machata | 4d4fd36 | 2018-04-29 10:56:08 +0300 | [diff] [blame] | 143 | |
| 144 | return dev; |
| 145 | } |
| 146 | EXPORT_SYMBOL_GPL(br_fdb_find_port); |
| 147 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 148 | struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br, |
| 149 | const unsigned char *addr, |
| 150 | __u16 vid) |
| 151 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 152 | return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 155 | /* When a static FDB entry is added, the mac address from the entry is |
| 156 | * added to the bridge private HW address list and all required ports |
| 157 | * are then updated with the new information. |
| 158 | * Called under RTNL. |
| 159 | */ |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 160 | static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr) |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 161 | { |
| 162 | int err; |
Li RongQing | a3f5ee7 | 2014-06-18 16:07:16 +0800 | [diff] [blame] | 163 | struct net_bridge_port *p; |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 164 | |
| 165 | ASSERT_RTNL(); |
| 166 | |
| 167 | list_for_each_entry(p, &br->port_list, list) { |
| 168 | if (!br_promisc_port(p)) { |
| 169 | err = dev_uc_add(p->dev, addr); |
| 170 | if (err) |
| 171 | goto undo; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return; |
| 176 | undo: |
Li RongQing | a3f5ee7 | 2014-06-18 16:07:16 +0800 | [diff] [blame] | 177 | list_for_each_entry_continue_reverse(p, &br->port_list, list) { |
| 178 | if (!br_promisc_port(p)) |
| 179 | dev_uc_del(p->dev, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | /* When a static FDB entry is deleted, the HW address from that entry is |
| 184 | * also removed from the bridge private HW address list and updates all |
| 185 | * the ports with needed information. |
| 186 | * Called under RTNL. |
| 187 | */ |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 188 | static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr) |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 189 | { |
| 190 | struct net_bridge_port *p; |
| 191 | |
| 192 | ASSERT_RTNL(); |
| 193 | |
| 194 | list_for_each_entry(p, &br->port_list, list) { |
| 195 | if (!br_promisc_port(p)) |
| 196 | dev_uc_del(p->dev, addr); |
| 197 | } |
| 198 | } |
| 199 | |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 200 | static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f, |
| 201 | bool swdev_notify) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
Roopa Prabhu | b74fd30 | 2017-08-29 13:16:57 -0700 | [diff] [blame] | 203 | trace_fdb_delete(br, f); |
| 204 | |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 205 | if (f->is_static) |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 206 | fdb_del_hw_addr(br, f->key.addr.addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 207 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 208 | hlist_del_init_rcu(&f->fdb_node); |
| 209 | rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode, |
| 210 | br_fdb_rht_params); |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 211 | fdb_notify(br, f, RTM_DELNEIGH, swdev_notify); |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 212 | call_rcu(&f->rcu, fdb_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 215 | /* Delete a local entry if no other port had the same address. */ |
| 216 | static void fdb_delete_local(struct net_bridge *br, |
| 217 | const struct net_bridge_port *p, |
| 218 | struct net_bridge_fdb_entry *f) |
| 219 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 220 | const unsigned char *addr = f->key.addr.addr; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 221 | struct net_bridge_vlan_group *vg; |
| 222 | const struct net_bridge_vlan *v; |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 223 | struct net_bridge_port *op; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 224 | u16 vid = f->key.vlan_id; |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 225 | |
| 226 | /* Maybe another port has same hw addr? */ |
| 227 | list_for_each_entry(op, &br->port_list, list) { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 228 | vg = nbp_vlan_group(op); |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 229 | if (op != p && ether_addr_equal(op->dev->dev_addr, addr) && |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 230 | (!vid || br_vlan_find(vg, vid))) { |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 231 | f->dst = op; |
Toshiaki Makita | a778e6d | 2014-02-07 16:48:24 +0900 | [diff] [blame] | 232 | f->added_by_user = 0; |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 233 | return; |
| 234 | } |
| 235 | } |
| 236 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 237 | vg = br_vlan_group(br); |
| 238 | v = br_vlan_find(vg, vid); |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 239 | /* Maybe bridge device has same hw addr? */ |
| 240 | if (p && ether_addr_equal(br->dev->dev_addr, addr) && |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 241 | (!vid || (v && br_vlan_should_use(v)))) { |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 242 | f->dst = NULL; |
Toshiaki Makita | a778e6d | 2014-02-07 16:48:24 +0900 | [diff] [blame] | 243 | f->added_by_user = 0; |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 244 | return; |
| 245 | } |
| 246 | |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 247 | fdb_delete(br, f, true); |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 248 | } |
| 249 | |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 250 | void br_fdb_find_delete_local(struct net_bridge *br, |
| 251 | const struct net_bridge_port *p, |
| 252 | const unsigned char *addr, u16 vid) |
| 253 | { |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 254 | struct net_bridge_fdb_entry *f; |
| 255 | |
| 256 | spin_lock_bh(&br->hash_lock); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 257 | f = br_fdb_find(br, addr, vid); |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 258 | if (f && f->is_local && !f->added_by_user && f->dst == p) |
| 259 | fdb_delete_local(br, p, f); |
| 260 | spin_unlock_bh(&br->hash_lock); |
| 261 | } |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr) |
| 264 | { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 265 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 266 | struct net_bridge_fdb_entry *f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | struct net_bridge *br = p->br; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 268 | struct net_bridge_vlan *v; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | spin_lock_bh(&br->hash_lock); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 271 | vg = nbp_vlan_group(p); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 272 | hlist_for_each_entry(f, &br->fdb_list, fdb_node) { |
| 273 | if (f->dst == p && f->is_local && !f->added_by_user) { |
| 274 | /* delete old one */ |
| 275 | fdb_delete_local(br, p, f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 277 | /* if this port has no vlan information |
| 278 | * configured, we can safely be done at |
| 279 | * this point. |
| 280 | */ |
| 281 | if (!vg || !vg->num_vlans) |
| 282 | goto insert; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Toshiaki Makita | 2836882 | 2014-02-07 16:48:19 +0900 | [diff] [blame] | 286 | insert: |
| 287 | /* insert new address, may fail if invalid address or dup. */ |
| 288 | fdb_insert(br, p, newaddr, 0); |
| 289 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 290 | if (!vg || !vg->num_vlans) |
Toshiaki Makita | 2836882 | 2014-02-07 16:48:19 +0900 | [diff] [blame] | 291 | goto done; |
| 292 | |
| 293 | /* Now add entries for every VLAN configured on the port. |
| 294 | * This function runs under RTNL so the bitmap will not change |
| 295 | * from under us. |
| 296 | */ |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 297 | list_for_each_entry(v, &vg->vlan_list, vlist) |
| 298 | fdb_insert(br, p, newaddr, v->vid); |
Toshiaki Makita | 2836882 | 2014-02-07 16:48:19 +0900 | [diff] [blame] | 299 | |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 300 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | spin_unlock_bh(&br->hash_lock); |
| 302 | } |
| 303 | |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 304 | void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr) |
| 305 | { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 306 | struct net_bridge_vlan_group *vg; |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 307 | struct net_bridge_fdb_entry *f; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 308 | struct net_bridge_vlan *v; |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 309 | |
Toshiaki Makita | ac4c886 | 2014-02-07 16:48:26 +0900 | [diff] [blame] | 310 | spin_lock_bh(&br->hash_lock); |
| 311 | |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 312 | /* If old entry was unassociated with any port, then delete it. */ |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 313 | f = br_fdb_find(br, br->dev->dev_addr, 0); |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 314 | if (f && f->is_local && !f->dst && !f->added_by_user) |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 315 | fdb_delete_local(br, NULL, f); |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 316 | |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 317 | fdb_insert(br, NULL, newaddr, 0); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 318 | vg = br_vlan_group(br); |
| 319 | if (!vg || !vg->num_vlans) |
| 320 | goto out; |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 321 | /* Now remove and add entries for every VLAN configured on the |
| 322 | * bridge. This function runs under RTNL so the bitmap will not |
| 323 | * change from under us. |
| 324 | */ |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 325 | list_for_each_entry(v, &vg->vlan_list, vlist) { |
Toshiaki Makita | 0b148de | 2016-06-07 19:14:17 +0900 | [diff] [blame] | 326 | if (!br_vlan_should_use(v)) |
| 327 | continue; |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 328 | f = br_fdb_find(br, br->dev->dev_addr, v->vid); |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 329 | if (f && f->is_local && !f->dst && !f->added_by_user) |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 330 | fdb_delete_local(br, NULL, f); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 331 | fdb_insert(br, NULL, newaddr, v->vid); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 332 | } |
Toshiaki Makita | ac4c886 | 2014-02-07 16:48:26 +0900 | [diff] [blame] | 333 | out: |
| 334 | spin_unlock_bh(&br->hash_lock); |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 337 | void br_fdb_cleanup(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 339 | struct net_bridge *br = container_of(work, struct net_bridge, |
| 340 | gc_work.work); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 341 | struct net_bridge_fdb_entry *f = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | unsigned long delay = hold_time(br); |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 343 | unsigned long work_delay = delay; |
| 344 | unsigned long now = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 346 | /* this part is tricky, in order to avoid blocking learning and |
| 347 | * consequently forwarding, we rely on rcu to delete objects with |
| 348 | * delayed freeing allowing us to continue traversing |
| 349 | */ |
| 350 | rcu_read_lock(); |
| 351 | hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { |
| 352 | unsigned long this_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 354 | if (f->is_static || f->added_by_external_learn) |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 355 | continue; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 356 | this_timer = f->updated + delay; |
| 357 | if (time_after(this_timer, now)) { |
| 358 | work_delay = min(work_delay, this_timer - now); |
| 359 | } else { |
| 360 | spin_lock_bh(&br->hash_lock); |
| 361 | if (!hlist_unhashed(&f->fdb_node)) |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 362 | fdb_delete(br, f, true); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 363 | spin_unlock_bh(&br->hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 366 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 368 | /* Cleanup minimum 10 milliseconds apart */ |
| 369 | work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10)); |
| 370 | mod_delayed_work(system_long_wq, &br->gc_work, work_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 373 | /* Completely flush all dynamic entries in forwarding database.*/ |
| 374 | void br_fdb_flush(struct net_bridge *br) |
| 375 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 376 | struct net_bridge_fdb_entry *f; |
| 377 | struct hlist_node *tmp; |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 378 | |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 379 | spin_lock_bh(&br->hash_lock); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 380 | hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) { |
| 381 | if (!f->is_static) |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 382 | fdb_delete(br, f, true); |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 383 | } |
| 384 | spin_unlock_bh(&br->hash_lock); |
| 385 | } |
| 386 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 387 | /* Flush all entries referring to a specific port. |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 388 | * if do_all is set also flush static entries |
Nikolay Aleksandrov | 1ea2d02 | 2015-06-23 05:28:16 -0700 | [diff] [blame] | 389 | * if vid is set delete all entries that match the vlan_id |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 390 | */ |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 391 | void br_fdb_delete_by_port(struct net_bridge *br, |
| 392 | const struct net_bridge_port *p, |
Nikolay Aleksandrov | 1ea2d02 | 2015-06-23 05:28:16 -0700 | [diff] [blame] | 393 | u16 vid, |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 394 | int do_all) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 396 | struct net_bridge_fdb_entry *f; |
| 397 | struct hlist_node *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
| 399 | spin_lock_bh(&br->hash_lock); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 400 | hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) { |
| 401 | if (f->dst != p) |
| 402 | continue; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 403 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 404 | if (!do_all) |
| 405 | if (f->is_static || (vid && f->key.vlan_id != vid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | continue; |
| 407 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 408 | if (f->is_local) |
| 409 | fdb_delete_local(br, p, f); |
| 410 | else |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 411 | fdb_delete(br, f, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
| 413 | spin_unlock_bh(&br->hash_lock); |
| 414 | } |
| 415 | |
Igor Maravić | e6373c4 | 2011-12-12 02:58:25 +0000 | [diff] [blame] | 416 | #if IS_ENABLED(CONFIG_ATM_LANE) |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 417 | /* Interface used by ATM LANE hook to test |
| 418 | * if an addr is on some other bridge port */ |
| 419 | int br_fdb_test_addr(struct net_device *dev, unsigned char *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | { |
| 421 | struct net_bridge_fdb_entry *fdb; |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 422 | struct net_bridge_port *port; |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 423 | int ret; |
| 424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | rcu_read_lock(); |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 426 | port = br_port_get_rcu(dev); |
| 427 | if (!port) |
| 428 | ret = 0; |
| 429 | else { |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 430 | fdb = br_fdb_find_rcu(port->br, addr, 0); |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 431 | ret = fdb && fdb->dst && fdb->dst->dev != dev && |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 432 | fdb->dst->state == BR_STATE_FORWARDING; |
| 433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 436 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 438 | #endif /* CONFIG_ATM_LANE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | /* |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 441 | * Fill buffer with forwarding table records in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | * the API format. |
| 443 | */ |
| 444 | int br_fdb_fillbuf(struct net_bridge *br, void *buf, |
| 445 | unsigned long maxnum, unsigned long skip) |
| 446 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | struct net_bridge_fdb_entry *f; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 448 | struct __fdb_entry *fe = buf; |
| 449 | int num = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
| 451 | memset(buf, 0, maxnum*sizeof(struct __fdb_entry)); |
| 452 | |
| 453 | rcu_read_lock(); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 454 | hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { |
| 455 | if (num >= maxnum) |
| 456 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 458 | if (has_expired(br, f)) |
| 459 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 461 | /* ignore pseudo entry for local MAC address */ |
| 462 | if (!f->dst) |
| 463 | continue; |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 464 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 465 | if (skip) { |
| 466 | --skip; |
| 467 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 470 | /* convert from internal format to API */ |
| 471 | memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN); |
| 472 | |
| 473 | /* due to ABI compat need to split into hi/lo */ |
| 474 | fe->port_no = f->dst->port_no; |
| 475 | fe->port_hi = f->dst->port_no >> 8; |
| 476 | |
| 477 | fe->is_local = f->is_local; |
| 478 | if (!f->is_static) |
| 479 | fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated); |
| 480 | ++fe; |
| 481 | ++num; |
| 482 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | rcu_read_unlock(); |
| 484 | |
| 485 | return num; |
| 486 | } |
| 487 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 488 | static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | struct net_bridge_port *source, |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 490 | const unsigned char *addr, |
Roopa Prabhu | b7af147 | 2015-10-27 07:52:56 -0700 | [diff] [blame] | 491 | __u16 vid, |
| 492 | unsigned char is_local, |
| 493 | unsigned char is_static) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | { |
| 495 | struct net_bridge_fdb_entry *fdb; |
| 496 | |
| 497 | fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC); |
| 498 | if (fdb) { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 499 | memcpy(fdb->key.addr.addr, addr, ETH_ALEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | fdb->dst = source; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 501 | fdb->key.vlan_id = vid; |
Roopa Prabhu | b7af147 | 2015-10-27 07:52:56 -0700 | [diff] [blame] | 502 | fdb->is_local = is_local; |
| 503 | fdb->is_static = is_static; |
Toshiaki Makita | a5642ab | 2014-02-07 16:48:18 +0900 | [diff] [blame] | 504 | fdb->added_by_user = 0; |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 505 | fdb->added_by_external_learn = 0; |
Arkadi Sharshevsky | 9fe8bce | 2017-06-08 08:44:15 +0200 | [diff] [blame] | 506 | fdb->offloaded = 0; |
stephen hemminger | 7cd8861 | 2011-04-04 14:03:28 +0000 | [diff] [blame] | 507 | fdb->updated = fdb->used = jiffies; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 508 | if (rhashtable_lookup_insert_fast(&br->fdb_hash_tbl, |
| 509 | &fdb->rhnode, |
| 510 | br_fdb_rht_params)) { |
| 511 | kmem_cache_free(br_fdb_cache, fdb); |
| 512 | fdb = NULL; |
| 513 | } else { |
| 514 | hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list); |
| 515 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
| 517 | return fdb; |
| 518 | } |
| 519 | |
| 520 | 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] | 521 | const unsigned char *addr, u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | struct net_bridge_fdb_entry *fdb; |
| 524 | |
| 525 | if (!is_valid_ether_addr(addr)) |
| 526 | return -EINVAL; |
| 527 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 528 | fdb = br_fdb_find(br, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | if (fdb) { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 530 | /* it is okay to have multiple ports with same |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | * address, just use the first one. |
| 532 | */ |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 533 | if (fdb->is_local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | return 0; |
Roopa Prabhu | de1dfee | 2016-10-11 16:12:56 -0700 | [diff] [blame] | 535 | br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n", |
| 536 | source ? source->dev->name : br->dev->name, addr, vid); |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 537 | fdb_delete(br, fdb, true); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 538 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 540 | fdb = fdb_create(br, source, addr, vid, 1, 1); |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 541 | if (!fdb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | return -ENOMEM; |
| 543 | |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 544 | fdb_add_hw_addr(br, addr); |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 545 | fdb_notify(br, fdb, RTM_NEWNEIGH, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | return 0; |
| 547 | } |
| 548 | |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 549 | /* Add entry for local address of interface */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | 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] | 551 | const unsigned char *addr, u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
| 553 | int ret; |
| 554 | |
| 555 | spin_lock_bh(&br->hash_lock); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 556 | ret = fdb_insert(br, source, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | spin_unlock_bh(&br->hash_lock); |
| 558 | return ret; |
| 559 | } |
| 560 | |
| 561 | 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] | 562 | const unsigned char *addr, u16 vid, bool added_by_user) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | struct net_bridge_fdb_entry *fdb; |
Jon Maxwell | c65c7a3 | 2014-05-29 17:27:16 +1000 | [diff] [blame] | 565 | bool fdb_modified = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | |
| 567 | /* some users want to always flood. */ |
| 568 | if (hold_time(br) == 0) |
| 569 | return; |
| 570 | |
Stephen Hemminger | df1c0b8 | 2007-08-30 22:15:35 -0700 | [diff] [blame] | 571 | /* ignore packets unless we are using this port */ |
| 572 | if (!(source->state == BR_STATE_LEARNING || |
| 573 | source->state == BR_STATE_FORWARDING)) |
| 574 | return; |
| 575 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 576 | fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | if (likely(fdb)) { |
| 578 | /* attempt to update an entry for a local interface */ |
| 579 | if (unlikely(fdb->is_local)) { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 580 | if (net_ratelimit()) |
Roopa Prabhu | de1dfee | 2016-10-11 16:12:56 -0700 | [diff] [blame] | 581 | br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n", |
| 582 | source->dev->name, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } else { |
stephen hemminger | ca6d448 | 2017-02-07 08:46:46 -0800 | [diff] [blame] | 584 | unsigned long now = jiffies; |
| 585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | /* fastpath: update of existing entry */ |
Jon Maxwell | c65c7a3 | 2014-05-29 17:27:16 +1000 | [diff] [blame] | 587 | if (unlikely(source != fdb->dst)) { |
| 588 | fdb->dst = source; |
| 589 | fdb_modified = true; |
Arkadi Sharshevsky | 58073b3 | 2017-04-28 22:39:07 +0300 | [diff] [blame] | 590 | /* Take over HW learned entry */ |
| 591 | if (unlikely(fdb->added_by_external_learn)) |
| 592 | fdb->added_by_external_learn = 0; |
Jon Maxwell | c65c7a3 | 2014-05-29 17:27:16 +1000 | [diff] [blame] | 593 | } |
stephen hemminger | ca6d448 | 2017-02-07 08:46:46 -0800 | [diff] [blame] | 594 | if (now != fdb->updated) |
| 595 | fdb->updated = now; |
Toshiaki Makita | a5642ab | 2014-02-07 16:48:18 +0900 | [diff] [blame] | 596 | if (unlikely(added_by_user)) |
| 597 | fdb->added_by_user = 1; |
Roopa Prabhu | e3cfddd | 2017-08-30 22:18:13 -0700 | [diff] [blame] | 598 | if (unlikely(fdb_modified)) { |
| 599 | trace_br_fdb_update(br, source, addr, vid, added_by_user); |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 600 | fdb_notify(br, fdb, RTM_NEWNEIGH, true); |
Roopa Prabhu | e3cfddd | 2017-08-30 22:18:13 -0700 | [diff] [blame] | 601 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | } |
| 603 | } else { |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 604 | spin_lock(&br->hash_lock); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 605 | fdb = fdb_create(br, source, addr, vid, 0, 0); |
| 606 | if (fdb) { |
| 607 | if (unlikely(added_by_user)) |
| 608 | fdb->added_by_user = 1; |
| 609 | trace_br_fdb_update(br, source, addr, vid, |
| 610 | added_by_user); |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 611 | fdb_notify(br, fdb, RTM_NEWNEIGH, true); |
stephen hemminger | f58ee4e | 2011-12-06 13:02:24 +0000 | [diff] [blame] | 612 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | /* else we lose race and someone else inserts |
| 614 | * it first, don't bother updating |
| 615 | */ |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 616 | spin_unlock(&br->hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 619 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 620 | static int fdb_to_nud(const struct net_bridge *br, |
| 621 | const struct net_bridge_fdb_entry *fdb) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 622 | { |
| 623 | if (fdb->is_local) |
| 624 | return NUD_PERMANENT; |
| 625 | else if (fdb->is_static) |
| 626 | return NUD_NOARP; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 627 | else if (has_expired(br, fdb)) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 628 | return NUD_STALE; |
| 629 | else |
| 630 | return NUD_REACHABLE; |
| 631 | } |
| 632 | |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 633 | 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] | 634 | const struct net_bridge_fdb_entry *fdb, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 635 | u32 portid, u32 seq, int type, unsigned int flags) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 636 | { |
| 637 | unsigned long now = jiffies; |
| 638 | struct nda_cacheinfo ci; |
| 639 | struct nlmsghdr *nlh; |
| 640 | struct ndmsg *ndm; |
| 641 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 642 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 643 | if (nlh == NULL) |
| 644 | return -EMSGSIZE; |
| 645 | |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 646 | ndm = nlmsg_data(nlh); |
| 647 | ndm->ndm_family = AF_BRIDGE; |
| 648 | ndm->ndm_pad1 = 0; |
| 649 | ndm->ndm_pad2 = 0; |
Arkadi Sharshevsky | 9fe8bce | 2017-06-08 08:44:15 +0200 | [diff] [blame] | 650 | ndm->ndm_flags = 0; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 651 | ndm->ndm_type = 0; |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 652 | ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 653 | ndm->ndm_state = fdb_to_nud(br, fdb); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 654 | |
Arkadi Sharshevsky | 9fe8bce | 2017-06-08 08:44:15 +0200 | [diff] [blame] | 655 | if (fdb->offloaded) |
| 656 | ndm->ndm_flags |= NTF_OFFLOADED; |
| 657 | if (fdb->added_by_external_learn) |
| 658 | ndm->ndm_flags |= NTF_EXT_LEARNED; |
| 659 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 660 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr)) |
David S. Miller | 2eb812e | 2012-04-01 20:49:54 -0400 | [diff] [blame] | 661 | goto nla_put_failure; |
Roopa Prabhu | 41c389d | 2014-05-27 22:39:37 -0700 | [diff] [blame] | 662 | if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex)) |
| 663 | goto nla_put_failure; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 664 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); |
| 665 | ci.ndm_confirmed = 0; |
| 666 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); |
| 667 | ci.ndm_refcnt = 0; |
David S. Miller | 2eb812e | 2012-04-01 20:49:54 -0400 | [diff] [blame] | 668 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) |
| 669 | goto nla_put_failure; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 670 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 671 | if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16), |
| 672 | &fdb->key.vlan_id)) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 673 | goto nla_put_failure; |
| 674 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 675 | nlmsg_end(skb, nlh); |
| 676 | return 0; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 677 | |
| 678 | nla_put_failure: |
| 679 | nlmsg_cancel(skb, nlh); |
| 680 | return -EMSGSIZE; |
| 681 | } |
| 682 | |
| 683 | static inline size_t fdb_nlmsg_size(void) |
| 684 | { |
| 685 | return NLMSG_ALIGN(sizeof(struct ndmsg)) |
| 686 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ |
Roopa Prabhu | 41c389d | 2014-05-27 22:39:37 -0700 | [diff] [blame] | 687 | + nla_total_size(sizeof(u32)) /* NDA_MASTER */ |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 688 | + nla_total_size(sizeof(u16)) /* NDA_VLAN */ |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 689 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
| 690 | } |
| 691 | |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 692 | static void fdb_notify(struct net_bridge *br, |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 693 | const struct net_bridge_fdb_entry *fdb, int type, |
| 694 | bool swdev_notify) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 695 | { |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 696 | struct net *net = dev_net(br->dev); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 697 | struct sk_buff *skb; |
| 698 | int err = -ENOBUFS; |
| 699 | |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 700 | if (swdev_notify) |
| 701 | br_switchdev_fdb_notify(fdb, type); |
Arkadi Sharshevsky | 6b26b51 | 2017-06-08 08:44:14 +0200 | [diff] [blame] | 702 | |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 703 | skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC); |
| 704 | if (skb == NULL) |
| 705 | goto errout; |
| 706 | |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 707 | err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 708 | if (err < 0) { |
| 709 | /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */ |
| 710 | WARN_ON(err == -EMSGSIZE); |
| 711 | kfree_skb(skb); |
| 712 | goto errout; |
| 713 | } |
| 714 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 715 | return; |
| 716 | errout: |
tanxiaojun | 87e823b | 2013-12-19 13:28:10 +0800 | [diff] [blame] | 717 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /* Dump information about entries, in response to GETNEIGH */ |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 721 | int br_fdb_dump(struct sk_buff *skb, |
| 722 | struct netlink_callback *cb, |
| 723 | struct net_device *dev, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 724 | struct net_device *filter_dev, |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 725 | int *idx) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 726 | { |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 727 | struct net_bridge *br = netdev_priv(dev); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 728 | struct net_bridge_fdb_entry *f; |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 729 | int err = 0; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 730 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 731 | if (!(dev->priv_flags & IFF_EBRIDGE)) |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 732 | return err; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 733 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 734 | if (!filter_dev) { |
| 735 | err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx); |
| 736 | if (err < 0) |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 737 | return err; |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 738 | } |
Hubert Sokolowski | 6cb6974 | 2015-01-05 17:29:21 +0000 | [diff] [blame] | 739 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 740 | rcu_read_lock(); |
| 741 | hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { |
| 742 | if (*idx < cb->args[2]) |
| 743 | goto skip; |
| 744 | if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) { |
| 745 | if (filter_dev != dev) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 746 | goto skip; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 747 | /* !f->dst is a special case for bridge |
| 748 | * It means the MAC belongs to the bridge |
| 749 | * Therefore need a little more filtering |
| 750 | * we only want to dump the !f->dst case |
| 751 | */ |
| 752 | if (f->dst) |
Hubert Sokolowski | 6cb6974 | 2015-01-05 17:29:21 +0000 | [diff] [blame] | 753 | goto skip; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 754 | } |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 755 | if (!filter_dev && f->dst) |
| 756 | goto skip; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 757 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 758 | err = fdb_fill_info(skb, br, f, |
| 759 | NETLINK_CB(cb->skb).portid, |
| 760 | cb->nlh->nlmsg_seq, |
| 761 | RTM_NEWNEIGH, |
| 762 | NLM_F_MULTI); |
| 763 | if (err < 0) |
| 764 | break; |
| 765 | skip: |
| 766 | *idx += 1; |
| 767 | } |
| 768 | rcu_read_unlock(); |
| 769 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 770 | return err; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 771 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 772 | |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 773 | /* Update (create or replace) forwarding database entry */ |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 774 | static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source, |
| 775 | const __u8 *addr, __u16 state, __u16 flags, __u16 vid) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 776 | { |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 777 | struct net_bridge_fdb_entry *fdb; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 778 | bool modified = false; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 779 | |
Wilson Kok | eb8d7ba | 2015-05-25 06:39:31 -0700 | [diff] [blame] | 780 | /* If the port cannot learn allow only local and static entries */ |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 781 | if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) && |
Wilson Kok | eb8d7ba | 2015-05-25 06:39:31 -0700 | [diff] [blame] | 782 | !(source->state == BR_STATE_LEARNING || |
| 783 | source->state == BR_STATE_FORWARDING)) |
| 784 | return -EPERM; |
| 785 | |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 786 | if (!source && !(state & NUD_PERMANENT)) { |
| 787 | pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n", |
| 788 | br->dev->name); |
| 789 | return -EINVAL; |
| 790 | } |
| 791 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 792 | fdb = br_fdb_find(br, addr, vid); |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 793 | if (fdb == NULL) { |
| 794 | if (!(flags & NLM_F_CREATE)) |
| 795 | return -ENOENT; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 796 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 797 | fdb = fdb_create(br, source, addr, vid, 0, 0); |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 798 | if (!fdb) |
| 799 | return -ENOMEM; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 800 | |
| 801 | modified = true; |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 802 | } else { |
| 803 | if (flags & NLM_F_EXCL) |
| 804 | return -EEXIST; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 805 | |
| 806 | if (fdb->dst != source) { |
| 807 | fdb->dst = source; |
| 808 | modified = true; |
| 809 | } |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 810 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 811 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 812 | if (fdb_to_nud(br, fdb) != state) { |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 813 | if (state & NUD_PERMANENT) { |
| 814 | fdb->is_local = 1; |
| 815 | if (!fdb->is_static) { |
| 816 | fdb->is_static = 1; |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 817 | fdb_add_hw_addr(br, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 818 | } |
| 819 | } else if (state & NUD_NOARP) { |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 820 | fdb->is_local = 0; |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 821 | if (!fdb->is_static) { |
| 822 | fdb->is_static = 1; |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 823 | fdb_add_hw_addr(br, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 824 | } |
| 825 | } else { |
| 826 | fdb->is_local = 0; |
| 827 | if (fdb->is_static) { |
| 828 | fdb->is_static = 0; |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 829 | fdb_del_hw_addr(br, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 830 | } |
| 831 | } |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 832 | |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 833 | modified = true; |
| 834 | } |
Toshiaki Makita | a5642ab | 2014-02-07 16:48:18 +0900 | [diff] [blame] | 835 | fdb->added_by_user = 1; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 836 | |
| 837 | fdb->used = jiffies; |
| 838 | if (modified) { |
| 839 | fdb->updated = jiffies; |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 840 | fdb_notify(br, fdb, RTM_NEWNEIGH, true); |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 841 | } |
| 842 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 843 | return 0; |
| 844 | } |
| 845 | |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 846 | static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br, |
| 847 | struct net_bridge_port *p, const unsigned char *addr, |
| 848 | u16 nlh_flags, u16 vid) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 849 | { |
| 850 | int err = 0; |
| 851 | |
| 852 | if (ndm->ndm_flags & NTF_USE) { |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 853 | if (!p) { |
| 854 | pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n", |
| 855 | br->dev->name); |
| 856 | return -EINVAL; |
| 857 | } |
Nikolay Aleksandrov | c4c832f | 2015-06-06 06:49:00 -0700 | [diff] [blame] | 858 | local_bh_disable(); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 859 | rcu_read_lock(); |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 860 | br_fdb_update(br, p, addr, vid, true); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 861 | rcu_read_unlock(); |
Nikolay Aleksandrov | c4c832f | 2015-06-06 06:49:00 -0700 | [diff] [blame] | 862 | local_bh_enable(); |
Nikolay Aleksandrov | eb100e0 | 2017-03-23 12:27:13 +0200 | [diff] [blame] | 863 | } else if (ndm->ndm_flags & NTF_EXT_LEARNED) { |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 864 | err = br_fdb_external_learn_add(br, p, addr, vid, true); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 865 | } else { |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 866 | spin_lock_bh(&br->hash_lock); |
| 867 | err = fdb_add_entry(br, p, addr, ndm->ndm_state, |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 868 | nlh_flags, vid); |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 869 | spin_unlock_bh(&br->hash_lock); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | return err; |
| 873 | } |
| 874 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 875 | /* Add new permanent fdb entry with RTM_NEWNEIGH */ |
stephen hemminger | edc7d57 | 2012-10-01 12:32:33 +0000 | [diff] [blame] | 876 | int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 877 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 878 | const unsigned char *addr, u16 vid, u16 nlh_flags) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 879 | { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 880 | struct net_bridge_vlan_group *vg; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 881 | struct net_bridge_port *p = NULL; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 882 | struct net_bridge_vlan *v; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 883 | struct net_bridge *br = NULL; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 884 | int err = 0; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 885 | |
Roopa Prabhu | b74fd30 | 2017-08-29 13:16:57 -0700 | [diff] [blame] | 886 | trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags); |
| 887 | |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 888 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) { |
| 889 | pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state); |
| 890 | return -EINVAL; |
| 891 | } |
| 892 | |
Stephen Hemminger | 537f7f8 | 2013-06-25 09:34:36 -0700 | [diff] [blame] | 893 | if (is_zero_ether_addr(addr)) { |
| 894 | pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n"); |
| 895 | return -EINVAL; |
| 896 | } |
| 897 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 898 | if (dev->priv_flags & IFF_EBRIDGE) { |
| 899 | br = netdev_priv(dev); |
| 900 | vg = br_vlan_group(br); |
| 901 | } else { |
| 902 | p = br_port_get_rtnl(dev); |
| 903 | if (!p) { |
| 904 | pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n", |
| 905 | dev->name); |
| 906 | return -EINVAL; |
| 907 | } |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 908 | br = p->br; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 909 | vg = nbp_vlan_group(p); |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 912 | if (vid) { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 913 | v = br_vlan_find(vg, vid); |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 914 | if (!v || !br_vlan_should_use(v)) { |
| 915 | pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 916 | return -EINVAL; |
| 917 | } |
| 918 | |
| 919 | /* VID was specified, so use it. */ |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 920 | err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid); |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 921 | } else { |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 922 | err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 923 | if (err || !vg || !vg->num_vlans) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 924 | goto out; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 925 | |
| 926 | /* We have vlans configured on this port and user didn't |
| 927 | * specify a VLAN. To be nice, add/update entry for every |
| 928 | * vlan on this port. |
| 929 | */ |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 930 | list_for_each_entry(v, &vg->vlan_list, vlist) { |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 931 | if (!br_vlan_should_use(v)) |
| 932 | continue; |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 933 | err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 934 | if (err) |
| 935 | goto out; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 936 | } |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 937 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 938 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 939 | out: |
| 940 | return err; |
| 941 | } |
| 942 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 943 | static int fdb_delete_by_addr_and_port(struct net_bridge *br, |
| 944 | const struct net_bridge_port *p, |
Nikolay Aleksandrov | 8c86f96 | 2015-06-09 03:34:13 -0700 | [diff] [blame] | 945 | const u8 *addr, u16 vlan) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 946 | { |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 947 | struct net_bridge_fdb_entry *fdb; |
| 948 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 949 | fdb = br_fdb_find(br, addr, vlan); |
Nikolay Aleksandrov | 8c86f96 | 2015-06-09 03:34:13 -0700 | [diff] [blame] | 950 | if (!fdb || fdb->dst != p) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 951 | return -ENOENT; |
| 952 | |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 953 | fdb_delete(br, fdb, true); |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 954 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 955 | return 0; |
| 956 | } |
| 957 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 958 | static int __br_fdb_delete(struct net_bridge *br, |
| 959 | const struct net_bridge_port *p, |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 960 | const unsigned char *addr, u16 vid) |
| 961 | { |
| 962 | int err; |
| 963 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 964 | spin_lock_bh(&br->hash_lock); |
| 965 | err = fdb_delete_by_addr_and_port(br, p, addr, vid); |
| 966 | spin_unlock_bh(&br->hash_lock); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 967 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 968 | return err; |
| 969 | } |
| 970 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 971 | /* Remove neighbor entry with RTM_DELNEIGH */ |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 972 | int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], |
| 973 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 974 | const unsigned char *addr, u16 vid) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 975 | { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 976 | struct net_bridge_vlan_group *vg; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 977 | struct net_bridge_port *p = NULL; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 978 | struct net_bridge_vlan *v; |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 979 | struct net_bridge *br; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 980 | int err; |
| 981 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 982 | if (dev->priv_flags & IFF_EBRIDGE) { |
| 983 | br = netdev_priv(dev); |
| 984 | vg = br_vlan_group(br); |
| 985 | } else { |
| 986 | p = br_port_get_rtnl(dev); |
| 987 | if (!p) { |
| 988 | pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n", |
| 989 | dev->name); |
| 990 | return -EINVAL; |
| 991 | } |
| 992 | vg = nbp_vlan_group(p); |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 993 | br = p->br; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 996 | if (vid) { |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 997 | v = br_vlan_find(vg, vid); |
| 998 | if (!v) { |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 999 | pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1000 | return -EINVAL; |
| 1001 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1002 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1003 | err = __br_fdb_delete(br, p, addr, vid); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1004 | } else { |
Toshiaki Makita | 25d3b49 | 2015-02-09 20:16:17 +0900 | [diff] [blame] | 1005 | err = -ENOENT; |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1006 | err &= __br_fdb_delete(br, p, addr, 0); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1007 | if (!vg || !vg->num_vlans) |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1008 | return err; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1009 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1010 | list_for_each_entry(v, &vg->vlan_list, vlist) { |
| 1011 | if (!br_vlan_should_use(v)) |
| 1012 | continue; |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1013 | err &= __br_fdb_delete(br, p, addr, v->vid); |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1014 | } |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1015 | } |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1016 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1017 | return err; |
| 1018 | } |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1019 | |
| 1020 | int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p) |
| 1021 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1022 | struct net_bridge_fdb_entry *f, *tmp; |
Geert Uytterhoeven | 367dc65 | 2018-02-01 11:25:27 +0100 | [diff] [blame] | 1023 | int err = 0; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1024 | |
| 1025 | ASSERT_RTNL(); |
| 1026 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1027 | /* the key here is that static entries change only under rtnl */ |
| 1028 | rcu_read_lock(); |
| 1029 | hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { |
| 1030 | /* We only care for static entries */ |
| 1031 | if (!f->is_static) |
| 1032 | continue; |
| 1033 | err = dev_uc_add(p->dev, f->key.addr.addr); |
| 1034 | if (err) |
| 1035 | goto rollback; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1036 | } |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1037 | done: |
| 1038 | rcu_read_unlock(); |
| 1039 | |
| 1040 | return err; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1041 | |
| 1042 | rollback: |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1043 | hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) { |
| 1044 | /* We only care for static entries */ |
| 1045 | if (!tmp->is_static) |
| 1046 | continue; |
| 1047 | if (tmp == f) |
| 1048 | break; |
| 1049 | dev_uc_del(p->dev, tmp->key.addr.addr); |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1050 | } |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1051 | |
| 1052 | goto done; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p) |
| 1056 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1057 | struct net_bridge_fdb_entry *f; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1058 | |
| 1059 | ASSERT_RTNL(); |
| 1060 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1061 | rcu_read_lock(); |
| 1062 | hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { |
| 1063 | /* We only care for static entries */ |
| 1064 | if (!f->is_static) |
| 1065 | continue; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1066 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1067 | dev_uc_del(p->dev, f->key.addr.addr); |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1068 | } |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1069 | rcu_read_unlock(); |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1070 | } |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1071 | |
Jiri Pirko | 3aeb661 | 2015-01-15 23:49:37 +0100 | [diff] [blame] | 1072 | int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p, |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 1073 | const unsigned char *addr, u16 vid, |
| 1074 | bool swdev_notify) |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1075 | { |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1076 | struct net_bridge_fdb_entry *fdb; |
Nikolay Aleksandrov | 7597b26 | 2017-07-03 15:14:59 -0700 | [diff] [blame] | 1077 | bool modified = false; |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1078 | int err = 0; |
| 1079 | |
Roopa Prabhu | b74fd30 | 2017-08-29 13:16:57 -0700 | [diff] [blame] | 1080 | trace_br_fdb_external_learn_add(br, p, addr, vid); |
| 1081 | |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1082 | spin_lock_bh(&br->hash_lock); |
| 1083 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 1084 | fdb = br_fdb_find(br, addr, vid); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1085 | if (!fdb) { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1086 | fdb = fdb_create(br, p, addr, vid, 0, 0); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1087 | if (!fdb) { |
| 1088 | err = -ENOMEM; |
| 1089 | goto err_unlock; |
| 1090 | } |
Ido Schimmel | 4799417 | 2019-01-18 15:58:00 +0000 | [diff] [blame] | 1091 | if (swdev_notify) |
| 1092 | fdb->added_by_user = 1; |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1093 | fdb->added_by_external_learn = 1; |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 1094 | fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify); |
Nikolay Aleksandrov | 7597b26 | 2017-07-03 15:14:59 -0700 | [diff] [blame] | 1095 | } else { |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1096 | fdb->updated = jiffies; |
Nikolay Aleksandrov | 7597b26 | 2017-07-03 15:14:59 -0700 | [diff] [blame] | 1097 | |
| 1098 | if (fdb->dst != p) { |
| 1099 | fdb->dst = p; |
| 1100 | modified = true; |
| 1101 | } |
| 1102 | |
| 1103 | if (fdb->added_by_external_learn) { |
| 1104 | /* Refresh entry */ |
| 1105 | fdb->used = jiffies; |
| 1106 | } else if (!fdb->added_by_user) { |
| 1107 | /* Take over SW learned entry */ |
| 1108 | fdb->added_by_external_learn = 1; |
| 1109 | modified = true; |
| 1110 | } |
| 1111 | |
Ido Schimmel | 4799417 | 2019-01-18 15:58:00 +0000 | [diff] [blame] | 1112 | if (swdev_notify) |
| 1113 | fdb->added_by_user = 1; |
| 1114 | |
Nikolay Aleksandrov | 7597b26 | 2017-07-03 15:14:59 -0700 | [diff] [blame] | 1115 | if (modified) |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 1116 | fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | err_unlock: |
| 1120 | spin_unlock_bh(&br->hash_lock); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1121 | |
| 1122 | return err; |
| 1123 | } |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1124 | |
Jiri Pirko | 3aeb661 | 2015-01-15 23:49:37 +0100 | [diff] [blame] | 1125 | int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p, |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 1126 | const unsigned char *addr, u16 vid, |
| 1127 | bool swdev_notify) |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1128 | { |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1129 | struct net_bridge_fdb_entry *fdb; |
| 1130 | int err = 0; |
| 1131 | |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1132 | spin_lock_bh(&br->hash_lock); |
| 1133 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 1134 | fdb = br_fdb_find(br, addr, vid); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1135 | if (fdb && fdb->added_by_external_learn) |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 1136 | fdb_delete(br, fdb, swdev_notify); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1137 | else |
| 1138 | err = -ENOENT; |
| 1139 | |
| 1140 | spin_unlock_bh(&br->hash_lock); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1141 | |
| 1142 | return err; |
| 1143 | } |
Arkadi Sharshevsky | 9fe8bce | 2017-06-08 08:44:15 +0200 | [diff] [blame] | 1144 | |
| 1145 | void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p, |
| 1146 | const unsigned char *addr, u16 vid) |
| 1147 | { |
| 1148 | struct net_bridge_fdb_entry *fdb; |
| 1149 | |
| 1150 | spin_lock_bh(&br->hash_lock); |
| 1151 | |
| 1152 | fdb = br_fdb_find(br, addr, vid); |
| 1153 | if (fdb) |
| 1154 | fdb->offloaded = 1; |
| 1155 | |
| 1156 | spin_unlock_bh(&br->hash_lock); |
| 1157 | } |