blob: be356293caa17d30a15b3eee0fc4695aa2b3d4d5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Device handling code
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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 Hemminger4505a3e2005-12-21 18:51:49 -080016#include <linux/etherdevice.h>
Stephen Hemmingeredb5e462005-12-21 19:00:58 -080017#include <linux/ethtool.h>
Stephen Hemminger4505a3e2005-12-21 18:51:49 -080018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/uaccess.h>
20#include "br_private.h"
21
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -080022/* net device transmit always called with no BH (preempt_disabled) */
Stephen Hemminger6fef4c02009-08-31 19:50:41 +000023netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
25 struct net_bridge *br = netdev_priv(dev);
26 const unsigned char *dest = skb->data;
27 struct net_bridge_fdb_entry *dst;
28
Herbert Xu6088a532010-02-27 19:41:42 +000029 BR_INPUT_SKB_CB(skb)->brdev = dev;
30
Pavel Emelyanova339f1c2008-05-21 14:13:47 -070031 dev->stats.tx_packets++;
32 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -070034 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 skb_pull(skb, ETH_HLEN);
36
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090037 if (dest[0] & 1)
Herbert Xue081e1e2007-09-16 16:20:48 -070038 br_flood_deliver(br, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 else if ((dst = __br_fdb_get(br, dest)) != NULL)
40 br_deliver(dst->dst, skb);
41 else
Herbert Xue081e1e2007-09-16 16:20:48 -070042 br_flood_deliver(br, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Patrick McHardy6ed10652009-06-23 06:03:08 +000044 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
47static int br_dev_open(struct net_device *dev)
48{
Stephen Hemminger81d35302005-05-29 14:15:17 -070049 struct net_bridge *br = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Stephen Hemminger81d35302005-05-29 14:15:17 -070051 br_features_recompute(br);
52 netif_start_queue(dev);
53 br_stp_enable_bridge(br);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 return 0;
56}
57
58static void br_dev_set_multicast_list(struct net_device *dev)
59{
60}
61
62static int br_dev_stop(struct net_device *dev)
63{
Stephen Hemminger81d35302005-05-29 14:15:17 -070064 br_stp_disable_bridge(netdev_priv(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 netif_stop_queue(dev);
67
68 return 0;
69}
70
71static int br_change_mtu(struct net_device *dev, int new_mtu)
72{
Simon Wunderlich4adf0af2008-07-30 16:27:55 -070073 struct net_bridge *br = netdev_priv(dev);
74 if (new_mtu < 68 || new_mtu > br_min_mtu(br))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return -EINVAL;
76
77 dev->mtu = new_mtu;
Simon Wunderlich4adf0af2008-07-30 16:27:55 -070078
79#ifdef CONFIG_BRIDGE_NETFILTER
80 /* remember the MTU in the rtable for PMTU */
81 br->fake_rtable.u.dst.metrics[RTAX_MTU - 1] = new_mtu;
82#endif
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return 0;
85}
86
Stephen Hemmingerffe1d492007-04-09 11:49:58 -070087/* Allow setting mac address to any valid ethernet address. */
Stephen Hemminger4505a3e2005-12-21 18:51:49 -080088static int br_set_mac_address(struct net_device *dev, void *p)
89{
90 struct net_bridge *br = netdev_priv(dev);
91 struct sockaddr *addr = p;
Stephen Hemmingerffe1d492007-04-09 11:49:58 -070092
93 if (!is_valid_ether_addr(addr->sa_data))
94 return -EINVAL;
Stephen Hemminger4505a3e2005-12-21 18:51:49 -080095
96 spin_lock_bh(&br->lock);
Stephen Hemmingerffe1d492007-04-09 11:49:58 -070097 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
98 br_stp_change_bridge_id(br, addr->sa_data);
Stephen Hemminger92c05742008-06-17 16:10:06 -070099 br->flags |= BR_SET_MAC_ADDR;
Stephen Hemminger4505a3e2005-12-21 18:51:49 -0800100 spin_unlock_bh(&br->lock);
101
Stephen Hemmingerffe1d492007-04-09 11:49:58 -0700102 return 0;
Stephen Hemminger4505a3e2005-12-21 18:51:49 -0800103}
104
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800105static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
106{
107 strcpy(info->driver, "bridge");
108 strcpy(info->version, BR_VERSION);
109 strcpy(info->fw_version, "N/A");
110 strcpy(info->bus_info, "N/A");
111}
112
113static int br_set_sg(struct net_device *dev, u32 data)
114{
115 struct net_bridge *br = netdev_priv(dev);
116
117 if (data)
118 br->feature_mask |= NETIF_F_SG;
119 else
120 br->feature_mask &= ~NETIF_F_SG;
121
122 br_features_recompute(br);
123 return 0;
124}
125
126static int br_set_tso(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_TSO;
132 else
133 br->feature_mask &= ~NETIF_F_TSO;
134
135 br_features_recompute(br);
136 return 0;
137}
138
139static int br_set_tx_csum(struct net_device *dev, u32 data)
140{
141 struct net_bridge *br = netdev_priv(dev);
142
143 if (data)
Herbert Xu2c6cc0d2006-06-17 22:06:45 -0700144 br->feature_mask |= NETIF_F_NO_CSUM;
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800145 else
Herbert Xu2c6cc0d2006-06-17 22:06:45 -0700146 br->feature_mask &= ~NETIF_F_ALL_CSUM;
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800147
148 br_features_recompute(br);
149 return 0;
150}
151
Stephen Hemmingera2dbb882008-11-19 21:49:00 -0800152static const struct ethtool_ops br_ethtool_ops = {
Stephen Hemmingere4119a42008-08-15 19:51:07 -0700153 .get_drvinfo = br_getinfo,
154 .get_link = ethtool_op_get_link,
155 .get_tx_csum = ethtool_op_get_tx_csum,
156 .set_tx_csum = br_set_tx_csum,
157 .get_sg = ethtool_op_get_sg,
158 .set_sg = br_set_sg,
159 .get_tso = ethtool_op_get_tso,
160 .set_tso = br_set_tso,
161 .get_ufo = ethtool_op_get_ufo,
Sridhar Samudrala72dad212009-10-07 12:41:17 +0000162 .set_ufo = ethtool_op_set_ufo,
Stephen Hemmingere4119a42008-08-15 19:51:07 -0700163 .get_flags = ethtool_op_get_flags,
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800164};
165
Stephen Hemmingera2dbb882008-11-19 21:49:00 -0800166static const struct net_device_ops br_netdev_ops = {
167 .ndo_open = br_dev_open,
168 .ndo_stop = br_dev_stop,
Stephen Hemminger00829822008-11-20 20:14:53 -0800169 .ndo_start_xmit = br_dev_xmit,
170 .ndo_set_mac_address = br_set_mac_address,
171 .ndo_set_multicast_list = br_dev_set_multicast_list,
172 .ndo_change_mtu = br_change_mtu,
173 .ndo_do_ioctl = br_dev_ioctl,
Stephen Hemmingera2dbb882008-11-19 21:49:00 -0800174};
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176void br_dev_setup(struct net_device *dev)
177{
Stephen Hemminger3ae41252007-12-16 13:35:51 -0800178 random_ether_addr(dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 ether_setup(dev);
180
Stephen Hemmingera2dbb882008-11-19 21:49:00 -0800181 dev->netdev_ops = &br_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 dev->destructor = free_netdev;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900183 SET_ETHTOOL_OPS(dev, &br_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 dev->tx_queue_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 dev->priv_flags = IFF_EBRIDGE;
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800186
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900187 dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
Alexey Dobriyan4aa678b2008-09-08 16:19:58 -0700188 NETIF_F_GSO_MASK | NETIF_F_NO_CSUM | NETIF_F_LLTX |
Herbert Xub63365a2008-10-23 01:11:29 -0700189 NETIF_F_NETNS_LOCAL | NETIF_F_GSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}