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