xref: /openbmc/linux/net/bridge/br_fdb.c (revision 8ffdff6a)
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 				br_switchdev_fdb_notify(fdb, RTM_DELNEIGH);
606 				fdb->dst = source;
607 				fdb_modified = true;
608 				/* Take over HW learned entry */
609 				if (unlikely(test_bit(BR_FDB_ADDED_BY_EXT_LEARN,
610 						      &fdb->flags)))
611 					clear_bit(BR_FDB_ADDED_BY_EXT_LEARN,
612 						  &fdb->flags);
613 			}
614 
615 			if (unlikely(test_bit(BR_FDB_ADDED_BY_USER, &flags)))
616 				set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
617 			if (unlikely(fdb_modified)) {
618 				trace_br_fdb_update(br, source, addr, vid, flags);
619 				fdb_notify(br, fdb, RTM_NEWNEIGH, true);
620 			}
621 		}
622 	} else {
623 		spin_lock(&br->hash_lock);
624 		fdb = fdb_create(br, source, addr, vid, flags);
625 		if (fdb) {
626 			trace_br_fdb_update(br, source, addr, vid, flags);
627 			fdb_notify(br, fdb, RTM_NEWNEIGH, true);
628 		}
629 		/* else  we lose race and someone else inserts
630 		 * it first, don't bother updating
631 		 */
632 		spin_unlock(&br->hash_lock);
633 	}
634 }
635 
636 static int fdb_to_nud(const struct net_bridge *br,
637 		      const struct net_bridge_fdb_entry *fdb)
638 {
639 	if (test_bit(BR_FDB_LOCAL, &fdb->flags))
640 		return NUD_PERMANENT;
641 	else if (test_bit(BR_FDB_STATIC, &fdb->flags))
642 		return NUD_NOARP;
643 	else if (has_expired(br, fdb))
644 		return NUD_STALE;
645 	else
646 		return NUD_REACHABLE;
647 }
648 
649 static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
650 			 const struct net_bridge_fdb_entry *fdb,
651 			 u32 portid, u32 seq, int type, unsigned int flags)
652 {
653 	unsigned long now = jiffies;
654 	struct nda_cacheinfo ci;
655 	struct nlmsghdr *nlh;
656 	struct ndmsg *ndm;
657 
658 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
659 	if (nlh == NULL)
660 		return -EMSGSIZE;
661 
662 	ndm = nlmsg_data(nlh);
663 	ndm->ndm_family	 = AF_BRIDGE;
664 	ndm->ndm_pad1    = 0;
665 	ndm->ndm_pad2    = 0;
666 	ndm->ndm_flags	 = 0;
667 	ndm->ndm_type	 = 0;
668 	ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
669 	ndm->ndm_state   = fdb_to_nud(br, fdb);
670 
671 	if (test_bit(BR_FDB_OFFLOADED, &fdb->flags))
672 		ndm->ndm_flags |= NTF_OFFLOADED;
673 	if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags))
674 		ndm->ndm_flags |= NTF_EXT_LEARNED;
675 	if (test_bit(BR_FDB_STICKY, &fdb->flags))
676 		ndm->ndm_flags |= NTF_STICKY;
677 
678 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr))
679 		goto nla_put_failure;
680 	if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
681 		goto nla_put_failure;
682 	ci.ndm_used	 = jiffies_to_clock_t(now - fdb->used);
683 	ci.ndm_confirmed = 0;
684 	ci.ndm_updated	 = jiffies_to_clock_t(now - fdb->updated);
685 	ci.ndm_refcnt	 = 0;
686 	if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
687 		goto nla_put_failure;
688 
689 	if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16),
690 					&fdb->key.vlan_id))
691 		goto nla_put_failure;
692 
693 	if (test_bit(BR_FDB_NOTIFY, &fdb->flags)) {
694 		struct nlattr *nest = nla_nest_start(skb, NDA_FDB_EXT_ATTRS);
695 		u8 notify_bits = FDB_NOTIFY_BIT;
696 
697 		if (!nest)
698 			goto nla_put_failure;
699 		if (test_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags))
700 			notify_bits |= FDB_NOTIFY_INACTIVE_BIT;
701 
702 		if (nla_put_u8(skb, NFEA_ACTIVITY_NOTIFY, notify_bits)) {
703 			nla_nest_cancel(skb, nest);
704 			goto nla_put_failure;
705 		}
706 
707 		nla_nest_end(skb, nest);
708 	}
709 
710 	nlmsg_end(skb, nlh);
711 	return 0;
712 
713 nla_put_failure:
714 	nlmsg_cancel(skb, nlh);
715 	return -EMSGSIZE;
716 }
717 
718 static inline size_t fdb_nlmsg_size(void)
719 {
720 	return NLMSG_ALIGN(sizeof(struct ndmsg))
721 		+ nla_total_size(ETH_ALEN) /* NDA_LLADDR */
722 		+ nla_total_size(sizeof(u32)) /* NDA_MASTER */
723 		+ nla_total_size(sizeof(u16)) /* NDA_VLAN */
724 		+ nla_total_size(sizeof(struct nda_cacheinfo))
725 		+ nla_total_size(0) /* NDA_FDB_EXT_ATTRS */
726 		+ nla_total_size(sizeof(u8)); /* NFEA_ACTIVITY_NOTIFY */
727 }
728 
729 static void fdb_notify(struct net_bridge *br,
730 		       const struct net_bridge_fdb_entry *fdb, int type,
731 		       bool swdev_notify)
732 {
733 	struct net *net = dev_net(br->dev);
734 	struct sk_buff *skb;
735 	int err = -ENOBUFS;
736 
737 	if (swdev_notify)
738 		br_switchdev_fdb_notify(fdb, type);
739 
740 	skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
741 	if (skb == NULL)
742 		goto errout;
743 
744 	err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
745 	if (err < 0) {
746 		/* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
747 		WARN_ON(err == -EMSGSIZE);
748 		kfree_skb(skb);
749 		goto errout;
750 	}
751 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
752 	return;
753 errout:
754 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
755 }
756 
757 /* Dump information about entries, in response to GETNEIGH */
758 int br_fdb_dump(struct sk_buff *skb,
759 		struct netlink_callback *cb,
760 		struct net_device *dev,
761 		struct net_device *filter_dev,
762 		int *idx)
763 {
764 	struct net_bridge *br = netdev_priv(dev);
765 	struct net_bridge_fdb_entry *f;
766 	int err = 0;
767 
768 	if (!(dev->priv_flags & IFF_EBRIDGE))
769 		return err;
770 
771 	if (!filter_dev) {
772 		err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
773 		if (err < 0)
774 			return err;
775 	}
776 
777 	rcu_read_lock();
778 	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
779 		if (*idx < cb->args[2])
780 			goto skip;
781 		if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) {
782 			if (filter_dev != dev)
783 				goto skip;
784 			/* !f->dst is a special case for bridge
785 			 * It means the MAC belongs to the bridge
786 			 * Therefore need a little more filtering
787 			 * we only want to dump the !f->dst case
788 			 */
789 			if (f->dst)
790 				goto skip;
791 		}
792 		if (!filter_dev && f->dst)
793 			goto skip;
794 
795 		err = fdb_fill_info(skb, br, f,
796 				    NETLINK_CB(cb->skb).portid,
797 				    cb->nlh->nlmsg_seq,
798 				    RTM_NEWNEIGH,
799 				    NLM_F_MULTI);
800 		if (err < 0)
801 			break;
802 skip:
803 		*idx += 1;
804 	}
805 	rcu_read_unlock();
806 
807 	return err;
808 }
809 
810 int br_fdb_get(struct sk_buff *skb,
811 	       struct nlattr *tb[],
812 	       struct net_device *dev,
813 	       const unsigned char *addr,
814 	       u16 vid, u32 portid, u32 seq,
815 	       struct netlink_ext_ack *extack)
816 {
817 	struct net_bridge *br = netdev_priv(dev);
818 	struct net_bridge_fdb_entry *f;
819 	int err = 0;
820 
821 	rcu_read_lock();
822 	f = br_fdb_find_rcu(br, addr, vid);
823 	if (!f) {
824 		NL_SET_ERR_MSG(extack, "Fdb entry not found");
825 		err = -ENOENT;
826 		goto errout;
827 	}
828 
829 	err = fdb_fill_info(skb, br, f, portid, seq,
830 			    RTM_NEWNEIGH, 0);
831 errout:
832 	rcu_read_unlock();
833 	return err;
834 }
835 
836 /* returns true if the fdb is modified */
837 static bool fdb_handle_notify(struct net_bridge_fdb_entry *fdb, u8 notify)
838 {
839 	bool modified = false;
840 
841 	/* allow to mark an entry as inactive, usually done on creation */
842 	if ((notify & FDB_NOTIFY_INACTIVE_BIT) &&
843 	    !test_and_set_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags))
844 		modified = true;
845 
846 	if ((notify & FDB_NOTIFY_BIT) &&
847 	    !test_and_set_bit(BR_FDB_NOTIFY, &fdb->flags)) {
848 		/* enabled activity tracking */
849 		modified = true;
850 	} else if (!(notify & FDB_NOTIFY_BIT) &&
851 		   test_and_clear_bit(BR_FDB_NOTIFY, &fdb->flags)) {
852 		/* disabled activity tracking, clear notify state */
853 		clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags);
854 		modified = true;
855 	}
856 
857 	return modified;
858 }
859 
860 /* Update (create or replace) forwarding database entry */
861 static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
862 			 const u8 *addr, struct ndmsg *ndm, u16 flags, u16 vid,
863 			 struct nlattr *nfea_tb[])
864 {
865 	bool is_sticky = !!(ndm->ndm_flags & NTF_STICKY);
866 	bool refresh = !nfea_tb[NFEA_DONT_REFRESH];
867 	struct net_bridge_fdb_entry *fdb;
868 	u16 state = ndm->ndm_state;
869 	bool modified = false;
870 	u8 notify = 0;
871 
872 	/* If the port cannot learn allow only local and static entries */
873 	if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
874 	    !(source->state == BR_STATE_LEARNING ||
875 	      source->state == BR_STATE_FORWARDING))
876 		return -EPERM;
877 
878 	if (!source && !(state & NUD_PERMANENT)) {
879 		pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
880 			br->dev->name);
881 		return -EINVAL;
882 	}
883 
884 	if (is_sticky && (state & NUD_PERMANENT))
885 		return -EINVAL;
886 
887 	if (nfea_tb[NFEA_ACTIVITY_NOTIFY]) {
888 		notify = nla_get_u8(nfea_tb[NFEA_ACTIVITY_NOTIFY]);
889 		if ((notify & ~BR_FDB_NOTIFY_SETTABLE_BITS) ||
890 		    (notify & BR_FDB_NOTIFY_SETTABLE_BITS) == FDB_NOTIFY_INACTIVE_BIT)
891 			return -EINVAL;
892 	}
893 
894 	fdb = br_fdb_find(br, addr, vid);
895 	if (fdb == NULL) {
896 		if (!(flags & NLM_F_CREATE))
897 			return -ENOENT;
898 
899 		fdb = fdb_create(br, source, addr, vid, 0);
900 		if (!fdb)
901 			return -ENOMEM;
902 
903 		modified = true;
904 	} else {
905 		if (flags & NLM_F_EXCL)
906 			return -EEXIST;
907 
908 		if (fdb->dst != source) {
909 			fdb->dst = source;
910 			modified = true;
911 		}
912 	}
913 
914 	if (fdb_to_nud(br, fdb) != state) {
915 		if (state & NUD_PERMANENT) {
916 			set_bit(BR_FDB_LOCAL, &fdb->flags);
917 			if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags))
918 				fdb_add_hw_addr(br, addr);
919 		} else if (state & NUD_NOARP) {
920 			clear_bit(BR_FDB_LOCAL, &fdb->flags);
921 			if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags))
922 				fdb_add_hw_addr(br, addr);
923 		} else {
924 			clear_bit(BR_FDB_LOCAL, &fdb->flags);
925 			if (test_and_clear_bit(BR_FDB_STATIC, &fdb->flags))
926 				fdb_del_hw_addr(br, addr);
927 		}
928 
929 		modified = true;
930 	}
931 
932 	if (is_sticky != test_bit(BR_FDB_STICKY, &fdb->flags)) {
933 		change_bit(BR_FDB_STICKY, &fdb->flags);
934 		modified = true;
935 	}
936 
937 	if (fdb_handle_notify(fdb, notify))
938 		modified = true;
939 
940 	set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
941 
942 	fdb->used = jiffies;
943 	if (modified) {
944 		if (refresh)
945 			fdb->updated = jiffies;
946 		fdb_notify(br, fdb, RTM_NEWNEIGH, true);
947 	}
948 
949 	return 0;
950 }
951 
952 static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br,
953 			struct net_bridge_port *p, const unsigned char *addr,
954 			u16 nlh_flags, u16 vid, struct nlattr *nfea_tb[])
955 {
956 	int err = 0;
957 
958 	if (ndm->ndm_flags & NTF_USE) {
959 		if (!p) {
960 			pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
961 				br->dev->name);
962 			return -EINVAL;
963 		}
964 		if (!nbp_state_should_learn(p))
965 			return 0;
966 
967 		local_bh_disable();
968 		rcu_read_lock();
969 		br_fdb_update(br, p, addr, vid, BIT(BR_FDB_ADDED_BY_USER));
970 		rcu_read_unlock();
971 		local_bh_enable();
972 	} else if (ndm->ndm_flags & NTF_EXT_LEARNED) {
973 		err = br_fdb_external_learn_add(br, p, addr, vid, true);
974 	} else {
975 		spin_lock_bh(&br->hash_lock);
976 		err = fdb_add_entry(br, p, addr, ndm, nlh_flags, vid, nfea_tb);
977 		spin_unlock_bh(&br->hash_lock);
978 	}
979 
980 	return err;
981 }
982 
983 static const struct nla_policy br_nda_fdb_pol[NFEA_MAX + 1] = {
984 	[NFEA_ACTIVITY_NOTIFY]	= { .type = NLA_U8 },
985 	[NFEA_DONT_REFRESH]	= { .type = NLA_FLAG },
986 };
987 
988 /* Add new permanent fdb entry with RTM_NEWNEIGH */
989 int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
990 	       struct net_device *dev,
991 	       const unsigned char *addr, u16 vid, u16 nlh_flags,
992 	       struct netlink_ext_ack *extack)
993 {
994 	struct nlattr *nfea_tb[NFEA_MAX + 1], *attr;
995 	struct net_bridge_vlan_group *vg;
996 	struct net_bridge_port *p = NULL;
997 	struct net_bridge_vlan *v;
998 	struct net_bridge *br = NULL;
999 	int err = 0;
1000 
1001 	trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);
1002 
1003 	if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
1004 		pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
1005 		return -EINVAL;
1006 	}
1007 
1008 	if (is_zero_ether_addr(addr)) {
1009 		pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
1010 		return -EINVAL;
1011 	}
1012 
1013 	if (dev->priv_flags & IFF_EBRIDGE) {
1014 		br = netdev_priv(dev);
1015 		vg = br_vlan_group(br);
1016 	} else {
1017 		p = br_port_get_rtnl(dev);
1018 		if (!p) {
1019 			pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
1020 				dev->name);
1021 			return -EINVAL;
1022 		}
1023 		br = p->br;
1024 		vg = nbp_vlan_group(p);
1025 	}
1026 
1027 	if (tb[NDA_FDB_EXT_ATTRS]) {
1028 		attr = tb[NDA_FDB_EXT_ATTRS];
1029 		err = nla_parse_nested(nfea_tb, NFEA_MAX, attr,
1030 				       br_nda_fdb_pol, extack);
1031 		if (err)
1032 			return err;
1033 	} else {
1034 		memset(nfea_tb, 0, sizeof(struct nlattr *) * (NFEA_MAX + 1));
1035 	}
1036 
1037 	if (vid) {
1038 		v = br_vlan_find(vg, vid);
1039 		if (!v || !br_vlan_should_use(v)) {
1040 			pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
1041 			return -EINVAL;
1042 		}
1043 
1044 		/* VID was specified, so use it. */
1045 		err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid, nfea_tb);
1046 	} else {
1047 		err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0, nfea_tb);
1048 		if (err || !vg || !vg->num_vlans)
1049 			goto out;
1050 
1051 		/* We have vlans configured on this port and user didn't
1052 		 * specify a VLAN.  To be nice, add/update entry for every
1053 		 * vlan on this port.
1054 		 */
1055 		list_for_each_entry(v, &vg->vlan_list, vlist) {
1056 			if (!br_vlan_should_use(v))
1057 				continue;
1058 			err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid,
1059 					   nfea_tb);
1060 			if (err)
1061 				goto out;
1062 		}
1063 	}
1064 
1065 out:
1066 	return err;
1067 }
1068 
1069 static int fdb_delete_by_addr_and_port(struct net_bridge *br,
1070 				       const struct net_bridge_port *p,
1071 				       const u8 *addr, u16 vlan)
1072 {
1073 	struct net_bridge_fdb_entry *fdb;
1074 
1075 	fdb = br_fdb_find(br, addr, vlan);
1076 	if (!fdb || fdb->dst != p)
1077 		return -ENOENT;
1078 
1079 	fdb_delete(br, fdb, true);
1080 
1081 	return 0;
1082 }
1083 
1084 static int __br_fdb_delete(struct net_bridge *br,
1085 			   const struct net_bridge_port *p,
1086 			   const unsigned char *addr, u16 vid)
1087 {
1088 	int err;
1089 
1090 	spin_lock_bh(&br->hash_lock);
1091 	err = fdb_delete_by_addr_and_port(br, p, addr, vid);
1092 	spin_unlock_bh(&br->hash_lock);
1093 
1094 	return err;
1095 }
1096 
1097 /* Remove neighbor entry with RTM_DELNEIGH */
1098 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1099 		  struct net_device *dev,
1100 		  const unsigned char *addr, u16 vid)
1101 {
1102 	struct net_bridge_vlan_group *vg;
1103 	struct net_bridge_port *p = NULL;
1104 	struct net_bridge_vlan *v;
1105 	struct net_bridge *br;
1106 	int err;
1107 
1108 	if (dev->priv_flags & IFF_EBRIDGE) {
1109 		br = netdev_priv(dev);
1110 		vg = br_vlan_group(br);
1111 	} else {
1112 		p = br_port_get_rtnl(dev);
1113 		if (!p) {
1114 			pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
1115 				dev->name);
1116 			return -EINVAL;
1117 		}
1118 		vg = nbp_vlan_group(p);
1119 		br = p->br;
1120 	}
1121 
1122 	if (vid) {
1123 		v = br_vlan_find(vg, vid);
1124 		if (!v) {
1125 			pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
1126 			return -EINVAL;
1127 		}
1128 
1129 		err = __br_fdb_delete(br, p, addr, vid);
1130 	} else {
1131 		err = -ENOENT;
1132 		err &= __br_fdb_delete(br, p, addr, 0);
1133 		if (!vg || !vg->num_vlans)
1134 			return err;
1135 
1136 		list_for_each_entry(v, &vg->vlan_list, vlist) {
1137 			if (!br_vlan_should_use(v))
1138 				continue;
1139 			err &= __br_fdb_delete(br, p, addr, v->vid);
1140 		}
1141 	}
1142 
1143 	return err;
1144 }
1145 
1146 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
1147 {
1148 	struct net_bridge_fdb_entry *f, *tmp;
1149 	int err = 0;
1150 
1151 	ASSERT_RTNL();
1152 
1153 	/* the key here is that static entries change only under rtnl */
1154 	rcu_read_lock();
1155 	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1156 		/* We only care for static entries */
1157 		if (!test_bit(BR_FDB_STATIC, &f->flags))
1158 			continue;
1159 		err = dev_uc_add(p->dev, f->key.addr.addr);
1160 		if (err)
1161 			goto rollback;
1162 	}
1163 done:
1164 	rcu_read_unlock();
1165 
1166 	return err;
1167 
1168 rollback:
1169 	hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
1170 		/* We only care for static entries */
1171 		if (!test_bit(BR_FDB_STATIC, &tmp->flags))
1172 			continue;
1173 		if (tmp == f)
1174 			break;
1175 		dev_uc_del(p->dev, tmp->key.addr.addr);
1176 	}
1177 
1178 	goto done;
1179 }
1180 
1181 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
1182 {
1183 	struct net_bridge_fdb_entry *f;
1184 
1185 	ASSERT_RTNL();
1186 
1187 	rcu_read_lock();
1188 	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1189 		/* We only care for static entries */
1190 		if (!test_bit(BR_FDB_STATIC, &f->flags))
1191 			continue;
1192 
1193 		dev_uc_del(p->dev, f->key.addr.addr);
1194 	}
1195 	rcu_read_unlock();
1196 }
1197 
1198 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
1199 			      const unsigned char *addr, u16 vid,
1200 			      bool swdev_notify)
1201 {
1202 	struct net_bridge_fdb_entry *fdb;
1203 	bool modified = false;
1204 	int err = 0;
1205 
1206 	trace_br_fdb_external_learn_add(br, p, addr, vid);
1207 
1208 	spin_lock_bh(&br->hash_lock);
1209 
1210 	fdb = br_fdb_find(br, addr, vid);
1211 	if (!fdb) {
1212 		unsigned long flags = BIT(BR_FDB_ADDED_BY_EXT_LEARN);
1213 
1214 		if (swdev_notify)
1215 			flags |= BIT(BR_FDB_ADDED_BY_USER);
1216 		fdb = fdb_create(br, p, addr, vid, flags);
1217 		if (!fdb) {
1218 			err = -ENOMEM;
1219 			goto err_unlock;
1220 		}
1221 		fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1222 	} else {
1223 		fdb->updated = jiffies;
1224 
1225 		if (fdb->dst != p) {
1226 			fdb->dst = p;
1227 			modified = true;
1228 		}
1229 
1230 		if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) {
1231 			/* Refresh entry */
1232 			fdb->used = jiffies;
1233 		} else if (!test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags)) {
1234 			/* Take over SW learned entry */
1235 			set_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags);
1236 			modified = true;
1237 		}
1238 
1239 		if (swdev_notify)
1240 			set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
1241 
1242 		if (modified)
1243 			fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1244 	}
1245 
1246 err_unlock:
1247 	spin_unlock_bh(&br->hash_lock);
1248 
1249 	return err;
1250 }
1251 
1252 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
1253 			      const unsigned char *addr, u16 vid,
1254 			      bool swdev_notify)
1255 {
1256 	struct net_bridge_fdb_entry *fdb;
1257 	int err = 0;
1258 
1259 	spin_lock_bh(&br->hash_lock);
1260 
1261 	fdb = br_fdb_find(br, addr, vid);
1262 	if (fdb && test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags))
1263 		fdb_delete(br, fdb, swdev_notify);
1264 	else
1265 		err = -ENOENT;
1266 
1267 	spin_unlock_bh(&br->hash_lock);
1268 
1269 	return err;
1270 }
1271 
1272 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
1273 			  const unsigned char *addr, u16 vid, bool offloaded)
1274 {
1275 	struct net_bridge_fdb_entry *fdb;
1276 
1277 	spin_lock_bh(&br->hash_lock);
1278 
1279 	fdb = br_fdb_find(br, addr, vid);
1280 	if (fdb && offloaded != test_bit(BR_FDB_OFFLOADED, &fdb->flags))
1281 		change_bit(BR_FDB_OFFLOADED, &fdb->flags);
1282 
1283 	spin_unlock_bh(&br->hash_lock);
1284 }
1285 
1286 void br_fdb_clear_offload(const struct net_device *dev, u16 vid)
1287 {
1288 	struct net_bridge_fdb_entry *f;
1289 	struct net_bridge_port *p;
1290 
1291 	ASSERT_RTNL();
1292 
1293 	p = br_port_get_rtnl(dev);
1294 	if (!p)
1295 		return;
1296 
1297 	spin_lock_bh(&p->br->hash_lock);
1298 	hlist_for_each_entry(f, &p->br->fdb_list, fdb_node) {
1299 		if (f->dst == p && f->key.vlan_id == vid)
1300 			clear_bit(BR_FDB_OFFLOADED, &f->flags);
1301 	}
1302 	spin_unlock_bh(&p->br->hash_lock);
1303 }
1304 EXPORT_SYMBOL_GPL(br_fdb_clear_offload);
1305