Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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