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