blob: 2e777c8b0921a23eec68eff2cb0b5e08d62d44d7 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Forwarding database
4 * Linux ethernet bridge
5 *
6 * Authors:
7 * Lennert Buytenhek <buytenh@gnu.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/kernel.h>
11#include <linux/init.h>
Franck Bui-Huu82524742008-05-12 21:21:05 +020012#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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 Hemminger3f890922007-03-21 13:42:33 -070018#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Arun Sharma600634972011-07-26 16:09:06 -070020#include <linux/atomic.h>
Stephen Hemminger3f890922007-03-21 13:42:33 -070021#include <asm/unaligned.h>
Vlad Yasevich2ba071e2013-02-13 12:00:16 +000022#include <linux/if_vlan.h>
Scott Feldmanb4ad7ba2015-06-14 11:33:11 -070023#include <net/switchdev.h>
Roopa Prabhub74fd302017-08-29 13:16:57 -070024#include <trace/events/bridge.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "br_private.h"
26
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020027static 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 Aleksandroveb793582017-12-12 16:02:50 +020032};
33
Christoph Lametere18b8902006-12-06 20:33:20 -080034static struct kmem_cache *br_fdb_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000036 const unsigned char *addr, u16 vid);
stephen hemminger31e8a49c2011-12-08 07:17:41 +000037static void fdb_notify(struct net_bridge *br,
Petr Machata161d82d2018-05-03 14:43:53 +020038 const struct net_bridge_fdb_entry *, int, bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Akinobu Mita87a596e2007-04-07 18:57:07 +090040int __init br_fdb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
43 sizeof(struct net_bridge_fdb_entry),
44 0,
Paul Mundt20c2df82007-07-20 10:11:58 +090045 SLAB_HWCACHE_ALIGN, NULL);
Akinobu Mita87a596e2007-04-07 18:57:07 +090046 if (!br_fdb_cache)
47 return -ENOMEM;
48
Akinobu Mita87a596e2007-04-07 18:57:07 +090049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Andrew Morton73afc902007-12-05 21:35:23 -080052void br_fdb_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 kmem_cache_destroy(br_fdb_cache);
55}
56
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020057int br_fdb_hash_init(struct net_bridge *br)
58{
59 return rhashtable_init(&br->fdb_hash_tbl, &br_fdb_rht_params);
60}
61
62void br_fdb_hash_fini(struct net_bridge *br)
63{
64 rhashtable_destroy(&br->fdb_hash_tbl);
65}
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/* if topology_changing then use forward_delay (default 15 sec)
68 * otherwise keep longer (default 5 minutes)
69 */
Stephen Hemminger3f890922007-03-21 13:42:33 -070070static inline unsigned long hold_time(const struct net_bridge *br)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 return br->topology_change ? br->forward_delay : br->ageing_time;
73}
74
Stephen Hemminger3f890922007-03-21 13:42:33 -070075static inline int has_expired(const struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 const struct net_bridge_fdb_entry *fdb)
77{
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +020078 return !test_bit(BR_FDB_STATIC, &fdb->flags) &&
Nikolay Aleksandrovb5cd9f72019-10-29 13:45:57 +020079 !test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags) &&
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +020080 time_before_eq(fdb->updated + hold_time(br), jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Michał Mirosławda678292009-06-05 05:35:28 +000083static 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 Aleksandroveb793582017-12-12 16:02:50 +020090static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl,
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +010091 const unsigned char *addr,
92 __u16 vid)
93{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020094 struct net_bridge_fdb_key key;
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +010095
Nikolay Aleksandrov410b3d42017-02-13 14:59:10 +010096 WARN_ON_ONCE(!rcu_read_lock_held());
97
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020098 key.vlan_id = vid;
99 memcpy(key.addr.addr, addr, sizeof(key.addr.addr));
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100100
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200101 return rhashtable_lookup(tbl, &key, br_fdb_rht_params);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100102}
103
104/* requires bridge hash_lock */
105static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br,
106 const unsigned char *addr,
107 __u16 vid)
108{
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100109 struct net_bridge_fdb_entry *fdb;
110
WANG Congd12c9172017-03-16 10:32:42 -0700111 lockdep_assert_held_once(&br->hash_lock);
Nikolay Aleksandrov410b3d42017-02-13 14:59:10 +0100112
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100113 rcu_read_lock();
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200114 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100115 rcu_read_unlock();
116
117 return fdb;
118}
119
Petr Machata4d4fd362018-04-29 10:56:08 +0300120struct 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 Machata873aca22018-06-08 15:11:47 +0200134 rcu_read_lock();
135 f = br_fdb_find_rcu(br, addr, vid);
Petr Machata4d4fd362018-04-29 10:56:08 +0300136 if (f && f->dst)
137 dev = f->dst->dev;
Petr Machata873aca22018-06-08 15:11:47 +0200138 rcu_read_unlock();
Petr Machata4d4fd362018-04-29 10:56:08 +0300139
140 return dev;
141}
142EXPORT_SYMBOL_GPL(br_fdb_find_port);
143
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100144struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
145 const unsigned char *addr,
146 __u16 vid)
147{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200148 return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100149}
150
Vlad Yasevich145beee2014-05-16 09:59:19 -0400151/* 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 Pirko020ec6b2014-11-28 14:34:12 +0100156static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr)
Vlad Yasevich145beee2014-05-16 09:59:19 -0400157{
158 int err;
Li RongQinga3f5ee72014-06-18 16:07:16 +0800159 struct net_bridge_port *p;
Vlad Yasevich145beee2014-05-16 09:59:19 -0400160
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;
172undo:
Li RongQinga3f5ee72014-06-18 16:07:16 +0800173 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 Yasevich145beee2014-05-16 09:59:19 -0400176 }
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 Pirko020ec6b2014-11-28 14:34:12 +0100184static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
Vlad Yasevich145beee2014-05-16 09:59:19 -0400185{
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 Machata161d82d2018-05-03 14:43:53 +0200196static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
197 bool swdev_notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Roopa Prabhub74fd302017-08-29 13:16:57 -0700199 trace_fdb_delete(br, f);
200
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200201 if (test_bit(BR_FDB_STATIC, &f->flags))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200202 fdb_del_hw_addr(br, f->key.addr.addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400203
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200204 hlist_del_init_rcu(&f->fdb_node);
205 rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode,
206 br_fdb_rht_params);
Petr Machata161d82d2018-05-03 14:43:53 +0200207 fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
Michał Mirosławda678292009-06-05 05:35:28 +0000208 call_rcu(&f->rcu, fdb_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Toshiaki Makita960b5892014-02-07 16:48:23 +0900211/* Delete a local entry if no other port had the same address. */
212static void fdb_delete_local(struct net_bridge *br,
213 const struct net_bridge_port *p,
214 struct net_bridge_fdb_entry *f)
215{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200216 const unsigned char *addr = f->key.addr.addr;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200217 struct net_bridge_vlan_group *vg;
218 const struct net_bridge_vlan *v;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900219 struct net_bridge_port *op;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200220 u16 vid = f->key.vlan_id;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900221
222 /* Maybe another port has same hw addr? */
223 list_for_each_entry(op, &br->port_list, list) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200224 vg = nbp_vlan_group(op);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900225 if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200226 (!vid || br_vlan_find(vg, vid))) {
Toshiaki Makita960b5892014-02-07 16:48:23 +0900227 f->dst = op;
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200228 clear_bit(BR_FDB_ADDED_BY_USER, &f->flags);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900229 return;
230 }
231 }
232
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200233 vg = br_vlan_group(br);
234 v = br_vlan_find(vg, vid);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900235 /* Maybe bridge device has same hw addr? */
236 if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200237 (!vid || (v && br_vlan_should_use(v)))) {
Toshiaki Makita960b5892014-02-07 16:48:23 +0900238 f->dst = NULL;
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200239 clear_bit(BR_FDB_ADDED_BY_USER, &f->flags);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900240 return;
241 }
242
Petr Machata161d82d2018-05-03 14:43:53 +0200243 fdb_delete(br, f, true);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900244}
245
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900246void 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 Makita424bb9c2014-02-07 16:48:25 +0900250 struct net_bridge_fdb_entry *f;
251
252 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100253 f = br_fdb_find(br, addr, vid);
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200254 if (f && test_bit(BR_FDB_LOCAL, &f->flags) &&
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200255 !test_bit(BR_FDB_ADDED_BY_USER, &f->flags) && f->dst == p)
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900256 fdb_delete_local(br, p, f);
257 spin_unlock_bh(&br->hash_lock);
258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
261{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200262 struct net_bridge_vlan_group *vg;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200263 struct net_bridge_fdb_entry *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 struct net_bridge *br = p->br;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200265 struct net_bridge_vlan *v;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200268 vg = nbp_vlan_group(p);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200269 hlist_for_each_entry(f, &br->fdb_list, fdb_node) {
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200270 if (f->dst == p && test_bit(BR_FDB_LOCAL, &f->flags) &&
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200271 !test_bit(BR_FDB_ADDED_BY_USER, &f->flags)) {
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200272 /* delete old one */
273 fdb_delete_local(br, p, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200275 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Toshiaki Makita28368822014-02-07 16:48:19 +0900284insert:
285 /* insert new address, may fail if invalid address or dup. */
286 fdb_insert(br, p, newaddr, 0);
287
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200288 if (!vg || !vg->num_vlans)
Toshiaki Makita28368822014-02-07 16:48:19 +0900289 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 Aleksandrov2594e9062015-09-25 19:00:11 +0200295 list_for_each_entry(v, &vg->vlan_list, vlist)
296 fdb_insert(br, p, newaddr, v->vid);
Toshiaki Makita28368822014-02-07 16:48:19 +0900297
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000298done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 spin_unlock_bh(&br->hash_lock);
300}
301
stephen hemminger43598812011-12-08 07:17:49 +0000302void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
303{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200304 struct net_bridge_vlan_group *vg;
stephen hemminger43598812011-12-08 07:17:49 +0000305 struct net_bridge_fdb_entry *f;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200306 struct net_bridge_vlan *v;
stephen hemminger43598812011-12-08 07:17:49 +0000307
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900308 spin_lock_bh(&br->hash_lock);
309
stephen hemminger43598812011-12-08 07:17:49 +0000310 /* If old entry was unassociated with any port, then delete it. */
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100311 f = br_fdb_find(br, br->dev->dev_addr, 0);
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200312 if (f && test_bit(BR_FDB_LOCAL, &f->flags) &&
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200313 !f->dst && !test_bit(BR_FDB_ADDED_BY_USER, &f->flags))
Toshiaki Makita960b5892014-02-07 16:48:23 +0900314 fdb_delete_local(br, NULL, f);
stephen hemminger43598812011-12-08 07:17:49 +0000315
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000316 fdb_insert(br, NULL, newaddr, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200317 vg = br_vlan_group(br);
318 if (!vg || !vg->num_vlans)
319 goto out;
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000320 /* 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 Aleksandrov2594e9062015-09-25 19:00:11 +0200324 list_for_each_entry(v, &vg->vlan_list, vlist) {
Toshiaki Makita0b148de2016-06-07 19:14:17 +0900325 if (!br_vlan_should_use(v))
326 continue;
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100327 f = br_fdb_find(br, br->dev->dev_addr, v->vid);
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200328 if (f && test_bit(BR_FDB_LOCAL, &f->flags) &&
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200329 !f->dst && !test_bit(BR_FDB_ADDED_BY_USER, &f->flags))
Toshiaki Makita960b5892014-02-07 16:48:23 +0900330 fdb_delete_local(br, NULL, f);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200331 fdb_insert(br, NULL, newaddr, v->vid);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000332 }
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900333out:
334 spin_unlock_bh(&br->hash_lock);
stephen hemminger43598812011-12-08 07:17:49 +0000335}
336
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100337void br_fdb_cleanup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100339 struct net_bridge *br = container_of(work, struct net_bridge,
340 gc_work.work);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200341 struct net_bridge_fdb_entry *f = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 unsigned long delay = hold_time(br);
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100343 unsigned long work_delay = delay;
344 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200346 /* 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 Aleksandrov31cbc39b2020-06-23 23:47:17 +0300352 unsigned long this_timer = f->updated + delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200354 if (test_bit(BR_FDB_STATIC, &f->flags) ||
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300355 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 Aleksandrovf7cdee82017-02-04 18:05:07 +0100364 continue;
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300365 }
366
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200367 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 Machata161d82d2018-05-03 14:43:53 +0200372 fdb_delete(br, f, true);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200373 spin_unlock_bh(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200376 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100378 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700383/* Completely flush all dynamic entries in forwarding database.*/
384void br_fdb_flush(struct net_bridge *br)
385{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200386 struct net_bridge_fdb_entry *f;
387 struct hlist_node *tmp;
Stephen Hemminger1a620692006-10-12 14:45:38 -0700388
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700389 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200390 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200391 if (!test_bit(BR_FDB_STATIC, &f->flags))
Petr Machata161d82d2018-05-03 14:43:53 +0200392 fdb_delete(br, f, true);
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700393 }
394 spin_unlock_bh(&br->hash_lock);
395}
396
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300397/* Flush all entries referring to a specific port.
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700398 * if do_all is set also flush static entries
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700399 * if vid is set delete all entries that match the vlan_id
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700400 */
Stephen Hemminger1a620692006-10-12 14:45:38 -0700401void br_fdb_delete_by_port(struct net_bridge *br,
402 const struct net_bridge_port *p,
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700403 u16 vid,
Stephen Hemminger1a620692006-10-12 14:45:38 -0700404 int do_all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200406 struct net_bridge_fdb_entry *f;
407 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200410 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
411 if (f->dst != p)
412 continue;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900413
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200414 if (!do_all)
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200415 if (test_bit(BR_FDB_STATIC, &f->flags) ||
Nikolay Aleksandrovf2f37292020-09-28 18:30:02 +0300416 (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &f->flags) &&
417 !test_bit(BR_FDB_OFFLOADED, &f->flags)) ||
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200418 (vid && f->key.vlan_id != vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 continue;
420
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200421 if (test_bit(BR_FDB_LOCAL, &f->flags))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200422 fdb_delete_local(br, p, f);
423 else
Petr Machata161d82d2018-05-03 14:43:53 +0200424 fdb_delete(br, f, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 spin_unlock_bh(&br->hash_lock);
427}
428
Igor Maraviće6373c42011-12-12 02:58:25 +0000429#if IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +0000430/* Interface used by ATM LANE hook to test
431 * if an addr is on some other bridge port */
432int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct net_bridge_fdb_entry *fdb;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000435 struct net_bridge_port *port;
Michał Mirosławda678292009-06-05 05:35:28 +0000436 int ret;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 rcu_read_lock();
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000439 port = br_port_get_rcu(dev);
440 if (!port)
441 ret = 0;
442 else {
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100443 fdb = br_fdb_find_rcu(port->br, addr, 0);
stephen hemminger43598812011-12-08 07:17:49 +0000444 ret = fdb && fdb->dst && fdb->dst->dev != dev &&
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000445 fdb->dst->state == BR_STATE_FORWARDING;
446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Michał Mirosławda678292009-06-05 05:35:28 +0000449 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
Michał Mirosławda678292009-06-05 05:35:28 +0000451#endif /* CONFIG_ATM_LANE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453/*
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900454 * Fill buffer with forwarding table records in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * the API format.
456 */
457int br_fdb_fillbuf(struct net_bridge *br, void *buf,
458 unsigned long maxnum, unsigned long skip)
459{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct net_bridge_fdb_entry *f;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200461 struct __fdb_entry *fe = buf;
462 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
465
466 rcu_read_lock();
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200467 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
468 if (num >= maxnum)
469 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200471 if (has_expired(br, f))
472 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200474 /* ignore pseudo entry for local MAC address */
475 if (!f->dst)
476 continue;
stephen hemminger43598812011-12-08 07:17:49 +0000477
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200478 if (skip) {
479 --skip;
480 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200483 /* convert from internal format to API */
484 memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN);
485
486 /* due to ABI compat need to split into hi/lo */
487 fe->port_no = f->dst->port_no;
488 fe->port_hi = f->dst->port_no >> 8;
489
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200490 fe->is_local = test_bit(BR_FDB_LOCAL, &f->flags);
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200491 if (!test_bit(BR_FDB_STATIC, &f->flags))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200492 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
493 ++fe;
494 ++num;
495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 rcu_read_unlock();
497
498 return num;
499}
500
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200501static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct net_bridge_port *source,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000503 const unsigned char *addr,
Roopa Prabhub7af1472015-10-27 07:52:56 -0700504 __u16 vid,
Nikolay Aleksandrov3fb01a32019-10-29 13:45:59 +0200505 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 struct net_bridge_fdb_entry *fdb;
508
509 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
510 if (fdb) {
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200511 memcpy(fdb->key.addr.addr, addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 fdb->dst = source;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200513 fdb->key.vlan_id = vid;
Nikolay Aleksandrov3fb01a32019-10-29 13:45:59 +0200514 fdb->flags = flags;
stephen hemminger7cd88612011-04-04 14:03:28 +0000515 fdb->updated = fdb->used = jiffies;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200516 if (rhashtable_lookup_insert_fast(&br->fdb_hash_tbl,
517 &fdb->rhnode,
518 br_fdb_rht_params)) {
519 kmem_cache_free(br_fdb_cache, fdb);
520 fdb = NULL;
521 } else {
522 hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list);
523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 return fdb;
526}
527
528static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000529 const unsigned char *addr, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct net_bridge_fdb_entry *fdb;
532
533 if (!is_valid_ether_addr(addr))
534 return -EINVAL;
535
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100536 fdb = br_fdb_find(br, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (fdb) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900538 /* it is okay to have multiple ports with same
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 * address, just use the first one.
540 */
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200541 if (test_bit(BR_FDB_LOCAL, &fdb->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return 0;
Roopa Prabhude1dfee2016-10-11 16:12:56 -0700543 br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
544 source ? source->dev->name : br->dev->name, addr, vid);
Petr Machata161d82d2018-05-03 14:43:53 +0200545 fdb_delete(br, fdb, true);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Nikolay Aleksandrov3fb01a32019-10-29 13:45:59 +0200548 fdb = fdb_create(br, source, addr, vid,
549 BIT(BR_FDB_LOCAL) | BIT(BR_FDB_STATIC));
stephen hemminger03e9b642011-04-04 14:03:27 +0000550 if (!fdb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return -ENOMEM;
552
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100553 fdb_add_hw_addr(br, addr);
Petr Machata161d82d2018-05-03 14:43:53 +0200554 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return 0;
556}
557
stephen hemminger03e9b642011-04-04 14:03:27 +0000558/* Add entry for local address of interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000560 const unsigned char *addr, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 int ret;
563
564 spin_lock_bh(&br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000565 ret = fdb_insert(br, source, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 spin_unlock_bh(&br->hash_lock);
567 return ret;
568}
569
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300570/* returns true if the fdb was modified */
571static bool __fdb_mark_active(struct net_bridge_fdb_entry *fdb)
572{
573 return !!(test_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags) &&
574 test_and_clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags));
575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +0200578 const unsigned char *addr, u16 vid, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct net_bridge_fdb_entry *fdb;
581
582 /* some users want to always flood. */
583 if (hold_time(br) == 0)
584 return;
585
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200586 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (likely(fdb)) {
588 /* attempt to update an entry for a local interface */
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200589 if (unlikely(test_bit(BR_FDB_LOCAL, &fdb->flags))) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900590 if (net_ratelimit())
Roopa Prabhude1dfee2016-10-11 16:12:56 -0700591 br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
592 source->dev->name, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 } else {
stephen hemmingerca6d4482017-02-07 08:46:46 -0800594 unsigned long now = jiffies;
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300595 bool fdb_modified = false;
596
597 if (now != fdb->updated) {
598 fdb->updated = now;
599 fdb_modified = __fdb_mark_active(fdb);
600 }
stephen hemmingerca6d4482017-02-07 08:46:46 -0800601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* fastpath: update of existing entry */
Nikolay Aleksandrove0458d92019-10-29 13:45:55 +0200603 if (unlikely(source != fdb->dst &&
604 !test_bit(BR_FDB_STICKY, &fdb->flags))) {
Vladimir Oltean90dc8fd2021-01-06 11:51:30 +0200605 br_switchdev_fdb_notify(fdb, RTM_DELNEIGH);
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000606 fdb->dst = source;
607 fdb_modified = true;
Arkadi Sharshevsky58073b32017-04-28 22:39:07 +0300608 /* Take over HW learned entry */
Nikolay Aleksandrov58ec1ea2019-11-01 14:46:39 +0200609 if (unlikely(test_bit(BR_FDB_ADDED_BY_EXT_LEARN,
610 &fdb->flags)))
611 clear_bit(BR_FDB_ADDED_BY_EXT_LEARN,
612 &fdb->flags);
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000613 }
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300614
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +0200615 if (unlikely(test_bit(BR_FDB_ADDED_BY_USER, &flags)))
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200616 set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
Roopa Prabhue3cfddd2017-08-30 22:18:13 -0700617 if (unlikely(fdb_modified)) {
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +0200618 trace_br_fdb_update(br, source, addr, vid, flags);
Petr Machata161d82d2018-05-03 14:43:53 +0200619 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
Roopa Prabhue3cfddd2017-08-30 22:18:13 -0700620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622 } else {
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800623 spin_lock(&br->hash_lock);
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +0200624 fdb = fdb_create(br, source, addr, vid, flags);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200625 if (fdb) {
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +0200626 trace_br_fdb_update(br, source, addr, vid, flags);
Petr Machata161d82d2018-05-03 14:43:53 +0200627 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
stephen hemmingerf58ee4e2011-12-06 13:02:24 +0000628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* else we lose race and someone else inserts
630 * it first, don't bother updating
631 */
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800632 spin_unlock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000635
Roopa Prabhu37418732015-10-08 10:38:52 -0700636static int fdb_to_nud(const struct net_bridge *br,
637 const struct net_bridge_fdb_entry *fdb)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000638{
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200639 if (test_bit(BR_FDB_LOCAL, &fdb->flags))
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000640 return NUD_PERMANENT;
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200641 else if (test_bit(BR_FDB_STATIC, &fdb->flags))
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000642 return NUD_NOARP;
Roopa Prabhu37418732015-10-08 10:38:52 -0700643 else if (has_expired(br, fdb))
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000644 return NUD_STALE;
645 else
646 return NUD_REACHABLE;
647}
648
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000649static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000650 const struct net_bridge_fdb_entry *fdb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000651 u32 portid, u32 seq, int type, unsigned int flags)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000652{
653 unsigned long now = jiffies;
654 struct nda_cacheinfo ci;
655 struct nlmsghdr *nlh;
656 struct ndmsg *ndm;
657
Eric W. Biederman15e47302012-09-07 20:12:54 +0000658 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000659 if (nlh == NULL)
660 return -EMSGSIZE;
661
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000662 ndm = nlmsg_data(nlh);
663 ndm->ndm_family = AF_BRIDGE;
664 ndm->ndm_pad1 = 0;
665 ndm->ndm_pad2 = 0;
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200666 ndm->ndm_flags = 0;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000667 ndm->ndm_type = 0;
stephen hemminger43598812011-12-08 07:17:49 +0000668 ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
Roopa Prabhu37418732015-10-08 10:38:52 -0700669 ndm->ndm_state = fdb_to_nud(br, fdb);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000670
Nikolay Aleksandrovd38c6e32019-10-29 13:45:58 +0200671 if (test_bit(BR_FDB_OFFLOADED, &fdb->flags))
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200672 ndm->ndm_flags |= NTF_OFFLOADED;
Nikolay Aleksandrovb5cd9f72019-10-29 13:45:57 +0200673 if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags))
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200674 ndm->ndm_flags |= NTF_EXT_LEARNED;
Nikolay Aleksandrove0458d92019-10-29 13:45:55 +0200675 if (test_bit(BR_FDB_STICKY, &fdb->flags))
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300676 ndm->ndm_flags |= NTF_STICKY;
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200677
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200678 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr))
David S. Miller2eb812e2012-04-01 20:49:54 -0400679 goto nla_put_failure;
Roopa Prabhu41c389d2014-05-27 22:39:37 -0700680 if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
681 goto nla_put_failure;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000682 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
683 ci.ndm_confirmed = 0;
684 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
685 ci.ndm_refcnt = 0;
David S. Miller2eb812e2012-04-01 20:49:54 -0400686 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
687 goto nla_put_failure;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000688
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200689 if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16),
690 &fdb->key.vlan_id))
Vlad Yasevich1690be62013-02-13 12:00:18 +0000691 goto nla_put_failure;
692
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300693 if (test_bit(BR_FDB_NOTIFY, &fdb->flags)) {
694 struct nlattr *nest = nla_nest_start(skb, NDA_FDB_EXT_ATTRS);
695 u8 notify_bits = FDB_NOTIFY_BIT;
696
697 if (!nest)
698 goto nla_put_failure;
699 if (test_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags))
700 notify_bits |= FDB_NOTIFY_INACTIVE_BIT;
701
702 if (nla_put_u8(skb, NFEA_ACTIVITY_NOTIFY, notify_bits)) {
703 nla_nest_cancel(skb, nest);
704 goto nla_put_failure;
705 }
706
707 nla_nest_end(skb, nest);
708 }
709
Johannes Berg053c0952015-01-16 22:09:00 +0100710 nlmsg_end(skb, nlh);
711 return 0;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000712
713nla_put_failure:
714 nlmsg_cancel(skb, nlh);
715 return -EMSGSIZE;
716}
717
718static inline size_t fdb_nlmsg_size(void)
719{
720 return NLMSG_ALIGN(sizeof(struct ndmsg))
721 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Roopa Prabhu41c389d2014-05-27 22:39:37 -0700722 + nla_total_size(sizeof(u32)) /* NDA_MASTER */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000723 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300724 + nla_total_size(sizeof(struct nda_cacheinfo))
725 + nla_total_size(0) /* NDA_FDB_EXT_ATTRS */
726 + nla_total_size(sizeof(u8)); /* NFEA_ACTIVITY_NOTIFY */
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000727}
728
Vladimir Oltean04846f92021-03-23 01:51:45 +0200729static int br_fdb_replay_one(struct notifier_block *nb,
Vladimir Olteanbdf123b2021-06-27 14:54:26 +0300730 const struct net_bridge_fdb_entry *fdb,
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +0300731 struct net_device *dev, const void *ctx)
Vladimir Oltean04846f92021-03-23 01:51:45 +0200732{
733 struct switchdev_notifier_fdb_info item;
734 int err;
735
736 item.addr = fdb->key.addr.addr;
737 item.vid = fdb->key.vlan_id;
738 item.added_by_user = test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
739 item.offloaded = test_bit(BR_FDB_OFFLOADED, &fdb->flags);
Vladimir Olteane887b2d2021-06-27 14:54:22 +0300740 item.is_local = test_bit(BR_FDB_LOCAL, &fdb->flags);
Vladimir Oltean04846f92021-03-23 01:51:45 +0200741 item.info.dev = dev;
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +0300742 item.info.ctx = ctx;
Vladimir Oltean04846f92021-03-23 01:51:45 +0200743
744 err = nb->notifier_call(nb, SWITCHDEV_FDB_ADD_TO_DEVICE, &item);
745 return notifier_to_errno(err);
746}
747
Vladimir Olteanbdf123b2021-06-27 14:54:26 +0300748int br_fdb_replay(const struct net_device *br_dev, const struct net_device *dev,
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +0300749 const void *ctx, struct notifier_block *nb)
Vladimir Oltean04846f92021-03-23 01:51:45 +0200750{
751 struct net_bridge_fdb_entry *fdb;
752 struct net_bridge *br;
753 int err = 0;
754
755 if (!netif_is_bridge_master(br_dev) || !netif_is_bridge_port(dev))
756 return -EINVAL;
757
758 br = netdev_priv(br_dev);
759
760 rcu_read_lock();
761
762 hlist_for_each_entry_rcu(fdb, &br->fdb_list, fdb_node) {
Vladimir Olteanbdf123b2021-06-27 14:54:26 +0300763 const struct net_bridge_port *dst = READ_ONCE(fdb->dst);
Vladimir Oltean04846f92021-03-23 01:51:45 +0200764 struct net_device *dst_dev;
765
766 dst_dev = dst ? dst->dev : br->dev;
767 if (dst_dev != br_dev && dst_dev != dev)
768 continue;
769
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +0300770 err = br_fdb_replay_one(nb, fdb, dst_dev, ctx);
Vladimir Oltean04846f92021-03-23 01:51:45 +0200771 if (err)
772 break;
773 }
774
775 rcu_read_unlock();
776
777 return err;
778}
779EXPORT_SYMBOL_GPL(br_fdb_replay);
780
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000781static void fdb_notify(struct net_bridge *br,
Petr Machata161d82d2018-05-03 14:43:53 +0200782 const struct net_bridge_fdb_entry *fdb, int type,
783 bool swdev_notify)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000784{
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000785 struct net *net = dev_net(br->dev);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000786 struct sk_buff *skb;
787 int err = -ENOBUFS;
788
Petr Machata161d82d2018-05-03 14:43:53 +0200789 if (swdev_notify)
790 br_switchdev_fdb_notify(fdb, type);
Arkadi Sharshevsky6b26b512017-06-08 08:44:14 +0200791
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000792 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
793 if (skb == NULL)
794 goto errout;
795
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000796 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000797 if (err < 0) {
798 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
799 WARN_ON(err == -EMSGSIZE);
800 kfree_skb(skb);
801 goto errout;
802 }
803 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
804 return;
805errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800806 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000807}
808
809/* Dump information about entries, in response to GETNEIGH */
John Fastabend77162022012-04-15 06:43:56 +0000810int br_fdb_dump(struct sk_buff *skb,
811 struct netlink_callback *cb,
812 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400813 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -0700814 int *idx)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000815{
John Fastabend77162022012-04-15 06:43:56 +0000816 struct net_bridge *br = netdev_priv(dev);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200817 struct net_bridge_fdb_entry *f;
Roopa Prabhud2976532016-08-30 21:56:45 -0700818 int err = 0;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000819
John Fastabend77162022012-04-15 06:43:56 +0000820 if (!(dev->priv_flags & IFF_EBRIDGE))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200821 return err;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000822
Roopa Prabhud2976532016-08-30 21:56:45 -0700823 if (!filter_dev) {
824 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
825 if (err < 0)
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200826 return err;
Roopa Prabhud2976532016-08-30 21:56:45 -0700827 }
Hubert Sokolowski6cb69742015-01-05 17:29:21 +0000828
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200829 rcu_read_lock();
830 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
831 if (*idx < cb->args[2])
832 goto skip;
833 if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) {
834 if (filter_dev != dev)
John Fastabend77162022012-04-15 06:43:56 +0000835 goto skip;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200836 /* !f->dst is a special case for bridge
837 * It means the MAC belongs to the bridge
838 * Therefore need a little more filtering
839 * we only want to dump the !f->dst case
840 */
841 if (f->dst)
Hubert Sokolowski6cb69742015-01-05 17:29:21 +0000842 goto skip;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000843 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200844 if (!filter_dev && f->dst)
845 goto skip;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000846
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200847 err = fdb_fill_info(skb, br, f,
848 NETLINK_CB(cb->skb).portid,
849 cb->nlh->nlmsg_seq,
850 RTM_NEWNEIGH,
851 NLM_F_MULTI);
852 if (err < 0)
853 break;
854skip:
855 *idx += 1;
856 }
857 rcu_read_unlock();
858
Roopa Prabhud2976532016-08-30 21:56:45 -0700859 return err;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000860}
stephen hemminger36fd2b62011-04-04 14:03:31 +0000861
Roopa Prabhu47674562018-12-15 22:35:09 -0800862int br_fdb_get(struct sk_buff *skb,
863 struct nlattr *tb[],
864 struct net_device *dev,
865 const unsigned char *addr,
866 u16 vid, u32 portid, u32 seq,
867 struct netlink_ext_ack *extack)
868{
869 struct net_bridge *br = netdev_priv(dev);
870 struct net_bridge_fdb_entry *f;
871 int err = 0;
872
873 rcu_read_lock();
874 f = br_fdb_find_rcu(br, addr, vid);
875 if (!f) {
876 NL_SET_ERR_MSG(extack, "Fdb entry not found");
877 err = -ENOENT;
878 goto errout;
879 }
880
881 err = fdb_fill_info(skb, br, f, portid, seq,
882 RTM_NEWNEIGH, 0);
883errout:
884 rcu_read_unlock();
885 return err;
886}
887
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300888/* returns true if the fdb is modified */
889static bool fdb_handle_notify(struct net_bridge_fdb_entry *fdb, u8 notify)
890{
891 bool modified = false;
892
893 /* allow to mark an entry as inactive, usually done on creation */
894 if ((notify & FDB_NOTIFY_INACTIVE_BIT) &&
895 !test_and_set_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags))
896 modified = true;
897
898 if ((notify & FDB_NOTIFY_BIT) &&
899 !test_and_set_bit(BR_FDB_NOTIFY, &fdb->flags)) {
900 /* enabled activity tracking */
901 modified = true;
902 } else if (!(notify & FDB_NOTIFY_BIT) &&
903 test_and_clear_bit(BR_FDB_NOTIFY, &fdb->flags)) {
904 /* disabled activity tracking, clear notify state */
905 clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags);
906 modified = true;
907 }
908
909 return modified;
910}
911
stephen hemminger292d1392011-11-09 18:30:08 +0000912/* Update (create or replace) forwarding database entry */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900913static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300914 const u8 *addr, struct ndmsg *ndm, u16 flags, u16 vid,
915 struct nlattr *nfea_tb[])
stephen hemminger36fd2b62011-04-04 14:03:31 +0000916{
Nikolay Aleksandrov0592ff82020-06-23 23:47:15 +0300917 bool is_sticky = !!(ndm->ndm_flags & NTF_STICKY);
Nikolay Aleksandrovb5f1d9e2020-06-23 23:47:18 +0300918 bool refresh = !nfea_tb[NFEA_DONT_REFRESH];
stephen hemminger36fd2b62011-04-04 14:03:31 +0000919 struct net_bridge_fdb_entry *fdb;
Nikolay Aleksandrov0592ff82020-06-23 23:47:15 +0300920 u16 state = ndm->ndm_state;
roopab0a397f2013-04-22 12:56:49 +0000921 bool modified = false;
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300922 u8 notify = 0;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000923
Wilson Kokeb8d7ba2015-05-25 06:39:31 -0700924 /* If the port cannot learn allow only local and static entries */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900925 if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
Wilson Kokeb8d7ba2015-05-25 06:39:31 -0700926 !(source->state == BR_STATE_LEARNING ||
927 source->state == BR_STATE_FORWARDING))
928 return -EPERM;
929
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900930 if (!source && !(state & NUD_PERMANENT)) {
931 pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
932 br->dev->name);
933 return -EINVAL;
934 }
935
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300936 if (is_sticky && (state & NUD_PERMANENT))
937 return -EINVAL;
938
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300939 if (nfea_tb[NFEA_ACTIVITY_NOTIFY]) {
940 notify = nla_get_u8(nfea_tb[NFEA_ACTIVITY_NOTIFY]);
941 if ((notify & ~BR_FDB_NOTIFY_SETTABLE_BITS) ||
942 (notify & BR_FDB_NOTIFY_SETTABLE_BITS) == FDB_NOTIFY_INACTIVE_BIT)
943 return -EINVAL;
944 }
945
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100946 fdb = br_fdb_find(br, addr, vid);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000947 if (fdb == NULL) {
948 if (!(flags & NLM_F_CREATE))
949 return -ENOENT;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000950
Nikolay Aleksandrov3fb01a32019-10-29 13:45:59 +0200951 fdb = fdb_create(br, source, addr, vid, 0);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000952 if (!fdb)
953 return -ENOMEM;
roopab0a397f2013-04-22 12:56:49 +0000954
955 modified = true;
stephen hemminger64af1ba2011-09-30 14:37:27 +0000956 } else {
957 if (flags & NLM_F_EXCL)
958 return -EEXIST;
roopab0a397f2013-04-22 12:56:49 +0000959
960 if (fdb->dst != source) {
961 fdb->dst = source;
962 modified = true;
963 }
stephen hemminger64af1ba2011-09-30 14:37:27 +0000964 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000965
Roopa Prabhu37418732015-10-08 10:38:52 -0700966 if (fdb_to_nud(br, fdb) != state) {
Vlad Yasevich145beee2014-05-16 09:59:19 -0400967 if (state & NUD_PERMANENT) {
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200968 set_bit(BR_FDB_LOCAL, &fdb->flags);
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200969 if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags))
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100970 fdb_add_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400971 } else if (state & NUD_NOARP) {
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200972 clear_bit(BR_FDB_LOCAL, &fdb->flags);
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200973 if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags))
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100974 fdb_add_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400975 } else {
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200976 clear_bit(BR_FDB_LOCAL, &fdb->flags);
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200977 if (test_and_clear_bit(BR_FDB_STATIC, &fdb->flags))
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100978 fdb_del_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400979 }
stephen hemminger292d1392011-11-09 18:30:08 +0000980
roopab0a397f2013-04-22 12:56:49 +0000981 modified = true;
982 }
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300983
Nikolay Aleksandrove0458d92019-10-29 13:45:55 +0200984 if (is_sticky != test_bit(BR_FDB_STICKY, &fdb->flags)) {
985 change_bit(BR_FDB_STICKY, &fdb->flags);
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300986 modified = true;
987 }
988
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300989 if (fdb_handle_notify(fdb, notify))
990 modified = true;
991
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200992 set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
roopab0a397f2013-04-22 12:56:49 +0000993
994 fdb->used = jiffies;
995 if (modified) {
Nikolay Aleksandrovb5f1d9e2020-06-23 23:47:18 +0300996 if (refresh)
997 fdb->updated = jiffies;
Petr Machata161d82d2018-05-03 14:43:53 +0200998 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
stephen hemminger292d1392011-11-09 18:30:08 +0000999 }
1000
stephen hemminger36fd2b62011-04-04 14:03:31 +00001001 return 0;
1002}
1003
Toshiaki Makita7bb90c32016-08-04 11:11:19 +09001004static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br,
1005 struct net_bridge_port *p, const unsigned char *addr,
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +03001006 u16 nlh_flags, u16 vid, struct nlattr *nfea_tb[])
Vlad Yasevich1690be62013-02-13 12:00:18 +00001007{
1008 int err = 0;
1009
1010 if (ndm->ndm_flags & NTF_USE) {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +09001011 if (!p) {
1012 pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
1013 br->dev->name);
1014 return -EINVAL;
1015 }
Nikolay Aleksandrov5d1fcaf2019-11-04 11:36:51 +02001016 if (!nbp_state_should_learn(p))
1017 return 0;
1018
Nikolay Aleksandrovc4c832f2015-06-06 06:49:00 -07001019 local_bh_disable();
Vlad Yasevich1690be62013-02-13 12:00:18 +00001020 rcu_read_lock();
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +02001021 br_fdb_update(br, p, addr, vid, BIT(BR_FDB_ADDED_BY_USER));
Vlad Yasevich1690be62013-02-13 12:00:18 +00001022 rcu_read_unlock();
Nikolay Aleksandrovc4c832f2015-06-06 06:49:00 -07001023 local_bh_enable();
Nikolay Aleksandroveb100e02017-03-23 12:27:13 +02001024 } else if (ndm->ndm_flags & NTF_EXT_LEARNED) {
Petr Machata161d82d2018-05-03 14:43:53 +02001025 err = br_fdb_external_learn_add(br, p, addr, vid, true);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001026 } else {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +09001027 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +03001028 err = fdb_add_entry(br, p, addr, ndm, nlh_flags, vid, nfea_tb);
Toshiaki Makita7bb90c32016-08-04 11:11:19 +09001029 spin_unlock_bh(&br->hash_lock);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001030 }
1031
1032 return err;
1033}
1034
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +03001035static const struct nla_policy br_nda_fdb_pol[NFEA_MAX + 1] = {
1036 [NFEA_ACTIVITY_NOTIFY] = { .type = NLA_U8 },
Nikolay Aleksandrovb5f1d9e2020-06-23 23:47:18 +03001037 [NFEA_DONT_REFRESH] = { .type = NLA_FLAG },
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +03001038};
1039
stephen hemminger36fd2b62011-04-04 14:03:31 +00001040/* Add new permanent fdb entry with RTM_NEWNEIGH */
stephen hemmingeredc7d572012-10-01 12:32:33 +00001041int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1042 struct net_device *dev,
Petr Machata87b09842019-01-16 23:06:50 +00001043 const unsigned char *addr, u16 vid, u16 nlh_flags,
1044 struct netlink_ext_ack *extack)
stephen hemminger36fd2b62011-04-04 14:03:31 +00001045{
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +03001046 struct nlattr *nfea_tb[NFEA_MAX + 1], *attr;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001047 struct net_bridge_vlan_group *vg;
Roopa Prabhu37418732015-10-08 10:38:52 -07001048 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001049 struct net_bridge_vlan *v;
Roopa Prabhu37418732015-10-08 10:38:52 -07001050 struct net_bridge *br = NULL;
John Fastabend77162022012-04-15 06:43:56 +00001051 int err = 0;
stephen hemminger36fd2b62011-04-04 14:03:31 +00001052
Roopa Prabhub74fd302017-08-29 13:16:57 -07001053 trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);
1054
stephen hemminger292d1392011-11-09 18:30:08 +00001055 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
1056 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
1057 return -EINVAL;
1058 }
1059
Stephen Hemminger537f7f82013-06-25 09:34:36 -07001060 if (is_zero_ether_addr(addr)) {
1061 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
1062 return -EINVAL;
1063 }
1064
Roopa Prabhu37418732015-10-08 10:38:52 -07001065 if (dev->priv_flags & IFF_EBRIDGE) {
1066 br = netdev_priv(dev);
1067 vg = br_vlan_group(br);
1068 } else {
1069 p = br_port_get_rtnl(dev);
1070 if (!p) {
1071 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
1072 dev->name);
1073 return -EINVAL;
1074 }
Toshiaki Makita7bb90c32016-08-04 11:11:19 +09001075 br = p->br;
Roopa Prabhu37418732015-10-08 10:38:52 -07001076 vg = nbp_vlan_group(p);
stephen hemminger36fd2b62011-04-04 14:03:31 +00001077 }
1078
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +03001079 if (tb[NDA_FDB_EXT_ATTRS]) {
1080 attr = tb[NDA_FDB_EXT_ATTRS];
1081 err = nla_parse_nested(nfea_tb, NFEA_MAX, attr,
1082 br_nda_fdb_pol, extack);
1083 if (err)
1084 return err;
1085 } else {
1086 memset(nfea_tb, 0, sizeof(struct nlattr *) * (NFEA_MAX + 1));
1087 }
1088
Jiri Pirkof6f64242014-11-28 14:34:15 +01001089 if (vid) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001090 v = br_vlan_find(vg, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -07001091 if (!v || !br_vlan_should_use(v)) {
1092 pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001093 return -EINVAL;
1094 }
1095
1096 /* VID was specified, so use it. */
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +03001097 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid, nfea_tb);
stephen hemminger292d1392011-11-09 18:30:08 +00001098 } else {
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +03001099 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0, nfea_tb);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001100 if (err || !vg || !vg->num_vlans)
Vlad Yasevich1690be62013-02-13 12:00:18 +00001101 goto out;
Vlad Yasevich1690be62013-02-13 12:00:18 +00001102
1103 /* We have vlans configured on this port and user didn't
1104 * specify a VLAN. To be nice, add/update entry for every
1105 * vlan on this port.
1106 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001107 list_for_each_entry(v, &vg->vlan_list, vlist) {
Roopa Prabhu37418732015-10-08 10:38:52 -07001108 if (!br_vlan_should_use(v))
1109 continue;
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +03001110 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid,
1111 nfea_tb);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001112 if (err)
1113 goto out;
Vlad Yasevich1690be62013-02-13 12:00:18 +00001114 }
stephen hemminger292d1392011-11-09 18:30:08 +00001115 }
stephen hemminger36fd2b62011-04-04 14:03:31 +00001116
Vlad Yasevich1690be62013-02-13 12:00:18 +00001117out:
1118 return err;
1119}
1120
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001121static int fdb_delete_by_addr_and_port(struct net_bridge *br,
1122 const struct net_bridge_port *p,
Nikolay Aleksandrov8c86f962015-06-09 03:34:13 -07001123 const u8 *addr, u16 vlan)
Vlad Yasevich1690be62013-02-13 12:00:18 +00001124{
Vlad Yasevich1690be62013-02-13 12:00:18 +00001125 struct net_bridge_fdb_entry *fdb;
1126
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +01001127 fdb = br_fdb_find(br, addr, vlan);
Nikolay Aleksandrov8c86f962015-06-09 03:34:13 -07001128 if (!fdb || fdb->dst != p)
Vlad Yasevich1690be62013-02-13 12:00:18 +00001129 return -ENOENT;
1130
Petr Machata161d82d2018-05-03 14:43:53 +02001131 fdb_delete(br, fdb, true);
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001132
Vlad Yasevich1690be62013-02-13 12:00:18 +00001133 return 0;
1134}
1135
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001136static int __br_fdb_delete(struct net_bridge *br,
1137 const struct net_bridge_port *p,
Vlad Yasevich1690be62013-02-13 12:00:18 +00001138 const unsigned char *addr, u16 vid)
1139{
1140 int err;
1141
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001142 spin_lock_bh(&br->hash_lock);
1143 err = fdb_delete_by_addr_and_port(br, p, addr, vid);
1144 spin_unlock_bh(&br->hash_lock);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001145
stephen hemminger36fd2b62011-04-04 14:03:31 +00001146 return err;
1147}
1148
stephen hemminger36fd2b62011-04-04 14:03:31 +00001149/* Remove neighbor entry with RTM_DELNEIGH */
Vlad Yasevich1690be62013-02-13 12:00:18 +00001150int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1151 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01001152 const unsigned char *addr, u16 vid)
stephen hemminger36fd2b62011-04-04 14:03:31 +00001153{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001154 struct net_bridge_vlan_group *vg;
Roopa Prabhu37418732015-10-08 10:38:52 -07001155 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001156 struct net_bridge_vlan *v;
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001157 struct net_bridge *br;
stephen hemminger36fd2b62011-04-04 14:03:31 +00001158 int err;
1159
Roopa Prabhu37418732015-10-08 10:38:52 -07001160 if (dev->priv_flags & IFF_EBRIDGE) {
1161 br = netdev_priv(dev);
1162 vg = br_vlan_group(br);
1163 } else {
1164 p = br_port_get_rtnl(dev);
1165 if (!p) {
1166 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
1167 dev->name);
1168 return -EINVAL;
1169 }
1170 vg = nbp_vlan_group(p);
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001171 br = p->br;
stephen hemminger36fd2b62011-04-04 14:03:31 +00001172 }
1173
Jiri Pirkof6f64242014-11-28 14:34:15 +01001174 if (vid) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001175 v = br_vlan_find(vg, vid);
1176 if (!v) {
Roopa Prabhu37418732015-10-08 10:38:52 -07001177 pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001178 return -EINVAL;
1179 }
stephen hemminger36fd2b62011-04-04 14:03:31 +00001180
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001181 err = __br_fdb_delete(br, p, addr, vid);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001182 } else {
Toshiaki Makita25d3b492015-02-09 20:16:17 +09001183 err = -ENOENT;
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001184 err &= __br_fdb_delete(br, p, addr, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001185 if (!vg || !vg->num_vlans)
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001186 return err;
Vlad Yasevich1690be62013-02-13 12:00:18 +00001187
Roopa Prabhu37418732015-10-08 10:38:52 -07001188 list_for_each_entry(v, &vg->vlan_list, vlist) {
1189 if (!br_vlan_should_use(v))
1190 continue;
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001191 err &= __br_fdb_delete(br, p, addr, v->vid);
Roopa Prabhu37418732015-10-08 10:38:52 -07001192 }
Vlad Yasevich1690be62013-02-13 12:00:18 +00001193 }
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001194
stephen hemminger36fd2b62011-04-04 14:03:31 +00001195 return err;
1196}
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001197
1198int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
1199{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001200 struct net_bridge_fdb_entry *f, *tmp;
Geert Uytterhoeven367dc652018-02-01 11:25:27 +01001201 int err = 0;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001202
1203 ASSERT_RTNL();
1204
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001205 /* the key here is that static entries change only under rtnl */
1206 rcu_read_lock();
1207 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1208 /* We only care for static entries */
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +02001209 if (!test_bit(BR_FDB_STATIC, &f->flags))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001210 continue;
1211 err = dev_uc_add(p->dev, f->key.addr.addr);
1212 if (err)
1213 goto rollback;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001214 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001215done:
1216 rcu_read_unlock();
1217
1218 return err;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001219
1220rollback:
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001221 hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
1222 /* We only care for static entries */
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +02001223 if (!test_bit(BR_FDB_STATIC, &tmp->flags))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001224 continue;
1225 if (tmp == f)
1226 break;
1227 dev_uc_del(p->dev, tmp->key.addr.addr);
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001228 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001229
1230 goto done;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001231}
1232
1233void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
1234{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001235 struct net_bridge_fdb_entry *f;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001236
1237 ASSERT_RTNL();
1238
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001239 rcu_read_lock();
1240 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1241 /* We only care for static entries */
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +02001242 if (!test_bit(BR_FDB_STATIC, &f->flags))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001243 continue;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001244
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001245 dev_uc_del(p->dev, f->key.addr.addr);
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001246 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001247 rcu_read_unlock();
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001248}
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001249
Jiri Pirko3aeb6612015-01-15 23:49:37 +01001250int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
Petr Machata161d82d2018-05-03 14:43:53 +02001251 const unsigned char *addr, u16 vid,
1252 bool swdev_notify)
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001253{
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001254 struct net_bridge_fdb_entry *fdb;
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001255 bool modified = false;
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001256 int err = 0;
1257
Roopa Prabhub74fd302017-08-29 13:16:57 -07001258 trace_br_fdb_external_learn_add(br, p, addr, vid);
1259
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001260 spin_lock_bh(&br->hash_lock);
1261
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +01001262 fdb = br_fdb_find(br, addr, vid);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001263 if (!fdb) {
Nikolay Aleksandrov31f11552019-11-01 14:46:38 +02001264 unsigned long flags = BIT(BR_FDB_ADDED_BY_EXT_LEARN);
1265
1266 if (swdev_notify)
1267 flags |= BIT(BR_FDB_ADDED_BY_USER);
1268 fdb = fdb_create(br, p, addr, vid, flags);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001269 if (!fdb) {
1270 err = -ENOMEM;
1271 goto err_unlock;
1272 }
Petr Machata161d82d2018-05-03 14:43:53 +02001273 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001274 } else {
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001275 fdb->updated = jiffies;
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001276
1277 if (fdb->dst != p) {
1278 fdb->dst = p;
1279 modified = true;
1280 }
1281
Nikolay Aleksandrovb5cd9f72019-10-29 13:45:57 +02001282 if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) {
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001283 /* Refresh entry */
1284 fdb->used = jiffies;
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +02001285 } else if (!test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags)) {
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001286 /* Take over SW learned entry */
Nikolay Aleksandrovb5cd9f72019-10-29 13:45:57 +02001287 set_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags);
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001288 modified = true;
1289 }
1290
Ido Schimmel710ae722019-01-18 15:58:00 +00001291 if (swdev_notify)
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +02001292 set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
Ido Schimmel710ae722019-01-18 15:58:00 +00001293
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001294 if (modified)
Petr Machata161d82d2018-05-03 14:43:53 +02001295 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001296 }
1297
1298err_unlock:
1299 spin_unlock_bh(&br->hash_lock);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001300
1301 return err;
1302}
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001303
Jiri Pirko3aeb6612015-01-15 23:49:37 +01001304int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
Petr Machata161d82d2018-05-03 14:43:53 +02001305 const unsigned char *addr, u16 vid,
1306 bool swdev_notify)
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001307{
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001308 struct net_bridge_fdb_entry *fdb;
1309 int err = 0;
1310
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001311 spin_lock_bh(&br->hash_lock);
1312
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +01001313 fdb = br_fdb_find(br, addr, vid);
Nikolay Aleksandrovb5cd9f72019-10-29 13:45:57 +02001314 if (fdb && test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags))
Petr Machata161d82d2018-05-03 14:43:53 +02001315 fdb_delete(br, fdb, swdev_notify);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001316 else
1317 err = -ENOENT;
1318
1319 spin_unlock_bh(&br->hash_lock);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001320
1321 return err;
1322}
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +02001323
1324void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
Ido Schimmele9ba0fb2018-10-17 08:53:29 +00001325 const unsigned char *addr, u16 vid, bool offloaded)
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +02001326{
1327 struct net_bridge_fdb_entry *fdb;
1328
1329 spin_lock_bh(&br->hash_lock);
1330
1331 fdb = br_fdb_find(br, addr, vid);
Nikolay Aleksandrovd38c6e32019-10-29 13:45:58 +02001332 if (fdb && offloaded != test_bit(BR_FDB_OFFLOADED, &fdb->flags))
1333 change_bit(BR_FDB_OFFLOADED, &fdb->flags);
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +02001334
1335 spin_unlock_bh(&br->hash_lock);
1336}
Petr Machata43920ed2018-12-07 19:55:07 +00001337
1338void br_fdb_clear_offload(const struct net_device *dev, u16 vid)
1339{
1340 struct net_bridge_fdb_entry *f;
1341 struct net_bridge_port *p;
1342
1343 ASSERT_RTNL();
1344
1345 p = br_port_get_rtnl(dev);
1346 if (!p)
1347 return;
1348
1349 spin_lock_bh(&p->br->hash_lock);
1350 hlist_for_each_entry(f, &p->br->fdb_list, fdb_node) {
1351 if (f->dst == p && f->key.vlan_id == vid)
Nikolay Aleksandrovd38c6e32019-10-29 13:45:58 +02001352 clear_bit(BR_FDB_OFFLOADED, &f->flags);
Petr Machata43920ed2018-12-07 19:55:07 +00001353 }
1354 spin_unlock_bh(&p->br->hash_lock);
1355}
1356EXPORT_SYMBOL_GPL(br_fdb_clear_offload);