- patches.suse/slab-handle-memoryless-nodes-v2a.patch: Refresh.
[linux-flexiantxendom0-3.2.10.git] / drivers / staging / batman-adv / soft-interface.c
1 /*
2  * Copyright (C) 2007-2009 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 "send.h"
26 #include "translation-table.h"
27 #include "log.h"
28 #include "types.h"
29 #include "hash.h"
30 #include <linux/ethtool.h>
31 #include <linux/etherdevice.h>
32 #include "compat.h"
33
34 static uint16_t bcast_seqno = 1; /* give own bcast messages seq numbers to avoid
35                                   * broadcast storms */
36 static int32_t skb_packets;
37 static int32_t skb_bad_packets;
38 static int32_t lock_dropped;
39
40 unsigned char mainIfAddr[ETH_ALEN];
41 static unsigned char mainIfAddr_default[ETH_ALEN];
42 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
43 static void bat_get_drvinfo(struct net_device *dev,
44                             struct ethtool_drvinfo *info);
45 static u32 bat_get_msglevel(struct net_device *dev);
46 static void bat_set_msglevel(struct net_device *dev, u32 value);
47 static u32 bat_get_link(struct net_device *dev);
48 static u32 bat_get_rx_csum(struct net_device *dev);
49 static int bat_set_rx_csum(struct net_device *dev, u32 data);
50
51 static const struct ethtool_ops bat_ethtool_ops = {
52         .get_settings = bat_get_settings,
53         .get_drvinfo = bat_get_drvinfo,
54         .get_msglevel = bat_get_msglevel,
55         .set_msglevel = bat_set_msglevel,
56         .get_link = bat_get_link,
57         .get_rx_csum = bat_get_rx_csum,
58         .set_rx_csum = bat_set_rx_csum
59 };
60
61 void set_main_if_addr(uint8_t *addr)
62 {
63         memcpy(mainIfAddr, addr, ETH_ALEN);
64 }
65
66 int main_if_was_up(void)
67 {
68         return (memcmp(mainIfAddr, mainIfAddr_default, ETH_ALEN) != 0 ? 1 : 0);
69 }
70
71 static int my_skb_push(struct sk_buff *skb, unsigned int len)
72 {
73         int result = 0;
74
75         skb_packets++;
76         if (skb->data - len < skb->head) {
77                 skb_bad_packets++;
78                 result = pskb_expand_head(skb, len, 0, GFP_ATOMIC);
79
80                 if (result < 0)
81                         return result;
82         }
83
84         skb_push(skb, len);
85         return 0;
86 }
87
88 #ifdef HAVE_NET_DEVICE_OPS
89 static const struct net_device_ops bat_netdev_ops = {
90         .ndo_open = interface_open,
91         .ndo_stop = interface_release,
92         .ndo_get_stats = interface_stats,
93         .ndo_set_mac_address = interface_set_mac_addr,
94         .ndo_change_mtu = interface_change_mtu,
95         .ndo_start_xmit = interface_tx,
96         .ndo_validate_addr = eth_validate_addr
97 };
98 #endif
99
100 void interface_setup(struct net_device *dev)
101 {
102         struct bat_priv *priv = netdev_priv(dev);
103         char dev_addr[ETH_ALEN];
104
105         ether_setup(dev);
106
107 #ifdef HAVE_NET_DEVICE_OPS
108         dev->netdev_ops = &bat_netdev_ops;
109 #else
110         dev->open = interface_open;
111         dev->stop = interface_release;
112         dev->get_stats = interface_stats;
113         dev->set_mac_address = interface_set_mac_addr;
114         dev->change_mtu = interface_change_mtu;
115         dev->hard_start_xmit = interface_tx;
116 #endif
117         dev->destructor = free_netdev;
118
119         dev->mtu = hardif_min_mtu();
120         dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
121                                                 * skbuff for our header */
122
123         /* generate random address */
124         random_ether_addr(dev_addr);
125         memcpy(dev->dev_addr, dev_addr, sizeof(dev->dev_addr));
126
127         SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
128
129         memset(priv, 0, sizeof(struct bat_priv));
130 }
131
132 int interface_open(struct net_device *dev)
133 {
134         netif_start_queue(dev);
135         return 0;
136 }
137
138 int interface_release(struct net_device *dev)
139 {
140         netif_stop_queue(dev);
141         return 0;
142 }
143
144 struct net_device_stats *interface_stats(struct net_device *dev)
145 {
146         struct bat_priv *priv = netdev_priv(dev);
147         return &priv->stats;
148 }
149
150 int interface_set_mac_addr(struct net_device *dev, void *addr)
151 {
152         return -EBUSY;
153 }
154
155 int interface_change_mtu(struct net_device *dev, int new_mtu)
156 {
157         /* check ranges */
158         if ((new_mtu < 68) || (new_mtu > hardif_min_mtu()))
159                 return -EINVAL;
160
161         dev->mtu = new_mtu;
162
163         return 0;
164 }
165
166 int interface_tx(struct sk_buff *skb, struct net_device *dev)
167 {
168         struct unicast_packet *unicast_packet;
169         struct bcast_packet *bcast_packet;
170         struct orig_node *orig_node;
171         struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
172         struct bat_priv *priv = netdev_priv(dev);
173         int data_len = skb->len;
174
175         if (atomic_read(&module_state) != MODULE_ACTIVE)
176                 goto dropped;
177
178         dev->trans_start = jiffies;
179         /* TODO: check this for locks */
180         hna_local_add(ethhdr->h_source);
181
182         /* ethernet packet should be broadcasted */
183         if (is_bcast(ethhdr->h_dest) || is_mcast(ethhdr->h_dest)) {
184
185                 if (my_skb_push(skb, sizeof(struct bcast_packet)) < 0)
186                         goto dropped;
187
188                 bcast_packet = (struct bcast_packet *)skb->data;
189
190                 bcast_packet->version = COMPAT_VERSION;
191
192                 /* batman packet type: broadcast */
193                 bcast_packet->packet_type = BAT_BCAST;
194
195                 /* hw address of first interface is the orig mac because only
196                  * this mac is known throughout the mesh */
197                 memcpy(bcast_packet->orig, mainIfAddr, ETH_ALEN);
198                 /* set broadcast sequence number */
199                 bcast_packet->seqno = htons(bcast_seqno);
200
201                 bcast_seqno++;
202
203                 /* broadcast packet */
204                 add_bcast_packet_to_list(skb->data, skb->len);
205
206         /* unicast packet */
207         } else {
208
209                 /* simply spin_lock()ing can deadlock when the lock is already
210                  * hold. */
211                 /* TODO: defer the work in a working queue instead of
212                  * dropping */
213                 if (!spin_trylock(&orig_hash_lock)) {
214                         lock_dropped++;
215                         debug_log(LOG_TYPE_NOTICE, "%d packets dropped because lock was hold\n", lock_dropped);
216                         goto dropped;
217                 }
218
219                 /* get routing information */
220                 orig_node = ((struct orig_node *)hash_find(orig_hash,
221                                                            ethhdr->h_dest));
222
223                 /* check for hna host */
224                 if (!orig_node)
225                         orig_node = transtable_search(ethhdr->h_dest);
226
227                 if ((orig_node) &&
228                     (orig_node->batman_if) &&
229                     (orig_node->router)) {
230                         if (my_skb_push(skb, sizeof(struct unicast_packet)) < 0)
231                                 goto unlock;
232
233                         unicast_packet = (struct unicast_packet *)skb->data;
234
235                         unicast_packet->version = COMPAT_VERSION;
236                         /* batman packet type: unicast */
237                         unicast_packet->packet_type = BAT_UNICAST;
238                         /* set unicast ttl */
239                         unicast_packet->ttl = TTL;
240                         /* copy the destination for faster routing */
241                         memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
242
243                         /* net_dev won't be available when not active */
244                         if (orig_node->batman_if->if_active != IF_ACTIVE)
245                                 goto unlock;
246
247                         send_raw_packet(skb->data, skb->len,
248                                         orig_node->batman_if,
249                                         orig_node->router->addr);
250                 } else {
251                         goto unlock;
252                 }
253
254                 spin_unlock(&orig_hash_lock);
255         }
256
257         priv->stats.tx_packets++;
258         priv->stats.tx_bytes += data_len;
259         goto end;
260
261 unlock:
262         spin_unlock(&orig_hash_lock);
263 dropped:
264         priv->stats.tx_dropped++;
265 end:
266         kfree_skb(skb);
267         return 0;
268 }
269
270 void interface_rx(struct net_device *dev, void *packet, int packet_len)
271 {
272         struct sk_buff *skb;
273         struct bat_priv *priv = netdev_priv(dev);
274
275         skb = dev_alloc_skb(packet_len);
276
277         if (!skb) {
278                 priv->stats.rx_dropped++;
279                 goto out;
280         }
281
282         memcpy(skb_put(skb, packet_len), packet, packet_len);
283
284         /* Write metadata, and then pass to the receive level */
285         skb->dev = dev;
286         skb->protocol = eth_type_trans(skb, dev);
287         skb->ip_summed = CHECKSUM_UNNECESSARY;
288
289         priv->stats.rx_packets++;
290         priv->stats.rx_bytes += packet_len;
291
292         dev->last_rx = jiffies;
293
294         netif_rx(skb);
295
296 out:
297         return;
298 }
299
300 /* ethtool */
301 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
302 {
303         cmd->supported = 0;
304         cmd->advertising = 0;
305         cmd->speed = SPEED_10;
306         cmd->duplex = DUPLEX_FULL;
307         cmd->port = PORT_TP;
308         cmd->phy_address = 0;
309         cmd->transceiver = XCVR_INTERNAL;
310         cmd->autoneg = AUTONEG_DISABLE;
311         cmd->maxtxpkt = 0;
312         cmd->maxrxpkt = 0;
313
314         return 0;
315 }
316
317 static void bat_get_drvinfo(struct net_device *dev,
318                             struct ethtool_drvinfo *info)
319 {
320         strcpy(info->driver, "B.A.T.M.A.N. advanced");
321         strcpy(info->version, SOURCE_VERSION);
322         strcpy(info->fw_version, "N/A");
323         strcpy(info->bus_info, "batman");
324 }
325
326 static u32 bat_get_msglevel(struct net_device *dev)
327 {
328         return -EOPNOTSUPP;
329 }
330
331 static void bat_set_msglevel(struct net_device *dev, u32 value)
332 {
333         return;
334 }
335
336 static u32 bat_get_link(struct net_device *dev)
337 {
338         return 1;
339 }
340
341 static u32 bat_get_rx_csum(struct net_device *dev)
342 {
343         return 0;
344 }
345
346 static int bat_set_rx_csum(struct net_device *dev, u32 data)
347 {
348         return -EOPNOTSUPP;
349 }