blob: eedf2c94820e26150d8823c2aa1652626c5f281d [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>
WANG Congc06ee962010-05-06 00:48:24 -070016#include <linux/netpoll.h>
Stephen Hemminger4505a3e2005-12-21 18:51:49 -080017#include <linux/etherdevice.h>
Stephen Hemmingeredb5e462005-12-21 19:00:58 -080018#include <linux/ethtool.h>
WANG Congc06ee962010-05-06 00:48:24 -070019#include <linux/list.h>
Bart De Schuymerea2d9b42010-04-15 12:14:51 +020020#include <linux/netfilter_bridge.h>
Stephen Hemminger4505a3e2005-12-21 18:51:49 -080021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/uaccess.h>
23#include "br_private.h"
24
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -080025/* net device transmit always called with no BH (preempt_disabled) */
Stephen Hemminger6fef4c02009-08-31 19:50:41 +000026netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 struct net_bridge *br = netdev_priv(dev);
29 const unsigned char *dest = skb->data;
30 struct net_bridge_fdb_entry *dst;
Herbert Xuc4fcb782010-02-27 19:41:48 +000031 struct net_bridge_mdb_entry *mdst;
stephen hemminger14bb4782010-03-02 13:32:09 +000032 struct br_cpu_netstats *brstats = this_cpu_ptr(br->stats);
33
Bart De Schuymerea2d9b42010-04-15 12:14:51 +020034#ifdef CONFIG_BRIDGE_NETFILTER
35 if (skb->nf_bridge && (skb->nf_bridge->mask & BRNF_BRIDGED_DNAT)) {
36 br_nf_pre_routing_finish_bridge_slow(skb);
37 return NETDEV_TX_OK;
38 }
39#endif
40
stephen hemminger14bb4782010-03-02 13:32:09 +000041 brstats->tx_packets++;
42 brstats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Herbert Xu6088a532010-02-27 19:41:42 +000044 BR_INPUT_SKB_CB(skb)->brdev = dev;
45
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -070046 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 skb_pull(skb, ETH_HLEN);
48
stephen hemminger7180f772010-04-27 07:13:06 +000049 if (is_multicast_ether_addr(dest)) {
Herbert Xuc4fcb782010-02-27 19:41:48 +000050 if (br_multicast_rcv(br, NULL, skb))
51 goto out;
52
53 mdst = br_mdb_get(br, skb);
YOSHIFUJI Hideaki / 吉藤英明32dec5d2010-03-15 21:51:18 +000054 if (mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb))
Herbert Xuc4fcb782010-02-27 19:41:48 +000055 br_multicast_deliver(mdst, skb);
56 else
57 br_flood_deliver(br, skb);
58 } else if ((dst = __br_fdb_get(br, dest)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 br_deliver(dst->dst, skb);
60 else
Herbert Xue081e1e2007-09-16 16:20:48 -070061 br_flood_deliver(br, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Herbert Xuc4fcb782010-02-27 19:41:48 +000063out:
Patrick McHardy6ed10652009-06-23 06:03:08 +000064 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67static int br_dev_open(struct net_device *dev)
68{
Stephen Hemminger81d35302005-05-29 14:15:17 -070069 struct net_bridge *br = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Stephen Hemminger81d35302005-05-29 14:15:17 -070071 br_features_recompute(br);
72 netif_start_queue(dev);
73 br_stp_enable_bridge(br);
Herbert Xu3fe2d7c2010-02-28 00:49:38 -080074 br_multicast_open(br);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 return 0;
77}
78
79static void br_dev_set_multicast_list(struct net_device *dev)
80{
81}
82
83static int br_dev_stop(struct net_device *dev)
84{
Herbert Xu3fe2d7c2010-02-28 00:49:38 -080085 struct net_bridge *br = netdev_priv(dev);
86
87 br_stp_disable_bridge(br);
88 br_multicast_stop(br);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 netif_stop_queue(dev);
91
92 return 0;
93}
94
stephen hemminger14bb4782010-03-02 13:32:09 +000095static struct net_device_stats *br_get_stats(struct net_device *dev)
96{
97 struct net_bridge *br = netdev_priv(dev);
98 struct net_device_stats *stats = &dev->stats;
99 struct br_cpu_netstats sum = { 0 };
100 unsigned int cpu;
101
102 for_each_possible_cpu(cpu) {
103 const struct br_cpu_netstats *bstats
104 = per_cpu_ptr(br->stats, cpu);
105
106 sum.tx_bytes += bstats->tx_bytes;
107 sum.tx_packets += bstats->tx_packets;
108 sum.rx_bytes += bstats->rx_bytes;
109 sum.rx_packets += bstats->rx_packets;
110 }
111
112 stats->tx_bytes = sum.tx_bytes;
113 stats->tx_packets = sum.tx_packets;
114 stats->rx_bytes = sum.rx_bytes;
115 stats->rx_packets = sum.rx_packets;
116
117 return stats;
118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static int br_change_mtu(struct net_device *dev, int new_mtu)
121{
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700122 struct net_bridge *br = netdev_priv(dev);
123 if (new_mtu < 68 || new_mtu > br_min_mtu(br))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return -EINVAL;
125
126 dev->mtu = new_mtu;
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700127
128#ifdef CONFIG_BRIDGE_NETFILTER
129 /* remember the MTU in the rtable for PMTU */
130 br->fake_rtable.u.dst.metrics[RTAX_MTU - 1] = new_mtu;
131#endif
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return 0;
134}
135
Stephen Hemmingerffe1d492007-04-09 11:49:58 -0700136/* Allow setting mac address to any valid ethernet address. */
Stephen Hemminger4505a3e2005-12-21 18:51:49 -0800137static int br_set_mac_address(struct net_device *dev, void *p)
138{
139 struct net_bridge *br = netdev_priv(dev);
140 struct sockaddr *addr = p;
Stephen Hemmingerffe1d492007-04-09 11:49:58 -0700141
142 if (!is_valid_ether_addr(addr->sa_data))
143 return -EINVAL;
Stephen Hemminger4505a3e2005-12-21 18:51:49 -0800144
145 spin_lock_bh(&br->lock);
Stephen Hemmingerffe1d492007-04-09 11:49:58 -0700146 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
147 br_stp_change_bridge_id(br, addr->sa_data);
Stephen Hemminger92c05742008-06-17 16:10:06 -0700148 br->flags |= BR_SET_MAC_ADDR;
Stephen Hemminger4505a3e2005-12-21 18:51:49 -0800149 spin_unlock_bh(&br->lock);
150
Stephen Hemmingerffe1d492007-04-09 11:49:58 -0700151 return 0;
Stephen Hemminger4505a3e2005-12-21 18:51:49 -0800152}
153
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800154static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
155{
156 strcpy(info->driver, "bridge");
157 strcpy(info->version, BR_VERSION);
158 strcpy(info->fw_version, "N/A");
159 strcpy(info->bus_info, "N/A");
160}
161
162static int br_set_sg(struct net_device *dev, u32 data)
163{
164 struct net_bridge *br = netdev_priv(dev);
165
166 if (data)
167 br->feature_mask |= NETIF_F_SG;
168 else
169 br->feature_mask &= ~NETIF_F_SG;
170
171 br_features_recompute(br);
172 return 0;
173}
174
175static int br_set_tso(struct net_device *dev, u32 data)
176{
177 struct net_bridge *br = netdev_priv(dev);
178
179 if (data)
180 br->feature_mask |= NETIF_F_TSO;
181 else
182 br->feature_mask &= ~NETIF_F_TSO;
183
184 br_features_recompute(br);
185 return 0;
186}
187
188static int br_set_tx_csum(struct net_device *dev, u32 data)
189{
190 struct net_bridge *br = netdev_priv(dev);
191
192 if (data)
Herbert Xu2c6cc0d2006-06-17 22:06:45 -0700193 br->feature_mask |= NETIF_F_NO_CSUM;
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800194 else
Herbert Xu2c6cc0d2006-06-17 22:06:45 -0700195 br->feature_mask &= ~NETIF_F_ALL_CSUM;
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800196
197 br_features_recompute(br);
198 return 0;
199}
200
WANG Congc06ee962010-05-06 00:48:24 -0700201#ifdef CONFIG_NET_POLL_CONTROLLER
stephen hemmingercfb478d2010-05-10 09:31:08 +0000202static bool br_devices_support_netpoll(struct net_bridge *br)
WANG Congc06ee962010-05-06 00:48:24 -0700203{
204 struct net_bridge_port *p;
205 bool ret = true;
206 int count = 0;
207 unsigned long flags;
208
209 spin_lock_irqsave(&br->lock, flags);
210 list_for_each_entry(p, &br->port_list, list) {
211 count++;
212 if ((p->dev->priv_flags & IFF_DISABLE_NETPOLL) ||
213 !p->dev->netdev_ops->ndo_poll_controller)
214 ret = false;
215 }
216 spin_unlock_irqrestore(&br->lock, flags);
217 return count != 0 && ret;
218}
219
220static void br_poll_controller(struct net_device *br_dev)
221{
222 struct netpoll *np = br_dev->npinfo->netpoll;
223
224 if (np->real_dev != br_dev)
225 netpoll_poll_dev(np->real_dev);
226}
227
stephen hemmingercfb478d2010-05-10 09:31:08 +0000228void br_netpoll_cleanup(struct net_device *dev)
WANG Congc06ee962010-05-06 00:48:24 -0700229{
stephen hemmingercfb478d2010-05-10 09:31:08 +0000230 struct net_bridge *br = netdev_priv(dev);
WANG Congc06ee962010-05-06 00:48:24 -0700231 struct net_bridge_port *p, *n;
232 const struct net_device_ops *ops;
233
234 br->dev->npinfo = NULL;
235 list_for_each_entry_safe(p, n, &br->port_list, list) {
236 if (p->dev) {
237 ops = p->dev->netdev_ops;
238 if (ops->ndo_netpoll_cleanup)
239 ops->ndo_netpoll_cleanup(p->dev);
240 else
241 p->dev->npinfo = NULL;
242 }
243 }
244}
245
stephen hemmingercfb478d2010-05-10 09:31:08 +0000246void br_netpoll_disable(struct net_bridge *br,
247 struct net_device *dev)
WANG Congc06ee962010-05-06 00:48:24 -0700248{
stephen hemmingercfb478d2010-05-10 09:31:08 +0000249 if (br_devices_support_netpoll(br))
250 br->dev->priv_flags &= ~IFF_DISABLE_NETPOLL;
251 if (dev->netdev_ops->ndo_netpoll_cleanup)
252 dev->netdev_ops->ndo_netpoll_cleanup(dev);
253 else
254 dev->npinfo = NULL;
255}
256
257void br_netpoll_enable(struct net_bridge *br,
258 struct net_device *dev)
259{
260 if (br_devices_support_netpoll(br)) {
261 br->dev->priv_flags &= ~IFF_DISABLE_NETPOLL;
262 if (br->dev->npinfo)
263 dev->npinfo = br->dev->npinfo;
264 } else if (!(br->dev->priv_flags & IFF_DISABLE_NETPOLL)) {
265 br->dev->priv_flags |= IFF_DISABLE_NETPOLL;
stephen hemminger28a16c92010-05-10 09:31:09 +0000266 br_info(br,"new device %s does not support netpoll (disabling)",
267 dev->name);
stephen hemmingercfb478d2010-05-10 09:31:08 +0000268 }
WANG Congc06ee962010-05-06 00:48:24 -0700269}
270
271#endif
272
Stephen Hemmingera2dbb882008-11-19 21:49:00 -0800273static const struct ethtool_ops br_ethtool_ops = {
Stephen Hemmingere4119a42008-08-15 19:51:07 -0700274 .get_drvinfo = br_getinfo,
275 .get_link = ethtool_op_get_link,
276 .get_tx_csum = ethtool_op_get_tx_csum,
277 .set_tx_csum = br_set_tx_csum,
278 .get_sg = ethtool_op_get_sg,
279 .set_sg = br_set_sg,
280 .get_tso = ethtool_op_get_tso,
281 .set_tso = br_set_tso,
282 .get_ufo = ethtool_op_get_ufo,
Sridhar Samudrala72dad212009-10-07 12:41:17 +0000283 .set_ufo = ethtool_op_set_ufo,
Stephen Hemmingere4119a42008-08-15 19:51:07 -0700284 .get_flags = ethtool_op_get_flags,
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800285};
286
Stephen Hemmingera2dbb882008-11-19 21:49:00 -0800287static const struct net_device_ops br_netdev_ops = {
288 .ndo_open = br_dev_open,
289 .ndo_stop = br_dev_stop,
Stephen Hemminger00829822008-11-20 20:14:53 -0800290 .ndo_start_xmit = br_dev_xmit,
stephen hemminger14bb4782010-03-02 13:32:09 +0000291 .ndo_get_stats = br_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -0800292 .ndo_set_mac_address = br_set_mac_address,
293 .ndo_set_multicast_list = br_dev_set_multicast_list,
294 .ndo_change_mtu = br_change_mtu,
295 .ndo_do_ioctl = br_dev_ioctl,
WANG Congc06ee962010-05-06 00:48:24 -0700296#ifdef CONFIG_NET_POLL_CONTROLLER
297 .ndo_netpoll_cleanup = br_netpoll_cleanup,
298 .ndo_poll_controller = br_poll_controller,
299#endif
Stephen Hemmingera2dbb882008-11-19 21:49:00 -0800300};
301
stephen hemminger14bb4782010-03-02 13:32:09 +0000302static void br_dev_free(struct net_device *dev)
303{
304 struct net_bridge *br = netdev_priv(dev);
305
306 free_percpu(br->stats);
307 free_netdev(dev);
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310void br_dev_setup(struct net_device *dev)
311{
Stephen Hemminger3ae41252007-12-16 13:35:51 -0800312 random_ether_addr(dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 ether_setup(dev);
314
Stephen Hemmingera2dbb882008-11-19 21:49:00 -0800315 dev->netdev_ops = &br_netdev_ops;
stephen hemminger14bb4782010-03-02 13:32:09 +0000316 dev->destructor = br_dev_free;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900317 SET_ETHTOOL_OPS(dev, &br_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 dev->tx_queue_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 dev->priv_flags = IFF_EBRIDGE;
Stephen Hemmingeredb5e462005-12-21 19:00:58 -0800320
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900321 dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
Alexey Dobriyan4aa678b2008-09-08 16:19:58 -0700322 NETIF_F_GSO_MASK | NETIF_F_NO_CSUM | NETIF_F_LLTX |
Herbert Xub63365a2008-10-23 01:11:29 -0700323 NETIF_F_NETNS_LOCAL | NETIF_F_GSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}