Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Device handling code |
| 3 | * Linux ethernet bridge |
| 4 | * |
| 5 | * Authors: |
| 6 | * Lennert Buytenhek <buytenh@gnu.org> |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
WANG Cong | c06ee96 | 2010-05-06 00:48:24 -0700 | [diff] [blame^] | 16 | #include <linux/netpoll.h> |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 17 | #include <linux/etherdevice.h> |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 18 | #include <linux/ethtool.h> |
WANG Cong | c06ee96 | 2010-05-06 00:48:24 -0700 | [diff] [blame^] | 19 | #include <linux/list.h> |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/uaccess.h> |
| 22 | #include "br_private.h" |
| 23 | |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 24 | /* net device transmit always called with no BH (preempt_disabled) */ |
Stephen Hemminger | 6fef4c0 | 2009-08-31 19:50:41 +0000 | [diff] [blame] | 25 | netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
| 27 | struct net_bridge *br = netdev_priv(dev); |
| 28 | const unsigned char *dest = skb->data; |
| 29 | struct net_bridge_fdb_entry *dst; |
Herbert Xu | c4fcb78 | 2010-02-27 19:41:48 +0000 | [diff] [blame] | 30 | struct net_bridge_mdb_entry *mdst; |
stephen hemminger | 14bb478 | 2010-03-02 13:32:09 +0000 | [diff] [blame] | 31 | struct br_cpu_netstats *brstats = this_cpu_ptr(br->stats); |
| 32 | |
| 33 | brstats->tx_packets++; |
| 34 | brstats->tx_bytes += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Herbert Xu | 6088a53 | 2010-02-27 19:41:42 +0000 | [diff] [blame] | 36 | BR_INPUT_SKB_CB(skb)->brdev = dev; |
| 37 | |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 38 | skb_reset_mac_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | skb_pull(skb, ETH_HLEN); |
| 40 | |
stephen hemminger | 7180f77 | 2010-04-27 07:13:06 +0000 | [diff] [blame] | 41 | if (is_multicast_ether_addr(dest)) { |
Herbert Xu | c4fcb78 | 2010-02-27 19:41:48 +0000 | [diff] [blame] | 42 | if (br_multicast_rcv(br, NULL, skb)) |
| 43 | goto out; |
| 44 | |
| 45 | mdst = br_mdb_get(br, skb); |
YOSHIFUJI Hideaki / 吉藤英明 | 32dec5d | 2010-03-15 21:51:18 +0000 | [diff] [blame] | 46 | if (mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) |
Herbert Xu | c4fcb78 | 2010-02-27 19:41:48 +0000 | [diff] [blame] | 47 | br_multicast_deliver(mdst, skb); |
| 48 | else |
| 49 | br_flood_deliver(br, skb); |
| 50 | } else if ((dst = __br_fdb_get(br, dest)) != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | br_deliver(dst->dst, skb); |
| 52 | else |
Herbert Xu | e081e1e | 2007-09-16 16:20:48 -0700 | [diff] [blame] | 53 | br_flood_deliver(br, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Herbert Xu | c4fcb78 | 2010-02-27 19:41:48 +0000 | [diff] [blame] | 55 | out: |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 56 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static int br_dev_open(struct net_device *dev) |
| 60 | { |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 61 | struct net_bridge *br = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 63 | br_features_recompute(br); |
| 64 | netif_start_queue(dev); |
| 65 | br_stp_enable_bridge(br); |
Herbert Xu | 3fe2d7c | 2010-02-28 00:49:38 -0800 | [diff] [blame] | 66 | br_multicast_open(br); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static void br_dev_set_multicast_list(struct net_device *dev) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | static int br_dev_stop(struct net_device *dev) |
| 76 | { |
Herbert Xu | 3fe2d7c | 2010-02-28 00:49:38 -0800 | [diff] [blame] | 77 | struct net_bridge *br = netdev_priv(dev); |
| 78 | |
| 79 | br_stp_disable_bridge(br); |
| 80 | br_multicast_stop(br); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | netif_stop_queue(dev); |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
stephen hemminger | 14bb478 | 2010-03-02 13:32:09 +0000 | [diff] [blame] | 87 | static struct net_device_stats *br_get_stats(struct net_device *dev) |
| 88 | { |
| 89 | struct net_bridge *br = netdev_priv(dev); |
| 90 | struct net_device_stats *stats = &dev->stats; |
| 91 | struct br_cpu_netstats sum = { 0 }; |
| 92 | unsigned int cpu; |
| 93 | |
| 94 | for_each_possible_cpu(cpu) { |
| 95 | const struct br_cpu_netstats *bstats |
| 96 | = per_cpu_ptr(br->stats, cpu); |
| 97 | |
| 98 | sum.tx_bytes += bstats->tx_bytes; |
| 99 | sum.tx_packets += bstats->tx_packets; |
| 100 | sum.rx_bytes += bstats->rx_bytes; |
| 101 | sum.rx_packets += bstats->rx_packets; |
| 102 | } |
| 103 | |
| 104 | stats->tx_bytes = sum.tx_bytes; |
| 105 | stats->tx_packets = sum.tx_packets; |
| 106 | stats->rx_bytes = sum.rx_bytes; |
| 107 | stats->rx_packets = sum.rx_packets; |
| 108 | |
| 109 | return stats; |
| 110 | } |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | static int br_change_mtu(struct net_device *dev, int new_mtu) |
| 113 | { |
Simon Wunderlich | 4adf0af | 2008-07-30 16:27:55 -0700 | [diff] [blame] | 114 | struct net_bridge *br = netdev_priv(dev); |
| 115 | if (new_mtu < 68 || new_mtu > br_min_mtu(br)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | return -EINVAL; |
| 117 | |
| 118 | dev->mtu = new_mtu; |
Simon Wunderlich | 4adf0af | 2008-07-30 16:27:55 -0700 | [diff] [blame] | 119 | |
| 120 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 121 | /* remember the MTU in the rtable for PMTU */ |
| 122 | br->fake_rtable.u.dst.metrics[RTAX_MTU - 1] = new_mtu; |
| 123 | #endif |
| 124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
Stephen Hemminger | ffe1d49 | 2007-04-09 11:49:58 -0700 | [diff] [blame] | 128 | /* Allow setting mac address to any valid ethernet address. */ |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 129 | static int br_set_mac_address(struct net_device *dev, void *p) |
| 130 | { |
| 131 | struct net_bridge *br = netdev_priv(dev); |
| 132 | struct sockaddr *addr = p; |
Stephen Hemminger | ffe1d49 | 2007-04-09 11:49:58 -0700 | [diff] [blame] | 133 | |
| 134 | if (!is_valid_ether_addr(addr->sa_data)) |
| 135 | return -EINVAL; |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 136 | |
| 137 | spin_lock_bh(&br->lock); |
Stephen Hemminger | ffe1d49 | 2007-04-09 11:49:58 -0700 | [diff] [blame] | 138 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); |
| 139 | br_stp_change_bridge_id(br, addr->sa_data); |
Stephen Hemminger | 92c0574 | 2008-06-17 16:10:06 -0700 | [diff] [blame] | 140 | br->flags |= BR_SET_MAC_ADDR; |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 141 | spin_unlock_bh(&br->lock); |
| 142 | |
Stephen Hemminger | ffe1d49 | 2007-04-09 11:49:58 -0700 | [diff] [blame] | 143 | return 0; |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 146 | static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info) |
| 147 | { |
| 148 | strcpy(info->driver, "bridge"); |
| 149 | strcpy(info->version, BR_VERSION); |
| 150 | strcpy(info->fw_version, "N/A"); |
| 151 | strcpy(info->bus_info, "N/A"); |
| 152 | } |
| 153 | |
| 154 | static int br_set_sg(struct net_device *dev, u32 data) |
| 155 | { |
| 156 | struct net_bridge *br = netdev_priv(dev); |
| 157 | |
| 158 | if (data) |
| 159 | br->feature_mask |= NETIF_F_SG; |
| 160 | else |
| 161 | br->feature_mask &= ~NETIF_F_SG; |
| 162 | |
| 163 | br_features_recompute(br); |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static int br_set_tso(struct net_device *dev, u32 data) |
| 168 | { |
| 169 | struct net_bridge *br = netdev_priv(dev); |
| 170 | |
| 171 | if (data) |
| 172 | br->feature_mask |= NETIF_F_TSO; |
| 173 | else |
| 174 | br->feature_mask &= ~NETIF_F_TSO; |
| 175 | |
| 176 | br_features_recompute(br); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int br_set_tx_csum(struct net_device *dev, u32 data) |
| 181 | { |
| 182 | struct net_bridge *br = netdev_priv(dev); |
| 183 | |
| 184 | if (data) |
Herbert Xu | 2c6cc0d | 2006-06-17 22:06:45 -0700 | [diff] [blame] | 185 | br->feature_mask |= NETIF_F_NO_CSUM; |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 186 | else |
Herbert Xu | 2c6cc0d | 2006-06-17 22:06:45 -0700 | [diff] [blame] | 187 | br->feature_mask &= ~NETIF_F_ALL_CSUM; |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 188 | |
| 189 | br_features_recompute(br); |
| 190 | return 0; |
| 191 | } |
| 192 | |
WANG Cong | c06ee96 | 2010-05-06 00:48:24 -0700 | [diff] [blame^] | 193 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 194 | bool br_devices_support_netpoll(struct net_bridge *br) |
| 195 | { |
| 196 | struct net_bridge_port *p; |
| 197 | bool ret = true; |
| 198 | int count = 0; |
| 199 | unsigned long flags; |
| 200 | |
| 201 | spin_lock_irqsave(&br->lock, flags); |
| 202 | list_for_each_entry(p, &br->port_list, list) { |
| 203 | count++; |
| 204 | if ((p->dev->priv_flags & IFF_DISABLE_NETPOLL) || |
| 205 | !p->dev->netdev_ops->ndo_poll_controller) |
| 206 | ret = false; |
| 207 | } |
| 208 | spin_unlock_irqrestore(&br->lock, flags); |
| 209 | return count != 0 && ret; |
| 210 | } |
| 211 | |
| 212 | static void br_poll_controller(struct net_device *br_dev) |
| 213 | { |
| 214 | struct netpoll *np = br_dev->npinfo->netpoll; |
| 215 | |
| 216 | if (np->real_dev != br_dev) |
| 217 | netpoll_poll_dev(np->real_dev); |
| 218 | } |
| 219 | |
| 220 | void br_netpoll_cleanup(struct net_device *br_dev) |
| 221 | { |
| 222 | struct net_bridge *br = netdev_priv(br_dev); |
| 223 | struct net_bridge_port *p, *n; |
| 224 | const struct net_device_ops *ops; |
| 225 | |
| 226 | br->dev->npinfo = NULL; |
| 227 | list_for_each_entry_safe(p, n, &br->port_list, list) { |
| 228 | if (p->dev) { |
| 229 | ops = p->dev->netdev_ops; |
| 230 | if (ops->ndo_netpoll_cleanup) |
| 231 | ops->ndo_netpoll_cleanup(p->dev); |
| 232 | else |
| 233 | p->dev->npinfo = NULL; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | #else |
| 239 | |
| 240 | void br_netpoll_cleanup(struct net_device *br_dev) |
| 241 | { |
| 242 | } |
| 243 | |
| 244 | #endif |
| 245 | |
Stephen Hemminger | a2dbb88 | 2008-11-19 21:49:00 -0800 | [diff] [blame] | 246 | static const struct ethtool_ops br_ethtool_ops = { |
Stephen Hemminger | e4119a4 | 2008-08-15 19:51:07 -0700 | [diff] [blame] | 247 | .get_drvinfo = br_getinfo, |
| 248 | .get_link = ethtool_op_get_link, |
| 249 | .get_tx_csum = ethtool_op_get_tx_csum, |
| 250 | .set_tx_csum = br_set_tx_csum, |
| 251 | .get_sg = ethtool_op_get_sg, |
| 252 | .set_sg = br_set_sg, |
| 253 | .get_tso = ethtool_op_get_tso, |
| 254 | .set_tso = br_set_tso, |
| 255 | .get_ufo = ethtool_op_get_ufo, |
Sridhar Samudrala | 72dad21 | 2009-10-07 12:41:17 +0000 | [diff] [blame] | 256 | .set_ufo = ethtool_op_set_ufo, |
Stephen Hemminger | e4119a4 | 2008-08-15 19:51:07 -0700 | [diff] [blame] | 257 | .get_flags = ethtool_op_get_flags, |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 258 | }; |
| 259 | |
Stephen Hemminger | a2dbb88 | 2008-11-19 21:49:00 -0800 | [diff] [blame] | 260 | static const struct net_device_ops br_netdev_ops = { |
| 261 | .ndo_open = br_dev_open, |
| 262 | .ndo_stop = br_dev_stop, |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 263 | .ndo_start_xmit = br_dev_xmit, |
stephen hemminger | 14bb478 | 2010-03-02 13:32:09 +0000 | [diff] [blame] | 264 | .ndo_get_stats = br_get_stats, |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 265 | .ndo_set_mac_address = br_set_mac_address, |
| 266 | .ndo_set_multicast_list = br_dev_set_multicast_list, |
| 267 | .ndo_change_mtu = br_change_mtu, |
| 268 | .ndo_do_ioctl = br_dev_ioctl, |
WANG Cong | c06ee96 | 2010-05-06 00:48:24 -0700 | [diff] [blame^] | 269 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 270 | .ndo_netpoll_cleanup = br_netpoll_cleanup, |
| 271 | .ndo_poll_controller = br_poll_controller, |
| 272 | #endif |
Stephen Hemminger | a2dbb88 | 2008-11-19 21:49:00 -0800 | [diff] [blame] | 273 | }; |
| 274 | |
stephen hemminger | 14bb478 | 2010-03-02 13:32:09 +0000 | [diff] [blame] | 275 | static void br_dev_free(struct net_device *dev) |
| 276 | { |
| 277 | struct net_bridge *br = netdev_priv(dev); |
| 278 | |
| 279 | free_percpu(br->stats); |
| 280 | free_netdev(dev); |
| 281 | } |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | void br_dev_setup(struct net_device *dev) |
| 284 | { |
Stephen Hemminger | 3ae4125 | 2007-12-16 13:35:51 -0800 | [diff] [blame] | 285 | random_ether_addr(dev->dev_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | ether_setup(dev); |
| 287 | |
Stephen Hemminger | a2dbb88 | 2008-11-19 21:49:00 -0800 | [diff] [blame] | 288 | dev->netdev_ops = &br_netdev_ops; |
stephen hemminger | 14bb478 | 2010-03-02 13:32:09 +0000 | [diff] [blame] | 289 | dev->destructor = br_dev_free; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 290 | SET_ETHTOOL_OPS(dev, &br_ethtool_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | dev->tx_queue_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | dev->priv_flags = IFF_EBRIDGE; |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 293 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 294 | dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | |
Alexey Dobriyan | 4aa678b | 2008-09-08 16:19:58 -0700 | [diff] [blame] | 295 | NETIF_F_GSO_MASK | NETIF_F_NO_CSUM | NETIF_F_LLTX | |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 296 | NETIF_F_NETNS_LOCAL | NETIF_F_GSO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |