blob: 0b33a7b3a00cd5c05b0b8e16ec91f0aec4a58599 [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 *
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 Hemminger4505a3e2005-12-21 18:51:49 -080018#include <linux/etherdevice.h>
Stephen Hemmingeredb5e462005-12-21 19:00:58 -080019#include <linux/ethtool.h>
Stephen Hemminger4505a3e2005-12-21 18:51:49 -080020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/uaccess.h>
22#include "br_private.h"
23
24static struct net_device_stats *br_dev_get_stats(struct net_device *dev)
25{
Stephen Hemminger81d35302005-05-29 14:15:17 -070026 struct net_bridge *br = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 return &br->statistics;
28}
29
30int br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
31{
32 struct net_bridge *br = netdev_priv(dev);
33 const unsigned char *dest = skb->data;
34 struct net_bridge_fdb_entry *dst;
35
36 br->statistics.tx_packets++;
37 br->statistics.tx_bytes += skb->len;
38
39 skb->mac.raw = skb->data;
40 skb_pull(skb, ETH_HLEN);
41
42 rcu_read_lock();
43 if (dest[0] & 1)
44 br_flood_deliver(br, skb, 0);
45 else if ((dst = __br_fdb_get(br, dest)) != NULL)
46 br_deliver(dst->dst, skb);
47 else
48 br_flood_deliver(br, skb, 0);
49
50 rcu_read_unlock();
51 return 0;
52}
53
54static int br_dev_open(struct net_device *dev)
55{
Stephen Hemminger81d35302005-05-29 14:15:17 -070056 struct net_bridge *br = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Stephen Hemminger81d35302005-05-29 14:15:17 -070058 br_features_recompute(br);
59 netif_start_queue(dev);
60 br_stp_enable_bridge(br);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 return 0;
63}
64
65static void br_dev_set_multicast_list(struct net_device *dev)
66{
67}
68
69static int br_dev_stop(struct net_device *dev)
70{
Stephen Hemminger81d35302005-05-29 14:15:17 -070071 br_stp_disable_bridge(netdev_priv(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 netif_stop_queue(dev);
74
75 return 0;
76}
77
78static int br_change_mtu(struct net_device *dev, int new_mtu)
79{
Stephen Hemminger81d35302005-05-29 14:15:17 -070080 if (new_mtu < 68 || new_mtu > br_min_mtu(netdev_priv(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return -EINVAL;
82
83 dev->mtu = new_mtu;
84 return 0;
85}
86
Stephen Hemminger4505a3e2005-12-21 18:51:49 -080087/* Allow setting mac address of pseudo-bridge to be same as
88 * any of the bound interfaces
89 */
90static int br_set_mac_address(struct net_device *dev, void *p)
91{
92 struct net_bridge *br = netdev_priv(dev);
93 struct sockaddr *addr = p;
94 struct net_bridge_port *port;
95 int err = -EADDRNOTAVAIL;
96
97 spin_lock_bh(&br->lock);
98 list_for_each_entry(port, &br->port_list, list) {
99 if (!compare_ether_addr(port->dev->dev_addr, addr->sa_data)) {
100 br_stp_change_bridge_id(br, addr->sa_data);
101 err = 0;
102 break;
103 }
104 }
105 spin_unlock_bh(&br->lock);
106
107 return err;
108}
109
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800110static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
111{
112 strcpy(info->driver, "bridge");
113 strcpy(info->version, BR_VERSION);
114 strcpy(info->fw_version, "N/A");
115 strcpy(info->bus_info, "N/A");
116}
117
118static int br_set_sg(struct net_device *dev, u32 data)
119{
120 struct net_bridge *br = netdev_priv(dev);
121
122 if (data)
123 br->feature_mask |= NETIF_F_SG;
124 else
125 br->feature_mask &= ~NETIF_F_SG;
126
127 br_features_recompute(br);
128 return 0;
129}
130
131static int br_set_tso(struct net_device *dev, u32 data)
132{
133 struct net_bridge *br = netdev_priv(dev);
134
135 if (data)
136 br->feature_mask |= NETIF_F_TSO;
137 else
138 br->feature_mask &= ~NETIF_F_TSO;
139
140 br_features_recompute(br);
141 return 0;
142}
143
144static int br_set_tx_csum(struct net_device *dev, u32 data)
145{
146 struct net_bridge *br = netdev_priv(dev);
147
148 if (data)
149 br->feature_mask |= NETIF_F_IP_CSUM;
150 else
151 br->feature_mask &= ~NETIF_F_IP_CSUM;
152
153 br_features_recompute(br);
154 return 0;
155}
156
157static struct ethtool_ops br_ethtool_ops = {
158 .get_drvinfo = br_getinfo,
159 .get_link = ethtool_op_get_link,
160 .get_sg = ethtool_op_get_sg,
161 .set_sg = br_set_sg,
162 .get_tx_csum = ethtool_op_get_tx_csum,
163 .set_tx_csum = br_set_tx_csum,
164 .get_tso = ethtool_op_get_tso,
165 .set_tso = br_set_tso,
166};
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168void br_dev_setup(struct net_device *dev)
169{
170 memset(dev->dev_addr, 0, ETH_ALEN);
171
172 ether_setup(dev);
173
174 dev->do_ioctl = br_dev_ioctl;
175 dev->get_stats = br_dev_get_stats;
176 dev->hard_start_xmit = br_dev_xmit;
177 dev->open = br_dev_open;
178 dev->set_multicast_list = br_dev_set_multicast_list;
179 dev->change_mtu = br_change_mtu;
180 dev->destructor = free_netdev;
181 SET_MODULE_OWNER(dev);
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800182 SET_ETHTOOL_OPS(dev, &br_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 dev->stop = br_dev_stop;
184 dev->tx_queue_len = 0;
Stephen Hemminger4505a3e2005-12-21 18:51:49 -0800185 dev->set_mac_address = br_set_mac_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 dev->priv_flags = IFF_EBRIDGE;
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800187
188 dev->features = NETIF_F_SG | NETIF_F_FRAGLIST
189 | NETIF_F_HIGHDMA | NETIF_F_TSO | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}