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