xref: /openbmc/linux/net/bridge/br_fdb.c (revision 05ea0424)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Forwarding database
4  *	Linux ethernet bridge
5  *
6  *	Authors:
7  *	Lennert Buytenhek		<buytenh@gnu.org>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/rculist.h>
13 #include <linux/spinlock.h>
14 #include <linux/times.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/jhash.h>
18 #include <linux/random.h>
19 #include <linux/slab.h>
20 #include <linux/atomic.h>
21 #include <asm/unaligned.h>
22 #include <linux/if_vlan.h>
23 #include <net/switchdev.h>
24 #include <trace/events/bridge.h>
25 #include "br_private.h"
26 
27 static 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,
32 };
33 
34 static struct kmem_cache *br_fdb_cache __read_mostly;
35 
36 int __init br_fdb_init(void)
37 {
38 	br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
39 					 sizeof(struct net_bridge_fdb_entry),
40 					 0,
41 					 SLAB_HWCACHE_ALIGN, NULL);
42 	if (!br_fdb_cache)
43 		return -ENOMEM;
44 
45 	return 0;
46 }
47 
48 void br_fdb_fini(void)
49 {
50 	kmem_cache_destroy(br_fdb_cache);
51 }
52 
53 int br_fdb_hash_init(struct net_bridge *br)
54 {
55 	return rhashtable_init(&br->fdb_hash_tbl, &br_fdb_rht_params);
56 }
57 
58 void br_fdb_hash_fini(struct net_bridge *br)
59 {
60 	rhashtable_destroy(&br->fdb_hash_tbl);
61 }
62 
63 /* if topology_changing then use forward_delay (default 15 sec)
64  * otherwise keep longer (default 5 minutes)
65  */
66 static inline unsigned long hold_time(const struct net_bridge *br)
67 {
68 	return br->topology_change ? br->forward_delay : br->ageing_time;
69 }
70 
71 static inline int has_expired(const struct net_bridge *br,
72 				  const struct net_bridge_fdb_entry *fdb)
73 {
74 	return !test_bit(BR_FDB_STATIC, &fdb->flags) &&
75 	       !test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags) &&
76 	       time_before_eq(fdb->updated + hold_time(br), jiffies);
77 }
78 
79 static 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 
86 static 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 
99 static 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 
164 nla_put_failure:
165 	nlmsg_cancel(skb, nlh);
166 	return -EMSGSIZE;
167 }
168 
169 static 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 
180 static 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;
204 errout:
205 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
206 }
207 
208 static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl,
209 						 const unsigned char *addr,
210 						 __u16 vid)
211 {
212 	struct net_bridge_fdb_key key;
213 
214 	WARN_ON_ONCE(!rcu_read_lock_held());
215 
216 	key.vlan_id = vid;
217 	memcpy(key.addr.addr, addr, sizeof(key.addr.addr));
218 
219 	return rhashtable_lookup(tbl, &key, br_fdb_rht_params);
220 }
221 
222 /* requires bridge hash_lock */
223 static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br,
224 						const unsigned char *addr,
225 						__u16 vid)
226 {
227 	struct net_bridge_fdb_entry *fdb;
228 
229 	lockdep_assert_held_once(&br->hash_lock);
230 
231 	rcu_read_lock();
232 	fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
233 	rcu_read_unlock();
234 
235 	return fdb;
236 }
237 
238 struct 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);
252 	rcu_read_lock();
253 	f = br_fdb_find_rcu(br, addr, vid);
254 	if (f && f->dst)
255 		dev = f->dst->dev;
256 	rcu_read_unlock();
257 
258 	return dev;
259 }
260 EXPORT_SYMBOL_GPL(br_fdb_find_port);
261 
262 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
263 					     const unsigned char *addr,
264 					     __u16 vid)
265 {
266 	return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
267 }
268 
269 /* 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  */
274 static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr)
275 {
276 	int err;
277 	struct net_bridge_port *p;
278 
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;
290 undo:
291 	list_for_each_entry_continue_reverse(p, &br->port_list, list) {
292 		if (!br_promisc_port(p))
293 			dev_uc_del(p->dev, addr);
294 	}
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  */
302 static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
303 {
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 
314 static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
315 		       bool swdev_notify)
316 {
317 	trace_fdb_delete(br, f);
318 
319 	if (test_bit(BR_FDB_STATIC, &f->flags))
320 		fdb_del_hw_addr(br, f->key.addr.addr);
321 
322 	hlist_del_init_rcu(&f->fdb_node);
323 	rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode,
324 			       br_fdb_rht_params);
325 	fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
326 	call_rcu(&f->rcu, fdb_rcu_free);
327 }
328 
329 /* Delete a local entry if no other port had the same address. */
330 static void fdb_delete_local(struct net_bridge *br,
331 			     const struct net_bridge_port *p,
332 			     struct net_bridge_fdb_entry *f)
333 {
334 	const unsigned char *addr = f->key.addr.addr;
335 	struct net_bridge_vlan_group *vg;
336 	const struct net_bridge_vlan *v;
337 	struct net_bridge_port *op;
338 	u16 vid = f->key.vlan_id;
339 
340 	/* Maybe another port has same hw addr? */
341 	list_for_each_entry(op, &br->port_list, list) {
342 		vg = nbp_vlan_group(op);
343 		if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
344 		    (!vid || br_vlan_find(vg, vid))) {
345 			f->dst = op;
346 			clear_bit(BR_FDB_ADDED_BY_USER, &f->flags);
347 			return;
348 		}
349 	}
350 
351 	vg = br_vlan_group(br);
352 	v = br_vlan_find(vg, vid);
353 	/* Maybe bridge device has same hw addr? */
354 	if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
355 	    (!vid || (v && br_vlan_should_use(v)))) {
356 		f->dst = NULL;
357 		clear_bit(BR_FDB_ADDED_BY_USER, &f->flags);
358 		return;
359 	}
360 
361 	fdb_delete(br, f, true);
362 }
363 
364 void br_fdb_find_delete_local(struct net_bridge *br,
365 			      const struct net_bridge_port *p,
366 			      const unsigned char *addr, u16 vid)
367 {
368 	struct net_bridge_fdb_entry *f;
369 
370 	spin_lock_bh(&br->hash_lock);
371 	f = br_fdb_find(br, addr, vid);
372 	if (f && test_bit(BR_FDB_LOCAL, &f->flags) &&
373 	    !test_bit(BR_FDB_ADDED_BY_USER, &f->flags) && f->dst == p)
374 		fdb_delete_local(br, p, f);
375 	spin_unlock_bh(&br->hash_lock);
376 }
377 
378 static 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 	int err;
386 
387 	fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
388 	if (!fdb)
389 		return NULL;
390 
391 	memcpy(fdb->key.addr.addr, addr, ETH_ALEN);
392 	WRITE_ONCE(fdb->dst, source);
393 	fdb->key.vlan_id = vid;
394 	fdb->flags = flags;
395 	fdb->updated = fdb->used = jiffies;
396 	err = rhashtable_lookup_insert_fast(&br->fdb_hash_tbl, &fdb->rhnode,
397 					    br_fdb_rht_params);
398 	if (err) {
399 		kmem_cache_free(br_fdb_cache, fdb);
400 		return NULL;
401 	}
402 
403 	hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list);
404 
405 	return fdb;
406 }
407 
408 static int fdb_add_local(struct net_bridge *br, struct net_bridge_port *source,
409 			 const unsigned char *addr, u16 vid)
410 {
411 	struct net_bridge_fdb_entry *fdb;
412 
413 	if (!is_valid_ether_addr(addr))
414 		return -EINVAL;
415 
416 	fdb = br_fdb_find(br, addr, vid);
417 	if (fdb) {
418 		/* it is okay to have multiple ports with same
419 		 * address, just use the first one.
420 		 */
421 		if (test_bit(BR_FDB_LOCAL, &fdb->flags))
422 			return 0;
423 		br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
424 			source ? source->dev->name : br->dev->name, addr, vid);
425 		fdb_delete(br, fdb, true);
426 	}
427 
428 	fdb = fdb_create(br, source, addr, vid,
429 			 BIT(BR_FDB_LOCAL) | BIT(BR_FDB_STATIC));
430 	if (!fdb)
431 		return -ENOMEM;
432 
433 	fdb_add_hw_addr(br, addr);
434 	fdb_notify(br, fdb, RTM_NEWNEIGH, true);
435 	return 0;
436 }
437 
438 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
439 {
440 	struct net_bridge_vlan_group *vg;
441 	struct net_bridge_fdb_entry *f;
442 	struct net_bridge *br = p->br;
443 	struct net_bridge_vlan *v;
444 
445 	spin_lock_bh(&br->hash_lock);
446 	vg = nbp_vlan_group(p);
447 	hlist_for_each_entry(f, &br->fdb_list, fdb_node) {
448 		if (f->dst == p && test_bit(BR_FDB_LOCAL, &f->flags) &&
449 		    !test_bit(BR_FDB_ADDED_BY_USER, &f->flags)) {
450 			/* delete old one */
451 			fdb_delete_local(br, p, f);
452 
453 			/* if this port has no vlan information
454 			 * configured, we can safely be done at
455 			 * this point.
456 			 */
457 			if (!vg || !vg->num_vlans)
458 				goto insert;
459 		}
460 	}
461 
462 insert:
463 	/* insert new address,  may fail if invalid address or dup. */
464 	fdb_add_local(br, p, newaddr, 0);
465 
466 	if (!vg || !vg->num_vlans)
467 		goto done;
468 
469 	/* Now add entries for every VLAN configured on the port.
470 	 * This function runs under RTNL so the bitmap will not change
471 	 * from under us.
472 	 */
473 	list_for_each_entry(v, &vg->vlan_list, vlist)
474 		fdb_add_local(br, p, newaddr, v->vid);
475 
476 done:
477 	spin_unlock_bh(&br->hash_lock);
478 }
479 
480 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
481 {
482 	struct net_bridge_vlan_group *vg;
483 	struct net_bridge_fdb_entry *f;
484 	struct net_bridge_vlan *v;
485 
486 	spin_lock_bh(&br->hash_lock);
487 
488 	/* If old entry was unassociated with any port, then delete it. */
489 	f = br_fdb_find(br, br->dev->dev_addr, 0);
490 	if (f && test_bit(BR_FDB_LOCAL, &f->flags) &&
491 	    !f->dst && !test_bit(BR_FDB_ADDED_BY_USER, &f->flags))
492 		fdb_delete_local(br, NULL, f);
493 
494 	fdb_add_local(br, NULL, newaddr, 0);
495 	vg = br_vlan_group(br);
496 	if (!vg || !vg->num_vlans)
497 		goto out;
498 	/* Now remove and add entries for every VLAN configured on the
499 	 * bridge.  This function runs under RTNL so the bitmap will not
500 	 * change from under us.
501 	 */
502 	list_for_each_entry(v, &vg->vlan_list, vlist) {
503 		if (!br_vlan_should_use(v))
504 			continue;
505 		f = br_fdb_find(br, br->dev->dev_addr, v->vid);
506 		if (f && test_bit(BR_FDB_LOCAL, &f->flags) &&
507 		    !f->dst && !test_bit(BR_FDB_ADDED_BY_USER, &f->flags))
508 			fdb_delete_local(br, NULL, f);
509 		fdb_add_local(br, NULL, newaddr, v->vid);
510 	}
511 out:
512 	spin_unlock_bh(&br->hash_lock);
513 }
514 
515 void br_fdb_cleanup(struct work_struct *work)
516 {
517 	struct net_bridge *br = container_of(work, struct net_bridge,
518 					     gc_work.work);
519 	struct net_bridge_fdb_entry *f = NULL;
520 	unsigned long delay = hold_time(br);
521 	unsigned long work_delay = delay;
522 	unsigned long now = jiffies;
523 
524 	/* this part is tricky, in order to avoid blocking learning and
525 	 * consequently forwarding, we rely on rcu to delete objects with
526 	 * delayed freeing allowing us to continue traversing
527 	 */
528 	rcu_read_lock();
529 	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
530 		unsigned long this_timer = f->updated + delay;
531 
532 		if (test_bit(BR_FDB_STATIC, &f->flags) ||
533 		    test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &f->flags)) {
534 			if (test_bit(BR_FDB_NOTIFY, &f->flags)) {
535 				if (time_after(this_timer, now))
536 					work_delay = min(work_delay,
537 							 this_timer - now);
538 				else if (!test_and_set_bit(BR_FDB_NOTIFY_INACTIVE,
539 							   &f->flags))
540 					fdb_notify(br, f, RTM_NEWNEIGH, false);
541 			}
542 			continue;
543 		}
544 
545 		if (time_after(this_timer, now)) {
546 			work_delay = min(work_delay, this_timer - now);
547 		} else {
548 			spin_lock_bh(&br->hash_lock);
549 			if (!hlist_unhashed(&f->fdb_node))
550 				fdb_delete(br, f, true);
551 			spin_unlock_bh(&br->hash_lock);
552 		}
553 	}
554 	rcu_read_unlock();
555 
556 	/* Cleanup minimum 10 milliseconds apart */
557 	work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10));
558 	mod_delayed_work(system_long_wq, &br->gc_work, work_delay);
559 }
560 
561 /* Completely flush all dynamic entries in forwarding database.*/
562 void br_fdb_flush(struct net_bridge *br)
563 {
564 	struct net_bridge_fdb_entry *f;
565 	struct hlist_node *tmp;
566 
567 	spin_lock_bh(&br->hash_lock);
568 	hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
569 		if (!test_bit(BR_FDB_STATIC, &f->flags))
570 			fdb_delete(br, f, true);
571 	}
572 	spin_unlock_bh(&br->hash_lock);
573 }
574 
575 /* Flush all entries referring to a specific port.
576  * if do_all is set also flush static entries
577  * if vid is set delete all entries that match the vlan_id
578  */
579 void br_fdb_delete_by_port(struct net_bridge *br,
580 			   const struct net_bridge_port *p,
581 			   u16 vid,
582 			   int do_all)
583 {
584 	struct net_bridge_fdb_entry *f;
585 	struct hlist_node *tmp;
586 
587 	spin_lock_bh(&br->hash_lock);
588 	hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
589 		if (f->dst != p)
590 			continue;
591 
592 		if (!do_all)
593 			if (test_bit(BR_FDB_STATIC, &f->flags) ||
594 			    (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &f->flags) &&
595 			     !test_bit(BR_FDB_OFFLOADED, &f->flags)) ||
596 			    (vid && f->key.vlan_id != vid))
597 				continue;
598 
599 		if (test_bit(BR_FDB_LOCAL, &f->flags))
600 			fdb_delete_local(br, p, f);
601 		else
602 			fdb_delete(br, f, true);
603 	}
604 	spin_unlock_bh(&br->hash_lock);
605 }
606 
607 #if IS_ENABLED(CONFIG_ATM_LANE)
608 /* Interface used by ATM LANE hook to test
609  * if an addr is on some other bridge port */
610 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
611 {
612 	struct net_bridge_fdb_entry *fdb;
613 	struct net_bridge_port *port;
614 	int ret;
615 
616 	rcu_read_lock();
617 	port = br_port_get_rcu(dev);
618 	if (!port)
619 		ret = 0;
620 	else {
621 		const struct net_bridge_port *dst = NULL;
622 
623 		fdb = br_fdb_find_rcu(port->br, addr, 0);
624 		if (fdb)
625 			dst = READ_ONCE(fdb->dst);
626 
627 		ret = dst && dst->dev != dev &&
628 		      dst->state == BR_STATE_FORWARDING;
629 	}
630 	rcu_read_unlock();
631 
632 	return ret;
633 }
634 #endif /* CONFIG_ATM_LANE */
635 
636 /*
637  * Fill buffer with forwarding table records in
638  * the API format.
639  */
640 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
641 		   unsigned long maxnum, unsigned long skip)
642 {
643 	struct net_bridge_fdb_entry *f;
644 	struct __fdb_entry *fe = buf;
645 	int num = 0;
646 
647 	memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
648 
649 	rcu_read_lock();
650 	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
651 		if (num >= maxnum)
652 			break;
653 
654 		if (has_expired(br, f))
655 			continue;
656 
657 		/* ignore pseudo entry for local MAC address */
658 		if (!f->dst)
659 			continue;
660 
661 		if (skip) {
662 			--skip;
663 			continue;
664 		}
665 
666 		/* convert from internal format to API */
667 		memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN);
668 
669 		/* due to ABI compat need to split into hi/lo */
670 		fe->port_no = f->dst->port_no;
671 		fe->port_hi = f->dst->port_no >> 8;
672 
673 		fe->is_local = test_bit(BR_FDB_LOCAL, &f->flags);
674 		if (!test_bit(BR_FDB_STATIC, &f->flags))
675 			fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
676 		++fe;
677 		++num;
678 	}
679 	rcu_read_unlock();
680 
681 	return num;
682 }
683 
684 /* Add entry for local address of interface */
685 int br_fdb_add_local(struct net_bridge *br, struct net_bridge_port *source,
686 		     const unsigned char *addr, u16 vid)
687 {
688 	int ret;
689 
690 	spin_lock_bh(&br->hash_lock);
691 	ret = fdb_add_local(br, source, addr, vid);
692 	spin_unlock_bh(&br->hash_lock);
693 	return ret;
694 }
695 
696 /* returns true if the fdb was modified */
697 static bool __fdb_mark_active(struct net_bridge_fdb_entry *fdb)
698 {
699 	return !!(test_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags) &&
700 		  test_and_clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags));
701 }
702 
703 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
704 		   const unsigned char *addr, u16 vid, unsigned long flags)
705 {
706 	struct net_bridge_fdb_entry *fdb;
707 
708 	/* some users want to always flood. */
709 	if (hold_time(br) == 0)
710 		return;
711 
712 	fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
713 	if (likely(fdb)) {
714 		/* attempt to update an entry for a local interface */
715 		if (unlikely(test_bit(BR_FDB_LOCAL, &fdb->flags))) {
716 			if (net_ratelimit())
717 				br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
718 					source->dev->name, addr, vid);
719 		} else {
720 			unsigned long now = jiffies;
721 			bool fdb_modified = false;
722 
723 			if (now != fdb->updated) {
724 				fdb->updated = now;
725 				fdb_modified = __fdb_mark_active(fdb);
726 			}
727 
728 			/* fastpath: update of existing entry */
729 			if (unlikely(source != READ_ONCE(fdb->dst) &&
730 				     !test_bit(BR_FDB_STICKY, &fdb->flags))) {
731 				br_switchdev_fdb_notify(br, fdb, RTM_DELNEIGH);
732 				WRITE_ONCE(fdb->dst, source);
733 				fdb_modified = true;
734 				/* Take over HW learned entry */
735 				if (unlikely(test_bit(BR_FDB_ADDED_BY_EXT_LEARN,
736 						      &fdb->flags)))
737 					clear_bit(BR_FDB_ADDED_BY_EXT_LEARN,
738 						  &fdb->flags);
739 			}
740 
741 			if (unlikely(test_bit(BR_FDB_ADDED_BY_USER, &flags)))
742 				set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
743 			if (unlikely(fdb_modified)) {
744 				trace_br_fdb_update(br, source, addr, vid, flags);
745 				fdb_notify(br, fdb, RTM_NEWNEIGH, true);
746 			}
747 		}
748 	} else {
749 		spin_lock(&br->hash_lock);
750 		fdb = fdb_create(br, source, addr, vid, flags);
751 		if (fdb) {
752 			trace_br_fdb_update(br, source, addr, vid, flags);
753 			fdb_notify(br, fdb, RTM_NEWNEIGH, true);
754 		}
755 		/* else  we lose race and someone else inserts
756 		 * it first, don't bother updating
757 		 */
758 		spin_unlock(&br->hash_lock);
759 	}
760 }
761 
762 /* Dump information about entries, in response to GETNEIGH */
763 int br_fdb_dump(struct sk_buff *skb,
764 		struct netlink_callback *cb,
765 		struct net_device *dev,
766 		struct net_device *filter_dev,
767 		int *idx)
768 {
769 	struct net_bridge *br = netdev_priv(dev);
770 	struct net_bridge_fdb_entry *f;
771 	int err = 0;
772 
773 	if (!netif_is_bridge_master(dev))
774 		return err;
775 
776 	if (!filter_dev) {
777 		err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
778 		if (err < 0)
779 			return err;
780 	}
781 
782 	rcu_read_lock();
783 	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
784 		if (*idx < cb->args[2])
785 			goto skip;
786 		if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) {
787 			if (filter_dev != dev)
788 				goto skip;
789 			/* !f->dst is a special case for bridge
790 			 * It means the MAC belongs to the bridge
791 			 * Therefore need a little more filtering
792 			 * we only want to dump the !f->dst case
793 			 */
794 			if (f->dst)
795 				goto skip;
796 		}
797 		if (!filter_dev && f->dst)
798 			goto skip;
799 
800 		err = fdb_fill_info(skb, br, f,
801 				    NETLINK_CB(cb->skb).portid,
802 				    cb->nlh->nlmsg_seq,
803 				    RTM_NEWNEIGH,
804 				    NLM_F_MULTI);
805 		if (err < 0)
806 			break;
807 skip:
808 		*idx += 1;
809 	}
810 	rcu_read_unlock();
811 
812 	return err;
813 }
814 
815 int br_fdb_get(struct sk_buff *skb,
816 	       struct nlattr *tb[],
817 	       struct net_device *dev,
818 	       const unsigned char *addr,
819 	       u16 vid, u32 portid, u32 seq,
820 	       struct netlink_ext_ack *extack)
821 {
822 	struct net_bridge *br = netdev_priv(dev);
823 	struct net_bridge_fdb_entry *f;
824 	int err = 0;
825 
826 	rcu_read_lock();
827 	f = br_fdb_find_rcu(br, addr, vid);
828 	if (!f) {
829 		NL_SET_ERR_MSG(extack, "Fdb entry not found");
830 		err = -ENOENT;
831 		goto errout;
832 	}
833 
834 	err = fdb_fill_info(skb, br, f, portid, seq,
835 			    RTM_NEWNEIGH, 0);
836 errout:
837 	rcu_read_unlock();
838 	return err;
839 }
840 
841 /* returns true if the fdb is modified */
842 static bool fdb_handle_notify(struct net_bridge_fdb_entry *fdb, u8 notify)
843 {
844 	bool modified = false;
845 
846 	/* allow to mark an entry as inactive, usually done on creation */
847 	if ((notify & FDB_NOTIFY_INACTIVE_BIT) &&
848 	    !test_and_set_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags))
849 		modified = true;
850 
851 	if ((notify & FDB_NOTIFY_BIT) &&
852 	    !test_and_set_bit(BR_FDB_NOTIFY, &fdb->flags)) {
853 		/* enabled activity tracking */
854 		modified = true;
855 	} else if (!(notify & FDB_NOTIFY_BIT) &&
856 		   test_and_clear_bit(BR_FDB_NOTIFY, &fdb->flags)) {
857 		/* disabled activity tracking, clear notify state */
858 		clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags);
859 		modified = true;
860 	}
861 
862 	return modified;
863 }
864 
865 /* Update (create or replace) forwarding database entry */
866 static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
867 			 const u8 *addr, struct ndmsg *ndm, u16 flags, u16 vid,
868 			 struct nlattr *nfea_tb[])
869 {
870 	bool is_sticky = !!(ndm->ndm_flags & NTF_STICKY);
871 	bool refresh = !nfea_tb[NFEA_DONT_REFRESH];
872 	struct net_bridge_fdb_entry *fdb;
873 	u16 state = ndm->ndm_state;
874 	bool modified = false;
875 	u8 notify = 0;
876 
877 	/* If the port cannot learn allow only local and static entries */
878 	if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
879 	    !(source->state == BR_STATE_LEARNING ||
880 	      source->state == BR_STATE_FORWARDING))
881 		return -EPERM;
882 
883 	if (!source && !(state & NUD_PERMANENT)) {
884 		pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
885 			br->dev->name);
886 		return -EINVAL;
887 	}
888 
889 	if (is_sticky && (state & NUD_PERMANENT))
890 		return -EINVAL;
891 
892 	if (nfea_tb[NFEA_ACTIVITY_NOTIFY]) {
893 		notify = nla_get_u8(nfea_tb[NFEA_ACTIVITY_NOTIFY]);
894 		if ((notify & ~BR_FDB_NOTIFY_SETTABLE_BITS) ||
895 		    (notify & BR_FDB_NOTIFY_SETTABLE_BITS) == FDB_NOTIFY_INACTIVE_BIT)
896 			return -EINVAL;
897 	}
898 
899 	fdb = br_fdb_find(br, addr, vid);
900 	if (fdb == NULL) {
901 		if (!(flags & NLM_F_CREATE))
902 			return -ENOENT;
903 
904 		fdb = fdb_create(br, source, addr, vid, 0);
905 		if (!fdb)
906 			return -ENOMEM;
907 
908 		modified = true;
909 	} else {
910 		if (flags & NLM_F_EXCL)
911 			return -EEXIST;
912 
913 		if (READ_ONCE(fdb->dst) != source) {
914 			WRITE_ONCE(fdb->dst, source);
915 			modified = true;
916 		}
917 	}
918 
919 	if (fdb_to_nud(br, fdb) != state) {
920 		if (state & NUD_PERMANENT) {
921 			set_bit(BR_FDB_LOCAL, &fdb->flags);
922 			if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags))
923 				fdb_add_hw_addr(br, addr);
924 		} else if (state & NUD_NOARP) {
925 			clear_bit(BR_FDB_LOCAL, &fdb->flags);
926 			if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags))
927 				fdb_add_hw_addr(br, addr);
928 		} else {
929 			clear_bit(BR_FDB_LOCAL, &fdb->flags);
930 			if (test_and_clear_bit(BR_FDB_STATIC, &fdb->flags))
931 				fdb_del_hw_addr(br, addr);
932 		}
933 
934 		modified = true;
935 	}
936 
937 	if (is_sticky != test_bit(BR_FDB_STICKY, &fdb->flags)) {
938 		change_bit(BR_FDB_STICKY, &fdb->flags);
939 		modified = true;
940 	}
941 
942 	if (fdb_handle_notify(fdb, notify))
943 		modified = true;
944 
945 	set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
946 
947 	fdb->used = jiffies;
948 	if (modified) {
949 		if (refresh)
950 			fdb->updated = jiffies;
951 		fdb_notify(br, fdb, RTM_NEWNEIGH, true);
952 	}
953 
954 	return 0;
955 }
956 
957 static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br,
958 			struct net_bridge_port *p, const unsigned char *addr,
959 			u16 nlh_flags, u16 vid, struct nlattr *nfea_tb[],
960 			struct netlink_ext_ack *extack)
961 {
962 	int err = 0;
963 
964 	if (ndm->ndm_flags & NTF_USE) {
965 		if (!p) {
966 			pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
967 				br->dev->name);
968 			return -EINVAL;
969 		}
970 		if (!nbp_state_should_learn(p))
971 			return 0;
972 
973 		local_bh_disable();
974 		rcu_read_lock();
975 		br_fdb_update(br, p, addr, vid, BIT(BR_FDB_ADDED_BY_USER));
976 		rcu_read_unlock();
977 		local_bh_enable();
978 	} else if (ndm->ndm_flags & NTF_EXT_LEARNED) {
979 		if (!p && !(ndm->ndm_state & NUD_PERMANENT)) {
980 			NL_SET_ERR_MSG_MOD(extack,
981 					   "FDB entry towards bridge must be permanent");
982 			return -EINVAL;
983 		}
984 		err = br_fdb_external_learn_add(br, p, addr, vid, true);
985 	} else {
986 		spin_lock_bh(&br->hash_lock);
987 		err = fdb_add_entry(br, p, addr, ndm, nlh_flags, vid, nfea_tb);
988 		spin_unlock_bh(&br->hash_lock);
989 	}
990 
991 	return err;
992 }
993 
994 static const struct nla_policy br_nda_fdb_pol[NFEA_MAX + 1] = {
995 	[NFEA_ACTIVITY_NOTIFY]	= { .type = NLA_U8 },
996 	[NFEA_DONT_REFRESH]	= { .type = NLA_FLAG },
997 };
998 
999 /* Add new permanent fdb entry with RTM_NEWNEIGH */
1000 int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1001 	       struct net_device *dev,
1002 	       const unsigned char *addr, u16 vid, u16 nlh_flags,
1003 	       struct netlink_ext_ack *extack)
1004 {
1005 	struct nlattr *nfea_tb[NFEA_MAX + 1], *attr;
1006 	struct net_bridge_vlan_group *vg;
1007 	struct net_bridge_port *p = NULL;
1008 	struct net_bridge_vlan *v;
1009 	struct net_bridge *br = NULL;
1010 	int err = 0;
1011 
1012 	trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);
1013 
1014 	if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
1015 		pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
1016 		return -EINVAL;
1017 	}
1018 
1019 	if (is_zero_ether_addr(addr)) {
1020 		pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
1021 		return -EINVAL;
1022 	}
1023 
1024 	if (netif_is_bridge_master(dev)) {
1025 		br = netdev_priv(dev);
1026 		vg = br_vlan_group(br);
1027 	} else {
1028 		p = br_port_get_rtnl(dev);
1029 		if (!p) {
1030 			pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
1031 				dev->name);
1032 			return -EINVAL;
1033 		}
1034 		br = p->br;
1035 		vg = nbp_vlan_group(p);
1036 	}
1037 
1038 	if (tb[NDA_FDB_EXT_ATTRS]) {
1039 		attr = tb[NDA_FDB_EXT_ATTRS];
1040 		err = nla_parse_nested(nfea_tb, NFEA_MAX, attr,
1041 				       br_nda_fdb_pol, extack);
1042 		if (err)
1043 			return err;
1044 	} else {
1045 		memset(nfea_tb, 0, sizeof(struct nlattr *) * (NFEA_MAX + 1));
1046 	}
1047 
1048 	if (vid) {
1049 		v = br_vlan_find(vg, vid);
1050 		if (!v || !br_vlan_should_use(v)) {
1051 			pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
1052 			return -EINVAL;
1053 		}
1054 
1055 		/* VID was specified, so use it. */
1056 		err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid, nfea_tb,
1057 				   extack);
1058 	} else {
1059 		err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0, nfea_tb,
1060 				   extack);
1061 		if (err || !vg || !vg->num_vlans)
1062 			goto out;
1063 
1064 		/* We have vlans configured on this port and user didn't
1065 		 * specify a VLAN.  To be nice, add/update entry for every
1066 		 * vlan on this port.
1067 		 */
1068 		list_for_each_entry(v, &vg->vlan_list, vlist) {
1069 			if (!br_vlan_should_use(v))
1070 				continue;
1071 			err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid,
1072 					   nfea_tb, extack);
1073 			if (err)
1074 				goto out;
1075 		}
1076 	}
1077 
1078 out:
1079 	return err;
1080 }
1081 
1082 static int fdb_delete_by_addr_and_port(struct net_bridge *br,
1083 				       const struct net_bridge_port *p,
1084 				       const u8 *addr, u16 vlan)
1085 {
1086 	struct net_bridge_fdb_entry *fdb;
1087 
1088 	fdb = br_fdb_find(br, addr, vlan);
1089 	if (!fdb || READ_ONCE(fdb->dst) != p)
1090 		return -ENOENT;
1091 
1092 	fdb_delete(br, fdb, true);
1093 
1094 	return 0;
1095 }
1096 
1097 static int __br_fdb_delete(struct net_bridge *br,
1098 			   const struct net_bridge_port *p,
1099 			   const unsigned char *addr, u16 vid)
1100 {
1101 	int err;
1102 
1103 	spin_lock_bh(&br->hash_lock);
1104 	err = fdb_delete_by_addr_and_port(br, p, addr, vid);
1105 	spin_unlock_bh(&br->hash_lock);
1106 
1107 	return err;
1108 }
1109 
1110 /* Remove neighbor entry with RTM_DELNEIGH */
1111 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1112 		  struct net_device *dev,
1113 		  const unsigned char *addr, u16 vid)
1114 {
1115 	struct net_bridge_vlan_group *vg;
1116 	struct net_bridge_port *p = NULL;
1117 	struct net_bridge_vlan *v;
1118 	struct net_bridge *br;
1119 	int err;
1120 
1121 	if (netif_is_bridge_master(dev)) {
1122 		br = netdev_priv(dev);
1123 		vg = br_vlan_group(br);
1124 	} else {
1125 		p = br_port_get_rtnl(dev);
1126 		if (!p) {
1127 			pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
1128 				dev->name);
1129 			return -EINVAL;
1130 		}
1131 		vg = nbp_vlan_group(p);
1132 		br = p->br;
1133 	}
1134 
1135 	if (vid) {
1136 		v = br_vlan_find(vg, vid);
1137 		if (!v) {
1138 			pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
1139 			return -EINVAL;
1140 		}
1141 
1142 		err = __br_fdb_delete(br, p, addr, vid);
1143 	} else {
1144 		err = -ENOENT;
1145 		err &= __br_fdb_delete(br, p, addr, 0);
1146 		if (!vg || !vg->num_vlans)
1147 			return err;
1148 
1149 		list_for_each_entry(v, &vg->vlan_list, vlist) {
1150 			if (!br_vlan_should_use(v))
1151 				continue;
1152 			err &= __br_fdb_delete(br, p, addr, v->vid);
1153 		}
1154 	}
1155 
1156 	return err;
1157 }
1158 
1159 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
1160 {
1161 	struct net_bridge_fdb_entry *f, *tmp;
1162 	int err = 0;
1163 
1164 	ASSERT_RTNL();
1165 
1166 	/* the key here is that static entries change only under rtnl */
1167 	rcu_read_lock();
1168 	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1169 		/* We only care for static entries */
1170 		if (!test_bit(BR_FDB_STATIC, &f->flags))
1171 			continue;
1172 		err = dev_uc_add(p->dev, f->key.addr.addr);
1173 		if (err)
1174 			goto rollback;
1175 	}
1176 done:
1177 	rcu_read_unlock();
1178 
1179 	return err;
1180 
1181 rollback:
1182 	hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
1183 		/* We only care for static entries */
1184 		if (!test_bit(BR_FDB_STATIC, &tmp->flags))
1185 			continue;
1186 		if (tmp == f)
1187 			break;
1188 		dev_uc_del(p->dev, tmp->key.addr.addr);
1189 	}
1190 
1191 	goto done;
1192 }
1193 
1194 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
1195 {
1196 	struct net_bridge_fdb_entry *f;
1197 
1198 	ASSERT_RTNL();
1199 
1200 	rcu_read_lock();
1201 	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1202 		/* We only care for static entries */
1203 		if (!test_bit(BR_FDB_STATIC, &f->flags))
1204 			continue;
1205 
1206 		dev_uc_del(p->dev, f->key.addr.addr);
1207 	}
1208 	rcu_read_unlock();
1209 }
1210 
1211 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
1212 			      const unsigned char *addr, u16 vid,
1213 			      bool swdev_notify)
1214 {
1215 	struct net_bridge_fdb_entry *fdb;
1216 	bool modified = false;
1217 	int err = 0;
1218 
1219 	trace_br_fdb_external_learn_add(br, p, addr, vid);
1220 
1221 	spin_lock_bh(&br->hash_lock);
1222 
1223 	fdb = br_fdb_find(br, addr, vid);
1224 	if (!fdb) {
1225 		unsigned long flags = BIT(BR_FDB_ADDED_BY_EXT_LEARN);
1226 
1227 		if (swdev_notify)
1228 			flags |= BIT(BR_FDB_ADDED_BY_USER);
1229 
1230 		if (!p)
1231 			flags |= BIT(BR_FDB_LOCAL);
1232 
1233 		fdb = fdb_create(br, p, addr, vid, flags);
1234 		if (!fdb) {
1235 			err = -ENOMEM;
1236 			goto err_unlock;
1237 		}
1238 		fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1239 	} else {
1240 		fdb->updated = jiffies;
1241 
1242 		if (READ_ONCE(fdb->dst) != p) {
1243 			WRITE_ONCE(fdb->dst, p);
1244 			modified = true;
1245 		}
1246 
1247 		if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) {
1248 			/* Refresh entry */
1249 			fdb->used = jiffies;
1250 		} else if (!test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags)) {
1251 			/* Take over SW learned entry */
1252 			set_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags);
1253 			modified = true;
1254 		}
1255 
1256 		if (swdev_notify)
1257 			set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
1258 
1259 		if (!p)
1260 			set_bit(BR_FDB_LOCAL, &fdb->flags);
1261 
1262 		if (modified)
1263 			fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1264 	}
1265 
1266 err_unlock:
1267 	spin_unlock_bh(&br->hash_lock);
1268 
1269 	return err;
1270 }
1271 
1272 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
1273 			      const unsigned char *addr, u16 vid,
1274 			      bool swdev_notify)
1275 {
1276 	struct net_bridge_fdb_entry *fdb;
1277 	int err = 0;
1278 
1279 	spin_lock_bh(&br->hash_lock);
1280 
1281 	fdb = br_fdb_find(br, addr, vid);
1282 	if (fdb && test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags))
1283 		fdb_delete(br, fdb, swdev_notify);
1284 	else
1285 		err = -ENOENT;
1286 
1287 	spin_unlock_bh(&br->hash_lock);
1288 
1289 	return err;
1290 }
1291 
1292 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
1293 			  const unsigned char *addr, u16 vid, bool offloaded)
1294 {
1295 	struct net_bridge_fdb_entry *fdb;
1296 
1297 	spin_lock_bh(&br->hash_lock);
1298 
1299 	fdb = br_fdb_find(br, addr, vid);
1300 	if (fdb && offloaded != test_bit(BR_FDB_OFFLOADED, &fdb->flags))
1301 		change_bit(BR_FDB_OFFLOADED, &fdb->flags);
1302 
1303 	spin_unlock_bh(&br->hash_lock);
1304 }
1305 
1306 void br_fdb_clear_offload(const struct net_device *dev, u16 vid)
1307 {
1308 	struct net_bridge_fdb_entry *f;
1309 	struct net_bridge_port *p;
1310 
1311 	ASSERT_RTNL();
1312 
1313 	p = br_port_get_rtnl(dev);
1314 	if (!p)
1315 		return;
1316 
1317 	spin_lock_bh(&p->br->hash_lock);
1318 	hlist_for_each_entry(f, &p->br->fdb_list, fdb_node) {
1319 		if (f->dst == p && f->key.vlan_id == vid)
1320 			clear_bit(BR_FDB_OFFLOADED, &f->flags);
1321 	}
1322 	spin_unlock_bh(&p->br->hash_lock);
1323 }
1324 EXPORT_SYMBOL_GPL(br_fdb_clear_offload);
1325