Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging-2.6
[pandora-kernel.git] / drivers / staging / batman-adv / soft-interface.c
1 /*
2  * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich
5  *
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.
9  *
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.
14  *
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
19  *
20  */
21
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"
37
38
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);
47
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
56 };
57
58 int my_skb_head_push(struct sk_buff *skb, unsigned int len)
59 {
60         int result;
61
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.
69          */
70         result = skb_cow_head(skb, len);
71         if (result < 0)
72                 return result;
73
74         skb_push(skb, len);
75         return 0;
76 }
77
78 static int interface_open(struct net_device *dev)
79 {
80         netif_start_queue(dev);
81         return 0;
82 }
83
84 static int interface_release(struct net_device *dev)
85 {
86         netif_stop_queue(dev);
87         return 0;
88 }
89
90 static struct net_device_stats *interface_stats(struct net_device *dev)
91 {
92         struct bat_priv *bat_priv = netdev_priv(dev);
93         return &bat_priv->stats;
94 }
95
96 static int interface_set_mac_addr(struct net_device *dev, void *p)
97 {
98         struct bat_priv *bat_priv = netdev_priv(dev);
99         struct sockaddr *addr = p;
100
101         if (!is_valid_ether_addr(addr->sa_data))
102                 return -EADDRNOTAVAIL;
103
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);
109         }
110
111         memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
112
113         return 0;
114 }
115
116 static int interface_change_mtu(struct net_device *dev, int new_mtu)
117 {
118         /* check ranges */
119         if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev)))
120                 return -EINVAL;
121
122         dev->mtu = new_mtu;
123
124         return 0;
125 }
126
127 int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
128 {
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;
133
134         if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
135                 goto dropped;
136
137         soft_iface->trans_start = jiffies;
138
139         /* TODO: check this for locks */
140         hna_local_add(soft_iface, ethhdr->h_source);
141
142         /* ethernet packet should be broadcasted */
143         if (is_bcast(ethhdr->h_dest) || is_mcast(ethhdr->h_dest)) {
144                 if (!bat_priv->primary_if)
145                         goto dropped;
146
147                 if (my_skb_head_push(skb, sizeof(struct bcast_packet)) < 0)
148                         goto dropped;
149
150                 bcast_packet = (struct bcast_packet *)skb->data;
151                 bcast_packet->version = COMPAT_VERSION;
152                 bcast_packet->ttl = TTL;
153
154                 /* batman packet type: broadcast */
155                 bcast_packet->packet_type = BAT_BCAST;
156
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);
161
162                 /* set broadcast sequence number */
163                 bcast_packet->seqno =
164                         htonl(atomic_inc_return(&bat_priv->bcast_seqno));
165
166                 add_bcast_packet_to_list(bat_priv, skb);
167
168                 /* a copy is stored in the bcast list, therefore removing
169                  * the original skb. */
170                 kfree_skb(skb);
171
172         /* unicast packet */
173         } else {
174                 ret = unicast_send_skb(skb, bat_priv);
175                 if (ret != 0)
176                         goto dropped_freed;
177         }
178
179         bat_priv->stats.tx_packets++;
180         bat_priv->stats.tx_bytes += data_len;
181         goto end;
182
183 dropped:
184         kfree_skb(skb);
185 dropped_freed:
186         bat_priv->stats.tx_dropped++;
187 end:
188         return NETDEV_TX_OK;
189 }
190
191 void interface_rx(struct net_device *soft_iface,
192                   struct sk_buff *skb, int hdr_size)
193 {
194         struct bat_priv *priv = netdev_priv(soft_iface);
195
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;
200         }
201         skb_pull_rcsum(skb, hdr_size);
202 /*      skb_set_mac_header(skb, -sizeof(struct ethhdr));*/
203
204         /* skb->dev & skb->pkt_type are set here */
205         skb->protocol = eth_type_trans(skb, soft_iface);
206
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 */
210
211 /*      skb->ip_summed = CHECKSUM_UNNECESSARY;*/
212
213         priv->stats.rx_packets++;
214         priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr);
215
216         soft_iface->last_rx = jiffies;
217
218         netif_rx(skb);
219 }
220
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
230 };
231 #endif
232
233 static void interface_setup(struct net_device *dev)
234 {
235         struct bat_priv *priv = netdev_priv(dev);
236         char dev_addr[ETH_ALEN];
237
238         ether_setup(dev);
239
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;
251
252         /**
253          * can't call min_mtu, because the needed variables
254          * have not been initialized yet
255          */
256         dev->mtu = ETH_DATA_LEN;
257         dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
258                                                 * skbuff for our header */
259
260         /* generate random address */
261         random_ether_addr(dev_addr);
262         memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
263
264         SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
265
266         memset(priv, 0, sizeof(struct bat_priv));
267 }
268
269 struct net_device *softif_create(char *name)
270 {
271         struct net_device *soft_iface;
272         struct bat_priv *bat_priv;
273         int ret;
274
275         soft_iface = alloc_netdev(sizeof(struct bat_priv) , name,
276                                    interface_setup);
277
278         if (!soft_iface) {
279                 pr_err("Unable to allocate the batman interface: %s\n", name);
280                 goto out;
281         }
282
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;
288         }
289
290         bat_priv = netdev_priv(soft_iface);
291
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);
300
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);
304
305         bat_priv->primary_if = NULL;
306         bat_priv->num_ifaces = 0;
307
308         ret = sysfs_add_meshif(soft_iface);
309         if (ret < 0)
310                 goto unreg_soft_iface;
311
312         ret = debugfs_add_meshif(soft_iface);
313         if (ret < 0)
314                 goto unreg_sysfs;
315
316         ret = mesh_init(soft_iface);
317         if (ret < 0)
318                 goto unreg_debugfs;
319
320         return soft_iface;
321
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;
329
330 free_soft_iface:
331         free_netdev(soft_iface);
332 out:
333         return NULL;
334 }
335
336 void softif_destroy(struct net_device *soft_iface)
337 {
338         debugfs_del_meshif(soft_iface);
339         sysfs_del_meshif(soft_iface);
340         mesh_free(soft_iface);
341         unregister_netdevice(soft_iface);
342 }
343
344 /* ethtool */
345 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
346 {
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;
357
358         return 0;
359 }
360
361 static void bat_get_drvinfo(struct net_device *dev,
362                             struct ethtool_drvinfo *info)
363 {
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");
368 }
369
370 static u32 bat_get_msglevel(struct net_device *dev)
371 {
372         return -EOPNOTSUPP;
373 }
374
375 static void bat_set_msglevel(struct net_device *dev, u32 value)
376 {
377 }
378
379 static u32 bat_get_link(struct net_device *dev)
380 {
381         return 1;
382 }
383
384 static u32 bat_get_rx_csum(struct net_device *dev)
385 {
386         return 0;
387 }
388
389 static int bat_set_rx_csum(struct net_device *dev, u32 data)
390 {
391         return -EOPNOTSUPP;
392 }