icmp_socket.c 10.6 KB
Newer Older
1
/* Copyright (C) 2007-2016  B.A.T.M.A.N. contributors:
2 3 4 5 6 7 8 9 10 11 12 13 14
 *
 * 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
15
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 17
 */

18
#include "icmp_socket.h"
19
#include "main.h"
20 21 22

#include <linux/atomic.h>
#include <linux/compiler.h>
23
#include <linux/debugfs.h>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <linux/errno.h>
#include <linux/etherdevice.h>
#include <linux/export.h>
#include <linux/fcntl.h>
#include <linux/fs.h>
#include <linux/if_ether.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/pkt_sched.h>
#include <linux/poll.h>
#include <linux/printk.h>
#include <linux/sched.h> /* for linux/wait.h */
#include <linux/skbuff.h>
39
#include <linux/slab.h>
40 41 42 43 44 45 46
#include <linux/spinlock.h>
#include <linux/stat.h>
#include <linux/stddef.h>
#include <linux/string.h>
#include <linux/uaccess.h>
#include <linux/wait.h>

47
#include "hard-interface.h"
48 49 50
#include "originator.h"
#include "packet.h"
#include "send.h"
51

52
static struct batadv_socket_client *batadv_socket_client_hash[256];
53

54
static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
55
				     struct batadv_icmp_header *icmph,
56
				     size_t icmp_len);
57

58
void batadv_socket_init(void)
59
{
60
	memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
61 62
}

63
static int batadv_socket_open(struct inode *inode, struct file *file)
64 65
{
	unsigned int i;
66
	struct batadv_socket_client *socket_client;
67

68 69 70
	if (!try_module_get(THIS_MODULE))
		return -EBUSY;

71 72
	nonseekable_open(inode, file);

73
	socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
74 75
	if (!socket_client) {
		module_put(THIS_MODULE);
76
		return -ENOMEM;
77
	}
78

79 80 81
	for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) {
		if (!batadv_socket_client_hash[i]) {
			batadv_socket_client_hash[i] = socket_client;
82 83 84 85
			break;
		}
	}

86
	if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
87
		pr_err("Error - can't add another packet client: maximum number of clients reached\n");
88
		kfree(socket_client);
89
		module_put(THIS_MODULE);
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
		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;

	return 0;
}

105
static int batadv_socket_release(struct inode *inode, struct file *file)
106
{
107 108
	struct batadv_socket_client *socket_client = file->private_data;
	struct batadv_socket_packet *socket_packet;
109 110 111 112 113 114 115
	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,
116
					   struct batadv_socket_packet, list);
117 118 119 120 121

		list_del(list_pos);
		kfree(socket_packet);
	}

122
	batadv_socket_client_hash[socket_client->index] = NULL;
123 124 125
	spin_unlock_bh(&socket_client->lock);

	kfree(socket_client);
126
	module_put(THIS_MODULE);
127 128 129 130

	return 0;
}

131 132
static ssize_t batadv_socket_read(struct file *file, char __user *buf,
				  size_t count, loff_t *ppos)
133
{
134 135
	struct batadv_socket_client *socket_client = file->private_data;
	struct batadv_socket_packet *socket_packet;
136 137 138 139 140 141
	size_t packet_len;
	int error;

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

142
	if ((!buf) || (count < sizeof(struct batadv_icmp_packet)))
143 144 145 146 147 148 149 150 151 152 153 154 155 156
		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,
157
					 struct batadv_socket_packet, list);
158 159 160 161 162
	list_del(&socket_packet->list);
	socket_client->queue_len--;

	spin_unlock_bh(&socket_client->lock);

163 164
	packet_len = min(count, socket_packet->icmp_len);
	error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
165 166 167 168 169 170 171 172 173

	kfree(socket_packet);

	if (error)
		return -EFAULT;

	return packet_len;
}

174 175
static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
				   size_t len, loff_t *off)
176
{
177 178 179
	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;
180
	struct sk_buff *skb;
181 182
	struct batadv_icmp_packet_rr *icmp_packet_rr;
	struct batadv_icmp_header *icmp_header;
183 184
	struct batadv_orig_node *orig_node = NULL;
	struct batadv_neigh_node *neigh_node = NULL;
185
	size_t packet_len = sizeof(struct batadv_icmp_packet);
186
	u8 *addr;
187

188
	if (len < sizeof(struct batadv_icmp_header)) {
189
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
190
			   "Error - can't send packet from char device: invalid packet size\n");
191 192 193
		return -EINVAL;
	}

194
	primary_if = batadv_primary_if_get_selected(bat_priv);
195 196 197 198 199

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

201 202 203 204
	if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
		packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
	else
		packet_len = len;
205

206
	skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
207 208 209 210
	if (!skb) {
		len = -ENOMEM;
		goto out;
	}
211

212
	skb->priority = TC_PRIO_CONTROL;
213
	skb_reserve(skb, ETH_HLEN);
214
	icmp_header = (struct batadv_icmp_header *)skb_put(skb, packet_len);
215

216
	if (copy_from_user(icmp_header, buff, packet_len)) {
217 218 219 220
		len = -EFAULT;
		goto free_skb;
	}

