icmp_socket.c 9.0 KB
Newer Older
1
/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Marek Lindner
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 */

#include "main.h"
#include <linux/debugfs.h>
#include <linux/slab.h>
#include "icmp_socket.h"
#include "send.h"
#include "hash.h"
#include "originator.h"
#include "hard-interface.h"

29
static struct batadv_socket_client *batadv_socket_client_hash[256];
30

31
static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
32
				     struct batadv_icmp_packet_rr *icmp_packet,
33
				     size_t icmp_len);
34

35
void batadv_socket_init(void)
36
{
37
	memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
38 39
}

40
static int batadv_socket_open(struct inode *inode, struct file *file)
41 42
{
	unsigned int i;
43
	struct batadv_socket_client *socket_client;
44 45 46

	nonseekable_open(inode, file);

47
	socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
48 49 50 51

	if (!socket_client)
		return -ENOMEM;

52 53 54
	for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) {
		if (!batadv_socket_client_hash[i]) {
			batadv_socket_client_hash[i] = socket_client;
55 56 57 58
			break;
		}
	}

59
	if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
60
		pr_err("Error - can't add another packet client: maximum number of clients reached\n");
61 62 63 64 65 66 67 68 69 70 71 72 73
		kfree(socket_client);
		return -EXFULL;
	}

	INIT_LIST_HEAD(&socket_client->queue_list);
	socket_client->queue_len = 0;
	socket_client->index = i;
	socket_client->bat_priv = inode->i_private;
	spin_lock_init(&socket_client->lock);
	init_waitqueue_head(&socket_client->queue_wait);

	file->private_data = socket_client;

74
	batadv_inc_module_count();
75 76 77
	return 0;
}

78
static int batadv_socket_release(struct inode *inode, struct file *file)
79
{
80 81
	struct batadv_socket_client *socket_client = file->private_data;
	struct batadv_socket_packet *socket_packet;
82 83 84 85 86 87 88
	struct list_head *list_pos, *list_pos_tmp;

	spin_lock_bh(&socket_client->lock);

	/* for all packets in the queue ... */
	list_for_each_safe(list_pos, list_pos_tmp, &socket_client->queue_list) {
		socket_packet = list_entry(list_pos,
89
					   struct batadv_socket_packet, list);
90 91 92 93 94

		list_del(list_pos);
		kfree(socket_packet);
	}

95
	batadv_socket_client_hash[socket_client->index] = NULL;
96 97 98
	spin_unlock_bh(&socket_client->lock);

	kfree(socket_client);
99
	batadv_dec_module_count();
100 101 102 103

	return 0;
}

104 105
static ssize_t batadv_socket_read(struct file *file, char __user *buf,
				  size_t count, loff_t *ppos)
106
{
107 108
	struct batadv_socket_client *socket_client = file->private_data;
	struct batadv_socket_packet *socket_packet;
109 110 111 112 113 114
	size_t packet_len;
	int error;

	if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0))
		return -EAGAIN;

115
	if ((!buf) || (count < sizeof(struct batadv_icmp_packet)))
116 117 118 119 120 121 122 123 124 125 126 127 128 129
		return -EINVAL;

	if (!access_ok(VERIFY_WRITE, buf, count))
		return -EFAULT;

	error = wait_event_interruptible(socket_client->queue_wait,
					 socket_client->queue_len);

	if (error)
		return error;

	spin_lock_bh(&socket_client->lock);

	socket_packet = list_first_entry(&socket_client->queue_list,
130
					 struct batadv_socket_packet, list);
131 132 133 134 135
	list_del(&socket_packet->list);
	socket_client->queue_len--;

	spin_unlock_bh(&socket_client->lock);

136 137
	packet_len = min(count, socket_packet->icmp_len);
	error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
138 139 140 141 142 143 144 145 146

	kfree(socket_packet);

	if (error)
		return -EFAULT;

	return packet_len;
}

147 148
static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
				   size_t len, loff_t *off)
149
{
150 151 152
	struct batadv_socket_client *socket_client = file->private_data;
	struct batadv_priv *bat_priv = socket_client->bat_priv;
	struct batadv_hard_iface *primary_if = NULL;
153
	struct sk_buff *skb;
154
	struct batadv_icmp_packet_rr *icmp_packet;
155

156 157
	struct batadv_orig_node *orig_node = NULL;
	struct batadv_neigh_node *neigh_node = NULL;
158
	size_t packet_len = sizeof(struct batadv_icmp_packet);
159

160
	if (len < sizeof(struct batadv_icmp_packet)) {
161
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
162
			   "Error - can't send packet from char device: invalid packet size\n");
163 164 165
		return -EINVAL;
	}

166
	primary_if = batadv_primary_if_get_selected(bat_priv);
167 168 169 170 171

	if (!primary_if) {
		len = -EFAULT;
		goto out;
	}
172

173 174
	if (len >= sizeof(struct batadv_icmp_packet_rr))
		packet_len = sizeof(struct batadv_icmp_packet_rr);
175

176
	skb = dev_alloc_skb(packet_len + ETH_HLEN);
177 178 179 180
	if (!skb) {
		len = -ENOMEM;
		goto out;
	}
181

