blob: b1c91f66d79c4e3e1beca4d091a7db7b5ffe3b34 [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,
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020036};
37
Christoph Lametere18b8902006-12-06 20:33:20 -080038static struct kmem_cache *br_fdb_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000040 const unsigned char *addr, u16 vid);
stephen hemminger31e8a49c2011-12-08 07:17:41 +000041static void fdb_notify(struct net_bridge *br,
Petr Machata161d82d2018-05-03 14:43:53 +020042 const struct net_bridge_fdb_entry *, int, bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Akinobu Mita87a596e2007-04-07 18:57:07 +090044int __init br_fdb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
47 sizeof(struct net_bridge_fdb_entry),
48 0,
Paul Mundt20c2df82007-07-20 10:11:58 +090049 SLAB_HWCACHE_ALIGN, NULL);
Akinobu Mita87a596e2007-04-07 18:57:07 +090050 if (!br_fdb_cache)
51 return -ENOMEM;
52
Akinobu Mita87a596e2007-04-07 18:57:07 +090053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Andrew Morton73afc902007-12-05 21:35:23 -080056void br_fdb_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 kmem_cache_destroy(br_fdb_cache);
59}
60
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020061int br_fdb_hash_init(struct net_bridge *br)
62{
63 return rhashtable_init(&br->fdb_hash_tbl, &br_fdb_rht_params);
64}
65
66void br_fdb_hash_fini(struct net_bridge *br)
67{
68 rhashtable_destroy(&br->fdb_hash_tbl);
69}
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/* if topology_changing then use forward_delay (default 15 sec)
72 * otherwise keep longer (default 5 minutes)
73 */
Stephen Hemminger3f890922007-03-21 13:42:33 -070074static inline unsigned long hold_time(const struct net_bridge *br)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 return br->topology_change ? br->forward_delay : br->ageing_time;
77}
78
Stephen Hemminger3f890922007-03-21 13:42:33 -070079static inline int has_expired(const struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 const struct net_bridge_fdb_entry *fdb)
81{
Roopa Prabhueda7a5e2017-02-16 13:38:04 -080082 return !fdb->is_static && !fdb->added_by_external_learn &&
stephen hemminger7cd88612011-04-04 14:03:28 +000083 time_before_eq(fdb->updated + hold_time(br), jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Michał Mirosławda678292009-06-05 05:35:28 +000086static void fdb_rcu_free(struct rcu_head *head)
87{
88 struct net_bridge_fdb_entry *ent
89 = container_of(head, struct net_bridge_fdb_entry, rcu);
90 kmem_cache_free(br_fdb_cache, ent);
91}
92
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020093static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl,
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +010094 const unsigned char *addr,
95 __u16 vid)
96{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020097 struct net_bridge_fdb_key key;
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +010098
Nikolay Aleksandrov410b3d42017-02-13 14:59:10 +010099 WARN_ON_ONCE(!rcu_read_lock_held());
100
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200101 key.vlan_id = vid;
102 memcpy(key.addr.addr, addr, sizeof(key.addr.addr));
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100103
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200104 return rhashtable_lookup(tbl, &key, br_fdb_rht_params);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100105}
106
107/* requires bridge hash_lock */
108static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br,
109 const unsigned char *addr,
110 __u16 vid)
111{
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100112 struct net_bridge_fdb_entry *fdb;
113
WANG Congd12c9172017-03-16 10:32:42 -0700114 lockdep_assert_held_once(&br->hash_lock);
Nikolay Aleksandrov410b3d42017-02-13 14:59:10 +0100115
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100116 rcu_read_lock();
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200117 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100118 rcu_read_unlock();
119
120 return fdb;
121}
122
Petr Machata4d4fd362018-04-29 10:56:08 +0300123struct net_device *br_fdb_find_port(const struct net_device *br_dev,
124 const unsigned char *addr,
125 __u16 vid)
126{
127 struct net_bridge_fdb_entry *f;
128 struct net_device *dev = NULL;
129 struct net_bridge *br;
130
131 ASSERT_RTNL();
132
133 if (!netif_is_bridge_master(br_dev))
134 return NULL;
135
136 br = netdev_priv(br_dev);
Petr Machata873aca22018-06-08 15:11:47 +0200137 rcu_read_lock();
138 f = br_fdb_find_rcu(br, addr, vid);
Petr Machata4d4fd362018-04-29 10:56:08 +0300139 if (f && f->dst)
140 dev = f->dst->dev;
Petr Machata873aca22018-06-08 15:11:47 +0200141 rcu_read_unlock();
Petr Machata4d4fd362018-04-29 10:56:08 +0300142
143 return dev;
144}
145EXPORT_SYMBOL_GPL(br_fdb_find_port);
146
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100147struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
148 const unsigned char *addr,
149 __u16 vid)
150{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200151 return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100152}
153
Vlad Yasevich145beee2014-05-16 09:59:19 -0400154/* When a static FDB entry is added, the mac address from the entry is
155 * added to the bridge private HW address list and all required ports
156 * are then updated with the new information.
157 * Called under RTNL.
158 */
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100159static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr)
Vlad Yasevich145beee2014-05-16 09:59:19 -0400160{
161 int err;
Li RongQinga3f5ee72014-06-18 16:07:16 +0800162 struct net_bridge_port *p;
Vlad Yasevich145beee2014-05-16 09:59:19 -0400163
164 ASSERT_RTNL();
165
166 list_for_each_entry(p, &br->port_list, list) {
167 if (!br_promisc_port(p)) {
168 err = dev_uc_add(p->dev, addr);
169 if (err)
170 goto undo;
171 }
172 }
173
174 return;
175undo:
Li RongQinga3f5ee72014-06-18 16:07:16 +0800176 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
177 if (!br_promisc_port(p))
178 dev_uc_del(p->dev, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400179 }
180}
181
182/* When a static FDB entry is deleted, the HW address from that entry is
183 * also removed from the bridge private HW address list and updates all
184 * the ports with needed information.
185 * Called under RTNL.
186 */
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100187static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
Vlad Yasevich145beee2014-05-16 09:59:19 -0400188{
189 struct net_bridge_port *p;
190
191 ASSERT_RTNL();
192
193 list_for_each_entry(p, &br->port_list, list) {
194 if (!br_promisc_port(p))
195 dev_uc_del(p->dev, addr);
196 }
197}
198
Petr Machata161d82d2018-05-03 14:43:53 +0200199static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
200 bool swdev_notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Roopa Prabhub74fd302017-08-29 13:16:57 -0700202 trace_fdb_delete(br, f);
203
Vlad Yasevich145beee2014-05-16 09:59:19 -0400204 if (f->is_static)
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200205 fdb_del_hw_addr(br, f->key.addr.addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400206
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200207 hlist_del_init_rcu(&f->fdb_node);
208 rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode,
209 br_fdb_rht_params);
Petr Machata161d82d2018-05-03 14:43:53 +0200210 fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
Michał Mirosławda678292009-06-05 05:35:28 +0000211 call_rcu(&f->rcu, fdb_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Toshiaki Makita960b5892014-02-07 16:48:23 +0900214/* Delete a local entry if no other port had the same address. */
215static void fdb_delete_local(struct net_bridge *br,
216 const struct net_bridge_port *p,
217 struct net_bridge_fdb_entry *f)
218{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200219 const unsigned char *addr = f->key.addr.addr;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200220 struct net_bridge_vlan_group *vg;
221 const struct net_bridge_vlan *v;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900222 struct net_bridge_port *op;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200223 u16 vid = f->key.vlan_id;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900224
225 /* Maybe another port has same hw addr? */
226 list_for_each_entry(op, &br->port_list, list) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200227 vg = nbp_vlan_group(op);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900228 if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200229 (!vid || br_vlan_find(vg, vid))) {
Toshiaki Makita960b5892014-02-07 16:48:23 +0900230 f->dst = op;
Toshiaki Makitaa778e6d2014-02-07 16:48:24 +0900231 f->added_by_user = 0;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900232 return;
233 }
234 }
235
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200236 vg = br_vlan_group(br);
237 v = br_vlan_find(vg, vid);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900238 /* Maybe bridge device has same hw addr? */
239 if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200240 (!vid || (v && br_vlan_should_use(v)))) {
Toshiaki Makita960b5892014-02-07 16:48:23 +0900241 f->dst = NULL;
Toshiaki Makitaa778e6d2014-02-07 16:48:24 +0900242 f->added_by_user = 0;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900243 return;
244 }
245
Petr Machata161d82d2018-05-03 14:43:53 +0200246 fdb_delete(br, f, true);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900247}
248
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900249void br_fdb_find_delete_local(struct net_bridge *br,
250 const struct net_bridge_port *p,
251 const unsigned char *addr, u16 vid)
252{
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900253 struct net_bridge_fdb_entry *f;
254
255 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100256 f = br_fdb_find(br, addr, vid);
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900257 if (f && f->is_local && !f->added_by_user && f->dst == p)
258 fdb_delete_local(br, p, f);
259 spin_unlock_bh(&br->hash_lock);
260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
263{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200264 struct net_bridge_vlan_group *vg;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200265 struct net_bridge_fdb_entry *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct net_bridge *br = p->br;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200267 struct net_bridge_vlan *v;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200270 vg = nbp_vlan_group(p);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200271 hlist_for_each_entry(f, &br->fdb_list, fdb_node) {
272 if (f->dst == p && f->is_local && !f->added_by_user) {
273 /* delete old one */
274 fdb_delete_local(br, p, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200276 /* if this port has no vlan information
277 * configured, we can safely be done at
278 * this point.
279 */
280 if (!vg || !vg->num_vlans)
281 goto insert;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Toshiaki Makita28368822014-02-07 16:48:19 +0900285insert:
286 /* insert new address, may fail if invalid address or dup. */
287 fdb_insert(br, p, newaddr, 0);
288
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200289 if (!vg || !vg->num_vlans)
Toshiaki Makita28368822014-02-07 16:48:19 +0900290 goto done;
291
292 /* Now add entries for every VLAN configured on the port.
293 * This function runs under RTNL so the bitmap will not change
294 * from under us.
295 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200296 list_for_each_entry(v, &vg->vlan_list, vlist)
297 fdb_insert(br, p, newaddr, v->vid);
Toshiaki Makita28368822014-02-07 16:48:19 +0900298
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000299done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 spin_unlock_bh(&br->hash_lock);
301}
302
stephen hemminger43598812011-12-08 07:17:49 +0000303void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
304{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200305 struct net_bridge_vlan_group *vg;
stephen hemminger43598812011-12-08 07:17:49 +0000306 struct net_bridge_fdb_entry *f;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200307 struct net_bridge_vlan *v;
stephen hemminger43598812011-12-08 07:17:49 +0000308
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900309 spin_lock_bh(&br->hash_lock);
310
stephen hemminger43598812011-12-08 07:17:49 +0000311 /* If old entry was unassociated with any port, then delete it. */
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100312 f = br_fdb_find(br, br->dev->dev_addr, 0);
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900313 if (f && f->is_local && !f->dst && !f->added_by_user)
Toshiaki Makita960b5892014-02-07 16:48:23 +0900314 fdb_delete_local(br, NULL, f);
stephen hemminger43598812011-12-08 07:17:49 +0000315
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000316 fdb_insert(br, NULL, newaddr, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200317 vg = br_vlan_group(br);
318 if (!vg || !vg->num_vlans)
319 goto out;
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000320 /* Now remove and add entries for every VLAN configured on the
321 * bridge. This function runs under RTNL so the bitmap will not
322 * change from under us.
323 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200324 list_for_each_entry(v, &vg->vlan_list, vlist) {
Toshiaki Makita0b148de2016-06-07 19:14:17 +0900325 if (!br_vlan_should_use(v))
326 continue;
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100327 f = br_fdb_find(br, br->dev->dev_addr, v->vid);
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900328 if (f && f->is_local && !f->dst && !f->added_by_user)
Toshiaki Makita960b5892014-02-07 16:48:23 +0900329 fdb_delete_local(br, NULL, f);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200330 fdb_insert(br, NULL, newaddr, v->vid);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000331 }
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900332out:
333 spin_unlock_bh(&br->hash_lock);
stephen hemminger43598812011-12-08 07:17:49 +0000334}
335
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100336void br_fdb_cleanup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100338 struct net_bridge *br = container_of(work, struct net_bridge,
339 gc_work.work);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200340 struct net_bridge_fdb_entry *f = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 unsigned long delay = hold_time(br);
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100342 unsigned long work_delay = delay;
343 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200345 /* this part is tricky, in order to avoid blocking learning and
346 * consequently forwarding, we rely on rcu to delete objects with
347 * delayed freeing allowing us to continue traversing
348 */
349 rcu_read_lock();
350 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
351 unsigned long this_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200353 if (f->is_static || f->added_by_external_learn)
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100354 continue;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200355 this_timer = f->updated + delay;
356 if (time_after(this_timer, now)) {
357 work_delay = min(work_delay, this_timer - now);
358 } else {
359 spin_lock_bh(&br->hash_lock);
360 if (!hlist_unhashed(&f->fdb_node))
Petr Machata161d82d2018-05-03 14:43:53 +0200361 fdb_delete(br, f, true);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200362 spin_unlock_bh(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200365 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100367 /* Cleanup minimum 10 milliseconds apart */
368 work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10));
369 mod_delayed_work(system_long_wq, &br->gc_work, work_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700372/* Completely flush all dynamic entries in forwarding database.*/
373void br_fdb_flush(struct net_bridge *br)
374{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200375 struct net_bridge_fdb_entry *f;
376 struct hlist_node *tmp;
Stephen Hemminger1a620692006-10-12 14:45:38 -0700377
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700378 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200379 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
380 if (!f->is_static)
Petr Machata161d82d2018-05-03 14:43:53 +0200381 fdb_delete(br, f, true);
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700382 }
383 spin_unlock_bh(&br->hash_lock);
384}
385
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300386/* Flush all entries referring to a specific port.
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700387 * if do_all is set also flush static entries
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700388 * if vid is set delete all entries that match the vlan_id
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700389 */
Stephen Hemminger1a620692006-10-12 14:45:38 -0700390void br_fdb_delete_by_port(struct net_bridge *br,
391 const struct net_bridge_port *p,
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700392 u16 vid,
Stephen Hemminger1a620692006-10-12 14:45:38 -0700393 int do_all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200395 struct net_bridge_fdb_entry *f;
396 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200399 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
400 if (f->dst != p)
401 continue;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900402
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200403 if (!do_all)
404 if (f->is_static || (vid && f->key.vlan_id != vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 continue;
406
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200407 if (f->is_local)
408 fdb_delete_local(br, p, f);
409 else
Petr Machata161d82d2018-05-03 14:43:53 +0200410 fdb_delete(br, f, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 spin_unlock_bh(&br->hash_lock);
413}
414
Igor Maraviće6373c42011-12-12 02:58:25 +0000415#if IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +0000416/* Interface used by ATM LANE hook to test
417 * if an addr is on some other bridge port */
418int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 struct net_bridge_fdb_entry *fdb;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000421 struct net_bridge_port *port;
Michał Mirosławda678292009-06-05 05:35:28 +0000422 int ret;
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 rcu_read_lock();
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000425 port = br_port_get_rcu(dev);
426 if (!port)
427 ret = 0;
428 else {
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100429 fdb = br_fdb_find_rcu(port->br, addr, 0);
stephen hemminger43598812011-12-08 07:17:49 +0000430 ret = fdb && fdb->dst && fdb->dst->dev != dev &&
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000431 fdb->dst->state == BR_STATE_FORWARDING;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Michał Mirosławda678292009-06-05 05:35:28 +0000435 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
Michał Mirosławda678292009-06-05 05:35:28 +0000437#endif /* CONFIG_ATM_LANE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439/*
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900440 * Fill buffer with forwarding table records in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * the API format.
442 */
443int br_fdb_fillbuf(struct net_bridge *br, void *buf,
444 unsigned long maxnum, unsigned long skip)
445{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct net_bridge_fdb_entry *f;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200447 struct __fdb_entry *fe = buf;
448 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
451
452 rcu_read_lock();
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200453 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
454 if (num >= maxnum)
455 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200457 if (has_expired(br, f))
458 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200460 /* ignore pseudo entry for local MAC address */
461 if (!f->dst)
462 continue;
stephen hemminger43598812011-12-08 07:17:49 +0000463
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200464 if (skip) {
465 --skip;
466 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200469 /* convert from internal format to API */
470 memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN);
471
472 /* due to ABI compat need to split into hi/lo */
473 fe->port_no = f->dst->port_no;
474 fe->port_hi = f->dst->port_no >> 8;
475
476 fe->is_local = f->is_local;
477 if (!f->is_static)
478 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
479 ++fe;
480 ++num;
481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 rcu_read_unlock();
483
484 return num;
485}
486
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200487static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct net_bridge_port *source,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000489 const unsigned char *addr,
Roopa Prabhub7af1472015-10-27 07:52:56 -0700490 __u16 vid,
491 unsigned char is_local,
492 unsigned char is_static)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 struct net_bridge_fdb_entry *fdb;
495
496 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
497 if (fdb) {
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200498 memcpy(fdb->key.addr.addr, addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 fdb->dst = source;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200500 fdb->key.vlan_id = vid;
Roopa Prabhub7af1472015-10-27 07:52:56 -0700501 fdb->is_local = is_local;
502 fdb->is_static = is_static;
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900503 fdb->added_by_user = 0;
Scott Feldmancf6b8e12014-11-28 14:34:21 +0100504 fdb->added_by_external_learn = 0;
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200505 fdb->offloaded = 0;
Nikolay Aleksandrov1288aa72018-09-27 15:05:10 +0300506 fdb->is_sticky = 0;
stephen hemminger7cd88612011-04-04 14:03:28 +0000507 fdb->updated = fdb->used = jiffies;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200508 if (rhashtable_lookup_insert_fast(&br->fdb_hash_tbl,
509 &fdb->rhnode,
510 br_fdb_rht_params)) {
511 kmem_cache_free(br_fdb_cache, fdb);
512 fdb = NULL;
513 } else {
514 hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list);
515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517 return fdb;
518}
519
520static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000521 const unsigned char *addr, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 struct net_bridge_fdb_entry *fdb;
524
525 if (!is_valid_ether_addr(addr))
526 return -EINVAL;
527
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100528 fdb = br_fdb_find(br, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (fdb) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900530 /* it is okay to have multiple ports with same
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * address, just use the first one.
532 */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900533 if (fdb->is_local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return 0;
Roopa Prabhude1dfee2016-10-11 16:12:56 -0700535 br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
536 source ? source->dev->name : br->dev->name, addr, vid);
Petr Machata161d82d2018-05-03 14:43:53 +0200537 fdb_delete(br, fdb, true);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200540 fdb = fdb_create(br, source, addr, vid, 1, 1);
stephen hemminger03e9b642011-04-04 14:03:27 +0000541 if (!fdb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return -ENOMEM;
543
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100544 fdb_add_hw_addr(br, addr);
Petr Machata161d82d2018-05-03 14:43:53 +0200545 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return 0;
547}
548
stephen hemminger03e9b642011-04-04 14:03:27 +0000549/* Add entry for local address of interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000551 const unsigned char *addr, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 int ret;
554
555 spin_lock_bh(&br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000556 ret = fdb_insert(br, source, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 spin_unlock_bh(&br->hash_lock);
558 return ret;
559}
560
561void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900562 const unsigned char *addr, u16 vid, bool added_by_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 struct net_bridge_fdb_entry *fdb;
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000565 bool fdb_modified = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* some users want to always flood. */
568 if (hold_time(br) == 0)
569 return;
570
Stephen Hemmingerdf1c0b82007-08-30 22:15:35 -0700571 /* ignore packets unless we are using this port */
572 if (!(source->state == BR_STATE_LEARNING ||
573 source->state == BR_STATE_FORWARDING))
574 return;
575
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200576 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (likely(fdb)) {
578 /* attempt to update an entry for a local interface */
579 if (unlikely(fdb->is_local)) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900580 if (net_ratelimit())
Roopa Prabhude1dfee2016-10-11 16:12:56 -0700581 br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
582 source->dev->name, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 } else {
stephen hemmingerca6d4482017-02-07 08:46:46 -0800584 unsigned long now = jiffies;
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /* fastpath: update of existing entry */
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300587 if (unlikely(source != fdb->dst && !fdb->is_sticky)) {
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000588 fdb->dst = source;
589 fdb_modified = true;
Arkadi Sharshevsky58073b32017-04-28 22:39:07 +0300590 /* Take over HW learned entry */
591 if (unlikely(fdb->added_by_external_learn))
592 fdb->added_by_external_learn = 0;
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000593 }
stephen hemmingerca6d4482017-02-07 08:46:46 -0800594 if (now != fdb->updated)
595 fdb->updated = now;
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900596 if (unlikely(added_by_user))
597 fdb->added_by_user = 1;
Roopa Prabhue3cfddd2017-08-30 22:18:13 -0700598 if (unlikely(fdb_modified)) {
599 trace_br_fdb_update(br, source, addr, vid, added_by_user);
Petr Machata161d82d2018-05-03 14:43:53 +0200600 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
Roopa Prabhue3cfddd2017-08-30 22:18:13 -0700601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603 } else {
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800604 spin_lock(&br->hash_lock);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200605 fdb = fdb_create(br, source, addr, vid, 0, 0);
606 if (fdb) {
607 if (unlikely(added_by_user))
608 fdb->added_by_user = 1;
609 trace_br_fdb_update(br, source, addr, vid,
610 added_by_user);
Petr Machata161d82d2018-05-03 14:43:53 +0200611 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
stephen hemmingerf58ee4e2011-12-06 13:02:24 +0000612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* else we lose race and someone else inserts
614 * it first, don't bother updating
615 */
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800616 spin_unlock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000619
Roopa Prabhu37418732015-10-08 10:38:52 -0700620static int fdb_to_nud(const struct net_bridge *br,
621 const struct net_bridge_fdb_entry *fdb)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000622{
623 if (fdb->is_local)
624 return NUD_PERMANENT;
625 else if (fdb->is_static)
626 return NUD_NOARP;
Roopa Prabhu37418732015-10-08 10:38:52 -0700627 else if (has_expired(br, fdb))
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000628 return NUD_STALE;
629 else
630 return NUD_REACHABLE;
631}
632
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000633static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000634 const struct net_bridge_fdb_entry *fdb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000635 u32 portid, u32 seq, int type, unsigned int flags)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000636{
637 unsigned long now = jiffies;
638 struct nda_cacheinfo ci;
639 struct nlmsghdr *nlh;
640 struct ndmsg *ndm;
641
Eric W. Biederman15e47302012-09-07 20:12:54 +0000642 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000643 if (nlh == NULL)
644 return -EMSGSIZE;
645
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000646 ndm = nlmsg_data(nlh);
647 ndm->ndm_family = AF_BRIDGE;
648 ndm->ndm_pad1 = 0;
649 ndm->ndm_pad2 = 0;
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200650 ndm->ndm_flags = 0;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000651 ndm->ndm_type = 0;
stephen hemminger43598812011-12-08 07:17:49 +0000652 ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
Roopa Prabhu37418732015-10-08 10:38:52 -0700653 ndm->ndm_state = fdb_to_nud(br, fdb);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000654
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200655 if (fdb->offloaded)
656 ndm->ndm_flags |= NTF_OFFLOADED;
657 if (fdb->added_by_external_learn)
658 ndm->ndm_flags |= NTF_EXT_LEARNED;
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300659 if (fdb->is_sticky)
660 ndm->ndm_flags |= NTF_STICKY;
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +0200661
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200662 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr))
David S. Miller2eb812e2012-04-01 20:49:54 -0400663 goto nla_put_failure;
Roopa Prabhu41c389d2014-05-27 22:39:37 -0700664 if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
665 goto nla_put_failure;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000666 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
667 ci.ndm_confirmed = 0;
668 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
669 ci.ndm_refcnt = 0;
David S. Miller2eb812e2012-04-01 20:49:54 -0400670 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
671 goto nla_put_failure;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000672
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200673 if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16),
674 &fdb->key.vlan_id))
Vlad Yasevich1690be62013-02-13 12:00:18 +0000675 goto nla_put_failure;
676
Johannes Berg053c0952015-01-16 22:09:00 +0100677 nlmsg_end(skb, nlh);
678 return 0;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000679
680nla_put_failure:
681 nlmsg_cancel(skb, nlh);
682 return -EMSGSIZE;
683}
684
685static inline size_t fdb_nlmsg_size(void)
686{
687 return NLMSG_ALIGN(sizeof(struct ndmsg))
688 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Roopa Prabhu41c389d2014-05-27 22:39:37 -0700689 + nla_total_size(sizeof(u32)) /* NDA_MASTER */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000690 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000691 + nla_total_size(sizeof(struct nda_cacheinfo));
692}
693
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000694static void fdb_notify(struct net_bridge *br,
Petr Machata161d82d2018-05-03 14:43:53 +0200695 const struct net_bridge_fdb_entry *fdb, int type,
696 bool swdev_notify)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000697{
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000698 struct net *net = dev_net(br->dev);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000699 struct sk_buff *skb;
700 int err = -ENOBUFS;
701
Petr Machata161d82d2018-05-03 14:43:53 +0200702 if (swdev_notify)
703 br_switchdev_fdb_notify(fdb, type);
Arkadi Sharshevsky6b26b512017-06-08 08:44:14 +0200704
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000705 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
706 if (skb == NULL)
707 goto errout;
708
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000709 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000710 if (err < 0) {
711 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
712 WARN_ON(err == -EMSGSIZE);
713 kfree_skb(skb);
714 goto errout;
715 }
716 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
717 return;
718errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800719 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000720}
721
722/* Dump information about entries, in response to GETNEIGH */
John Fastabend77162022012-04-15 06:43:56 +0000723int br_fdb_dump(struct sk_buff *skb,
724 struct netlink_callback *cb,
725 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400726 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -0700727 int *idx)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000728{
John Fastabend77162022012-04-15 06:43:56 +0000729 struct net_bridge *br = netdev_priv(dev);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200730 struct net_bridge_fdb_entry *f;
Roopa Prabhud2976532016-08-30 21:56:45 -0700731 int err = 0;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000732
John Fastabend77162022012-04-15 06:43:56 +0000733 if (!(dev->priv_flags & IFF_EBRIDGE))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200734 return err;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000735
Roopa Prabhud2976532016-08-30 21:56:45 -0700736 if (!filter_dev) {
737 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
738 if (err < 0)
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200739 return err;
Roopa Prabhud2976532016-08-30 21:56:45 -0700740 }
Hubert Sokolowski6cb69742015-01-05 17:29:21 +0000741
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200742 rcu_read_lock();
743 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
744 if (*idx < cb->args[2])
745 goto skip;
746 if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) {
747 if (filter_dev != dev)
John Fastabend77162022012-04-15 06:43:56 +0000748 goto skip;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200749 /* !f->dst is a special case for bridge
750 * It means the MAC belongs to the bridge
751 * Therefore need a little more filtering
752 * we only want to dump the !f->dst case
753 */
754 if (f->dst)
Hubert Sokolowski6cb69742015-01-05 17:29:21 +0000755 goto skip;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000756 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200757 if (!filter_dev && f->dst)
758 goto skip;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000759
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200760 err = fdb_fill_info(skb, br, f,
761 NETLINK_CB(cb->skb).portid,
762 cb->nlh->nlmsg_seq,
763 RTM_NEWNEIGH,
764 NLM_F_MULTI);
765 if (err < 0)
766 break;
767skip:
768 *idx += 1;
769 }
770 rcu_read_unlock();
771
Roopa Prabhud2976532016-08-30 21:56:45 -0700772 return err;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000773}
stephen hemminger36fd2b62011-04-04 14:03:31 +0000774
Roopa Prabhu47674562018-12-15 22:35:09 -0800775int br_fdb_get(struct sk_buff *skb,
776 struct nlattr *tb[],
777 struct net_device *dev,
778 const unsigned char *addr,
779 u16 vid, u32 portid, u32 seq,
780 struct netlink_ext_ack *extack)
781{
782 struct net_bridge *br = netdev_priv(dev);
783 struct net_bridge_fdb_entry *f;
784 int err = 0;
785
786 rcu_read_lock();
787 f = br_fdb_find_rcu(br, addr, vid);
788 if (!f) {
789 NL_SET_ERR_MSG(extack, "Fdb entry not found");
790 err = -ENOENT;
791 goto errout;
792 }
793
794 err = fdb_fill_info(skb, br, f, portid, seq,
795 RTM_NEWNEIGH, 0);
796errout:
797 rcu_read_unlock();
798 return err;
799}
800
stephen hemminger292d1392011-11-09 18:30:08 +0000801/* Update (create or replace) forwarding database entry */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900802static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300803 const u8 *addr, u16 state, u16 flags, u16 vid,
804 u8 ndm_flags)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000805{
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300806 u8 is_sticky = !!(ndm_flags & NTF_STICKY);
stephen hemminger36fd2b62011-04-04 14:03:31 +0000807 struct net_bridge_fdb_entry *fdb;
roopab0a397f2013-04-22 12:56:49 +0000808 bool modified = false;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000809
Wilson Kokeb8d7ba2015-05-25 06:39:31 -0700810 /* If the port cannot learn allow only local and static entries */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900811 if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
Wilson Kokeb8d7ba2015-05-25 06:39:31 -0700812 !(source->state == BR_STATE_LEARNING ||
813 source->state == BR_STATE_FORWARDING))
814 return -EPERM;
815
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900816 if (!source && !(state & NUD_PERMANENT)) {
817 pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
818 br->dev->name);
819 return -EINVAL;
820 }
821
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300822 if (is_sticky && (state & NUD_PERMANENT))
823 return -EINVAL;
824
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100825 fdb = br_fdb_find(br, addr, vid);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000826 if (fdb == NULL) {
827 if (!(flags & NLM_F_CREATE))
828 return -ENOENT;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000829
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200830 fdb = fdb_create(br, source, addr, vid, 0, 0);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000831 if (!fdb)
832 return -ENOMEM;
roopab0a397f2013-04-22 12:56:49 +0000833
834 modified = true;
stephen hemminger64af1ba2011-09-30 14:37:27 +0000835 } else {
836 if (flags & NLM_F_EXCL)
837 return -EEXIST;
roopab0a397f2013-04-22 12:56:49 +0000838
839 if (fdb->dst != source) {
840 fdb->dst = source;
841 modified = true;
842 }
stephen hemminger64af1ba2011-09-30 14:37:27 +0000843 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000844
Roopa Prabhu37418732015-10-08 10:38:52 -0700845 if (fdb_to_nud(br, fdb) != state) {
Vlad Yasevich145beee2014-05-16 09:59:19 -0400846 if (state & NUD_PERMANENT) {
847 fdb->is_local = 1;
848 if (!fdb->is_static) {
849 fdb->is_static = 1;
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100850 fdb_add_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400851 }
852 } else if (state & NUD_NOARP) {
stephen hemminger292d1392011-11-09 18:30:08 +0000853 fdb->is_local = 0;
Vlad Yasevich145beee2014-05-16 09:59:19 -0400854 if (!fdb->is_static) {
855 fdb->is_static = 1;
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100856 fdb_add_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400857 }
858 } else {
859 fdb->is_local = 0;
860 if (fdb->is_static) {
861 fdb->is_static = 0;
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100862 fdb_del_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400863 }
864 }
stephen hemminger292d1392011-11-09 18:30:08 +0000865
roopab0a397f2013-04-22 12:56:49 +0000866 modified = true;
867 }
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300868
869 if (is_sticky != fdb->is_sticky) {
870 fdb->is_sticky = is_sticky;
871 modified = true;
872 }
873
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900874 fdb->added_by_user = 1;
roopab0a397f2013-04-22 12:56:49 +0000875
876 fdb->used = jiffies;
877 if (modified) {
878 fdb->updated = jiffies;
Petr Machata161d82d2018-05-03 14:43:53 +0200879 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
stephen hemminger292d1392011-11-09 18:30:08 +0000880 }
881
stephen hemminger36fd2b62011-04-04 14:03:31 +0000882 return 0;
883}
884
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900885static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br,
886 struct net_bridge_port *p, const unsigned char *addr,
887 u16 nlh_flags, u16 vid)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000888{
889 int err = 0;
890
891 if (ndm->ndm_flags & NTF_USE) {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900892 if (!p) {
893 pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
894 br->dev->name);
895 return -EINVAL;
896 }
Nikolay Aleksandrovc4c832f2015-06-06 06:49:00 -0700897 local_bh_disable();
Vlad Yasevich1690be62013-02-13 12:00:18 +0000898 rcu_read_lock();
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900899 br_fdb_update(br, p, addr, vid, true);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000900 rcu_read_unlock();
Nikolay Aleksandrovc4c832f2015-06-06 06:49:00 -0700901 local_bh_enable();
Nikolay Aleksandroveb100e02017-03-23 12:27:13 +0200902 } else if (ndm->ndm_flags & NTF_EXT_LEARNED) {
Petr Machata161d82d2018-05-03 14:43:53 +0200903 err = br_fdb_external_learn_add(br, p, addr, vid, true);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000904 } else {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900905 spin_lock_bh(&br->hash_lock);
906 err = fdb_add_entry(br, p, addr, ndm->ndm_state,
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300907 nlh_flags, vid, ndm->ndm_flags);
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900908 spin_unlock_bh(&br->hash_lock);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000909 }
910
911 return err;
912}
913
stephen hemminger36fd2b62011-04-04 14:03:31 +0000914/* Add new permanent fdb entry with RTM_NEWNEIGH */
stephen hemmingeredc7d572012-10-01 12:32:33 +0000915int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
916 struct net_device *dev,
Petr Machata87b09842019-01-16 23:06:50 +0000917 const unsigned char *addr, u16 vid, u16 nlh_flags,
918 struct netlink_ext_ack *extack)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000919{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200920 struct net_bridge_vlan_group *vg;
Roopa Prabhu37418732015-10-08 10:38:52 -0700921 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200922 struct net_bridge_vlan *v;
Roopa Prabhu37418732015-10-08 10:38:52 -0700923 struct net_bridge *br = NULL;
John Fastabend77162022012-04-15 06:43:56 +0000924 int err = 0;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000925
Roopa Prabhub74fd302017-08-29 13:16:57 -0700926 trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);
927
stephen hemminger292d1392011-11-09 18:30:08 +0000928 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
929 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
930 return -EINVAL;
931 }
932
Stephen Hemminger537f7f82013-06-25 09:34:36 -0700933 if (is_zero_ether_addr(addr)) {
934 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
935 return -EINVAL;
936 }
937
Roopa Prabhu37418732015-10-08 10:38:52 -0700938 if (dev->priv_flags & IFF_EBRIDGE) {
939 br = netdev_priv(dev);
940 vg = br_vlan_group(br);
941 } else {
942 p = br_port_get_rtnl(dev);
943 if (!p) {
944 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
945 dev->name);
946 return -EINVAL;
947 }
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900948 br = p->br;
Roopa Prabhu37418732015-10-08 10:38:52 -0700949 vg = nbp_vlan_group(p);
stephen hemminger36fd2b62011-04-04 14:03:31 +0000950 }
951
Jiri Pirkof6f64242014-11-28 14:34:15 +0100952 if (vid) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200953 v = br_vlan_find(vg, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700954 if (!v || !br_vlan_should_use(v)) {
955 pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000956 return -EINVAL;
957 }
958
959 /* VID was specified, so use it. */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900960 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid);
stephen hemminger292d1392011-11-09 18:30:08 +0000961 } else {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900962 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200963 if (err || !vg || !vg->num_vlans)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000964 goto out;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000965
966 /* We have vlans configured on this port and user didn't
967 * specify a VLAN. To be nice, add/update entry for every
968 * vlan on this port.
969 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200970 list_for_each_entry(v, &vg->vlan_list, vlist) {
Roopa Prabhu37418732015-10-08 10:38:52 -0700971 if (!br_vlan_should_use(v))
972 continue;
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900973 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000974 if (err)
975 goto out;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000976 }
stephen hemminger292d1392011-11-09 18:30:08 +0000977 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000978
Vlad Yasevich1690be62013-02-13 12:00:18 +0000979out:
980 return err;
981}
982
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +0100983static int fdb_delete_by_addr_and_port(struct net_bridge *br,
984 const struct net_bridge_port *p,
Nikolay Aleksandrov8c86f962015-06-09 03:34:13 -0700985 const u8 *addr, u16 vlan)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000986{
Vlad Yasevich1690be62013-02-13 12:00:18 +0000987 struct net_bridge_fdb_entry *fdb;
988
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100989 fdb = br_fdb_find(br, addr, vlan);
Nikolay Aleksandrov8c86f962015-06-09 03:34:13 -0700990 if (!fdb || fdb->dst != p)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000991 return -ENOENT;
992
Petr Machata161d82d2018-05-03 14:43:53 +0200993 fdb_delete(br, fdb, true);
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +0100994
Vlad Yasevich1690be62013-02-13 12:00:18 +0000995 return 0;
996}
997
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +0100998static int __br_fdb_delete(struct net_bridge *br,
999 const struct net_bridge_port *p,
Vlad Yasevich1690be62013-02-13 12:00:18 +00001000 const unsigned char *addr, u16 vid)
1001{
1002 int err;
1003
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001004 spin_lock_bh(&br->hash_lock);
1005 err = fdb_delete_by_addr_and_port(br, p, addr, vid);
1006 spin_unlock_bh(&br->hash_lock);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001007
stephen hemminger36fd2b62011-04-04 14:03:31 +00001008 return err;
1009}
1010
stephen hemminger36fd2b62011-04-04 14:03:31 +00001011/* Remove neighbor entry with RTM_DELNEIGH */
Vlad Yasevich1690be62013-02-13 12:00:18 +00001012int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1013 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01001014 const unsigned char *addr, u16 vid)
stephen hemminger36fd2b62011-04-04 14:03:31 +00001015{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001016 struct net_bridge_vlan_group *vg;
Roopa Prabhu37418732015-10-08 10:38:52 -07001017 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001018 struct net_bridge_vlan *v;
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001019 struct net_bridge *br;
stephen hemminger36fd2b62011-04-04 14:03:31 +00001020 int err;
1021
Roopa Prabhu37418732015-10-08 10:38:52 -07001022 if (dev->priv_flags & IFF_EBRIDGE) {
1023 br = netdev_priv(dev);
1024 vg = br_vlan_group(br);
1025 } else {
1026 p = br_port_get_rtnl(dev);
1027 if (!p) {
1028 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
1029 dev->name);
1030 return -EINVAL;
1031 }
1032 vg = nbp_vlan_group(p);
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001033 br = p->br;
stephen hemminger36fd2b62011-04-04 14:03:31 +00001034 }
1035
Jiri Pirkof6f64242014-11-28 14:34:15 +01001036 if (vid) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001037 v = br_vlan_find(vg, vid);
1038 if (!v) {
Roopa Prabhu37418732015-10-08 10:38:52 -07001039 pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001040 return -EINVAL;
1041 }
stephen hemminger36fd2b62011-04-04 14:03:31 +00001042
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001043 err = __br_fdb_delete(br, p, addr, vid);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001044 } else {
Toshiaki Makita25d3b492015-02-09 20:16:17 +09001045 err = -ENOENT;
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001046 err &= __br_fdb_delete(br, p, addr, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001047 if (!vg || !vg->num_vlans)
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001048 return err;
Vlad Yasevich1690be62013-02-13 12:00:18 +00001049
Roopa Prabhu37418732015-10-08 10:38:52 -07001050 list_for_each_entry(v, &vg->vlan_list, vlist) {
1051 if (!br_vlan_should_use(v))
1052 continue;
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001053 err &= __br_fdb_delete(br, p, addr, v->vid);
Roopa Prabhu37418732015-10-08 10:38:52 -07001054 }
Vlad Yasevich1690be62013-02-13 12:00:18 +00001055 }
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001056
stephen hemminger36fd2b62011-04-04 14:03:31 +00001057 return err;
1058}
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001059
1060int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
1061{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001062 struct net_bridge_fdb_entry *f, *tmp;
Geert Uytterhoeven367dc652018-02-01 11:25:27 +01001063 int err = 0;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001064
1065 ASSERT_RTNL();
1066
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001067 /* the key here is that static entries change only under rtnl */
1068 rcu_read_lock();
1069 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1070 /* We only care for static entries */
1071 if (!f->is_static)
1072 continue;
1073 err = dev_uc_add(p->dev, f->key.addr.addr);
1074 if (err)
1075 goto rollback;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001076 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001077done:
1078 rcu_read_unlock();
1079
1080 return err;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001081
1082rollback:
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001083 hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
1084 /* We only care for static entries */
1085 if (!tmp->is_static)
1086 continue;
1087 if (tmp == f)
1088 break;
1089 dev_uc_del(p->dev, tmp->key.addr.addr);
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001090 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001091
1092 goto done;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001093}
1094
1095void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
1096{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001097 struct net_bridge_fdb_entry *f;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001098
1099 ASSERT_RTNL();
1100
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001101 rcu_read_lock();
1102 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1103 /* We only care for static entries */
1104 if (!f->is_static)
1105 continue;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001106
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001107 dev_uc_del(p->dev, f->key.addr.addr);
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001108 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001109 rcu_read_unlock();
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001110}
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001111
Jiri Pirko3aeb6612015-01-15 23:49:37 +01001112int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
Petr Machata161d82d2018-05-03 14:43:53 +02001113 const unsigned char *addr, u16 vid,
1114 bool swdev_notify)
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001115{
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001116 struct net_bridge_fdb_entry *fdb;
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001117 bool modified = false;
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001118 int err = 0;
1119
Roopa Prabhub74fd302017-08-29 13:16:57 -07001120 trace_br_fdb_external_learn_add(br, p, addr, vid);
1121
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001122 spin_lock_bh(&br->hash_lock);
1123
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +01001124 fdb = br_fdb_find(br, addr, vid);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001125 if (!fdb) {
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001126 fdb = fdb_create(br, p, addr, vid, 0, 0);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001127 if (!fdb) {
1128 err = -ENOMEM;
1129 goto err_unlock;
1130 }
Ido Schimmel710ae722019-01-18 15:58:00 +00001131 if (swdev_notify)
1132 fdb->added_by_user = 1;
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001133 fdb->added_by_external_learn = 1;
Petr Machata161d82d2018-05-03 14:43:53 +02001134 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001135 } else {
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001136 fdb->updated = jiffies;
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001137
1138 if (fdb->dst != p) {
1139 fdb->dst = p;
1140 modified = true;
1141 }
1142
1143 if (fdb->added_by_external_learn) {
1144 /* Refresh entry */
1145 fdb->used = jiffies;
1146 } else if (!fdb->added_by_user) {
1147 /* Take over SW learned entry */
1148 fdb->added_by_external_learn = 1;
1149 modified = true;
1150 }
1151
Ido Schimmel710ae722019-01-18 15:58:00 +00001152 if (swdev_notify)
1153 fdb->added_by_user = 1;
1154
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001155 if (modified)
Petr Machata161d82d2018-05-03 14:43:53 +02001156 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001157 }
1158
1159err_unlock:
1160 spin_unlock_bh(&br->hash_lock);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001161
1162 return err;
1163}
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001164
Jiri Pirko3aeb6612015-01-15 23:49:37 +01001165int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
Petr Machata161d82d2018-05-03 14:43:53 +02001166 const unsigned char *addr, u16 vid,
1167 bool swdev_notify)
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001168{
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001169 struct net_bridge_fdb_entry *fdb;
1170 int err = 0;
1171
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001172 spin_lock_bh(&br->hash_lock);
1173
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +01001174 fdb = br_fdb_find(br, addr, vid);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001175 if (fdb && fdb->added_by_external_learn)
Petr Machata161d82d2018-05-03 14:43:53 +02001176 fdb_delete(br, fdb, swdev_notify);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001177 else
1178 err = -ENOENT;
1179
1180 spin_unlock_bh(&br->hash_lock);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001181
1182 return err;
1183}
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +02001184
1185void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
Ido Schimmele9ba0fb2018-10-17 08:53:29 +00001186 const unsigned char *addr, u16 vid, bool offloaded)
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +02001187{
1188 struct net_bridge_fdb_entry *fdb;
1189
1190 spin_lock_bh(&br->hash_lock);
1191
1192 fdb = br_fdb_find(br, addr, vid);
1193 if (fdb)
Ido Schimmele9ba0fb2018-10-17 08:53:29 +00001194 fdb->offloaded = offloaded;
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +02001195
1196 spin_unlock_bh(&br->hash_lock);
1197}
Petr Machata43920ed2018-12-07 19:55:07 +00001198
1199void br_fdb_clear_offload(const struct net_device *dev, u16 vid)
1200{
1201 struct net_bridge_fdb_entry *f;
1202 struct net_bridge_port *p;
1203
1204 ASSERT_RTNL();
1205
1206 p = br_port_get_rtnl(dev);
1207 if (!p)
1208 return;
1209
1210 spin_lock_bh(&p->br->hash_lock);
1211 hlist_for_each_entry(f, &p->br->fdb_list, fdb_node) {
1212 if (f->dst == p && f->key.vlan_id == vid)
1213 f->offloaded = 0;
1214 }
1215 spin_unlock_bh(&p->br->hash_lock);
1216}
1217EXPORT_SYMBOL_GPL(br_fdb_clear_offload);