]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/batman-adv/soft-interface.c
Merge branch 'fix/hda' into for-linus
[mv-sheeva.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                 goto dropped;
199
200         skb_pull_rcsum(skb, hdr_size);
201 /*      skb_set_mac_header(skb, -sizeof(struct ethhdr));*/
202
203         /* skb->dev & skb->pkt_type are set here */
204         if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
205                 goto dropped;
206         skb->protocol = eth_type_trans(skb, soft_iface);
207
208         /* should not be neccesary anymore as we use skb_pull_rcsum()
209          * TODO: please verify this and remove this TODO
210          * -- Dec 21st 2009, Simon Wunderlich */
211
212 /*      skb->ip_summed = CHECKSUM_UNNECESSARY;*/
213
214         priv->stats.rx_packets++;
215         priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr);
216
217         soft_iface->last_rx = jiffies;
218
219         netif_rx(skb);
220         return;
221
222 dropped:
223         kfree_skb(skb);
224         return;
225 }
226
227 #ifdef HAVE_NET_DEVICE_OPS
228 static const struct net_device_ops bat_netdev_ops = {
229         .ndo_open = interface_open,
230         .ndo_stop = interface_release,
231         .ndo_get_stats = interface_stats,
232         .ndo_set_mac_address = interface_set_mac_addr,
233         .ndo_change_mtu = interface_change_mtu,
234         .ndo_start_xmit = interface_tx,
235         .ndo_validate_addr = eth_validate_addr
236 };
237 #endif
238
239 static void interface_setup(struct net_device *dev)
240 {
241         struct bat_priv *priv = netdev_priv(dev);
242         char dev_addr[ETH_ALEN];
243
244         ether_setup(dev);
245
246 #ifdef HAVE_NET_DEVICE_OPS
247         dev->netdev_ops = &bat_netdev_ops;
248 #else
249         dev->open = interface_open;
250         dev->stop = interface_release;
251         dev->get_stats = interface_stats;
252         dev->set_mac_address = interface_set_mac_addr;
253         dev->change_mtu = interface_change_mtu;
254         dev->hard_start_xmit = interface_tx;
255 #endif
256         dev->destructor = free_netdev;
257
258         /**
259          * can't call min_mtu, because the needed variables
260          * have not been initialized yet
261          */
262         dev->mtu = ETH_DATA_LEN;
263         dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
264                                                 * skbuff for our header */
265
266         /* generate random address */
267         random_ether_addr(dev_addr);
268         memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
269
270         SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
271
272         memset(priv, 0, sizeof(struct bat_priv));
273 }
274
275 struct net_device *softif_create(char *name)
276 {
277         struct net_device *soft_iface;
278         struct bat_priv *bat_priv;
279         int ret;
280
281         soft_iface = alloc_netdev(sizeof(struct bat_priv) , name,
282                                    interface_setup);
283
284         if (!soft_iface) {
285                 pr_err("Unable to allocate the batman interface: %s\n", name);
286                 goto out;
287         }
288
289         ret = register_netdev(soft_iface);
290         if (ret < 0) {
291                 pr_err("Unable to register the batman interface '%s': %i\n",
292                        name, ret);
293                 goto free_soft_iface;
294         }
295
296         bat_priv = netdev_priv(soft_iface);
297
298         atomic_set(&bat_priv->aggregation_enabled, 1);
299         atomic_set(&bat_priv->bonding_enabled, 0);
300         atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
301         atomic_set(&bat_priv->orig_interval, 1000);
302         atomic_set(&bat_priv->log_level, 0);
303         atomic_set(&bat_priv->frag_enabled, 1);
304         atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
305         atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
306
307         atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
308         atomic_set(&bat_priv->bcast_seqno, 1);
309         atomic_set(&bat_priv->hna_local_changed, 0);
310
311         bat_priv->primary_if = NULL;
312         bat_priv->num_ifaces = 0;
313
314         ret = sysfs_add_meshif(soft_iface);
315         if (ret < 0)
316                 goto unreg_soft_iface;
317
318         ret = debugfs_add_meshif(soft_iface);
319         if (ret < 0)
320                 goto unreg_sysfs;
321
322         ret = mesh_init(soft_iface);
323         if (ret < 0)
324                 goto unreg_debugfs;
325
326         return soft_iface;
327
328 unreg_debugfs:
329         debugfs_del_meshif(soft_iface);
330 unreg_sysfs:
331         sysfs_del_meshif(soft_iface);
332 unreg_soft_iface:
333         unregister_netdev(soft_iface);
334         return NULL;
335
336 free_soft_iface:
337         free_netdev(soft_iface);
338 out:
339         return NULL;
340 }
341
342 void softif_destroy(struct net_device *soft_iface)
343 {
344         debugfs_del_meshif(soft_iface);
345         sysfs_del_meshif(soft_iface);
346         mesh_free(soft_iface);
347         unregister_netdevice(soft_iface);
348 }
349
350 /* ethtool */
351 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
352 {
353         cmd->supported = 0;
354         cmd->advertising = 0;
355         cmd->speed = SPEED_10;
356         cmd->duplex = DUPLEX_FULL;
357         cmd->port = PORT_TP;
358         cmd->phy_address = 0;
359         cmd->transceiver = XCVR_INTERNAL;
360         cmd->autoneg = AUTONEG_DISABLE;
361         cmd->maxtxpkt = 0;
362         cmd->maxrxpkt = 0;
363
364         return 0;
365 }
366
367 static void bat_get_drvinfo(struct net_device *dev,
368                             struct ethtool_drvinfo *info)
369 {
370         strcpy(info->driver, "B.A.T.M.A.N. advanced");
371         strcpy(info->version, SOURCE_VERSION);
372         strcpy(info->fw_version, "N/A");
373         strcpy(info->bus_info, "batman");
374 }
375
376 static u32 bat_get_msglevel(struct net_device *dev)
377 {
378         return -EOPNOTSUPP;
379 }
380
381 static void bat_set_msglevel(struct net_device *dev, u32 value)
382 {
383 }
384
385 static u32 bat_get_link(struct net_device *dev)
386 {
387         return 1;
388 }
389
390 static u32 bat_get_rx_csum(struct net_device *dev)
391 {
392         return 0;
393 }
394
395 static int bat_set_rx_csum(struct net_device *dev, u32 data)
396 {
397         return -EOPNOTSUPP;
398 }