Staging: batman-adv: Remove hashdata_compare_cb from hash
[pandora-kernel.git] / drivers / staging / batman-adv / icmp_socket.c
1 /*
2  * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner
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 <linux/debugfs.h>
24 #include <linux/slab.h>
25 #include "icmp_socket.h"
26 #include "send.h"
27 #include "types.h"
28 #include "hash.h"
29 #include "originator.h"
30 #include "hard-interface.h"
31
32
33 static struct socket_client *socket_client_hash[256];
34
35 static void bat_socket_add_packet(struct socket_client *socket_client,
36                                   struct icmp_packet_rr *icmp_packet,
37                                   size_t icmp_len);
38
39 void bat_socket_init(void)
40 {
41         memset(socket_client_hash, 0, sizeof(socket_client_hash));
42 }
43
44 static int bat_socket_open(struct inode *inode, struct file *file)
45 {
46         unsigned int i;
47         struct socket_client *socket_client;
48
49         nonseekable_open(inode, file);
50
51         socket_client = kmalloc(sizeof(struct socket_client), GFP_KERNEL);
52
53         if (!socket_client)
54                 return -ENOMEM;
55
56         for (i = 0; i < ARRAY_SIZE(socket_client_hash); i++) {
57                 if (!socket_client_hash[i]) {
58                         socket_client_hash[i] = socket_client;
59                         break;
60                 }
61         }
62
63         if (i == ARRAY_SIZE(socket_client_hash)) {
64                 pr_err("Error - can't add another packet client: "
65                        "maximum number of clients reached\n");
66                 kfree(socket_client);
67                 return -EXFULL;
68         }
69
70         INIT_LIST_HEAD(&socket_client->queue_list);
71         socket_client->queue_len = 0;
72         socket_client->index = i;
73         socket_client->bat_priv = inode->i_private;
74         spin_lock_init(&socket_client->lock);
75         init_waitqueue_head(&socket_client->queue_wait);
76
77         file->private_data = socket_client;
78
79         inc_module_count();
80         return 0;
81 }
82
83 static int bat_socket_release(struct inode *inode, struct file *file)
84 {
85         struct socket_client *socket_client = file->private_data;
86         struct socket_packet *socket_packet;
87         struct list_head *list_pos, *list_pos_tmp;
88         unsigned long flags;
89
90         spin_lock_irqsave(&socket_client->lock, flags);
91
92         /* for all packets in the queue ... */
93         list_for_each_safe(list_pos, list_pos_tmp, &socket_client->queue_list) {
94                 socket_packet = list_entry(list_pos,
95                                            struct socket_packet, list);
96
97                 list_del(list_pos);
98                 kfree(socket_packet);
99         }
100
101         socket_client_hash[socket_client->index] = NULL;
102         spin_unlock_irqrestore(&socket_client->lock, flags);
103
104         kfree(socket_client);
105         dec_module_count();
106
107         return 0;
108 }
109
110 static ssize_t bat_socket_read(struct file *file, char __user *buf,
111                                size_t count, loff_t *ppos)
112 {
113         struct socket_client *socket_client = file->private_data;
114         struct socket_packet *socket_packet;
115         size_t packet_len;
116         int error;
117         unsigned long flags;
118
119         if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0))
120                 return -EAGAIN;
121
122         if ((!buf) || (count < sizeof(struct icmp_packet)))
123                 return -EINVAL;
124
125         if (!access_ok(VERIFY_WRITE, buf, count))
126                 return -EFAULT;
127
128         error = wait_event_interruptible(socket_client->queue_wait,
129                                          socket_client->queue_len);
130
131         if (error)
132                 return error;
133
134         spin_lock_irqsave(&socket_client->lock, flags);
135
136         socket_packet = list_first_entry(&socket_client->queue_list,
137                                          struct socket_packet, list);
138         list_del(&socket_packet->list);
139         socket_client->queue_len--;
140
141         spin_unlock_irqrestore(&socket_client->lock, flags);
142
143         error = __copy_to_user(buf, &socket_packet->icmp_packet,
144                                socket_packet->icmp_len);
145
146         packet_len = socket_packet->icmp_len;
147         kfree(socket_packet);
148
149         if (error)
150                 return -EFAULT;
151
152         return packet_len;
153 }
154
155 static ssize_t bat_socket_write(struct file *file, const char __user *buff,
156                                 size_t len, loff_t *off)
157 {
158         struct socket_client *socket_client = file->private_data;
159         struct bat_priv *bat_priv = socket_client->bat_priv;
160         struct sk_buff *skb;
161         struct icmp_packet_rr *icmp_packet;
162
163         struct orig_node *orig_node;
164         struct batman_if *batman_if;
165         size_t packet_len = sizeof(struct icmp_packet);
166         uint8_t dstaddr[ETH_ALEN];
167         unsigned long flags;
168
169         if (len < sizeof(struct icmp_packet)) {
170                 bat_dbg(DBG_BATMAN, bat_priv,
171                         "Error - can't send packet from char device: "
172                         "invalid packet size\n");
173                 return -EINVAL;
174         }
175
176         if (!bat_priv->primary_if)
177                 return -EFAULT;
178
179         if (len >= sizeof(struct icmp_packet_rr))
180                 packet_len = sizeof(struct icmp_packet_rr);
181
182         skb = dev_alloc_skb(packet_len + sizeof(struct ethhdr));
183         if (!skb)
184                 return -ENOMEM;
185
186         skb_reserve(skb, sizeof(struct ethhdr));
187         icmp_packet = (struct icmp_packet_rr *)skb_put(skb, packet_len);
188
189         if (!access_ok(VERIFY_READ, buff, packet_len)) {
190                 len = -EFAULT;
191                 goto free_skb;
192         }
193
194         if (__copy_from_user(icmp_packet, buff, packet_len)) {
195                 len = -EFAULT;
196                 goto free_skb;
197         }
198
199         if (icmp_packet->packet_type != BAT_ICMP) {
200                 bat_dbg(DBG_BATMAN, bat_priv,
201                         "Error - can't send packet from char device: "
202                         "got bogus packet type (expected: BAT_ICMP)\n");
203                 len = -EINVAL;
204                 goto free_skb;
205         }
206
207         if (icmp_packet->msg_type != ECHO_REQUEST) {
208                 bat_dbg(DBG_BATMAN, bat_priv,
209                         "Error - can't send packet from char device: "
210                         "got bogus message type (expected: ECHO_REQUEST)\n");
211                 len = -EINVAL;
212                 goto free_skb;
213         }
214
215         icmp_packet->uid = socket_client->index;
216
217         if (icmp_packet->version != COMPAT_VERSION) {
218                 icmp_packet->msg_type = PARAMETER_PROBLEM;
219                 icmp_packet->ttl = COMPAT_VERSION;
220                 bat_socket_add_packet(socket_client, icmp_packet, packet_len);
221                 goto free_skb;
222         }
223
224         if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
225                 goto dst_unreach;
226
227         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
228         orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
229                                                    compare_orig,
230                                                    icmp_packet->dst));
231
232         if (!orig_node)
233                 goto unlock;
234
235         if (!orig_node->router)
236                 goto unlock;
237
238         batman_if = orig_node->router->if_incoming;
239         memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
240
241         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
242
243         if (!batman_if)
244                 goto dst_unreach;
245
246         if (batman_if->if_status != IF_ACTIVE)
247                 goto dst_unreach;
248
249         memcpy(icmp_packet->orig,
250                bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
251
252         if (packet_len == sizeof(struct icmp_packet_rr))
253                 memcpy(icmp_packet->rr, batman_if->net_dev->dev_addr, ETH_ALEN);
254
255
256         send_skb_packet(skb, batman_if, dstaddr);
257
258         goto out;
259
260 unlock:
261         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
262 dst_unreach:
263         icmp_packet->msg_type = DESTINATION_UNREACHABLE;
264         bat_socket_add_packet(socket_client, icmp_packet, packet_len);
265 free_skb:
266         kfree_skb(skb);
267 out:
268         return len;
269 }
270
271 static unsigned int bat_socket_poll(struct file *file, poll_table *wait)
272 {
273         struct socket_client *socket_client = file->private_data;
274
275         poll_wait(file, &socket_client->queue_wait, wait);
276
277         if (socket_client->queue_len > 0)
278                 return POLLIN | POLLRDNORM;
279
280         return 0;
281 }
282
283 static const struct file_operations fops = {
284         .owner = THIS_MODULE,
285         .open = bat_socket_open,
286         .release = bat_socket_release,
287         .read = bat_socket_read,
288         .write = bat_socket_write,
289         .poll = bat_socket_poll,
290         .llseek = no_llseek,
291 };
292
293 int bat_socket_setup(struct bat_priv *bat_priv)
294 {
295         struct dentry *d;
296
297         if (!bat_priv->debug_dir)
298                 goto err;
299
300         d = debugfs_create_file(ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
301                                 bat_priv->debug_dir, bat_priv, &fops);
302         if (d)
303                 goto err;
304
305         return 0;
306
307 err:
308         return 1;
309 }
310
311 static void bat_socket_add_packet(struct socket_client *socket_client,
312                                   struct icmp_packet_rr *icmp_packet,
313                                   size_t icmp_len)
314 {
315         struct socket_packet *socket_packet;
316         unsigned long flags;
317
318         socket_packet = kmalloc(sizeof(struct socket_packet), GFP_ATOMIC);
319
320         if (!socket_packet)
321                 return;
322
323         INIT_LIST_HEAD(&socket_packet->list);
324         memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
325         socket_packet->icmp_len = icmp_len;
326
327         spin_lock_irqsave(&socket_client->lock, flags);
328
329         /* while waiting for the lock the socket_client could have been
330          * deleted */
331         if (!socket_client_hash[icmp_packet->uid]) {
332                 spin_unlock_irqrestore(&socket_client->lock, flags);
333                 kfree(socket_packet);
334                 return;
335         }
336
337         list_add_tail(&socket_packet->list, &socket_client->queue_list);
338         socket_client->queue_len++;
339
340         if (socket_client->queue_len > 100) {
341                 socket_packet = list_first_entry(&socket_client->queue_list,
342                                                  struct socket_packet, list);
343
344                 list_del(&socket_packet->list);
345                 kfree(socket_packet);
346                 socket_client->queue_len--;
347         }
348
349         spin_unlock_irqrestore(&socket_client->lock, flags);
350
351         wake_up(&socket_client->queue_wait);
352 }
353
354 void bat_socket_receive_packet(struct icmp_packet_rr *icmp_packet,
355                                size_t icmp_len)
356 {
357         struct socket_client *hash = socket_client_hash[icmp_packet->uid];
358
359         if (hash)
360                 bat_socket_add_packet(hash, icmp_packet, icmp_len);
361 }