182
	skb_reserve(skb, ETH_HLEN);
183
	icmp_packet = (struct batadv_icmp_packet_rr *)skb_put(skb, packet_len);
184

185
	if (copy_from_user(icmp_packet, buff, packet_len)) {
186 187 188 189
		len = -EFAULT;
		goto free_skb;
	}

190
	if (icmp_packet->header.packet_type != BATADV_ICMP) {
191
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
192
			   "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
193 194 195 196
		len = -EINVAL;
		goto free_skb;
	}

197
	if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
198
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
199
			   "Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
200 201 202 203 204 205
		len = -EINVAL;
		goto free_skb;
	}

	icmp_packet->uid = socket_client->index;

206
	if (icmp_packet->header.version != BATADV_COMPAT_VERSION) {
207
		icmp_packet->msg_type = BATADV_PARAMETER_PROBLEM;
208
		icmp_packet->header.version = BATADV_COMPAT_VERSION;
209 210
		batadv_socket_add_packet(socket_client, icmp_packet,
					 packet_len);
211 212 213
		goto free_skb;
	}

214
	if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
215 216
		goto dst_unreach;

217
	orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
218
	if (!orig_node)
219
		goto dst_unreach;
220

221
	neigh_node = batadv_orig_node_get_router(orig_node);
222
	if (!neigh_node)
223
		goto dst_unreach;
224

225
	if (!neigh_node->if_incoming)
226 227
		goto dst_unreach;

228
	if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
229 230 231
		goto dst_unreach;

	memcpy(icmp_packet->orig,
232
	       primary_if->net_dev->dev_addr, ETH_ALEN);
233

234
	if (packet_len == sizeof(struct batadv_icmp_packet_rr))
235
		memcpy(icmp_packet->rr,
236
		       neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN);
237

238
	batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
239 240 241
	goto out;

dst_unreach:
242
	icmp_packet->msg_type = BATADV_DESTINATION_UNREACHABLE;
243
	batadv_socket_add_packet(socket_client, icmp_packet, packet_len);
244 245 246
free_skb:
	kfree_skb(skb);
out:
247
	if (primary_if)
248
		batadv_hardif_free_ref(primary_if);
249
	if (neigh_node)
250
		batadv_neigh_node_free_ref(neigh_node);
251
	if (orig_node)
252
		batadv_orig_node_free_ref(orig_node);
253 254 255
	return len;
}

256
static unsigned int batadv_socket_poll(struct file *file, poll_table *wait)
257
{
258
	struct batadv_socket_client *socket_client = file->private_data;
259 260 261 262 263 264 265 266 267

	poll_wait(file, &socket_client->queue_wait, wait);

	if (socket_client->queue_len > 0)
		return POLLIN | POLLRDNORM;

	return 0;
}

268
static const struct file_operations batadv_fops = {
269
	.owner = THIS_MODULE,
270 271 272 273 274
	.open = batadv_socket_open,
	.release = batadv_socket_release,
	.read = batadv_socket_read,
	.write = batadv_socket_write,
	.poll = batadv_socket_poll,
275 276 277
	.llseek = no_llseek,
};

278
int batadv_socket_setup(struct batadv_priv *bat_priv)
279 280 281 282 283 284
{
	struct dentry *d;

	if (!bat_priv->debug_dir)
		goto err;

285
	d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
286
				bat_priv->debug_dir, bat_priv, &batadv_fops);
287
	if (!d)
288 289 290 291 292
		goto err;

	return 0;

err:
293
	return -ENOMEM;
294 295
}

296
static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
297
				     struct batadv_icmp_packet_rr *icmp_packet,
298
				     size_t icmp_len)
299
{
300
	struct batadv_socket_packet *socket_packet;
301

302
	socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
303 304 305 306 307 308 309 310 311 312 313

	if (!socket_packet)
		return;

	INIT_LIST_HEAD(&socket_packet->list);
	memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
	socket_packet->icmp_len = icmp_len;

	spin_lock_bh(&socket_client->lock);

	/* while waiting for the lock the socket_client could have been
314 315
	 * deleted
	 */
316
	if (!batadv_socket_client_hash[icmp_packet->uid]) {
317 318 319 320 321 322 323 324 325 326
		spin_unlock_bh(&socket_client->lock);
		kfree(socket_packet);
		return;
	}

	list_add_tail(&socket_packet->list, &socket_client->queue_list);
	socket_client->queue_len++;

	if (socket_client->queue_len > 100) {
		socket_packet = list_first_entry(&socket_client->queue_list,
327 328
						 struct batadv_socket_packet,
						 list);
329 330 331 332 333 334 335 336 337 338 339

		list_del(&socket_packet->list);
		kfree(socket_packet);
		socket_client->queue_len--;
	}

	spin_unlock_bh(&socket_client->lock);

	wake_up(&socket_client->queue_wait);
}

340
void batadv_socket_receive_packet(struct batadv_icmp_packet_rr *icmp_packet,
341
				  size_t icmp_len)
342
{
343
	struct batadv_socket_client *hash;
344

345
	hash = batadv_socket_client_hash[icmp_packet->uid];
346
	if (hash)
347
		batadv_socket_add_packet(hash, icmp_packet, icmp_len);
348
}