1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Device handling code 4 * Linux ethernet bridge 5 * 6 * Authors: 7 * Lennert Buytenhek <buytenh@gnu.org> 8 */ 9 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> 17 18 #include <linux/uaccess.h> 19 #include "br_private.h" 20 21 #define COMMON_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | \ 22 NETIF_F_GSO_MASK | NETIF_F_HW_CSUM) 23 24 const struct nf_br_ops __rcu *nf_br_ops __read_mostly; 25 EXPORT_SYMBOL_GPL(nf_br_ops); 26 27 static struct lock_class_key bridge_netdev_addr_lock_key; 28 29 /* net device transmit always called with BH disabled */ 30 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev) 31 { 32 struct net_bridge *br = netdev_priv(dev); 33 struct net_bridge_fdb_entry *dst; 34 struct net_bridge_mdb_entry *mdst; 35 struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats); 36 const struct nf_br_ops *nf_ops; 37 const unsigned char *dest; 38 struct ethhdr *eth; 39 u16 vid = 0; 40 41 rcu_read_lock(); 42 nf_ops = rcu_dereference(nf_br_ops); 43 if (nf_ops && nf_ops->br_dev_xmit_hook(skb)) { 44 rcu_read_unlock(); 45 return NETDEV_TX_OK; 46 } 47 48 u64_stats_update_begin(&brstats->syncp); 49 brstats->tx_packets++; 50 brstats->tx_bytes += skb->len; 51 u64_stats_update_end(&brstats->syncp); 52 53 br_switchdev_frame_unmark(skb); 54 BR_INPUT_SKB_CB(skb)->brdev = dev; 55 56 skb_reset_mac_header(skb); 57 eth = eth_hdr(skb); 58 skb_pull(skb, ETH_HLEN); 59 60 if (!br_allowed_ingress(br, br_vlan_group_rcu(br), skb, &vid)) 61 goto out; 62 63 if (IS_ENABLED(CONFIG_INET) && 64 (eth->h_proto == htons(ETH_P_ARP) || 65 eth->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; 75 76 msg = br_is_nd_neigh_msg(skb, &_msg); 77 if (msg) 78 br_do_suppress_nd(skb, br, vid, NULL, msg); 79 } 80 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); 87 goto out; 88 } 89 if (br_multicast_rcv(br, NULL, skb, vid)) { 90 kfree_skb(skb); 91 goto out; 92 } 93 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); 98 else 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); 102 } else { 103 br_flood(br, skb, BR_PKT_UNICAST, false, true); 104 } 105 out: 106 rcu_read_unlock(); 107 return NETDEV_TX_OK; 108 } 109 110 static void br_set_lockdep_class(struct net_device *dev) 111 { 112 lockdep_set_class(&dev->addr_list_lock, &bridge_netdev_addr_lock_key); 113 } 114 115 static int br_dev_init(struct net_device *dev) 116 { 117 struct net_bridge *br = netdev_priv(dev); 118 int err; 119 120 br->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 121 if (!br->stats) 122 return -ENOMEM; 123 124 err = br_fdb_hash_init(br); 125 if (err) { 126 free_percpu(br->stats); 127 return err; 128 } 129 130 err = br_mdb_hash_init(br); 131 if (err) { 132 free_percpu(br->stats); 133 br_fdb_hash_fini(br); 134 return err; 135 } 136 137 err = br_vlan_init(br); 138 if (err) { 139 free_percpu(br->stats); 140 br_mdb_hash_fini(br); 141 br_fdb_hash_fini(br); 142 return err; 143 } 144 145 err = br_multicast_init_stats(br); 146 if (err) { 147 free_percpu(br->stats); 148 br_vlan_flush(br); 149 br_mdb_hash_fini(br); 150 br_fdb_hash_fini(br); 151 } 152 br_set_lockdep_class(dev); 153 154 return err; 155 } 156 157 static void br_dev_uninit(struct net_device *dev) 158 { 159 struct net_bridge *br = netdev_priv(dev); 160 161 br_multicast_dev_del(br); 162 br_multicast_uninit_stats(br); 163 br_vlan_flush(br); 164 br_mdb_hash_fini(br); 165 br_fdb_hash_fini(br); 166 free_percpu(br->stats); 167 } 168 169 static int br_dev_open(struct net_device *dev) 170 { 171 struct net_bridge *br = netdev_priv(dev); 172 173 netdev_update_features(dev); 174 netif_start_queue(dev); 175 br_stp_enable_bridge(br); 176 br_multicast_open(br); 177 178 return 0; 179 } 180 181 static void br_dev_set_multicast_list(struct net_device *dev) 182 { 183 } 184 185 static void br_dev_change_rx_flags(struct net_device *dev, int change) 186 { 187 if (change & IFF_PROMISC) 188 br_manage_promisc(netdev_priv(dev)); 189 } 190 191 static int br_dev_stop(struct net_device *dev) 192 { 193 struct net_bridge *br = netdev_priv(dev); 194 195 br_stp_disable_bridge(br); 196 br_multicast_stop(br); 197 198 netif_stop_queue(dev); 199 200 return 0; 201 } 202 203 static void br_get_stats64(struct net_device *dev, 204 struct rtnl_link_stats64 *stats) 205 { 206 struct net_bridge *br = netdev_priv(dev); 207 struct pcpu_sw_netstats tmp, sum = { 0 }; 208 unsigned int cpu; 209 210 for_each_possible_cpu(cpu) { 211 unsigned int start; 212 const struct pcpu_sw_netstats *bstats 213 = per_cpu_ptr(br->stats, cpu); 214 do { 215 start = u64_stats_fetch_begin_irq(&bstats->syncp); 216 memcpy(&tmp, bstats, sizeof(tmp)); 217 } while (u64_stats_fetch_retry_irq(&bstats->syncp, start)); 218 sum.tx_bytes += tmp.tx_bytes; 219 sum.tx_packets += tmp.tx_packets; 220 sum.rx_bytes += tmp.rx_bytes; 221 sum.rx_packets += tmp.rx_packets; 222 } 223 224 stats->tx_bytes = sum.tx_bytes; 225 stats->tx_packets = sum.tx_packets; 226 stats->rx_bytes = sum.rx_bytes; 227 stats->rx_packets = sum.rx_packets; 228 } 229 230 static int br_change_mtu(struct net_device *dev, int new_mtu) 231 { 232 struct net_bridge *br = netdev_priv(dev); 233 234 dev->mtu = new_mtu; 235 236 /* this flag will be cleared if the MTU was automatically adjusted */ 237 br_opt_toggle(br, BROPT_MTU_SET_BY_USER, true); 238 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 239 /* remember the MTU in the rtable for PMTU */ 240 dst_metric_set(&br->fake_rtable.dst, RTAX_MTU, new_mtu); 241 #endif 242 243 return 0; 244 } 245 246 /* Allow setting mac address to any valid ethernet address. */ 247 static int br_set_mac_address(struct net_device *dev, void *p) 248 { 249 struct net_bridge *br = netdev_priv(dev); 250 struct sockaddr *addr = p; 251 252 if (!is_valid_ether_addr(addr->sa_data)) 253 return -EADDRNOTAVAIL; 254 255 spin_lock_bh(&br->lock); 256 if (!ether_addr_equal(dev->dev_addr, addr->sa_data)) { 257 /* Mac address will be changed in br_stp_change_bridge_id(). */ 258 br_stp_change_bridge_id(br, addr->sa_data); 259 } 260 spin_unlock_bh(&br->lock); 261 262 return 0; 263 } 264 265 static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info) 266 { 267 strlcpy(info->driver, "bridge", sizeof(info->driver)); 268 strlcpy(info->version, BR_VERSION, sizeof(info->version)); 269 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version)); 270 strlcpy(info->bus_info, "N/A", sizeof(info->bus_info)); 271 } 272 273 static netdev_features_t br_fix_features(struct net_device *dev, 274 netdev_features_t features) 275 { 276 struct net_bridge *br = netdev_priv(dev); 277 278 return br_features_recompute(br, features); 279 } 280 281 #ifdef CONFIG_NET_POLL_CONTROLLER 282 static void br_poll_controller(struct net_device *br_dev) 283 { 284 } 285 286 static void br_netpoll_cleanup(struct net_device *dev) 287 { 288 struct net_bridge *br = netdev_priv(dev); 289 struct net_bridge_port *p; 290 291 list_for_each_entry(p, &br->port_list, list) 292 br_netpoll_disable(p); 293 } 294 295 static int __br_netpoll_enable(struct net_bridge_port *p) 296 { 297 struct netpoll *np; 298 int err; 299 300 np = kzalloc(sizeof(*p->np), GFP_KERNEL); 301 if (!np) 302 return -ENOMEM; 303 304 err = __netpoll_setup(np, p->dev); 305 if (err) { 306 kfree(np); 307 return err; 308 } 309 310 p->np = np; 311 return err; 312 } 313 314 int br_netpoll_enable(struct net_bridge_port *p) 315 { 316 if (!p->br->dev->npinfo) 317 return 0; 318 319 return __br_netpoll_enable(p); 320 } 321 322 static int br_netpoll_setup(struct net_device *dev, struct netpoll_info *ni) 323 { 324 struct net_bridge *br = netdev_priv(dev); 325 struct net_bridge_port *p; 326 int err = 0; 327 328 list_for_each_entry(p, &br->port_list, list) { 329 if (!p->dev) 330 continue; 331 err = __br_netpoll_enable(p); 332 if (err) 333 goto fail; 334 } 335 336 out: 337 return err; 338 339 fail: 340 br_netpoll_cleanup(dev); 341 goto out; 342 } 343 344 void br_netpoll_disable(struct net_bridge_port *p) 345 { 346 struct netpoll *np = p->np; 347 348 if (!np) 349 return; 350 351 p->np = NULL; 352 353 __netpoll_free(np); 354 } 355 356 #endif 357 358 static int br_add_slave(struct net_device *dev, struct net_device *slave_dev, 359 struct netlink_ext_ack *extack) 360 361 { 362 struct net_bridge *br = netdev_priv(dev); 363 364 return br_add_if(br, slave_dev, extack); 365 } 366 367 static int br_del_slave(struct net_device *dev, struct net_device *slave_dev) 368 { 369 struct net_bridge *br = netdev_priv(dev); 370 371 return br_del_if(br, slave_dev); 372 } 373 374 static const struct ethtool_ops br_ethtool_ops = { 375 .get_drvinfo = br_getinfo, 376 .get_link = ethtool_op_get_link, 377 }; 378 379 static const struct net_device_ops br_netdev_ops = { 380 .ndo_open = br_dev_open, 381 .ndo_stop = br_dev_stop, 382 .ndo_init = br_dev_init, 383 .ndo_uninit = br_dev_uninit, 384 .ndo_start_xmit = br_dev_xmit, 385 .ndo_get_stats64 = br_get_stats64, 386 .ndo_set_mac_address = br_set_mac_address, 387 .ndo_set_rx_mode = br_dev_set_multicast_list, 388 .ndo_change_rx_flags = br_dev_change_rx_flags, 389 .ndo_change_mtu = br_change_mtu, 390 .ndo_do_ioctl = br_dev_ioctl, 391 #ifdef CONFIG_NET_POLL_CONTROLLER 392 .ndo_netpoll_setup = br_netpoll_setup, 393 .ndo_netpoll_cleanup = br_netpoll_cleanup, 394 .ndo_poll_controller = br_poll_controller, 395 #endif 396 .ndo_add_slave = br_add_slave, 397 .ndo_del_slave = br_del_slave, 398 .ndo_fix_features = br_fix_features, 399 .ndo_fdb_add = br_fdb_add, 400 .ndo_fdb_del = br_fdb_delete, 401 .ndo_fdb_dump = br_fdb_dump, 402 .ndo_fdb_get = br_fdb_get, 403 .ndo_bridge_getlink = br_getlink, 404 .ndo_bridge_setlink = br_setlink, 405 .ndo_bridge_dellink = br_dellink, 406 .ndo_features_check = passthru_features_check, 407 }; 408 409 static struct device_type br_type = { 410 .name = "bridge", 411 }; 412 413 void br_dev_setup(struct net_device *dev) 414 { 415 struct net_bridge *br = netdev_priv(dev); 416 417 eth_hw_addr_random(dev); 418 ether_setup(dev); 419 420 dev->netdev_ops = &br_netdev_ops; 421 dev->needs_free_netdev = true; 422 dev->ethtool_ops = &br_ethtool_ops; 423 SET_NETDEV_DEVTYPE(dev, &br_type); 424 dev->priv_flags = IFF_EBRIDGE | IFF_NO_QUEUE; 425 426 dev->features = COMMON_FEATURES | NETIF_F_LLTX | NETIF_F_NETNS_LOCAL | 427 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; 428 dev->hw_features = COMMON_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | 429 NETIF_F_HW_VLAN_STAG_TX; 430 dev->vlan_features = COMMON_FEATURES; 431 432 br->dev = dev; 433 spin_lock_init(&br->lock); 434 INIT_LIST_HEAD(&br->port_list); 435 INIT_HLIST_HEAD(&br->fdb_list); 436 spin_lock_init(&br->hash_lock); 437 438 br->bridge_id.prio[0] = 0x80; 439 br->bridge_id.prio[1] = 0x00; 440 441 ether_addr_copy(br->group_addr, eth_stp_addr); 442 443 br->stp_enabled = BR_NO_STP; 444 br->group_fwd_mask = BR_GROUPFWD_DEFAULT; 445 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT; 446 447 br->designated_root = br->bridge_id; 448 br->bridge_max_age = br->max_age = 20 * HZ; 449 br->bridge_hello_time = br->hello_time = 2 * HZ; 450 br->bridge_forward_delay = br->forward_delay = 15 * HZ; 451 br->bridge_ageing_time = br->ageing_time = BR_DEFAULT_AGEING_TIME; 452 dev->max_mtu = ETH_MAX_MTU; 453 454 br_netfilter_rtable_init(br); 455 br_stp_timer_init(br); 456 br_multicast_init(br); 457 INIT_DELAYED_WORK(&br->gc_work, br_fdb_cleanup); 458 } 459