net/core:Remove memleak reports by kmemleak_not_leak.
[linux-2.6/btrfs-unstable.git] / net / batman-adv / icmp_socket.c
blobb87518edcef935d749e46d552c1860d2045b7616
1 /*
2 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
4 * Marek Lindner
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.
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.
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
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"
31 static struct socket_client *socket_client_hash[256];
33 static void bat_socket_add_packet(struct socket_client *socket_client,
34 struct icmp_packet_rr *icmp_packet,
35 size_t icmp_len);
37 void bat_socket_init(void)
39 memset(socket_client_hash, 0, sizeof(socket_client_hash));
42 static int bat_socket_open(struct inode *inode, struct file *file)
44 unsigned int i;
45 struct socket_client *socket_client;
47 nonseekable_open(inode, file);
49 socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
51 if (!socket_client)
52 return -ENOMEM;
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;
61 if (i == ARRAY_SIZE(socket_client_hash)) {
62 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
63 kfree(socket_client);
64 return -EXFULL;
67 INIT_LIST_HEAD(&socket_client->queue_list);
68 socket_client->queue_len = 0;
69 socket_client->index = i;
70 socket_client->bat_priv = inode->i_private;
71 spin_lock_init(&socket_client->lock);
72 init_waitqueue_head(&socket_client->queue_wait);
74 file->private_data = socket_client;
76 inc_module_count();
77 return 0;
80 static int bat_socket_release(struct inode *inode, struct file *file)
82 struct socket_client *socket_client = file->private_data;
83 struct socket_packet *socket_packet;
84 struct list_head *list_pos, *list_pos_tmp;
86 spin_lock_bh(&socket_client->lock);
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,
91 struct socket_packet, list);
93 list_del(list_pos);
94 kfree(socket_packet);
97 socket_client_hash[socket_client->index] = NULL;
98 spin_unlock_bh(&socket_client->lock);
100 kfree(socket_client);
101 dec_module_count();
103 return 0;
106 static ssize_t bat_socket_read(struct file *file, char __user *buf,
107 size_t count, loff_t *ppos)
109 struct socket_client *socket_client = file->private_data;
110 struct socket_packet *socket_packet;
111 size_t packet_len;
112 int error;
114 if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0))
115 return -EAGAIN;
117 if ((!buf) || (count < sizeof(struct icmp_packet)))
118 return -EINVAL;
120 if (!access_ok(VERIFY_WRITE, buf, count))
121 return -EFAULT;
123 error = wait_event_interruptible(socket_client->queue_wait,
124 socket_client->queue_len);
126 if (error)
127 return error;
129 spin_lock_bh(&socket_client->lock);
131 socket_packet = list_first_entry(&socket_client->queue_list,
132 struct socket_packet, list);
133 list_del(&socket_packet->list);
134 socket_client->queue_len--;
136 spin_unlock_bh(&socket_client->lock);
138 packet_len = min(count, socket_packet->icmp_len);
139 error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
141 kfree(socket_packet);
143 if (error)
144 return -EFAULT;
146 return packet_len;
149 static ssize_t bat_socket_write(struct file *file, const char __user *buff,
150 size_t len, loff_t *off)
152 struct socket_client *socket_client = file->private_data;
153 struct bat_priv *bat_priv = socket_client->bat_priv;
154 struct hard_iface *primary_if = NULL;
155 struct sk_buff *skb;
156 struct icmp_packet_rr *icmp_packet;
158 struct orig_node *orig_node = NULL;
159 struct neigh_node *neigh_node = NULL;
160 size_t packet_len = sizeof(struct icmp_packet);
162 if (len < sizeof(struct icmp_packet)) {
163 bat_dbg(DBG_BATMAN, bat_priv,
164 "Error - can't send packet from char device: invalid packet size\n");
165 return -EINVAL;
168 primary_if = primary_if_get_selected(bat_priv);
170 if (!primary_if) {
171 len = -EFAULT;
172 goto out;
175 if (len >= sizeof(struct icmp_packet_rr))
176 packet_len = sizeof(struct icmp_packet_rr);
178 skb = dev_alloc_skb(packet_len + sizeof(struct ethhdr));
179 if (!skb) {
180 len = -ENOMEM;
181 goto out;
184 skb_reserve(skb, sizeof(struct ethhdr));
185 icmp_packet = (struct icmp_packet_rr *)skb_put(skb, packet_len);
187 if (copy_from_user(icmp_packet, buff, packet_len)) {
188 len = -EFAULT;
189 goto free_skb;
192 if (icmp_packet->header.packet_type != BAT_ICMP) {
193 bat_dbg(DBG_BATMAN, bat_priv,
194 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
195 len = -EINVAL;
196 goto free_skb;
199 if (icmp_packet->msg_type != ECHO_REQUEST) {
200 bat_dbg(DBG_BATMAN, bat_priv,
201 "Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
202 len = -EINVAL;
203 goto free_skb;
206 icmp_packet->uid = socket_client->index;
208 if (icmp_packet->header.version != COMPAT_VERSION) {
209 icmp_packet->msg_type = PARAMETER_PROBLEM;
210 icmp_packet->header.version = COMPAT_VERSION;
211 bat_socket_add_packet(socket_client, icmp_packet, packet_len);
212 goto free_skb;
215 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
216 goto dst_unreach;
218 orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
219 if (!orig_node)
220 goto dst_unreach;
222 neigh_node = orig_node_get_router(orig_node);
223 if (!neigh_node)
224 goto dst_unreach;
226 if (!neigh_node->if_incoming)
227 goto dst_unreach;
229 if (neigh_node->if_incoming->if_status != IF_ACTIVE)
230 goto dst_unreach;
232 memcpy(icmp_packet->orig,
233 primary_if->net_dev->dev_addr, ETH_ALEN);
235 if (packet_len == sizeof(struct icmp_packet_rr))
236 memcpy(icmp_packet->rr,
237 neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN);
239 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
240 goto out;
242 dst_unreach:
243 icmp_packet->msg_type = DESTINATION_UNREACHABLE;
244 bat_socket_add_packet(socket_client, icmp_packet, packet_len);
245 free_skb:
246 kfree_skb(skb);
247 out:
248 if (primary_if)
249 hardif_free_ref(primary_if);
250 if (neigh_node)
251 neigh_node_free_ref(neigh_node);
252 if (orig_node)
253 orig_node_free_ref(orig_node);
254 return len;
257 static unsigned int bat_socket_poll(struct file *file, poll_table *wait)
259 struct socket_client *socket_client = file->private_data;
261 poll_wait(file, &socket_client->queue_wait, wait);
263 if (socket_client->queue_len > 0)
264 return POLLIN | POLLRDNORM;
266 return 0;
269 static const struct file_operations fops = {
270 .owner = THIS_MODULE,
271 .open = bat_socket_open,
272 .release = bat_socket_release,
273 .read = bat_socket_read,
274 .write = bat_socket_write,
275 .poll = bat_socket_poll,
276 .llseek = no_llseek,
279 int bat_socket_setup(struct bat_priv *bat_priv)
281 struct dentry *d;
283 if (!bat_priv->debug_dir)
284 goto err;
286 d = debugfs_create_file(ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
287 bat_priv->debug_dir, bat_priv, &fops);
288 if (d)
289 goto err;
291 return 0;
293 err:
294 return 1;
297 static void bat_socket_add_packet(struct socket_client *socket_client,
298 struct icmp_packet_rr *icmp_packet,
299 size_t icmp_len)
301 struct socket_packet *socket_packet;
303 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
305 if (!socket_packet)
306 return;
308 INIT_LIST_HEAD(&socket_packet->list);
309 memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
310 socket_packet->icmp_len = icmp_len;
312 spin_lock_bh(&socket_client->lock);
314 /* while waiting for the lock the socket_client could have been
315 * deleted */
316 if (!socket_client_hash[icmp_packet->uid]) {
317 spin_unlock_bh(&socket_client->lock);
318 kfree(socket_packet);
319 return;
322 list_add_tail(&socket_packet->list, &socket_client->queue_list);
323 socket_client->queue_len++;
325 if (socket_client->queue_len > 100) {
326 socket_packet = list_first_entry(&socket_client->queue_list,
327 struct socket_packet, list);
329 list_del(&socket_packet->list);
330 kfree(socket_packet);
331 socket_client->queue_len--;
334 spin_unlock_bh(&socket_client->lock);
336 wake_up(&socket_client->queue_wait);
339 void bat_socket_receive_packet(struct icmp_packet_rr *icmp_packet,
340 size_t icmp_len)
342 struct socket_client *hash = socket_client_hash[icmp_packet->uid];
344 if (hash)
345 bat_socket_add_packet(hash, icmp_packet, icmp_len);