blob: 21c001074ebe616c19fb330f802a98327bd227c3 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann567db7b2012-01-01 00:41:38 +01002 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
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"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000027#include "hash.h"
28#include "originator.h"
29#include "hard-interface.h"
30
31static struct socket_client *socket_client_hash[256];
32
33static void bat_socket_add_packet(struct socket_client *socket_client,
34 struct icmp_packet_rr *icmp_packet,
35 size_t icmp_len);
36
Sven Eckelmann9039dc72012-05-12 02:09:33 +020037void batadv_socket_init(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038{
39 memset(socket_client_hash, 0, sizeof(socket_client_hash));
40}
41
42static 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
Sven Eckelmann704509b2011-05-14 23:14:54 +020049 socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000050
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)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +010062 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000063 kfree(socket_client);
64 return -EXFULL;
65 }
66
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);
73
74 file->private_data = socket_client;
75
76 inc_module_count();
77 return 0;
78}
79
80static int bat_socket_release(struct inode *inode, struct file *file)
81{
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
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,
91 struct socket_packet, list);
92
93 list_del(list_pos);
94 kfree(socket_packet);
95 }
96
97 socket_client_hash[socket_client->index] = NULL;
98 spin_unlock_bh(&socket_client->lock);
99
100 kfree(socket_client);
101 dec_module_count();
102
103 return 0;
104}
105
106static ssize_t bat_socket_read(struct file *file, char __user *buf,
107 size_t count, loff_t *ppos)
108{
109 struct socket_client *socket_client = file->private_data;
110 struct socket_packet *socket_packet;
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
117 if ((!buf) || (count < sizeof(struct icmp_packet)))
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,
132 struct socket_packet, list);
133 list_del(&socket_packet->list);
134 socket_client->queue_len--;
135
136 spin_unlock_bh(&socket_client->lock);
137
Sven Eckelmannb5a1eee2011-12-10 15:28:36 +0100138 packet_len = min(count, socket_packet->icmp_len);
139 error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000140
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000141 kfree(socket_packet);
142
143 if (error)
144 return -EFAULT;
145
146 return packet_len;
147}
148
149static ssize_t bat_socket_write(struct file *file, const char __user *buff,
150 size_t len, loff_t *off)
151{
152 struct socket_client *socket_client = file->private_data;
153 struct bat_priv *bat_priv = socket_client->bat_priv;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200154 struct hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155 struct sk_buff *skb;
156 struct icmp_packet_rr *icmp_packet;
157
Marek Lindner44524fc2011-02-10 14:33:53 +0000158 struct orig_node *orig_node = NULL;
159 struct neigh_node *neigh_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000160 size_t packet_len = sizeof(struct icmp_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000161
162 if (len < sizeof(struct icmp_packet)) {
163 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100164 "Error - can't send packet from char device: invalid packet size\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000165 return -EINVAL;
166 }
167
Marek Lindner32ae9b22011-04-20 15:40:58 +0200168 primary_if = primary_if_get_selected(bat_priv);
169
170 if (!primary_if) {
171 len = -EFAULT;
172 goto out;
173 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000174
175 if (len >= sizeof(struct icmp_packet_rr))
176 packet_len = sizeof(struct icmp_packet_rr);
177
Antonio Quartulli0d125072012-02-18 11:27:34 +0100178 skb = dev_alloc_skb(packet_len + ETH_HLEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200179 if (!skb) {
180 len = -ENOMEM;
181 goto out;
182 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183
Antonio Quartulli0d125072012-02-18 11:27:34 +0100184 skb_reserve(skb, ETH_HLEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185 icmp_packet = (struct icmp_packet_rr *)skb_put(skb, packet_len);
186
Sven Eckelmannd18eb452011-12-10 15:28:35 +0100187 if (copy_from_user(icmp_packet, buff, packet_len)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188 len = -EFAULT;
189 goto free_skb;
190 }
191
Sven Eckelmann76543d12011-11-20 15:47:38 +0100192 if (icmp_packet->header.packet_type != BAT_ICMP) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000193 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100194 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000195 len = -EINVAL;
196 goto free_skb;
197 }
198
199 if (icmp_packet->msg_type != ECHO_REQUEST) {
200 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100201 "Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000202 len = -EINVAL;
203 goto free_skb;
204 }
205
206 icmp_packet->uid = socket_client->index;
207
Sven Eckelmann76543d12011-11-20 15:47:38 +0100208 if (icmp_packet->header.version != COMPAT_VERSION) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209 icmp_packet->msg_type = PARAMETER_PROBLEM;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100210 icmp_packet->header.version = COMPAT_VERSION;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000211 bat_socket_add_packet(socket_client, icmp_packet, packet_len);
212 goto free_skb;
213 }
214
215 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
216 goto dst_unreach;
217
Marek Lindner7aadf882011-02-18 12:28:09 +0000218 orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000219 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000220 goto dst_unreach;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000221
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200222 neigh_node = batadv_orig_node_get_router(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000223 if (!neigh_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000224 goto dst_unreach;
Marek Lindner44524fc2011-02-10 14:33:53 +0000225
Marek Lindnerd0072602011-01-19 20:01:44 +0000226 if (!neigh_node->if_incoming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227 goto dst_unreach;
228
Marek Lindnerd0072602011-01-19 20:01:44 +0000229 if (neigh_node->if_incoming->if_status != IF_ACTIVE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230 goto dst_unreach;
231
232 memcpy(icmp_packet->orig,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200233 primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234
235 if (packet_len == sizeof(struct icmp_packet_rr))
Marek Lindner44524fc2011-02-10 14:33:53 +0000236 memcpy(icmp_packet->rr,
Marek Lindnerd0072602011-01-19 20:01:44 +0000237 neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000238
Sven Eckelmann9455e342012-05-12 02:09:37 +0200239 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000240 goto out;
241
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000242dst_unreach:
243 icmp_packet->msg_type = DESTINATION_UNREACHABLE;
244 bat_socket_add_packet(socket_client, icmp_packet, packet_len);
245free_skb:
246 kfree_skb(skb);
247out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200248 if (primary_if)
249 hardif_free_ref(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000250 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200251 batadv_neigh_node_free_ref(neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000252 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200253 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254 return len;
255}
256
257static unsigned int bat_socket_poll(struct file *file, poll_table *wait)
258{
259 struct socket_client *socket_client = file->private_data;
260
261 poll_wait(file, &socket_client->queue_wait, wait);
262
263 if (socket_client->queue_len > 0)
264 return POLLIN | POLLRDNORM;
265
266 return 0;
267}
268
269static 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,
277};
278
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200279int batadv_socket_setup(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280{
281 struct dentry *d;
282
283 if (!bat_priv->debug_dir)
284 goto err;
285
286 d = debugfs_create_file(ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
287 bat_priv->debug_dir, bat_priv, &fops);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200288 if (!d)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289 goto err;
290
291 return 0;
292
293err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200294 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000295}
296
297static void bat_socket_add_packet(struct socket_client *socket_client,
298 struct icmp_packet_rr *icmp_packet,
299 size_t icmp_len)
300{
301 struct socket_packet *socket_packet;
302
Sven Eckelmann704509b2011-05-14 23:14:54 +0200303 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304
305 if (!socket_packet)
306 return;
307
308 INIT_LIST_HEAD(&socket_packet->list);
309 memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
310 socket_packet->icmp_len = icmp_len;
311
312 spin_lock_bh(&socket_client->lock);
313
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;
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
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200339void batadv_socket_receive_packet(struct icmp_packet_rr *icmp_packet,
340 size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341{
342 struct socket_client *hash = socket_client_hash[icmp_packet->uid];
343
344 if (hash)
345 bat_socket_add_packet(hash, icmp_packet, icmp_len);
346}