Staging: batman-adv: Directly prepare icmp packets in socket buffer
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / batman-adv / icmp_socket.c
blob8e986a175c72c1b40491405bcdd4465330665b5c
1 /*
2 * Copyright (C) 2007-2010 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 "types.h"
28 #include "hash.h"
29 #include "hard-interface.h"
32 static struct socket_client *socket_client_hash[256];
34 static void bat_socket_add_packet(struct socket_client *socket_client,
35 struct icmp_packet_rr *icmp_packet,
36 size_t icmp_len);
38 void bat_socket_init(void)
40 memset(socket_client_hash, 0, sizeof(socket_client_hash));
43 static int bat_socket_open(struct inode *inode, struct file *file)
45 unsigned int i;
46 struct socket_client *socket_client;
48 socket_client = kmalloc(sizeof(struct socket_client), GFP_KERNEL);
50 if (!socket_client)
51 return -ENOMEM;
53 for (i = 0; i < ARRAY_SIZE(socket_client_hash); i++) {
54 if (!socket_client_hash[i]) {
55 socket_client_hash[i] = socket_client;
56 break;
60 if (i == ARRAY_SIZE(socket_client_hash)) {
61 pr_err("Error - can't add another packet client: "
62 "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;
85 unsigned long flags;
87 spin_lock_irqsave(&socket_client->lock, flags);
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);
94 list_del(list_pos);
95 kfree(socket_packet);
98 socket_client_hash[socket_client->index] = NULL;
99 spin_unlock_irqrestore(&socket_client->lock, flags);
101 kfree(socket_client);
102 dec_module_count();
104 return 0;
107 static ssize_t bat_socket_read(struct file *file, char __user *buf,
108 size_t count, loff_t *ppos)
110 struct socket_client *socket_client = file->private_data;
111 struct socket_packet *socket_packet;
112 size_t packet_len;
113 int error;
114 unsigned long flags;
116 if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0))
117 return -EAGAIN;
119 if ((!buf) || (count < sizeof(struct icmp_packet)))
120 return -EINVAL;
122 if (!access_ok(VERIFY_WRITE, buf, count))
123 return -EFAULT;
125 error = wait_event_interruptible(socket_client->queue_wait,
126 socket_client->queue_len);
128 if (error)
129 return error;
131 spin_lock_irqsave(&socket_client->lock, flags);
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--;
138 spin_unlock_irqrestore(&socket_client->lock, flags);
140 error = __copy_to_user(buf, &socket_packet->icmp_packet,
141 socket_packet->icmp_len);
143 packet_len = socket_packet->icmp_len;
144 kfree(socket_packet);
146 if (error)
147 return -EFAULT;
149 return packet_len;
152 static ssize_t bat_socket_write(struct file *file, const char __user *buff,
153 size_t len, loff_t *off)
155 struct socket_client *socket_client = file->private_data;
156 struct bat_priv *bat_priv = socket_client->bat_priv;
157 struct sk_buff *skb;
158 struct icmp_packet_rr *icmp_packet;
159 struct orig_node *orig_node;
160 struct batman_if *batman_if;
161 size_t packet_len = sizeof(struct icmp_packet);
162 uint8_t dstaddr[ETH_ALEN];
163 unsigned long flags;
165 if (len < sizeof(struct icmp_packet)) {
166 bat_dbg(DBG_BATMAN, bat_priv,
167 "Error - can't send packet from char device: "
168 "invalid packet size\n");
169 return -EINVAL;
172 if (!bat_priv->primary_if)
173 return -EFAULT;
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 return -ENOMEM;
182 skb_reserve(skb, sizeof(struct ethhdr));
183 icmp_packet = (struct icmp_packet_rr *)skb_put(skb, packet_len);
185 if (!access_ok(VERIFY_READ, buff, packet_len)) {
186 len = -EFAULT;
187 goto free_skb;
190 if (__copy_from_user(icmp_packet, buff, packet_len)) {
191 len = -EFAULT;
192 goto free_skb;
195 if (icmp_packet->packet_type != BAT_ICMP) {
196 bat_dbg(DBG_BATMAN, bat_priv,
197 "Error - can't send packet from char device: "
198 "got bogus packet type (expected: BAT_ICMP)\n");
199 len = -EINVAL;
200 goto free_skb;
203 if (icmp_packet->msg_type != ECHO_REQUEST) {
204 bat_dbg(DBG_BATMAN, bat_priv,
205 "Error - can't send packet from char device: "
206 "got bogus message type (expected: ECHO_REQUEST)\n");
207 len = -EINVAL;
208 goto free_skb;
211 icmp_packet->uid = socket_client->index;
213 if (icmp_packet->version != COMPAT_VERSION) {
214 icmp_packet->msg_type = PARAMETER_PROBLEM;
215 icmp_packet->ttl = COMPAT_VERSION;
216 bat_socket_add_packet(socket_client, icmp_packet, packet_len);
217 goto free_skb;
220 if (atomic_read(&module_state) != MODULE_ACTIVE)
221 goto dst_unreach;
223 spin_lock_irqsave(&orig_hash_lock, flags);
224 orig_node = (struct orig_node *)hash_find(orig_hash, icmp_packet->dst);
226 if (!orig_node)
227 goto unlock;
229 if (!orig_node->router)
230 goto unlock;
232 batman_if = orig_node->router->if_incoming;
233 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
235 spin_unlock_irqrestore(&orig_hash_lock, flags);
237 if (!batman_if)
238 goto dst_unreach;
240 if (batman_if->if_status != IF_ACTIVE)
241 goto dst_unreach;
243 memcpy(icmp_packet->orig,
244 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
246 if (packet_len == sizeof(struct icmp_packet_rr))
247 memcpy(icmp_packet->rr, batman_if->net_dev->dev_addr, ETH_ALEN);
250 send_skb_packet(skb, batman_if, dstaddr);
252 goto out;
254 unlock:
255 spin_unlock_irqrestore(&orig_hash_lock, flags);
256 dst_unreach:
257 icmp_packet->msg_type = DESTINATION_UNREACHABLE;
258 bat_socket_add_packet(socket_client, icmp_packet, packet_len);
259 free_skb:
260 kfree_skb(skb);
261 out:
262 return len;
265 static unsigned int bat_socket_poll(struct file *file, poll_table *wait)
267 struct socket_client *socket_client = file->private_data;
269 poll_wait(file, &socket_client->queue_wait, wait);
271 if (socket_client->queue_len > 0)
272 return POLLIN | POLLRDNORM;
274 return 0;
277 static const struct file_operations fops = {
278 .owner = THIS_MODULE,
279 .open = bat_socket_open,
280 .release = bat_socket_release,
281 .read = bat_socket_read,
282 .write = bat_socket_write,
283 .poll = bat_socket_poll,
286 int bat_socket_setup(struct bat_priv *bat_priv)
288 struct dentry *d;
290 if (!bat_priv->debug_dir)
291 goto err;
293 d = debugfs_create_file(ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
294 bat_priv->debug_dir, bat_priv, &fops);
295 if (d)
296 goto err;
298 return 0;
300 err:
301 return 1;
304 static void bat_socket_add_packet(struct socket_client *socket_client,
305 struct icmp_packet_rr *icmp_packet,
306 size_t icmp_len)
308 struct socket_packet *socket_packet;
309 unsigned long flags;
311 socket_packet = kmalloc(sizeof(struct socket_packet), GFP_ATOMIC);
313 if (!socket_packet)
314 return;
316 INIT_LIST_HEAD(&socket_packet->list);
317 memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
318 socket_packet->icmp_len = icmp_len;
320 spin_lock_irqsave(&socket_client->lock, flags);
322 /* while waiting for the lock the socket_client could have been
323 * deleted */
324 if (!socket_client_hash[icmp_packet->uid]) {
325 spin_unlock_irqrestore(&socket_client->lock, flags);
326 kfree(socket_packet);
327 return;
330 list_add_tail(&socket_packet->list, &socket_client->queue_list);
331 socket_client->queue_len++;
333 if (socket_client->queue_len > 100) {
334 socket_packet = list_first_entry(&socket_client->queue_list,
335 struct socket_packet, list);
337 list_del(&socket_packet->list);
338 kfree(socket_packet);
339 socket_client->queue_len--;
342 spin_unlock_irqrestore(&socket_client->lock, flags);
344 wake_up(&socket_client->queue_wait);
347 void bat_socket_receive_packet(struct icmp_packet_rr *icmp_packet,
348 size_t icmp_len)
350 struct socket_client *hash = socket_client_hash[icmp_packet->uid];
352 if (hash)
353 bat_socket_add_packet(hash, icmp_packet, icmp_len);