batman-adv: split tq information in neigh_node struct
[deliverable/linux.git] / net / batman-adv / icmp_socket.c
CommitLineData
0b873931 1/* Copyright (C) 2007-2013 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
ebf38fb7 15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
c6c8fea2
SE
16 */
17
18#include "main.h"
19#include <linux/debugfs.h>
20#include <linux/slab.h>
21#include "icmp_socket.h"
22#include "send.h"
c6c8fea2
SE
23#include "hash.h"
24#include "originator.h"
25#include "hard-interface.h"
26
56303d34 27static struct batadv_socket_client *batadv_socket_client_hash[256];
c6c8fea2 28
56303d34 29static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
da6b8c20 30 struct batadv_icmp_header *icmph,
af4447f6 31 size_t icmp_len);
c6c8fea2 32
9039dc7e 33void batadv_socket_init(void)
c6c8fea2 34{
af4447f6 35 memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
c6c8fea2
SE
36}
37
af4447f6 38static int batadv_socket_open(struct inode *inode, struct file *file)
c6c8fea2
SE
39{
40 unsigned int i;
56303d34 41 struct batadv_socket_client *socket_client;
c6c8fea2 42
bd5b80d5
SE
43 if (!try_module_get(THIS_MODULE))
44 return -EBUSY;
45
c6c8fea2
SE
46 nonseekable_open(inode, file);
47
704509b8 48 socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
bd5b80d5
SE
49 if (!socket_client) {
50 module_put(THIS_MODULE);
c6c8fea2 51 return -ENOMEM;
bd5b80d5 52 }
c6c8fea2 53
af4447f6
SE
54 for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) {
55 if (!batadv_socket_client_hash[i]) {
56 batadv_socket_client_hash[i] = socket_client;
c6c8fea2
SE
57 break;
58 }
59 }
60
af4447f6 61 if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
86ceb360 62 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
c6c8fea2 63 kfree(socket_client);
bd5b80d5 64 module_put(THIS_MODULE);
c6c8fea2
SE
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
c6c8fea2
SE
77 return 0;
78}
79
af4447f6 80static int batadv_socket_release(struct inode *inode, struct file *file)
c6c8fea2 81{
56303d34
SE
82 struct batadv_socket_client *socket_client = file->private_data;
83 struct batadv_socket_packet *socket_packet;
c6c8fea2
SE
84 struct list_head *list_pos, *list_pos_tmp;
85
86 spin_lock_bh(&socket_client->lock);
87
88 /* for all packets in the queue ... */
89 list_for_each_safe(list_pos, list_pos_tmp, &socket_client->queue_list) {
90 socket_packet = list_entry(list_pos,
56303d34 91 struct batadv_socket_packet, list);
c6c8fea2
SE
92
93 list_del(list_pos);
94 kfree(socket_packet);
95 }
96
af4447f6 97 batadv_socket_client_hash[socket_client->index] = NULL;
c6c8fea2
SE
98 spin_unlock_bh(&socket_client->lock);
99
100 kfree(socket_client);
bd5b80d5 101 module_put(THIS_MODULE);
c6c8fea2
SE
102
103 return 0;
104}
105
af4447f6
SE
106static ssize_t batadv_socket_read(struct file *file, char __user *buf,
107 size_t count, loff_t *ppos)
c6c8fea2 108{
56303d34
SE
109 struct batadv_socket_client *socket_client = file->private_data;
110 struct batadv_socket_packet *socket_packet;
c6c8fea2
SE
111 size_t packet_len;
112 int error;
113
114 if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0))
115 return -EAGAIN;
116
96412690 117 if ((!buf) || (count < sizeof(struct batadv_icmp_packet)))
c6c8fea2
SE
118 return -EINVAL;
119
120 if (!access_ok(VERIFY_WRITE, buf, count))
121 return -EFAULT;
122
123 error = wait_event_interruptible(socket_client->queue_wait,
124 socket_client->queue_len);
125
126 if (error)
127 return error;
128
129 spin_lock_bh(&socket_client->lock);
130
131 socket_packet = list_first_entry(&socket_client->queue_list,
56303d34 132 struct batadv_socket_packet, list);
c6c8fea2
SE
133 list_del(&socket_packet->list);
134 socket_client->queue_len--;
135
136 spin_unlock_bh(&socket_client->lock);
137
b5a1eeef
SE
138 packet_len = min(count, socket_packet->icmp_len);
139 error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
c6c8fea2 140
c6c8fea2
SE
141 kfree(socket_packet);
142
143 if (error)
144 return -EFAULT;
145
146 return packet_len;
147}
148
af4447f6
SE
149static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
150 size_t len, loff_t *off)
c6c8fea2 151{
56303d34
SE
152 struct batadv_socket_client *socket_client = file->private_data;
153 struct batadv_priv *bat_priv = socket_client->bat_priv;
154 struct batadv_hard_iface *primary_if = NULL;
c6c8fea2 155 struct sk_buff *skb;
da6b8c20
SW
156 struct batadv_icmp_packet_rr *icmp_packet_rr;
157 struct batadv_icmp_header *icmp_header;
56303d34
SE
158 struct batadv_orig_node *orig_node = NULL;
159 struct batadv_neigh_node *neigh_node = NULL;
96412690 160 size_t packet_len = sizeof(struct batadv_icmp_packet);
c6c8fea2 161
da6b8c20 162 if (len < sizeof(struct batadv_icmp_header)) {
39c75a51 163 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 164 "Error - can't send packet from char device: invalid packet size\n");
c6c8fea2
SE
165 return -EINVAL;
166 }
167
e5d89254 168 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22
ML
169
170 if (!primary_if) {
171 len = -EFAULT;
172 goto out;
173 }
c6c8fea2 174
da6b8c20
SW
175 if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
176 packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
177 else
178 packet_len = len;
c6c8fea2 179
41ab6c48 180 skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
32ae9b22
ML
181 if (!skb) {
182 len = -ENOMEM;
183 goto out;
184 }
c6c8fea2 185
c54f38c9 186 skb->priority = TC_PRIO_CONTROL;
41ab6c48 187 skb_reserve(skb, ETH_HLEN);
da6b8c20 188 icmp_header = (struct batadv_icmp_header *)skb_put(skb, packet_len);
c6c8fea2 189
da6b8c20 190 if (copy_from_user(icmp_header, buff, packet_len)) {
c6c8fea2
SE
191 len = -EFAULT;
192 goto free_skb;
193 }
194
a40d9b07 195 if (icmp_header->packet_type != BATADV_ICMP) {
39c75a51 196 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 197 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
c6c8fea2
SE
198 len = -EINVAL;
199 goto free_skb;
200 }
201
da6b8c20
SW
202 switch (icmp_header->msg_type) {
203 case BATADV_ECHO_REQUEST:
204 if (len < sizeof(struct batadv_icmp_packet)) {
205 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
206 "Error - can't send packet from char device: invalid packet size\n");
207 len = -EINVAL;
208 goto free_skb;
209 }
210
211 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
212 goto dst_unreach;
213
214 orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
215 if (!orig_node)
216 goto dst_unreach;
217
218 neigh_node = batadv_orig_node_get_router(orig_node);
219 if (!neigh_node)
220 goto dst_unreach;
221
222 if (!neigh_node->if_incoming)
223 goto dst_unreach;
224
225 if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
226 goto dst_unreach;
227
228 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header;
229 if (packet_len == sizeof(*icmp_packet_rr))
230 memcpy(icmp_packet_rr->rr,
231 neigh_node->if_incoming->net_dev->dev_addr,
232 ETH_ALEN);
233
234 break;
235 default:
39c75a51 236 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
da6b8c20 237 "Error - can't send packet from char device: got unknown message type\n");
c6c8fea2
SE
238 len = -EINVAL;
239 goto free_skb;
240 }
241
da6b8c20 242 icmp_header->uid = socket_client->index;
c6c8fea2 243
a40d9b07 244 if (icmp_header->version != BATADV_COMPAT_VERSION) {
da6b8c20 245 icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
a40d9b07 246 icmp_header->version = BATADV_COMPAT_VERSION;
da6b8c20 247 batadv_socket_add_packet(socket_client, icmp_header,
af4447f6 248 packet_len);
c6c8fea2
SE
249 goto free_skb;
250 }
251
da6b8c20 252 memcpy(icmp_header->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
c6c8fea2 253
9455e34c 254 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
c6c8fea2
SE
255 goto out;
256
c6c8fea2 257dst_unreach:
da6b8c20
SW
258 icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
259 batadv_socket_add_packet(socket_client, icmp_header, packet_len);
c6c8fea2
SE
260free_skb:
261 kfree_skb(skb);
262out:
32ae9b22 263 if (primary_if)
e5d89254 264 batadv_hardif_free_ref(primary_if);
44524fcd 265 if (neigh_node)
7d211efc 266 batadv_neigh_node_free_ref(neigh_node);
44524fcd 267 if (orig_node)
7d211efc 268 batadv_orig_node_free_ref(orig_node);
c6c8fea2
SE
269 return len;
270}
271
af4447f6 272static unsigned int batadv_socket_poll(struct file *file, poll_table *wait)
c6c8fea2 273{
56303d34 274 struct batadv_socket_client *socket_client = file->private_data;
c6c8fea2
SE
275
276 poll_wait(file, &socket_client->queue_wait, wait);
277
278 if (socket_client->queue_len > 0)
279 return POLLIN | POLLRDNORM;
280
281 return 0;
282}
283
af4447f6 284static const struct file_operations batadv_fops = {
c6c8fea2 285 .owner = THIS_MODULE,
af4447f6
SE
286 .open = batadv_socket_open,
287 .release = batadv_socket_release,
288 .read = batadv_socket_read,
289 .write = batadv_socket_write,
290 .poll = batadv_socket_poll,
c6c8fea2
SE
291 .llseek = no_llseek,
292};
293
56303d34 294int batadv_socket_setup(struct batadv_priv *bat_priv)
c6c8fea2
SE
295{
296 struct dentry *d;
297
298 if (!bat_priv->debug_dir)
299 goto err;
300
64346643 301 d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
af4447f6 302 bat_priv->debug_dir, bat_priv, &batadv_fops);
5346c35e 303 if (!d)
c6c8fea2
SE
304 goto err;
305
306 return 0;
307
308err:
5346c35e 309 return -ENOMEM;
c6c8fea2
SE
310}
311
da6b8c20
SW
312/**
313 * batadv_socket_receive_packet - schedule an icmp packet to be sent to userspace
314 * on an icmp socket.
315 * @socket_client: the socket this packet belongs to
316 * @icmph: pointer to the header of the icmp packet
317 * @icmp_len: total length of the icmp packet
318 */
56303d34 319static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
da6b8c20 320 struct batadv_icmp_header *icmph,
af4447f6 321 size_t icmp_len)
c6c8fea2 322{
56303d34 323 struct batadv_socket_packet *socket_packet;
da6b8c20 324 size_t len;
c6c8fea2 325
704509b8 326 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
c6c8fea2
SE
327
328 if (!socket_packet)
329 return;
330
da6b8c20
SW
331 len = icmp_len;
332 /* check the maximum length before filling the buffer */
333 if (len > sizeof(socket_packet->icmp_packet))
334 len = sizeof(socket_packet->icmp_packet);
335
c6c8fea2 336 INIT_LIST_HEAD(&socket_packet->list);
da6b8c20
SW
337 memcpy(&socket_packet->icmp_packet, icmph, len);
338 socket_packet->icmp_len = len;
c6c8fea2
SE
339
340 spin_lock_bh(&socket_client->lock);
341
342 /* while waiting for the lock the socket_client could have been
9cfc7bd6
SE
343 * deleted
344 */
da6b8c20 345 if (!batadv_socket_client_hash[icmph->uid]) {
c6c8fea2
SE
346 spin_unlock_bh(&socket_client->lock);
347 kfree(socket_packet);
348 return;
349 }
350
351 list_add_tail(&socket_packet->list, &socket_client->queue_list);
352 socket_client->queue_len++;
353
354 if (socket_client->queue_len > 100) {
355 socket_packet = list_first_entry(&socket_client->queue_list,
56303d34
SE
356 struct batadv_socket_packet,
357 list);
c6c8fea2
SE
358
359 list_del(&socket_packet->list);
360 kfree(socket_packet);
361 socket_client->queue_len--;
362 }
363
364 spin_unlock_bh(&socket_client->lock);
365
366 wake_up(&socket_client->queue_wait);
367}
368
da6b8c20
SW
369/**
370 * batadv_socket_receive_packet - schedule an icmp packet to be received
371 * locally and sent to userspace.
372 * @icmph: pointer to the header of the icmp packet
373 * @icmp_len: total length of the icmp packet
374 */
375void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
9039dc7e 376 size_t icmp_len)
c6c8fea2 377{
56303d34 378 struct batadv_socket_client *hash;
c6c8fea2 379
da6b8c20 380 hash = batadv_socket_client_hash[icmph->uid];
c6c8fea2 381 if (hash)
da6b8c20 382 batadv_socket_add_packet(hash, icmph, icmp_len);
c6c8fea2 383}
This page took 0.244286 seconds and 5 git commands to generate.