staging: batman-adv: Use linux/etherdevice.h address helper functions
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / batman-adv / soft-interface.c
blob820e14159dd3a40c287fc953b58036819c3e3b2e
1 /*
2 * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
22 #include "main.h"
23 #include "soft-interface.h"
24 #include "hard-interface.h"
25 #include "routing.h"
26 #include "send.h"
27 #include "bat_debugfs.h"
28 #include "translation-table.h"
29 #include "types.h"
30 #include "hash.h"
31 #include "send.h"
32 #include "bat_sysfs.h"
33 #include <linux/slab.h>
34 #include <linux/ethtool.h>
35 #include <linux/etherdevice.h>
36 #include "unicast.h"
39 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
40 static void bat_get_drvinfo(struct net_device *dev,
41 struct ethtool_drvinfo *info);
42 static u32 bat_get_msglevel(struct net_device *dev);
43 static void bat_set_msglevel(struct net_device *dev, u32 value);
44 static u32 bat_get_link(struct net_device *dev);
45 static u32 bat_get_rx_csum(struct net_device *dev);
46 static int bat_set_rx_csum(struct net_device *dev, u32 data);
48 static const struct ethtool_ops bat_ethtool_ops = {
49 .get_settings = bat_get_settings,
50 .get_drvinfo = bat_get_drvinfo,
51 .get_msglevel = bat_get_msglevel,
52 .set_msglevel = bat_set_msglevel,
53 .get_link = bat_get_link,
54 .get_rx_csum = bat_get_rx_csum,
55 .set_rx_csum = bat_set_rx_csum
58 int my_skb_head_push(struct sk_buff *skb, unsigned int len)
60 int result;
62 /**
63 * TODO: We must check if we can release all references to non-payload
64 * data using skb_header_release in our skbs to allow skb_cow_header to
65 * work optimally. This means that those skbs are not allowed to read
66 * or write any data which is before the current position of skb->data
67 * after that call and thus allow other skbs with the same data buffer
68 * to write freely in that area.
70 result = skb_cow_head(skb, len);
71 if (result < 0)
72 return result;
74 skb_push(skb, len);
75 return 0;
78 static int interface_open(struct net_device *dev)
80 netif_start_queue(dev);
81 return 0;
84 static int interface_release(struct net_device *dev)
86 netif_stop_queue(dev);
87 return 0;
90 static struct net_device_stats *interface_stats(struct net_device *dev)
92 struct bat_priv *bat_priv = netdev_priv(dev);
93 return &bat_priv->stats;
96 static int interface_set_mac_addr(struct net_device *dev, void *p)
98 struct bat_priv *bat_priv = netdev_priv(dev);
99 struct sockaddr *addr = p;
101 if (!is_valid_ether_addr(addr->sa_data))
102 return -EADDRNOTAVAIL;
104 /* only modify hna-table if it has been initialised before */
105 if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
106 hna_local_remove(bat_priv, dev->dev_addr,
107 "mac address changed");
108 hna_local_add(dev, addr->sa_data);
111 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
113 return 0;
116 static int interface_change_mtu(struct net_device *dev, int new_mtu)
118 /* check ranges */
119 if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev)))
120 return -EINVAL;
122 dev->mtu = new_mtu;
124 return 0;
127 int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
129 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
130 struct bat_priv *bat_priv = netdev_priv(soft_iface);
131 struct bcast_packet *bcast_packet;
132 int data_len = skb->len, ret;
134 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
135 goto dropped;
137 soft_iface->trans_start = jiffies;
139 /* TODO: check this for locks */
140 hna_local_add(soft_iface, ethhdr->h_source);
142 /* ethernet packet should be broadcasted */
143 if (is_multicast_ether_addr(ethhdr->h_dest)) {
144 if (!bat_priv->primary_if)
145 goto dropped;
147 if (my_skb_head_push(skb, sizeof(struct bcast_packet)) < 0)
148 goto dropped;
150 bcast_packet = (struct bcast_packet *)skb->data;
151 bcast_packet->version = COMPAT_VERSION;
152 bcast_packet->ttl = TTL;
154 /* batman packet type: broadcast */
155 bcast_packet->packet_type = BAT_BCAST;
157 /* hw address of first interface is the orig mac because only
158 * this mac is known throughout the mesh */
159 memcpy(bcast_packet->orig,
160 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
162 /* set broadcast sequence number */
163 bcast_packet->seqno =
164 htonl(atomic_inc_return(&bat_priv->bcast_seqno));
166 add_bcast_packet_to_list(bat_priv, skb);
168 /* a copy is stored in the bcast list, therefore removing
169 * the original skb. */
170 kfree_skb(skb);
172 /* unicast packet */
173 } else {
174 ret = unicast_send_skb(skb, bat_priv);
175 if (ret != 0)
176 goto dropped_freed;
179 bat_priv->stats.tx_packets++;
180 bat_priv->stats.tx_bytes += data_len;
181 goto end;
183 dropped:
184 kfree_skb(skb);
185 dropped_freed:
186 bat_priv->stats.tx_dropped++;
187 end:
188 return NETDEV_TX_OK;
191 void interface_rx(struct net_device *soft_iface,
192 struct sk_buff *skb, int hdr_size)
194 struct bat_priv *priv = netdev_priv(soft_iface);
196 /* check if enough space is available for pulling, and pull */
197 if (!pskb_may_pull(skb, hdr_size)) {
198 kfree_skb(skb);
199 return;
201 skb_pull_rcsum(skb, hdr_size);
202 /* skb_set_mac_header(skb, -sizeof(struct ethhdr));*/
204 /* skb->dev & skb->pkt_type are set here */
205 skb->protocol = eth_type_trans(skb, soft_iface);
207 /* should not be neccesary anymore as we use skb_pull_rcsum()
208 * TODO: please verify this and remove this TODO
209 * -- Dec 21st 2009, Simon Wunderlich */
211 /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
213 priv->stats.rx_packets++;
214 priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr);
216 soft_iface->last_rx = jiffies;
218 netif_rx(skb);
221 #ifdef HAVE_NET_DEVICE_OPS
222 static const struct net_device_ops bat_netdev_ops = {
223 .ndo_open = interface_open,
224 .ndo_stop = interface_release,
225 .ndo_get_stats = interface_stats,
226 .ndo_set_mac_address = interface_set_mac_addr,
227 .ndo_change_mtu = interface_change_mtu,
228 .ndo_start_xmit = interface_tx,
229 .ndo_validate_addr = eth_validate_addr
231 #endif
233 static void interface_setup(struct net_device *dev)
235 struct bat_priv *priv = netdev_priv(dev);
236 char dev_addr[ETH_ALEN];
238 ether_setup(dev);
240 #ifdef HAVE_NET_DEVICE_OPS
241 dev->netdev_ops = &bat_netdev_ops;
242 #else
243 dev->open = interface_open;
244 dev->stop = interface_release;
245 dev->get_stats = interface_stats;
246 dev->set_mac_address = interface_set_mac_addr;
247 dev->change_mtu = interface_change_mtu;
248 dev->hard_start_xmit = interface_tx;
249 #endif
250 dev->destructor = free_netdev;
253 * can't call min_mtu, because the needed variables
254 * have not been initialized yet
256 dev->mtu = ETH_DATA_LEN;
257 dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
258 * skbuff for our header */
260 /* generate random address */
261 random_ether_addr(dev_addr);
262 memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
264 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
266 memset(priv, 0, sizeof(struct bat_priv));
269 struct net_device *softif_create(char *name)
271 struct net_device *soft_iface;
272 struct bat_priv *bat_priv;
273 int ret;
275 soft_iface = alloc_netdev(sizeof(struct bat_priv) , name,
276 interface_setup);
278 if (!soft_iface) {
279 pr_err("Unable to allocate the batman interface: %s\n", name);
280 goto out;
283 ret = register_netdev(soft_iface);
284 if (ret < 0) {
285 pr_err("Unable to register the batman interface '%s': %i\n",
286 name, ret);
287 goto free_soft_iface;
290 bat_priv = netdev_priv(soft_iface);
292 atomic_set(&bat_priv->aggregation_enabled, 1);
293 atomic_set(&bat_priv->bonding_enabled, 0);
294 atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
295 atomic_set(&bat_priv->orig_interval, 1000);
296 atomic_set(&bat_priv->log_level, 0);
297 atomic_set(&bat_priv->frag_enabled, 1);
298 atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
299 atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
301 atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
302 atomic_set(&bat_priv->bcast_seqno, 1);
303 atomic_set(&bat_priv->hna_local_changed, 0);
305 bat_priv->primary_if = NULL;
306 bat_priv->num_ifaces = 0;
308 ret = sysfs_add_meshif(soft_iface);
309 if (ret < 0)
310 goto unreg_soft_iface;
312 ret = debugfs_add_meshif(soft_iface);
313 if (ret < 0)
314 goto unreg_sysfs;
316 ret = mesh_init(soft_iface);
317 if (ret < 0)
318 goto unreg_debugfs;
320 return soft_iface;
322 unreg_debugfs:
323 debugfs_del_meshif(soft_iface);
324 unreg_sysfs:
325 sysfs_del_meshif(soft_iface);
326 unreg_soft_iface:
327 unregister_netdev(soft_iface);
328 return NULL;
330 free_soft_iface:
331 free_netdev(soft_iface);
332 out:
333 return NULL;
336 void softif_destroy(struct net_device *soft_iface)
338 debugfs_del_meshif(soft_iface);
339 sysfs_del_meshif(soft_iface);
340 mesh_free(soft_iface);
341 unregister_netdevice(soft_iface);
344 /* ethtool */
345 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
347 cmd->supported = 0;
348 cmd->advertising = 0;
349 cmd->speed = SPEED_10;
350 cmd->duplex = DUPLEX_FULL;
351 cmd->port = PORT_TP;
352 cmd->phy_address = 0;
353 cmd->transceiver = XCVR_INTERNAL;
354 cmd->autoneg = AUTONEG_DISABLE;
355 cmd->maxtxpkt = 0;
356 cmd->maxrxpkt = 0;
358 return 0;
361 static void bat_get_drvinfo(struct net_device *dev,
362 struct ethtool_drvinfo *info)
364 strcpy(info->driver, "B.A.T.M.A.N. advanced");
365 strcpy(info->version, SOURCE_VERSION);
366 strcpy(info->fw_version, "N/A");
367 strcpy(info->bus_info, "batman");
370 static u32 bat_get_msglevel(struct net_device *dev)
372 return -EOPNOTSUPP;
375 static void bat_set_msglevel(struct net_device *dev, u32 value)
379 static u32 bat_get_link(struct net_device *dev)
381 return 1;
384 static u32 bat_get_rx_csum(struct net_device *dev)
386 return 0;
389 static int bat_set_rx_csum(struct net_device *dev, u32 data)
391 return -EOPNOTSUPP;