blob: 09e7a1dd9e3ce99ca094636f3b9c3c0e74948130 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Forwarding database
4 * Linux ethernet bridge
5 *
6 * Authors:
7 * Lennert Buytenhek <buytenh@gnu.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/kernel.h>
11#include <linux/init.h>
Franck Bui-Huu82524742008-05-12 21:21:05 +020012#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/spinlock.h>
14#include <linux/times.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
17#include <linux/jhash.h>
Stephen Hemminger3f890922007-03-21 13:42:33 -070018#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Arun Sharma600634972011-07-26 16:09:06 -070020#include <linux/atomic.h>
Stephen Hemminger3f890922007-03-21 13:42:33 -070021#include <asm/unaligned.h>
Vlad Yasevich2ba071e2013-02-13 12:00:16 +000022#include <linux/if_vlan.h>
Scott Feldmanb4ad7ba2015-06-14 11:33:11 -070023#include <net/switchdev.h>
Roopa Prabhub74fd302017-08-29 13:16:57 -070024#include <trace/events/bridge.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "br_private.h"
26
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020027static const struct rhashtable_params br_fdb_rht_params = {
28 .head_offset = offsetof(struct net_bridge_fdb_entry, rhnode),
29 .key_offset = offsetof(struct net_bridge_fdb_entry, key),
30 .key_len = sizeof(struct net_bridge_fdb_key),
31 .automatic_shrinking = true,
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020032};
33
Christoph Lametere18b8902006-12-06 20:33:20 -080034static struct kmem_cache *br_fdb_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Akinobu Mita87a596e2007-04-07 18:57:07 +090036int __init br_fdb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
39 sizeof(struct net_bridge_fdb_entry),
40 0,
Paul Mundt20c2df82007-07-20 10:11:58 +090041 SLAB_HWCACHE_ALIGN, NULL);
Akinobu Mita87a596e2007-04-07 18:57:07 +090042 if (!br_fdb_cache)
43 return -ENOMEM;
44
Akinobu Mita87a596e2007-04-07 18:57:07 +090045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Andrew Morton73afc902007-12-05 21:35:23 -080048void br_fdb_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 kmem_cache_destroy(br_fdb_cache);
51}
52
Nikolay Aleksandroveb793582017-12-12 16:02:50 +020053int br_fdb_hash_init(struct net_bridge *br)
54{
55 return rhashtable_init(&br->fdb_hash_tbl, &br_fdb_rht_params);
56}
57
58void br_fdb_hash_fini(struct net_bridge *br)
59{
60 rhashtable_destroy(&br->fdb_hash_tbl);
61}
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/* if topology_changing then use forward_delay (default 15 sec)
64 * otherwise keep longer (default 5 minutes)
65 */
Stephen Hemminger3f890922007-03-21 13:42:33 -070066static inline unsigned long hold_time(const struct net_bridge *br)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 return br->topology_change ? br->forward_delay : br->ageing_time;
69}
70
Stephen Hemminger3f890922007-03-21 13:42:33 -070071static inline int has_expired(const struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 const struct net_bridge_fdb_entry *fdb)
73{
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +020074 return !test_bit(BR_FDB_STATIC, &fdb->flags) &&
Nikolay Aleksandrovb5cd9f72019-10-29 13:45:57 +020075 !test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags) &&
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +020076 time_before_eq(fdb->updated + hold_time(br), jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Michał Mirosławda678292009-06-05 05:35:28 +000079static void fdb_rcu_free(struct rcu_head *head)
80{
81 struct net_bridge_fdb_entry *ent
82 = container_of(head, struct net_bridge_fdb_entry, rcu);
83 kmem_cache_free(br_fdb_cache, ent);
84}
85
Vladimir Oltean46820482021-10-26 17:27:36 +030086static int fdb_to_nud(const struct net_bridge *br,
87 const struct net_bridge_fdb_entry *fdb)
88{
89 if (test_bit(BR_FDB_LOCAL, &fdb->flags))
90 return NUD_PERMANENT;
91 else if (test_bit(BR_FDB_STATIC, &fdb->flags))
92 return NUD_NOARP;
93 else if (has_expired(br, fdb))
94 return NUD_STALE;
95 else
96 return NUD_REACHABLE;
97}
98
99static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
100 const struct net_bridge_fdb_entry *fdb,
101 u32 portid, u32 seq, int type, unsigned int flags)
102{
103 const struct net_bridge_port *dst = READ_ONCE(fdb->dst);
104 unsigned long now = jiffies;
105 struct nda_cacheinfo ci;
106 struct nlmsghdr *nlh;
107 struct ndmsg *ndm;
108
109 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
110 if (nlh == NULL)
111 return -EMSGSIZE;
112
113 ndm = nlmsg_data(nlh);
114 ndm->ndm_family = AF_BRIDGE;
115 ndm->ndm_pad1 = 0;
116 ndm->ndm_pad2 = 0;
117 ndm->ndm_flags = 0;
118 ndm->ndm_type = 0;
119 ndm->ndm_ifindex = dst ? dst->dev->ifindex : br->dev->ifindex;
120 ndm->ndm_state = fdb_to_nud(br, fdb);
121
122 if (test_bit(BR_FDB_OFFLOADED, &fdb->flags))
123 ndm->ndm_flags |= NTF_OFFLOADED;
124 if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags))
125 ndm->ndm_flags |= NTF_EXT_LEARNED;
126 if (test_bit(BR_FDB_STICKY, &fdb->flags))
127 ndm->ndm_flags |= NTF_STICKY;
128
129 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr))
130 goto nla_put_failure;
131 if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
132 goto nla_put_failure;
133 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
134 ci.ndm_confirmed = 0;
135 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
136 ci.ndm_refcnt = 0;
137 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
138 goto nla_put_failure;
139
140 if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16),
141 &fdb->key.vlan_id))
142 goto nla_put_failure;
143
144 if (test_bit(BR_FDB_NOTIFY, &fdb->flags)) {
145 struct nlattr *nest = nla_nest_start(skb, NDA_FDB_EXT_ATTRS);
146 u8 notify_bits = FDB_NOTIFY_BIT;
147
148 if (!nest)
149 goto nla_put_failure;
150 if (test_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags))
151 notify_bits |= FDB_NOTIFY_INACTIVE_BIT;
152
153 if (nla_put_u8(skb, NFEA_ACTIVITY_NOTIFY, notify_bits)) {
154 nla_nest_cancel(skb, nest);
155 goto nla_put_failure;
156 }
157
158 nla_nest_end(skb, nest);
159 }
160
161 nlmsg_end(skb, nlh);
162 return 0;
163
164nla_put_failure:
165 nlmsg_cancel(skb, nlh);
166 return -EMSGSIZE;
167}
168
169static inline size_t fdb_nlmsg_size(void)
170{
171 return NLMSG_ALIGN(sizeof(struct ndmsg))
172 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
173 + nla_total_size(sizeof(u32)) /* NDA_MASTER */
174 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
175 + nla_total_size(sizeof(struct nda_cacheinfo))
176 + nla_total_size(0) /* NDA_FDB_EXT_ATTRS */
177 + nla_total_size(sizeof(u8)); /* NFEA_ACTIVITY_NOTIFY */
178}
179
180static void fdb_notify(struct net_bridge *br,
181 const struct net_bridge_fdb_entry *fdb, int type,
182 bool swdev_notify)
183{
184 struct net *net = dev_net(br->dev);
185 struct sk_buff *skb;
186 int err = -ENOBUFS;
187
188 if (swdev_notify)
189 br_switchdev_fdb_notify(br, fdb, type);
190
191 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
192 if (skb == NULL)
193 goto errout;
194
195 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
196 if (err < 0) {
197 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
198 WARN_ON(err == -EMSGSIZE);
199 kfree_skb(skb);
200 goto errout;
201 }
202 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
203 return;
204errout:
205 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
206}
207
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200208static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl,
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100209 const unsigned char *addr,
210 __u16 vid)
211{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200212 struct net_bridge_fdb_key key;
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100213
Nikolay Aleksandrov410b3d42017-02-13 14:59:10 +0100214 WARN_ON_ONCE(!rcu_read_lock_held());
215
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200216 key.vlan_id = vid;
217 memcpy(key.addr.addr, addr, sizeof(key.addr.addr));
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100218
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200219 return rhashtable_lookup(tbl, &key, br_fdb_rht_params);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100220}
221
222/* requires bridge hash_lock */
223static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br,
224 const unsigned char *addr,
225 __u16 vid)
226{
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100227 struct net_bridge_fdb_entry *fdb;
228
WANG Congd12c9172017-03-16 10:32:42 -0700229 lockdep_assert_held_once(&br->hash_lock);
Nikolay Aleksandrov410b3d42017-02-13 14:59:10 +0100230
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100231 rcu_read_lock();
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200232 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100233 rcu_read_unlock();
234
235 return fdb;
236}
237
Petr Machata4d4fd362018-04-29 10:56:08 +0300238struct net_device *br_fdb_find_port(const struct net_device *br_dev,
239 const unsigned char *addr,
240 __u16 vid)
241{
242 struct net_bridge_fdb_entry *f;
243 struct net_device *dev = NULL;
244 struct net_bridge *br;
245
246 ASSERT_RTNL();
247
248 if (!netif_is_bridge_master(br_dev))
249 return NULL;
250
251 br = netdev_priv(br_dev);
Petr Machata873aca22018-06-08 15:11:47 +0200252 rcu_read_lock();
253 f = br_fdb_find_rcu(br, addr, vid);
Petr Machata4d4fd362018-04-29 10:56:08 +0300254 if (f && f->dst)
255 dev = f->dst->dev;
Petr Machata873aca22018-06-08 15:11:47 +0200256 rcu_read_unlock();
Petr Machata4d4fd362018-04-29 10:56:08 +0300257
258 return dev;
259}
260EXPORT_SYMBOL_GPL(br_fdb_find_port);
261
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100262struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
263 const unsigned char *addr,
264 __u16 vid)
265{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200266 return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100267}
268
Vlad Yasevich145beee2014-05-16 09:59:19 -0400269/* When a static FDB entry is added, the mac address from the entry is
270 * added to the bridge private HW address list and all required ports
271 * are then updated with the new information.
272 * Called under RTNL.
273 */
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100274static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr)
Vlad Yasevich145beee2014-05-16 09:59:19 -0400275{
276 int err;
Li RongQinga3f5ee72014-06-18 16:07:16 +0800277 struct net_bridge_port *p;
Vlad Yasevich145beee2014-05-16 09:59:19 -0400278
279 ASSERT_RTNL();
280
281 list_for_each_entry(p, &br->port_list, list) {
282 if (!br_promisc_port(p)) {
283 err = dev_uc_add(p->dev, addr);
284 if (err)
285 goto undo;
286 }
287 }
288
289 return;
290undo:
Li RongQinga3f5ee72014-06-18 16:07:16 +0800291 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
292 if (!br_promisc_port(p))
293 dev_uc_del(p->dev, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400294 }
295}
296
297/* When a static FDB entry is deleted, the HW address from that entry is
298 * also removed from the bridge private HW address list and updates all
299 * the ports with needed information.
300 * Called under RTNL.
301 */
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100302static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
Vlad Yasevich145beee2014-05-16 09:59:19 -0400303{
304 struct net_bridge_port *p;
305
306 ASSERT_RTNL();
307
308 list_for_each_entry(p, &br->port_list, list) {
309 if (!br_promisc_port(p))
310 dev_uc_del(p->dev, addr);
311 }
312}
313
Petr Machata161d82d2018-05-03 14:43:53 +0200314static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
315 bool swdev_notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Roopa Prabhub74fd302017-08-29 13:16:57 -0700317 trace_fdb_delete(br, f);
318
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200319 if (test_bit(BR_FDB_STATIC, &f->flags))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200320 fdb_del_hw_addr(br, f->key.addr.addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400321
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200322 hlist_del_init_rcu(&f->fdb_node);
323 rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode,
324 br_fdb_rht_params);
Petr Machata161d82d2018-05-03 14:43:53 +0200325 fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
Michał Mirosławda678292009-06-05 05:35:28 +0000326 call_rcu(&f->rcu, fdb_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Toshiaki Makita960b5892014-02-07 16:48:23 +0900329/* Delete a local entry if no other port had the same address. */
330static void fdb_delete_local(struct net_bridge *br,
331 const struct net_bridge_port *p,
332 struct net_bridge_fdb_entry *f)
333{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200334 const unsigned char *addr = f->key.addr.addr;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200335 struct net_bridge_vlan_group *vg;
336 const struct net_bridge_vlan *v;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900337 struct net_bridge_port *op;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200338 u16 vid = f->key.vlan_id;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900339
340 /* Maybe another port has same hw addr? */
341 list_for_each_entry(op, &br->port_list, list) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200342 vg = nbp_vlan_group(op);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900343 if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200344 (!vid || br_vlan_find(vg, vid))) {
Toshiaki Makita960b5892014-02-07 16:48:23 +0900345 f->dst = op;
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200346 clear_bit(BR_FDB_ADDED_BY_USER, &f->flags);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900347 return;
348 }
349 }
350
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200351 vg = br_vlan_group(br);
352 v = br_vlan_find(vg, vid);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900353 /* Maybe bridge device has same hw addr? */
354 if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200355 (!vid || (v && br_vlan_should_use(v)))) {
Toshiaki Makita960b5892014-02-07 16:48:23 +0900356 f->dst = NULL;
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200357 clear_bit(BR_FDB_ADDED_BY_USER, &f->flags);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900358 return;
359 }
360
Petr Machata161d82d2018-05-03 14:43:53 +0200361 fdb_delete(br, f, true);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900362}
363
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900364void br_fdb_find_delete_local(struct net_bridge *br,
365 const struct net_bridge_port *p,
366 const unsigned char *addr, u16 vid)
367{
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900368 struct net_bridge_fdb_entry *f;
369
370 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100371 f = br_fdb_find(br, addr, vid);
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200372 if (f && test_bit(BR_FDB_LOCAL, &f->flags) &&
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200373 !test_bit(BR_FDB_ADDED_BY_USER, &f->flags) && f->dst == p)
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900374 fdb_delete_local(br, p, f);
375 spin_unlock_bh(&br->hash_lock);
376}
377
Vladimir Oltean5f94a5e2021-10-26 17:27:37 +0300378static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
379 struct net_bridge_port *source,
380 const unsigned char *addr,
381 __u16 vid,
382 unsigned long flags)
383{
384 struct net_bridge_fdb_entry *fdb;
385
386 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
387 if (fdb) {
388 memcpy(fdb->key.addr.addr, addr, ETH_ALEN);
389 WRITE_ONCE(fdb->dst, source);
390 fdb->key.vlan_id = vid;
391 fdb->flags = flags;
392 fdb->updated = fdb->used = jiffies;
393 if (rhashtable_lookup_insert_fast(&br->fdb_hash_tbl,
394 &fdb->rhnode,
395 br_fdb_rht_params)) {
396 kmem_cache_free(br_fdb_cache, fdb);
397 fdb = NULL;
398 } else {
399 hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list);
400 }
401 }
402 return fdb;
403}
404
Vladimir Oltean4731b6d2021-10-26 17:27:38 +0300405static int fdb_add_local(struct net_bridge *br, struct net_bridge_port *source,
406 const unsigned char *addr, u16 vid)
Vladimir Oltean5f94a5e2021-10-26 17:27:37 +0300407{
408 struct net_bridge_fdb_entry *fdb;
409
410 if (!is_valid_ether_addr(addr))
411 return -EINVAL;
412
413 fdb = br_fdb_find(br, addr, vid);
414 if (fdb) {
415 /* it is okay to have multiple ports with same
416 * address, just use the first one.
417 */
418 if (test_bit(BR_FDB_LOCAL, &fdb->flags))
419 return 0;
420 br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
421 source ? source->dev->name : br->dev->name, addr, vid);
422 fdb_delete(br, fdb, true);
423 }
424
425 fdb = fdb_create(br, source, addr, vid,
426 BIT(BR_FDB_LOCAL) | BIT(BR_FDB_STATIC));
427 if (!fdb)
428 return -ENOMEM;
429
430 fdb_add_hw_addr(br, addr);
431 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
432 return 0;
433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
436{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200437 struct net_bridge_vlan_group *vg;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200438 struct net_bridge_fdb_entry *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct net_bridge *br = p->br;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200440 struct net_bridge_vlan *v;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200443 vg = nbp_vlan_group(p);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200444 hlist_for_each_entry(f, &br->fdb_list, fdb_node) {
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200445 if (f->dst == p && test_bit(BR_FDB_LOCAL, &f->flags) &&
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200446 !test_bit(BR_FDB_ADDED_BY_USER, &f->flags)) {
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200447 /* delete old one */
448 fdb_delete_local(br, p, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200450 /* if this port has no vlan information
451 * configured, we can safely be done at
452 * this point.
453 */
454 if (!vg || !vg->num_vlans)
455 goto insert;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Toshiaki Makita28368822014-02-07 16:48:19 +0900459insert:
460 /* insert new address, may fail if invalid address or dup. */
Vladimir Oltean4731b6d2021-10-26 17:27:38 +0300461 fdb_add_local(br, p, newaddr, 0);
Toshiaki Makita28368822014-02-07 16:48:19 +0900462
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200463 if (!vg || !vg->num_vlans)
Toshiaki Makita28368822014-02-07 16:48:19 +0900464 goto done;
465
466 /* Now add entries for every VLAN configured on the port.
467 * This function runs under RTNL so the bitmap will not change
468 * from under us.
469 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200470 list_for_each_entry(v, &vg->vlan_list, vlist)
Vladimir Oltean4731b6d2021-10-26 17:27:38 +0300471 fdb_add_local(br, p, newaddr, v->vid);
Toshiaki Makita28368822014-02-07 16:48:19 +0900472
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000473done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 spin_unlock_bh(&br->hash_lock);
475}
476
stephen hemminger43598812011-12-08 07:17:49 +0000477void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
478{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200479 struct net_bridge_vlan_group *vg;
stephen hemminger43598812011-12-08 07:17:49 +0000480 struct net_bridge_fdb_entry *f;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200481 struct net_bridge_vlan *v;
stephen hemminger43598812011-12-08 07:17:49 +0000482
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900483 spin_lock_bh(&br->hash_lock);
484
stephen hemminger43598812011-12-08 07:17:49 +0000485 /* If old entry was unassociated with any port, then delete it. */
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100486 f = br_fdb_find(br, br->dev->dev_addr, 0);
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200487 if (f && test_bit(BR_FDB_LOCAL, &f->flags) &&
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200488 !f->dst && !test_bit(BR_FDB_ADDED_BY_USER, &f->flags))
Toshiaki Makita960b5892014-02-07 16:48:23 +0900489 fdb_delete_local(br, NULL, f);
stephen hemminger43598812011-12-08 07:17:49 +0000490
Vladimir Oltean4731b6d2021-10-26 17:27:38 +0300491 fdb_add_local(br, NULL, newaddr, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200492 vg = br_vlan_group(br);
493 if (!vg || !vg->num_vlans)
494 goto out;
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000495 /* Now remove and add entries for every VLAN configured on the
496 * bridge. This function runs under RTNL so the bitmap will not
497 * change from under us.
498 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200499 list_for_each_entry(v, &vg->vlan_list, vlist) {
Toshiaki Makita0b148de2016-06-07 19:14:17 +0900500 if (!br_vlan_should_use(v))
501 continue;
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100502 f = br_fdb_find(br, br->dev->dev_addr, v->vid);
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200503 if (f && test_bit(BR_FDB_LOCAL, &f->flags) &&
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200504 !f->dst && !test_bit(BR_FDB_ADDED_BY_USER, &f->flags))
Toshiaki Makita960b5892014-02-07 16:48:23 +0900505 fdb_delete_local(br, NULL, f);
Vladimir Oltean4731b6d2021-10-26 17:27:38 +0300506 fdb_add_local(br, NULL, newaddr, v->vid);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000507 }
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900508out:
509 spin_unlock_bh(&br->hash_lock);
stephen hemminger43598812011-12-08 07:17:49 +0000510}
511
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100512void br_fdb_cleanup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100514 struct net_bridge *br = container_of(work, struct net_bridge,
515 gc_work.work);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200516 struct net_bridge_fdb_entry *f = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 unsigned long delay = hold_time(br);
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100518 unsigned long work_delay = delay;
519 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200521 /* this part is tricky, in order to avoid blocking learning and
522 * consequently forwarding, we rely on rcu to delete objects with
523 * delayed freeing allowing us to continue traversing
524 */
525 rcu_read_lock();
526 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300527 unsigned long this_timer = f->updated + delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200529 if (test_bit(BR_FDB_STATIC, &f->flags) ||
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300530 test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &f->flags)) {
531 if (test_bit(BR_FDB_NOTIFY, &f->flags)) {
532 if (time_after(this_timer, now))
533 work_delay = min(work_delay,
534 this_timer - now);
535 else if (!test_and_set_bit(BR_FDB_NOTIFY_INACTIVE,
536 &f->flags))
537 fdb_notify(br, f, RTM_NEWNEIGH, false);
538 }
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100539 continue;
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300540 }
541
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200542 if (time_after(this_timer, now)) {
543 work_delay = min(work_delay, this_timer - now);
544 } else {
545 spin_lock_bh(&br->hash_lock);
546 if (!hlist_unhashed(&f->fdb_node))
Petr Machata161d82d2018-05-03 14:43:53 +0200547 fdb_delete(br, f, true);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200548 spin_unlock_bh(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200551 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100553 /* Cleanup minimum 10 milliseconds apart */
554 work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10));
555 mod_delayed_work(system_long_wq, &br->gc_work, work_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700558/* Completely flush all dynamic entries in forwarding database.*/
559void br_fdb_flush(struct net_bridge *br)
560{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200561 struct net_bridge_fdb_entry *f;
562 struct hlist_node *tmp;
Stephen Hemminger1a620692006-10-12 14:45:38 -0700563
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700564 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200565 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200566 if (!test_bit(BR_FDB_STATIC, &f->flags))
Petr Machata161d82d2018-05-03 14:43:53 +0200567 fdb_delete(br, f, true);
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700568 }
569 spin_unlock_bh(&br->hash_lock);
570}
571
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300572/* Flush all entries referring to a specific port.
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700573 * if do_all is set also flush static entries
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700574 * if vid is set delete all entries that match the vlan_id
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700575 */
Stephen Hemminger1a620692006-10-12 14:45:38 -0700576void br_fdb_delete_by_port(struct net_bridge *br,
577 const struct net_bridge_port *p,
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700578 u16 vid,
Stephen Hemminger1a620692006-10-12 14:45:38 -0700579 int do_all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200581 struct net_bridge_fdb_entry *f;
582 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200585 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
586 if (f->dst != p)
587 continue;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900588
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200589 if (!do_all)
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200590 if (test_bit(BR_FDB_STATIC, &f->flags) ||
Nikolay Aleksandrovf2f37292020-09-28 18:30:02 +0300591 (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &f->flags) &&
592 !test_bit(BR_FDB_OFFLOADED, &f->flags)) ||
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200593 (vid && f->key.vlan_id != vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 continue;
595
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200596 if (test_bit(BR_FDB_LOCAL, &f->flags))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200597 fdb_delete_local(br, p, f);
598 else
Petr Machata161d82d2018-05-03 14:43:53 +0200599 fdb_delete(br, f, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601 spin_unlock_bh(&br->hash_lock);
602}
603
Igor Maraviće6373c42011-12-12 02:58:25 +0000604#if IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +0000605/* Interface used by ATM LANE hook to test
606 * if an addr is on some other bridge port */
607int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 struct net_bridge_fdb_entry *fdb;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000610 struct net_bridge_port *port;
Michał Mirosławda678292009-06-05 05:35:28 +0000611 int ret;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 rcu_read_lock();
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000614 port = br_port_get_rcu(dev);
615 if (!port)
616 ret = 0;
617 else {
Vladimir Oltean3e19ae72021-06-29 17:06:44 +0300618 const struct net_bridge_port *dst = NULL;
619
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100620 fdb = br_fdb_find_rcu(port->br, addr, 0);
Vladimir Oltean3e19ae72021-06-29 17:06:44 +0300621 if (fdb)
622 dst = READ_ONCE(fdb->dst);
623
624 ret = dst && dst->dev != dev &&
625 dst->state == BR_STATE_FORWARDING;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Michał Mirosławda678292009-06-05 05:35:28 +0000629 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
Michał Mirosławda678292009-06-05 05:35:28 +0000631#endif /* CONFIG_ATM_LANE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633/*
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900634 * Fill buffer with forwarding table records in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 * the API format.
636 */
637int br_fdb_fillbuf(struct net_bridge *br, void *buf,
638 unsigned long maxnum, unsigned long skip)
639{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct net_bridge_fdb_entry *f;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200641 struct __fdb_entry *fe = buf;
642 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
645
646 rcu_read_lock();
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200647 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
648 if (num >= maxnum)
649 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200651 if (has_expired(br, f))
652 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200654 /* ignore pseudo entry for local MAC address */
655 if (!f->dst)
656 continue;
stephen hemminger43598812011-12-08 07:17:49 +0000657
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200658 if (skip) {
659 --skip;
660 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200663 /* convert from internal format to API */
664 memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN);
665
666 /* due to ABI compat need to split into hi/lo */
667 fe->port_no = f->dst->port_no;
668 fe->port_hi = f->dst->port_no >> 8;
669
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200670 fe->is_local = test_bit(BR_FDB_LOCAL, &f->flags);
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200671 if (!test_bit(BR_FDB_STATIC, &f->flags))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200672 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
673 ++fe;
674 ++num;
675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 rcu_read_unlock();
677
678 return num;
679}
680
stephen hemminger03e9b642011-04-04 14:03:27 +0000681/* Add entry for local address of interface */
Vladimir Olteanf6814fd2021-10-26 17:27:39 +0300682int br_fdb_add_local(struct net_bridge *br, struct net_bridge_port *source,
683 const unsigned char *addr, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 int ret;
686
687 spin_lock_bh(&br->hash_lock);
Vladimir Oltean4731b6d2021-10-26 17:27:38 +0300688 ret = fdb_add_local(br, source, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 spin_unlock_bh(&br->hash_lock);
690 return ret;
691}
692
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300693/* returns true if the fdb was modified */
694static bool __fdb_mark_active(struct net_bridge_fdb_entry *fdb)
695{
696 return !!(test_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags) &&
697 test_and_clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags));
698}
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +0200701 const unsigned char *addr, u16 vid, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct net_bridge_fdb_entry *fdb;
704
705 /* some users want to always flood. */
706 if (hold_time(br) == 0)
707 return;
708
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200709 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (likely(fdb)) {
711 /* attempt to update an entry for a local interface */
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200712 if (unlikely(test_bit(BR_FDB_LOCAL, &fdb->flags))) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900713 if (net_ratelimit())
Roopa Prabhude1dfee2016-10-11 16:12:56 -0700714 br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
715 source->dev->name, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 } else {
stephen hemmingerca6d4482017-02-07 08:46:46 -0800717 unsigned long now = jiffies;
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300718 bool fdb_modified = false;
719
720 if (now != fdb->updated) {
721 fdb->updated = now;
722 fdb_modified = __fdb_mark_active(fdb);
723 }
stephen hemmingerca6d4482017-02-07 08:46:46 -0800724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /* fastpath: update of existing entry */
Vladimir Oltean3e19ae72021-06-29 17:06:44 +0300726 if (unlikely(source != READ_ONCE(fdb->dst) &&
Nikolay Aleksandrove0458d92019-10-29 13:45:55 +0200727 !test_bit(BR_FDB_STICKY, &fdb->flags))) {
Tobias Waldekranz6eb38bf2021-06-29 17:06:45 +0300728 br_switchdev_fdb_notify(br, fdb, RTM_DELNEIGH);
Vladimir Oltean3e19ae72021-06-29 17:06:44 +0300729 WRITE_ONCE(fdb->dst, source);
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000730 fdb_modified = true;
Arkadi Sharshevsky58073b32017-04-28 22:39:07 +0300731 /* Take over HW learned entry */
Nikolay Aleksandrov58ec1ea2019-11-01 14:46:39 +0200732 if (unlikely(test_bit(BR_FDB_ADDED_BY_EXT_LEARN,
733 &fdb->flags)))
734 clear_bit(BR_FDB_ADDED_BY_EXT_LEARN,
735 &fdb->flags);
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000736 }
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300737
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +0200738 if (unlikely(test_bit(BR_FDB_ADDED_BY_USER, &flags)))
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200739 set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
Roopa Prabhue3cfddd2017-08-30 22:18:13 -0700740 if (unlikely(fdb_modified)) {
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +0200741 trace_br_fdb_update(br, source, addr, vid, flags);
Petr Machata161d82d2018-05-03 14:43:53 +0200742 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
Roopa Prabhue3cfddd2017-08-30 22:18:13 -0700743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745 } else {
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800746 spin_lock(&br->hash_lock);
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +0200747 fdb = fdb_create(br, source, addr, vid, flags);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200748 if (fdb) {
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +0200749 trace_br_fdb_update(br, source, addr, vid, flags);
Petr Machata161d82d2018-05-03 14:43:53 +0200750 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
stephen hemmingerf58ee4e2011-12-06 13:02:24 +0000751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /* else we lose race and someone else inserts
753 * it first, don't bother updating
754 */
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800755 spin_unlock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000758
Vladimir Olteanb4454bc2021-07-28 21:27:47 +0300759static int br_fdb_replay_one(struct net_bridge *br, struct notifier_block *nb,
Vladimir Olteanbdf123b2021-06-27 14:54:26 +0300760 const struct net_bridge_fdb_entry *fdb,
Vladimir Olteanb4454bc2021-07-28 21:27:47 +0300761 unsigned long action, const void *ctx)
Vladimir Oltean04846f92021-03-23 01:51:45 +0200762{
Vladimir Olteanb4454bc2021-07-28 21:27:47 +0300763 const struct net_bridge_port *p = READ_ONCE(fdb->dst);
Vladimir Oltean04846f92021-03-23 01:51:45 +0200764 struct switchdev_notifier_fdb_info item;
765 int err;
766
767 item.addr = fdb->key.addr.addr;
768 item.vid = fdb->key.vlan_id;
769 item.added_by_user = test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
770 item.offloaded = test_bit(BR_FDB_OFFLOADED, &fdb->flags);
Vladimir Olteane887b2d2021-06-27 14:54:22 +0300771 item.is_local = test_bit(BR_FDB_LOCAL, &fdb->flags);
Vladimir Oltean2e19bb32021-08-02 14:36:33 +0300772 item.info.dev = (!p || item.is_local) ? br->dev : p->dev;
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +0300773 item.info.ctx = ctx;
Vladimir Oltean04846f92021-03-23 01:51:45 +0200774
Vladimir Oltean7e8c1852021-06-27 14:54:27 +0300775 err = nb->notifier_call(nb, action, &item);
Vladimir Oltean04846f92021-03-23 01:51:45 +0200776 return notifier_to_errno(err);
777}
778
Vladimir Olteanb4454bc2021-07-28 21:27:47 +0300779int br_fdb_replay(const struct net_device *br_dev, const void *ctx, bool adding,
780 struct notifier_block *nb)
Vladimir Oltean04846f92021-03-23 01:51:45 +0200781{
782 struct net_bridge_fdb_entry *fdb;
783 struct net_bridge *br;
Vladimir Oltean7e8c1852021-06-27 14:54:27 +0300784 unsigned long action;
Vladimir Oltean04846f92021-03-23 01:51:45 +0200785 int err = 0;
786
Vladimir Oltean7105b502021-07-21 19:24:02 +0300787 if (!nb)
788 return 0;
789
Vladimir Olteanf851a722021-06-29 17:06:46 +0300790 if (!netif_is_bridge_master(br_dev))
791 return -EINVAL;
792
Vladimir Oltean04846f92021-03-23 01:51:45 +0200793 br = netdev_priv(br_dev);
794
Vladimir Oltean7e8c1852021-06-27 14:54:27 +0300795 if (adding)
796 action = SWITCHDEV_FDB_ADD_TO_DEVICE;
797 else
798 action = SWITCHDEV_FDB_DEL_TO_DEVICE;
799
Vladimir Oltean04846f92021-03-23 01:51:45 +0200800 rcu_read_lock();
801
802 hlist_for_each_entry_rcu(fdb, &br->fdb_list, fdb_node) {
Vladimir Olteanb4454bc2021-07-28 21:27:47 +0300803 err = br_fdb_replay_one(br, nb, fdb, action, ctx);
Vladimir Oltean04846f92021-03-23 01:51:45 +0200804 if (err)
805 break;
806 }
807
808 rcu_read_unlock();
809
810 return err;
811}
Vladimir Oltean04846f92021-03-23 01:51:45 +0200812
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000813/* Dump information about entries, in response to GETNEIGH */
John Fastabend77162022012-04-15 06:43:56 +0000814int br_fdb_dump(struct sk_buff *skb,
815 struct netlink_callback *cb,
816 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400817 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -0700818 int *idx)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000819{
John Fastabend77162022012-04-15 06:43:56 +0000820 struct net_bridge *br = netdev_priv(dev);
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200821 struct net_bridge_fdb_entry *f;
Roopa Prabhud2976532016-08-30 21:56:45 -0700822 int err = 0;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000823
Kyungrok Chung254ec032021-10-16 20:21:36 +0900824 if (!netif_is_bridge_master(dev))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200825 return err;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000826
Roopa Prabhud2976532016-08-30 21:56:45 -0700827 if (!filter_dev) {
828 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
829 if (err < 0)
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200830 return err;
Roopa Prabhud2976532016-08-30 21:56:45 -0700831 }
Hubert Sokolowski6cb69742015-01-05 17:29:21 +0000832
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200833 rcu_read_lock();
834 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
835 if (*idx < cb->args[2])
836 goto skip;
837 if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) {
838 if (filter_dev != dev)
John Fastabend77162022012-04-15 06:43:56 +0000839 goto skip;
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200840 /* !f->dst is a special case for bridge
841 * It means the MAC belongs to the bridge
842 * Therefore need a little more filtering
843 * we only want to dump the !f->dst case
844 */
845 if (f->dst)
Hubert Sokolowski6cb69742015-01-05 17:29:21 +0000846 goto skip;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000847 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200848 if (!filter_dev && f->dst)
849 goto skip;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000850
Nikolay Aleksandroveb793582017-12-12 16:02:50 +0200851 err = fdb_fill_info(skb, br, f,
852 NETLINK_CB(cb->skb).portid,
853 cb->nlh->nlmsg_seq,
854 RTM_NEWNEIGH,
855 NLM_F_MULTI);
856 if (err < 0)
857 break;
858skip:
859 *idx += 1;
860 }
861 rcu_read_unlock();
862
Roopa Prabhud2976532016-08-30 21:56:45 -0700863 return err;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000864}
stephen hemminger36fd2b62011-04-04 14:03:31 +0000865
Roopa Prabhu47674562018-12-15 22:35:09 -0800866int br_fdb_get(struct sk_buff *skb,
867 struct nlattr *tb[],
868 struct net_device *dev,
869 const unsigned char *addr,
870 u16 vid, u32 portid, u32 seq,
871 struct netlink_ext_ack *extack)
872{
873 struct net_bridge *br = netdev_priv(dev);
874 struct net_bridge_fdb_entry *f;
875 int err = 0;
876
877 rcu_read_lock();
878 f = br_fdb_find_rcu(br, addr, vid);
879 if (!f) {
880 NL_SET_ERR_MSG(extack, "Fdb entry not found");
881 err = -ENOENT;
882 goto errout;
883 }
884
885 err = fdb_fill_info(skb, br, f, portid, seq,
886 RTM_NEWNEIGH, 0);
887errout:
888 rcu_read_unlock();
889 return err;
890}
891
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300892/* returns true if the fdb is modified */
893static bool fdb_handle_notify(struct net_bridge_fdb_entry *fdb, u8 notify)
894{
895 bool modified = false;
896
897 /* allow to mark an entry as inactive, usually done on creation */
898 if ((notify & FDB_NOTIFY_INACTIVE_BIT) &&
899 !test_and_set_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags))
900 modified = true;
901
902 if ((notify & FDB_NOTIFY_BIT) &&
903 !test_and_set_bit(BR_FDB_NOTIFY, &fdb->flags)) {
904 /* enabled activity tracking */
905 modified = true;
906 } else if (!(notify & FDB_NOTIFY_BIT) &&
907 test_and_clear_bit(BR_FDB_NOTIFY, &fdb->flags)) {
908 /* disabled activity tracking, clear notify state */
909 clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags);
910 modified = true;
911 }
912
913 return modified;
914}
915
stephen hemminger292d1392011-11-09 18:30:08 +0000916/* Update (create or replace) forwarding database entry */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900917static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300918 const u8 *addr, struct ndmsg *ndm, u16 flags, u16 vid,
919 struct nlattr *nfea_tb[])
stephen hemminger36fd2b62011-04-04 14:03:31 +0000920{
Nikolay Aleksandrov0592ff82020-06-23 23:47:15 +0300921 bool is_sticky = !!(ndm->ndm_flags & NTF_STICKY);
Nikolay Aleksandrovb5f1d9e2020-06-23 23:47:18 +0300922 bool refresh = !nfea_tb[NFEA_DONT_REFRESH];
stephen hemminger36fd2b62011-04-04 14:03:31 +0000923 struct net_bridge_fdb_entry *fdb;
Nikolay Aleksandrov0592ff82020-06-23 23:47:15 +0300924 u16 state = ndm->ndm_state;
roopab0a397f2013-04-22 12:56:49 +0000925 bool modified = false;
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300926 u8 notify = 0;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000927
Wilson Kokeb8d7ba2015-05-25 06:39:31 -0700928 /* If the port cannot learn allow only local and static entries */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900929 if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
Wilson Kokeb8d7ba2015-05-25 06:39:31 -0700930 !(source->state == BR_STATE_LEARNING ||
931 source->state == BR_STATE_FORWARDING))
932 return -EPERM;
933
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900934 if (!source && !(state & NUD_PERMANENT)) {
935 pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
936 br->dev->name);
937 return -EINVAL;
938 }
939
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300940 if (is_sticky && (state & NUD_PERMANENT))
941 return -EINVAL;
942
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300943 if (nfea_tb[NFEA_ACTIVITY_NOTIFY]) {
944 notify = nla_get_u8(nfea_tb[NFEA_ACTIVITY_NOTIFY]);
945 if ((notify & ~BR_FDB_NOTIFY_SETTABLE_BITS) ||
946 (notify & BR_FDB_NOTIFY_SETTABLE_BITS) == FDB_NOTIFY_INACTIVE_BIT)
947 return -EINVAL;
948 }
949
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100950 fdb = br_fdb_find(br, addr, vid);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000951 if (fdb == NULL) {
952 if (!(flags & NLM_F_CREATE))
953 return -ENOENT;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000954
Nikolay Aleksandrov3fb01a32019-10-29 13:45:59 +0200955 fdb = fdb_create(br, source, addr, vid, 0);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000956 if (!fdb)
957 return -ENOMEM;
roopab0a397f2013-04-22 12:56:49 +0000958
959 modified = true;
stephen hemminger64af1ba2011-09-30 14:37:27 +0000960 } else {
961 if (flags & NLM_F_EXCL)
962 return -EEXIST;
roopab0a397f2013-04-22 12:56:49 +0000963
Vladimir Oltean3e19ae72021-06-29 17:06:44 +0300964 if (READ_ONCE(fdb->dst) != source) {
965 WRITE_ONCE(fdb->dst, source);
roopab0a397f2013-04-22 12:56:49 +0000966 modified = true;
967 }
stephen hemminger64af1ba2011-09-30 14:37:27 +0000968 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000969
Roopa Prabhu37418732015-10-08 10:38:52 -0700970 if (fdb_to_nud(br, fdb) != state) {
Vlad Yasevich145beee2014-05-16 09:59:19 -0400971 if (state & NUD_PERMANENT) {
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200972 set_bit(BR_FDB_LOCAL, &fdb->flags);
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200973 if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags))
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100974 fdb_add_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400975 } else if (state & NUD_NOARP) {
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200976 clear_bit(BR_FDB_LOCAL, &fdb->flags);
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200977 if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags))
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100978 fdb_add_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400979 } else {
Nikolay Aleksandrov6869c3b2019-10-29 13:45:53 +0200980 clear_bit(BR_FDB_LOCAL, &fdb->flags);
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +0200981 if (test_and_clear_bit(BR_FDB_STATIC, &fdb->flags))
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100982 fdb_del_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400983 }
stephen hemminger292d1392011-11-09 18:30:08 +0000984
roopab0a397f2013-04-22 12:56:49 +0000985 modified = true;
986 }
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300987
Nikolay Aleksandrove0458d92019-10-29 13:45:55 +0200988 if (is_sticky != test_bit(BR_FDB_STICKY, &fdb->flags)) {
989 change_bit(BR_FDB_STICKY, &fdb->flags);
Nikolay Aleksandrov435f2e72018-09-11 09:39:53 +0300990 modified = true;
991 }
992
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +0300993 if (fdb_handle_notify(fdb, notify))
994 modified = true;
995
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +0200996 set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
roopab0a397f2013-04-22 12:56:49 +0000997
998 fdb->used = jiffies;
999 if (modified) {
Nikolay Aleksandrovb5f1d9e2020-06-23 23:47:18 +03001000 if (refresh)
1001 fdb->updated = jiffies;
Petr Machata161d82d2018-05-03 14:43:53 +02001002 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
stephen hemminger292d1392011-11-09 18:30:08 +00001003 }
1004
stephen hemminger36fd2b62011-04-04 14:03:31 +00001005 return 0;
1006}
1007
Toshiaki Makita7bb90c32016-08-04 11:11:19 +09001008static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br,
1009 struct net_bridge_port *p, const unsigned char *addr,
Vladimir Oltean0541a622021-08-02 02:17:30 +03001010 u16 nlh_flags, u16 vid, struct nlattr *nfea_tb[],
1011 struct netlink_ext_ack *extack)
Vlad Yasevich1690be62013-02-13 12:00:18 +00001012{
1013 int err = 0;
1014
1015 if (ndm->ndm_flags & NTF_USE) {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +09001016 if (!p) {
1017 pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
1018 br->dev->name);
1019 return -EINVAL;
1020 }
Nikolay Aleksandrov5d1fcaf2019-11-04 11:36:51 +02001021 if (!nbp_state_should_learn(p))
1022 return 0;
1023
Nikolay Aleksandrovc4c832f2015-06-06 06:49:00 -07001024 local_bh_disable();
Vlad Yasevich1690be62013-02-13 12:00:18 +00001025 rcu_read_lock();
Nikolay Aleksandrovbe0c5672019-11-01 14:46:37 +02001026 br_fdb_update(br, p, addr, vid, BIT(BR_FDB_ADDED_BY_USER));
Vlad Yasevich1690be62013-02-13 12:00:18 +00001027 rcu_read_unlock();
Nikolay Aleksandrovc4c832f2015-06-06 06:49:00 -07001028 local_bh_enable();
Nikolay Aleksandroveb100e02017-03-23 12:27:13 +02001029 } else if (ndm->ndm_flags & NTF_EXT_LEARNED) {
Vladimir Oltean0541a622021-08-02 02:17:30 +03001030 if (!p && !(ndm->ndm_state & NUD_PERMANENT)) {
1031 NL_SET_ERR_MSG_MOD(extack,
1032 "FDB entry towards bridge must be permanent");
1033 return -EINVAL;
1034 }
Nikolay Aleksandrov45a68782021-08-10 14:00:10 +03001035 err = br_fdb_external_learn_add(br, p, addr, vid, true);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001036 } else {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +09001037 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +03001038 err = fdb_add_entry(br, p, addr, ndm, nlh_flags, vid, nfea_tb);
Toshiaki Makita7bb90c32016-08-04 11:11:19 +09001039 spin_unlock_bh(&br->hash_lock);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001040 }
1041
1042 return err;
1043}
1044
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +03001045static const struct nla_policy br_nda_fdb_pol[NFEA_MAX + 1] = {
1046 [NFEA_ACTIVITY_NOTIFY] = { .type = NLA_U8 },
Nikolay Aleksandrovb5f1d9e2020-06-23 23:47:18 +03001047 [NFEA_DONT_REFRESH] = { .type = NLA_FLAG },
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +03001048};
1049
stephen hemminger36fd2b62011-04-04 14:03:31 +00001050/* Add new permanent fdb entry with RTM_NEWNEIGH */
stephen hemmingeredc7d572012-10-01 12:32:33 +00001051int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1052 struct net_device *dev,
Petr Machata87b09842019-01-16 23:06:50 +00001053 const unsigned char *addr, u16 vid, u16 nlh_flags,
1054 struct netlink_ext_ack *extack)
stephen hemminger36fd2b62011-04-04 14:03:31 +00001055{
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +03001056 struct nlattr *nfea_tb[NFEA_MAX + 1], *attr;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001057 struct net_bridge_vlan_group *vg;
Roopa Prabhu37418732015-10-08 10:38:52 -07001058 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001059 struct net_bridge_vlan *v;
Roopa Prabhu37418732015-10-08 10:38:52 -07001060 struct net_bridge *br = NULL;
John Fastabend77162022012-04-15 06:43:56 +00001061 int err = 0;
stephen hemminger36fd2b62011-04-04 14:03:31 +00001062
Roopa Prabhub74fd302017-08-29 13:16:57 -07001063 trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);
1064
stephen hemminger292d1392011-11-09 18:30:08 +00001065 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
1066 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
1067 return -EINVAL;
1068 }
1069
Stephen Hemminger537f7f82013-06-25 09:34:36 -07001070 if (is_zero_ether_addr(addr)) {
1071 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
1072 return -EINVAL;
1073 }
1074
Kyungrok Chung254ec032021-10-16 20:21:36 +09001075 if (netif_is_bridge_master(dev)) {
Roopa Prabhu37418732015-10-08 10:38:52 -07001076 br = netdev_priv(dev);
1077 vg = br_vlan_group(br);
1078 } else {
1079 p = br_port_get_rtnl(dev);
1080 if (!p) {
1081 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
1082 dev->name);
1083 return -EINVAL;
1084 }
Toshiaki Makita7bb90c32016-08-04 11:11:19 +09001085 br = p->br;
Roopa Prabhu37418732015-10-08 10:38:52 -07001086 vg = nbp_vlan_group(p);
stephen hemminger36fd2b62011-04-04 14:03:31 +00001087 }
1088
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +03001089 if (tb[NDA_FDB_EXT_ATTRS]) {
1090 attr = tb[NDA_FDB_EXT_ATTRS];
1091 err = nla_parse_nested(nfea_tb, NFEA_MAX, attr,
1092 br_nda_fdb_pol, extack);
1093 if (err)
1094 return err;
1095 } else {
1096 memset(nfea_tb, 0, sizeof(struct nlattr *) * (NFEA_MAX + 1));
1097 }
1098
Jiri Pirkof6f64242014-11-28 14:34:15 +01001099 if (vid) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001100 v = br_vlan_find(vg, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -07001101 if (!v || !br_vlan_should_use(v)) {
1102 pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001103 return -EINVAL;
1104 }
1105
1106 /* VID was specified, so use it. */
Vladimir Oltean0541a622021-08-02 02:17:30 +03001107 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid, nfea_tb,
1108 extack);
stephen hemminger292d1392011-11-09 18:30:08 +00001109 } else {
Vladimir Oltean0541a622021-08-02 02:17:30 +03001110 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0, nfea_tb,
1111 extack);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001112 if (err || !vg || !vg->num_vlans)
Vlad Yasevich1690be62013-02-13 12:00:18 +00001113 goto out;
Vlad Yasevich1690be62013-02-13 12:00:18 +00001114
1115 /* We have vlans configured on this port and user didn't
1116 * specify a VLAN. To be nice, add/update entry for every
1117 * vlan on this port.
1118 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001119 list_for_each_entry(v, &vg->vlan_list, vlist) {
Roopa Prabhu37418732015-10-08 10:38:52 -07001120 if (!br_vlan_should_use(v))
1121 continue;
Nikolay Aleksandrov31cbc39b2020-06-23 23:47:17 +03001122 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid,
Vladimir Oltean0541a622021-08-02 02:17:30 +03001123 nfea_tb, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001124 if (err)
1125 goto out;
Vlad Yasevich1690be62013-02-13 12:00:18 +00001126 }
stephen hemminger292d1392011-11-09 18:30:08 +00001127 }
stephen hemminger36fd2b62011-04-04 14:03:31 +00001128
Vlad Yasevich1690be62013-02-13 12:00:18 +00001129out:
1130 return err;
1131}
1132
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001133static int fdb_delete_by_addr_and_port(struct net_bridge *br,
1134 const struct net_bridge_port *p,
Nikolay Aleksandrov8c86f962015-06-09 03:34:13 -07001135 const u8 *addr, u16 vlan)
Vlad Yasevich1690be62013-02-13 12:00:18 +00001136{
Vlad Yasevich1690be62013-02-13 12:00:18 +00001137 struct net_bridge_fdb_entry *fdb;
1138
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +01001139 fdb = br_fdb_find(br, addr, vlan);
Vladimir Oltean3e19ae72021-06-29 17:06:44 +03001140 if (!fdb || READ_ONCE(fdb->dst) != p)
Vlad Yasevich1690be62013-02-13 12:00:18 +00001141 return -ENOENT;
1142
Petr Machata161d82d2018-05-03 14:43:53 +02001143 fdb_delete(br, fdb, true);
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001144
Vlad Yasevich1690be62013-02-13 12:00:18 +00001145 return 0;
1146}
1147
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001148static int __br_fdb_delete(struct net_bridge *br,
1149 const struct net_bridge_port *p,
Vlad Yasevich1690be62013-02-13 12:00:18 +00001150 const unsigned char *addr, u16 vid)
1151{
1152 int err;
1153
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001154 spin_lock_bh(&br->hash_lock);
1155 err = fdb_delete_by_addr_and_port(br, p, addr, vid);
1156 spin_unlock_bh(&br->hash_lock);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001157
stephen hemminger36fd2b62011-04-04 14:03:31 +00001158 return err;
1159}
1160
stephen hemminger36fd2b62011-04-04 14:03:31 +00001161/* Remove neighbor entry with RTM_DELNEIGH */
Vlad Yasevich1690be62013-02-13 12:00:18 +00001162int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1163 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01001164 const unsigned char *addr, u16 vid)
stephen hemminger36fd2b62011-04-04 14:03:31 +00001165{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001166 struct net_bridge_vlan_group *vg;
Roopa Prabhu37418732015-10-08 10:38:52 -07001167 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001168 struct net_bridge_vlan *v;
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001169 struct net_bridge *br;
stephen hemminger36fd2b62011-04-04 14:03:31 +00001170 int err;
1171
Kyungrok Chung254ec032021-10-16 20:21:36 +09001172 if (netif_is_bridge_master(dev)) {
Roopa Prabhu37418732015-10-08 10:38:52 -07001173 br = netdev_priv(dev);
1174 vg = br_vlan_group(br);
1175 } else {
1176 p = br_port_get_rtnl(dev);
1177 if (!p) {
1178 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
1179 dev->name);
1180 return -EINVAL;
1181 }
1182 vg = nbp_vlan_group(p);
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001183 br = p->br;
stephen hemminger36fd2b62011-04-04 14:03:31 +00001184 }
1185
Jiri Pirkof6f64242014-11-28 14:34:15 +01001186 if (vid) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001187 v = br_vlan_find(vg, vid);
1188 if (!v) {
Roopa Prabhu37418732015-10-08 10:38:52 -07001189 pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001190 return -EINVAL;
1191 }
stephen hemminger36fd2b62011-04-04 14:03:31 +00001192
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001193 err = __br_fdb_delete(br, p, addr, vid);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001194 } else {
Toshiaki Makita25d3b492015-02-09 20:16:17 +09001195 err = -ENOENT;
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001196 err &= __br_fdb_delete(br, p, addr, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001197 if (!vg || !vg->num_vlans)
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001198 return err;
Vlad Yasevich1690be62013-02-13 12:00:18 +00001199
Roopa Prabhu37418732015-10-08 10:38:52 -07001200 list_for_each_entry(v, &vg->vlan_list, vlist) {
1201 if (!br_vlan_should_use(v))
1202 continue;
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001203 err &= __br_fdb_delete(br, p, addr, v->vid);
Roopa Prabhu37418732015-10-08 10:38:52 -07001204 }
Vlad Yasevich1690be62013-02-13 12:00:18 +00001205 }
Nikolay Aleksandrov5019ab52017-02-13 14:59:11 +01001206
stephen hemminger36fd2b62011-04-04 14:03:31 +00001207 return err;
1208}
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001209
1210int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
1211{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001212 struct net_bridge_fdb_entry *f, *tmp;
Geert Uytterhoeven367dc652018-02-01 11:25:27 +01001213 int err = 0;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001214
1215 ASSERT_RTNL();
1216
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001217 /* the key here is that static entries change only under rtnl */
1218 rcu_read_lock();
1219 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1220 /* We only care for static entries */
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +02001221 if (!test_bit(BR_FDB_STATIC, &f->flags))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001222 continue;
1223 err = dev_uc_add(p->dev, f->key.addr.addr);
1224 if (err)
1225 goto rollback;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001226 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001227done:
1228 rcu_read_unlock();
1229
1230 return err;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001231
1232rollback:
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001233 hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
1234 /* We only care for static entries */
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +02001235 if (!test_bit(BR_FDB_STATIC, &tmp->flags))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001236 continue;
1237 if (tmp == f)
1238 break;
1239 dev_uc_del(p->dev, tmp->key.addr.addr);
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001240 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001241
1242 goto done;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001243}
1244
1245void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
1246{
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001247 struct net_bridge_fdb_entry *f;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001248
1249 ASSERT_RTNL();
1250
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001251 rcu_read_lock();
1252 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1253 /* We only care for static entries */
Nikolay Aleksandrov29e63ff2019-10-29 13:45:54 +02001254 if (!test_bit(BR_FDB_STATIC, &f->flags))
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001255 continue;
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001256
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001257 dev_uc_del(p->dev, f->key.addr.addr);
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001258 }
Nikolay Aleksandroveb793582017-12-12 16:02:50 +02001259 rcu_read_unlock();
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001260}
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001261
Jiri Pirko3aeb6612015-01-15 23:49:37 +01001262int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
Nikolay Aleksandrov45a68782021-08-10 14:00:10 +03001263 const unsigned char *addr, u16 vid,
Petr Machata161d82d2018-05-03 14:43:53 +02001264 bool swdev_notify)
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001265{
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001266 struct net_bridge_fdb_entry *fdb;
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001267 bool modified = false;
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001268 int err = 0;
1269
Roopa Prabhub74fd302017-08-29 13:16:57 -07001270 trace_br_fdb_external_learn_add(br, p, addr, vid);
1271
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001272 spin_lock_bh(&br->hash_lock);
1273
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +01001274 fdb = br_fdb_find(br, addr, vid);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001275 if (!fdb) {
Nikolay Aleksandrov31f11552019-11-01 14:46:38 +02001276 unsigned long flags = BIT(BR_FDB_ADDED_BY_EXT_LEARN);
1277
1278 if (swdev_notify)
1279 flags |= BIT(BR_FDB_ADDED_BY_USER);
Vladimir Oltean0541a622021-08-02 02:17:30 +03001280
Nikolay Aleksandrov45a68782021-08-10 14:00:10 +03001281 if (!p)
Vladimir Oltean0541a622021-08-02 02:17:30 +03001282 flags |= BIT(BR_FDB_LOCAL);
1283
Nikolay Aleksandrov31f11552019-11-01 14:46:38 +02001284 fdb = fdb_create(br, p, addr, vid, flags);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001285 if (!fdb) {
1286 err = -ENOMEM;
1287 goto err_unlock;
1288 }
Petr Machata161d82d2018-05-03 14:43:53 +02001289 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001290 } else {
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001291 fdb->updated = jiffies;
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001292
Vladimir Oltean3e19ae72021-06-29 17:06:44 +03001293 if (READ_ONCE(fdb->dst) != p) {
1294 WRITE_ONCE(fdb->dst, p);
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001295 modified = true;
1296 }
1297
Nikolay Aleksandrovb5cd9f72019-10-29 13:45:57 +02001298 if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) {
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001299 /* Refresh entry */
1300 fdb->used = jiffies;
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +02001301 } else if (!test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags)) {
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001302 /* Take over SW learned entry */
Nikolay Aleksandrovb5cd9f72019-10-29 13:45:57 +02001303 set_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags);
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001304 modified = true;
1305 }
1306
Ido Schimmel710ae722019-01-18 15:58:00 +00001307 if (swdev_notify)
Nikolay Aleksandrovac3ca6a2019-10-29 13:45:56 +02001308 set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
Ido Schimmel710ae722019-01-18 15:58:00 +00001309
Nikolay Aleksandrov45a68782021-08-10 14:00:10 +03001310 if (!p)
Vladimir Oltean0541a622021-08-02 02:17:30 +03001311 set_bit(BR_FDB_LOCAL, &fdb->flags);
1312
Nikolay Aleksandrov7597b262017-07-03 15:14:59 -07001313 if (modified)
Petr Machata161d82d2018-05-03 14:43:53 +02001314 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001315 }
1316
1317err_unlock:
1318 spin_unlock_bh(&br->hash_lock);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001319
1320 return err;
1321}
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001322
Jiri Pirko3aeb6612015-01-15 23:49:37 +01001323int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
Petr Machata161d82d2018-05-03 14:43:53 +02001324 const unsigned char *addr, u16 vid,
1325 bool swdev_notify)
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001326{
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001327 struct net_bridge_fdb_entry *fdb;
1328 int err = 0;
1329
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001330 spin_lock_bh(&br->hash_lock);
1331
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +01001332 fdb = br_fdb_find(br, addr, vid);
Nikolay Aleksandrovb5cd9f72019-10-29 13:45:57 +02001333 if (fdb && test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags))
Petr Machata161d82d2018-05-03 14:43:53 +02001334 fdb_delete(br, fdb, swdev_notify);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001335 else
1336 err = -ENOENT;
1337
1338 spin_unlock_bh(&br->hash_lock);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001339
1340 return err;
1341}
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +02001342
1343void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
Ido Schimmele9ba0fb2018-10-17 08:53:29 +00001344 const unsigned char *addr, u16 vid, bool offloaded)
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +02001345{
1346 struct net_bridge_fdb_entry *fdb;
1347
1348 spin_lock_bh(&br->hash_lock);
1349
1350 fdb = br_fdb_find(br, addr, vid);
Nikolay Aleksandrovd38c6e32019-10-29 13:45:58 +02001351 if (fdb && offloaded != test_bit(BR_FDB_OFFLOADED, &fdb->flags))
1352 change_bit(BR_FDB_OFFLOADED, &fdb->flags);
Arkadi Sharshevsky9fe8bce2017-06-08 08:44:15 +02001353
1354 spin_unlock_bh(&br->hash_lock);
1355}
Petr Machata43920ed2018-12-07 19:55:07 +00001356
1357void br_fdb_clear_offload(const struct net_device *dev, u16 vid)
1358{
1359 struct net_bridge_fdb_entry *f;
1360 struct net_bridge_port *p;
1361
1362 ASSERT_RTNL();
1363
1364 p = br_port_get_rtnl(dev);
1365 if (!p)
1366 return;
1367
1368 spin_lock_bh(&p->br->hash_lock);
1369 hlist_for_each_entry(f, &p->br->fdb_list, fdb_node) {
1370 if (f->dst == p && f->key.vlan_id == vid)
Nikolay Aleksandrovd38c6e32019-10-29 13:45:58 +02001371 clear_bit(BR_FDB_OFFLOADED, &f->flags);
Petr Machata43920ed2018-12-07 19:55:07 +00001372 }
1373 spin_unlock_bh(&p->br->hash_lock);
1374}
1375EXPORT_SYMBOL_GPL(br_fdb_clear_offload);