blob: da5ed7d0a3c8395e7d6ad962f97829ce5bfdaf77 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "br_private.h"
29
Christoph Lametere18b8902006-12-06 20:33:20 -080030static struct kmem_cache *br_fdb_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000032 const unsigned char *addr, u16 vid);
stephen hemminger31e8a49c2011-12-08 07:17:41 +000033static void fdb_notify(struct net_bridge *br,
34 const struct net_bridge_fdb_entry *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Stephen Hemminger3f890922007-03-21 13:42:33 -070036static u32 fdb_salt __read_mostly;
37
Akinobu Mita87a596e2007-04-07 18:57:07 +090038int __init br_fdb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
41 sizeof(struct net_bridge_fdb_entry),
42 0,
Paul Mundt20c2df82007-07-20 10:11:58 +090043 SLAB_HWCACHE_ALIGN, NULL);
Akinobu Mita87a596e2007-04-07 18:57:07 +090044 if (!br_fdb_cache)
45 return -ENOMEM;
46
Stephen Hemminger3f890922007-03-21 13:42:33 -070047 get_random_bytes(&fdb_salt, sizeof(fdb_salt));
Akinobu Mita87a596e2007-04-07 18:57:07 +090048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Andrew Morton73afc902007-12-05 21:35:23 -080051void br_fdb_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 kmem_cache_destroy(br_fdb_cache);
54}
55
56
57/* if topology_changing then use forward_delay (default 15 sec)
58 * otherwise keep longer (default 5 minutes)
59 */
Stephen Hemminger3f890922007-03-21 13:42:33 -070060static inline unsigned long hold_time(const struct net_bridge *br)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 return br->topology_change ? br->forward_delay : br->ageing_time;
63}
64
Stephen Hemminger3f890922007-03-21 13:42:33 -070065static inline int has_expired(const struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 const struct net_bridge_fdb_entry *fdb)
67{
Joe Perchesf64f9e72009-11-29 16:55:45 -080068 return !fdb->is_static &&
stephen hemminger7cd88612011-04-04 14:03:28 +000069 time_before_eq(fdb->updated + hold_time(br), jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
Vlad Yasevich2ba071e2013-02-13 12:00:16 +000072static inline int br_mac_hash(const unsigned char *mac, __u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Vlad Yasevich2ba071e2013-02-13 12:00:16 +000074 /* use 1 byte of OUI and 3 bytes of NIC */
Stephen Hemminger3f890922007-03-21 13:42:33 -070075 u32 key = get_unaligned((u32 *)(mac + 2));
Vlad Yasevich2ba071e2013-02-13 12:00:16 +000076 return jhash_2words(key, vid, fdb_salt) & (BR_HASH_SIZE - 1);
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
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +010086static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head,
87 const unsigned char *addr,
88 __u16 vid)
89{
90 struct net_bridge_fdb_entry *f;
91
Nikolay Aleksandrov410b3d42017-02-13 14:59:10 +010092 WARN_ON_ONCE(!rcu_read_lock_held());
93
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +010094 hlist_for_each_entry_rcu(f, head, hlist)
95 if (ether_addr_equal(f->addr.addr, addr) && f->vlan_id == vid)
96 break;
97
98 return f;
99}
100
101/* requires bridge hash_lock */
102static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br,
103 const unsigned char *addr,
104 __u16 vid)
105{
106 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
107 struct net_bridge_fdb_entry *fdb;
108
Nikolay Aleksandrov410b3d42017-02-13 14:59:10 +0100109 WARN_ON_ONCE(!br_hash_lock_held(br));
110
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100111 rcu_read_lock();
112 fdb = fdb_find_rcu(head, addr, vid);
113 rcu_read_unlock();
114
115 return fdb;
116}
117
118struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
119 const unsigned char *addr,
120 __u16 vid)
121{
122 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
123
124 return fdb_find_rcu(head, addr, vid);
125}
126
Vlad Yasevich145beee2014-05-16 09:59:19 -0400127/* When a static FDB entry is added, the mac address from the entry is
128 * added to the bridge private HW address list and all required ports
129 * are then updated with the new information.
130 * Called under RTNL.
131 */
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100132static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr)
Vlad Yasevich145beee2014-05-16 09:59:19 -0400133{
134 int err;
Li RongQinga3f5ee72014-06-18 16:07:16 +0800135 struct net_bridge_port *p;
Vlad Yasevich145beee2014-05-16 09:59:19 -0400136
137 ASSERT_RTNL();
138
139 list_for_each_entry(p, &br->port_list, list) {
140 if (!br_promisc_port(p)) {
141 err = dev_uc_add(p->dev, addr);
142 if (err)
143 goto undo;
144 }
145 }
146
147 return;
148undo:
Li RongQinga3f5ee72014-06-18 16:07:16 +0800149 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
150 if (!br_promisc_port(p))
151 dev_uc_del(p->dev, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400152 }
153}
154
155/* When a static FDB entry is deleted, the HW address from that entry is
156 * also removed from the bridge private HW address list and updates all
157 * the ports with needed information.
158 * Called under RTNL.
159 */
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100160static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
Vlad Yasevich145beee2014-05-16 09:59:19 -0400161{
162 struct net_bridge_port *p;
163
164 ASSERT_RTNL();
165
166 list_for_each_entry(p, &br->port_list, list) {
167 if (!br_promisc_port(p))
168 dev_uc_del(p->dev, addr);
169 }
170}
171
Scott Feldmanb4ad7ba2015-06-14 11:33:11 -0700172static void fdb_del_external_learn(struct net_bridge_fdb_entry *f)
173{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200174 struct switchdev_obj_port_fdb fdb = {
Jiri Pirko56607382015-10-14 19:40:53 +0200175 .obj = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +0100176 .orig_dev = f->dst->dev,
Jiri Pirko56607382015-10-14 19:40:53 +0200177 .id = SWITCHDEV_OBJ_ID_PORT_FDB,
178 .flags = SWITCHDEV_F_DEFER,
179 },
Vivien Didelotab069002015-09-29 12:07:17 -0400180 .vid = f->vlan_id,
Scott Feldmanb4ad7ba2015-06-14 11:33:11 -0700181 };
182
Jiri Pirko850d0cb2015-10-14 19:40:51 +0200183 ether_addr_copy(fdb.addr, f->addr.addr);
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200184 switchdev_port_obj_del(f->dst->dev, &fdb.obj);
Scott Feldmanb4ad7ba2015-06-14 11:33:11 -0700185}
186
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000187static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Vlad Yasevich145beee2014-05-16 09:59:19 -0400189 if (f->is_static)
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100190 fdb_del_hw_addr(br, f->addr.addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400191
Scott Feldmanb4ad7ba2015-06-14 11:33:11 -0700192 if (f->added_by_external_learn)
193 fdb_del_external_learn(f);
194
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100195 hlist_del_init_rcu(&f->hlist);
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000196 fdb_notify(br, f, RTM_DELNEIGH);
Michał Mirosławda678292009-06-05 05:35:28 +0000197 call_rcu(&f->rcu, fdb_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Toshiaki Makita960b5892014-02-07 16:48:23 +0900200/* Delete a local entry if no other port had the same address. */
201static void fdb_delete_local(struct net_bridge *br,
202 const struct net_bridge_port *p,
203 struct net_bridge_fdb_entry *f)
204{
205 const unsigned char *addr = f->addr.addr;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200206 struct net_bridge_vlan_group *vg;
207 const struct net_bridge_vlan *v;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900208 struct net_bridge_port *op;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200209 u16 vid = f->vlan_id;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900210
211 /* Maybe another port has same hw addr? */
212 list_for_each_entry(op, &br->port_list, list) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200213 vg = nbp_vlan_group(op);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900214 if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200215 (!vid || br_vlan_find(vg, vid))) {
Toshiaki Makita960b5892014-02-07 16:48:23 +0900216 f->dst = op;
Toshiaki Makitaa778e6d2014-02-07 16:48:24 +0900217 f->added_by_user = 0;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900218 return;
219 }
220 }
221
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200222 vg = br_vlan_group(br);
223 v = br_vlan_find(vg, vid);
Toshiaki Makita960b5892014-02-07 16:48:23 +0900224 /* Maybe bridge device has same hw addr? */
225 if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200226 (!vid || (v && br_vlan_should_use(v)))) {
Toshiaki Makita960b5892014-02-07 16:48:23 +0900227 f->dst = NULL;
Toshiaki Makitaa778e6d2014-02-07 16:48:24 +0900228 f->added_by_user = 0;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900229 return;
230 }
231
232 fdb_delete(br, f);
233}
234
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900235void br_fdb_find_delete_local(struct net_bridge *br,
236 const struct net_bridge_port *p,
237 const unsigned char *addr, u16 vid)
238{
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900239 struct net_bridge_fdb_entry *f;
240
241 spin_lock_bh(&br->hash_lock);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100242 f = br_fdb_find(br, addr, vid);
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900243 if (f && f->is_local && !f->added_by_user && f->dst == p)
244 fdb_delete_local(br, p, f);
245 spin_unlock_bh(&br->hash_lock);
246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
249{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200250 struct net_bridge_vlan_group *vg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 struct net_bridge *br = p->br;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200252 struct net_bridge_vlan *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 int i;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 spin_lock_bh(&br->hash_lock);
256
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200257 vg = nbp_vlan_group(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* Search all chains since old address/hash is unknown */
259 for (i = 0; i < BR_HASH_SIZE; i++) {
260 struct hlist_node *h;
261 hlist_for_each(h, &br->hash[i]) {
262 struct net_bridge_fdb_entry *f;
263
264 f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900265 if (f->dst == p && f->is_local && !f->added_by_user) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /* delete old one */
Toshiaki Makita960b5892014-02-07 16:48:23 +0900267 fdb_delete_local(br, p, f);
268
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000269 /* if this port has no vlan information
270 * configured, we can safely be done at
271 * this point.
272 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200273 if (!vg || !vg->num_vlans)
Toshiaki Makita28368822014-02-07 16:48:19 +0900274 goto insert;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276 }
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Toshiaki Makita28368822014-02-07 16:48:19 +0900279insert:
280 /* insert new address, may fail if invalid address or dup. */
281 fdb_insert(br, p, newaddr, 0);
282
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200283 if (!vg || !vg->num_vlans)
Toshiaki Makita28368822014-02-07 16:48:19 +0900284 goto done;
285
286 /* Now add entries for every VLAN configured on the port.
287 * This function runs under RTNL so the bitmap will not change
288 * from under us.
289 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200290 list_for_each_entry(v, &vg->vlan_list, vlist)
291 fdb_insert(br, p, newaddr, v->vid);
Toshiaki Makita28368822014-02-07 16:48:19 +0900292
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000293done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 spin_unlock_bh(&br->hash_lock);
295}
296
stephen hemminger43598812011-12-08 07:17:49 +0000297void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
298{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200299 struct net_bridge_vlan_group *vg;
stephen hemminger43598812011-12-08 07:17:49 +0000300 struct net_bridge_fdb_entry *f;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200301 struct net_bridge_vlan *v;
stephen hemminger43598812011-12-08 07:17:49 +0000302
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900303 spin_lock_bh(&br->hash_lock);
304
stephen hemminger43598812011-12-08 07:17:49 +0000305 /* If old entry was unassociated with any port, then delete it. */
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100306 f = br_fdb_find(br, br->dev->dev_addr, 0);
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900307 if (f && f->is_local && !f->dst && !f->added_by_user)
Toshiaki Makita960b5892014-02-07 16:48:23 +0900308 fdb_delete_local(br, NULL, f);
stephen hemminger43598812011-12-08 07:17:49 +0000309
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000310 fdb_insert(br, NULL, newaddr, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200311 vg = br_vlan_group(br);
312 if (!vg || !vg->num_vlans)
313 goto out;
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000314 /* Now remove and add entries for every VLAN configured on the
315 * bridge. This function runs under RTNL so the bitmap will not
316 * change from under us.
317 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200318 list_for_each_entry(v, &vg->vlan_list, vlist) {
Toshiaki Makita0b148de2016-06-07 19:14:17 +0900319 if (!br_vlan_should_use(v))
320 continue;
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100321 f = br_fdb_find(br, br->dev->dev_addr, v->vid);
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900322 if (f && f->is_local && !f->dst && !f->added_by_user)
Toshiaki Makita960b5892014-02-07 16:48:23 +0900323 fdb_delete_local(br, NULL, f);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200324 fdb_insert(br, NULL, newaddr, v->vid);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000325 }
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900326out:
327 spin_unlock_bh(&br->hash_lock);
stephen hemminger43598812011-12-08 07:17:49 +0000328}
329
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100330void br_fdb_cleanup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100332 struct net_bridge *br = container_of(work, struct net_bridge,
333 gc_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 unsigned long delay = hold_time(br);
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100335 unsigned long work_delay = delay;
336 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int i;
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 for (i = 0; i < BR_HASH_SIZE; i++) {
340 struct net_bridge_fdb_entry *f;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800341 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100343 if (!br->hash[i].first)
344 continue;
345
346 spin_lock_bh(&br->hash_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800347 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
Baruch Even071f7722007-05-31 01:20:45 -0700348 unsigned long this_timer;
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100349
David S. Miller3fcf9012015-02-04 23:52:44 -0800350 if (f->is_static)
Baruch Even071f7722007-05-31 01:20:45 -0700351 continue;
Siva Mannemdcd45e02015-09-23 08:39:19 -0700352 if (f->added_by_external_learn)
353 continue;
stephen hemminger7cd88612011-04-04 14:03:28 +0000354 this_timer = f->updated + delay;
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100355 if (time_after(this_timer, now))
356 work_delay = min(work_delay, this_timer - now);
357 else
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000358 fdb_delete(br, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100360 spin_unlock_bh(&br->hash_lock);
361 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100364 /* Cleanup minimum 10 milliseconds apart */
365 work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10));
366 mod_delayed_work(system_long_wq, &br->gc_work, work_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700369/* Completely flush all dynamic entries in forwarding database.*/
370void br_fdb_flush(struct net_bridge *br)
371{
372 int i;
Stephen Hemminger1a620692006-10-12 14:45:38 -0700373
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700374 spin_lock_bh(&br->hash_lock);
375 for (i = 0; i < BR_HASH_SIZE; i++) {
376 struct net_bridge_fdb_entry *f;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800377 struct hlist_node *n;
378 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700379 if (!f->is_static)
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000380 fdb_delete(br, f);
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700381 }
382 }
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{
395 int i;
396
397 spin_lock_bh(&br->hash_lock);
398 for (i = 0; i < BR_HASH_SIZE; i++) {
399 struct hlist_node *h, *g;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 hlist_for_each_safe(h, g, &br->hash[i]) {
402 struct net_bridge_fdb_entry *f
403 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900404 if (f->dst != p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 continue;
406
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700407 if (!do_all)
408 if (f->is_static || (vid && f->vlan_id != vid))
409 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Toshiaki Makitaa778e6d2014-02-07 16:48:24 +0900411 if (f->is_local)
412 fdb_delete_local(br, p, f);
413 else
414 fdb_delete(br, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416 }
417 spin_unlock_bh(&br->hash_lock);
418}
419
Igor Maraviće6373c42011-12-12 02:58:25 +0000420#if IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +0000421/* Interface used by ATM LANE hook to test
422 * if an addr is on some other bridge port */
423int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct net_bridge_fdb_entry *fdb;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000426 struct net_bridge_port *port;
Michał Mirosławda678292009-06-05 05:35:28 +0000427 int ret;
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 rcu_read_lock();
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000430 port = br_port_get_rcu(dev);
431 if (!port)
432 ret = 0;
433 else {
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100434 fdb = br_fdb_find_rcu(port->br, addr, 0);
stephen hemminger43598812011-12-08 07:17:49 +0000435 ret = fdb && fdb->dst && fdb->dst->dev != dev &&
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000436 fdb->dst->state == BR_STATE_FORWARDING;
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Michał Mirosławda678292009-06-05 05:35:28 +0000440 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
Michał Mirosławda678292009-06-05 05:35:28 +0000442#endif /* CONFIG_ATM_LANE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444/*
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900445 * Fill buffer with forwarding table records in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * the API format.
447 */
448int br_fdb_fillbuf(struct net_bridge *br, void *buf,
449 unsigned long maxnum, unsigned long skip)
450{
451 struct __fdb_entry *fe = buf;
452 int i, num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct net_bridge_fdb_entry *f;
454
455 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
456
457 rcu_read_lock();
458 for (i = 0; i < BR_HASH_SIZE; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800459 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (num >= maxnum)
461 goto out;
462
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900463 if (has_expired(br, f))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 continue;
465
stephen hemminger43598812011-12-08 07:17:49 +0000466 /* ignore pseudo entry for local MAC address */
467 if (!f->dst)
468 continue;
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (skip) {
471 --skip;
472 continue;
473 }
474
475 /* convert from internal format to API */
476 memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
Stephen Hemmingerae4f8fc2008-05-02 16:53:33 -0700477
478 /* due to ABI compat need to split into hi/lo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 fe->port_no = f->dst->port_no;
Stephen Hemmingerae4f8fc2008-05-02 16:53:33 -0700480 fe->port_hi = f->dst->port_no >> 8;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 fe->is_local = f->is_local;
483 if (!f->is_static)
Eric Dumazeta399a802012-08-08 21:13:53 +0000484 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 ++fe;
486 ++num;
487 }
488 }
489
490 out:
491 rcu_read_unlock();
492
493 return num;
494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
497 struct net_bridge_port *source,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000498 const unsigned char *addr,
Roopa Prabhub7af1472015-10-27 07:52:56 -0700499 __u16 vid,
500 unsigned char is_local,
501 unsigned char is_static)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct net_bridge_fdb_entry *fdb;
504
505 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
506 if (fdb) {
507 memcpy(fdb->addr.addr, addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 fdb->dst = source;
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000509 fdb->vlan_id = vid;
Roopa Prabhub7af1472015-10-27 07:52:56 -0700510 fdb->is_local = is_local;
511 fdb->is_static = is_static;
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900512 fdb->added_by_user = 0;
Scott Feldmancf6b8e12014-11-28 14:34:21 +0100513 fdb->added_by_external_learn = 0;
stephen hemminger7cd88612011-04-04 14:03:28 +0000514 fdb->updated = fdb->used = jiffies;
Pavel Emelyanov1158f762011-02-04 13:02:36 -0800515 hlist_add_head_rcu(&fdb->hlist, head);
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{
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000523 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct net_bridge_fdb_entry *fdb;
525
526 if (!is_valid_ether_addr(addr))
527 return -EINVAL;
528
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100529 fdb = br_fdb_find(br, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (fdb) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900531 /* it is okay to have multiple ports with same
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 * address, just use the first one.
533 */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900534 if (fdb->is_local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return 0;
Roopa Prabhude1dfee2016-10-11 16:12:56 -0700536 br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
537 source ? source->dev->name : br->dev->name, addr, vid);
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000538 fdb_delete(br, fdb);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Roopa Prabhub7af1472015-10-27 07:52:56 -0700541 fdb = fdb_create(head, source, addr, vid, 1, 1);
stephen hemminger03e9b642011-04-04 14:03:27 +0000542 if (!fdb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return -ENOMEM;
544
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100545 fdb_add_hw_addr(br, addr);
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000546 fdb_notify(br, fdb, RTM_NEWNEIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return 0;
548}
549
stephen hemminger03e9b642011-04-04 14:03:27 +0000550/* Add entry for local address of interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000552 const unsigned char *addr, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 int ret;
555
556 spin_lock_bh(&br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000557 ret = fdb_insert(br, source, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 spin_unlock_bh(&br->hash_lock);
559 return ret;
560}
561
562void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900563 const unsigned char *addr, u16 vid, bool added_by_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000565 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 struct net_bridge_fdb_entry *fdb;
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000567 bool fdb_modified = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* some users want to always flood. */
570 if (hold_time(br) == 0)
571 return;
572
Stephen Hemmingerdf1c0b82007-08-30 22:15:35 -0700573 /* ignore packets unless we are using this port */
574 if (!(source->state == BR_STATE_LEARNING ||
575 source->state == BR_STATE_FORWARDING))
576 return;
577
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000578 fdb = fdb_find_rcu(head, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (likely(fdb)) {
580 /* attempt to update an entry for a local interface */
581 if (unlikely(fdb->is_local)) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900582 if (net_ratelimit())
Roopa Prabhude1dfee2016-10-11 16:12:56 -0700583 br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
584 source->dev->name, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 } else {
stephen hemmingerca6d4482017-02-07 08:46:46 -0800586 unsigned long now = jiffies;
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* fastpath: update of existing entry */
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000589 if (unlikely(source != fdb->dst)) {
590 fdb->dst = source;
591 fdb_modified = true;
592 }
stephen hemmingerca6d4482017-02-07 08:46:46 -0800593 if (now != fdb->updated)
594 fdb->updated = now;
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900595 if (unlikely(added_by_user))
596 fdb->added_by_user = 1;
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000597 if (unlikely(fdb_modified))
598 fdb_notify(br, fdb, RTM_NEWNEIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600 } else {
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800601 spin_lock(&br->hash_lock);
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100602 if (likely(!fdb_find_rcu(head, addr, vid))) {
Roopa Prabhub7af1472015-10-27 07:52:56 -0700603 fdb = fdb_create(head, source, addr, vid, 0, 0);
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900604 if (fdb) {
605 if (unlikely(added_by_user))
606 fdb->added_by_user = 1;
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000607 fdb_notify(br, fdb, RTM_NEWNEIGH);
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900608 }
stephen hemmingerf58ee4e2011-12-06 13:02:24 +0000609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* else we lose race and someone else inserts
611 * it first, don't bother updating
612 */
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800613 spin_unlock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000616
Roopa Prabhu37418732015-10-08 10:38:52 -0700617static int fdb_to_nud(const struct net_bridge *br,
618 const struct net_bridge_fdb_entry *fdb)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000619{
620 if (fdb->is_local)
621 return NUD_PERMANENT;
622 else if (fdb->is_static)
623 return NUD_NOARP;
Roopa Prabhu37418732015-10-08 10:38:52 -0700624 else if (has_expired(br, fdb))
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000625 return NUD_STALE;
626 else
627 return NUD_REACHABLE;
628}
629
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000630static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000631 const struct net_bridge_fdb_entry *fdb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000632 u32 portid, u32 seq, int type, unsigned int flags)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000633{
634 unsigned long now = jiffies;
635 struct nda_cacheinfo ci;
636 struct nlmsghdr *nlh;
637 struct ndmsg *ndm;
638
Eric W. Biederman15e47302012-09-07 20:12:54 +0000639 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000640 if (nlh == NULL)
641 return -EMSGSIZE;
642
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000643 ndm = nlmsg_data(nlh);
644 ndm->ndm_family = AF_BRIDGE;
645 ndm->ndm_pad1 = 0;
646 ndm->ndm_pad2 = 0;
Scott Feldmancf6b8e12014-11-28 14:34:21 +0100647 ndm->ndm_flags = fdb->added_by_external_learn ? NTF_EXT_LEARNED : 0;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000648 ndm->ndm_type = 0;
stephen hemminger43598812011-12-08 07:17:49 +0000649 ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
Roopa Prabhu37418732015-10-08 10:38:52 -0700650 ndm->ndm_state = fdb_to_nud(br, fdb);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000651
David S. Miller2eb812e2012-04-01 20:49:54 -0400652 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr))
653 goto nla_put_failure;
Roopa Prabhu41c389d2014-05-27 22:39:37 -0700654 if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
655 goto nla_put_failure;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000656 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
657 ci.ndm_confirmed = 0;
658 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
659 ci.ndm_refcnt = 0;
David S. Miller2eb812e2012-04-01 20:49:54 -0400660 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
661 goto nla_put_failure;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000662
Toshiaki Makita47fab412014-07-30 13:31:51 +0900663 if (fdb->vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16), &fdb->vlan_id))
Vlad Yasevich1690be62013-02-13 12:00:18 +0000664 goto nla_put_failure;
665
Johannes Berg053c0952015-01-16 22:09:00 +0100666 nlmsg_end(skb, nlh);
667 return 0;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000668
669nla_put_failure:
670 nlmsg_cancel(skb, nlh);
671 return -EMSGSIZE;
672}
673
674static inline size_t fdb_nlmsg_size(void)
675{
676 return NLMSG_ALIGN(sizeof(struct ndmsg))
677 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Roopa Prabhu41c389d2014-05-27 22:39:37 -0700678 + nla_total_size(sizeof(u32)) /* NDA_MASTER */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000679 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000680 + nla_total_size(sizeof(struct nda_cacheinfo));
681}
682
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000683static void fdb_notify(struct net_bridge *br,
684 const struct net_bridge_fdb_entry *fdb, int type)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000685{
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000686 struct net *net = dev_net(br->dev);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000687 struct sk_buff *skb;
688 int err = -ENOBUFS;
689
690 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
691 if (skb == NULL)
692 goto errout;
693
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000694 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000695 if (err < 0) {
696 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
697 WARN_ON(err == -EMSGSIZE);
698 kfree_skb(skb);
699 goto errout;
700 }
701 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
702 return;
703errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800704 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000705}
706
707/* Dump information about entries, in response to GETNEIGH */
John Fastabend77162022012-04-15 06:43:56 +0000708int br_fdb_dump(struct sk_buff *skb,
709 struct netlink_callback *cb,
710 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400711 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -0700712 int *idx)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000713{
John Fastabend77162022012-04-15 06:43:56 +0000714 struct net_bridge *br = netdev_priv(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -0700715 int err = 0;
John Fastabend77162022012-04-15 06:43:56 +0000716 int i;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000717
John Fastabend77162022012-04-15 06:43:56 +0000718 if (!(dev->priv_flags & IFF_EBRIDGE))
719 goto out;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000720
Roopa Prabhud2976532016-08-30 21:56:45 -0700721 if (!filter_dev) {
722 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
723 if (err < 0)
724 goto out;
725 }
Hubert Sokolowski6cb69742015-01-05 17:29:21 +0000726
John Fastabend77162022012-04-15 06:43:56 +0000727 for (i = 0; i < BR_HASH_SIZE; i++) {
John Fastabend77162022012-04-15 06:43:56 +0000728 struct net_bridge_fdb_entry *f;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000729
Sasha Levinb67bfe02013-02-27 17:06:00 -0800730 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +0900731
Roopa Prabhud2976532016-08-30 21:56:45 -0700732 if (*idx < cb->args[2])
John Fastabend77162022012-04-15 06:43:56 +0000733 goto skip;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000734
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -0400735 if (filter_dev &&
736 (!f->dst || f->dst->dev != filter_dev)) {
737 if (filter_dev != dev)
738 goto skip;
Hubert Sokolowski6cb69742015-01-05 17:29:21 +0000739 /* !f->dst is a special case for bridge
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -0400740 * It means the MAC belongs to the bridge
741 * Therefore need a little more filtering
742 * we only want to dump the !f->dst case
743 */
744 if (f->dst)
745 goto skip;
746 }
Hubert Sokolowski6cb69742015-01-05 17:29:21 +0000747 if (!filter_dev && f->dst)
748 goto skip;
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400749
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +0900750 err = fdb_fill_info(skb, br, f,
751 NETLINK_CB(cb->skb).portid,
752 cb->nlh->nlmsg_seq,
753 RTM_NEWNEIGH,
754 NLM_F_MULTI);
Roopa Prabhud2976532016-08-30 21:56:45 -0700755 if (err < 0)
756 goto out;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000757skip:
Roopa Prabhud2976532016-08-30 21:56:45 -0700758 *idx += 1;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000759 }
760 }
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000761
John Fastabend77162022012-04-15 06:43:56 +0000762out:
Roopa Prabhud2976532016-08-30 21:56:45 -0700763 return err;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000764}
stephen hemminger36fd2b62011-04-04 14:03:31 +0000765
stephen hemminger292d1392011-11-09 18:30:08 +0000766/* Update (create or replace) forwarding database entry */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900767static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
768 const __u8 *addr, __u16 state, __u16 flags, __u16 vid)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000769{
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000770 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
stephen hemminger36fd2b62011-04-04 14:03:31 +0000771 struct net_bridge_fdb_entry *fdb;
roopab0a397f2013-04-22 12:56:49 +0000772 bool modified = false;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000773
Wilson Kokeb8d7ba2015-05-25 06:39:31 -0700774 /* If the port cannot learn allow only local and static entries */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900775 if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
Wilson Kokeb8d7ba2015-05-25 06:39:31 -0700776 !(source->state == BR_STATE_LEARNING ||
777 source->state == BR_STATE_FORWARDING))
778 return -EPERM;
779
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900780 if (!source && !(state & NUD_PERMANENT)) {
781 pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
782 br->dev->name);
783 return -EINVAL;
784 }
785
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100786 fdb = br_fdb_find(br, addr, vid);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000787 if (fdb == NULL) {
788 if (!(flags & NLM_F_CREATE))
789 return -ENOENT;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000790
Roopa Prabhub7af1472015-10-27 07:52:56 -0700791 fdb = fdb_create(head, source, addr, vid, 0, 0);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000792 if (!fdb)
793 return -ENOMEM;
roopab0a397f2013-04-22 12:56:49 +0000794
795 modified = true;
stephen hemminger64af1ba2011-09-30 14:37:27 +0000796 } else {
797 if (flags & NLM_F_EXCL)
798 return -EEXIST;
roopab0a397f2013-04-22 12:56:49 +0000799
800 if (fdb->dst != source) {
801 fdb->dst = source;
802 modified = true;
803 }
stephen hemminger64af1ba2011-09-30 14:37:27 +0000804 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000805
Roopa Prabhu37418732015-10-08 10:38:52 -0700806 if (fdb_to_nud(br, fdb) != state) {
Vlad Yasevich145beee2014-05-16 09:59:19 -0400807 if (state & NUD_PERMANENT) {
808 fdb->is_local = 1;
809 if (!fdb->is_static) {
810 fdb->is_static = 1;
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100811 fdb_add_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400812 }
813 } else if (state & NUD_NOARP) {
stephen hemminger292d1392011-11-09 18:30:08 +0000814 fdb->is_local = 0;
Vlad Yasevich145beee2014-05-16 09:59:19 -0400815 if (!fdb->is_static) {
816 fdb->is_static = 1;
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100817 fdb_add_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400818 }
819 } else {
820 fdb->is_local = 0;
821 if (fdb->is_static) {
822 fdb->is_static = 0;
Jiri Pirko020ec6b2014-11-28 14:34:12 +0100823 fdb_del_hw_addr(br, addr);
Vlad Yasevich145beee2014-05-16 09:59:19 -0400824 }
825 }
stephen hemminger292d1392011-11-09 18:30:08 +0000826
roopab0a397f2013-04-22 12:56:49 +0000827 modified = true;
828 }
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900829 fdb->added_by_user = 1;
roopab0a397f2013-04-22 12:56:49 +0000830
831 fdb->used = jiffies;
832 if (modified) {
833 fdb->updated = jiffies;
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000834 fdb_notify(br, fdb, RTM_NEWNEIGH);
stephen hemminger292d1392011-11-09 18:30:08 +0000835 }
836
stephen hemminger36fd2b62011-04-04 14:03:31 +0000837 return 0;
838}
839
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900840static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br,
841 struct net_bridge_port *p, const unsigned char *addr,
842 u16 nlh_flags, u16 vid)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000843{
844 int err = 0;
845
846 if (ndm->ndm_flags & NTF_USE) {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900847 if (!p) {
848 pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
849 br->dev->name);
850 return -EINVAL;
851 }
Nikolay Aleksandrovc4c832f2015-06-06 06:49:00 -0700852 local_bh_disable();
Vlad Yasevich1690be62013-02-13 12:00:18 +0000853 rcu_read_lock();
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900854 br_fdb_update(br, p, addr, vid, true);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000855 rcu_read_unlock();
Nikolay Aleksandrovc4c832f2015-06-06 06:49:00 -0700856 local_bh_enable();
Vlad Yasevich1690be62013-02-13 12:00:18 +0000857 } else {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900858 spin_lock_bh(&br->hash_lock);
859 err = fdb_add_entry(br, p, addr, ndm->ndm_state,
Vlad Yasevich1690be62013-02-13 12:00:18 +0000860 nlh_flags, vid);
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900861 spin_unlock_bh(&br->hash_lock);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000862 }
863
864 return err;
865}
866
stephen hemminger36fd2b62011-04-04 14:03:31 +0000867/* Add new permanent fdb entry with RTM_NEWNEIGH */
stephen hemmingeredc7d572012-10-01 12:32:33 +0000868int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
869 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100870 const unsigned char *addr, u16 vid, u16 nlh_flags)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000871{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200872 struct net_bridge_vlan_group *vg;
Roopa Prabhu37418732015-10-08 10:38:52 -0700873 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200874 struct net_bridge_vlan *v;
Roopa Prabhu37418732015-10-08 10:38:52 -0700875 struct net_bridge *br = NULL;
John Fastabend77162022012-04-15 06:43:56 +0000876 int err = 0;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000877
stephen hemminger292d1392011-11-09 18:30:08 +0000878 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
879 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
880 return -EINVAL;
881 }
882
Stephen Hemminger537f7f82013-06-25 09:34:36 -0700883 if (is_zero_ether_addr(addr)) {
884 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
885 return -EINVAL;
886 }
887
Roopa Prabhu37418732015-10-08 10:38:52 -0700888 if (dev->priv_flags & IFF_EBRIDGE) {
889 br = netdev_priv(dev);
890 vg = br_vlan_group(br);
891 } else {
892 p = br_port_get_rtnl(dev);
893 if (!p) {
894 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
895 dev->name);
896 return -EINVAL;
897 }
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900898 br = p->br;
Roopa Prabhu37418732015-10-08 10:38:52 -0700899 vg = nbp_vlan_group(p);
stephen hemminger36fd2b62011-04-04 14:03:31 +0000900 }
901
Jiri Pirkof6f64242014-11-28 14:34:15 +0100902 if (vid) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200903 v = br_vlan_find(vg, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700904 if (!v || !br_vlan_should_use(v)) {
905 pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000906 return -EINVAL;
907 }
908
909 /* VID was specified, so use it. */
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900910 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid);
stephen hemminger292d1392011-11-09 18:30:08 +0000911 } else {
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900912 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200913 if (err || !vg || !vg->num_vlans)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000914 goto out;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000915
916 /* We have vlans configured on this port and user didn't
917 * specify a VLAN. To be nice, add/update entry for every
918 * vlan on this port.
919 */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200920 list_for_each_entry(v, &vg->vlan_list, vlist) {
Roopa Prabhu37418732015-10-08 10:38:52 -0700921 if (!br_vlan_should_use(v))
922 continue;
Toshiaki Makita7bb90c32016-08-04 11:11:19 +0900923 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000924 if (err)
925 goto out;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000926 }
stephen hemminger292d1392011-11-09 18:30:08 +0000927 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000928
Vlad Yasevich1690be62013-02-13 12:00:18 +0000929out:
930 return err;
931}
932
Roopa Prabhu37418732015-10-08 10:38:52 -0700933static int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr,
934 u16 vid)
935{
Roopa Prabhu37418732015-10-08 10:38:52 -0700936 struct net_bridge_fdb_entry *fdb;
937
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100938 fdb = br_fdb_find(br, addr, vid);
Roopa Prabhu37418732015-10-08 10:38:52 -0700939 if (!fdb)
940 return -ENOENT;
941
942 fdb_delete(br, fdb);
943 return 0;
944}
945
946static int __br_fdb_delete_by_addr(struct net_bridge *br,
947 const unsigned char *addr, u16 vid)
948{
949 int err;
950
951 spin_lock_bh(&br->hash_lock);
952 err = fdb_delete_by_addr(br, addr, vid);
953 spin_unlock_bh(&br->hash_lock);
954
955 return err;
956}
957
Nikolay Aleksandrov8c86f962015-06-09 03:34:13 -0700958static int fdb_delete_by_addr_and_port(struct net_bridge_port *p,
959 const u8 *addr, u16 vlan)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000960{
Nikolay Aleksandrov8c86f962015-06-09 03:34:13 -0700961 struct net_bridge *br = p->br;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000962 struct net_bridge_fdb_entry *fdb;
963
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +0100964 fdb = br_fdb_find(br, addr, vlan);
Nikolay Aleksandrov8c86f962015-06-09 03:34:13 -0700965 if (!fdb || fdb->dst != p)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000966 return -ENOENT;
967
968 fdb_delete(br, fdb);
969 return 0;
970}
971
972static int __br_fdb_delete(struct net_bridge_port *p,
973 const unsigned char *addr, u16 vid)
974{
975 int err;
976
977 spin_lock_bh(&p->br->hash_lock);
Nikolay Aleksandrov8c86f962015-06-09 03:34:13 -0700978 err = fdb_delete_by_addr_and_port(p, addr, vid);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000979 spin_unlock_bh(&p->br->hash_lock);
980
stephen hemminger36fd2b62011-04-04 14:03:31 +0000981 return err;
982}
983
stephen hemminger36fd2b62011-04-04 14:03:31 +0000984/* Remove neighbor entry with RTM_DELNEIGH */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000985int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
986 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100987 const unsigned char *addr, u16 vid)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000988{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200989 struct net_bridge_vlan_group *vg;
Roopa Prabhu37418732015-10-08 10:38:52 -0700990 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200991 struct net_bridge_vlan *v;
Roopa Prabhu37418732015-10-08 10:38:52 -0700992 struct net_bridge *br = NULL;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000993 int err;
994
Roopa Prabhu37418732015-10-08 10:38:52 -0700995 if (dev->priv_flags & IFF_EBRIDGE) {
996 br = netdev_priv(dev);
997 vg = br_vlan_group(br);
998 } else {
999 p = br_port_get_rtnl(dev);
1000 if (!p) {
1001 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
1002 dev->name);
1003 return -EINVAL;
1004 }
1005 vg = nbp_vlan_group(p);
stephen hemminger36fd2b62011-04-04 14:03:31 +00001006 }
1007
Jiri Pirkof6f64242014-11-28 14:34:15 +01001008 if (vid) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001009 v = br_vlan_find(vg, vid);
1010 if (!v) {
Roopa Prabhu37418732015-10-08 10:38:52 -07001011 pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001012 return -EINVAL;
1013 }
stephen hemminger36fd2b62011-04-04 14:03:31 +00001014
Roopa Prabhu37418732015-10-08 10:38:52 -07001015 if (dev->priv_flags & IFF_EBRIDGE)
1016 err = __br_fdb_delete_by_addr(br, addr, vid);
1017 else
1018 err = __br_fdb_delete(p, addr, vid);
Vlad Yasevich1690be62013-02-13 12:00:18 +00001019 } else {
Toshiaki Makita25d3b492015-02-09 20:16:17 +09001020 err = -ENOENT;
Roopa Prabhu37418732015-10-08 10:38:52 -07001021 if (dev->priv_flags & IFF_EBRIDGE)
1022 err = __br_fdb_delete_by_addr(br, addr, 0);
1023 else
1024 err &= __br_fdb_delete(p, addr, 0);
1025
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001026 if (!vg || !vg->num_vlans)
Vlad Yasevich1690be62013-02-13 12:00:18 +00001027 goto out;
Vlad Yasevich1690be62013-02-13 12:00:18 +00001028
Roopa Prabhu37418732015-10-08 10:38:52 -07001029 list_for_each_entry(v, &vg->vlan_list, vlist) {
1030 if (!br_vlan_should_use(v))
1031 continue;
1032 if (dev->priv_flags & IFF_EBRIDGE)
1033 err = __br_fdb_delete_by_addr(br, addr, v->vid);
1034 else
1035 err &= __br_fdb_delete(p, addr, v->vid);
1036 }
Vlad Yasevich1690be62013-02-13 12:00:18 +00001037 }
1038out:
stephen hemminger36fd2b62011-04-04 14:03:31 +00001039 return err;
1040}
Vlad Yasevich8db24af2014-05-16 09:59:17 -04001041
1042int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
1043{
1044 struct net_bridge_fdb_entry *fdb, *tmp;
1045 int i;
1046 int err;
1047
1048 ASSERT_RTNL();
1049
1050 for (i = 0; i < BR_HASH_SIZE; i++) {
1051 hlist_for_each_entry(fdb, &br->hash[i], hlist) {
1052 /* We only care for static entries */
1053 if (!fdb->is_static)
1054 continue;
1055
1056 err = dev_uc_add(p->dev, fdb->addr.addr);
1057 if (err)
1058 goto rollback;
1059 }
1060 }
1061 return 0;
1062
1063rollback:
1064 for (i = 0; i < BR_HASH_SIZE; i++) {
1065 hlist_for_each_entry(tmp, &br->hash[i], hlist) {
1066 /* If we reached the fdb that failed, we can stop */
1067 if (tmp == fdb)
1068 break;
1069
1070 /* We only care for static entries */
1071 if (!tmp->is_static)
1072 continue;
1073
1074 dev_uc_del(p->dev, tmp->addr.addr);
1075 }
1076 }
1077 return err;
1078}
1079
1080void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
1081{
1082 struct net_bridge_fdb_entry *fdb;
1083 int i;
1084
1085 ASSERT_RTNL();
1086
1087 for (i = 0; i < BR_HASH_SIZE; i++) {
1088 hlist_for_each_entry_rcu(fdb, &br->hash[i], hlist) {
1089 /* We only care for static entries */
1090 if (!fdb->is_static)
1091 continue;
1092
1093 dev_uc_del(p->dev, fdb->addr.addr);
1094 }
1095 }
1096}
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001097
Jiri Pirko3aeb6612015-01-15 23:49:37 +01001098int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001099 const unsigned char *addr, u16 vid)
1100{
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001101 struct hlist_head *head;
1102 struct net_bridge_fdb_entry *fdb;
1103 int err = 0;
1104
Jiri Pirko3aeb6612015-01-15 23:49:37 +01001105 ASSERT_RTNL();
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001106 spin_lock_bh(&br->hash_lock);
1107
1108 head = &br->hash[br_mac_hash(addr, vid)];
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +01001109 fdb = br_fdb_find(br, addr, vid);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001110 if (!fdb) {
Roopa Prabhub7af1472015-10-27 07:52:56 -07001111 fdb = fdb_create(head, p, addr, vid, 0, 0);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001112 if (!fdb) {
1113 err = -ENOMEM;
1114 goto err_unlock;
1115 }
1116 fdb->added_by_external_learn = 1;
1117 fdb_notify(br, fdb, RTM_NEWNEIGH);
1118 } else if (fdb->added_by_external_learn) {
1119 /* Refresh entry */
1120 fdb->updated = fdb->used = jiffies;
1121 } else if (!fdb->added_by_user) {
1122 /* Take over SW learned entry */
1123 fdb->added_by_external_learn = 1;
1124 fdb->updated = jiffies;
1125 fdb_notify(br, fdb, RTM_NEWNEIGH);
1126 }
1127
1128err_unlock:
1129 spin_unlock_bh(&br->hash_lock);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001130
1131 return err;
1132}
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001133
Jiri Pirko3aeb6612015-01-15 23:49:37 +01001134int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001135 const unsigned char *addr, u16 vid)
1136{
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001137 struct net_bridge_fdb_entry *fdb;
1138 int err = 0;
1139
Jiri Pirko3aeb6612015-01-15 23:49:37 +01001140 ASSERT_RTNL();
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001141 spin_lock_bh(&br->hash_lock);
1142
Nikolay Aleksandrovbfd0aea2017-02-13 14:59:09 +01001143 fdb = br_fdb_find(br, addr, vid);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001144 if (fdb && fdb->added_by_external_learn)
1145 fdb_delete(br, fdb);
1146 else
1147 err = -ENOENT;
1148
1149 spin_unlock_bh(&br->hash_lock);
Scott Feldmancf6b8e12014-11-28 14:34:21 +01001150
1151 return err;
1152}