blob: b1d3248c02521307def6f0ef791ae64c97977d7f [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{
Roopa Prabhueda7a5e2017-02-16 13:38:04 -080078 return !fdb->is_static && !fdb->added_by_external_learn &&
stephen hemminger7cd88612011-04-04 14:03:28 +000079 time_before_eq(fdb->updated + hold_time(br), jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Michał Mirosławda678292009-06-05 05:35:28 +000082static void fdb_rcu_free(struct rcu_head *head)
83{
84 struct net_bridge_fdb_entry *ent
85 = container_of(head, struct net_bridge_fdb_entry, rcu);
86 kmem_cache_free(br_fdb_cache, ent);
87}
88
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020089static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl,
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +010090 const unsigned char *addr,
91 __u16 vid)
92{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020093 struct net_bridge_fdb_key key;
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +010094
Nikolay Aleksandrov410b3d42017-02-13 14:59:10 +010095 WARN_ON_ONCE(!rcu_read_lock_held());
96
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020097 key.vlan_id = vid;
98 memcpy(key.addr.addr, addr, sizeof(key.addr.addr));
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +010099
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200100 return rhashtable_lookup(tbl, &key, br_fdb_rht_params);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100101}
102
103/* requires bridge hash_lock */
104static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br,
105 const unsigned char *addr,
106 __u16 vid)
107{
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100108 struct net_bridge_fdb_entry *fdb;
109
WANG Congd12c9172017-03-16 10:32:42 -0700110 lockdep_assert_held_once(&br->hash_lock);
Nikolay Aleksandrov410b3d42017-02-13 14:59:10 +0100111
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100112 rcu_read_lock();
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200113 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100114 rcu_read_unlock();
115
116 return fdb;
117}
118
Petr Machata4d4fd362018-04-29 10:56:08 +0300119struct net_device *br_fdb_find_port(const struct net_device *br_dev,
120 const unsigned char *addr,
121 __u16 vid)
122{
123 struct net_bridge_fdb_entry *f;
124 struct net_device *dev = NULL;
125 struct net_bridge *br;
126
127 ASSERT_RTNL();
128
129 if (!netif_is_bridge_master(br_dev))
130 return NULL;
131
132 br = netdev_priv(br_dev);
Petr Machata873aca22018-06-08 15:11:47 +0200133 rcu_read_lock();
134 f = br_fdb_find_rcu(br, addr, vid);
Petr Machata4d4fd362018-04-29 10:56:08 +0300135 if (f && f->dst)
136 dev = f->dst->dev;
Petr Machata873aca22018-06-08 15:11:47 +0200137 rcu_read_unlock();
Petr Machata4d4fd362018-04-29 10:56:08 +0300138
139 return dev;
140}
141EXPORT_SYMBOL_GPL(br_fdb_find_port);
142
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100143struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
144 const unsigned char *addr,
145 __u16 vid)
146{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200147 return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100148}
149
Vlad Yasevich145beee2014-05-16 09:59:19 -0400150/* When a static FDB entry is added, the mac address from the entry is
151 * added to the bridge private HW address list and all required ports
152 * are then updated with the new information.
153 * Called under RTNL.
154 */
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100155static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr)
Vlad Yasevich145beee2014-05-16 09:59:19 -0400156{
157 int err;
Li RongQinga3f5ee72014-06-18 16:07:16 +0800158 struct net_bridge_port *p;
Vlad Yasevich145beee2014-05-16 09:59:19 -0400159
160 ASSERT_RTNL();
161
162 list_for_each_entry(p, &br->port_list, list) {
163 if (!br_promisc_port(p)) {
164 err = dev_uc_add(p->dev, addr);
165 if (err)
166 goto undo;
167 }
168 }
169
170 return;
171undo:
Li RongQinga3f5ee72014-06-18 16:07:16 +0800172 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
173 if (!br_promisc_port(p))
174 dev_uc_del(p->dev, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400175 }
176}
177
178/* When a static FDB entry is deleted, the HW address from that entry is
179 * also removed from the bridge private HW address list and updates all
180 * the ports with needed information.
181 * Called under RTNL.
182 */
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100183static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
Vlad Yasevich145beee2014-05-16 09:59:19 -0400184{
185 struct net_bridge_port *p;
186
187 ASSERT_RTNL();
188
189 list_for_each_entry(p, &br->port_list, list) {
190 if (!br_promisc_port(p))
191 dev_uc_del(p->dev, addr);
192 }
193}
194
Petr Machata161d82d2018-05-03 14:43:53 +0200195static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
196 bool swdev_notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Roopa Prabhub74fd302017-08-29 13:16:57 -0700198 trace_fdb_delete(br, f);
199
Vlad Yasevich145beee2014-05-16 09:59:19 -0400200 if (f->is_static)
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200201 fdb_del_hw_addr(br, f->key.addr.addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400202
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200203 hlist_del_init_rcu(&f->fdb_node);
204 rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode,
205 br_fdb_rht_params);
Petr Machata161d82d2018-05-03 14:43:53 +0200206 fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
Michał Mirosławda678292009-06-05 05:35:28 +0000207 call_rcu(&f->rcu, fdb_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Toshiaki Makita960b5892014-02-07 16:48:23 +0900210/* Delete a local entry if no other port had the same address. */
211static void fdb_delete_local(struct net_bridge *br,
212 const struct net_bridge_port *p,
213 struct net_bridge_fdb_entry *f)
214{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200215 const unsigned char *addr = f->key.addr.addr;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200216 struct net_bridge_vlan_group *vg;
217 const struct net_bridge_vlan *v;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900218 struct net_bridge_port *op;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200219 u16 vid = f->key.vlan_id;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900220
221 /* Maybe another port has same hw addr? */
222 list_for_each_entry(op, &br->port_list, list) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200223 vg = nbp_vlan_group(op);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900224 if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200225 (!vid || br_vlan_find(vg, vid))) {
Toshiaki Makita960b5892014-02-07 16:48:23 +0900226 f->dst = op;
Toshiaki Makitaa778e6d2014-02-07 16:48:24 +0900227 f->added_by_user = 0;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900228 return;
229 }
230 }
231
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200232 vg = br_vlan_group(br);
233 v = br_vlan_find(vg, vid);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900234 /* Maybe bridge device has same hw addr? */
235 if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200236 (!vid || (v && br_vlan_should_use(v)))) {
Toshiaki Makita960b5892014-02-07 16:48:23 +0900237 f->dst = NULL;
Toshiaki Makitaa778e6d2014-02-07 16:48:24 +0900238 f->added_by_user = 0;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900239 return;
240 }
241
Petr Machata161d82d2018-05-03 14:43:53 +0200242 fdb_delete(br, f, true);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900243}
244
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900245void br_fdb_find_delete_local(struct net_bridge *br,
246 const struct net_bridge_port *p,
247 const unsigned char *addr, u16 vid)
248{
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900249 struct net_bridge_fdb_entry *f;
250
251 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100252 f = br_fdb_find(br, addr, vid);
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900253 if (f && f->is_local && !f->added_by_user && f->dst == p)
254 fdb_delete_local(br, p, f);
255 spin_unlock_bh(&br->hash_lock);
256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
259{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200260 struct net_bridge_vlan_group *vg;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200261 struct net_bridge_fdb_entry *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct net_bridge *br = p->br;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200263 struct net_bridge_vlan *v;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200266 vg = nbp_vlan_group(p);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200267 hlist_for_each_entry(f, &br->fdb_list, fdb_node) {
268 if (f->dst == p && f->is_local && !f->added_by_user) {
269 /* delete old one */
270 fdb_delete_local(br, p, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200272 /* if this port has no vlan information
273 * configured, we can safely be done at
274 * this point.
275 */
276 if (!vg || !vg->num_vlans)
277 goto insert;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Toshiaki Makita28368822014-02-07 16:48:19 +0900281insert:
282 /* insert new address, may fail if invalid address or dup. */
283 fdb_insert(br, p, newaddr, 0);
284
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200285 if (!vg || !vg->num_vlans)
Toshiaki Makita28368822014-02-07 16:48:19 +0900286 goto done;
287
288 /* Now add entries for every VLAN configured on the port.
289 * This function runs under RTNL so the bitmap will not change
290 * from under us.
291 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200292 list_for_each_entry(v, &vg->vlan_list, vlist)
293 fdb_insert(br, p, newaddr, v->vid);
Toshiaki Makita28368822014-02-07 16:48:19 +0900294
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000295done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 spin_unlock_bh(&br->hash_lock);
297}
298
stephen hemminger43598812011-12-08 07:17:49 +0000299void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
300{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200301 struct net_bridge_vlan_group *vg;
stephen hemminger43598812011-12-08 07:17:49 +0000302 struct net_bridge_fdb_entry *f;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200303 struct net_bridge_vlan *v;
stephen hemminger43598812011-12-08 07:17:49 +0000304
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900305 spin_lock_bh(&br->hash_lock);
306
stephen hemminger43598812011-12-08 07:17:49 +0000307 /* If old entry was unassociated with any port, then delete it. */
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100308 f = br_fdb_find(br, br->dev->dev_addr, 0);
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900309 if (f && f->is_local && !f->dst && !f->added_by_user)
Toshiaki Makita960b5892014-02-07 16:48:23 +0900310 fdb_delete_local(br, NULL, f);
stephen hemminger43598812011-12-08 07:17:49 +0000311
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000312 fdb_insert(br, NULL, newaddr, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200313 vg = br_vlan_group(br);
314 if (!vg || !vg->num_vlans)
315 goto out;
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000316 /* Now remove and add entries for every VLAN configured on the
317 * bridge. This function runs under RTNL so the bitmap will not
318 * change from under us.
319 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200320 list_for_each_entry(v, &vg->vlan_list, vlist) {
Toshiaki Makita0b148de2016-06-07 19:14:17 +0900321 if (!br_vlan_should_use(v))
322 continue;
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100323 f = br_fdb_find(br, br->dev->dev_addr, v->vid);
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900324 if (f && f->is_local && !f->dst && !f->added_by_user)
Toshiaki Makita960b5892014-02-07 16:48:23 +0900325 fdb_delete_local(br, NULL, f);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200326 fdb_insert(br, NULL, newaddr, v->vid);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000327 }
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900328out:
329 spin_unlock_bh(&br->hash_lock);
stephen hemminger43598812011-12-08 07:17:49 +0000330}
331
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100332void br_fdb_cleanup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100334 struct net_bridge *br = container_of(work, struct net_bridge,
335 gc_work.work);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200336 struct net_bridge_fdb_entry *f = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 unsigned long delay = hold_time(br);
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100338 unsigned long work_delay = delay;
339 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200341 /* this part is tricky, in order to avoid blocking learning and
342 * consequently forwarding, we rely on rcu to delete objects with
343 * delayed freeing allowing us to continue traversing
344 */
345 rcu_read_lock();
346 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
347 unsigned long this_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200349 if (f->is_static || f->added_by_external_learn)
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100350 continue;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200351 this_timer = f->updated + delay;
352 if (time_after(this_timer, now)) {
353 work_delay = min(work_delay, this_timer - now);
354 } else {
355 spin_lock_bh(&br->hash_lock);
356 if (!hlist_unhashed(&f->fdb_node))
Petr Machata161d82d2018-05-03 14:43:53 +0200357 fdb_delete(br, f, true);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200358 spin_unlock_bh(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200361 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100363 /* Cleanup minimum 10 milliseconds apart */
364 work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10));
365 mod_delayed_work(system_long_wq, &br->gc_work, work_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700368/* Completely flush all dynamic entries in forwarding database.*/
369void br_fdb_flush(struct net_bridge *br)
370{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200371 struct net_bridge_fdb_entry *f;
372 struct hlist_node *tmp;
Stephen Hemminger1a620692006-10-12 14:45:38 -0700373
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700374 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200375 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
376 if (!f->is_static)
Petr Machata161d82d2018-05-03 14:43:53 +0200377 fdb_delete(br, f, true);
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700378 }
379 spin_unlock_bh(&br->hash_lock);
380}
381
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300382/* Flush all entries referring to a specific port.
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700383 * if do_all is set also flush static entries
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700384 * if vid is set delete all entries that match the vlan_id
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700385 */
Stephen Hemminger1a620692006-10-12 14:45:38 -0700386void br_fdb_delete_by_port(struct net_bridge *br,
387 const struct net_bridge_port *p,
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700388 u16 vid,
Stephen Hemminger1a620692006-10-12 14:45:38 -0700389 int do_all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200391 struct net_bridge_fdb_entry *f;
392 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200395 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
396 if (f->dst != p)
397 continue;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900398
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200399 if (!do_all)
400 if (f->is_static || (vid && f->key.vlan_id != vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 continue;
402
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200403 if (f->is_local)
404 fdb_delete_local(br, p, f);
405 else
Petr Machata161d82d2018-05-03 14:43:53 +0200406 fdb_delete(br, f, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408 spin_unlock_bh(&br->hash_lock);
409}
410
Igor Maraviće6373c42011-12-12 02:58:25 +0000411#if IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +0000412/* Interface used by ATM LANE hook to test
413 * if an addr is on some other bridge port */
414int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 struct net_bridge_fdb_entry *fdb;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000417 struct net_bridge_port *port;
Michał Mirosławda678292009-06-05 05:35:28 +0000418 int ret;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 rcu_read_lock();
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000421 port = br_port_get_rcu(dev);
422 if (!port)
423 ret = 0;
424 else {
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100425 fdb = br_fdb_find_rcu(port->br, addr, 0);
stephen hemminger43598812011-12-08 07:17:49 +0000426 ret = fdb && fdb->dst && fdb->dst->dev != dev &&
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000427 fdb->dst->state == BR_STATE_FORWARDING;
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Michał Mirosławda678292009-06-05 05:35:28 +0000431 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
Michał Mirosławda678292009-06-05 05:35:28 +0000433#endif /* CONFIG_ATM_LANE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435/*
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900436 * Fill buffer with forwarding table records in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * the API format.
438 */
439int br_fdb_fillbuf(struct net_bridge *br, void *buf,
440 unsigned long maxnum, unsigned long skip)
441{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct net_bridge_fdb_entry *f;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200443 struct __fdb_entry *fe = buf;
444 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
447
448 rcu_read_lock();
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200449 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
450 if (num >= maxnum)
451 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200453 if (has_expired(br, f))
454 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200456 /* ignore pseudo entry for local MAC address */
457 if (!f->dst)
458 continue;
stephen hemminger43598812011-12-08 07:17:49 +0000459
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200460 if (skip) {
461 --skip;
462 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200465 /* convert from internal format to API */
466 memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN);
467
468 /* due to ABI compat need to split into hi/lo */
469 fe->port_no = f->dst->port_no;
470 fe->port_hi = f->dst->port_no >> 8;
471
472 fe->is_local = f->is_local;
473 if (!f->is_static)
474 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
475 ++fe;
476 ++num;
477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 rcu_read_unlock();
479
480 return num;
481}
482
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200483static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct net_bridge_port *source,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000485 const unsigned char *addr,
Roopa Prabhub7af1472015-10-27 07:52:56 -0700486 __u16 vid,
487 unsigned char is_local,
488 unsigned char is_static)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct net_bridge_fdb_entry *fdb;
491
492 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
493 if (fdb) {
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200494 memcpy(fdb->key.addr.addr, addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 fdb->dst = source;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200496 fdb->key.vlan_id = vid;
Roopa Prabhub7af1472015-10-27 07:52:56 -0700497 fdb->is_local = is_local;
498 fdb->is_static = is_static;
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900499 fdb->added_by_user = 0;
Scott Feldmancf6b8e12014-11-28 14:34:21 +0100500 fdb->added_by_external_learn = 0;
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200501 fdb->offloaded = 0;
Nikolay Aleksandrov1288aa72018-09-27 15:05:10 +0300502 fdb->is_sticky = 0;
stephen hemminger7cd88612011-04-04 14:03:28 +0000503 fdb->updated = fdb->used = jiffies;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200504 if (rhashtable_lookup_insert_fast(&br->fdb_hash_tbl,
505 &fdb->rhnode,
506 br_fdb_rht_params)) {
507 kmem_cache_free(br_fdb_cache, fdb);
508 fdb = NULL;
509 } else {
510 hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list);
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 return fdb;
514}
515
516static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000517 const unsigned char *addr, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct net_bridge_fdb_entry *fdb;
520
521 if (!is_valid_ether_addr(addr))
522 return -EINVAL;
523
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100524 fdb = br_fdb_find(br, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (fdb) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900526 /* it is okay to have multiple ports with same
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * address, just use the first one.
528 */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900529 if (fdb->is_local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return 0;
Roopa Prabhude1dfee2016-10-11 16:12:56 -0700531 br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
532 source ? source->dev->name : br->dev->name, addr, vid);
Petr Machata161d82d2018-05-03 14:43:53 +0200533 fdb_delete(br, fdb, true);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200536 fdb = fdb_create(br, source, addr, vid, 1, 1);
stephen hemminger03e9b642011-04-04 14:03:27 +0000537 if (!fdb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return -ENOMEM;
539
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100540 fdb_add_hw_addr(br, addr);
Petr Machata161d82d2018-05-03 14:43:53 +0200541 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return 0;
543}
544
stephen hemminger03e9b642011-04-04 14:03:27 +0000545/* Add entry for local address of interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000547 const unsigned char *addr, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 int ret;
550
551 spin_lock_bh(&br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000552 ret = fdb_insert(br, source, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 spin_unlock_bh(&br->hash_lock);
554 return ret;
555}
556
557void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900558 const unsigned char *addr, u16 vid, bool added_by_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct net_bridge_fdb_entry *fdb;
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000561 bool fdb_modified = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 /* some users want to always flood. */
564 if (hold_time(br) == 0)
565 return;
566
Stephen Hemmingerdf1c0b82007-08-30 22:15:35 -0700567 /* ignore packets unless we are using this port */
568 if (!(source->state == BR_STATE_LEARNING ||
569 source->state == BR_STATE_FORWARDING))
570 return;
571
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200572 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (likely(fdb)) {
574 /* attempt to update an entry for a local interface */
575 if (unlikely(fdb->is_local)) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900576 if (net_ratelimit())
Roopa Prabhude1dfee2016-10-11 16:12:56 -0700577 br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
578 source->dev->name, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 } else {
stephen hemmingerca6d4482017-02-07 08:46:46 -0800580 unsigned long now = jiffies;
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* fastpath: update of existing entry */
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300583 if (unlikely(source != fdb->dst && !fdb->is_sticky)) {
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000584 fdb->dst = source;
585 fdb_modified = true;
Arkadi Sharshevsky58073b32017-04-28 22:39:07 +0300586 /* Take over HW learned entry */
587 if (unlikely(fdb->added_by_external_learn))
588 fdb->added_by_external_learn = 0;
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000589 }
stephen hemmingerca6d4482017-02-07 08:46:46 -0800590 if (now != fdb->updated)
591 fdb->updated = now;
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900592 if (unlikely(added_by_user))
593 fdb->added_by_user = 1;
Roopa Prabhue3cfddd2017-08-30 22:18:13 -0700594 if (unlikely(fdb_modified)) {
595 trace_br_fdb_update(br, source, addr, vid, added_by_user);
Petr Machata161d82d2018-05-03 14:43:53 +0200596 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
Roopa Prabhue3cfddd2017-08-30 22:18:13 -0700597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599 } else {
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800600 spin_lock(&br->hash_lock);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200601 fdb = fdb_create(br, source, addr, vid, 0, 0);
602 if (fdb) {
603 if (unlikely(added_by_user))
604 fdb->added_by_user = 1;
605 trace_br_fdb_update(br, source, addr, vid,
606 added_by_user);
Petr Machata161d82d2018-05-03 14:43:53 +0200607 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
stephen hemmingerf58ee4e2011-12-06 13:02:24 +0000608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* else we lose race and someone else inserts
610 * it first, don't bother updating
611 */
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800612 spin_unlock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000615
Roopa Prabhu37418732015-10-08 10:38:52 -0700616static int fdb_to_nud(const struct net_bridge *br,
617 const struct net_bridge_fdb_entry *fdb)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000618{
619 if (fdb->is_local)
620 return NUD_PERMANENT;
621 else if (fdb->is_static)
622 return NUD_NOARP;
Roopa Prabhu37418732015-10-08 10:38:52 -0700623 else if (has_expired(br, fdb))
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000624 return NUD_STALE;
625 else
626 return NUD_REACHABLE;
627}
628
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000629static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000630 const struct net_bridge_fdb_entry *fdb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000631 u32 portid, u32 seq, int type, unsigned int flags)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000632{
633 unsigned long now = jiffies;
634 struct nda_cacheinfo ci;
635 struct nlmsghdr *nlh;
636 struct ndmsg *ndm;
637
Eric W. Biederman15e47302012-09-07 20:12:54 +0000638 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000639 if (nlh == NULL)
640 return -EMSGSIZE;
641
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000642 ndm = nlmsg_data(nlh);
643 ndm->ndm_family = AF_BRIDGE;
644 ndm->ndm_pad1 = 0;
645 ndm->ndm_pad2 = 0;
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200646 ndm->ndm_flags = 0;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000647 ndm->ndm_type = 0;
stephen hemminger43598812011-12-08 07:17:49 +0000648 ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
Roopa Prabhu37418732015-10-08 10:38:52 -0700649 ndm->ndm_state = fdb_to_nud(br, fdb);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000650
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200651 if (fdb->offloaded)
652 ndm->ndm_flags |= NTF_OFFLOADED;
653 if (fdb->added_by_external_learn)
654 ndm->ndm_flags |= NTF_EXT_LEARNED;
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300655 if (fdb->is_sticky)
656 ndm->ndm_flags |= NTF_STICKY;
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200657
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200658 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr))
David S. Miller2eb812e2012-04-01 20:49:54 -0400659 goto nla_put_failure;
Roopa Prabhu41c389d2014-05-27 22:39:37 -0700660 if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
661 goto nla_put_failure;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000662 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
663 ci.ndm_confirmed = 0;
664 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
665 ci.ndm_refcnt = 0;
David S. Miller2eb812e2012-04-01 20:49:54 -0400666 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
667 goto nla_put_failure;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000668
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200669 if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16),
670 &fdb->key.vlan_id))
Vlad Yasevich1690be62013-02-13 12:00:18 +0000671 goto nla_put_failure;
672
Johannes Berg053c0952015-01-16 22:09:00 +0100673 nlmsg_end(skb, nlh);
674 return 0;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000675
676nla_put_failure:
677 nlmsg_cancel(skb, nlh);
678 return -EMSGSIZE;
679}
680
681static inline size_t fdb_nlmsg_size(void)
682{
683 return NLMSG_ALIGN(sizeof(struct ndmsg))
684 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Roopa Prabhu41c389d2014-05-27 22:39:37 -0700685 + nla_total_size(sizeof(u32)) /* NDA_MASTER */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000686 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000687 + nla_total_size(sizeof(struct nda_cacheinfo));
688}
689
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000690static void fdb_notify(struct net_bridge *br,
Petr Machata161d82d2018-05-03 14:43:53 +0200691 const struct net_bridge_fdb_entry *fdb, int type,
692 bool swdev_notify)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000693{
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000694 struct net *net = dev_net(br->dev);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000695 struct sk_buff *skb;
696 int err = -ENOBUFS;
697
Petr Machata161d82d2018-05-03 14:43:53 +0200698 if (swdev_notify)
699 br_switchdev_fdb_notify(fdb, type);
Arkadi Sharshevsky6b26b512017-06-08 08:44:14 +0200700
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000701 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
702 if (skb == NULL)
703 goto errout;
704
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000705 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000706 if (err < 0) {
707 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
708 WARN_ON(err == -EMSGSIZE);
709 kfree_skb(skb);
710 goto errout;
711 }
712 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
713 return;
714errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800715 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000716}
717
718/* Dump information about entries, in response to GETNEIGH */
John Fastabend77162022012-04-15 06:43:56 +0000719int br_fdb_dump(struct sk_buff *skb,
720 struct netlink_callback *cb,
721 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400722 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -0700723 int *idx)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000724{
John Fastabend77162022012-04-15 06:43:56 +0000725 struct net_bridge *br = netdev_priv(dev);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200726 struct net_bridge_fdb_entry *f;
Roopa Prabhud2976532016-08-30 21:56:45 -0700727 int err = 0;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000728
John Fastabend77162022012-04-15 06:43:56 +0000729 if (!(dev->priv_flags & IFF_EBRIDGE))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200730 return err;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000731
Roopa Prabhud2976532016-08-30 21:56:45 -0700732 if (!filter_dev) {
733 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
734 if (err < 0)
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200735 return err;
Roopa Prabhud2976532016-08-30 21:56:45 -0700736 }
Hubert Sokolowski6cb69742015-01-05 17:29:21 +0000737
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200738 rcu_read_lock();
739 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
740 if (*idx < cb->args[2])
741 goto skip;
742 if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) {
743 if (filter_dev != dev)
John Fastabend77162022012-04-15 06:43:56 +0000744 goto skip;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200745 /* !f->dst is a special case for bridge
746 * It means the MAC belongs to the bridge
747 * Therefore need a little more filtering
748 * we only want to dump the !f->dst case
749 */
750 if (f->dst)
Hubert Sokolowski6cb69742015-01-05 17:29:21 +0000751 goto skip;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000752 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200753 if (!filter_dev && f->dst)
754 goto skip;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000755
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200756 err = fdb_fill_info(skb, br, f,
757 NETLINK_CB(cb->skb).portid,
758 cb->nlh->nlmsg_seq,
759 RTM_NEWNEIGH,
760 NLM_F_MULTI);
761 if (err < 0)
762 break;
763skip:
764 *idx += 1;
765 }
766 rcu_read_unlock();
767
Roopa Prabhud2976532016-08-30 21:56:45 -0700768 return err;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000769}
stephen hemminger36fd2b62011-04-04 14:03:31 +0000770
Roopa Prabhu47674562018-12-15 22:35:09 -0800771int br_fdb_get(struct sk_buff *skb,
772 struct nlattr *tb[],
773 struct net_device *dev,
774 const unsigned char *addr,
775 u16 vid, u32 portid, u32 seq,
776 struct netlink_ext_ack *extack)
777{
778 struct net_bridge *br = netdev_priv(dev);
779 struct net_bridge_fdb_entry *f;
780 int err = 0;
781
782 rcu_read_lock();
783 f = br_fdb_find_rcu(br, addr, vid);
784 if (!f) {
785 NL_SET_ERR_MSG(extack, "Fdb entry not found");
786 err = -ENOENT;
787 goto errout;
788 }
789
790 err = fdb_fill_info(skb, br, f, portid, seq,
791 RTM_NEWNEIGH, 0);
792errout:
793 rcu_read_unlock();
794 return err;
795}
796
stephen hemminger292d1392011-11-09 18:30:08 +0000797/* Update (create or replace) forwarding database entry */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900798static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300799 const u8 *addr, u16 state, u16 flags, u16 vid,
800 u8 ndm_flags)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000801{
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300802 u8 is_sticky = !!(ndm_flags & NTF_STICKY);
stephen hemminger36fd2b62011-04-04 14:03:31 +0000803 struct net_bridge_fdb_entry *fdb;
roopab0a397f2013-04-22 12:56:49 +0000804 bool modified = false;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000805
Wilson Kokeb8d7ba2015-05-25 06:39:31 -0700806 /* If the port cannot learn allow only local and static entries */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900807 if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
Wilson Kokeb8d7ba2015-05-25 06:39:31 -0700808 !(source->state == BR_STATE_LEARNING ||
809 source->state == BR_STATE_FORWARDING))
810 return -EPERM;
811
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900812 if (!source && !(state & NUD_PERMANENT)) {
813 pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
814 br->dev->name);
815 return -EINVAL;
816 }
817
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300818 if (is_sticky && (state & NUD_PERMANENT))
819 return -EINVAL;
820
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100821 fdb = br_fdb_find(br, addr, vid);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000822 if (fdb == NULL) {
823 if (!(flags & NLM_F_CREATE))
824 return -ENOENT;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000825
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200826 fdb = fdb_create(br, source, addr, vid, 0, 0);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000827 if (!fdb)
828 return -ENOMEM;
roopab0a397f2013-04-22 12:56:49 +0000829
830 modified = true;
stephen hemminger64af1ba2011-09-30 14:37:27 +0000831 } else {
832 if (flags & NLM_F_EXCL)
833 return -EEXIST;
roopab0a397f2013-04-22 12:56:49 +0000834
835 if (fdb->dst != source) {
836 fdb->dst = source;
837 modified = true;
838 }
stephen hemminger64af1ba2011-09-30 14:37:27 +0000839 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000840
Roopa Prabhu37418732015-10-08 10:38:52 -0700841 if (fdb_to_nud(br, fdb) != state) {
Vlad Yasevich145beee2014-05-16 09:59:19 -0400842 if (state & NUD_PERMANENT) {
843 fdb->is_local = 1;
844 if (!fdb->is_static) {
845 fdb->is_static = 1;
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100846 fdb_add_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400847 }
848 } else if (state & NUD_NOARP) {
stephen hemminger292d1392011-11-09 18:30:08 +0000849 fdb->is_local = 0;
Vlad Yasevich145beee2014-05-16 09:59:19 -0400850 if (!fdb->is_static) {
851 fdb->is_static = 1;
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100852 fdb_add_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400853 }
854 } else {
855 fdb->is_local = 0;
856 if (fdb->is_static) {
857 fdb->is_static = 0;
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100858 fdb_del_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400859 }
860 }
stephen hemminger292d1392011-11-09 18:30:08 +0000861
roopab0a397f2013-04-22 12:56:49 +0000862 modified = true;
863 }
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300864
865 if (is_sticky != fdb->is_sticky) {
866 fdb->is_sticky = is_sticky;
867 modified = true;
868 }
869
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900870 fdb->added_by_user = 1;
roopab0a397f2013-04-22 12:56:49 +0000871
872 fdb->used = jiffies;
873 if (modified) {
874 fdb->updated = jiffies;
Petr Machata161d82d2018-05-03 14:43:53 +0200875 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
stephen hemminger292d1392011-11-09 18:30:08 +0000876 }
877
stephen hemminger36fd2b62011-04-04 14:03:31 +0000878 return 0;
879}
880
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900881static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br,
882 struct net_bridge_port *p, const unsigned char *addr,
883 u16 nlh_flags, u16 vid)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000884{
885 int err = 0;
886
887 if (ndm->ndm_flags & NTF_USE) {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900888 if (!p) {
889 pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
890 br->dev->name);
891 return -EINVAL;
892 }
Nikolay Aleksandrovc4c832f2015-06-06 06:49:00 -0700893 local_bh_disable();
Vlad Yasevich1690be62013-02-13 12:00:18 +0000894 rcu_read_lock();
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900895 br_fdb_update(br, p, addr, vid, true);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000896 rcu_read_unlock();
Nikolay Aleksandrovc4c832f2015-06-06 06:49:00 -0700897 local_bh_enable();
Nikolay Aleksandroveb100e02017-03-23 12:27:13 +0200898 } else if (ndm->ndm_flags & NTF_EXT_LEARNED) {
Petr Machata161d82d2018-05-03 14:43:53 +0200899 err = br_fdb_external_learn_add(br, p, addr, vid, true);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000900 } else {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900901 spin_lock_bh(&br->hash_lock);
902 err = fdb_add_entry(br, p, addr, ndm->ndm_state,
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300903 nlh_flags, vid, ndm->ndm_flags);
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900904 spin_unlock_bh(&br->hash_lock);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000905 }
906
907 return err;
908}
909
stephen hemminger36fd2b62011-04-04 14:03:31 +0000910/* Add new permanent fdb entry with RTM_NEWNEIGH */
stephen hemmingeredc7d572012-10-01 12:32:33 +0000911int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
912 struct net_device *dev,
Petr Machata87b09842019-01-16 23:06:50 +0000913 const unsigned char *addr, u16 vid, u16 nlh_flags,
914 struct netlink_ext_ack *extack)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000915{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200916 struct net_bridge_vlan_group *vg;
Roopa Prabhu37418732015-10-08 10:38:52 -0700917 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200918 struct net_bridge_vlan *v;
Roopa Prabhu37418732015-10-08 10:38:52 -0700919 struct net_bridge *br = NULL;
John Fastabend77162022012-04-15 06:43:56 +0000920 int err = 0;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000921
Roopa Prabhub74fd302017-08-29 13:16:57 -0700922 trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);
923
stephen hemminger292d1392011-11-09 18:30:08 +0000924 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
925 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
926 return -EINVAL;
927 }
928
Stephen Hemminger537f7f82013-06-25 09:34:36 -0700929 if (is_zero_ether_addr(addr)) {
930 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
931 return -EINVAL;
932 }
933
Roopa Prabhu37418732015-10-08 10:38:52 -0700934 if (dev->priv_flags & IFF_EBRIDGE) {
935 br = netdev_priv(dev);
936 vg = br_vlan_group(br);
937 } else {
938 p = br_port_get_rtnl(dev);
939 if (!p) {
940 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
941 dev->name);
942 return -EINVAL;
943 }
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900944 br = p->br;
Roopa Prabhu37418732015-10-08 10:38:52 -0700945 vg = nbp_vlan_group(p);
stephen hemminger36fd2b62011-04-04 14:03:31 +0000946 }
947
Jiri Pirkof6f64242014-11-28 14:34:15 +0100948 if (vid) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200949 v = br_vlan_find(vg, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700950 if (!v || !br_vlan_should_use(v)) {
951 pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000952 return -EINVAL;
953 }
954
955 /* VID was specified, so use it. */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900956 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid);
stephen hemminger292d1392011-11-09 18:30:08 +0000957 } else {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900958 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200959 if (err || !vg || !vg->num_vlans)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000960 goto out;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000961
962 /* We have vlans configured on this port and user didn't
963 * specify a VLAN. To be nice, add/update entry for every
964 * vlan on this port.
965 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200966 list_for_each_entry(v, &vg->vlan_list, vlist) {
Roopa Prabhu37418732015-10-08 10:38:52 -0700967 if (!br_vlan_should_use(v))
968 continue;
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900969 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000970 if (err)
971 goto out;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000972 }
stephen hemminger292d1392011-11-09 18:30:08 +0000973 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000974
Vlad Yasevich1690be62013-02-13 12:00:18 +0000975out:
976 return err;
977}
978
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +0100979static int fdb_delete_by_addr_and_port(struct net_bridge *br,
980 const struct net_bridge_port *p,
Nikolay Aleksandrov8c86f962015-06-09 03:34:13 -0700981 const u8 *addr, u16 vlan)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000982{
Vlad Yasevich1690be62013-02-13 12:00:18 +0000983 struct net_bridge_fdb_entry *fdb;
984
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100985 fdb = br_fdb_find(br, addr, vlan);
Nikolay Aleksandrov8c86f962015-06-09 03:34:13 -0700986 if (!fdb || fdb->dst != p)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000987 return -ENOENT;
988
Petr Machata161d82d2018-05-03 14:43:53 +0200989 fdb_delete(br, fdb, true);
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +0100990
Vlad Yasevich1690be62013-02-13 12:00:18 +0000991 return 0;
992}
993
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +0100994static int __br_fdb_delete(struct net_bridge *br,
995 const struct net_bridge_port *p,
Vlad Yasevich1690be62013-02-13 12:00:18 +0000996 const unsigned char *addr, u16 vid)
997{
998 int err;
999
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001000 spin_lock_bh(&br->hash_lock);
1001 err = fdb_delete_by_addr_and_port(br, p, addr, vid);
1002 spin_unlock_bh(&br->hash_lock);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001003
stephen hemminger36fd2b62011-04-04 14:03:31 +00001004 return err;
1005}
1006
stephen hemminger36fd2b62011-04-04 14:03:31 +00001007/* Remove neighbor entry with RTM_DELNEIGH */
Vlad Yasevich1690be62013-02-13 12:00:18 +00001008int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1009 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01001010 const unsigned char *addr, u16 vid)
stephen hemminger36fd2b62011-04-04 14:03:31 +00001011{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001012 struct net_bridge_vlan_group *vg;
Roopa Prabhu37418732015-10-08 10:38:52 -07001013 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001014 struct net_bridge_vlan *v;
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001015 struct net_bridge *br;
stephen hemminger36fd2b62011-04-04 14:03:31 +00001016 int err;
1017
Roopa Prabhu37418732015-10-08 10:38:52 -07001018 if (dev->priv_flags & IFF_EBRIDGE) {
1019 br = netdev_priv(dev);
1020 vg = br_vlan_group(br);
1021 } else {
1022 p = br_port_get_rtnl(dev);
1023 if (!p) {
1024 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
1025 dev->name);
1026 return -EINVAL;
1027 }
1028 vg = nbp_vlan_group(p);
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001029 br = p->br;
stephen hemminger36fd2b62011-04-04 14:03:31 +00001030 }
1031
Jiri Pirkof6f64242014-11-28 14:34:15 +01001032 if (vid) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001033 v = br_vlan_find(vg, vid);
1034 if (!v) {
Roopa Prabhu37418732015-10-08 10:38:52 -07001035 pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001036 return -EINVAL;
1037 }
stephen hemminger36fd2b62011-04-04 14:03:31 +00001038
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001039 err = __br_fdb_delete(br, p, addr, vid);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001040 } else {
Toshiaki Makita25d3b492015-02-09 20:16:17 +09001041 err = -ENOENT;
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001042 err &= __br_fdb_delete(br, p, addr, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001043 if (!vg || !vg->num_vlans)
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001044 return err;
Vlad Yasevich1690be62013-02-13 12:00:18 +00001045
Roopa Prabhu37418732015-10-08 10:38:52 -07001046 list_for_each_entry(v, &vg->vlan_list, vlist) {
1047 if (!br_vlan_should_use(v))
1048 continue;
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001049 err &= __br_fdb_delete(br, p, addr, v->vid);
Roopa Prabhu37418732015-10-08 10:38:52 -07001050 }
Vlad Yasevich1690be62013-02-13 12:00:18 +00001051 }
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001052
stephen hemminger36fd2b62011-04-04 14:03:31 +00001053 return err;
1054}
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001055
1056int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
1057{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001058 struct net_bridge_fdb_entry *f, *tmp;
Geert Uytterhoeven367dc652018-02-01 11:25:27 +01001059 int err = 0;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001060
1061 ASSERT_RTNL();
1062
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001063 /* the key here is that static entries change only under rtnl */
1064 rcu_read_lock();
1065 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1066 /* We only care for static entries */
1067 if (!f->is_static)
1068 continue;
1069 err = dev_uc_add(p->dev, f->key.addr.addr);
1070 if (err)
1071 goto rollback;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001072 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001073done:
1074 rcu_read_unlock();
1075
1076 return err;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001077
1078rollback:
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001079 hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
1080 /* We only care for static entries */
1081 if (!tmp->is_static)
1082 continue;
1083 if (tmp == f)
1084 break;
1085 dev_uc_del(p->dev, tmp->key.addr.addr);
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001086 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001087
1088 goto done;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001089}
1090
1091void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
1092{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001093 struct net_bridge_fdb_entry *f;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001094
1095 ASSERT_RTNL();
1096
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001097 rcu_read_lock();
1098 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1099 /* We only care for static entries */
1100 if (!f->is_static)
1101 continue;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001102
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001103 dev_uc_del(p->dev, f->key.addr.addr);
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001104 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001105 rcu_read_unlock();
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001106}
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001107
Jiri Pirko3aeb6612015-01-15 23:49:37 +01001108int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
Petr Machata161d82d2018-05-03 14:43:53 +02001109 const unsigned char *addr, u16 vid,
1110 bool swdev_notify)
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001111{
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001112 struct net_bridge_fdb_entry *fdb;
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001113 bool modified = false;
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001114 int err = 0;
1115
Roopa Prabhub74fd302017-08-29 13:16:57 -07001116 trace_br_fdb_external_learn_add(br, p, addr, vid);
1117
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001118 spin_lock_bh(&br->hash_lock);
1119
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +01001120 fdb = br_fdb_find(br, addr, vid);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001121 if (!fdb) {
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001122 fdb = fdb_create(br, p, addr, vid, 0, 0);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001123 if (!fdb) {
1124 err = -ENOMEM;
1125 goto err_unlock;
1126 }
Ido Schimmel710ae722019-01-18 15:58:00 +00001127 if (swdev_notify)
1128 fdb->added_by_user = 1;
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001129 fdb->added_by_external_learn = 1;
Petr Machata161d82d2018-05-03 14:43:53 +02001130 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001131 } else {
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001132 fdb->updated = jiffies;
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001133
1134 if (fdb->dst != p) {
1135 fdb->dst = p;
1136 modified = true;
1137 }
1138
1139 if (fdb->added_by_external_learn) {
1140 /* Refresh entry */
1141 fdb->used = jiffies;
1142 } else if (!fdb->added_by_user) {
1143 /* Take over SW learned entry */
1144 fdb->added_by_external_learn = 1;
1145 modified = true;
1146 }
1147
Ido Schimmel710ae722019-01-18 15:58:00 +00001148 if (swdev_notify)
1149 fdb->added_by_user = 1;
1150
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001151 if (modified)
Petr Machata161d82d2018-05-03 14:43:53 +02001152 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001153 }
1154
1155err_unlock:
1156 spin_unlock_bh(&br->hash_lock);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001157
1158 return err;
1159}
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001160
Jiri Pirko3aeb6612015-01-15 23:49:37 +01001161int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
Petr Machata161d82d2018-05-03 14:43:53 +02001162 const unsigned char *addr, u16 vid,
1163 bool swdev_notify)
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001164{
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001165 struct net_bridge_fdb_entry *fdb;
1166 int err = 0;
1167
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001168 spin_lock_bh(&br->hash_lock);
1169
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +01001170 fdb = br_fdb_find(br, addr, vid);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001171 if (fdb && fdb->added_by_external_learn)
Petr Machata161d82d2018-05-03 14:43:53 +02001172 fdb_delete(br, fdb, swdev_notify);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001173 else
1174 err = -ENOENT;
1175
1176 spin_unlock_bh(&br->hash_lock);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001177
1178 return err;
1179}
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +02001180
1181void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
Ido Schimmele9ba0fb2018-10-17 08:53:29 +00001182 const unsigned char *addr, u16 vid, bool offloaded)
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +02001183{
1184 struct net_bridge_fdb_entry *fdb;
1185
1186 spin_lock_bh(&br->hash_lock);
1187
1188 fdb = br_fdb_find(br, addr, vid);
1189 if (fdb)
Ido Schimmele9ba0fb2018-10-17 08:53:29 +00001190 fdb->offloaded = offloaded;
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +02001191
1192 spin_unlock_bh(&br->hash_lock);
1193}
Petr Machata43920ed2018-12-07 19:55:07 +00001194
1195void br_fdb_clear_offload(const struct net_device *dev, u16 vid)
1196{
1197 struct net_bridge_fdb_entry *f;
1198 struct net_bridge_port *p;
1199
1200 ASSERT_RTNL();
1201
1202 p = br_port_get_rtnl(dev);
1203 if (!p)
1204 return;
1205
1206 spin_lock_bh(&p->br->hash_lock);
1207 hlist_for_each_entry(f, &p->br->fdb_list, fdb_node) {
1208 if (f->dst == p && f->key.vlan_id == vid)
1209 f->offloaded = 0;
1210 }
1211 spin_unlock_bh(&p->br->hash_lock);
1212}
1213EXPORT_SYMBOL_GPL(br_fdb_clear_offload);