]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/bridge/br_device.c
act_nat: the checksum of ICMP doesn't have pseudo header
[net-next-2.6.git] / net / bridge / br_device.c
1 /*
2  *      Device handling code
3  *      Linux ethernet bridge
4  *
5  *      Authors:
6  *      Lennert Buytenhek               <buytenh@gnu.org>
7  *
8  *      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>
16 #include <linux/netpoll.h>
17 #include <linux/etherdevice.h>
18 #include <linux/ethtool.h>
19 #include <linux/list.h>
20 #include <linux/netfilter_bridge.h>
21
22 #include <asm/uaccess.h>
23 #include "br_private.h"
24
25 /* net device transmit always called with BH disabled */
26 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
27 {
28         struct net_bridge *br = netdev_priv(dev);
29         const unsigned char *dest = skb->data;
30         struct net_bridge_fdb_entry *dst;
31         struct net_bridge_mdb_entry *mdst;
32         struct br_cpu_netstats *brstats = this_cpu_ptr(br->stats);
33
34 #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
41         brstats->tx_packets++;
42         brstats->tx_bytes += skb->len;
43
44         BR_INPUT_SKB_CB(skb)->brdev = dev;
45
46         skb_reset_mac_header(skb);
47         skb_pull(skb, ETH_HLEN);
48
49         rcu_read_lock();
50         if (is_multicast_ether_addr(dest)) {
51                 if (br_multicast_rcv(br, NULL, skb)) {
52                         kfree_skb(skb);
53                         goto out;
54                 }
55
56                 mdst = br_mdb_get(br, skb);
57                 if (mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb))
58                         br_multicast_deliver(mdst, skb);
59                 else
60                         br_flood_deliver(br, skb);
61         } else if ((dst = __br_fdb_get(br, dest)) != NULL)
62                 br_deliver(dst->dst, skb);
63         else
64                 br_flood_deliver(br, skb);
65
66 out:
67         rcu_read_unlock();
68         return NETDEV_TX_OK;
69 }
70
71 static int br_dev_open(struct net_device *dev)
72 {
73         struct net_bridge *br = netdev_priv(dev);
74
75         br_features_recompute(br);
76         netif_start_queue(dev);
77         br_stp_enable_bridge(br);
78         br_multicast_open(br);
79
80         return 0;
81 }
82
83 static void br_dev_set_multicast_list(struct net_device *dev)
84 {
85 }
86
87 static int br_dev_stop(struct net_device *dev)
88 {
89         struct net_bridge *br = netdev_priv(dev);
90
91         br_stp_disable_bridge(br);
92         br_multicast_stop(br);
93
94         netif_stop_queue(dev);
95
96         return 0;
97 }
98
99 static struct net_device_stats *br_get_stats(struct net_device *dev)
100 {
101         struct net_bridge *br = netdev_priv(dev);
102         struct net_device_stats *stats = &dev->stats;
103         struct br_cpu_netstats sum = { 0 };
104         unsigned int cpu;
105
106         for_each_possible_cpu(cpu) {
107                 const struct br_cpu_netstats *bstats
108                         = per_cpu_ptr(br->stats, cpu);
109
110                 sum.tx_bytes   += bstats->tx_bytes;
111                 sum.tx_packets += bstats->tx_packets;
112                 sum.rx_bytes   += bstats->rx_bytes;
113                 sum.rx_packets += bstats->rx_packets;
114         }
115
116         stats->tx_bytes   = sum.tx_bytes;
117         stats->tx_packets = sum.tx_packets;
118         stats->rx_bytes   = sum.rx_bytes;
119         stats->rx_packets = sum.rx_packets;
120
121         return stats;
122 }
123
124 static int br_change_mtu(struct net_device *dev, int new_mtu)
125 {
126         struct net_bridge *br = netdev_priv(dev);
127         if (new_mtu < 68 || new_mtu > br_min_mtu(br))
128                 return -EINVAL;
129
130         dev->mtu = new_mtu;
131
132 #ifdef CONFIG_BRIDGE_NETFILTER
133         /* remember the MTU in the rtable for PMTU */
134         br->fake_rtable.u.dst.metrics[RTAX_MTU - 1] = new_mtu;
135 #endif
136
137         return 0;
138 }
139
140 /* Allow setting mac address to any valid ethernet address. */
141 static int br_set_mac_address(struct net_device *dev, void *p)
142 {
143         struct net_bridge *br = netdev_priv(dev);
144         struct sockaddr *addr = p;
145
146         if (!is_valid_ether_addr(addr->sa_data))
147                 return -EINVAL;
148
149         spin_lock_bh(&br->lock);
150         memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
151         br_stp_change_bridge_id(br, addr->sa_data);
152         br->flags |= BR_SET_MAC_ADDR;
153         spin_unlock_bh(&br->lock);
154
155         return 0;
156 }
157
158 static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
159 {
160         strcpy(info->driver, "bridge");
161         strcpy(info->version, BR_VERSION);
162         strcpy(info->fw_version, "N/A");
163         strcpy(info->bus_info, "N/A");
164 }
165
166 static int br_set_sg(struct net_device *dev, u32 data)
167 {
168         struct net_bridge *br = netdev_priv(dev);
169
170         if (data)
171                 br->feature_mask |= NETIF_F_SG;
172         else
173                 br->feature_mask &= ~NETIF_F_SG;
174
175         br_features_recompute(br);
176         return 0;
177 }
178
179 static int br_set_tso(struct net_device *dev, u32 data)
180 {
181         struct net_bridge *br = netdev_priv(dev);
182
183         if (data)
184                 br->feature_mask |= NETIF_F_TSO;
185         else
186                 br->feature_mask &= ~NETIF_F_TSO;
187
188         br_features_recompute(br);
189         return 0;
190 }
191
192 static int br_set_tx_csum(struct net_device *dev, u32 data)
193 {
194         struct net_bridge *br = netdev_priv(dev);
195
196         if (data)
197                 br->feature_mask |= NETIF_F_NO_CSUM;
198         else
199                 br->feature_mask &= ~NETIF_F_ALL_CSUM;
200
201         br_features_recompute(br);
202         return 0;
203 }
204
205 #ifdef CONFIG_NET_POLL_CONTROLLER
206 static bool br_devices_support_netpoll(struct net_bridge *br)
207 {
208         struct net_bridge_port *p;
209         bool ret = true;
210         int count = 0;
211         unsigned long flags;
212
213         spin_lock_irqsave(&br->lock, flags);
214         list_for_each_entry(p, &br->port_list, list) {
215                 count++;
216                 if ((p->dev->priv_flags & IFF_DISABLE_NETPOLL) ||
217                     !p->dev->netdev_ops->ndo_poll_controller)
218                         ret = false;
219         }
220         spin_unlock_irqrestore(&br->lock, flags);
221         return count != 0 && ret;
222 }
223
224 void br_netpoll_cleanup(struct net_device *dev)
225 {
226         struct net_bridge *br = netdev_priv(dev);
227         struct net_bridge_port *p, *n;
228         const struct net_device_ops *ops;
229
230         br->dev->npinfo = NULL;
231         list_for_each_entry_safe(p, n, &br->port_list, list) {
232                 if (p->dev) {
233                         ops = p->dev->netdev_ops;
234                         if (ops->ndo_netpoll_cleanup)
235                                 ops->ndo_netpoll_cleanup(p->dev);
236                         else
237                                 p->dev->npinfo = NULL;
238                 }
239         }
240 }
241
242 void br_netpoll_disable(struct net_bridge *br,
243                         struct net_device *dev)
244 {
245         if (br_devices_support_netpoll(br))
246                 br->dev->priv_flags &= ~IFF_DISABLE_NETPOLL;
247         if (dev->netdev_ops->ndo_netpoll_cleanup)
248                 dev->netdev_ops->ndo_netpoll_cleanup(dev);
249         else
250                 dev->npinfo = NULL;
251 }
252
253 void br_netpoll_enable(struct net_bridge *br,
254                        struct net_device *dev)
255 {
256         if (br_devices_support_netpoll(br)) {
257                 br->dev->priv_flags &= ~IFF_DISABLE_NETPOLL;
258                 if (br->dev->npinfo)
259                         dev->npinfo = br->dev->npinfo;
260         } else if (!(br->dev->priv_flags & IFF_DISABLE_NETPOLL)) {
261                 br->dev->priv_flags |= IFF_DISABLE_NETPOLL;
262                 br_info(br,"new device %s does not support netpoll (disabling)",
263                         dev->name);
264         }
265 }
266
267 #endif
268
269 static const struct ethtool_ops br_ethtool_ops = {
270         .get_drvinfo    = br_getinfo,
271         .get_link       = ethtool_op_get_link,
272         .get_tx_csum    = ethtool_op_get_tx_csum,
273         .set_tx_csum    = br_set_tx_csum,
274         .get_sg         = ethtool_op_get_sg,
275         .set_sg         = br_set_sg,
276         .get_tso        = ethtool_op_get_tso,
277         .set_tso        = br_set_tso,
278         .get_ufo        = ethtool_op_get_ufo,
279         .set_ufo        = ethtool_op_set_ufo,
280         .get_flags      = ethtool_op_get_flags,
281 };
282
283 static const struct net_device_ops br_netdev_ops = {
284         .ndo_open                = br_dev_open,
285         .ndo_stop                = br_dev_stop,
286         .ndo_start_xmit          = br_dev_xmit,
287         .ndo_get_stats           = br_get_stats,
288         .ndo_set_mac_address     = br_set_mac_address,
289         .ndo_set_multicast_list  = br_dev_set_multicast_list,
290         .ndo_change_mtu          = br_change_mtu,
291         .ndo_do_ioctl            = br_dev_ioctl,
292 #ifdef CONFIG_NET_POLL_CONTROLLER
293         .ndo_netpoll_cleanup     = br_netpoll_cleanup,
294 #endif
295 };
296
297 static void br_dev_free(struct net_device *dev)
298 {
299         struct net_bridge *br = netdev_priv(dev);
300
301         free_percpu(br->stats);
302         free_netdev(dev);
303 }
304
305 void br_dev_setup(struct net_device *dev)
306 {
307         random_ether_addr(dev->dev_addr);
308         ether_setup(dev);
309
310         dev->netdev_ops = &br_netdev_ops;
311         dev->destructor = br_dev_free;
312         SET_ETHTOOL_OPS(dev, &br_ethtool_ops);
313         dev->tx_queue_len = 0;
314         dev->priv_flags = IFF_EBRIDGE;
315
316         dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
317                         NETIF_F_GSO_MASK | NETIF_F_NO_CSUM | NETIF_F_LLTX |
318                         NETIF_F_NETNS_LOCAL | NETIF_F_GSO;
319 }