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)  *	Userspace interface
^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/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/netpoll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/ethtool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <net/dsa.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/if_vlan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <net/switchdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include "br_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * Determine initial path cost based on speed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * using recommendations from 802.1d standard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * Since driver might sleep need to not be holding any locks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static int port_cost(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct ethtool_link_ksettings ecmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	if (!__ethtool_get_link_ksettings(dev, &ecmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		switch (ecmd.base.speed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		case SPEED_10000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 			return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		case SPEED_1000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			return 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		case SPEED_100:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 			return 19;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		case SPEED_10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 			return 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		}
^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) 	/* Old silly heuristics based on name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (!strncmp(dev->name, "lec", 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (!strncmp(dev->name, "plip", 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		return 2500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	return 100;	/* assume old 10Mbps */
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) /* Check for port carrier transitions. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) void br_port_carrier_check(struct net_bridge_port *p, bool *notified)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct net_device *dev = p->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct net_bridge *br = p->br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (!(p->flags & BR_ADMIN_COST) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	    netif_running(dev) && netif_oper_up(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		p->path_cost = port_cost(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	*notified = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (!netif_running(br->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	spin_lock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (netif_running(dev) && netif_oper_up(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		if (p->state == BR_STATE_DISABLED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			br_stp_enable_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			*notified = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		if (p->state != BR_STATE_DISABLED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			br_stp_disable_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			*notified = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	spin_unlock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static void br_port_set_promisc(struct net_bridge_port *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (br_promisc_port(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	err = dev_set_promiscuity(p->dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	br_fdb_unsync_static(p->br, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	p->flags |= BR_PROMISC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static void br_port_clear_promisc(struct net_bridge_port *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	/* Check if the port is already non-promisc or if it doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	 * support UNICAST filtering.  Without unicast filtering support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	 * we'll end up re-enabling promisc mode anyway, so just check for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	 * it here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (!br_promisc_port(p) || !(p->dev->priv_flags & IFF_UNICAST_FLT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	/* Since we'll be clearing the promisc mode, program the port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	 * first so that we don't have interruption in traffic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	err = br_fdb_sync_static(p->br, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	dev_set_promiscuity(p->dev, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	p->flags &= ~BR_PROMISC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* When a port is added or removed or when certain port flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)  * change, this function is called to automatically manage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  * promiscuity setting of all the bridge ports.  We are always called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  * under RTNL so can skip using rcu primitives.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) void br_manage_promisc(struct net_bridge *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	bool set_all = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	/* If vlan filtering is disabled or bridge interface is placed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	 * into promiscuous mode, place all ports in promiscuous mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if ((br->dev->flags & IFF_PROMISC) || !br_vlan_enabled(br->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		set_all = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	list_for_each_entry(p, &br->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		if (set_all) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			br_port_set_promisc(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			/* If the number of auto-ports is <= 1, then all other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			 * ports will have their output configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			 * statically specified through fdbs.  Since ingress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			 * on the auto-port becomes forwarding/egress to other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			 * ports and egress configuration is statically known,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			 * we can say that ingress configuration of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			 * auto-port is also statically known.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			 * This lets us disable promiscuous mode and write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			 * this config to hw.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			if (br->auto_cnt == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			    (br->auto_cnt == 1 && br_auto_port(p)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				br_port_clear_promisc(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				br_port_set_promisc(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int nbp_backup_change(struct net_bridge_port *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		      struct net_device *backup_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct net_bridge_port *old_backup = rtnl_dereference(p->backup_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct net_bridge_port *backup_p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (backup_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		if (!netif_is_bridge_port(backup_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		backup_p = br_port_get_rtnl(backup_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		if (backup_p->br != p->br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (p == backup_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (old_backup == backup_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	/* if the backup link is already set, clear it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if (old_backup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		old_backup->backup_redirected_cnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (backup_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		backup_p->backup_redirected_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	rcu_assign_pointer(p->backup_port, backup_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static void nbp_backup_clear(struct net_bridge_port *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	nbp_backup_change(p, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	if (p->backup_redirected_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		struct net_bridge_port *cur_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		list_for_each_entry(cur_p, &p->br->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			struct net_bridge_port *backup_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			backup_p = rtnl_dereference(cur_p->backup_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			if (backup_p == p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 				nbp_backup_change(cur_p, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	WARN_ON(rcu_access_pointer(p->backup_port) || p->backup_redirected_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static void nbp_update_port_count(struct net_bridge *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	u32 cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	list_for_each_entry(p, &br->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		if (br_auto_port(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (br->auto_cnt != cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		br->auto_cnt = cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		br_manage_promisc(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static void nbp_delete_promisc(struct net_bridge_port *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	/* If port is currently promiscuous, unset promiscuity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	 * Otherwise, it is a static port so remove all addresses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	 * from it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	dev_set_allmulti(p->dev, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (br_promisc_port(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		dev_set_promiscuity(p->dev, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		br_fdb_unsync_static(p->br, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static void release_nbp(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	struct net_bridge_port *p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		= container_of(kobj, struct net_bridge_port, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static void brport_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	struct net_bridge_port *p = kobj_to_brport(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	net_ns_get_ownership(dev_net(p->dev), uid, gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static struct kobj_type brport_ktype = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) #ifdef CONFIG_SYSFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	.sysfs_ops = &brport_sysfs_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	.release = release_nbp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	.get_ownership = brport_get_ownership,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static void destroy_nbp(struct net_bridge_port *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	struct net_device *dev = p->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	p->br = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	p->dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	kobject_put(&p->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static void destroy_nbp_rcu(struct rcu_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	struct net_bridge_port *p =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			container_of(head, struct net_bridge_port, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	destroy_nbp(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static unsigned get_max_headroom(struct net_bridge *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	unsigned max_headroom = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	list_for_each_entry(p, &br->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		unsigned dev_headroom = netdev_get_fwd_headroom(p->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		if (dev_headroom > max_headroom)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			max_headroom = dev_headroom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	return max_headroom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static void update_headroom(struct net_bridge *br, int new_hr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	list_for_each_entry(p, &br->port_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		netdev_set_rx_headroom(p->dev, new_hr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	br->dev->needed_headroom = new_hr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /* Delete port(interface) from bridge is done in two steps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)  * via RCU. First step, marks device as down. That deletes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)  * all the timers and stops new packets from flowing through.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)  * Final cleanup doesn't occur until after all CPU's finished
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)  * processing packets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)  * Protected from multiple admin operations by RTNL mutex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static void del_nbp(struct net_bridge_port *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	struct net_bridge *br = p->br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct net_device *dev = p->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	sysfs_remove_link(br->ifobj, p->dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	nbp_delete_promisc(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	spin_lock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	br_stp_disable_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	spin_unlock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	br_mrp_port_del(br, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	br_ifinfo_notify(RTM_DELLINK, NULL, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	list_del_rcu(&p->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if (netdev_get_fwd_headroom(dev) == br->dev->needed_headroom)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		update_headroom(br, get_max_headroom(br));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	netdev_reset_rx_headroom(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	nbp_vlan_flush(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	br_fdb_delete_by_port(br, p, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	switchdev_deferred_process();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	nbp_backup_clear(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	nbp_update_port_count(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	netdev_upper_dev_unlink(dev, br->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	dev->priv_flags &= ~IFF_BRIDGE_PORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	netdev_rx_handler_unregister(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	br_multicast_del_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	kobject_uevent(&p->kobj, KOBJ_REMOVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	kobject_del(&p->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	br_netpoll_disable(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	call_rcu(&p->rcu, destroy_nbp_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) /* Delete bridge device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) void br_dev_delete(struct net_device *dev, struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	struct net_bridge *br = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	struct net_bridge_port *p, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	list_for_each_entry_safe(p, n, &br->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		del_nbp(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	br_recalculate_neigh_suppress_enabled(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	br_fdb_delete_by_port(br, NULL, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	cancel_delayed_work_sync(&br->gc_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	br_sysfs_delbr(br->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	unregister_netdevice_queue(br->dev, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) /* find an available port number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) static int find_portno(struct net_bridge *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	unsigned long *inuse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	inuse = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (!inuse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	set_bit(0, inuse);	/* zero is reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	list_for_each_entry(p, &br->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		set_bit(p->port_no, inuse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	index = find_first_zero_bit(inuse, BR_MAX_PORTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	bitmap_free(inuse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	return (index >= BR_MAX_PORTS) ? -EXFULL : index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) /* called with RTNL but without bridge lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) static struct net_bridge_port *new_nbp(struct net_bridge *br,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 				       struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	int index, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	index = find_portno(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	if (index < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		return ERR_PTR(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	p = kzalloc(sizeof(*p), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	if (p == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	p->br = br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	dev_hold(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	p->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	p->path_cost = port_cost(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	p->priority = 0x8000 >> BR_PORT_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	p->port_no = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	p->flags = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	br_init_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	br_set_state(p, BR_STATE_DISABLED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	br_stp_port_timer_init(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	err = br_multicast_add_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		p = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) int br_add_bridge(struct net *net, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	dev = alloc_netdev(sizeof(struct net_bridge), name, NET_NAME_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 			   br_dev_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	dev_net_set(dev, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	dev->rtnl_link_ops = &br_link_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	res = register_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) int br_del_bridge(struct net *net, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	dev = __dev_get_by_name(net, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	if (dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		ret =  -ENXIO; 	/* Could not find device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	else if (!(dev->priv_flags & IFF_EBRIDGE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		/* Attempt to delete non bridge device! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	else if (dev->flags & IFF_UP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		/* Not shutdown yet. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		br_dev_delete(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) /* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static int br_mtu_min(const struct net_bridge *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	const struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	int ret_mtu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	list_for_each_entry(p, &br->port_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		if (!ret_mtu || ret_mtu > p->dev->mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 			ret_mtu = p->dev->mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	return ret_mtu ? ret_mtu : ETH_DATA_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) void br_mtu_auto_adjust(struct net_bridge *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	/* if the bridge MTU was manually configured don't mess with it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	if (br_opt_get(br, BROPT_MTU_SET_BY_USER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	/* change to the minimum MTU and clear the flag which was set by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	 * the bridge ndo_change_mtu callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	dev_set_mtu(br->dev, br_mtu_min(br));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	br_opt_toggle(br, BROPT_MTU_SET_BY_USER, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) static void br_set_gso_limits(struct net_bridge *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	unsigned int gso_max_size = GSO_MAX_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	u16 gso_max_segs = GSO_MAX_SEGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	const struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	list_for_each_entry(p, &br->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		gso_max_size = min(gso_max_size, p->dev->gso_max_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		gso_max_segs = min(gso_max_segs, p->dev->gso_max_segs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	br->dev->gso_max_size = gso_max_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	br->dev->gso_max_segs = gso_max_segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)  * Recomputes features using slave's features
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) netdev_features_t br_features_recompute(struct net_bridge *br,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	netdev_features_t features)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	netdev_features_t mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	if (list_empty(&br->port_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		return features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	mask = features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	features &= ~NETIF_F_ONE_FOR_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	list_for_each_entry(p, &br->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		features = netdev_increment_features(features,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 						     p->dev->features, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	features = netdev_add_tso_features(features, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	return features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) /* called with RTNL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) int br_add_if(struct net_bridge *br, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	      struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	unsigned br_hr, dev_hr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	bool changed_addr, fdb_synced = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	/* Don't allow bridging non-ethernet like devices. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	if ((dev->flags & IFF_LOOPBACK) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	    dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	    !is_valid_ether_addr(dev->dev_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	/* Also don't allow bridging of net devices that are DSA masters, since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	 * the bridge layer rx_handler prevents the DSA fake ethertype handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	 * to be invoked, so we don't get the chance to strip off and parse the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	 * DSA switch tag protocol header (the bridge layer just returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	 * RX_HANDLER_CONSUMED, stopping RX processing for these frames).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	 * The only case where that would not be an issue is when bridging can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	 * already be offloaded, such as when the DSA master is itself a DSA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	 * or plain switchdev port, and is bridged only with other ports from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	 * the same hardware device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	if (netdev_uses_dsa(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		list_for_each_entry(p, &br->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 			if (!netdev_port_same_parent_id(dev, p->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 				NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 					       "Cannot do software bridging with a DSA master");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	/* No bridging of bridges */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 			       "Can not enslave a bridge to a bridge");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		return -ELOOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	/* Device has master upper dev */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	if (netdev_master_upper_dev_get(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	/* No bridging devices that dislike that (e.g. wireless) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	if (dev->priv_flags & IFF_DONT_BRIDGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 		NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 			       "Device does not allow enslaving to a bridge");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	p = new_nbp(br, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	if (IS_ERR(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		return PTR_ERR(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	call_netdevice_notifiers(NETDEV_JOIN, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	err = dev_set_allmulti(dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		br_multicast_del_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 		kfree(p);	/* kobject not yet init'd, manually free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		goto err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 				   SYSFS_BRIDGE_PORT_ATTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 		goto err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	err = br_sysfs_addif(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		goto err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	err = br_netpoll_enable(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		goto err3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	err = netdev_rx_handler_register(dev, br_get_rx_handler(dev), p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 		goto err4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	dev->priv_flags |= IFF_BRIDGE_PORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	err = netdev_master_upper_dev_link(dev, br->dev, NULL, NULL, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 		goto err5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	err = nbp_switchdev_mark_set(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 		goto err6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	dev_disable_lro(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	list_add_rcu(&p->list, &br->port_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	nbp_update_port_count(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	if (!br_promisc_port(p) && (p->dev->priv_flags & IFF_UNICAST_FLT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 		/* When updating the port count we also update all ports'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 		 * promiscuous mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		 * A port leaving promiscuous mode normally gets the bridge's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		 * fdb synced to the unicast filter (if supported), however,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		 * `br_port_clear_promisc` does not distinguish between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		 * non-promiscuous ports and *new* ports, so we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		 * sync explicitly here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		fdb_synced = br_fdb_sync_static(br, p) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		if (!fdb_synced)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 			netdev_err(dev, "failed to sync bridge static fdb addresses to this port\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	netdev_update_features(br->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	br_hr = br->dev->needed_headroom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	dev_hr = netdev_get_fwd_headroom(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	if (br_hr < dev_hr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		update_headroom(br, dev_hr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		netdev_set_rx_headroom(dev, br_hr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	if (br_fdb_insert(br, p, dev->dev_addr, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		netdev_err(dev, "failed insert local address bridge forwarding table\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	if (br->dev->addr_assign_type != NET_ADDR_SET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 		/* Ask for permission to use this MAC address now, even if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 		 * don't end up choosing it below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		err = dev_pre_changeaddr_notify(br->dev, dev->dev_addr, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 			goto err7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	err = nbp_vlan_init(p, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		netdev_err(dev, "failed to initialize vlan filtering on this port\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		goto err7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	spin_lock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	changed_addr = br_stp_recalculate_bridge_id(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	if (netif_running(dev) && netif_oper_up(dev) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	    (br->dev->flags & IFF_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 		br_stp_enable_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	spin_unlock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	br_ifinfo_notify(RTM_NEWLINK, NULL, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	if (changed_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	br_mtu_auto_adjust(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	br_set_gso_limits(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	kobject_uevent(&p->kobj, KOBJ_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) err7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	if (fdb_synced)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 		br_fdb_unsync_static(br, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	list_del_rcu(&p->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	br_fdb_delete_by_port(br, p, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	nbp_update_port_count(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) err6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	netdev_upper_dev_unlink(dev, br->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) err5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	dev->priv_flags &= ~IFF_BRIDGE_PORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	netdev_rx_handler_unregister(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) err4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	br_netpoll_disable(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) err3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	sysfs_remove_link(br->ifobj, p->dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) err2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	br_multicast_del_port(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	kobject_put(&p->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	dev_set_allmulti(dev, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) err1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) /* called with RTNL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) int br_del_if(struct net_bridge *br, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	bool changed_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	p = br_port_get_rtnl(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	if (!p || p->br != br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	/* Since more than one interface can be attached to a bridge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	 * there still maybe an alternate path for netconsole to use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	 * therefore there is no reason for a NETDEV_RELEASE event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	del_nbp(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	br_mtu_auto_adjust(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	br_set_gso_limits(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	spin_lock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	changed_addr = br_stp_recalculate_bridge_id(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	spin_unlock_bh(&br->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	if (changed_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 		call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	netdev_update_features(br->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) void br_port_flags_change(struct net_bridge_port *p, unsigned long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 	struct net_bridge *br = p->br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	if (mask & BR_AUTO_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 		nbp_update_port_count(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 	if (mask & BR_NEIGH_SUPPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 		br_recalculate_neigh_suppress_enabled(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) bool br_port_flag_is_set(const struct net_device *dev, unsigned long flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	struct net_bridge_port *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	p = br_port_get_rtnl_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	return p->flags & flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) EXPORT_SYMBOL_GPL(br_port_flag_is_set);