xref: /openbmc/linux/net/bridge/br_fdb.c (revision 9e255e2b)
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 int br_fdb_replay_one(struct notifier_block *nb,
730 			     struct net_bridge_fdb_entry *fdb,
731 			     struct net_device *dev)
732 {
733 	struct switchdev_notifier_fdb_info item;
734 	int err;
735 
736 	item.addr = fdb->key.addr.addr;
737 	item.vid = fdb->key.vlan_id;
738 	item.added_by_user = test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
739 	item.offloaded = test_bit(BR_FDB_OFFLOADED, &fdb->flags);
740 	item.info.dev = dev;
741 
742 	err = nb->notifier_call(nb, SWITCHDEV_FDB_ADD_TO_DEVICE, &item);
743 	return notifier_to_errno(err);
744 }
745 
746 int br_fdb_replay(struct net_device *br_dev, struct net_device *dev,
747 		  struct notifier_block *nb)
748 {
749 	struct net_bridge_fdb_entry *fdb;
750 	struct net_bridge *br;
751 	int err = 0;
752 
753 	if (!netif_is_bridge_master(br_dev) || !netif_is_bridge_port(dev))
754 		return -EINVAL;
755 
756 	br = netdev_priv(br_dev);
757 
758 	rcu_read_lock();
759 
760 	hlist_for_each_entry_rcu(fdb, &br->fdb_list, fdb_node) {
761 		struct net_bridge_port *dst = READ_ONCE(fdb->dst);
762 		struct net_device *dst_dev;
763 
764 		dst_dev = dst ? dst->dev : br->dev;
765 		if (dst_dev != br_dev && dst_dev != dev)
766 			continue;
767 
768 		err = br_fdb_replay_one(nb, fdb, dst_dev);
769 		if (err)
770 			break;
771 	}
772 
773 	rcu_read_unlock();
774 
775 	return err;
776 }
777 EXPORT_SYMBOL_GPL(br_fdb_replay);
778 
779 static void fdb_notify(struct net_bridge *br,
780 		       const struct net_bridge_fdb_entry *fdb, int type,
781 		       bool swdev_notify)
782 {
783 	struct net *net = dev_net(br->dev);
784 	struct sk_buff *skb;
785 	int err = -ENOBUFS;
786 
787 	if (swdev_notify)
788 		br_switchdev_fdb_notify(fdb, type);
789 
790 	skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
791 	if (skb == NULL)
792 		goto errout;
793 
794 	err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
795 	if (err < 0) {
796 		/* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
797 		WARN_ON(err == -EMSGSIZE);
798 		kfree_skb(skb);
799 		goto errout;
800 	}
801 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
802 	return;
803 errout:
804 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
805 }
806 
807 /* Dump information about entries, in response to GETNEIGH */
808 int br_fdb_dump(struct sk_buff *skb,
809 		struct netlink_callback *cb,
810 		struct net_device *dev,
811 		struct net_device *filter_dev,
812 		int *idx)
813 {
814 	struct net_bridge *br = netdev_priv(dev);
815 	struct net_bridge_fdb_entry *f;
816 	int err = 0;
817 
818 	if (!(dev->priv_flags & IFF_EBRIDGE))
819 		return err;
820 
821 	if (!filter_dev) {
822 		err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
823 		if (err < 0)
824 			return err;
825 	}
826 
827 	rcu_read_lock();
828 	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
829 		if (*idx < cb->args[2])
830 			goto skip;
831 		if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) {
832 			if (filter_dev != dev)
833 				goto skip;
834 			/* !f->dst is a special case for bridge
835 			 * It means the MAC belongs to the bridge
836 			 * Therefore need a little more filtering
837 			 * we only want to dump the !f->dst case
838 			 */
839 			if (f->dst)
840 				goto skip;
841 		}
842 		if (!filter_dev && f->dst)
843 			goto skip;
844 
845 		err = fdb_fill_info(skb, br, f,
846 				    NETLINK_CB(cb->skb).portid,
847 				    cb->nlh->nlmsg_seq,
848 				    RTM_NEWNEIGH,
849 				    NLM_F_MULTI);
850 		if (err < 0)
851 			break;
852 skip:
853 		*idx += 1;
854 	}
855 	rcu_read_unlock();
856 
857 	return err;
858 }
859 
860 int br_fdb_get(struct sk_buff *skb,
861 	       struct nlattr *tb[],
862 	       struct net_device *dev,
863 	       const unsigned char *addr,
864 	       u16 vid, u32 portid, u32 seq,
865 	       struct netlink_ext_ack *extack)
866 {
867 	struct net_bridge *br = netdev_priv(dev);
868 	struct net_bridge_fdb_entry *f;
869 	int err = 0;
870 
871 	rcu_read_lock();
872 	f = br_fdb_find_rcu(br, addr, vid);
873 	if (!f) {
874 		NL_SET_ERR_MSG(extack, "Fdb entry not found");
875 		err = -ENOENT;
876 		goto errout;
877 	}
878 
879 	err = fdb_fill_info(skb, br, f, portid, seq,
880 			    RTM_NEWNEIGH, 0);
881 errout:
882 	rcu_read_unlock();
883 	return err;
884 }
885 
886 /* returns true if the fdb is modified */
887 static bool fdb_handle_notify(struct net_bridge_fdb_entry *fdb, u8 notify)
888 {
889 	bool modified = false;
890 
891 	/* allow to mark an entry as inactive, usually done on creation */
892 	if ((notify & FDB_NOTIFY_INACTIVE_BIT) &&
893 	    !test_and_set_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags))
894 		modified = true;
895 
896 	if ((notify & FDB_NOTIFY_BIT) &&
897 	    !test_and_set_bit(BR_FDB_NOTIFY, &fdb->flags)) {
898 		/* enabled activity tracking */
899 		modified = true;
900 	} else if (!(notify & FDB_NOTIFY_BIT) &&
901 		   test_and_clear_bit(BR_FDB_NOTIFY, &fdb->flags)) {
902 		/* disabled activity tracking, clear notify state */
903 		clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags);
904 		modified = true;
905 	}
906 
907 	return modified;
908 }
909 
910 /* Update (create or replace) forwarding database entry */
911 static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
912 			 const u8 *addr, struct ndmsg *ndm, u16 flags, u16 vid,
913 			 struct nlattr *nfea_tb[])
914 {
915 	bool is_sticky = !!(ndm->ndm_flags & NTF_STICKY);
916 	bool refresh = !nfea_tb[NFEA_DONT_REFRESH];
917 	struct net_bridge_fdb_entry *fdb;
918 	u16 state = ndm->ndm_state;
919 	bool modified = false;
920 	u8 notify = 0;
921 
922 	/* If the port cannot learn allow only local and static entries */
923 	if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
924 	    !(source->state == BR_STATE_LEARNING ||
925 	      source->state == BR_STATE_FORWARDING))
926 		return -EPERM;
927 
928 	if (!source && !(state & NUD_PERMANENT)) {
929 		pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
930 			br->dev->name);
931 		return -EINVAL;
932 	}
933 
934 	if (is_sticky && (state & NUD_PERMANENT))
935 		return -EINVAL;
936 
937 	if (nfea_tb[NFEA_ACTIVITY_NOTIFY]) {
938 		notify = nla_get_u8(nfea_tb[NFEA_ACTIVITY_NOTIFY]);
939 		if ((notify & ~BR_FDB_NOTIFY_SETTABLE_BITS) ||
940 		    (notify & BR_FDB_NOTIFY_SETTABLE_BITS) == FDB_NOTIFY_INACTIVE_BIT)
941 			return -EINVAL;
942 	}
943 
944 	fdb = br_fdb_find(br, addr, vid);
945 	if (fdb == NULL) {
946 		if (!(flags & NLM_F_CREATE))
947 			return -ENOENT;
948 
949 		fdb = fdb_create(br, source, addr, vid, 0);
950 		if (!fdb)
951 			return -ENOMEM;
952 
953 		modified = true;
954 	} else {
955 		if (flags & NLM_F_EXCL)
956 			return -EEXIST;
957 
958 		if (fdb->dst != source) {
959 			fdb->dst = source;
960 			modified = true;
961 		}
962 	}
963 
964 	if (fdb_to_nud(br, fdb) != state) {
965 		if (state & NUD_PERMANENT) {
966 			set_bit(BR_FDB_LOCAL, &fdb->flags);
967 			if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags))
968 				fdb_add_hw_addr(br, addr);
969 		} else if (state & NUD_NOARP) {
970 			clear_bit(BR_FDB_LOCAL, &fdb->flags);
971 			if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags))
972 				fdb_add_hw_addr(br, addr);
973 		} else {
974 			clear_bit(BR_FDB_LOCAL, &fdb->flags);
975 			if (test_and_clear_bit(BR_FDB_STATIC, &fdb->flags))
976 				fdb_del_hw_addr(br, addr);
977 		}
978 
979 		modified = true;
980 	}
981 
982 	if (is_sticky != test_bit(BR_FDB_STICKY, &fdb->flags)) {
983 		change_bit(BR_FDB_STICKY, &fdb->flags);
984 		modified = true;
985 	}
986 
987 	if (fdb_handle_notify(fdb, notify))
988 		modified = true;
989 
990 	set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
991 
992 	fdb->used = jiffies;
993 	if (modified) {
994 		if (refresh)
995 			fdb->updated = jiffies;
996 		fdb_notify(br, fdb, RTM_NEWNEIGH, true);
997 	}
998 
999 	return 0;
1000 }
1001 
1002 static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br,
1003 			struct net_bridge_port *p, const unsigned char *addr,
1004 			u16 nlh_flags, u16 vid, struct nlattr *nfea_tb[])
1005 {
1006 	int err = 0;
1007 
1008 	if (ndm->ndm_flags & NTF_USE) {
1009 		if (!p) {
1010 			pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
1011 				br->dev->name);
1012 			return -EINVAL;
1013 		}
1014 		if (!nbp_state_should_learn(p))
1015 			return 0;
1016 
1017 		local_bh_disable();
1018 		rcu_read_lock();
1019 		br_fdb_update(br, p, addr, vid, BIT(BR_FDB_ADDED_BY_USER));
1020 		rcu_read_unlock();
1021 		local_bh_enable();
1022 	} else if (ndm->ndm_flags & NTF_EXT_LEARNED) {
1023 		err = br_fdb_external_learn_add(br, p, addr, vid, true);
1024 	} else {
1025 		spin_lock_bh(&br->hash_lock);
1026 		err = fdb_add_entry(br, p, addr, ndm, nlh_flags, vid, nfea_tb);
1027 		spin_unlock_bh(&br->hash_lock);
1028 	}
1029 
1030 	return err;
1031 }
1032 
1033 static const struct nla_policy br_nda_fdb_pol[NFEA_MAX + 1] = {
1034 	[NFEA_ACTIVITY_NOTIFY]	= { .type = NLA_U8 },
1035 	[NFEA_DONT_REFRESH]	= { .type = NLA_FLAG },
1036 };
1037 
1038 /* Add new permanent fdb entry with RTM_NEWNEIGH */
1039 int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1040 	       struct net_device *dev,
1041 	       const unsigned char *addr, u16 vid, u16 nlh_flags,
1042 	       struct netlink_ext_ack *extack)
1043 {
1044 	struct nlattr *nfea_tb[NFEA_MAX + 1], *attr;
1045 	struct net_bridge_vlan_group *vg;
1046 	struct net_bridge_port *p = NULL;
1047 	struct net_bridge_vlan *v;
1048 	struct net_bridge *br = NULL;
1049 	int err = 0;
1050 
1051 	trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);
1052 
1053 	if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
1054 		pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
1055 		return -EINVAL;
1056 	}
1057 
1058 	if (is_zero_ether_addr(addr)) {
1059 		pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
1060 		return -EINVAL;
1061 	}
1062 
1063 	if (dev->priv_flags & IFF_EBRIDGE) {
1064 		br = netdev_priv(dev);
1065 		vg = br_vlan_group(br);
1066 	} else {
1067 		p = br_port_get_rtnl(dev);
1068 		if (!p) {
1069 			pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
1070 				dev->name);
1071 			return -EINVAL;
1072 		}
1073 		br = p->br;
1074 		vg = nbp_vlan_group(p);
1075 	}
1076 
1077 	if (tb[NDA_FDB_EXT_ATTRS]) {
1078 		attr = tb[NDA_FDB_EXT_ATTRS];
1079 		err = nla_parse_nested(nfea_tb, NFEA_MAX, attr,
1080 				       br_nda_fdb_pol, extack);
1081 		if (err)
1082 			return err;
1083 	} else {
1084 		memset(nfea_tb, 0, sizeof(struct nlattr *) * (NFEA_MAX + 1));
1085 	}
1086 
1087 	if (vid) {
1088 		v = br_vlan_find(vg, vid);
1089 		if (!v || !br_vlan_should_use(v)) {
1090 			pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
1091 			return -EINVAL;
1092 		}
1093 
1094 		/* VID was specified, so use it. */
1095 		err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid, nfea_tb);
1096 	} else {
1097 		err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0, nfea_tb);
1098 		if (err || !vg || !vg->num_vlans)
1099 			goto out;
1100 
1101 		/* We have vlans configured on this port and user didn't
1102 		 * specify a VLAN.  To be nice, add/update entry for every
1103 		 * vlan on this port.
1104 		 */
1105 		list_for_each_entry(v, &vg->vlan_list, vlist) {
1106 			if (!br_vlan_should_use(v))
1107 				continue;
1108 			err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid,
1109 					   nfea_tb);
1110 			if (err)
1111 				goto out;
1112 		}
1113 	}
1114 
1115 out:
1116 	return err;
1117 }
1118 
1119 static int fdb_delete_by_addr_and_port(struct net_bridge *br,
1120 				       const struct net_bridge_port *p,
1121 				       const u8 *addr, u16 vlan)
1122 {
1123 	struct net_bridge_fdb_entry *fdb;
1124 
1125 	fdb = br_fdb_find(br, addr, vlan);
1126 	if (!fdb || fdb->dst != p)
1127 		return -ENOENT;
1128 
1129 	fdb_delete(br, fdb, true);
1130 
1131 	return 0;
1132 }
1133 
1134 static int __br_fdb_delete(struct net_bridge *br,
1135 			   const struct net_bridge_port *p,
1136 			   const unsigned char *addr, u16 vid)
1137 {
1138 	int err;
1139 
1140 	spin_lock_bh(&br->hash_lock);
1141 	err = fdb_delete_by_addr_and_port(br, p, addr, vid);
1142 	spin_unlock_bh(&br->hash_lock);
1143 
1144 	return err;
1145 }
1146 
1147 /* Remove neighbor entry with RTM_DELNEIGH */
1148 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1149 		  struct net_device *dev,
1150 		  const unsigned char *addr, u16 vid)
1151 {
1152 	struct net_bridge_vlan_group *vg;
1153 	struct net_bridge_port *p = NULL;
1154 	struct net_bridge_vlan *v;
1155 	struct net_bridge *br;
1156 	int err;
1157 
1158 	if (dev->priv_flags & IFF_EBRIDGE) {
1159 		br = netdev_priv(dev);
1160 		vg = br_vlan_group(br);
1161 	} else {
1162 		p = br_port_get_rtnl(dev);
1163 		if (!p) {
1164 			pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
1165 				dev->name);
1166 			return -EINVAL;
1167 		}
1168 		vg = nbp_vlan_group(p);
1169 		br = p->br;
1170 	}
1171 
1172 	if (vid) {
1173 		v = br_vlan_find(vg, vid);
1174 		if (!v) {
1175 			pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
1176 			return -EINVAL;
1177 		}
1178 
1179 		err = __br_fdb_delete(br, p, addr, vid);
1180 	} else {
1181 		err = -ENOENT;
1182 		err &= __br_fdb_delete(br, p, addr, 0);
1183 		if (!vg || !vg->num_vlans)
1184 			return err;
1185 
1186 		list_for_each_entry(v, &vg->vlan_list, vlist) {
1187 			if (!br_vlan_should_use(v))
1188 				continue;
1189 			err &= __br_fdb_delete(br, p, addr, v->vid);
1190 		}
1191 	}
1192 
1193 	return err;
1194 }
1195 
1196 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
1197 {
1198 	struct net_bridge_fdb_entry *f, *tmp;
1199 	int err = 0;
1200 
1201 	ASSERT_RTNL();
1202 
1203 	/* the key here is that static entries change only under rtnl */
1204 	rcu_read_lock();
1205 	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1206 		/* We only care for static entries */
1207 		if (!test_bit(BR_FDB_STATIC, &f->flags))
1208 			continue;
1209 		err = dev_uc_add(p->dev, f->key.addr.addr);
1210 		if (err)
1211 			goto rollback;
1212 	}
1213 done:
1214 	rcu_read_unlock();
1215 
1216 	return err;
1217 
1218 rollback:
1219 	hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
1220 		/* We only care for static entries */
1221 		if (!test_bit(BR_FDB_STATIC, &tmp->flags))
1222 			continue;
1223 		if (tmp == f)
1224 			break;
1225 		dev_uc_del(p->dev, tmp->key.addr.addr);
1226 	}
1227 
1228 	goto done;
1229 }
1230 
1231 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
1232 {
1233 	struct net_bridge_fdb_entry *f;
1234 
1235 	ASSERT_RTNL();
1236 
1237 	rcu_read_lock();
1238 	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1239 		/* We only care for static entries */
1240 		if (!test_bit(BR_FDB_STATIC, &f->flags))
1241 			continue;
1242 
1243 		dev_uc_del(p->dev, f->key.addr.addr);
1244 	}
1245 	rcu_read_unlock();
1246 }
1247 
1248 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
1249 			      const unsigned char *addr, u16 vid,
1250 			      bool swdev_notify)
1251 {
1252 	struct net_bridge_fdb_entry *fdb;
1253 	bool modified = false;
1254 	int err = 0;
1255 
1256 	trace_br_fdb_external_learn_add(br, p, addr, vid);
1257 
1258 	spin_lock_bh(&br->hash_lock);
1259 
1260 	fdb = br_fdb_find(br, addr, vid);
1261 	if (!fdb) {
1262 		unsigned long flags = BIT(BR_FDB_ADDED_BY_EXT_LEARN);
1263 
1264 		if (swdev_notify)
1265 			flags |= BIT(BR_FDB_ADDED_BY_USER);
1266 		fdb = fdb_create(br, p, addr, vid, flags);
1267 		if (!fdb) {
1268 			err = -ENOMEM;
1269 			goto err_unlock;
1270 		}
1271 		fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1272 	} else {
1273 		fdb->updated = jiffies;
1274 
1275 		if (fdb->dst != p) {
1276 			fdb->dst = p;
1277 			modified = true;
1278 		}
1279 
1280 		if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) {
1281 			/* Refresh entry */
1282 			fdb->used = jiffies;
1283 		} else if (!test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags)) {
1284 			/* Take over SW learned entry */
1285 			set_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags);
1286 			modified = true;
1287 		}
1288 
1289 		if (swdev_notify)
1290 			set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
1291 
1292 		if (modified)
1293 			fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1294 	}
1295 
1296 err_unlock:
1297 	spin_unlock_bh(&br->hash_lock);
1298 
1299 	return err;
1300 }
1301 
1302 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
1303 			      const unsigned char *addr, u16 vid,
1304 			      bool swdev_notify)
1305 {
1306 	struct net_bridge_fdb_entry *fdb;
1307 	int err = 0;
1308 
1309 	spin_lock_bh(&br->hash_lock);
1310 
1311 	fdb = br_fdb_find(br, addr, vid);
1312 	if (fdb && test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags))
1313 		fdb_delete(br, fdb, swdev_notify);
1314 	else
1315 		err = -ENOENT;
1316 
1317 	spin_unlock_bh(&br->hash_lock);
1318 
1319 	return err;
1320 }
1321 
1322 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
1323 			  const unsigned char *addr, u16 vid, bool offloaded)
1324 {
1325 	struct net_bridge_fdb_entry *fdb;
1326 
1327 	spin_lock_bh(&br->hash_lock);
1328 
1329 	fdb = br_fdb_find(br, addr, vid);
1330 	if (fdb && offloaded != test_bit(BR_FDB_OFFLOADED, &fdb->flags))
1331 		change_bit(BR_FDB_OFFLOADED, &fdb->flags);
1332 
1333 	spin_unlock_bh(&br->hash_lock);
1334 }
1335 
1336 void br_fdb_clear_offload(const struct net_device *dev, u16 vid)
1337 {
1338 	struct net_bridge_fdb_entry *f;
1339 	struct net_bridge_port *p;
1340 
1341 	ASSERT_RTNL();
1342 
1343 	p = br_port_get_rtnl(dev);
1344 	if (!p)
1345 		return;
1346 
1347 	spin_lock_bh(&p->br->hash_lock);
1348 	hlist_for_each_entry(f, &p->br->fdb_list, fdb_node) {
1349 		if (f->dst == p && f->key.vlan_id == vid)
1350 			clear_bit(BR_FDB_OFFLOADED, &f->flags);
1351 	}
1352 	spin_unlock_bh(&p->br->hash_lock);
1353 }
1354 EXPORT_SYMBOL_GPL(br_fdb_clear_offload);
1355