2012-05-12 08:09:43 +08:00
|
|
|
/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
|
2010-12-13 19:19:28 +08:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2012-06-06 04:31:31 +08:00
|
|
|
static struct batadv_socket_client *batadv_socket_client_hash[256];
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2012-06-06 04:31:31 +08:00
|
|
|
static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
|
2012-06-06 04:31:30 +08:00
|
|
|
struct batadv_icmp_packet_rr *icmp_packet,
|
2012-05-13 00:33:59 +08:00
|
|
|
size_t icmp_len);
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2012-05-12 08:09:33 +08:00
|
|
|
void batadv_socket_init(void)
|
2010-12-13 19:19:28 +08:00
|
|
|
{
|
2012-05-13 00:33:59 +08:00
|
|
|
memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
|
2010-12-13 19:19:28 +08:00
|
|
|
}
|
|
|
|
|
2012-05-13 00:33:59 +08:00
|
|
|
static int batadv_socket_open(struct inode *inode, struct file *file)
|
2010-12-13 19:19:28 +08:00
|
|
|
{
|
|
|
|
unsigned int i;
|
2012-06-06 04:31:31 +08:00
|
|
|
struct batadv_socket_client *socket_client;
|
2010-12-13 19:19:28 +08:00
|
|
|
|
|
|
|
nonseekable_open(inode, file);
|
|
|
|
|
2011-05-15 05:14:54 +08:00
|
|
|
socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
|
2010-12-13 19:19:28 +08:00
|
|
|
|
|
|
|
if (!socket_client)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2012-05-13 00:33:59 +08:00
|
|
|
for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) {
|
|
|
|
if (!batadv_socket_client_hash[i]) {
|
|
|
|
batadv_socket_client_hash[i] = socket_client;
|
2010-12-13 19:19:28 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-13 00:33:59 +08:00
|
|
|
if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
|
2012-03-07 16:07:45 +08:00
|
|
|
pr_err("Error - can't add another packet client: maximum number of clients reached\n");
|
2010-12-13 19:19:28 +08:00
|
|
|
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;
|
|
|
|
|
2012-05-12 08:09:42 +08:00
|
|
|
batadv_inc_module_count();
|
2010-12-13 19:19:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-13 00:33:59 +08:00
|
|
|
static int batadv_socket_release(struct inode *inode, struct file *file)
|
2010-12-13 19:19:28 +08:00
|
|
|
{
|
2012-06-06 04:31:31 +08:00
|
|
|
struct batadv_socket_client *socket_client = file->private_data;
|
|
|
|
struct batadv_socket_packet *socket_packet;
|
2010-12-13 19:19:28 +08:00
|
|
|
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,
|
2012-06-06 04:31:31 +08:00
|
|
|
struct batadv_socket_packet, list);
|
2010-12-13 19:19:28 +08:00
|
|
|
|
|
|
|
list_del(list_pos);
|
|
|
|
kfree(socket_packet);
|
|
|
|
}
|
|
|
|
|
2012-05-13 00:33:59 +08:00
|
|
|
batadv_socket_client_hash[socket_client->index] = NULL;
|
2010-12-13 19:19:28 +08:00
|
|
|
spin_unlock_bh(&socket_client->lock);
|
|
|
|
|
|
|
|
kfree(socket_client);
|
2012-05-12 08:09:42 +08:00
|
|
|
batadv_dec_module_count();
|
2010-12-13 19:19:28 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-13 00:33:59 +08:00
|
|
|
static ssize_t batadv_socket_read(struct file *file, char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
2010-12-13 19:19:28 +08:00
|
|
|
{
|
2012-06-06 04:31:31 +08:00
|
|
|
struct batadv_socket_client *socket_client = file->private_data;
|
|
|
|
struct batadv_socket_packet *socket_packet;
|
2010-12-13 19:19:28 +08:00
|
|
|
size_t packet_len;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0))
|
|
|
|
return -EAGAIN;
|
|
|
|
|
2012-06-06 04:31:30 +08:00
|
|
|
if ((!buf) || (count < sizeof(struct batadv_icmp_packet)))
|
2010-12-13 19:19:28 +08:00
|
|
|
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,
|
2012-06-06 04:31:31 +08:00
|
|
|
struct batadv_socket_packet, list);
|
2010-12-13 19:19:28 +08:00
|
|
|
list_del(&socket_packet->list);
|
|
|
|
socket_client->queue_len--;
|
|
|
|
|
|
|
|
spin_unlock_bh(&socket_client->lock);
|
|
|
|
|
2011-12-10 22:28:36 +08:00
|
|
|
packet_len = min(count, socket_packet->icmp_len);
|
|
|
|
error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
|
2010-12-13 19:19:28 +08:00
|
|
|
|
|
|
|
kfree(socket_packet);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return packet_len;
|
|
|
|
}
|
|
|
|
|
2012-05-13 00:33:59 +08:00
|
|
|
static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
|
|
|
|
size_t len, loff_t *off)
|
2010-12-13 19:19:28 +08:00
|
|
|
{
|
2012-06-06 04:31:31 +08:00
|
|
|
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;
|
2010-12-13 19:19:28 +08:00
|
|
|
struct sk_buff *skb;
|
2012-06-06 04:31:30 +08:00
|
|
|
struct batadv_icmp_packet_rr *icmp_packet;
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2012-06-06 04:31:31 +08:00
|
|
|
struct batadv_orig_node *orig_node = NULL;
|
|
|
|
struct batadv_neigh_node *neigh_node = NULL;
|
2012-06-06 04:31:30 +08:00
|
|
|
size_t packet_len = sizeof(struct batadv_icmp_packet);
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2012-06-06 04:31:30 +08:00
|
|
|
if (len < sizeof(struct batadv_icmp_packet)) {
|
2012-06-04 04:19:22 +08:00
|
|
|
batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
|
2012-05-12 19:48:58 +08:00
|
|
|
"Error - can't send packet from char device: invalid packet size\n");
|
2010-12-13 19:19:28 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-05-12 19:48:54 +08:00
|
|
|
primary_if = batadv_primary_if_get_selected(bat_priv);
|
2011-04-20 21:40:58 +08:00
|
|
|
|
|
|
|
if (!primary_if) {
|
|
|
|
len = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2012-06-06 04:31:30 +08:00
|
|
|
if (len >= sizeof(struct batadv_icmp_packet_rr))
|
|
|
|
packet_len = sizeof(struct batadv_icmp_packet_rr);
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2012-02-18 18:27:34 +08:00
|
|
|
skb = dev_alloc_skb(packet_len + ETH_HLEN);
|
2011-04-20 21:40:58 +08:00
|
|
|
if (!skb) {
|
|
|
|
len = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2012-02-18 18:27:34 +08:00
|
|
|
skb_reserve(skb, ETH_HLEN);
|
2012-06-06 04:31:30 +08:00
|
|
|
icmp_packet = (struct batadv_icmp_packet_rr *)skb_put(skb, packet_len);
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2011-12-10 22:28:35 +08:00
|
|
|
if (copy_from_user(icmp_packet, buff, packet_len)) {
|
2010-12-13 19:19:28 +08:00
|
|
|
len = -EFAULT;
|
|
|
|
goto free_skb;
|
|
|
|
}
|
|
|
|
|
2012-06-04 04:19:21 +08:00
|
|
|
if (icmp_packet->header.packet_type != BATADV_ICMP) {
|
2012-06-04 04:19:22 +08:00
|
|
|
batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
|
2012-05-12 19:48:58 +08:00
|
|
|
"Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
|
2010-12-13 19:19:28 +08:00
|
|
|
len = -EINVAL;
|
|
|
|
goto free_skb;
|
|
|
|
}
|
|
|
|
|
2012-06-04 04:19:21 +08:00
|
|
|
if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
|
2012-06-04 04:19:22 +08:00
|
|
|
batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
|
2012-05-12 19:48:58 +08:00
|
|
|
"Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
|
2010-12-13 19:19:28 +08:00
|
|
|
len = -EINVAL;
|
|
|
|
goto free_skb;
|
|
|
|
}
|
|
|
|
|
|
|
|
icmp_packet->uid = socket_client->index;
|
|
|
|
|
2012-06-04 04:19:13 +08:00
|
|
|
if (icmp_packet->header.version != BATADV_COMPAT_VERSION) {
|
2012-06-04 04:19:21 +08:00
|
|
|
icmp_packet->msg_type = BATADV_PARAMETER_PROBLEM;
|
2012-06-04 04:19:13 +08:00
|
|
|
icmp_packet->header.version = BATADV_COMPAT_VERSION;
|
2012-05-13 00:33:59 +08:00
|
|
|
batadv_socket_add_packet(socket_client, icmp_packet,
|
|
|
|
packet_len);
|
2010-12-13 19:19:28 +08:00
|
|
|
goto free_skb;
|
|
|
|
}
|
|
|
|
|
2012-06-04 04:19:22 +08:00
|
|
|
if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
|
2010-12-13 19:19:28 +08:00
|
|
|
goto dst_unreach;
|
|
|
|
|
2012-05-12 19:48:56 +08:00
|
|
|
orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
|
2010-12-13 19:19:28 +08:00
|
|
|
if (!orig_node)
|
2011-03-15 06:43:37 +08:00
|
|
|
goto dst_unreach;
|
2011-02-10 22:33:53 +08:00
|
|
|
|
2012-05-12 08:09:34 +08:00
|
|
|
neigh_node = batadv_orig_node_get_router(orig_node);
|
2011-02-10 22:33:53 +08:00
|
|
|
if (!neigh_node)
|
2011-03-15 06:43:37 +08:00
|
|
|
goto dst_unreach;
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2011-01-20 04:01:44 +08:00
|
|
|
if (!neigh_node->if_incoming)
|
2010-12-13 19:19:28 +08:00
|
|
|
goto dst_unreach;
|
|
|
|
|
2012-06-04 04:19:19 +08:00
|
|
|
if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
|
2010-12-13 19:19:28 +08:00
|
|
|
goto dst_unreach;
|
|
|
|
|
|
|
|
memcpy(icmp_packet->orig,
|
2011-04-20 21:40:58 +08:00
|
|
|
primary_if->net_dev->dev_addr, ETH_ALEN);
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2012-06-06 04:31:30 +08:00
|
|
|
if (packet_len == sizeof(struct batadv_icmp_packet_rr))
|
2011-02-10 22:33:53 +08:00
|
|
|
memcpy(icmp_packet->rr,
|
2011-01-20 04:01:44 +08:00
|
|
|
neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN);
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2012-05-12 08:09:37 +08:00
|
|
|
batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
|
2010-12-13 19:19:28 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
dst_unreach:
|
2012-06-04 04:19:21 +08:00
|
|
|
icmp_packet->msg_type = BATADV_DESTINATION_UNREACHABLE;
|
2012-05-13 00:33:59 +08:00
|
|
|
batadv_socket_add_packet(socket_client, icmp_packet, packet_len);
|
2010-12-13 19:19:28 +08:00
|
|
|
free_skb:
|
|
|
|
kfree_skb(skb);
|
|
|
|
out:
|
2011-04-20 21:40:58 +08:00
|
|
|
if (primary_if)
|
2012-05-12 19:48:54 +08:00
|
|
|
batadv_hardif_free_ref(primary_if);
|
2011-02-10 22:33:53 +08:00
|
|
|
if (neigh_node)
|
2012-05-12 08:09:34 +08:00
|
|
|
batadv_neigh_node_free_ref(neigh_node);
|
2011-02-10 22:33:53 +08:00
|
|
|
if (orig_node)
|
2012-05-12 08:09:34 +08:00
|
|
|
batadv_orig_node_free_ref(orig_node);
|
2010-12-13 19:19:28 +08:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2012-05-13 00:33:59 +08:00
|
|
|
static unsigned int batadv_socket_poll(struct file *file, poll_table *wait)
|
2010-12-13 19:19:28 +08:00
|
|
|
{
|
2012-06-06 04:31:31 +08:00
|
|
|
struct batadv_socket_client *socket_client = file->private_data;
|
2010-12-13 19:19:28 +08:00
|
|
|
|
|
|
|
poll_wait(file, &socket_client->queue_wait, wait);
|
|
|
|
|
|
|
|
if (socket_client->queue_len > 0)
|
|
|
|
return POLLIN | POLLRDNORM;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-13 00:33:59 +08:00
|
|
|
static const struct file_operations batadv_fops = {
|
2010-12-13 19:19:28 +08:00
|
|
|
.owner = THIS_MODULE,
|
2012-05-13 00:33:59 +08:00
|
|
|
.open = batadv_socket_open,
|
|
|
|
.release = batadv_socket_release,
|
|
|
|
.read = batadv_socket_read,
|
|
|
|
.write = batadv_socket_write,
|
|
|
|
.poll = batadv_socket_poll,
|
2010-12-13 19:19:28 +08:00
|
|
|
.llseek = no_llseek,
|
|
|
|
};
|
|
|
|
|
2012-06-06 04:31:31 +08:00
|
|
|
int batadv_socket_setup(struct batadv_priv *bat_priv)
|
2010-12-13 19:19:28 +08:00
|
|
|
{
|
|
|
|
struct dentry *d;
|
|
|
|
|
|
|
|
if (!bat_priv->debug_dir)
|
|
|
|
goto err;
|
|
|
|
|
2012-06-04 04:19:12 +08:00
|
|
|
d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
|
2012-05-13 00:33:59 +08:00
|
|
|
bat_priv->debug_dir, bat_priv, &batadv_fops);
|
2012-05-05 19:27:28 +08:00
|
|
|
if (!d)
|
2010-12-13 19:19:28 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
2012-05-05 19:27:28 +08:00
|
|
|
return -ENOMEM;
|
2010-12-13 19:19:28 +08:00
|
|
|
}
|
|
|
|
|
2012-06-06 04:31:31 +08:00
|
|
|
static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
|
2012-06-06 04:31:30 +08:00
|
|
|
struct batadv_icmp_packet_rr *icmp_packet,
|
2012-05-13 00:33:59 +08:00
|
|
|
size_t icmp_len)
|
2010-12-13 19:19:28 +08:00
|
|
|
{
|
2012-06-06 04:31:31 +08:00
|
|
|
struct batadv_socket_packet *socket_packet;
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2011-05-15 05:14:54 +08:00
|
|
|
socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
|
2010-12-13 19:19:28 +08:00
|
|
|
|
|
|
|
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
|
2012-05-12 08:09:43 +08:00
|
|
|
* deleted
|
|
|
|
*/
|
2012-05-13 00:33:59 +08:00
|
|
|
if (!batadv_socket_client_hash[icmp_packet->uid]) {
|
2010-12-13 19:19:28 +08:00
|
|
|
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,
|
2012-06-06 04:31:31 +08:00
|
|
|
struct batadv_socket_packet,
|
|
|
|
list);
|
2010-12-13 19:19:28 +08:00
|
|
|
|
|
|
|
list_del(&socket_packet->list);
|
|
|
|
kfree(socket_packet);
|
|
|
|
socket_client->queue_len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_bh(&socket_client->lock);
|
|
|
|
|
|
|
|
wake_up(&socket_client->queue_wait);
|
|
|
|
}
|
|
|
|
|
2012-06-06 04:31:30 +08:00
|
|
|
void batadv_socket_receive_packet(struct batadv_icmp_packet_rr *icmp_packet,
|
2012-05-12 08:09:33 +08:00
|
|
|
size_t icmp_len)
|
2010-12-13 19:19:28 +08:00
|
|
|
{
|
2012-06-06 04:31:31 +08:00
|
|
|
struct batadv_socket_client *hash;
|
2010-12-13 19:19:28 +08:00
|
|
|
|
2012-05-13 00:33:59 +08:00
|
|
|
hash = batadv_socket_client_hash[icmp_packet->uid];
|
2010-12-13 19:19:28 +08:00
|
|
|
if (hash)
|
2012-05-13 00:33:59 +08:00
|
|
|
batadv_socket_add_packet(hash, icmp_packet, icmp_len);
|
2010-12-13 19:19:28 +08:00
|
|
|
}
|