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