18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *	Device handling code
48c2ecf20Sopenharmony_ci *	Linux ethernet bridge
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *	Authors:
78c2ecf20Sopenharmony_ci *	Lennert Buytenhek		<buytenh@gnu.org>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
128c2ecf20Sopenharmony_ci#include <linux/netpoll.h>
138c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
148c2ecf20Sopenharmony_ci#include <linux/ethtool.h>
158c2ecf20Sopenharmony_ci#include <linux/list.h>
168c2ecf20Sopenharmony_ci#include <linux/netfilter_bridge.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
198c2ecf20Sopenharmony_ci#include "br_private.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define COMMON_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | \
228c2ecf20Sopenharmony_ci			 NETIF_F_GSO_MASK | NETIF_F_HW_CSUM)
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciconst struct nf_br_ops __rcu *nf_br_ops __read_mostly;
258c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(nf_br_ops);
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* net device transmit always called with BH disabled */
288c2ecf20Sopenharmony_cinetdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	struct net_bridge *br = netdev_priv(dev);
318c2ecf20Sopenharmony_ci	struct net_bridge_fdb_entry *dst;
328c2ecf20Sopenharmony_ci	struct net_bridge_mdb_entry *mdst;
338c2ecf20Sopenharmony_ci	struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats);
348c2ecf20Sopenharmony_ci	const struct nf_br_ops *nf_ops;
358c2ecf20Sopenharmony_ci	u8 state = BR_STATE_FORWARDING;
368c2ecf20Sopenharmony_ci	const unsigned char *dest;
378c2ecf20Sopenharmony_ci	u16 vid = 0;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	memset(skb->cb, 0, sizeof(struct br_input_skb_cb));
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	rcu_read_lock();
428c2ecf20Sopenharmony_ci	nf_ops = rcu_dereference(nf_br_ops);
438c2ecf20Sopenharmony_ci	if (nf_ops && nf_ops->br_dev_xmit_hook(skb)) {
448c2ecf20Sopenharmony_ci		rcu_read_unlock();
458c2ecf20Sopenharmony_ci		return NETDEV_TX_OK;
468c2ecf20Sopenharmony_ci	}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	u64_stats_update_begin(&brstats->syncp);
498c2ecf20Sopenharmony_ci	brstats->tx_packets++;
508c2ecf20Sopenharmony_ci	brstats->tx_bytes += skb->len;
518c2ecf20Sopenharmony_ci	u64_stats_update_end(&brstats->syncp);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	br_switchdev_frame_unmark(skb);
548c2ecf20Sopenharmony_ci	BR_INPUT_SKB_CB(skb)->brdev = dev;
558c2ecf20Sopenharmony_ci	BR_INPUT_SKB_CB(skb)->frag_max_size = 0;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	skb_reset_mac_header(skb);
588c2ecf20Sopenharmony_ci	skb_pull(skb, ETH_HLEN);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	if (!br_allowed_ingress(br, br_vlan_group_rcu(br), skb, &vid, &state))
618c2ecf20Sopenharmony_ci		goto out;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_INET) &&
648c2ecf20Sopenharmony_ci	    (eth_hdr(skb)->h_proto == htons(ETH_P_ARP) ||
658c2ecf20Sopenharmony_ci	     eth_hdr(skb)->h_proto == htons(ETH_P_RARP)) &&
668c2ecf20Sopenharmony_ci	    br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED)) {
678c2ecf20Sopenharmony_ci		br_do_proxy_suppress_arp(skb, br, vid, NULL);
688c2ecf20Sopenharmony_ci	} else if (IS_ENABLED(CONFIG_IPV6) &&
698c2ecf20Sopenharmony_ci		   skb->protocol == htons(ETH_P_IPV6) &&
708c2ecf20Sopenharmony_ci		   br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) &&
718c2ecf20Sopenharmony_ci		   pskb_may_pull(skb, sizeof(struct ipv6hdr) +
728c2ecf20Sopenharmony_ci				 sizeof(struct nd_msg)) &&
738c2ecf20Sopenharmony_ci		   ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
748c2ecf20Sopenharmony_ci			struct nd_msg *msg, _msg;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci			msg = br_is_nd_neigh_msg(skb, &_msg);
778c2ecf20Sopenharmony_ci			if (msg)
788c2ecf20Sopenharmony_ci				br_do_suppress_nd(skb, br, vid, NULL, msg);
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	dest = eth_hdr(skb)->h_dest;
828c2ecf20Sopenharmony_ci	if (is_broadcast_ether_addr(dest)) {
838c2ecf20Sopenharmony_ci		br_flood(br, skb, BR_PKT_BROADCAST, false, true);
848c2ecf20Sopenharmony_ci	} else if (is_multicast_ether_addr(dest)) {
858c2ecf20Sopenharmony_ci		if (unlikely(netpoll_tx_running(dev))) {
868c2ecf20Sopenharmony_ci			br_flood(br, skb, BR_PKT_MULTICAST, false, true);
878c2ecf20Sopenharmony_ci			goto out;
888c2ecf20Sopenharmony_ci		}
898c2ecf20Sopenharmony_ci		if (br_multicast_rcv(br, NULL, skb, vid)) {
908c2ecf20Sopenharmony_ci			kfree_skb(skb);
918c2ecf20Sopenharmony_ci			goto out;
928c2ecf20Sopenharmony_ci		}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci		mdst = br_mdb_get(br, skb, vid);
958c2ecf20Sopenharmony_ci		if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
968c2ecf20Sopenharmony_ci		    br_multicast_querier_exists(br, eth_hdr(skb)))
978c2ecf20Sopenharmony_ci			br_multicast_flood(mdst, skb, false, true);
988c2ecf20Sopenharmony_ci		else
998c2ecf20Sopenharmony_ci			br_flood(br, skb, BR_PKT_MULTICAST, false, true);
1008c2ecf20Sopenharmony_ci	} else if ((dst = br_fdb_find_rcu(br, dest, vid)) != NULL) {
1018c2ecf20Sopenharmony_ci		br_forward(dst->dst, skb, false, true);
1028c2ecf20Sopenharmony_ci	} else {
1038c2ecf20Sopenharmony_ci		br_flood(br, skb, BR_PKT_UNICAST, false, true);
1048c2ecf20Sopenharmony_ci	}
1058c2ecf20Sopenharmony_ciout:
1068c2ecf20Sopenharmony_ci	rcu_read_unlock();
1078c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic struct lock_class_key bridge_netdev_addr_lock_key;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic void br_set_lockdep_class(struct net_device *dev)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	lockdep_set_class(&dev->addr_list_lock, &bridge_netdev_addr_lock_key);
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic int br_dev_init(struct net_device *dev)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	struct net_bridge *br = netdev_priv(dev);
1208c2ecf20Sopenharmony_ci	int err;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	br->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1238c2ecf20Sopenharmony_ci	if (!br->stats)
1248c2ecf20Sopenharmony_ci		return -ENOMEM;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	err = br_fdb_hash_init(br);
1278c2ecf20Sopenharmony_ci	if (err) {
1288c2ecf20Sopenharmony_ci		free_percpu(br->stats);
1298c2ecf20Sopenharmony_ci		return err;
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	err = br_mdb_hash_init(br);
1338c2ecf20Sopenharmony_ci	if (err) {
1348c2ecf20Sopenharmony_ci		free_percpu(br->stats);
1358c2ecf20Sopenharmony_ci		br_fdb_hash_fini(br);
1368c2ecf20Sopenharmony_ci		return err;
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	err = br_vlan_init(br);
1408c2ecf20Sopenharmony_ci	if (err) {
1418c2ecf20Sopenharmony_ci		free_percpu(br->stats);
1428c2ecf20Sopenharmony_ci		br_mdb_hash_fini(br);
1438c2ecf20Sopenharmony_ci		br_fdb_hash_fini(br);
1448c2ecf20Sopenharmony_ci		return err;
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	err = br_multicast_init_stats(br);
1488c2ecf20Sopenharmony_ci	if (err) {
1498c2ecf20Sopenharmony_ci		free_percpu(br->stats);
1508c2ecf20Sopenharmony_ci		br_vlan_flush(br);
1518c2ecf20Sopenharmony_ci		br_mdb_hash_fini(br);
1528c2ecf20Sopenharmony_ci		br_fdb_hash_fini(br);
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	br_set_lockdep_class(dev);
1568c2ecf20Sopenharmony_ci	return err;
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic void br_dev_uninit(struct net_device *dev)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	struct net_bridge *br = netdev_priv(dev);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	br_multicast_dev_del(br);
1648c2ecf20Sopenharmony_ci	br_multicast_uninit_stats(br);
1658c2ecf20Sopenharmony_ci	br_vlan_flush(br);
1668c2ecf20Sopenharmony_ci	br_mdb_hash_fini(br);
1678c2ecf20Sopenharmony_ci	br_fdb_hash_fini(br);
1688c2ecf20Sopenharmony_ci	free_percpu(br->stats);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic int br_dev_open(struct net_device *dev)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	struct net_bridge *br = netdev_priv(dev);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	netdev_update_features(dev);
1768c2ecf20Sopenharmony_ci	netif_start_queue(dev);
1778c2ecf20Sopenharmony_ci	br_stp_enable_bridge(br);
1788c2ecf20Sopenharmony_ci	br_multicast_open(br);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
1818c2ecf20Sopenharmony_ci		br_multicast_join_snoopers(br);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	return 0;
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic void br_dev_set_multicast_list(struct net_device *dev)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic void br_dev_change_rx_flags(struct net_device *dev, int change)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	if (change & IFF_PROMISC)
1938c2ecf20Sopenharmony_ci		br_manage_promisc(netdev_priv(dev));
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic int br_dev_stop(struct net_device *dev)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	struct net_bridge *br = netdev_priv(dev);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	br_stp_disable_bridge(br);
2018c2ecf20Sopenharmony_ci	br_multicast_stop(br);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
2048c2ecf20Sopenharmony_ci		br_multicast_leave_snoopers(br);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	netif_stop_queue(dev);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	return 0;
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistatic void br_get_stats64(struct net_device *dev,
2128c2ecf20Sopenharmony_ci			   struct rtnl_link_stats64 *stats)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	struct net_bridge *br = netdev_priv(dev);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	netdev_stats_to_stats64(stats, &dev->stats);
2178c2ecf20Sopenharmony_ci	dev_fetch_sw_netstats(stats, br->stats);
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic int br_change_mtu(struct net_device *dev, int new_mtu)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	struct net_bridge *br = netdev_priv(dev);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	dev->mtu = new_mtu;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	/* this flag will be cleared if the MTU was automatically adjusted */
2278c2ecf20Sopenharmony_ci	br_opt_toggle(br, BROPT_MTU_SET_BY_USER, true);
2288c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
2298c2ecf20Sopenharmony_ci	/* remember the MTU in the rtable for PMTU */
2308c2ecf20Sopenharmony_ci	dst_metric_set(&br->fake_rtable.dst, RTAX_MTU, new_mtu);
2318c2ecf20Sopenharmony_ci#endif
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	return 0;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci/* Allow setting mac address to any valid ethernet address. */
2378c2ecf20Sopenharmony_cistatic int br_set_mac_address(struct net_device *dev, void *p)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	struct net_bridge *br = netdev_priv(dev);
2408c2ecf20Sopenharmony_ci	struct sockaddr *addr = p;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	if (!is_valid_ether_addr(addr->sa_data))
2438c2ecf20Sopenharmony_ci		return -EADDRNOTAVAIL;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	/* dev_set_mac_addr() can be called by a master device on bridge's
2468c2ecf20Sopenharmony_ci	 * NETDEV_UNREGISTER, but since it's being destroyed do nothing
2478c2ecf20Sopenharmony_ci	 */
2488c2ecf20Sopenharmony_ci	if (dev->reg_state != NETREG_REGISTERED)
2498c2ecf20Sopenharmony_ci		return -EBUSY;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	spin_lock_bh(&br->lock);
2528c2ecf20Sopenharmony_ci	if (!ether_addr_equal(dev->dev_addr, addr->sa_data)) {
2538c2ecf20Sopenharmony_ci		/* Mac address will be changed in br_stp_change_bridge_id(). */
2548c2ecf20Sopenharmony_ci		br_stp_change_bridge_id(br, addr->sa_data);
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci	spin_unlock_bh(&br->lock);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	return 0;
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	strlcpy(info->driver, "bridge", sizeof(info->driver));
2648c2ecf20Sopenharmony_ci	strlcpy(info->version, BR_VERSION, sizeof(info->version));
2658c2ecf20Sopenharmony_ci	strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
2668c2ecf20Sopenharmony_ci	strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
2678c2ecf20Sopenharmony_ci}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cistatic int br_get_link_ksettings(struct net_device *dev,
2708c2ecf20Sopenharmony_ci				 struct ethtool_link_ksettings *cmd)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	struct net_bridge *br = netdev_priv(dev);
2738c2ecf20Sopenharmony_ci	struct net_bridge_port *p;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	cmd->base.duplex = DUPLEX_UNKNOWN;
2768c2ecf20Sopenharmony_ci	cmd->base.port = PORT_OTHER;
2778c2ecf20Sopenharmony_ci	cmd->base.speed = SPEED_UNKNOWN;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	list_for_each_entry(p, &br->port_list, list) {
2808c2ecf20Sopenharmony_ci		struct ethtool_link_ksettings ecmd;
2818c2ecf20Sopenharmony_ci		struct net_device *pdev = p->dev;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci		if (!netif_running(pdev) || !netif_oper_up(pdev))
2848c2ecf20Sopenharmony_ci			continue;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci		if (__ethtool_get_link_ksettings(pdev, &ecmd))
2878c2ecf20Sopenharmony_ci			continue;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci		if (ecmd.base.speed == (__u32)SPEED_UNKNOWN)
2908c2ecf20Sopenharmony_ci			continue;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci		if (cmd->base.speed == (__u32)SPEED_UNKNOWN ||
2938c2ecf20Sopenharmony_ci		    cmd->base.speed < ecmd.base.speed)
2948c2ecf20Sopenharmony_ci			cmd->base.speed = ecmd.base.speed;
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	return 0;
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic netdev_features_t br_fix_features(struct net_device *dev,
3018c2ecf20Sopenharmony_ci	netdev_features_t features)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	struct net_bridge *br = netdev_priv(dev);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	return br_features_recompute(br, features);
3068c2ecf20Sopenharmony_ci}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci#ifdef CONFIG_NET_POLL_CONTROLLER
3098c2ecf20Sopenharmony_cistatic void br_poll_controller(struct net_device *br_dev)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_cistatic void br_netpoll_cleanup(struct net_device *dev)
3148c2ecf20Sopenharmony_ci{
3158c2ecf20Sopenharmony_ci	struct net_bridge *br = netdev_priv(dev);
3168c2ecf20Sopenharmony_ci	struct net_bridge_port *p;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	list_for_each_entry(p, &br->port_list, list)
3198c2ecf20Sopenharmony_ci		br_netpoll_disable(p);
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic int __br_netpoll_enable(struct net_bridge_port *p)
3238c2ecf20Sopenharmony_ci{
3248c2ecf20Sopenharmony_ci	struct netpoll *np;
3258c2ecf20Sopenharmony_ci	int err;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	np = kzalloc(sizeof(*p->np), GFP_KERNEL);
3288c2ecf20Sopenharmony_ci	if (!np)
3298c2ecf20Sopenharmony_ci		return -ENOMEM;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	err = __netpoll_setup(np, p->dev);
3328c2ecf20Sopenharmony_ci	if (err) {
3338c2ecf20Sopenharmony_ci		kfree(np);
3348c2ecf20Sopenharmony_ci		return err;
3358c2ecf20Sopenharmony_ci	}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	p->np = np;
3388c2ecf20Sopenharmony_ci	return err;
3398c2ecf20Sopenharmony_ci}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ciint br_netpoll_enable(struct net_bridge_port *p)
3428c2ecf20Sopenharmony_ci{
3438c2ecf20Sopenharmony_ci	if (!p->br->dev->npinfo)
3448c2ecf20Sopenharmony_ci		return 0;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	return __br_netpoll_enable(p);
3478c2ecf20Sopenharmony_ci}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_cistatic int br_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	struct net_bridge *br = netdev_priv(dev);
3528c2ecf20Sopenharmony_ci	struct net_bridge_port *p;
3538c2ecf20Sopenharmony_ci	int err = 0;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	list_for_each_entry(p, &br->port_list, list) {
3568c2ecf20Sopenharmony_ci		if (!p->dev)
3578c2ecf20Sopenharmony_ci			continue;
3588c2ecf20Sopenharmony_ci		err = __br_netpoll_enable(p);
3598c2ecf20Sopenharmony_ci		if (err)
3608c2ecf20Sopenharmony_ci			goto fail;
3618c2ecf20Sopenharmony_ci	}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ciout:
3648c2ecf20Sopenharmony_ci	return err;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cifail:
3678c2ecf20Sopenharmony_ci	br_netpoll_cleanup(dev);
3688c2ecf20Sopenharmony_ci	goto out;
3698c2ecf20Sopenharmony_ci}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_civoid br_netpoll_disable(struct net_bridge_port *p)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	struct netpoll *np = p->np;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	if (!np)
3768c2ecf20Sopenharmony_ci		return;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	p->np = NULL;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	__netpoll_free(np);
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci#endif
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_cistatic int br_add_slave(struct net_device *dev, struct net_device *slave_dev,
3868c2ecf20Sopenharmony_ci			struct netlink_ext_ack *extack)
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci{
3898c2ecf20Sopenharmony_ci	struct net_bridge *br = netdev_priv(dev);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	return br_add_if(br, slave_dev, extack);
3928c2ecf20Sopenharmony_ci}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_cistatic int br_del_slave(struct net_device *dev, struct net_device *slave_dev)
3958c2ecf20Sopenharmony_ci{
3968c2ecf20Sopenharmony_ci	struct net_bridge *br = netdev_priv(dev);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	return br_del_if(br, slave_dev);
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistatic const struct ethtool_ops br_ethtool_ops = {
4028c2ecf20Sopenharmony_ci	.get_drvinfo		 = br_getinfo,
4038c2ecf20Sopenharmony_ci	.get_link		 = ethtool_op_get_link,
4048c2ecf20Sopenharmony_ci	.get_link_ksettings	 = br_get_link_ksettings,
4058c2ecf20Sopenharmony_ci};
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistatic const struct net_device_ops br_netdev_ops = {
4088c2ecf20Sopenharmony_ci	.ndo_open		 = br_dev_open,
4098c2ecf20Sopenharmony_ci	.ndo_stop		 = br_dev_stop,
4108c2ecf20Sopenharmony_ci	.ndo_init		 = br_dev_init,
4118c2ecf20Sopenharmony_ci	.ndo_uninit		 = br_dev_uninit,
4128c2ecf20Sopenharmony_ci	.ndo_start_xmit		 = br_dev_xmit,
4138c2ecf20Sopenharmony_ci	.ndo_get_stats64	 = br_get_stats64,
4148c2ecf20Sopenharmony_ci	.ndo_set_mac_address	 = br_set_mac_address,
4158c2ecf20Sopenharmony_ci	.ndo_set_rx_mode	 = br_dev_set_multicast_list,
4168c2ecf20Sopenharmony_ci	.ndo_change_rx_flags	 = br_dev_change_rx_flags,
4178c2ecf20Sopenharmony_ci	.ndo_change_mtu		 = br_change_mtu,
4188c2ecf20Sopenharmony_ci	.ndo_do_ioctl		 = br_dev_ioctl,
4198c2ecf20Sopenharmony_ci#ifdef CONFIG_NET_POLL_CONTROLLER
4208c2ecf20Sopenharmony_ci	.ndo_netpoll_setup	 = br_netpoll_setup,
4218c2ecf20Sopenharmony_ci	.ndo_netpoll_cleanup	 = br_netpoll_cleanup,
4228c2ecf20Sopenharmony_ci	.ndo_poll_controller	 = br_poll_controller,
4238c2ecf20Sopenharmony_ci#endif
4248c2ecf20Sopenharmony_ci	.ndo_add_slave		 = br_add_slave,
4258c2ecf20Sopenharmony_ci	.ndo_del_slave		 = br_del_slave,
4268c2ecf20Sopenharmony_ci	.ndo_fix_features        = br_fix_features,
4278c2ecf20Sopenharmony_ci	.ndo_fdb_add		 = br_fdb_add,
4288c2ecf20Sopenharmony_ci	.ndo_fdb_del		 = br_fdb_delete,
4298c2ecf20Sopenharmony_ci	.ndo_fdb_dump		 = br_fdb_dump,
4308c2ecf20Sopenharmony_ci	.ndo_fdb_get		 = br_fdb_get,
4318c2ecf20Sopenharmony_ci	.ndo_bridge_getlink	 = br_getlink,
4328c2ecf20Sopenharmony_ci	.ndo_bridge_setlink	 = br_setlink,
4338c2ecf20Sopenharmony_ci	.ndo_bridge_dellink	 = br_dellink,
4348c2ecf20Sopenharmony_ci	.ndo_features_check	 = passthru_features_check,
4358c2ecf20Sopenharmony_ci};
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_cistatic struct device_type br_type = {
4388c2ecf20Sopenharmony_ci	.name	= "bridge",
4398c2ecf20Sopenharmony_ci};
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_civoid br_dev_setup(struct net_device *dev)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	struct net_bridge *br = netdev_priv(dev);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	eth_hw_addr_random(dev);
4468c2ecf20Sopenharmony_ci	ether_setup(dev);
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	dev->netdev_ops = &br_netdev_ops;
4498c2ecf20Sopenharmony_ci	dev->needs_free_netdev = true;
4508c2ecf20Sopenharmony_ci	dev->ethtool_ops = &br_ethtool_ops;
4518c2ecf20Sopenharmony_ci	SET_NETDEV_DEVTYPE(dev, &br_type);
4528c2ecf20Sopenharmony_ci	dev->priv_flags = IFF_EBRIDGE | IFF_NO_QUEUE;
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	dev->features = COMMON_FEATURES | NETIF_F_LLTX | NETIF_F_NETNS_LOCAL |
4558c2ecf20Sopenharmony_ci			NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
4568c2ecf20Sopenharmony_ci	dev->hw_features = COMMON_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
4578c2ecf20Sopenharmony_ci			   NETIF_F_HW_VLAN_STAG_TX;
4588c2ecf20Sopenharmony_ci	dev->vlan_features = COMMON_FEATURES;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	br->dev = dev;
4618c2ecf20Sopenharmony_ci	spin_lock_init(&br->lock);
4628c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&br->port_list);
4638c2ecf20Sopenharmony_ci	INIT_HLIST_HEAD(&br->fdb_list);
4648c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_BRIDGE_MRP)
4658c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&br->mrp_list);
4668c2ecf20Sopenharmony_ci#endif
4678c2ecf20Sopenharmony_ci	spin_lock_init(&br->hash_lock);
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	br->bridge_id.prio[0] = 0x80;
4708c2ecf20Sopenharmony_ci	br->bridge_id.prio[1] = 0x00;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	ether_addr_copy(br->group_addr, eth_stp_addr);
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	br->stp_enabled = BR_NO_STP;
4758c2ecf20Sopenharmony_ci	br->group_fwd_mask = BR_GROUPFWD_DEFAULT;
4768c2ecf20Sopenharmony_ci	br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	br->designated_root = br->bridge_id;
4798c2ecf20Sopenharmony_ci	br->bridge_max_age = br->max_age = 20 * HZ;
4808c2ecf20Sopenharmony_ci	br->bridge_hello_time = br->hello_time = 2 * HZ;
4818c2ecf20Sopenharmony_ci	br->bridge_forward_delay = br->forward_delay = 15 * HZ;
4828c2ecf20Sopenharmony_ci	br->bridge_ageing_time = br->ageing_time = BR_DEFAULT_AGEING_TIME;
4838c2ecf20Sopenharmony_ci	dev->max_mtu = ETH_MAX_MTU;
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	br_netfilter_rtable_init(br);
4868c2ecf20Sopenharmony_ci	br_stp_timer_init(br);
4878c2ecf20Sopenharmony_ci	br_multicast_init(br);
4888c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&br->gc_work, br_fdb_cleanup);
4898c2ecf20Sopenharmony_ci}
490