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> |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 16 | #include <linux/etherdevice.h> |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 17 | #include <linux/ethtool.h> |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/uaccess.h> |
| 20 | #include "br_private.h" |
| 21 | |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 22 | /* net device transmit always called with no BH (preempt_disabled) */ |
Stephen Hemminger | 6fef4c0 | 2009-08-31 19:50:41 +0000 | [diff] [blame] | 23 | 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] | 24 | { |
| 25 | struct net_bridge *br = netdev_priv(dev); |
| 26 | const unsigned char *dest = skb->data; |
| 27 | struct net_bridge_fdb_entry *dst; |
Herbert Xu | c4fcb78 | 2010-02-27 19:41:48 +0000 | [diff] [blame^] | 28 | struct net_bridge_mdb_entry *mdst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Herbert Xu | 6088a53 | 2010-02-27 19:41:42 +0000 | [diff] [blame] | 30 | BR_INPUT_SKB_CB(skb)->brdev = dev; |
| 31 | |
Pavel Emelyanov | a339f1c | 2008-05-21 14:13:47 -0700 | [diff] [blame] | 32 | dev->stats.tx_packets++; |
| 33 | dev->stats.tx_bytes += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 35 | skb_reset_mac_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | skb_pull(skb, ETH_HLEN); |
| 37 | |
Herbert Xu | c4fcb78 | 2010-02-27 19:41:48 +0000 | [diff] [blame^] | 38 | if (dest[0] & 1) { |
| 39 | if (br_multicast_rcv(br, NULL, skb)) |
| 40 | goto out; |
| 41 | |
| 42 | mdst = br_mdb_get(br, skb); |
| 43 | if (mdst || BR_INPUT_SKB_CB(skb)->mrouters_only) |
| 44 | br_multicast_deliver(mdst, skb); |
| 45 | else |
| 46 | br_flood_deliver(br, skb); |
| 47 | } else if ((dst = __br_fdb_get(br, dest)) != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | br_deliver(dst->dst, skb); |
| 49 | else |
Herbert Xu | e081e1e | 2007-09-16 16:20:48 -0700 | [diff] [blame] | 50 | br_flood_deliver(br, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Herbert Xu | c4fcb78 | 2010-02-27 19:41:48 +0000 | [diff] [blame^] | 52 | out: |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 53 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static int br_dev_open(struct net_device *dev) |
| 57 | { |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 58 | struct net_bridge *br = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 60 | br_features_recompute(br); |
| 61 | netif_start_queue(dev); |
| 62 | br_stp_enable_bridge(br); |
Herbert Xu | 3fe2d7c | 2010-02-28 00:49:38 -0800 | [diff] [blame] | 63 | br_multicast_open(br); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static void br_dev_set_multicast_list(struct net_device *dev) |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | static int br_dev_stop(struct net_device *dev) |
| 73 | { |
Herbert Xu | 3fe2d7c | 2010-02-28 00:49:38 -0800 | [diff] [blame] | 74 | struct net_bridge *br = netdev_priv(dev); |
| 75 | |
| 76 | br_stp_disable_bridge(br); |
| 77 | br_multicast_stop(br); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | netif_stop_queue(dev); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int br_change_mtu(struct net_device *dev, int new_mtu) |
| 85 | { |
Simon Wunderlich | 4adf0af | 2008-07-30 16:27:55 -0700 | [diff] [blame] | 86 | struct net_bridge *br = netdev_priv(dev); |
| 87 | if (new_mtu < 68 || new_mtu > br_min_mtu(br)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | return -EINVAL; |
| 89 | |
| 90 | dev->mtu = new_mtu; |
Simon Wunderlich | 4adf0af | 2008-07-30 16:27:55 -0700 | [diff] [blame] | 91 | |
| 92 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 93 | /* remember the MTU in the rtable for PMTU */ |
| 94 | br->fake_rtable.u.dst.metrics[RTAX_MTU - 1] = new_mtu; |
| 95 | #endif |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | |
Stephen Hemminger | ffe1d49 | 2007-04-09 11:49:58 -0700 | [diff] [blame] | 100 | /* Allow setting mac address to any valid ethernet address. */ |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 101 | static int br_set_mac_address(struct net_device *dev, void *p) |
| 102 | { |
| 103 | struct net_bridge *br = netdev_priv(dev); |
| 104 | struct sockaddr *addr = p; |
Stephen Hemminger | ffe1d49 | 2007-04-09 11:49:58 -0700 | [diff] [blame] | 105 | |
| 106 | if (!is_valid_ether_addr(addr->sa_data)) |
| 107 | return -EINVAL; |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 108 | |
| 109 | spin_lock_bh(&br->lock); |
Stephen Hemminger | ffe1d49 | 2007-04-09 11:49:58 -0700 | [diff] [blame] | 110 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); |
| 111 | br_stp_change_bridge_id(br, addr->sa_data); |
Stephen Hemminger | 92c0574 | 2008-06-17 16:10:06 -0700 | [diff] [blame] | 112 | br->flags |= BR_SET_MAC_ADDR; |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 113 | spin_unlock_bh(&br->lock); |
| 114 | |
Stephen Hemminger | ffe1d49 | 2007-04-09 11:49:58 -0700 | [diff] [blame] | 115 | return 0; |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 118 | static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info) |
| 119 | { |
| 120 | strcpy(info->driver, "bridge"); |
| 121 | strcpy(info->version, BR_VERSION); |
| 122 | strcpy(info->fw_version, "N/A"); |
| 123 | strcpy(info->bus_info, "N/A"); |
| 124 | } |
| 125 | |
| 126 | static int br_set_sg(struct net_device *dev, u32 data) |
| 127 | { |
| 128 | struct net_bridge *br = netdev_priv(dev); |
| 129 | |
| 130 | if (data) |
| 131 | br->feature_mask |= NETIF_F_SG; |
| 132 | else |
| 133 | br->feature_mask &= ~NETIF_F_SG; |
| 134 | |
| 135 | br_features_recompute(br); |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int br_set_tso(struct net_device *dev, u32 data) |
| 140 | { |
| 141 | struct net_bridge *br = netdev_priv(dev); |
| 142 | |
| 143 | if (data) |
| 144 | br->feature_mask |= NETIF_F_TSO; |
| 145 | else |
| 146 | br->feature_mask &= ~NETIF_F_TSO; |
| 147 | |
| 148 | br_features_recompute(br); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int br_set_tx_csum(struct net_device *dev, u32 data) |
| 153 | { |
| 154 | struct net_bridge *br = netdev_priv(dev); |
| 155 | |
| 156 | if (data) |
Herbert Xu | 2c6cc0d | 2006-06-17 22:06:45 -0700 | [diff] [blame] | 157 | br->feature_mask |= NETIF_F_NO_CSUM; |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 158 | else |
Herbert Xu | 2c6cc0d | 2006-06-17 22:06:45 -0700 | [diff] [blame] | 159 | br->feature_mask &= ~NETIF_F_ALL_CSUM; |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 160 | |
| 161 | br_features_recompute(br); |
| 162 | return 0; |
| 163 | } |
| 164 | |
Stephen Hemminger | a2dbb88 | 2008-11-19 21:49:00 -0800 | [diff] [blame] | 165 | static const struct ethtool_ops br_ethtool_ops = { |
Stephen Hemminger | e4119a4 | 2008-08-15 19:51:07 -0700 | [diff] [blame] | 166 | .get_drvinfo = br_getinfo, |
| 167 | .get_link = ethtool_op_get_link, |
| 168 | .get_tx_csum = ethtool_op_get_tx_csum, |
| 169 | .set_tx_csum = br_set_tx_csum, |
| 170 | .get_sg = ethtool_op_get_sg, |
| 171 | .set_sg = br_set_sg, |
| 172 | .get_tso = ethtool_op_get_tso, |
| 173 | .set_tso = br_set_tso, |
| 174 | .get_ufo = ethtool_op_get_ufo, |
Sridhar Samudrala | 72dad21 | 2009-10-07 12:41:17 +0000 | [diff] [blame] | 175 | .set_ufo = ethtool_op_set_ufo, |
Stephen Hemminger | e4119a4 | 2008-08-15 19:51:07 -0700 | [diff] [blame] | 176 | .get_flags = ethtool_op_get_flags, |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 177 | }; |
| 178 | |
Stephen Hemminger | a2dbb88 | 2008-11-19 21:49:00 -0800 | [diff] [blame] | 179 | static const struct net_device_ops br_netdev_ops = { |
| 180 | .ndo_open = br_dev_open, |
| 181 | .ndo_stop = br_dev_stop, |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 182 | .ndo_start_xmit = br_dev_xmit, |
| 183 | .ndo_set_mac_address = br_set_mac_address, |
| 184 | .ndo_set_multicast_list = br_dev_set_multicast_list, |
| 185 | .ndo_change_mtu = br_change_mtu, |
| 186 | .ndo_do_ioctl = br_dev_ioctl, |
Stephen Hemminger | a2dbb88 | 2008-11-19 21:49:00 -0800 | [diff] [blame] | 187 | }; |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | void br_dev_setup(struct net_device *dev) |
| 190 | { |
Stephen Hemminger | 3ae4125 | 2007-12-16 13:35:51 -0800 | [diff] [blame] | 191 | random_ether_addr(dev->dev_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | ether_setup(dev); |
| 193 | |
Stephen Hemminger | a2dbb88 | 2008-11-19 21:49:00 -0800 | [diff] [blame] | 194 | dev->netdev_ops = &br_netdev_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | dev->destructor = free_netdev; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 196 | SET_ETHTOOL_OPS(dev, &br_ethtool_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | dev->tx_queue_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | dev->priv_flags = IFF_EBRIDGE; |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 199 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 200 | dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | |
Alexey Dobriyan | 4aa678b | 2008-09-08 16:19:58 -0700 | [diff] [blame] | 201 | NETIF_F_GSO_MASK | NETIF_F_NO_CSUM | NETIF_F_LLTX | |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 202 | NETIF_F_NETNS_LOCAL | NETIF_F_GSO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |