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