blob: e56ba3912a905b3617db0f470c32d45652d7359c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Forwarding database
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14#include <linux/kernel.h>
15#include <linux/init.h>
Franck Bui-Huu82524742008-05-12 21:21:05 +020016#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/spinlock.h>
18#include <linux/times.h>
19#include <linux/netdevice.h>
20#include <linux/etherdevice.h>
21#include <linux/jhash.h>
Stephen Hemminger3f890922007-03-21 13:42:33 -070022#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Arun Sharma600634972011-07-26 16:09:06 -070024#include <linux/atomic.h>
Stephen Hemminger3f890922007-03-21 13:42:33 -070025#include <asm/unaligned.h>
Vlad Yasevich2ba071e2013-02-13 12:00:16 +000026#include <linux/if_vlan.h>
Scott Feldmanb4ad7ba2015-06-14 11:33:11 -070027#include <net/switchdev.h>
Roopa Prabhub74fd302017-08-29 13:16:57 -070028#include <trace/events/bridge.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "br_private.h"
30
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020031static const struct rhashtable_params br_fdb_rht_params = {
32 .head_offset = offsetof(struct net_bridge_fdb_entry, rhnode),
33 .key_offset = offsetof(struct net_bridge_fdb_entry, key),
34 .key_len = sizeof(struct net_bridge_fdb_key),
35 .automatic_shrinking = true,
36 .locks_mul = 1,
37};
38
Christoph Lametere18b8902006-12-06 20:33:20 -080039static struct kmem_cache *br_fdb_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000041 const unsigned char *addr, u16 vid);
stephen hemminger31e8a49c2011-12-08 07:17:41 +000042static void fdb_notify(struct net_bridge *br,
Petr Machata161d82d2018-05-03 14:43:53 +020043 const struct net_bridge_fdb_entry *, int, bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Akinobu Mita87a596e2007-04-07 18:57:07 +090045int __init br_fdb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
48 sizeof(struct net_bridge_fdb_entry),
49 0,
Paul Mundt20c2df82007-07-20 10:11:58 +090050 SLAB_HWCACHE_ALIGN, NULL);
Akinobu Mita87a596e2007-04-07 18:57:07 +090051 if (!br_fdb_cache)
52 return -ENOMEM;
53
Akinobu Mita87a596e2007-04-07 18:57:07 +090054 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Andrew Morton73afc902007-12-05 21:35:23 -080057void br_fdb_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 kmem_cache_destroy(br_fdb_cache);
60}
61
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020062int br_fdb_hash_init(struct net_bridge *br)
63{
64 return rhashtable_init(&br->fdb_hash_tbl, &br_fdb_rht_params);
65}
66
67void br_fdb_hash_fini(struct net_bridge *br)
68{
69 rhashtable_destroy(&br->fdb_hash_tbl);
70}
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/* if topology_changing then use forward_delay (default 15 sec)
73 * otherwise keep longer (default 5 minutes)
74 */
Stephen Hemminger3f890922007-03-21 13:42:33 -070075static inline unsigned long hold_time(const struct net_bridge *br)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 return br->topology_change ? br->forward_delay : br->ageing_time;
78}
79
Stephen Hemminger3f890922007-03-21 13:42:33 -070080static inline int has_expired(const struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 const struct net_bridge_fdb_entry *fdb)
82{
Roopa Prabhueda7a5e2017-02-16 13:38:04 -080083 return !fdb->is_static && !fdb->added_by_external_learn &&
stephen hemminger7cd88612011-04-04 14:03:28 +000084 time_before_eq(fdb->updated + hold_time(br), jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Michał Mirosławda678292009-06-05 05:35:28 +000087static void fdb_rcu_free(struct rcu_head *head)
88{
89 struct net_bridge_fdb_entry *ent
90 = container_of(head, struct net_bridge_fdb_entry, rcu);
91 kmem_cache_free(br_fdb_cache, ent);
92}
93
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020094static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl,
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +010095 const unsigned char *addr,
96 __u16 vid)
97{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020098 struct net_bridge_fdb_key key;
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +010099
Nikolay Aleksandrov410b3d42017-02-13 14:59:10 +0100100 WARN_ON_ONCE(!rcu_read_lock_held());
101
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200102 key.vlan_id = vid;
103 memcpy(key.addr.addr, addr, sizeof(key.addr.addr));
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100104
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200105 return rhashtable_lookup(tbl, &key, br_fdb_rht_params);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100106}
107
108/* requires bridge hash_lock */
109static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br,
110 const unsigned char *addr,
111 __u16 vid)
112{
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100113 struct net_bridge_fdb_entry *fdb;
114
WANG Congd12c9172017-03-16 10:32:42 -0700115 lockdep_assert_held_once(&br->hash_lock);
Nikolay Aleksandrov410b3d42017-02-13 14:59:10 +0100116
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100117 rcu_read_lock();
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200118 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100119 rcu_read_unlock();
120
121 return fdb;
122}
123
Petr Machata4d4fd362018-04-29 10:56:08 +0300124struct net_device *br_fdb_find_port(const struct net_device *br_dev,
125 const unsigned char *addr,
126 __u16 vid)
127{
128 struct net_bridge_fdb_entry *f;
129 struct net_device *dev = NULL;
130 struct net_bridge *br;
131
132 ASSERT_RTNL();
133
134 if (!netif_is_bridge_master(br_dev))
135 return NULL;
136
137 br = netdev_priv(br_dev);
Petr Machata873aca22018-06-08 15:11:47 +0200138 rcu_read_lock();
139 f = br_fdb_find_rcu(br, addr, vid);
Petr Machata4d4fd362018-04-29 10:56:08 +0300140 if (f && f->dst)
141 dev = f->dst->dev;
Petr Machata873aca22018-06-08 15:11:47 +0200142 rcu_read_unlock();
Petr Machata4d4fd362018-04-29 10:56:08 +0300143
144 return dev;
145}
146EXPORT_SYMBOL_GPL(br_fdb_find_port);
147
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100148struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
149 const unsigned char *addr,
150 __u16 vid)
151{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200152 return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100153}
154
Vlad Yasevich145beee2014-05-16 09:59:19 -0400155/* When a static FDB entry is added, the mac address from the entry is
156 * added to the bridge private HW address list and all required ports
157 * are then updated with the new information.
158 * Called under RTNL.
159 */
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100160static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr)
Vlad Yasevich145beee2014-05-16 09:59:19 -0400161{
162 int err;
Li RongQinga3f5ee72014-06-18 16:07:16 +0800163 struct net_bridge_port *p;
Vlad Yasevich145beee2014-05-16 09:59:19 -0400164
165 ASSERT_RTNL();
166
167 list_for_each_entry(p, &br->port_list, list) {
168 if (!br_promisc_port(p)) {
169 err = dev_uc_add(p->dev, addr);
170 if (err)
171 goto undo;
172 }
173 }
174
175 return;
176undo:
Li RongQinga3f5ee72014-06-18 16:07:16 +0800177 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
178 if (!br_promisc_port(p))
179 dev_uc_del(p->dev, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400180 }
181}
182
183/* When a static FDB entry is deleted, the HW address from that entry is
184 * also removed from the bridge private HW address list and updates all
185 * the ports with needed information.
186 * Called under RTNL.
187 */
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100188static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
Vlad Yasevich145beee2014-05-16 09:59:19 -0400189{
190 struct net_bridge_port *p;
191
192 ASSERT_RTNL();
193
194 list_for_each_entry(p, &br->port_list, list) {
195 if (!br_promisc_port(p))
196 dev_uc_del(p->dev, addr);
197 }
198}
199
Petr Machata161d82d2018-05-03 14:43:53 +0200200static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
201 bool swdev_notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Roopa Prabhub74fd302017-08-29 13:16:57 -0700203 trace_fdb_delete(br, f);
204
Vlad Yasevich145beee2014-05-16 09:59:19 -0400205 if (f->is_static)
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200206 fdb_del_hw_addr(br, f->key.addr.addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400207
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200208 hlist_del_init_rcu(&f->fdb_node);
209 rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode,
210 br_fdb_rht_params);
Petr Machata161d82d2018-05-03 14:43:53 +0200211 fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
Michał Mirosławda678292009-06-05 05:35:28 +0000212 call_rcu(&f->rcu, fdb_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Toshiaki Makita960b5892014-02-07 16:48:23 +0900215/* Delete a local entry if no other port had the same address. */
216static void fdb_delete_local(struct net_bridge *br,
217 const struct net_bridge_port *p,
218 struct net_bridge_fdb_entry *f)
219{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200220 const unsigned char *addr = f->key.addr.addr;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200221 struct net_bridge_vlan_group *vg;
222 const struct net_bridge_vlan *v;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900223 struct net_bridge_port *op;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200224 u16 vid = f->key.vlan_id;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900225
226 /* Maybe another port has same hw addr? */
227 list_for_each_entry(op, &br->port_list, list) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200228 vg = nbp_vlan_group(op);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900229 if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200230 (!vid || br_vlan_find(vg, vid))) {
Toshiaki Makita960b5892014-02-07 16:48:23 +0900231 f->dst = op;
Toshiaki Makitaa778e6d2014-02-07 16:48:24 +0900232 f->added_by_user = 0;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900233 return;
234 }
235 }
236
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200237 vg = br_vlan_group(br);
238 v = br_vlan_find(vg, vid);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900239 /* Maybe bridge device has same hw addr? */
240 if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200241 (!vid || (v && br_vlan_should_use(v)))) {
Toshiaki Makita960b5892014-02-07 16:48:23 +0900242 f->dst = NULL;
Toshiaki Makitaa778e6d2014-02-07 16:48:24 +0900243 f->added_by_user = 0;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900244 return;
245 }
246
Petr Machata161d82d2018-05-03 14:43:53 +0200247 fdb_delete(br, f, true);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900248}
249
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900250void br_fdb_find_delete_local(struct net_bridge *br,
251 const struct net_bridge_port *p,
252 const unsigned char *addr, u16 vid)
253{
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900254 struct net_bridge_fdb_entry *f;
255
256 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100257 f = br_fdb_find(br, addr, vid);
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900258 if (f && f->is_local && !f->added_by_user && f->dst == p)
259 fdb_delete_local(br, p, f);
260 spin_unlock_bh(&br->hash_lock);
261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
264{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200265 struct net_bridge_vlan_group *vg;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200266 struct net_bridge_fdb_entry *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct net_bridge *br = p->br;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200268 struct net_bridge_vlan *v;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200271 vg = nbp_vlan_group(p);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200272 hlist_for_each_entry(f, &br->fdb_list, fdb_node) {
273 if (f->dst == p && f->is_local && !f->added_by_user) {
274 /* delete old one */
275 fdb_delete_local(br, p, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200277 /* if this port has no vlan information
278 * configured, we can safely be done at
279 * this point.
280 */
281 if (!vg || !vg->num_vlans)
282 goto insert;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Toshiaki Makita28368822014-02-07 16:48:19 +0900286insert:
287 /* insert new address, may fail if invalid address or dup. */
288 fdb_insert(br, p, newaddr, 0);
289
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200290 if (!vg || !vg->num_vlans)
Toshiaki Makita28368822014-02-07 16:48:19 +0900291 goto done;
292
293 /* Now add entries for every VLAN configured on the port.
294 * This function runs under RTNL so the bitmap will not change
295 * from under us.
296 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200297 list_for_each_entry(v, &vg->vlan_list, vlist)
298 fdb_insert(br, p, newaddr, v->vid);
Toshiaki Makita28368822014-02-07 16:48:19 +0900299
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000300done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 spin_unlock_bh(&br->hash_lock);
302}
303
stephen hemminger43598812011-12-08 07:17:49 +0000304void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
305{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200306 struct net_bridge_vlan_group *vg;
stephen hemminger43598812011-12-08 07:17:49 +0000307 struct net_bridge_fdb_entry *f;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200308 struct net_bridge_vlan *v;
stephen hemminger43598812011-12-08 07:17:49 +0000309
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900310 spin_lock_bh(&br->hash_lock);
311
stephen hemminger43598812011-12-08 07:17:49 +0000312 /* If old entry was unassociated with any port, then delete it. */
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100313 f = br_fdb_find(br, br->dev->dev_addr, 0);
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900314 if (f && f->is_local && !f->dst && !f->added_by_user)
Toshiaki Makita960b5892014-02-07 16:48:23 +0900315 fdb_delete_local(br, NULL, f);
stephen hemminger43598812011-12-08 07:17:49 +0000316
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000317 fdb_insert(br, NULL, newaddr, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200318 vg = br_vlan_group(br);
319 if (!vg || !vg->num_vlans)
320 goto out;
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000321 /* Now remove and add entries for every VLAN configured on the
322 * bridge. This function runs under RTNL so the bitmap will not
323 * change from under us.
324 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200325 list_for_each_entry(v, &vg->vlan_list, vlist) {
Toshiaki Makita0b148de2016-06-07 19:14:17 +0900326 if (!br_vlan_should_use(v))
327 continue;
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100328 f = br_fdb_find(br, br->dev->dev_addr, v->vid);
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900329 if (f && f->is_local && !f->dst && !f->added_by_user)
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) {
352 unsigned long this_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200354 if (f->is_static || f->added_by_external_learn)
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100355 continue;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200356 this_timer = f->updated + delay;
357 if (time_after(this_timer, now)) {
358 work_delay = min(work_delay, this_timer - now);
359 } else {
360 spin_lock_bh(&br->hash_lock);
361 if (!hlist_unhashed(&f->fdb_node))
Petr Machata161d82d2018-05-03 14:43:53 +0200362 fdb_delete(br, f, true);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200363 spin_unlock_bh(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200366 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100368 /* Cleanup minimum 10 milliseconds apart */
369 work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10));
370 mod_delayed_work(system_long_wq, &br->gc_work, work_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700373/* Completely flush all dynamic entries in forwarding database.*/
374void br_fdb_flush(struct net_bridge *br)
375{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200376 struct net_bridge_fdb_entry *f;
377 struct hlist_node *tmp;
Stephen Hemminger1a620692006-10-12 14:45:38 -0700378
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700379 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200380 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
381 if (!f->is_static)
Petr Machata161d82d2018-05-03 14:43:53 +0200382 fdb_delete(br, f, true);
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700383 }
384 spin_unlock_bh(&br->hash_lock);
385}
386
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300387/* Flush all entries referring to a specific port.
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700388 * if do_all is set also flush static entries
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700389 * if vid is set delete all entries that match the vlan_id
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700390 */
Stephen Hemminger1a620692006-10-12 14:45:38 -0700391void br_fdb_delete_by_port(struct net_bridge *br,
392 const struct net_bridge_port *p,
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700393 u16 vid,
Stephen Hemminger1a620692006-10-12 14:45:38 -0700394 int do_all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200396 struct net_bridge_fdb_entry *f;
397 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200400 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
401 if (f->dst != p)
402 continue;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900403
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200404 if (!do_all)
405 if (f->is_static || (vid && f->key.vlan_id != vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 continue;
407
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200408 if (f->is_local)
409 fdb_delete_local(br, p, f);
410 else
Petr Machata161d82d2018-05-03 14:43:53 +0200411 fdb_delete(br, f, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 spin_unlock_bh(&br->hash_lock);
414}
415
Igor Maraviće6373c42011-12-12 02:58:25 +0000416#if IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +0000417/* Interface used by ATM LANE hook to test
418 * if an addr is on some other bridge port */
419int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct net_bridge_fdb_entry *fdb;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000422 struct net_bridge_port *port;
Michał Mirosławda678292009-06-05 05:35:28 +0000423 int ret;
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 rcu_read_lock();
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000426 port = br_port_get_rcu(dev);
427 if (!port)
428 ret = 0;
429 else {
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100430 fdb = br_fdb_find_rcu(port->br, addr, 0);
stephen hemminger43598812011-12-08 07:17:49 +0000431 ret = fdb && fdb->dst && fdb->dst->dev != dev &&
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000432 fdb->dst->state == BR_STATE_FORWARDING;
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Michał Mirosławda678292009-06-05 05:35:28 +0000436 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
Michał Mirosławda678292009-06-05 05:35:28 +0000438#endif /* CONFIG_ATM_LANE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440/*
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900441 * Fill buffer with forwarding table records in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * the API format.
443 */
444int br_fdb_fillbuf(struct net_bridge *br, void *buf,
445 unsigned long maxnum, unsigned long skip)
446{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct net_bridge_fdb_entry *f;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200448 struct __fdb_entry *fe = buf;
449 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
452
453 rcu_read_lock();
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200454 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
455 if (num >= maxnum)
456 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200458 if (has_expired(br, f))
459 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200461 /* ignore pseudo entry for local MAC address */
462 if (!f->dst)
463 continue;
stephen hemminger43598812011-12-08 07:17:49 +0000464
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200465 if (skip) {
466 --skip;
467 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200470 /* convert from internal format to API */
471 memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN);
472
473 /* due to ABI compat need to split into hi/lo */
474 fe->port_no = f->dst->port_no;
475 fe->port_hi = f->dst->port_no >> 8;
476
477 fe->is_local = f->is_local;
478 if (!f->is_static)
479 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
480 ++fe;
481 ++num;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 rcu_read_unlock();
484
485 return num;
486}
487
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200488static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 struct net_bridge_port *source,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000490 const unsigned char *addr,
Roopa Prabhub7af1472015-10-27 07:52:56 -0700491 __u16 vid,
492 unsigned char is_local,
493 unsigned char is_static)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct net_bridge_fdb_entry *fdb;
496
497 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
498 if (fdb) {
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200499 memcpy(fdb->key.addr.addr, addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 fdb->dst = source;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200501 fdb->key.vlan_id = vid;
Roopa Prabhub7af1472015-10-27 07:52:56 -0700502 fdb->is_local = is_local;
503 fdb->is_static = is_static;
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900504 fdb->added_by_user = 0;
Scott Feldmancf6b8e12014-11-28 14:34:21 +0100505 fdb->added_by_external_learn = 0;
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200506 fdb->offloaded = 0;
Nikolay Aleksandrov1288aa72018-09-27 15:05:10 +0300507 fdb->is_sticky = 0;
stephen hemminger7cd88612011-04-04 14:03:28 +0000508 fdb->updated = fdb->used = jiffies;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200509 if (rhashtable_lookup_insert_fast(&br->fdb_hash_tbl,
510 &fdb->rhnode,
511 br_fdb_rht_params)) {
512 kmem_cache_free(br_fdb_cache, fdb);
513 fdb = NULL;
514 } else {
515 hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list);
516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518 return fdb;
519}
520
521static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000522 const unsigned char *addr, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct net_bridge_fdb_entry *fdb;
525
526 if (!is_valid_ether_addr(addr))
527 return -EINVAL;
528
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100529 fdb = br_fdb_find(br, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (fdb) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900531 /* it is okay to have multiple ports with same
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 * address, just use the first one.
533 */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900534 if (fdb->is_local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return 0;
Roopa Prabhude1dfee2016-10-11 16:12:56 -0700536 br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
537 source ? source->dev->name : br->dev->name, addr, vid);
Petr Machata161d82d2018-05-03 14:43:53 +0200538 fdb_delete(br, fdb, true);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200541 fdb = fdb_create(br, source, addr, vid, 1, 1);
stephen hemminger03e9b642011-04-04 14:03:27 +0000542 if (!fdb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return -ENOMEM;
544
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100545 fdb_add_hw_addr(br, addr);
Petr Machata161d82d2018-05-03 14:43:53 +0200546 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return 0;
548}
549
stephen hemminger03e9b642011-04-04 14:03:27 +0000550/* Add entry for local address of interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000552 const unsigned char *addr, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 int ret;
555
556 spin_lock_bh(&br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000557 ret = fdb_insert(br, source, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 spin_unlock_bh(&br->hash_lock);
559 return ret;
560}
561
562void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900563 const unsigned char *addr, u16 vid, bool added_by_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct net_bridge_fdb_entry *fdb;
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000566 bool fdb_modified = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /* some users want to always flood. */
569 if (hold_time(br) == 0)
570 return;
571
Stephen Hemmingerdf1c0b82007-08-30 22:15:35 -0700572 /* ignore packets unless we are using this port */
573 if (!(source->state == BR_STATE_LEARNING ||
574 source->state == BR_STATE_FORWARDING))
575 return;
576
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200577 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (likely(fdb)) {
579 /* attempt to update an entry for a local interface */
580 if (unlikely(fdb->is_local)) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900581 if (net_ratelimit())
Roopa Prabhude1dfee2016-10-11 16:12:56 -0700582 br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
583 source->dev->name, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 } else {
stephen hemmingerca6d4482017-02-07 08:46:46 -0800585 unsigned long now = jiffies;
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* fastpath: update of existing entry */
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300588 if (unlikely(source != fdb->dst && !fdb->is_sticky)) {
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000589 fdb->dst = source;
590 fdb_modified = true;
Arkadi Sharshevsky58073b32017-04-28 22:39:07 +0300591 /* Take over HW learned entry */
592 if (unlikely(fdb->added_by_external_learn))
593 fdb->added_by_external_learn = 0;
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000594 }
stephen hemmingerca6d4482017-02-07 08:46:46 -0800595 if (now != fdb->updated)
596 fdb->updated = now;
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900597 if (unlikely(added_by_user))
598 fdb->added_by_user = 1;
Roopa Prabhue3cfddd2017-08-30 22:18:13 -0700599 if (unlikely(fdb_modified)) {
600 trace_br_fdb_update(br, source, addr, vid, added_by_user);
Petr Machata161d82d2018-05-03 14:43:53 +0200601 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
Roopa Prabhue3cfddd2017-08-30 22:18:13 -0700602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604 } else {
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800605 spin_lock(&br->hash_lock);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200606 fdb = fdb_create(br, source, addr, vid, 0, 0);
607 if (fdb) {
608 if (unlikely(added_by_user))
609 fdb->added_by_user = 1;
610 trace_br_fdb_update(br, source, addr, vid,
611 added_by_user);
Petr Machata161d82d2018-05-03 14:43:53 +0200612 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
stephen hemmingerf58ee4e2011-12-06 13:02:24 +0000613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* else we lose race and someone else inserts
615 * it first, don't bother updating
616 */
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800617 spin_unlock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000620
Roopa Prabhu37418732015-10-08 10:38:52 -0700621static int fdb_to_nud(const struct net_bridge *br,
622 const struct net_bridge_fdb_entry *fdb)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000623{
624 if (fdb->is_local)
625 return NUD_PERMANENT;
626 else if (fdb->is_static)
627 return NUD_NOARP;
Roopa Prabhu37418732015-10-08 10:38:52 -0700628 else if (has_expired(br, fdb))
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000629 return NUD_STALE;
630 else
631 return NUD_REACHABLE;
632}
633
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000634static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000635 const struct net_bridge_fdb_entry *fdb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000636 u32 portid, u32 seq, int type, unsigned int flags)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000637{
638 unsigned long now = jiffies;
639 struct nda_cacheinfo ci;
640 struct nlmsghdr *nlh;
641 struct ndmsg *ndm;
642
Eric W. Biederman15e47302012-09-07 20:12:54 +0000643 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000644 if (nlh == NULL)
645 return -EMSGSIZE;
646
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000647 ndm = nlmsg_data(nlh);
648 ndm->ndm_family = AF_BRIDGE;
649 ndm->ndm_pad1 = 0;
650 ndm->ndm_pad2 = 0;
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200651 ndm->ndm_flags = 0;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000652 ndm->ndm_type = 0;
stephen hemminger43598812011-12-08 07:17:49 +0000653 ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
Roopa Prabhu37418732015-10-08 10:38:52 -0700654 ndm->ndm_state = fdb_to_nud(br, fdb);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000655
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200656 if (fdb->offloaded)
657 ndm->ndm_flags |= NTF_OFFLOADED;
658 if (fdb->added_by_external_learn)
659 ndm->ndm_flags |= NTF_EXT_LEARNED;
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300660 if (fdb->is_sticky)
661 ndm->ndm_flags |= NTF_STICKY;
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200662
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200663 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr))
David S. Miller2eb812e2012-04-01 20:49:54 -0400664 goto nla_put_failure;
Roopa Prabhu41c389d2014-05-27 22:39:37 -0700665 if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
666 goto nla_put_failure;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000667 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
668 ci.ndm_confirmed = 0;
669 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
670 ci.ndm_refcnt = 0;
David S. Miller2eb812e2012-04-01 20:49:54 -0400671 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
672 goto nla_put_failure;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000673
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200674 if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16),
675 &fdb->key.vlan_id))
Vlad Yasevich1690be62013-02-13 12:00:18 +0000676 goto nla_put_failure;
677
Johannes Berg053c0952015-01-16 22:09:00 +0100678 nlmsg_end(skb, nlh);
679 return 0;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000680
681nla_put_failure:
682 nlmsg_cancel(skb, nlh);
683 return -EMSGSIZE;
684}
685
686static inline size_t fdb_nlmsg_size(void)
687{
688 return NLMSG_ALIGN(sizeof(struct ndmsg))
689 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Roopa Prabhu41c389d2014-05-27 22:39:37 -0700690 + nla_total_size(sizeof(u32)) /* NDA_MASTER */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000691 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000692 + nla_total_size(sizeof(struct nda_cacheinfo));
693}
694
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000695static void fdb_notify(struct net_bridge *br,
Petr Machata161d82d2018-05-03 14:43:53 +0200696 const struct net_bridge_fdb_entry *fdb, int type,
697 bool swdev_notify)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000698{
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000699 struct net *net = dev_net(br->dev);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000700 struct sk_buff *skb;
701 int err = -ENOBUFS;
702
Petr Machata161d82d2018-05-03 14:43:53 +0200703 if (swdev_notify)
704 br_switchdev_fdb_notify(fdb, type);
Arkadi Sharshevsky6b26b512017-06-08 08:44:14 +0200705
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000706 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
707 if (skb == NULL)
708 goto errout;
709
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000710 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000711 if (err < 0) {
712 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
713 WARN_ON(err == -EMSGSIZE);
714 kfree_skb(skb);
715 goto errout;
716 }
717 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
718 return;
719errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800720 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000721}
722
723/* Dump information about entries, in response to GETNEIGH */
John Fastabend77162022012-04-15 06:43:56 +0000724int br_fdb_dump(struct sk_buff *skb,
725 struct netlink_callback *cb,
726 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400727 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -0700728 int *idx)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000729{
John Fastabend77162022012-04-15 06:43:56 +0000730 struct net_bridge *br = netdev_priv(dev);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200731 struct net_bridge_fdb_entry *f;
Roopa Prabhud2976532016-08-30 21:56:45 -0700732 int err = 0;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000733
John Fastabend77162022012-04-15 06:43:56 +0000734 if (!(dev->priv_flags & IFF_EBRIDGE))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200735 return err;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000736
Roopa Prabhud2976532016-08-30 21:56:45 -0700737 if (!filter_dev) {
738 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
739 if (err < 0)
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200740 return err;
Roopa Prabhud2976532016-08-30 21:56:45 -0700741 }
Hubert Sokolowski6cb69742015-01-05 17:29:21 +0000742
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200743 rcu_read_lock();
744 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
745 if (*idx < cb->args[2])
746 goto skip;
747 if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) {
748 if (filter_dev != dev)
John Fastabend77162022012-04-15 06:43:56 +0000749 goto skip;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200750 /* !f->dst is a special case for bridge
751 * It means the MAC belongs to the bridge
752 * Therefore need a little more filtering
753 * we only want to dump the !f->dst case
754 */
755 if (f->dst)
Hubert Sokolowski6cb69742015-01-05 17:29:21 +0000756 goto skip;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000757 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200758 if (!filter_dev && f->dst)
759 goto skip;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000760
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200761 err = fdb_fill_info(skb, br, f,
762 NETLINK_CB(cb->skb).portid,
763 cb->nlh->nlmsg_seq,
764 RTM_NEWNEIGH,
765 NLM_F_MULTI);
766 if (err < 0)
767 break;
768skip:
769 *idx += 1;
770 }
771 rcu_read_unlock();
772
Roopa Prabhud2976532016-08-30 21:56:45 -0700773 return err;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000774}
stephen hemminger36fd2b62011-04-04 14:03:31 +0000775
stephen hemminger292d1392011-11-09 18:30:08 +0000776/* Update (create or replace) forwarding database entry */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900777static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300778 const u8 *addr, u16 state, u16 flags, u16 vid,
779 u8 ndm_flags)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000780{
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300781 u8 is_sticky = !!(ndm_flags & NTF_STICKY);
stephen hemminger36fd2b62011-04-04 14:03:31 +0000782 struct net_bridge_fdb_entry *fdb;
roopab0a397f2013-04-22 12:56:49 +0000783 bool modified = false;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000784
Wilson Kokeb8d7ba2015-05-25 06:39:31 -0700785 /* If the port cannot learn allow only local and static entries */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900786 if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
Wilson Kokeb8d7ba2015-05-25 06:39:31 -0700787 !(source->state == BR_STATE_LEARNING ||
788 source->state == BR_STATE_FORWARDING))
789 return -EPERM;
790
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900791 if (!source && !(state & NUD_PERMANENT)) {
792 pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
793 br->dev->name);
794 return -EINVAL;
795 }
796
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300797 if (is_sticky && (state & NUD_PERMANENT))
798 return -EINVAL;
799
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100800 fdb = br_fdb_find(br, addr, vid);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000801 if (fdb == NULL) {
802 if (!(flags & NLM_F_CREATE))
803 return -ENOENT;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000804
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200805 fdb = fdb_create(br, source, addr, vid, 0, 0);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000806 if (!fdb)
807 return -ENOMEM;
roopab0a397f2013-04-22 12:56:49 +0000808
809 modified = true;
stephen hemminger64af1ba2011-09-30 14:37:27 +0000810 } else {
811 if (flags & NLM_F_EXCL)
812 return -EEXIST;
roopab0a397f2013-04-22 12:56:49 +0000813
814 if (fdb->dst != source) {
815 fdb->dst = source;
816 modified = true;
817 }
stephen hemminger64af1ba2011-09-30 14:37:27 +0000818 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000819
Roopa Prabhu37418732015-10-08 10:38:52 -0700820 if (fdb_to_nud(br, fdb) != state) {
Vlad Yasevich145beee2014-05-16 09:59:19 -0400821 if (state & NUD_PERMANENT) {
822 fdb->is_local = 1;
823 if (!fdb->is_static) {
824 fdb->is_static = 1;
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100825 fdb_add_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400826 }
827 } else if (state & NUD_NOARP) {
stephen hemminger292d1392011-11-09 18:30:08 +0000828 fdb->is_local = 0;
Vlad Yasevich145beee2014-05-16 09:59:19 -0400829 if (!fdb->is_static) {
830 fdb->is_static = 1;
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100831 fdb_add_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400832 }
833 } else {
834 fdb->is_local = 0;
835 if (fdb->is_static) {
836 fdb->is_static = 0;
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100837 fdb_del_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400838 }
839 }
stephen hemminger292d1392011-11-09 18:30:08 +0000840
roopab0a397f2013-04-22 12:56:49 +0000841 modified = true;
842 }
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300843
844 if (is_sticky != fdb->is_sticky) {
845 fdb->is_sticky = is_sticky;
846 modified = true;
847 }
848
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900849 fdb->added_by_user = 1;
roopab0a397f2013-04-22 12:56:49 +0000850
851 fdb->used = jiffies;
852 if (modified) {
853 fdb->updated = jiffies;
Petr Machata161d82d2018-05-03 14:43:53 +0200854 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
stephen hemminger292d1392011-11-09 18:30:08 +0000855 }
856
stephen hemminger36fd2b62011-04-04 14:03:31 +0000857 return 0;
858}
859
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900860static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br,
861 struct net_bridge_port *p, const unsigned char *addr,
862 u16 nlh_flags, u16 vid)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000863{
864 int err = 0;
865
866 if (ndm->ndm_flags & NTF_USE) {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900867 if (!p) {
868 pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
869 br->dev->name);
870 return -EINVAL;
871 }
Nikolay Aleksandrovc4c832f2015-06-06 06:49:00 -0700872 local_bh_disable();
Vlad Yasevich1690be62013-02-13 12:00:18 +0000873 rcu_read_lock();
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900874 br_fdb_update(br, p, addr, vid, true);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000875 rcu_read_unlock();
Nikolay Aleksandrovc4c832f2015-06-06 06:49:00 -0700876 local_bh_enable();
Nikolay Aleksandroveb100e02017-03-23 12:27:13 +0200877 } else if (ndm->ndm_flags & NTF_EXT_LEARNED) {
Petr Machata161d82d2018-05-03 14:43:53 +0200878 err = br_fdb_external_learn_add(br, p, addr, vid, true);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000879 } else {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900880 spin_lock_bh(&br->hash_lock);
881 err = fdb_add_entry(br, p, addr, ndm->ndm_state,
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300882 nlh_flags, vid, ndm->ndm_flags);
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900883 spin_unlock_bh(&br->hash_lock);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000884 }
885
886 return err;
887}
888
stephen hemminger36fd2b62011-04-04 14:03:31 +0000889/* Add new permanent fdb entry with RTM_NEWNEIGH */
stephen hemmingeredc7d572012-10-01 12:32:33 +0000890int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
891 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100892 const unsigned char *addr, u16 vid, u16 nlh_flags)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000893{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200894 struct net_bridge_vlan_group *vg;
Roopa Prabhu37418732015-10-08 10:38:52 -0700895 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200896 struct net_bridge_vlan *v;
Roopa Prabhu37418732015-10-08 10:38:52 -0700897 struct net_bridge *br = NULL;
John Fastabend77162022012-04-15 06:43:56 +0000898 int err = 0;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000899
Roopa Prabhub74fd302017-08-29 13:16:57 -0700900 trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);
901
stephen hemminger292d1392011-11-09 18:30:08 +0000902 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
903 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
904 return -EINVAL;
905 }
906
Stephen Hemminger537f7f82013-06-25 09:34:36 -0700907 if (is_zero_ether_addr(addr)) {
908 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
909 return -EINVAL;
910 }
911
Roopa Prabhu37418732015-10-08 10:38:52 -0700912 if (dev->priv_flags & IFF_EBRIDGE) {
913 br = netdev_priv(dev);
914 vg = br_vlan_group(br);
915 } else {
916 p = br_port_get_rtnl(dev);
917 if (!p) {
918 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
919 dev->name);
920 return -EINVAL;
921 }
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900922 br = p->br;
Roopa Prabhu37418732015-10-08 10:38:52 -0700923 vg = nbp_vlan_group(p);
stephen hemminger36fd2b62011-04-04 14:03:31 +0000924 }
925
Jiri Pirkof6f64242014-11-28 14:34:15 +0100926 if (vid) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200927 v = br_vlan_find(vg, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700928 if (!v || !br_vlan_should_use(v)) {
929 pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000930 return -EINVAL;
931 }
932
933 /* VID was specified, so use it. */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900934 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid);
stephen hemminger292d1392011-11-09 18:30:08 +0000935 } else {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900936 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200937 if (err || !vg || !vg->num_vlans)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000938 goto out;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000939
940 /* We have vlans configured on this port and user didn't
941 * specify a VLAN. To be nice, add/update entry for every
942 * vlan on this port.
943 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200944 list_for_each_entry(v, &vg->vlan_list, vlist) {
Roopa Prabhu37418732015-10-08 10:38:52 -0700945 if (!br_vlan_should_use(v))
946 continue;
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900947 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000948 if (err)
949 goto out;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000950 }
stephen hemminger292d1392011-11-09 18:30:08 +0000951 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000952
Vlad Yasevich1690be62013-02-13 12:00:18 +0000953out:
954 return err;
955}
956
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +0100957static int fdb_delete_by_addr_and_port(struct net_bridge *br,
958 const struct net_bridge_port *p,
Nikolay Aleksandrov8c86f962015-06-09 03:34:13 -0700959 const u8 *addr, u16 vlan)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000960{
Vlad Yasevich1690be62013-02-13 12:00:18 +0000961 struct net_bridge_fdb_entry *fdb;
962
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100963 fdb = br_fdb_find(br, addr, vlan);
Nikolay Aleksandrov8c86f962015-06-09 03:34:13 -0700964 if (!fdb || fdb->dst != p)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000965 return -ENOENT;
966
Petr Machata161d82d2018-05-03 14:43:53 +0200967 fdb_delete(br, fdb, true);
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +0100968
Vlad Yasevich1690be62013-02-13 12:00:18 +0000969 return 0;
970}
971
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +0100972static int __br_fdb_delete(struct net_bridge *br,
973 const struct net_bridge_port *p,
Vlad Yasevich1690be62013-02-13 12:00:18 +0000974 const unsigned char *addr, u16 vid)
975{
976 int err;
977
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +0100978 spin_lock_bh(&br->hash_lock);
979 err = fdb_delete_by_addr_and_port(br, p, addr, vid);
980 spin_unlock_bh(&br->hash_lock);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000981
stephen hemminger36fd2b62011-04-04 14:03:31 +0000982 return err;
983}
984
stephen hemminger36fd2b62011-04-04 14:03:31 +0000985/* Remove neighbor entry with RTM_DELNEIGH */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000986int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
987 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100988 const unsigned char *addr, u16 vid)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000989{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200990 struct net_bridge_vlan_group *vg;
Roopa Prabhu37418732015-10-08 10:38:52 -0700991 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200992 struct net_bridge_vlan *v;
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +0100993 struct net_bridge *br;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000994 int err;
995
Roopa Prabhu37418732015-10-08 10:38:52 -0700996 if (dev->priv_flags & IFF_EBRIDGE) {
997 br = netdev_priv(dev);
998 vg = br_vlan_group(br);
999 } else {
1000 p = br_port_get_rtnl(dev);
1001 if (!p) {
1002 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
1003 dev->name);
1004 return -EINVAL;
1005 }
1006 vg = nbp_vlan_group(p);
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001007 br = p->br;
stephen hemminger36fd2b62011-04-04 14:03:31 +00001008 }
1009
Jiri Pirkof6f64242014-11-28 14:34:15 +01001010 if (vid) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001011 v = br_vlan_find(vg, vid);
1012 if (!v) {
Roopa Prabhu37418732015-10-08 10:38:52 -07001013 pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001014 return -EINVAL;
1015 }
stephen hemminger36fd2b62011-04-04 14:03:31 +00001016
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001017 err = __br_fdb_delete(br, p, addr, vid);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001018 } else {
Toshiaki Makita25d3b492015-02-09 20:16:17 +09001019 err = -ENOENT;
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001020 err &= __br_fdb_delete(br, p, addr, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001021 if (!vg || !vg->num_vlans)
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001022 return err;
Vlad Yasevich1690be62013-02-13 12:00:18 +00001023
Roopa Prabhu37418732015-10-08 10:38:52 -07001024 list_for_each_entry(v, &vg->vlan_list, vlist) {
1025 if (!br_vlan_should_use(v))
1026 continue;
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001027 err &= __br_fdb_delete(br, p, addr, v->vid);
Roopa Prabhu37418732015-10-08 10:38:52 -07001028 }
Vlad Yasevich1690be62013-02-13 12:00:18 +00001029 }
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001030
stephen hemminger36fd2b62011-04-04 14:03:31 +00001031 return err;
1032}
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001033
1034int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
1035{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001036 struct net_bridge_fdb_entry *f, *tmp;
Geert Uytterhoeven367dc652018-02-01 11:25:27 +01001037 int err = 0;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001038
1039 ASSERT_RTNL();
1040
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001041 /* the key here is that static entries change only under rtnl */
1042 rcu_read_lock();
1043 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1044 /* We only care for static entries */
1045 if (!f->is_static)
1046 continue;
1047 err = dev_uc_add(p->dev, f->key.addr.addr);
1048 if (err)
1049 goto rollback;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001050 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001051done:
1052 rcu_read_unlock();
1053
1054 return err;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001055
1056rollback:
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001057 hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
1058 /* We only care for static entries */
1059 if (!tmp->is_static)
1060 continue;
1061 if (tmp == f)
1062 break;
1063 dev_uc_del(p->dev, tmp->key.addr.addr);
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001064 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001065
1066 goto done;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001067}
1068
1069void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
1070{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001071 struct net_bridge_fdb_entry *f;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001072
1073 ASSERT_RTNL();
1074
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001075 rcu_read_lock();
1076 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1077 /* We only care for static entries */
1078 if (!f->is_static)
1079 continue;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001080
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001081 dev_uc_del(p->dev, f->key.addr.addr);
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001082 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001083 rcu_read_unlock();
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001084}
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001085
Jiri Pirko3aeb6612015-01-15 23:49:37 +01001086int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
Petr Machata161d82d2018-05-03 14:43:53 +02001087 const unsigned char *addr, u16 vid,
1088 bool swdev_notify)
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001089{
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001090 struct net_bridge_fdb_entry *fdb;
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001091 bool modified = false;
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001092 int err = 0;
1093
Roopa Prabhub74fd302017-08-29 13:16:57 -07001094 trace_br_fdb_external_learn_add(br, p, addr, vid);
1095
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001096 spin_lock_bh(&br->hash_lock);
1097
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +01001098 fdb = br_fdb_find(br, addr, vid);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001099 if (!fdb) {
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001100 fdb = fdb_create(br, p, addr, vid, 0, 0);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001101 if (!fdb) {
1102 err = -ENOMEM;
1103 goto err_unlock;
1104 }
1105 fdb->added_by_external_learn = 1;
Petr Machata161d82d2018-05-03 14:43:53 +02001106 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001107 } else {
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001108 fdb->updated = jiffies;
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001109
1110 if (fdb->dst != p) {
1111 fdb->dst = p;
1112 modified = true;
1113 }
1114
1115 if (fdb->added_by_external_learn) {
1116 /* Refresh entry */
1117 fdb->used = jiffies;
1118 } else if (!fdb->added_by_user) {
1119 /* Take over SW learned entry */
1120 fdb->added_by_external_learn = 1;
1121 modified = true;
1122 }
1123
1124 if (modified)
Petr Machata161d82d2018-05-03 14:43:53 +02001125 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001126 }
1127
1128err_unlock:
1129 spin_unlock_bh(&br->hash_lock);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001130
1131 return err;
1132}
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001133
Jiri Pirko3aeb6612015-01-15 23:49:37 +01001134int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
Petr Machata161d82d2018-05-03 14:43:53 +02001135 const unsigned char *addr, u16 vid,
1136 bool swdev_notify)
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001137{
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001138 struct net_bridge_fdb_entry *fdb;
1139 int err = 0;
1140
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001141 spin_lock_bh(&br->hash_lock);
1142
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +01001143 fdb = br_fdb_find(br, addr, vid);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001144 if (fdb && fdb->added_by_external_learn)
Petr Machata161d82d2018-05-03 14:43:53 +02001145 fdb_delete(br, fdb, swdev_notify);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001146 else
1147 err = -ENOENT;
1148
1149 spin_unlock_bh(&br->hash_lock);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001150
1151 return err;
1152}
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +02001153
1154void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
Ido Schimmele9ba0fb2018-10-17 08:53:29 +00001155 const unsigned char *addr, u16 vid, bool offloaded)
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +02001156{
1157 struct net_bridge_fdb_entry *fdb;
1158
1159 spin_lock_bh(&br->hash_lock);
1160
1161 fdb = br_fdb_find(br, addr, vid);
1162 if (fdb)
Ido Schimmele9ba0fb2018-10-17 08:53:29 +00001163 fdb->offloaded = offloaded;
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +02001164
1165 spin_unlock_bh(&br->hash_lock);
1166}