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 /* net device transmit always called with BH disabled */ 28 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev) 29 { 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; 36 u16 vid = 0; 37 38 memset(skb->cb, 0, sizeof(struct br_input_skb_cb)); 39 40 rcu_read_lock(); 41 nf_ops = rcu_dereference(nf_br_ops); 42 if (nf_ops && nf_ops->br_dev_xmit_hook(skb)) { 43 rcu_read_unlock(); 44 return NETDEV_TX_OK; 45 } 46 47 dev_sw_netstats_tx_add(dev, 1, skb->len); 48 49 br_switchdev_frame_unmark(skb); 50 BR_INPUT_SKB_CB(skb)->brdev = dev; 51 BR_INPUT_SKB_CB(skb)->frag_max_size = 0; 52 53 skb_reset_mac_header(skb); 54 skb_pull(skb, ETH_HLEN); 55 56 if (!br_allowed_ingress(br, br_vlan_group_rcu(br), skb, &vid, &state)) 57 goto out; 58 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; 71 72 msg = br_is_nd_neigh_msg(skb, &_msg); 73 if (msg) 74 br_do_suppress_nd(skb, br, vid, NULL, msg); 75 } 76 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); 83 goto out; 84 } 85 if (br_multicast_rcv(br, NULL, skb, vid)) { 86 kfree_skb(skb); 87 goto out; 88 } 89 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); 94 else 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); 98 } else { 99 br_flood(br, skb, BR_PKT_UNICAST, false, true); 100 } 101 out: 102 rcu_read_unlock(); 103 return NETDEV_TX_OK; 104 } 105 106 static struct lock_class_key bridge_netdev_addr_lock_key; 107 108 static void br_set_lockdep_class(struct net_device *dev) 109 { 110 lockdep_set_class(&dev->addr_list_lock, &bridge_netdev_addr_lock_key); 111 } 112 113 static int br_dev_init(struct net_device *dev) 114 { 115 struct net_bridge *br = netdev_priv(dev); 116 int err; 117 118 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 119 if (!dev->tstats) 120 return -ENOMEM; 121 122 err = br_fdb_hash_init(br); 123 if (err) { 124 free_percpu(dev->tstats); 125 return err; 126 } 127 128 err = br_mdb_hash_init(br); 129 if (err) { 130 free_percpu(dev->tstats); 131 br_fdb_hash_fini(br); 132 return err; 133 } 134 135 err = br_vlan_init(br); 136 if (err) { 137 free_percpu(dev->tstats); 138 br_mdb_hash_fini(br); 139 br_fdb_hash_fini(br); 140 return err; 141 } 142 143 err = br_multicast_init_stats(br); 144 if (err) { 145 free_percpu(dev->tstats); 146 br_vlan_flush(br); 147 br_mdb_hash_fini(br); 148 br_fdb_hash_fini(br); 149 } 150 151 br_set_lockdep_class(dev); 152 return err; 153 } 154 155 static void br_dev_uninit(struct net_device *dev) 156 { 157 struct net_bridge *br = netdev_priv(dev); 158 159 br_multicast_dev_del(br); 160 br_multicast_uninit_stats(br); 161 br_vlan_flush(br); 162 br_mdb_hash_fini(br); 163 br_fdb_hash_fini(br); 164 free_percpu(dev->tstats); 165 } 166 167 static int br_dev_open(struct net_device *dev) 168 { 169 struct net_bridge *br = netdev_priv(dev); 170 171 netdev_update_features(dev); 172 netif_start_queue(dev); 173 br_stp_enable_bridge(br); 174 br_multicast_open(br); 175 176 if (br_opt_get(br, BROPT_MULTICAST_ENABLED)) 177 br_multicast_join_snoopers(br); 178 179 return 0; 180 } 181 182 static void br_dev_set_multicast_list(struct net_device *dev) 183 { 184 } 185 186 static void br_dev_change_rx_flags(struct net_device *dev, int change) 187 { 188 if (change & IFF_PROMISC) 189 br_manage_promisc(netdev_priv(dev)); 190 } 191 192 static int br_dev_stop(struct net_device *dev) 193 { 194 struct net_bridge *br = netdev_priv(dev); 195 196 br_stp_disable_bridge(br); 197 br_multicast_stop(br); 198 199 if (br_opt_get(br, BROPT_MULTICAST_ENABLED)) 200 br_multicast_leave_snoopers(br); 201 202 netif_stop_queue(dev); 203 204 return 0; 205 } 206 207 static int br_change_mtu(struct net_device *dev, int new_mtu) 208 { 209 struct net_bridge *br = netdev_priv(dev); 210 211 dev->mtu = new_mtu; 212 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); 218 #endif 219 220 return 0; 221 } 222 223 /* Allow setting mac address to any valid ethernet address. */ 224 static int br_set_mac_address(struct net_device *dev, void *p) 225 { 226 struct net_bridge *br = netdev_priv(dev); 227 struct sockaddr *addr = p; 228 229 if (!is_valid_ether_addr(addr->sa_data)) 230 return -EADDRNOTAVAIL; 231 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 234 */ 235 if (dev->reg_state != NETREG_REGISTERED) 236 return -EBUSY; 237 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); 242 } 243 spin_unlock_bh(&br->lock); 244 245 return 0; 246 } 247 248 static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info) 249 { 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)); 254 } 255 256 static int br_get_link_ksettings(struct net_device *dev, 257 struct ethtool_link_ksettings *cmd) 258 { 259 struct net_bridge *br = netdev_priv(dev); 260 struct net_bridge_port *p; 261 262 cmd->base.duplex = DUPLEX_UNKNOWN; 263 cmd->base.port = PORT_OTHER; 264 cmd->base.speed = SPEED_UNKNOWN; 265 266 list_for_each_entry(p, &br->port_list, list) { 267 struct ethtool_link_ksettings ecmd; 268 struct net_device *pdev = p->dev; 269 270 if (!netif_running(pdev) || !netif_oper_up(pdev)) 271 continue; 272 273 if (__ethtool_get_link_ksettings(pdev, &ecmd)) 274 continue; 275 276 if (ecmd.base.speed == (__u32)SPEED_UNKNOWN) 277 continue; 278 279 if (cmd->base.speed == (__u32)SPEED_UNKNOWN || 280 cmd->base.speed < ecmd.base.speed) 281 cmd->base.speed = ecmd.base.speed; 282 } 283 284 return 0; 285 } 286 287 static netdev_features_t br_fix_features(struct net_device *dev, 288 netdev_features_t features) 289 { 290 struct net_bridge *br = netdev_priv(dev); 291 292 return br_features_recompute(br, features); 293 } 294 295 #ifdef CONFIG_NET_POLL_CONTROLLER 296 static void br_poll_controller(struct net_device *br_dev) 297 { 298 } 299 300 static void br_netpoll_cleanup(struct net_device *dev) 301 { 302 struct net_bridge *br = netdev_priv(dev); 303 struct net_bridge_port *p; 304 305 list_for_each_entry(p, &br->port_list, list) 306 br_netpoll_disable(p); 307 } 308 309 static int __br_netpoll_enable(struct net_bridge_port *p) 310 { 311 struct netpoll *np; 312 int err; 313 314 np = kzalloc(sizeof(*p->np), GFP_KERNEL); 315 if (!np) 316 return -ENOMEM; 317 318 err = __netpoll_setup(np, p->dev); 319 if (err) { 320 kfree(np); 321 return err; 322 } 323 324 p->np = np; 325 return err; 326 } 327 328 int br_netpoll_enable(struct net_bridge_port *p) 329 { 330 if (!p->br->dev->npinfo) 331 return 0; 332 333 return __br_netpoll_enable(p); 334 } 335 336 static int br_netpoll_setup(struct net_device *dev, struct netpoll_info *ni) 337 { 338 struct net_bridge *br = netdev_priv(dev); 339 struct net_bridge_port *p; 340 int err = 0; 341 342 list_for_each_entry(p, &br->port_list, list) { 343 if (!p->dev) 344 continue; 345 err = __br_netpoll_enable(p); 346 if (err) 347 goto fail; 348 } 349 350 out: 351 return err; 352 353 fail: 354 br_netpoll_cleanup(dev); 355 goto out; 356 } 357 358 void br_netpoll_disable(struct net_bridge_port *p) 359 { 360 struct netpoll *np = p->np; 361 362 if (!np) 363 return; 364 365 p->np = NULL; 366 367 __netpoll_free(np); 368 } 369 370 #endif 371 372 static int br_add_slave(struct net_device *dev, struct net_device *slave_dev, 373 struct netlink_ext_ack *extack) 374 375 { 376 struct net_bridge *br = netdev_priv(dev); 377 378 return br_add_if(br, slave_dev, extack); 379 } 380 381 static int br_del_slave(struct net_device *dev, struct net_device *slave_dev) 382 { 383 struct net_bridge *br = netdev_priv(dev); 384 385 return br_del_if(br, slave_dev); 386 } 387 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, 392 }; 393 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, 410 #endif 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, 422 }; 423 424 static struct device_type br_type = { 425 .name = "bridge", 426 }; 427 428 void br_dev_setup(struct net_device *dev) 429 { 430 struct net_bridge *br = netdev_priv(dev); 431 432 eth_hw_addr_random(dev); 433 ether_setup(dev); 434 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; 440 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; 446 447 br->dev = dev; 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); 454 #endif 455 #if IS_ENABLED(CONFIG_BRIDGE_CFM) 456 INIT_HLIST_HEAD(&br->mep_list); 457 #endif 458 spin_lock_init(&br->hash_lock); 459 460 br->bridge_id.prio[0] = 0x80; 461 br->bridge_id.prio[1] = 0x00; 462 463 ether_addr_copy(br->group_addr, eth_stp_addr); 464 465 br->stp_enabled = BR_NO_STP; 466 br->group_fwd_mask = BR_GROUPFWD_DEFAULT; 467 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT; 468 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; 475 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); 480 } 481