• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
br_fdb_init(void)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 
br_fdb_fini(void)52 void br_fdb_fini(void)
53 {
54 	kmem_cache_destroy(br_fdb_cache);
55 }
56 
br_fdb_hash_init(struct net_bridge * br)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 
br_fdb_hash_fini(struct net_bridge * br)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  */
hold_time(const struct net_bridge * br)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 
has_expired(const struct net_bridge * br,const struct net_bridge_fdb_entry * fdb)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 
fdb_rcu_free(struct rcu_head * head)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 
fdb_find_rcu(struct rhashtable * tbl,const unsigned char * addr,__u16 vid)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 */
br_fdb_find(struct net_bridge * br,const unsigned char * addr,__u16 vid)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 
br_fdb_find_port(const struct net_device * br_dev,const unsigned char * addr,__u16 vid)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 
br_fdb_find_rcu(struct net_bridge * br,const unsigned char * addr,__u16 vid)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  */
fdb_add_hw_addr(struct net_bridge * br,const unsigned char * addr)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  */
fdb_del_hw_addr(struct net_bridge * br,const unsigned char * addr)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 
fdb_delete(struct net_bridge * br,struct net_bridge_fdb_entry * f,bool swdev_notify)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. */
fdb_delete_local(struct net_bridge * br,const struct net_bridge_port * p,struct net_bridge_fdb_entry * f)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 
br_fdb_find_delete_local(struct net_bridge * br,const struct net_bridge_port * p,const unsigned char * addr,u16 vid)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 
br_fdb_changeaddr(struct net_bridge_port * p,const unsigned char * newaddr)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 
br_fdb_change_mac_address(struct net_bridge * br,const u8 * newaddr)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 
br_fdb_cleanup(struct work_struct * work)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.*/
br_fdb_flush(struct net_bridge * br)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  */
br_fdb_delete_by_port(struct net_bridge * br,const struct net_bridge_port * p,u16 vid,int do_all)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 */
br_fdb_test_addr(struct net_device * dev,unsigned char * addr)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  */
br_fdb_fillbuf(struct net_bridge * br,void * buf,unsigned long maxnum,unsigned long skip)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 
fdb_create(struct net_bridge * br,struct net_bridge_port * source,const unsigned char * addr,__u16 vid,unsigned long flags)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 
fdb_insert(struct net_bridge * br,struct net_bridge_port * source,const unsigned char * addr,u16 vid)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 */
br_fdb_insert(struct net_bridge * br,struct net_bridge_port * source,const unsigned char * addr,u16 vid)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 */
__fdb_mark_active(struct net_bridge_fdb_entry * fdb)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 
br_fdb_update(struct net_bridge * br,struct net_bridge_port * source,const unsigned char * addr,u16 vid,unsigned long flags)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 
fdb_to_nud(const struct net_bridge * br,const struct net_bridge_fdb_entry * fdb)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 
fdb_fill_info(struct sk_buff * skb,const struct net_bridge * br,const struct net_bridge_fdb_entry * fdb,u32 portid,u32 seq,int type,unsigned int flags)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 
fdb_nlmsg_size(void)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 
fdb_notify(struct net_bridge * br,const struct net_bridge_fdb_entry * fdb,int type,bool swdev_notify)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 */
br_fdb_dump(struct sk_buff * skb,struct netlink_callback * cb,struct net_device * dev,struct net_device * filter_dev,int * idx)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 
br_fdb_get(struct sk_buff * skb,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u32 portid,u32 seq,struct netlink_ext_ack * extack)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 */
fdb_handle_notify(struct net_bridge_fdb_entry * fdb,u8 notify)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 */
fdb_add_entry(struct net_bridge * br,struct net_bridge_port * source,const u8 * addr,struct ndmsg * ndm,u16 flags,u16 vid,struct nlattr * nfea_tb[])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 
__br_fdb_add(struct ndmsg * ndm,struct net_bridge * br,struct net_bridge_port * p,const unsigned char * addr,u16 nlh_flags,u16 vid,struct nlattr * nfea_tb[],struct netlink_ext_ack * extack)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 			struct netlink_ext_ack *extack)
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 		if (!p && !(ndm->ndm_state & NUD_PERMANENT)) {
974 			NL_SET_ERR_MSG_MOD(extack,
975 					   "FDB entry towards bridge must be permanent");
976 			return -EINVAL;
977 		}
978 		err = br_fdb_external_learn_add(br, p, addr, vid, true);
979 	} else {
980 		spin_lock_bh(&br->hash_lock);
981 		err = fdb_add_entry(br, p, addr, ndm, nlh_flags, vid, nfea_tb);
982 		spin_unlock_bh(&br->hash_lock);
983 	}
984 
985 	return err;
986 }
987 
988 static const struct nla_policy br_nda_fdb_pol[NFEA_MAX + 1] = {
989 	[NFEA_ACTIVITY_NOTIFY]	= { .type = NLA_U8 },
990 	[NFEA_DONT_REFRESH]	= { .type = NLA_FLAG },
991 };
992 
993 /* Add new permanent fdb entry with RTM_NEWNEIGH */
br_fdb_add(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u16 nlh_flags,struct netlink_ext_ack * extack)994 int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
995 	       struct net_device *dev,
996 	       const unsigned char *addr, u16 vid, u16 nlh_flags,
997 	       struct netlink_ext_ack *extack)
998 {
999 	struct nlattr *nfea_tb[NFEA_MAX + 1], *attr;
1000 	struct net_bridge_vlan_group *vg;
1001 	struct net_bridge_port *p = NULL;
1002 	struct net_bridge_vlan *v;
1003 	struct net_bridge *br = NULL;
1004 	int err = 0;
1005 
1006 	trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);
1007 
1008 	if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
1009 		pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
1010 		return -EINVAL;
1011 	}
1012 
1013 	if (is_zero_ether_addr(addr)) {
1014 		pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
1015 		return -EINVAL;
1016 	}
1017 
1018 	if (dev->priv_flags & IFF_EBRIDGE) {
1019 		br = netdev_priv(dev);
1020 		vg = br_vlan_group(br);
1021 	} else {
1022 		p = br_port_get_rtnl(dev);
1023 		if (!p) {
1024 			pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
1025 				dev->name);
1026 			return -EINVAL;
1027 		}
1028 		br = p->br;
1029 		vg = nbp_vlan_group(p);
1030 	}
1031 
1032 	if (tb[NDA_FDB_EXT_ATTRS]) {
1033 		attr = tb[NDA_FDB_EXT_ATTRS];
1034 		err = nla_parse_nested(nfea_tb, NFEA_MAX, attr,
1035 				       br_nda_fdb_pol, extack);
1036 		if (err)
1037 			return err;
1038 	} else {
1039 		memset(nfea_tb, 0, sizeof(struct nlattr *) * (NFEA_MAX + 1));
1040 	}
1041 
1042 	if (vid) {
1043 		v = br_vlan_find(vg, vid);
1044 		if (!v || !br_vlan_should_use(v)) {
1045 			pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
1046 			return -EINVAL;
1047 		}
1048 
1049 		/* VID was specified, so use it. */
1050 		err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid, nfea_tb,
1051 				   extack);
1052 	} else {
1053 		err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0, nfea_tb,
1054 				   extack);
1055 		if (err || !vg || !vg->num_vlans)
1056 			goto out;
1057 
1058 		/* We have vlans configured on this port and user didn't
1059 		 * specify a VLAN.  To be nice, add/update entry for every
1060 		 * vlan on this port.
1061 		 */
1062 		list_for_each_entry(v, &vg->vlan_list, vlist) {
1063 			if (!br_vlan_should_use(v))
1064 				continue;
1065 			err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid,
1066 					   nfea_tb, extack);
1067 			if (err)
1068 				goto out;
1069 		}
1070 	}
1071 
1072 out:
1073 	return err;
1074 }
1075 
fdb_delete_by_addr_and_port(struct net_bridge * br,const struct net_bridge_port * p,const u8 * addr,u16 vlan)1076 static int fdb_delete_by_addr_and_port(struct net_bridge *br,
1077 				       const struct net_bridge_port *p,
1078 				       const u8 *addr, u16 vlan)
1079 {
1080 	struct net_bridge_fdb_entry *fdb;
1081 
1082 	fdb = br_fdb_find(br, addr, vlan);
1083 	if (!fdb || fdb->dst != p)
1084 		return -ENOENT;
1085 
1086 	fdb_delete(br, fdb, true);
1087 
1088 	return 0;
1089 }
1090 
__br_fdb_delete(struct net_bridge * br,const struct net_bridge_port * p,const unsigned char * addr,u16 vid)1091 static int __br_fdb_delete(struct net_bridge *br,
1092 			   const struct net_bridge_port *p,
1093 			   const unsigned char *addr, u16 vid)
1094 {
1095 	int err;
1096 
1097 	spin_lock_bh(&br->hash_lock);
1098 	err = fdb_delete_by_addr_and_port(br, p, addr, vid);
1099 	spin_unlock_bh(&br->hash_lock);
1100 
1101 	return err;
1102 }
1103 
1104 /* Remove neighbor entry with RTM_DELNEIGH */
br_fdb_delete(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid)1105 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1106 		  struct net_device *dev,
1107 		  const unsigned char *addr, u16 vid)
1108 {
1109 	struct net_bridge_vlan_group *vg;
1110 	struct net_bridge_port *p = NULL;
1111 	struct net_bridge_vlan *v;
1112 	struct net_bridge *br;
1113 	int err;
1114 
1115 	if (dev->priv_flags & IFF_EBRIDGE) {
1116 		br = netdev_priv(dev);
1117 		vg = br_vlan_group(br);
1118 	} else {
1119 		p = br_port_get_rtnl(dev);
1120 		if (!p) {
1121 			pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
1122 				dev->name);
1123 			return -EINVAL;
1124 		}
1125 		vg = nbp_vlan_group(p);
1126 		br = p->br;
1127 	}
1128 
1129 	if (vid) {
1130 		v = br_vlan_find(vg, vid);
1131 		if (!v) {
1132 			pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
1133 			return -EINVAL;
1134 		}
1135 
1136 		err = __br_fdb_delete(br, p, addr, vid);
1137 	} else {
1138 		err = -ENOENT;
1139 		err &= __br_fdb_delete(br, p, addr, 0);
1140 		if (!vg || !vg->num_vlans)
1141 			return err;
1142 
1143 		list_for_each_entry(v, &vg->vlan_list, vlist) {
1144 			if (!br_vlan_should_use(v))
1145 				continue;
1146 			err &= __br_fdb_delete(br, p, addr, v->vid);
1147 		}
1148 	}
1149 
1150 	return err;
1151 }
1152 
br_fdb_sync_static(struct net_bridge * br,struct net_bridge_port * p)1153 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
1154 {
1155 	struct net_bridge_fdb_entry *f, *tmp;
1156 	int err = 0;
1157 
1158 	ASSERT_RTNL();
1159 
1160 	/* the key here is that static entries change only under rtnl */
1161 	rcu_read_lock();
1162 	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1163 		/* We only care for static entries */
1164 		if (!test_bit(BR_FDB_STATIC, &f->flags))
1165 			continue;
1166 		err = dev_uc_add(p->dev, f->key.addr.addr);
1167 		if (err)
1168 			goto rollback;
1169 	}
1170 done:
1171 	rcu_read_unlock();
1172 
1173 	return err;
1174 
1175 rollback:
1176 	hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
1177 		/* We only care for static entries */
1178 		if (!test_bit(BR_FDB_STATIC, &tmp->flags))
1179 			continue;
1180 		if (tmp == f)
1181 			break;
1182 		dev_uc_del(p->dev, tmp->key.addr.addr);
1183 	}
1184 
1185 	goto done;
1186 }
1187 
br_fdb_unsync_static(struct net_bridge * br,struct net_bridge_port * p)1188 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
1189 {
1190 	struct net_bridge_fdb_entry *f;
1191 
1192 	ASSERT_RTNL();
1193 
1194 	rcu_read_lock();
1195 	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1196 		/* We only care for static entries */
1197 		if (!test_bit(BR_FDB_STATIC, &f->flags))
1198 			continue;
1199 
1200 		dev_uc_del(p->dev, f->key.addr.addr);
1201 	}
1202 	rcu_read_unlock();
1203 }
1204 
br_fdb_external_learn_add(struct net_bridge * br,struct net_bridge_port * p,const unsigned char * addr,u16 vid,bool swdev_notify)1205 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
1206 			      const unsigned char *addr, u16 vid,
1207 			      bool swdev_notify)
1208 {
1209 	struct net_bridge_fdb_entry *fdb;
1210 	bool modified = false;
1211 	int err = 0;
1212 
1213 	trace_br_fdb_external_learn_add(br, p, addr, vid);
1214 
1215 	spin_lock_bh(&br->hash_lock);
1216 
1217 	fdb = br_fdb_find(br, addr, vid);
1218 	if (!fdb) {
1219 		unsigned long flags = BIT(BR_FDB_ADDED_BY_EXT_LEARN);
1220 
1221 		if (swdev_notify)
1222 			flags |= BIT(BR_FDB_ADDED_BY_USER);
1223 
1224 		if (!p)
1225 			flags |= BIT(BR_FDB_LOCAL);
1226 
1227 		fdb = fdb_create(br, p, addr, vid, flags);
1228 		if (!fdb) {
1229 			err = -ENOMEM;
1230 			goto err_unlock;
1231 		}
1232 		fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1233 	} else {
1234 		fdb->updated = jiffies;
1235 
1236 		if (fdb->dst != p) {
1237 			fdb->dst = p;
1238 			modified = true;
1239 		}
1240 
1241 		if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) {
1242 			/* Refresh entry */
1243 			fdb->used = jiffies;
1244 		} else if (!test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags)) {
1245 			/* Take over SW learned entry */
1246 			set_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags);
1247 			modified = true;
1248 		}
1249 
1250 		if (swdev_notify)
1251 			set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
1252 
1253 		if (!p)
1254 			set_bit(BR_FDB_LOCAL, &fdb->flags);
1255 
1256 		if (modified)
1257 			fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1258 	}
1259 
1260 err_unlock:
1261 	spin_unlock_bh(&br->hash_lock);
1262 
1263 	return err;
1264 }
1265 
br_fdb_external_learn_del(struct net_bridge * br,struct net_bridge_port * p,const unsigned char * addr,u16 vid,bool swdev_notify)1266 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
1267 			      const unsigned char *addr, u16 vid,
1268 			      bool swdev_notify)
1269 {
1270 	struct net_bridge_fdb_entry *fdb;
1271 	int err = 0;
1272 
1273 	spin_lock_bh(&br->hash_lock);
1274 
1275 	fdb = br_fdb_find(br, addr, vid);
1276 	if (fdb && test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags))
1277 		fdb_delete(br, fdb, swdev_notify);
1278 	else
1279 		err = -ENOENT;
1280 
1281 	spin_unlock_bh(&br->hash_lock);
1282 
1283 	return err;
1284 }
1285 
br_fdb_offloaded_set(struct net_bridge * br,struct net_bridge_port * p,const unsigned char * addr,u16 vid,bool offloaded)1286 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
1287 			  const unsigned char *addr, u16 vid, bool offloaded)
1288 {
1289 	struct net_bridge_fdb_entry *fdb;
1290 
1291 	spin_lock_bh(&br->hash_lock);
1292 
1293 	fdb = br_fdb_find(br, addr, vid);
1294 	if (fdb && offloaded != test_bit(BR_FDB_OFFLOADED, &fdb->flags))
1295 		change_bit(BR_FDB_OFFLOADED, &fdb->flags);
1296 
1297 	spin_unlock_bh(&br->hash_lock);
1298 }
1299 
br_fdb_clear_offload(const struct net_device * dev,u16 vid)1300 void br_fdb_clear_offload(const struct net_device *dev, u16 vid)
1301 {
1302 	struct net_bridge_fdb_entry *f;
1303 	struct net_bridge_port *p;
1304 
1305 	ASSERT_RTNL();
1306 
1307 	p = br_port_get_rtnl(dev);
1308 	if (!p)
1309 		return;
1310 
1311 	spin_lock_bh(&p->br->hash_lock);
1312 	hlist_for_each_entry(f, &p->br->fdb_list, fdb_node) {
1313 		if (f->dst == p && f->key.vlan_id == vid)
1314 			clear_bit(BR_FDB_OFFLOADED, &f->flags);
1315 	}
1316 	spin_unlock_bh(&p->br->hash_lock);
1317 }
1318 EXPORT_SYMBOL_GPL(br_fdb_clear_offload);
1319