1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Linux ethernet bridge
7 * Lennert Buytenhek <buytenh@gnu.org>
10 #include <linux/kernel.h>
11 #include <linux/netdevice.h>
12 #include <linux/netpoll.h>
13 #include <linux/etherdevice.h>
14 #include <linux/ethtool.h>
15 #include <linux/list.h>
16 #include <linux/netfilter_bridge.h>
18 #include <linux/uaccess.h>
19 #include "br_private.h"
21 #define COMMON_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | \
22 NETIF_F_GSO_MASK | NETIF_F_HW_CSUM)
24 const struct nf_br_ops __rcu *nf_br_ops __read_mostly;
25 EXPORT_SYMBOL_GPL(nf_br_ops);
27 /* net device transmit always called with BH disabled */
28 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
30 struct net_bridge *br = netdev_priv(dev);
31 struct net_bridge_fdb_entry *dst;
32 struct net_bridge_mdb_entry *mdst;
33 struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats);
34 const struct nf_br_ops *nf_ops;
35 u8 state = BR_STATE_FORWARDING;
36 const unsigned char *dest;
39 memset(skb->cb, 0, sizeof(struct br_input_skb_cb));
42 nf_ops = rcu_dereference(nf_br_ops);
43 if (nf_ops && nf_ops->br_dev_xmit_hook(skb)) {
48 u64_stats_update_begin(&brstats->syncp);
49 brstats->tx_packets++;
50 brstats->tx_bytes += skb->len;
51 u64_stats_update_end(&brstats->syncp);
53 br_switchdev_frame_unmark(skb);
54 BR_INPUT_SKB_CB(skb)->brdev = dev;
55 BR_INPUT_SKB_CB(skb)->frag_max_size = 0;
57 skb_reset_mac_header(skb);
58 skb_pull(skb, ETH_HLEN);
60 if (!br_allowed_ingress(br, br_vlan_group_rcu(br), skb, &vid, &state))
63 if (IS_ENABLED(CONFIG_INET) &&
64 (eth_hdr(skb)->h_proto == htons(ETH_P_ARP) ||
65 eth_hdr(skb)->h_proto == htons(ETH_P_RARP)) &&
66 br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED)) {
67 br_do_proxy_suppress_arp(skb, br, vid, NULL);
68 } else if (IS_ENABLED(CONFIG_IPV6) &&
69 skb->protocol == htons(ETH_P_IPV6) &&
70 br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) &&
71 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
72 sizeof(struct nd_msg)) &&
73 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
74 struct nd_msg *msg, _msg;
76 msg = br_is_nd_neigh_msg(skb, &_msg);
78 br_do_suppress_nd(skb, br, vid, NULL, msg);
81 dest = eth_hdr(skb)->h_dest;
82 if (is_broadcast_ether_addr(dest)) {
83 br_flood(br, skb, BR_PKT_BROADCAST, false, true);
84 } else if (is_multicast_ether_addr(dest)) {
85 if (unlikely(netpoll_tx_running(dev))) {
86 br_flood(br, skb, BR_PKT_MULTICAST, false, true);
89 if (br_multicast_rcv(br, NULL, skb, vid)) {
94 mdst = br_mdb_get(br, skb, vid);
95 if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
96 br_multicast_querier_exists(br, eth_hdr(skb)))
97 br_multicast_flood(mdst, skb, false, true);
99 br_flood(br, skb, BR_PKT_MULTICAST, false, true);
100 } else if ((dst = br_fdb_find_rcu(br, dest, vid)) != NULL) {
101 br_forward(dst->dst, skb, false, true);
103 br_flood(br, skb, BR_PKT_UNICAST, false, true);
110 static struct lock_class_key bridge_netdev_addr_lock_key;
112 static void br_set_lockdep_class(struct net_device *dev)
114 lockdep_set_class(&dev->addr_list_lock, &bridge_netdev_addr_lock_key);
117 static int br_dev_init(struct net_device *dev)
119 struct net_bridge *br = netdev_priv(dev);
122 br->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
126 err = br_fdb_hash_init(br);
128 free_percpu(br->stats);
132 err = br_mdb_hash_init(br);
134 free_percpu(br->stats);
135 br_fdb_hash_fini(br);
139 err = br_vlan_init(br);
141 free_percpu(br->stats);
142 br_mdb_hash_fini(br);
143 br_fdb_hash_fini(br);
147 err = br_multicast_init_stats(br);
149 free_percpu(br->stats);
151 br_mdb_hash_fini(br);
152 br_fdb_hash_fini(br);
155 br_set_lockdep_class(dev);
159 static void br_dev_uninit(struct net_device *dev)
161 struct net_bridge *br = netdev_priv(dev);
163 br_multicast_dev_del(br);
164 br_multicast_uninit_stats(br);
166 br_mdb_hash_fini(br);
167 br_fdb_hash_fini(br);
168 free_percpu(br->stats);
171 static int br_dev_open(struct net_device *dev)
173 struct net_bridge *br = netdev_priv(dev);
175 netdev_update_features(dev);
176 netif_start_queue(dev);
177 br_stp_enable_bridge(br);
178 br_multicast_open(br);
183 static void br_dev_set_multicast_list(struct net_device *dev)
187 static void br_dev_change_rx_flags(struct net_device *dev, int change)
189 if (change & IFF_PROMISC)
190 br_manage_promisc(netdev_priv(dev));
193 static int br_dev_stop(struct net_device *dev)
195 struct net_bridge *br = netdev_priv(dev);
197 br_stp_disable_bridge(br);
198 br_multicast_stop(br);
200 netif_stop_queue(dev);
205 static void br_get_stats64(struct net_device *dev,
206 struct rtnl_link_stats64 *stats)
208 struct net_bridge *br = netdev_priv(dev);
210 dev_fetch_sw_netstats(stats, br->stats);
213 static int br_change_mtu(struct net_device *dev, int new_mtu)
215 struct net_bridge *br = netdev_priv(dev);
219 /* this flag will be cleared if the MTU was automatically adjusted */
220 br_opt_toggle(br, BROPT_MTU_SET_BY_USER, true);
221 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
222 /* remember the MTU in the rtable for PMTU */
223 dst_metric_set(&br->fake_rtable.dst, RTAX_MTU, new_mtu);
229 /* Allow setting mac address to any valid ethernet address. */
230 static int br_set_mac_address(struct net_device *dev, void *p)
232 struct net_bridge *br = netdev_priv(dev);
233 struct sockaddr *addr = p;
235 if (!is_valid_ether_addr(addr->sa_data))
236 return -EADDRNOTAVAIL;
238 /* dev_set_mac_addr() can be called by a master device on bridge's
239 * NETDEV_UNREGISTER, but since it's being destroyed do nothing
241 if (dev->reg_state != NETREG_REGISTERED)
244 spin_lock_bh(&br->lock);
245 if (!ether_addr_equal(dev->dev_addr, addr->sa_data)) {
246 /* Mac address will be changed in br_stp_change_bridge_id(). */
247 br_stp_change_bridge_id(br, addr->sa_data);
249 spin_unlock_bh(&br->lock);
254 static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
256 strlcpy(info->driver, "bridge", sizeof(info->driver));
257 strlcpy(info->version, BR_VERSION, sizeof(info->version));
258 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
259 strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
262 static int br_get_link_ksettings(struct net_device *dev,
263 struct ethtool_link_ksettings *cmd)
265 struct net_bridge *br = netdev_priv(dev);
266 struct net_bridge_port *p;
268 cmd->base.duplex = DUPLEX_UNKNOWN;
269 cmd->base.port = PORT_OTHER;
270 cmd->base.speed = SPEED_UNKNOWN;
272 list_for_each_entry(p, &br->port_list, list) {
273 struct ethtool_link_ksettings ecmd;
274 struct net_device *pdev = p->dev;
276 if (!netif_running(pdev) || !netif_oper_up(pdev))
279 if (__ethtool_get_link_ksettings(pdev, &ecmd))
282 if (ecmd.base.speed == (__u32)SPEED_UNKNOWN)
285 if (cmd->base.speed == (__u32)SPEED_UNKNOWN ||
286 cmd->base.speed < ecmd.base.speed)
287 cmd->base.speed = ecmd.base.speed;
293 static netdev_features_t br_fix_features(struct net_device *dev,
294 netdev_features_t features)
296 struct net_bridge *br = netdev_priv(dev);
298 return br_features_recompute(br, features);
301 #ifdef CONFIG_NET_POLL_CONTROLLER
302 static void br_poll_controller(struct net_device *br_dev)
306 static void br_netpoll_cleanup(struct net_device *dev)
308 struct net_bridge *br = netdev_priv(dev);
309 struct net_bridge_port *p;
311 list_for_each_entry(p, &br->port_list, list)
312 br_netpoll_disable(p);
315 static int __br_netpoll_enable(struct net_bridge_port *p)
320 np = kzalloc(sizeof(*p->np), GFP_KERNEL);
324 err = __netpoll_setup(np, p->dev);
334 int br_netpoll_enable(struct net_bridge_port *p)
336 if (!p->br->dev->npinfo)
339 return __br_netpoll_enable(p);
342 static int br_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
344 struct net_bridge *br = netdev_priv(dev);
345 struct net_bridge_port *p;
348 list_for_each_entry(p, &br->port_list, list) {
351 err = __br_netpoll_enable(p);
360 br_netpoll_cleanup(dev);
364 void br_netpoll_disable(struct net_bridge_port *p)
366 struct netpoll *np = p->np;
378 static int br_add_slave(struct net_device *dev, struct net_device *slave_dev,
379 struct netlink_ext_ack *extack)
382 struct net_bridge *br = netdev_priv(dev);
384 return br_add_if(br, slave_dev, extack);
387 static int br_del_slave(struct net_device *dev, struct net_device *slave_dev)
389 struct net_bridge *br = netdev_priv(dev);
391 return br_del_if(br, slave_dev);
394 static const struct ethtool_ops br_ethtool_ops = {
395 .get_drvinfo = br_getinfo,
396 .get_link = ethtool_op_get_link,
397 .get_link_ksettings = br_get_link_ksettings,
400 static const struct net_device_ops br_netdev_ops = {
401 .ndo_open = br_dev_open,
402 .ndo_stop = br_dev_stop,
403 .ndo_init = br_dev_init,
404 .ndo_uninit = br_dev_uninit,
405 .ndo_start_xmit = br_dev_xmit,
406 .ndo_get_stats64 = br_get_stats64,
407 .ndo_set_mac_address = br_set_mac_address,
408 .ndo_set_rx_mode = br_dev_set_multicast_list,
409 .ndo_change_rx_flags = br_dev_change_rx_flags,
410 .ndo_change_mtu = br_change_mtu,
411 .ndo_do_ioctl = br_dev_ioctl,
412 #ifdef CONFIG_NET_POLL_CONTROLLER
413 .ndo_netpoll_setup = br_netpoll_setup,
414 .ndo_netpoll_cleanup = br_netpoll_cleanup,
415 .ndo_poll_controller = br_poll_controller,
417 .ndo_add_slave = br_add_slave,
418 .ndo_del_slave = br_del_slave,
419 .ndo_fix_features = br_fix_features,
420 .ndo_fdb_add = br_fdb_add,
421 .ndo_fdb_del = br_fdb_delete,
422 .ndo_fdb_dump = br_fdb_dump,
423 .ndo_fdb_get = br_fdb_get,
424 .ndo_bridge_getlink = br_getlink,
425 .ndo_bridge_setlink = br_setlink,
426 .ndo_bridge_dellink = br_dellink,
427 .ndo_features_check = passthru_features_check,
430 static struct device_type br_type = {
434 void br_dev_setup(struct net_device *dev)
436 struct net_bridge *br = netdev_priv(dev);
438 eth_hw_addr_random(dev);
441 dev->netdev_ops = &br_netdev_ops;
442 dev->needs_free_netdev = true;
443 dev->ethtool_ops = &br_ethtool_ops;
444 SET_NETDEV_DEVTYPE(dev, &br_type);
445 dev->priv_flags = IFF_EBRIDGE | IFF_NO_QUEUE;
447 dev->features = COMMON_FEATURES | NETIF_F_LLTX | NETIF_F_NETNS_LOCAL |
448 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
449 dev->hw_features = COMMON_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
450 NETIF_F_HW_VLAN_STAG_TX;
451 dev->vlan_features = COMMON_FEATURES;
454 spin_lock_init(&br->lock);
455 INIT_LIST_HEAD(&br->port_list);
456 INIT_HLIST_HEAD(&br->fdb_list);
457 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
458 INIT_LIST_HEAD(&br->mrp_list);
460 spin_lock_init(&br->hash_lock);
462 br->bridge_id.prio[0] = 0x80;
463 br->bridge_id.prio[1] = 0x00;
465 ether_addr_copy(br->group_addr, eth_stp_addr);
467 br->stp_enabled = BR_NO_STP;
468 br->group_fwd_mask = BR_GROUPFWD_DEFAULT;
469 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
471 br->designated_root = br->bridge_id;
472 br->bridge_max_age = br->max_age = 20 * HZ;
473 br->bridge_hello_time = br->hello_time = 2 * HZ;
474 br->bridge_forward_delay = br->forward_delay = 15 * HZ;
475 br->bridge_ageing_time = br->ageing_time = BR_DEFAULT_AGEING_TIME;
476 dev->max_mtu = ETH_MAX_MTU;
478 br_netfilter_rtable_init(br);
479 br_stp_timer_init(br);
480 br_multicast_init(br);
481 INIT_DELAYED_WORK(&br->gc_work, br_fdb_cleanup);