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 <asm/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 /* net device transmit always called with BH disabled */ 29 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev) 30 { 31 struct net_bridge *br = netdev_priv(dev); 32 const unsigned char *dest = skb->data; 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 u16 vid = 0; 37 38 rcu_read_lock(); 39 if (br_nf_prerouting_finish_bridge(skb)) { 40 rcu_read_unlock(); 41 return NETDEV_TX_OK; 42 } 43 44 u64_stats_update_begin(&brstats->syncp); 45 brstats->tx_packets++; 46 brstats->tx_bytes += skb->len; 47 u64_stats_update_end(&brstats->syncp); 48 49 BR_INPUT_SKB_CB(skb)->brdev = dev; 50 51 skb_reset_mac_header(skb); 52 skb_pull(skb, ETH_HLEN); 53 54 if (!br_allowed_ingress(br, br_get_vlan_info(br), skb, &vid)) 55 goto out; 56 57 if (is_broadcast_ether_addr(dest)) 58 br_flood_deliver(br, skb, false); 59 else if (is_multicast_ether_addr(dest)) { 60 if (unlikely(netpoll_tx_running(dev))) { 61 br_flood_deliver(br, skb, false); 62 goto out; 63 } 64 if (br_multicast_rcv(br, NULL, skb, vid)) { 65 kfree_skb(skb); 66 goto out; 67 } 68 69 mdst = br_mdb_get(br, skb, vid); 70 if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) && 71 br_multicast_querier_exists(br, eth_hdr(skb))) 72 br_multicast_deliver(mdst, skb); 73 else 74 br_flood_deliver(br, skb, false); 75 } else if ((dst = __br_fdb_get(br, dest, vid)) != NULL) 76 br_deliver(dst->dst, skb); 77 else 78 br_flood_deliver(br, skb, true); 79 80 out: 81 rcu_read_unlock(); 82 return NETDEV_TX_OK; 83 } 84 85 static int br_dev_init(struct net_device *dev) 86 { 87 struct net_bridge *br = netdev_priv(dev); 88 int err; 89 90 br->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 91 if (!br->stats) 92 return -ENOMEM; 93 94 err = br_vlan_init(br); 95 if (err) 96 free_percpu(br->stats); 97 98 return err; 99 } 100 101 static int br_dev_open(struct net_device *dev) 102 { 103 struct net_bridge *br = netdev_priv(dev); 104 105 netdev_update_features(dev); 106 netif_start_queue(dev); 107 br_stp_enable_bridge(br); 108 br_multicast_open(br); 109 110 return 0; 111 } 112 113 static void br_dev_set_multicast_list(struct net_device *dev) 114 { 115 } 116 117 static void br_dev_change_rx_flags(struct net_device *dev, int change) 118 { 119 if (change & IFF_PROMISC) 120 br_manage_promisc(netdev_priv(dev)); 121 } 122 123 static int br_dev_stop(struct net_device *dev) 124 { 125 struct net_bridge *br = netdev_priv(dev); 126 127 br_stp_disable_bridge(br); 128 br_multicast_stop(br); 129 130 netif_stop_queue(dev); 131 132 return 0; 133 } 134 135 static struct rtnl_link_stats64 *br_get_stats64(struct net_device *dev, 136 struct rtnl_link_stats64 *stats) 137 { 138 struct net_bridge *br = netdev_priv(dev); 139 struct pcpu_sw_netstats tmp, sum = { 0 }; 140 unsigned int cpu; 141 142 for_each_possible_cpu(cpu) { 143 unsigned int start; 144 const struct pcpu_sw_netstats *bstats 145 = per_cpu_ptr(br->stats, cpu); 146 do { 147 start = u64_stats_fetch_begin_irq(&bstats->syncp); 148 memcpy(&tmp, bstats, sizeof(tmp)); 149 } while (u64_stats_fetch_retry_irq(&bstats->syncp, start)); 150 sum.tx_bytes += tmp.tx_bytes; 151 sum.tx_packets += tmp.tx_packets; 152 sum.rx_bytes += tmp.rx_bytes; 153 sum.rx_packets += tmp.rx_packets; 154 } 155 156 stats->tx_bytes = sum.tx_bytes; 157 stats->tx_packets = sum.tx_packets; 158 stats->rx_bytes = sum.rx_bytes; 159 stats->rx_packets = sum.rx_packets; 160 161 return stats; 162 } 163 164 static int br_change_mtu(struct net_device *dev, int new_mtu) 165 { 166 struct net_bridge *br = netdev_priv(dev); 167 if (new_mtu < 68 || new_mtu > br_min_mtu(br)) 168 return -EINVAL; 169 170 dev->mtu = new_mtu; 171 172 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 173 /* remember the MTU in the rtable for PMTU */ 174 dst_metric_set(&br->fake_rtable.dst, RTAX_MTU, new_mtu); 175 #endif 176 177 return 0; 178 } 179 180 /* Allow setting mac address to any valid ethernet address. */ 181 static int br_set_mac_address(struct net_device *dev, void *p) 182 { 183 struct net_bridge *br = netdev_priv(dev); 184 struct sockaddr *addr = p; 185 186 if (!is_valid_ether_addr(addr->sa_data)) 187 return -EADDRNOTAVAIL; 188 189 spin_lock_bh(&br->lock); 190 if (!ether_addr_equal(dev->dev_addr, addr->sa_data)) { 191 /* Mac address will be changed in br_stp_change_bridge_id(). */ 192 br_stp_change_bridge_id(br, addr->sa_data); 193 } 194 spin_unlock_bh(&br->lock); 195 196 return 0; 197 } 198 199 static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info) 200 { 201 strlcpy(info->driver, "bridge", sizeof(info->driver)); 202 strlcpy(info->version, BR_VERSION, sizeof(info->version)); 203 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version)); 204 strlcpy(info->bus_info, "N/A", sizeof(info->bus_info)); 205 } 206 207 static netdev_features_t br_fix_features(struct net_device *dev, 208 netdev_features_t features) 209 { 210 struct net_bridge *br = netdev_priv(dev); 211 212 return br_features_recompute(br, features); 213 } 214 215 #ifdef CONFIG_NET_POLL_CONTROLLER 216 static void br_poll_controller(struct net_device *br_dev) 217 { 218 } 219 220 static void br_netpoll_cleanup(struct net_device *dev) 221 { 222 struct net_bridge *br = netdev_priv(dev); 223 struct net_bridge_port *p; 224 225 list_for_each_entry(p, &br->port_list, list) 226 br_netpoll_disable(p); 227 } 228 229 static int __br_netpoll_enable(struct net_bridge_port *p) 230 { 231 struct netpoll *np; 232 int err; 233 234 np = kzalloc(sizeof(*p->np), GFP_KERNEL); 235 if (!np) 236 return -ENOMEM; 237 238 err = __netpoll_setup(np, p->dev); 239 if (err) { 240 kfree(np); 241 return err; 242 } 243 244 p->np = np; 245 return err; 246 } 247 248 int br_netpoll_enable(struct net_bridge_port *p) 249 { 250 if (!p->br->dev->npinfo) 251 return 0; 252 253 return __br_netpoll_enable(p); 254 } 255 256 static int br_netpoll_setup(struct net_device *dev, struct netpoll_info *ni) 257 { 258 struct net_bridge *br = netdev_priv(dev); 259 struct net_bridge_port *p; 260 int err = 0; 261 262 list_for_each_entry(p, &br->port_list, list) { 263 if (!p->dev) 264 continue; 265 err = __br_netpoll_enable(p); 266 if (err) 267 goto fail; 268 } 269 270 out: 271 return err; 272 273 fail: 274 br_netpoll_cleanup(dev); 275 goto out; 276 } 277 278 void br_netpoll_disable(struct net_bridge_port *p) 279 { 280 struct netpoll *np = p->np; 281 282 if (!np) 283 return; 284 285 p->np = NULL; 286 287 __netpoll_free_async(np); 288 } 289 290 #endif 291 292 static int br_add_slave(struct net_device *dev, struct net_device *slave_dev) 293 294 { 295 struct net_bridge *br = netdev_priv(dev); 296 297 return br_add_if(br, slave_dev); 298 } 299 300 static int br_del_slave(struct net_device *dev, struct net_device *slave_dev) 301 { 302 struct net_bridge *br = netdev_priv(dev); 303 304 return br_del_if(br, slave_dev); 305 } 306 307 static const struct ethtool_ops br_ethtool_ops = { 308 .get_drvinfo = br_getinfo, 309 .get_link = ethtool_op_get_link, 310 }; 311 312 static const struct net_device_ops br_netdev_ops = { 313 .ndo_open = br_dev_open, 314 .ndo_stop = br_dev_stop, 315 .ndo_init = br_dev_init, 316 .ndo_start_xmit = br_dev_xmit, 317 .ndo_get_stats64 = br_get_stats64, 318 .ndo_set_mac_address = br_set_mac_address, 319 .ndo_set_rx_mode = br_dev_set_multicast_list, 320 .ndo_change_rx_flags = br_dev_change_rx_flags, 321 .ndo_change_mtu = br_change_mtu, 322 .ndo_do_ioctl = br_dev_ioctl, 323 #ifdef CONFIG_NET_POLL_CONTROLLER 324 .ndo_netpoll_setup = br_netpoll_setup, 325 .ndo_netpoll_cleanup = br_netpoll_cleanup, 326 .ndo_poll_controller = br_poll_controller, 327 #endif 328 .ndo_add_slave = br_add_slave, 329 .ndo_del_slave = br_del_slave, 330 .ndo_fix_features = br_fix_features, 331 .ndo_fdb_add = br_fdb_add, 332 .ndo_fdb_del = br_fdb_delete, 333 .ndo_fdb_dump = br_fdb_dump, 334 .ndo_bridge_getlink = br_getlink, 335 .ndo_bridge_setlink = br_setlink, 336 .ndo_bridge_dellink = br_dellink, 337 }; 338 339 static void br_dev_free(struct net_device *dev) 340 { 341 struct net_bridge *br = netdev_priv(dev); 342 343 free_percpu(br->stats); 344 free_netdev(dev); 345 } 346 347 static struct device_type br_type = { 348 .name = "bridge", 349 }; 350 351 void br_dev_setup(struct net_device *dev) 352 { 353 struct net_bridge *br = netdev_priv(dev); 354 355 eth_hw_addr_random(dev); 356 ether_setup(dev); 357 358 dev->netdev_ops = &br_netdev_ops; 359 dev->destructor = br_dev_free; 360 dev->ethtool_ops = &br_ethtool_ops; 361 SET_NETDEV_DEVTYPE(dev, &br_type); 362 dev->tx_queue_len = 0; 363 dev->priv_flags = IFF_EBRIDGE; 364 365 dev->features = COMMON_FEATURES | NETIF_F_LLTX | NETIF_F_NETNS_LOCAL | 366 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; 367 dev->hw_features = COMMON_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | 368 NETIF_F_HW_VLAN_STAG_TX; 369 dev->vlan_features = COMMON_FEATURES; 370 371 br->dev = dev; 372 spin_lock_init(&br->lock); 373 INIT_LIST_HEAD(&br->port_list); 374 spin_lock_init(&br->hash_lock); 375 376 br->bridge_id.prio[0] = 0x80; 377 br->bridge_id.prio[1] = 0x00; 378 379 ether_addr_copy(br->group_addr, eth_reserved_addr_base); 380 381 br->stp_enabled = BR_NO_STP; 382 br->group_fwd_mask = BR_GROUPFWD_DEFAULT; 383 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT; 384 385 br->designated_root = br->bridge_id; 386 br->bridge_max_age = br->max_age = 20 * HZ; 387 br->bridge_hello_time = br->hello_time = 2 * HZ; 388 br->bridge_forward_delay = br->forward_delay = 15 * HZ; 389 br->ageing_time = 300 * HZ; 390 391 br_netfilter_rtable_init(br); 392 br_stp_timer_init(br); 393 br_multicast_init(br); 394 } 395