net/mlx4_en: Fix mixed PFC and Global pause user control requests
[pandora-kernel.git] / net / batman-adv / icmp_socket.c
1 /*
2  * Copyright (C) 2007-2011 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 "hash.h"
28 #include "originator.h"
29 #include "hard-interface.h"
30
31 static struct socket_client *socket_client_hash[256];
32
33 static void bat_socket_add_packet(struct socket_client *socket_client,
34                                   struct icmp_packet_rr *icmp_packet,
35                                   size_t icmp_len);
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         nonseekable_open(inode, file);
48
49         socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
50
51         if (!socket_client)
52                 return -ENOMEM;
53
54         for (i = 0; i < ARRAY_SIZE(socket_client_hash); i++) {
55                 if (!socket_client_hash[i]) {
56                         socket_client_hash[i] = socket_client;
57                         break;
58                 }
59         }
60
61         if (i == ARRAY_SIZE(socket_client_hash)) {
62                 pr_err("Error - can't add another packet client: "
63                        "maximum number of clients reached\n");
64                 kfree(socket_client);
65                 return -EXFULL;
66         }
67
68         INIT_LIST_HEAD(&socket_client->queue_list);
69         socket_client->queue_len = 0;
70         socket_client->index = i;
71         socket_client->bat_priv = inode->i_private;
72         spin_lock_init(&socket_client->lock);
73         init_waitqueue_head(&socket_client->queue_wait);
74
75         file->private_data = socket_client;
76
77         inc_module_count();
78         return 0;
79 }
80
81 static int bat_socket_release(struct inode *inode, struct file *file)
82 {
83         struct socket_client *socket_client = file->private_data;
84         struct socket_packet *socket_packet;
85         struct list_head *list_pos, *list_pos_tmp;
86
87         spin_lock_bh(&socket_client->lock);
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_bh(&socket_client->lock);
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 = file->private_data;
111         struct socket_packet *socket_packet;
112         size_t packet_len;
113         int error;
114
115         if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0))
116                 return -EAGAIN;
117
118         if ((!buf) || (count < sizeof(struct icmp_packet)))
119                 return -EINVAL;
120
121         if (!access_ok(VERIFY_WRITE, buf, count))
122                 return -EFAULT;
123
124         error = wait_event_interruptible(socket_client->queue_wait,
125                                          socket_client->queue_len);
126
127         if (error)
128                 return error;
129
130         spin_lock_bh(&socket_client->lock);
131
132         socket_packet = list_first_entry(&socket_client->queue_list,
133                                          struct socket_packet, list);
134         list_del(&socket_packet->list);
135         socket_client->queue_len--;
136
137         spin_unlock_bh(&socket_client->lock);
138
139         packet_len = min(count, socket_packet->icmp_len);
140         error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
141
142         kfree(socket_packet);
143
144         if (error)
145                 return -EFAULT;
146
147         return packet_len;
148 }
149
150 static ssize_t bat_socket_write(struct file *file, const char __user *buff,
151                                 size_t len, loff_t *off)
152 {
153         struct socket_client *socket_client = file->private_data;
154         struct bat_priv *bat_priv = socket_client->bat_priv;
155         struct hard_iface *primary_if = NULL;
156         struct sk_buff *skb;
157         struct icmp_packet_rr *icmp_packet;
158
159         struct orig_node *orig_node = NULL;
160         struct neigh_node *neigh_node = NULL;
161         size_t packet_len = sizeof(struct icmp_packet);
162
163         if (len < sizeof(struct icmp_packet)) {
164                 bat_dbg(DBG_BATMAN, bat_priv,
165                         "Error - can't send packet from char device: "
166                         "invalid packet size\n");
167                 return -EINVAL;
168         }
169
170         primary_if = primary_if_get_selected(bat_priv);
171
172         if (!primary_if) {
173                 len = -EFAULT;
174                 goto out;
175         }
176
177         if (len >= sizeof(struct icmp_packet_rr))
178                 packet_len = sizeof(struct icmp_packet_rr);
179
180         skb = dev_alloc_skb(packet_len + sizeof(struct ethhdr));
181         if (!skb) {
182                 len = -ENOMEM;
183                 goto out;
184         }
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         orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
228         if (!orig_node)
229                 goto dst_unreach;
230
231         neigh_node = orig_node_get_router(orig_node);
232         if (!neigh_node)
233                 goto dst_unreach;
234
235         if (!neigh_node->if_incoming)
236                 goto dst_unreach;
237
238         if (neigh_node->if_incoming->if_status != IF_ACTIVE)
239                 goto dst_unreach;
240
241         memcpy(icmp_packet->orig,
242                primary_if->net_dev->dev_addr, ETH_ALEN);
243
244         if (packet_len == sizeof(struct icmp_packet_rr))
245                 memcpy(icmp_packet->rr,
246                        neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN);
247
248         send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
249         goto out;
250
251 dst_unreach:
252         icmp_packet->msg_type = DESTINATION_UNREACHABLE;
253         bat_socket_add_packet(socket_client, icmp_packet, packet_len);
254 free_skb:
255         kfree_skb(skb);
256 out:
257         if (primary_if)
258                 hardif_free_ref(primary_if);
259         if (neigh_node)
260                 neigh_node_free_ref(neigh_node);
261         if (orig_node)
262                 orig_node_free_ref(orig_node);
263         return len;
264 }
265
266 static unsigned int bat_socket_poll(struct file *file, poll_table *wait)
267 {
268         struct socket_client *socket_client = file->private_data;
269
270         poll_wait(file, &socket_client->queue_wait, wait);
271
272         if (socket_client->queue_len > 0)
273                 return POLLIN | POLLRDNORM;
274
275         return 0;
276 }
277
278 static const struct file_operations fops = {
279         .owner = THIS_MODULE,
280         .open = bat_socket_open,
281         .release = bat_socket_release,
282         .read = bat_socket_read,
283         .write = bat_socket_write,
284         .poll = bat_socket_poll,
285         .llseek = no_llseek,
286 };
287
288 int bat_socket_setup(struct bat_priv *bat_priv)
289 {
290         struct dentry *d;
291
292         if (!bat_priv->debug_dir)
293                 goto err;
294
295         d = debugfs_create_file(ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
296                                 bat_priv->debug_dir, bat_priv, &fops);
297         if (d)
298                 goto err;
299
300         return 0;
301
302 err:
303         return 1;
304 }
305
306 static void bat_socket_add_packet(struct socket_client *socket_client,
307                                   struct icmp_packet_rr *icmp_packet,
308                                   size_t icmp_len)
309 {
310         struct socket_packet *socket_packet;
311
312         socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
313
314         if (!socket_packet)
315                 return;
316
317         INIT_LIST_HEAD(&socket_packet->list);
318         memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
319         socket_packet->icmp_len = icmp_len;
320
321         spin_lock_bh(&socket_client->lock);
322
323         /* while waiting for the lock the socket_client could have been
324          * deleted */
325         if (!socket_client_hash[icmp_packet->uid]) {
326                 spin_unlock_bh(&socket_client->lock);
327                 kfree(socket_packet);
328                 return;
329         }
330
331         list_add_tail(&socket_packet->list, &socket_client->queue_list);
332         socket_client->queue_len++;
333
334         if (socket_client->queue_len > 100) {
335                 socket_packet = list_first_entry(&socket_client->queue_list,
336                                                  struct socket_packet, list);
337
338                 list_del(&socket_packet->list);
339                 kfree(socket_packet);
340                 socket_client->queue_len--;
341         }
342
343         spin_unlock_bh(&socket_client->lock);
344
345         wake_up(&socket_client->queue_wait);
346 }
347
348 void bat_socket_receive_packet(struct icmp_packet_rr *icmp_packet,
349                                size_t icmp_len)
350 {
351         struct socket_client *hash = socket_client_hash[icmp_packet->uid];
352
353         if (hash)
354                 bat_socket_add_packet(hash, icmp_packet, icmp_len);
355 }