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 const struct nf_br_ops *nf_ops;
34 u8 state = BR_STATE_FORWARDING;
35 const unsigned char *dest;
38 memset(skb->cb, 0, sizeof(struct br_input_skb_cb));
41 nf_ops = rcu_dereference(nf_br_ops);
42 if (nf_ops && nf_ops->br_dev_xmit_hook(skb)) {
47 dev_sw_netstats_tx_add(dev, 1, skb->len);
49 br_switchdev_frame_unmark(skb);
50 BR_INPUT_SKB_CB(skb)->brdev = dev;
51 BR_INPUT_SKB_CB(skb)->frag_max_size = 0;
53 skb_reset_mac_header(skb);
54 skb_pull(skb, ETH_HLEN);
56 if (!br_allowed_ingress(br, br_vlan_group_rcu(br), skb, &vid, &state))
59 if (IS_ENABLED(CONFIG_INET) &&
60 (eth_hdr(skb)->h_proto == htons(ETH_P_ARP) ||
61 eth_hdr(skb)->h_proto == htons(ETH_P_RARP)) &&
62 br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED)) {
63 br_do_proxy_suppress_arp(skb, br, vid, NULL);
64 } else if (IS_ENABLED(CONFIG_IPV6) &&
65 skb->protocol == htons(ETH_P_IPV6) &&
66 br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) &&
67 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
68 sizeof(struct nd_msg)) &&
69 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
70 struct nd_msg *msg, _msg;
72 msg = br_is_nd_neigh_msg(skb, &_msg);
74 br_do_suppress_nd(skb, br, vid, NULL, msg);
77 dest = eth_hdr(skb)->h_dest;
78 if (is_broadcast_ether_addr(dest)) {
79 br_flood(br, skb, BR_PKT_BROADCAST, false, true);
80 } else if (is_multicast_ether_addr(dest)) {
81 if (unlikely(netpoll_tx_running(dev))) {
82 br_flood(br, skb, BR_PKT_MULTICAST, false, true);
85 if (br_multicast_rcv(br, NULL, skb, vid)) {
90 mdst = br_mdb_get(br, skb, vid);
91 if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
92 br_multicast_querier_exists(br, eth_hdr(skb), mdst))
93 br_multicast_flood(mdst, skb, false, true);
95 br_flood(br, skb, BR_PKT_MULTICAST, false, true);
96 } else if ((dst = br_fdb_find_rcu(br, dest, vid)) != NULL) {
97 br_forward(dst->dst, skb, false, true);
99 br_flood(br, skb, BR_PKT_UNICAST, false, true);
106 static struct lock_class_key bridge_netdev_addr_lock_key;
108 static void br_set_lockdep_class(struct net_device *dev)
110 lockdep_set_class(&dev->addr_list_lock, &bridge_netdev_addr_lock_key);
113 static int br_dev_init(struct net_device *dev)
115 struct net_bridge *br = netdev_priv(dev);
118 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
122 err = br_fdb_hash_init(br);
124 free_percpu(dev->tstats);
128 err = br_mdb_hash_init(br);
130 free_percpu(dev->tstats);
131 br_fdb_hash_fini(br);
135 err = br_vlan_init(br);
137 free_percpu(dev->tstats);
138 br_mdb_hash_fini(br);
139 br_fdb_hash_fini(br);
143 err = br_multicast_init_stats(br);
145 free_percpu(dev->tstats);
147 br_mdb_hash_fini(br);
148 br_fdb_hash_fini(br);
151 br_set_lockdep_class(dev);
155 static void br_dev_uninit(struct net_device *dev)
157 struct net_bridge *br = netdev_priv(dev);
159 br_multicast_dev_del(br);
160 br_multicast_uninit_stats(br);
162 br_mdb_hash_fini(br);
163 br_fdb_hash_fini(br);
164 free_percpu(dev->tstats);
167 static int br_dev_open(struct net_device *dev)
169 struct net_bridge *br = netdev_priv(dev);
171 netdev_update_features(dev);
172 netif_start_queue(dev);
173 br_stp_enable_bridge(br);
174 br_multicast_open(br);
176 if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
177 br_multicast_join_snoopers(br);
182 static void br_dev_set_multicast_list(struct net_device *dev)
186 static void br_dev_change_rx_flags(struct net_device *dev, int change)
188 if (change & IFF_PROMISC)
189 br_manage_promisc(netdev_priv(dev));
192 static int br_dev_stop(struct net_device *dev)
194 struct net_bridge *br = netdev_priv(dev);
196 br_stp_disable_bridge(br);
197 br_multicast_stop(br);
199 if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
200 br_multicast_leave_snoopers(br);
202 netif_stop_queue(dev);
207 static int br_change_mtu(struct net_device *dev, int new_mtu)
209 struct net_bridge *br = netdev_priv(dev);
213 /* this flag will be cleared if the MTU was automatically adjusted */
214 br_opt_toggle(br, BROPT_MTU_SET_BY_USER, true);
215 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
216 /* remember the MTU in the rtable for PMTU */
217 dst_metric_set(&br->fake_rtable.dst, RTAX_MTU, new_mtu);
223 /* Allow setting mac address to any valid ethernet address. */
224 static int br_set_mac_address(struct net_device *dev, void *p)
226 struct net_bridge *br = netdev_priv(dev);
227 struct sockaddr *addr = p;
229 if (!is_valid_ether_addr(addr->sa_data))
230 return -EADDRNOTAVAIL;
232 /* dev_set_mac_addr() can be called by a master device on bridge's
233 * NETDEV_UNREGISTER, but since it's being destroyed do nothing
235 if (dev->reg_state != NETREG_REGISTERED)
238 spin_lock_bh(&br->lock);
239 if (!ether_addr_equal(dev->dev_addr, addr->sa_data)) {
240 /* Mac address will be changed in br_stp_change_bridge_id(). */
241 br_stp_change_bridge_id(br, addr->sa_data);
243 spin_unlock_bh(&br->lock);
248 static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
250 strlcpy(info->driver, "bridge", sizeof(info->driver));
251 strlcpy(info->version, BR_VERSION, sizeof(info->version));
252 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
253 strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
256 static int br_get_link_ksettings(struct net_device *dev,
257 struct ethtool_link_ksettings *cmd)
259 struct net_bridge *br = netdev_priv(dev);
260 struct net_bridge_port *p;
262 cmd->base.duplex = DUPLEX_UNKNOWN;
263 cmd->base.port = PORT_OTHER;
264 cmd->base.speed = SPEED_UNKNOWN;
266 list_for_each_entry(p, &br->port_list, list) {
267 struct ethtool_link_ksettings ecmd;
268 struct net_device *pdev = p->dev;
270 if (!netif_running(pdev) || !netif_oper_up(pdev))
273 if (__ethtool_get_link_ksettings(pdev, &ecmd))
276 if (ecmd.base.speed == (__u32)SPEED_UNKNOWN)
279 if (cmd->base.speed == (__u32)SPEED_UNKNOWN ||
280 cmd->base.speed < ecmd.base.speed)
281 cmd->base.speed = ecmd.base.speed;
287 static netdev_features_t br_fix_features(struct net_device *dev,
288 netdev_features_t features)
290 struct net_bridge *br = netdev_priv(dev);
292 return br_features_recompute(br, features);
295 #ifdef CONFIG_NET_POLL_CONTROLLER
296 static void br_poll_controller(struct net_device *br_dev)
300 static void br_netpoll_cleanup(struct net_device *dev)
302 struct net_bridge *br = netdev_priv(dev);
303 struct net_bridge_port *p;
305 list_for_each_entry(p, &br->port_list, list)
306 br_netpoll_disable(p);
309 static int __br_netpoll_enable(struct net_bridge_port *p)
314 np = kzalloc(sizeof(*p->np), GFP_KERNEL);
318 err = __netpoll_setup(np, p->dev);
328 int br_netpoll_enable(struct net_bridge_port *p)
330 if (!p->br->dev->npinfo)
333 return __br_netpoll_enable(p);
336 static int br_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
338 struct net_bridge *br = netdev_priv(dev);
339 struct net_bridge_port *p;
342 list_for_each_entry(p, &br->port_list, list) {
345 err = __br_netpoll_enable(p);
354 br_netpoll_cleanup(dev);
358 void br_netpoll_disable(struct net_bridge_port *p)
360 struct netpoll *np = p->np;
372 static int br_add_slave(struct net_device *dev, struct net_device *slave_dev,
373 struct netlink_ext_ack *extack)
376 struct net_bridge *br = netdev_priv(dev);
378 return br_add_if(br, slave_dev, extack);
381 static int br_del_slave(struct net_device *dev, struct net_device *slave_dev)
383 struct net_bridge *br = netdev_priv(dev);
385 return br_del_if(br, slave_dev);
388 static const struct ethtool_ops br_ethtool_ops = {
389 .get_drvinfo = br_getinfo,
390 .get_link = ethtool_op_get_link,
391 .get_link_ksettings = br_get_link_ksettings,
394 static const struct net_device_ops br_netdev_ops = {
395 .ndo_open = br_dev_open,
396 .ndo_stop = br_dev_stop,
397 .ndo_init = br_dev_init,
398 .ndo_uninit = br_dev_uninit,
399 .ndo_start_xmit = br_dev_xmit,
400 .ndo_get_stats64 = dev_get_tstats64,
401 .ndo_set_mac_address = br_set_mac_address,
402 .ndo_set_rx_mode = br_dev_set_multicast_list,
403 .ndo_change_rx_flags = br_dev_change_rx_flags,
404 .ndo_change_mtu = br_change_mtu,
405 .ndo_do_ioctl = br_dev_ioctl,
406 #ifdef CONFIG_NET_POLL_CONTROLLER
407 .ndo_netpoll_setup = br_netpoll_setup,
408 .ndo_netpoll_cleanup = br_netpoll_cleanup,
409 .ndo_poll_controller = br_poll_controller,
411 .ndo_add_slave = br_add_slave,
412 .ndo_del_slave = br_del_slave,
413 .ndo_fix_features = br_fix_features,
414 .ndo_fdb_add = br_fdb_add,
415 .ndo_fdb_del = br_fdb_delete,
416 .ndo_fdb_dump = br_fdb_dump,
417 .ndo_fdb_get = br_fdb_get,
418 .ndo_bridge_getlink = br_getlink,
419 .ndo_bridge_setlink = br_setlink,
420 .ndo_bridge_dellink = br_dellink,
421 .ndo_features_check = passthru_features_check,
424 static struct device_type br_type = {
428 void br_dev_setup(struct net_device *dev)
430 struct net_bridge *br = netdev_priv(dev);
432 eth_hw_addr_random(dev);
435 dev->netdev_ops = &br_netdev_ops;
436 dev->needs_free_netdev = true;
437 dev->ethtool_ops = &br_ethtool_ops;
438 SET_NETDEV_DEVTYPE(dev, &br_type);
439 dev->priv_flags = IFF_EBRIDGE | IFF_NO_QUEUE;
441 dev->features = COMMON_FEATURES | NETIF_F_LLTX | NETIF_F_NETNS_LOCAL |
442 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
443 dev->hw_features = COMMON_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
444 NETIF_F_HW_VLAN_STAG_TX;
445 dev->vlan_features = COMMON_FEATURES;
448 spin_lock_init(&br->lock);
449 INIT_LIST_HEAD(&br->port_list);
450 INIT_HLIST_HEAD(&br->fdb_list);
451 INIT_HLIST_HEAD(&br->frame_type_list);
452 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
453 INIT_HLIST_HEAD(&br->mrp_list);
455 #if IS_ENABLED(CONFIG_BRIDGE_CFM)
456 INIT_HLIST_HEAD(&br->mep_list);
458 spin_lock_init(&br->hash_lock);
460 br->bridge_id.prio[0] = 0x80;
461 br->bridge_id.prio[1] = 0x00;
463 ether_addr_copy(br->group_addr, eth_stp_addr);
465 br->stp_enabled = BR_NO_STP;
466 br->group_fwd_mask = BR_GROUPFWD_DEFAULT;
467 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
469 br->designated_root = br->bridge_id;
470 br->bridge_max_age = br->max_age = 20 * HZ;
471 br->bridge_hello_time = br->hello_time = 2 * HZ;
472 br->bridge_forward_delay = br->forward_delay = 15 * HZ;
473 br->bridge_ageing_time = br->ageing_time = BR_DEFAULT_AGEING_TIME;
474 dev->max_mtu = ETH_MAX_MTU;
476 br_netfilter_rtable_init(br);
477 br_stp_timer_init(br);
478 br_multicast_init(br);
479 INIT_DELAYED_WORK(&br->gc_work, br_fdb_cleanup);