batman-adv: Prefix icmp_socket defines with BATADV_
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / batman-adv / icmp_socket.c
CommitLineData
9cfc7bd6 1/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
c6c8fea2
SE
2 *
3 * Marek Lindner
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
c6c8fea2
SE
18 */
19
20#include "main.h"
21#include <linux/debugfs.h>
22#include <linux/slab.h>
23#include "icmp_socket.h"
24#include "send.h"
c6c8fea2
SE
25#include "hash.h"
26#include "originator.h"
27#include "hard-interface.h"
28
af4447f6 29static struct socket_client *batadv_socket_client_hash[256];
c6c8fea2 30
af4447f6
SE
31static void batadv_socket_add_packet(struct socket_client *socket_client,
32 struct icmp_packet_rr *icmp_packet,
33 size_t icmp_len);
c6c8fea2 34
9039dc7e 35void batadv_socket_init(void)
c6c8fea2 36{
af4447f6 37 memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
c6c8fea2
SE
38}
39
af4447f6 40static int batadv_socket_open(struct inode *inode, struct file *file)
c6c8fea2
SE
41{
42 unsigned int i;
43 struct socket_client *socket_client;
44
45 nonseekable_open(inode, file);
46
704509b8 47 socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
c6c8fea2
SE
48
49 if (!socket_client)
50 return -ENOMEM;
51
af4447f6
SE
52 for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) {
53 if (!batadv_socket_client_hash[i]) {
54 batadv_socket_client_hash[i] = socket_client;
c6c8fea2
SE
55 break;
56 }
57 }
58
af4447f6 59 if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
86ceb360 60 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
c6c8fea2
SE
61 kfree(socket_client);
62 return -EXFULL;
63 }
64
65 INIT_LIST_HEAD(&socket_client->queue_list);
66 socket_client->queue_len = 0;
67 socket_client->index = i;
68 socket_client->bat_priv = inode->i_private;
69 spin_lock_init(&socket_client->lock);
70 init_waitqueue_head(&socket_client->queue_wait);
71
72 file->private_data = socket_client;
73
3193e8fd 74 batadv_inc_module_count();
c6c8fea2
SE
75 return 0;
76}
77
af4447f6 78static int batadv_socket_release(struct inode *inode, struct file *file)
c6c8fea2
SE
79{
80 struct socket_client *socket_client = file->private_data;
81 struct socket_packet *socket_packet;
82 struct list_head *list_pos, *list_pos_tmp;
83
84 spin_lock_bh(&socket_client->lock);
85
86 /* for all packets in the queue ... */
87 list_for_each_safe(list_pos, list_pos_tmp, &socket_client->queue_list) {
88 socket_packet = list_entry(list_pos,
89 struct socket_packet, list);
90
91 list_del(list_pos);
92 kfree(socket_packet);
93 }
94
af4447f6 95 batadv_socket_client_hash[socket_client->index] = NULL;
c6c8fea2
SE
96 spin_unlock_bh(&socket_client->lock);
97
98 kfree(socket_client);
3193e8fd 99 batadv_dec_module_count();
c6c8fea2
SE
100
101 return 0;
102}
103
af4447f6
SE
104static ssize_t batadv_socket_read(struct file *file, char __user *buf,
105 size_t count, loff_t *ppos)
c6c8fea2
SE
106{
107 struct socket_client *socket_client = file->private_data;
108 struct socket_packet *socket_packet;
109 size_t packet_len;
110 int error;
111
112 if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0))
113 return -EAGAIN;
114
115 if ((!buf) || (count < sizeof(struct icmp_packet)))
116 return -EINVAL;
117
118 if (!access_ok(VERIFY_WRITE, buf, count))
119 return -EFAULT;
120
121 error = wait_event_interruptible(socket_client->queue_wait,
122 socket_client->queue_len);
123
124 if (error)
125 return error;
126
127 spin_lock_bh(&socket_client->lock);
128
129 socket_packet = list_first_entry(&socket_client->queue_list,
130 struct socket_packet, list);
131 list_del(&socket_packet->list);
132 socket_client->queue_len--;
133
134 spin_unlock_bh(&socket_client->lock);
135
b5a1eeef
SE
136 packet_len = min(count, socket_packet->icmp_len);
137 error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
c6c8fea2 138
c6c8fea2
SE
139 kfree(socket_packet);
140
141 if (error)
142 return -EFAULT;
143
144 return packet_len;
145}
146
af4447f6
SE
147static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
148 size_t len, loff_t *off)
c6c8fea2
SE
149{
150 struct socket_client *socket_client = file->private_data;
151 struct bat_priv *bat_priv = socket_client->bat_priv;
32ae9b22 152 struct hard_iface *primary_if = NULL;
c6c8fea2
SE
153 struct sk_buff *skb;
154 struct icmp_packet_rr *icmp_packet;
155
44524fcd
ML
156 struct orig_node *orig_node = NULL;
157 struct neigh_node *neigh_node = NULL;
c6c8fea2 158 size_t packet_len = sizeof(struct icmp_packet);
c6c8fea2
SE
159
160 if (len < sizeof(struct icmp_packet)) {
1eda58bf
SE
161 batadv_dbg(DBG_BATMAN, bat_priv,
162 "Error - can't send packet from char device: invalid packet size\n");
c6c8fea2
SE
163 return -EINVAL;
164 }
165
e5d89254 166 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22
ML
167
168 if (!primary_if) {
169 len = -EFAULT;
170 goto out;
171 }
c6c8fea2
SE
172
173 if (len >= sizeof(struct icmp_packet_rr))
174 packet_len = sizeof(struct icmp_packet_rr);
175
0d125074 176 skb = dev_alloc_skb(packet_len + ETH_HLEN);
32ae9b22
ML
177 if (!skb) {
178 len = -ENOMEM;
179 goto out;
180 }
c6c8fea2 181
0d125074 182 skb_reserve(skb, ETH_HLEN);
c6c8fea2
SE
183 icmp_packet = (struct icmp_packet_rr *)skb_put(skb, packet_len);
184
d18eb453 185 if (copy_from_user(icmp_packet, buff, packet_len)) {
c6c8fea2
SE
186 len = -EFAULT;
187 goto free_skb;
188 }
189
76543d14 190 if (icmp_packet->header.packet_type != BAT_ICMP) {
1eda58bf
SE
191 batadv_dbg(DBG_BATMAN, bat_priv,
192 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
c6c8fea2
SE
193 len = -EINVAL;
194 goto free_skb;
195 }
196
197 if (icmp_packet->msg_type != ECHO_REQUEST) {
1eda58bf
SE
198 batadv_dbg(DBG_BATMAN, bat_priv,
199 "Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
c6c8fea2
SE
200 len = -EINVAL;
201 goto free_skb;
202 }
203
204 icmp_packet->uid = socket_client->index;
205
76543d14 206 if (icmp_packet->header.version != COMPAT_VERSION) {
c6c8fea2 207 icmp_packet->msg_type = PARAMETER_PROBLEM;
76543d14 208 icmp_packet->header.version = COMPAT_VERSION;
af4447f6
SE
209 batadv_socket_add_packet(socket_client, icmp_packet,
210 packet_len);
c6c8fea2
SE
211 goto free_skb;
212 }
213
214 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
215 goto dst_unreach;
216
da641193 217 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
c6c8fea2 218 if (!orig_node)
e1a5382f 219 goto dst_unreach;
44524fcd 220
7d211efc 221 neigh_node = batadv_orig_node_get_router(orig_node);
44524fcd 222 if (!neigh_node)
e1a5382f 223 goto dst_unreach;
c6c8fea2 224
d0072609 225 if (!neigh_node->if_incoming)
c6c8fea2
SE
226 goto dst_unreach;
227
d0072609 228 if (neigh_node->if_incoming->if_status != IF_ACTIVE)
c6c8fea2
SE
229 goto dst_unreach;
230
231 memcpy(icmp_packet->orig,
32ae9b22 232 primary_if->net_dev->dev_addr, ETH_ALEN);
c6c8fea2
SE
233
234 if (packet_len == sizeof(struct icmp_packet_rr))
44524fcd 235 memcpy(icmp_packet->rr,
d0072609 236 neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN);
c6c8fea2 237
9455e34c 238 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
c6c8fea2
SE
239 goto out;
240
c6c8fea2
SE
241dst_unreach:
242 icmp_packet->msg_type = DESTINATION_UNREACHABLE;
af4447f6 243 batadv_socket_add_packet(socket_client, icmp_packet, packet_len);
c6c8fea2
SE
244free_skb:
245 kfree_skb(skb);
246out:
32ae9b22 247 if (primary_if)
e5d89254 248 batadv_hardif_free_ref(primary_if);
44524fcd 249 if (neigh_node)
7d211efc 250 batadv_neigh_node_free_ref(neigh_node);
44524fcd 251 if (orig_node)
7d211efc 252 batadv_orig_node_free_ref(orig_node);
c6c8fea2
SE
253 return len;
254}
255
af4447f6 256static unsigned int batadv_socket_poll(struct file *file, poll_table *wait)
c6c8fea2
SE
257{
258 struct socket_client *socket_client = file->private_data;
259
260 poll_wait(file, &socket_client->queue_wait, wait);
261
262 if (socket_client->queue_len > 0)
263 return POLLIN | POLLRDNORM;
264
265 return 0;
266}
267
af4447f6 268static const struct file_operations batadv_fops = {
c6c8fea2 269 .owner = THIS_MODULE,
af4447f6
SE
270 .open = batadv_socket_open,
271 .release = batadv_socket_release,
272 .read = batadv_socket_read,
273 .write = batadv_socket_write,
274 .poll = batadv_socket_poll,
c6c8fea2
SE
275 .llseek = no_llseek,
276};
277
9039dc7e 278int batadv_socket_setup(struct bat_priv *bat_priv)
c6c8fea2
SE
279{
280 struct dentry *d;
281
282 if (!bat_priv->debug_dir)
283 goto err;
284
64346643 285 d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
af4447f6 286 bat_priv->debug_dir, bat_priv, &batadv_fops);
5346c35e 287 if (!d)
c6c8fea2
SE
288 goto err;
289
290 return 0;
291
292err:
5346c35e 293 return -ENOMEM;
c6c8fea2
SE
294}
295
af4447f6
SE
296static void batadv_socket_add_packet(struct socket_client *socket_client,
297 struct icmp_packet_rr *icmp_packet,
298 size_t icmp_len)
c6c8fea2
SE
299{
300 struct socket_packet *socket_packet;
301
704509b8 302 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
c6c8fea2
SE
303
304 if (!socket_packet)
305 return;
306
307 INIT_LIST_HEAD(&socket_packet->list);
308 memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
309 socket_packet->icmp_len = icmp_len;
310
311 spin_lock_bh(&socket_client->lock);
312
313 /* while waiting for the lock the socket_client could have been
9cfc7bd6
SE
314 * deleted
315 */
af4447f6 316 if (!batadv_socket_client_hash[icmp_packet->uid]) {
c6c8fea2
SE
317 spin_unlock_bh(&socket_client->lock);
318 kfree(socket_packet);
319 return;
320 }
321
322 list_add_tail(&socket_packet->list, &socket_client->queue_list);
323 socket_client->queue_len++;
324
325 if (socket_client->queue_len > 100) {
326 socket_packet = list_first_entry(&socket_client->queue_list,
327 struct socket_packet, list);
328
329 list_del(&socket_packet->list);
330 kfree(socket_packet);
331 socket_client->queue_len--;
332 }
333
334 spin_unlock_bh(&socket_client->lock);
335
336 wake_up(&socket_client->queue_wait);
337}
338
9039dc7e
SE
339void batadv_socket_receive_packet(struct icmp_packet_rr *icmp_packet,
340 size_t icmp_len)
c6c8fea2 341{
af4447f6 342 struct socket_client *hash;
c6c8fea2 343
af4447f6 344 hash = batadv_socket_client_hash[icmp_packet->uid];
c6c8fea2 345 if (hash)
af4447f6 346 batadv_socket_add_packet(hash, icmp_packet, icmp_len);
c6c8fea2 347}