221
	if (icmp_header->packet_type != BATADV_ICMP) {
222
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
223
			   "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
224 225 226 227
		len = -EINVAL;
		goto free_skb;
	}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	switch (icmp_header->msg_type) {
	case BATADV_ECHO_REQUEST:
		if (len < sizeof(struct batadv_icmp_packet)) {
			batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
				   "Error - can't send packet from char device: invalid packet size\n");
			len = -EINVAL;
			goto free_skb;
		}

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

		orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
		if (!orig_node)
			goto dst_unreach;

244 245
		neigh_node = batadv_orig_router_get(orig_node,
						    BATADV_IF_DEFAULT);
246 247 248 249 250 251 252 253 254 255
		if (!neigh_node)
			goto dst_unreach;

		if (!neigh_node->if_incoming)
			goto dst_unreach;

		if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
			goto dst_unreach;

		icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header;
256 257 258 259
		if (packet_len == sizeof(*icmp_packet_rr)) {
			addr = neigh_node->if_incoming->net_dev->dev_addr;
			ether_addr_copy(icmp_packet_rr->rr[0], addr);
		}
260 261 262

		break;
	default:
263
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
264
			   "Error - can't send packet from char device: got unknown message type\n");
265 266 267 268
		len = -EINVAL;
		goto free_skb;
	}

269
	icmp_header->uid = socket_client->index;
270

271
	if (icmp_header->version != BATADV_COMPAT_VERSION) {
272
		icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
273
		icmp_header->version = BATADV_COMPAT_VERSION;
274
		batadv_socket_add_packet(socket_client, icmp_header,
275
					 packet_len);
276 277 278
		goto free_skb;
	}

279
	ether_addr_copy(icmp_header->orig, primary_if->net_dev->dev_addr);
280

281
	batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
282 283 284
	goto out;

dst_unreach:
285 286
	icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
	batadv_socket_add_packet(socket_client, icmp_header, packet_len);
287 288 289
free_skb:
	kfree_skb(skb);
out:
290
	if (primary_if)
291
		batadv_hardif_put(primary_if);
292
	if (neigh_node)
293
		batadv_neigh_node_free_ref(neigh_node);
294
	if (orig_node)
295
		batadv_orig_node_put(orig_node);
296 297 298
	return len;
}

299
static unsigned int batadv_socket_poll(struct file *file, poll_table *wait)
300
{
301
	struct batadv_socket_client *socket_client = file->private_data;
302 303 304 305 306 307 308 309 310

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

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

	return 0;
}

311
static const struct file_operations batadv_fops = {
312
	.owner = THIS_MODULE,
313 314 315 316 317
	.open = batadv_socket_open,
	.release = batadv_socket_release,
	.read = batadv_socket_read,
	.write = batadv_socket_write,
	.poll = batadv_socket_poll,
318 319 320
	.llseek = no_llseek,
};

321
int batadv_socket_setup(struct batadv_priv *bat_priv)
322 323 324 325 326 327
{
	struct dentry *d;

	if (!bat_priv->debug_dir)
		goto err;

328
	d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
329
				bat_priv->debug_dir, bat_priv, &batadv_fops);
330
	if (!d)
331 332 333 334 335
		goto err;

	return 0;

err:
336
	return -ENOMEM;
337 338
}

339
/**
340 341
 * batadv_socket_receive_packet - schedule an icmp packet to be sent to
 *  userspace on an icmp socket.
342 343 344 345
 * @socket_client: the socket this packet belongs to
 * @icmph: pointer to the header of the icmp packet
 * @icmp_len: total length of the icmp packet
 */
346
static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
347
				     struct batadv_icmp_header *icmph,
348
				     size_t icmp_len)
349
{
350
	struct batadv_socket_packet *socket_packet;
351
	size_t len;
352

353
	socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
354 355 356 357

	if (!socket_packet)
		return;

358 359 360 361 362
	len = icmp_len;
	/* check the maximum length before filling the buffer */
	if (len > sizeof(socket_packet->icmp_packet))
		len = sizeof(socket_packet->icmp_packet);

363
	INIT_LIST_HEAD(&socket_packet->list);
364 365
	memcpy(&socket_packet->icmp_packet, icmph, len);
	socket_packet->icmp_len = len;
366 367 368 369

	spin_lock_bh(&socket_client->lock);

	/* while waiting for the lock the socket_client could have been
370 371
	 * deleted
	 */
372
	if (!batadv_socket_client_hash[icmph->uid]) {
373 374 375 376 377 378 379 380 381 382
		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,
383 384
						 struct batadv_socket_packet,
						 list);
385 386 387 388 389 390 391 392 393 394 395

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

	spin_unlock_bh(&socket_client->lock);

	wake_up(&socket_client->queue_wait);
}

396 397 398 399 400 401 402
/**
 * batadv_socket_receive_packet - schedule an icmp packet to be received
 *  locally and sent to userspace.
 * @icmph: pointer to the header of the icmp packet
 * @icmp_len: total length of the icmp packet
 */
void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
403
				  size_t icmp_len)
404
{
405
	struct batadv_socket_client *hash;
406

407
	hash = batadv_socket_client_hash[icmph->uid];
408
	if (hash)
409
		batadv_socket_add_packet(hash, icmph, icmp_len);
410
}