blob: d9449df7cad518d52d499b201d4e2cfd6ab4718f [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) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023int br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
24{
25 struct net_bridge *br = netdev_priv(dev);
26 const unsigned char *dest = skb->data;
27 struct net_bridge_fdb_entry *dst;
28
Pavel Emelyanova339f1c2008-05-21 14:13:47 -070029 dev->stats.tx_packets++;
30 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -070032 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 skb_pull(skb, ETH_HLEN);
34
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090035 if (dest[0] & 1)
Herbert Xue081e1e2007-09-16 16:20:48 -070036 br_flood_deliver(br, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 else if ((dst = __br_fdb_get(br, dest)) != NULL)
38 br_deliver(dst->dst, skb);
39 else
Herbert Xue081e1e2007-09-16 16:20:48 -070040 br_flood_deliver(br, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 return 0;
43}
44
45static int br_dev_open(struct net_device *dev)
46{
Stephen Hemminger81d35302005-05-29 14:15:17 -070047 struct net_bridge *br = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Stephen Hemminger81d35302005-05-29 14:15:17 -070049 br_features_recompute(br);
50 netif_start_queue(dev);
51 br_stp_enable_bridge(br);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 return 0;
54}
55
56static void br_dev_set_multicast_list(struct net_device *dev)
57{
58}
59
60static int br_dev_stop(struct net_device *dev)
61{
Stephen Hemminger81d35302005-05-29 14:15:17 -070062 br_stp_disable_bridge(netdev_priv(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 netif_stop_queue(dev);
65
66 return 0;
67}
68
69static int br_change_mtu(struct net_device *dev, int new_mtu)
70{
Stephen Hemminger81d35302005-05-29 14:15:17 -070071 if (new_mtu < 68 || new_mtu > br_min_mtu(netdev_priv(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return -EINVAL;
73
74 dev->mtu = new_mtu;
75 return 0;
76}
77
Stephen Hemmingerffe1d492007-04-09 11:49:58 -070078/* Allow setting mac address to any valid ethernet address. */
Stephen Hemminger4505a3e2005-12-21 18:51:49 -080079static int br_set_mac_address(struct net_device *dev, void *p)
80{
81 struct net_bridge *br = netdev_priv(dev);
82 struct sockaddr *addr = p;
Stephen Hemmingerffe1d492007-04-09 11:49:58 -070083
84 if (!is_valid_ether_addr(addr->sa_data))
85 return -EINVAL;
Stephen Hemminger4505a3e2005-12-21 18:51:49 -080086
87 spin_lock_bh(&br->lock);
Stephen Hemmingerffe1d492007-04-09 11:49:58 -070088 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
89 br_stp_change_bridge_id(br, addr->sa_data);
Stephen Hemminger92c05742008-06-17 16:10:06 -070090 br->flags |= BR_SET_MAC_ADDR;
Stephen Hemminger4505a3e2005-12-21 18:51:49 -080091 spin_unlock_bh(&br->lock);
92
Stephen Hemmingerffe1d492007-04-09 11:49:58 -070093 return 0;
Stephen Hemminger4505a3e2005-12-21 18:51:49 -080094}
95
Stephen Hemmingeredb5e462005-12-21 19:00:58 -080096static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
97{
98 strcpy(info->driver, "bridge");
99 strcpy(info->version, BR_VERSION);
100 strcpy(info->fw_version, "N/A");
101 strcpy(info->bus_info, "N/A");
102}
103
104static int br_set_sg(struct net_device *dev, u32 data)
105{
106 struct net_bridge *br = netdev_priv(dev);
107
108 if (data)
109 br->feature_mask |= NETIF_F_SG;
110 else
111 br->feature_mask &= ~NETIF_F_SG;
112
113 br_features_recompute(br);
114 return 0;
115}
116
117static int br_set_tso(struct net_device *dev, u32 data)
118{
119 struct net_bridge *br = netdev_priv(dev);
120
121 if (data)
122 br->feature_mask |= NETIF_F_TSO;
123 else
124 br->feature_mask &= ~NETIF_F_TSO;
125
126 br_features_recompute(br);
127 return 0;
128}
129
130static int br_set_tx_csum(struct net_device *dev, u32 data)
131{
132 struct net_bridge *br = netdev_priv(dev);
133
134 if (data)
Herbert Xu2c6cc0d2006-06-17 22:06:45 -0700135 br->feature_mask |= NETIF_F_NO_CSUM;
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800136 else
Herbert Xu2c6cc0d2006-06-17 22:06:45 -0700137 br->feature_mask &= ~NETIF_F_ALL_CSUM;
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800138
139 br_features_recompute(br);
140 return 0;
141}
142
143static struct ethtool_ops br_ethtool_ops = {
144 .get_drvinfo = br_getinfo,
145 .get_link = ethtool_op_get_link,
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800146 .set_sg = br_set_sg,
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800147 .set_tx_csum = br_set_tx_csum,
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800148 .set_tso = br_set_tso,
149};
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151void br_dev_setup(struct net_device *dev)
152{
Stephen Hemminger3ae41252007-12-16 13:35:51 -0800153 random_ether_addr(dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 ether_setup(dev);
155
156 dev->do_ioctl = br_dev_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 dev->hard_start_xmit = br_dev_xmit;
158 dev->open = br_dev_open;
159 dev->set_multicast_list = br_dev_set_multicast_list;
160 dev->change_mtu = br_change_mtu;
161 dev->destructor = free_netdev;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900162 SET_ETHTOOL_OPS(dev, &br_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 dev->stop = br_dev_stop;
164 dev->tx_queue_len = 0;
Stephen Hemminger4505a3e2005-12-21 18:51:49 -0800165 dev->set_mac_address = br_set_mac_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 dev->priv_flags = IFF_EBRIDGE;
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800167
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900168 dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
Herbert Xu7f353bf2007-08-10 15:47:58 -0700169 NETIF_F_GSO_MASK | NETIF_F_NO_CSUM | NETIF_F_LLTX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}