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 | * |
| 8 | * $Id: br_device.c,v 1.6 2001/12/24 00:59:55 davem Exp $ |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * as published by the Free Software Foundation; either version |
| 13 | * 2 of the License, or (at your option) any later version. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/netdevice.h> |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 18 | #include <linux/etherdevice.h> |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 19 | #include <linux/ethtool.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 | |
| 24 | static struct net_device_stats *br_dev_get_stats(struct net_device *dev) |
| 25 | { |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 26 | struct net_bridge *br = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | return &br->statistics; |
| 28 | } |
| 29 | |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 30 | /* net device transmit always called with no BH (preempt_disabled) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | int br_dev_xmit(struct sk_buff *skb, struct net_device *dev) |
| 32 | { |
| 33 | struct net_bridge *br = netdev_priv(dev); |
| 34 | const unsigned char *dest = skb->data; |
| 35 | struct net_bridge_fdb_entry *dst; |
| 36 | |
| 37 | br->statistics.tx_packets++; |
| 38 | br->statistics.tx_bytes += skb->len; |
| 39 | |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 40 | skb_reset_mac_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | skb_pull(skb, ETH_HLEN); |
| 42 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 43 | if (dest[0] & 1) |
Herbert Xu | e081e1e | 2007-09-16 16:20:48 -0700 | [diff] [blame] | 44 | br_flood_deliver(br, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | else if ((dst = __br_fdb_get(br, dest)) != NULL) |
| 46 | br_deliver(dst->dst, skb); |
| 47 | else |
Herbert Xu | e081e1e | 2007-09-16 16:20:48 -0700 | [diff] [blame] | 48 | br_flood_deliver(br, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static int br_dev_open(struct net_device *dev) |
| 54 | { |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 55 | struct net_bridge *br = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 57 | br_features_recompute(br); |
| 58 | netif_start_queue(dev); |
| 59 | br_stp_enable_bridge(br); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static void br_dev_set_multicast_list(struct net_device *dev) |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | static int br_dev_stop(struct net_device *dev) |
| 69 | { |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 70 | br_stp_disable_bridge(netdev_priv(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | netif_stop_queue(dev); |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int br_change_mtu(struct net_device *dev, int new_mtu) |
| 78 | { |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 79 | if (new_mtu < 68 || new_mtu > br_min_mtu(netdev_priv(dev))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | return -EINVAL; |
| 81 | |
| 82 | dev->mtu = new_mtu; |
| 83 | return 0; |
| 84 | } |
| 85 | |
Stephen Hemminger | ffe1d49 | 2007-04-09 11:49:58 -0700 | [diff] [blame] | 86 | /* Allow setting mac address to any valid ethernet address. */ |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 87 | static int br_set_mac_address(struct net_device *dev, void *p) |
| 88 | { |
| 89 | struct net_bridge *br = netdev_priv(dev); |
| 90 | struct sockaddr *addr = p; |
Stephen Hemminger | ffe1d49 | 2007-04-09 11:49:58 -0700 | [diff] [blame] | 91 | |
| 92 | if (!is_valid_ether_addr(addr->sa_data)) |
| 93 | return -EINVAL; |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 94 | |
| 95 | spin_lock_bh(&br->lock); |
Stephen Hemminger | ffe1d49 | 2007-04-09 11:49:58 -0700 | [diff] [blame] | 96 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); |
| 97 | br_stp_change_bridge_id(br, addr->sa_data); |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 98 | spin_unlock_bh(&br->lock); |
| 99 | |
Stephen Hemminger | ffe1d49 | 2007-04-09 11:49:58 -0700 | [diff] [blame] | 100 | return 0; |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 103 | static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info) |
| 104 | { |
| 105 | strcpy(info->driver, "bridge"); |
| 106 | strcpy(info->version, BR_VERSION); |
| 107 | strcpy(info->fw_version, "N/A"); |
| 108 | strcpy(info->bus_info, "N/A"); |
| 109 | } |
| 110 | |
| 111 | static int br_set_sg(struct net_device *dev, u32 data) |
| 112 | { |
| 113 | struct net_bridge *br = netdev_priv(dev); |
| 114 | |
| 115 | if (data) |
| 116 | br->feature_mask |= NETIF_F_SG; |
| 117 | else |
| 118 | br->feature_mask &= ~NETIF_F_SG; |
| 119 | |
| 120 | br_features_recompute(br); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int br_set_tso(struct net_device *dev, u32 data) |
| 125 | { |
| 126 | struct net_bridge *br = netdev_priv(dev); |
| 127 | |
| 128 | if (data) |
| 129 | br->feature_mask |= NETIF_F_TSO; |
| 130 | else |
| 131 | br->feature_mask &= ~NETIF_F_TSO; |
| 132 | |
| 133 | br_features_recompute(br); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int br_set_tx_csum(struct net_device *dev, u32 data) |
| 138 | { |
| 139 | struct net_bridge *br = netdev_priv(dev); |
| 140 | |
| 141 | if (data) |
Herbert Xu | 2c6cc0d | 2006-06-17 22:06:45 -0700 | [diff] [blame] | 142 | br->feature_mask |= NETIF_F_NO_CSUM; |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 143 | else |
Herbert Xu | 2c6cc0d | 2006-06-17 22:06:45 -0700 | [diff] [blame] | 144 | br->feature_mask &= ~NETIF_F_ALL_CSUM; |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 145 | |
| 146 | br_features_recompute(br); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static struct ethtool_ops br_ethtool_ops = { |
| 151 | .get_drvinfo = br_getinfo, |
| 152 | .get_link = ethtool_op_get_link, |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 153 | .set_sg = br_set_sg, |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 154 | .set_tx_csum = br_set_tx_csum, |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 155 | .set_tso = br_set_tso, |
| 156 | }; |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | void br_dev_setup(struct net_device *dev) |
| 159 | { |
Stephen Hemminger | 3ae4125 | 2007-12-16 13:35:51 -0800 | [diff] [blame] | 160 | random_ether_addr(dev->dev_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | ether_setup(dev); |
| 162 | |
| 163 | dev->do_ioctl = br_dev_ioctl; |
| 164 | dev->get_stats = br_dev_get_stats; |
| 165 | dev->hard_start_xmit = br_dev_xmit; |
| 166 | dev->open = br_dev_open; |
| 167 | dev->set_multicast_list = br_dev_set_multicast_list; |
| 168 | dev->change_mtu = br_change_mtu; |
| 169 | dev->destructor = free_netdev; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 170 | SET_ETHTOOL_OPS(dev, &br_ethtool_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | dev->stop = br_dev_stop; |
| 172 | dev->tx_queue_len = 0; |
Stephen Hemminger | 4505a3e | 2005-12-21 18:51:49 -0800 | [diff] [blame] | 173 | dev->set_mac_address = br_set_mac_address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | dev->priv_flags = IFF_EBRIDGE; |
Stephen Hemminger | edb5e46 | 2005-12-21 19:00:58 -0800 | [diff] [blame] | 175 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 176 | dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | |
Herbert Xu | 7f353bf | 2007-08-10 15:47:58 -0700 | [diff] [blame] | 177 | NETIF_F_GSO_MASK | NETIF_F_NO_CSUM | NETIF_F_LLTX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |