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