007bde87415d1ead4b6b1ffc1aa14c6be67a714e
[linux-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/etherdevice.h>
17 #include <linux/ethtool.h>
18 #include <linux/netfilter_bridge.h>
19 #include <asm/uaccess.h>
20 #include "br_private.h"
21
22 /* net device transmit always called with no BH (preempt_disabled) */
23 netdev_tx_t 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         struct net_bridge_mdb_entry *mdst;
29         struct br_cpu_netstats *brstats = this_cpu_ptr(br->stats);
30
31 #ifdef CONFIG_BRIDGE_NETFILTER
32         if (skb->nf_bridge && (skb->nf_bridge->mask & BRNF_BRIDGED_DNAT)) {
33                 br_nf_pre_routing_finish_bridge_slow(skb);
34                 return NETDEV_TX_OK;
35         }
36 #endif
37
38         brstats->tx_packets++;
39         brstats->tx_bytes += skb->len;
40
41         BR_INPUT_SKB_CB(skb)->brdev = dev;
42
43         skb_reset_mac_header(skb);
44         skb_pull(skb, ETH_HLEN);
45
46         if (dest[0] & 1) {
47                 if (br_multicast_rcv(br, NULL, skb))
48                         goto out;
49
50                 mdst = br_mdb_get(br, skb);
51                 if (mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb))
52                         br_multicast_deliver(mdst, skb);
53                 else
54                         br_flood_deliver(br, skb);
55         } else if ((dst = __br_fdb_get(br, dest)) != NULL)
56                 br_deliver(dst->dst, skb);
57         else
58                 br_flood_deliver(br, skb);
59
60 out:
61         return NETDEV_TX_OK;
62 }
63
64 static int br_dev_open(struct net_device *dev)
65 {
66         struct net_bridge *br = netdev_priv(dev);
67
68         br_features_recompute(br);
69         netif_start_queue(dev);
70         br_stp_enable_bridge(br);
71         br_multicast_open(br);
72
73         return 0;
74 }
75
76 static void br_dev_set_multicast_list(struct net_device *dev)
77 {
78 }
79
80 static int br_dev_stop(struct net_device *dev)
81 {
82         struct net_bridge *br = netdev_priv(dev);
83
84         br_stp_disable_bridge(br);
85         br_multicast_stop(br);
86
87         netif_stop_queue(dev);
88
89         return 0;
90 }
91
92 static struct net_device_stats *br_get_stats(struct net_device *dev)
93 {
94         struct net_bridge *br = netdev_priv(dev);
95         struct net_device_stats *stats = &dev->stats;
96         struct br_cpu_netstats sum = { 0 };
97         unsigned int cpu;
98
99         for_each_possible_cpu(cpu) {
100                 const struct br_cpu_netstats *bstats
101                         = per_cpu_ptr(br->stats, cpu);
102
103                 sum.tx_bytes   += bstats->tx_bytes;
104                 sum.tx_packets += bstats->tx_packets;
105                 sum.rx_bytes   += bstats->rx_bytes;
106                 sum.rx_packets += bstats->rx_packets;
107         }
108
109         stats->tx_bytes   = sum.tx_bytes;
110         stats->tx_packets = sum.tx_packets;
111         stats->rx_bytes   = sum.rx_bytes;
112         stats->rx_packets = sum.rx_packets;
113
114         return stats;
115 }
116
117 static int br_change_mtu(struct net_device *dev, int new_mtu)
118 {
119         struct net_bridge *br = netdev_priv(dev);
120         if (new_mtu < 68 || new_mtu > br_min_mtu(br))
121                 return -EINVAL;
122
123         dev->mtu = new_mtu;
124
125 #ifdef CONFIG_BRIDGE_NETFILTER
126         /* remember the MTU in the rtable for PMTU */
127         br->fake_rtable.u.dst.metrics[RTAX_MTU - 1] = new_mtu;
128 #endif
129
130         return 0;
131 }
132
133 /* Allow setting mac address to any valid ethernet address. */
134 static int br_set_mac_address(struct net_device *dev, void *p)
135 {
136         struct net_bridge *br = netdev_priv(dev);
137         struct sockaddr *addr = p;
138
139         if (!is_valid_ether_addr(addr->sa_data))
140                 return -EINVAL;
141
142         spin_lock_bh(&br->lock);
143         memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
144         br_stp_change_bridge_id(br, addr->sa_data);
145         br->flags |= BR_SET_MAC_ADDR;
146         spin_unlock_bh(&br->lock);
147
148         return 0;
149 }
150
151 static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
152 {
153         strcpy(info->driver, "bridge");
154         strcpy(info->version, BR_VERSION);
155         strcpy(info->fw_version, "N/A");
156         strcpy(info->bus_info, "N/A");
157 }
158
159 static int br_set_sg(struct net_device *dev, u32 data)
160 {
161         struct net_bridge *br = netdev_priv(dev);
162
163         if (data)
164                 br->feature_mask |= NETIF_F_SG;
165         else
166                 br->feature_mask &= ~NETIF_F_SG;
167
168         br_features_recompute(br);
169         return 0;
170 }
171
172 static int br_set_tso(struct net_device *dev, u32 data)
173 {
174         struct net_bridge *br = netdev_priv(dev);
175
176         if (data)
177                 br->feature_mask |= NETIF_F_TSO;
178         else
179                 br->feature_mask &= ~NETIF_F_TSO;
180
181         br_features_recompute(br);
182         return 0;
183 }
184
185 static int br_set_tx_csum(struct net_device *dev, u32 data)
186 {
187         struct net_bridge *br = netdev_priv(dev);
188
189         if (data)
190                 br->feature_mask |= NETIF_F_NO_CSUM;
191         else
192                 br->feature_mask &= ~NETIF_F_ALL_CSUM;
193
194         br_features_recompute(br);
195         return 0;
196 }
197
198 static const struct ethtool_ops br_ethtool_ops = {
199         .get_drvinfo    = br_getinfo,
200         .get_link       = ethtool_op_get_link,
201         .get_tx_csum    = ethtool_op_get_tx_csum,
202         .set_tx_csum    = br_set_tx_csum,
203         .get_sg         = ethtool_op_get_sg,
204         .set_sg         = br_set_sg,
205         .get_tso        = ethtool_op_get_tso,
206         .set_tso        = br_set_tso,
207         .get_ufo        = ethtool_op_get_ufo,
208         .set_ufo        = ethtool_op_set_ufo,
209         .get_flags      = ethtool_op_get_flags,
210 };
211
212 static const struct net_device_ops br_netdev_ops = {
213         .ndo_open                = br_dev_open,
214         .ndo_stop                = br_dev_stop,
215         .ndo_start_xmit          = br_dev_xmit,
216         .ndo_get_stats           = br_get_stats,
217         .ndo_set_mac_address     = br_set_mac_address,
218         .ndo_set_multicast_list  = br_dev_set_multicast_list,
219         .ndo_change_mtu          = br_change_mtu,
220         .ndo_do_ioctl            = br_dev_ioctl,
221 };
222
223 static void br_dev_free(struct net_device *dev)
224 {
225         struct net_bridge *br = netdev_priv(dev);
226
227         free_percpu(br->stats);
228         free_netdev(dev);
229 }
230
231 void br_dev_setup(struct net_device *dev)
232 {
233         random_ether_addr(dev->dev_addr);
234         ether_setup(dev);
235
236         dev->netdev_ops = &br_netdev_ops;
237         dev->destructor = br_dev_free;
238         SET_ETHTOOL_OPS(dev, &br_ethtool_ops);
239         dev->tx_queue_len = 0;
240         dev->priv_flags = IFF_EBRIDGE;
241
242         dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
243                         NETIF_F_GSO_MASK | NETIF_F_NO_CSUM | NETIF_F_LLTX |
244                         NETIF_F_NETNS_LOCAL | NETIF_F_GSO;
245 }