Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "main.h"
- #include <linux/debugfs.h>
- #include <linux/slab.h>
- #include "bat_auth.h"
- #include "send.h"
- #include "types.h"
- #include "hash.h"
- #include "hard-interface.h"
- #include "compat.h"
- static struct ogm_client *ogm_client;
- //static struct ogm_packet *ogm_packet;
- void recv_batman_auth(struct batman_packet *batman_packet)
- {
- unsigned long flags;
- struct ogm_packet *ogm_packet;
- ogm_packet = kmalloc(sizeof(struct ogm_packet), GFP_KERNEL);
- if (!ogm_packet)
- printk(KERN_DEBUG "OGM is null");
- INIT_LIST_HEAD(&ogm_packet->ogm_list);
- memcpy(&ogm_packet->bat_packet, &batman_packet, sizeof(struct batman_packet));
- ogm_packet->ogm_len = sizeof(struct batman_packet);
- printk(KERN_DEBUG "ogm_packet copy successful");
- spin_lock_irqsave(&ogm_client->clientlock, flags);
- list_add_tail(&ogm_packet->ogm_list, &ogm_client->client_list);
- ogm_client->client_len++;
- printk(KERN_DEBUG "ogm_packet added to list");
- printk(KERN_DEBUG "ogm_client length %d",ogm_client->client_len);
- spin_unlock_irqrestore(&ogm_client->clientlock, flags);
- wake_up(&ogm_client->client_wait);
- }
- static int bat_auth_open(struct inode *inode, struct file *file)
- {
- ogm_client = kmalloc(sizeof(struct ogm_client), GFP_KERNEL);
- if (!ogm_client)
- return -ENOMEM;
- INIT_LIST_HEAD(&ogm_client->client_list);
- ogm_client->client_len = 0;
- // ogm_client->clientindex = i;
- ogm_client->bat_priv = inode->i_private;
- spin_lock_init(&ogm_client->clientlock);
- init_waitqueue_head(&ogm_client->client_wait);
- file->private_data = ogm_client;
- inc_module_count();
- return 0;
- }
- static int bat_auth_release(struct inode *inode, struct file *file)
- {
- struct ogm_client *ogm_client = file->private_data;
- struct ogm_packet *ogm_packet;
- struct list_head *list_ogm, *list_ogm_tmp;
- unsigned long flags;
- spin_lock_irqsave(&ogm_client->clientlock, flags);
- /* for all packets in the queue ... */
- list_for_each_safe(list_ogm, list_ogm_tmp, &ogm_client->client_list) {
- ogm_packet = list_entry(list_ogm,
- struct ogm_packet, ogm_list);
- list_del(list_ogm);
- kfree(ogm_packet);
- }
- spin_unlock_irqrestore(&ogm_client->clientlock, flags);
- kfree(ogm_client);
- return 0;
- }
- static ssize_t bat_auth_read(struct file *file, char __user *buf,
- size_t count, loff_t *ppos)
- {
- struct ogm_client *ogm_client = file->private_data;
- struct ogm_packet *ogm_packet;
- size_t packet_len;
- int error;
- unsigned long flags;
- printk("bat_auth_read entered");
- if ((!buf) || (count < sizeof(struct batman_packet)))
- {
- printk("value in buffer %s",buf);
- return -EINVAL;
- }
- if (!access_ok(VERIFY_WRITE, buf, count))
- {
- printk("access not ok for read\n");
- return -EFAULT;
- }
- if(!ogm_packet)
- printk(KERN_DEBUG "ogm_pointer is null");
- error = wait_event_interruptible(ogm_client->client_wait,
- ogm_client->client_len);
- if (error)
- return error;
- spin_lock_irqsave(&ogm_client->clientlock, flags);
- ogm_packet = list_first_entry(&ogm_client->client_list,
- struct ogm_packet, ogm_list);
- list_del(&ogm_packet->ogm_list);
- ogm_client->client_len--;
- spin_unlock_irqrestore(&ogm_client->clientlock, flags);
- printk("call to copy to user");
- error = __copy_to_user(buf, &ogm_packet->bat_packet,
- ogm_packet->ogm_len);
- printk("error while copying to user %d\n", error);
- /* error = __copy_to_user(buf, &bat_socket_packet->auth_packet, bat_socket_packet->auth_len);
- bat_dbg(DBG_BATMAN, bat_priv,
- "batman authentication sent to user\n");*/
- packet_len = ogm_packet->ogm_len;
- kfree(ogm_packet);
- if (error)
- return -EFAULT;
- return packet_len;
- }
- static ssize_t bat_auth_write(struct file *file, const char __user *buff,
- size_t len, loff_t *off)
- {
- struct bat_priv *bat_priv = ogm_client->bat_priv;
- struct bat_auth_packet auth_packet;
- struct orig_node *orig_node;
- struct batman_if *batman_if;
- size_t packet_len = sizeof(struct bat_auth_packet);
- uint8_t dstaddr[ETH_ALEN];
- unsigned long flags;
- printk("length of buffer %d",len);
- if (len < sizeof(struct bat_auth_packet)) {
- bat_dbg(DBG_BATMAN, bat_priv,
- "Error - can't send packet from char device: "
- "invalid packet size\n");
- return -EINVAL;
- }
- if (len >= sizeof(struct bat_auth_packet))
- packet_len = sizeof(struct bat_auth_packet);
- if (!access_ok(VERIFY_READ, buff, packet_len))
- return -EFAULT;
- printk("starting to copy from user");
- if (__copy_from_user(&auth_packet, buff, packet_len))
- {
- printk("copy from user successful");
- return -EFAULT;
- }
- if (auth_packet.packet_type != BAT_AUTH) {
- printk( "Error - can't send packet from char device: (expected: BAT_AUTH)\n");
- // return -EINVAL;
- }
- if (auth_packet.version != COMPAT_VERSION) {
- auth_packet.ttl = COMPAT_VERSION;
- goto out;
- }
- if (atomic_read(&module_state) != MODULE_ACTIVE)
- goto dst_unreach;
- spin_lock_irqsave(&orig_hash_lock, flags);
- orig_node = ((struct orig_node *)hash_find(orig_hash, auth_packet.dst));
- if (!orig_node)
- goto unlock;
- if (!orig_node->router)
- goto unlock;
- batman_if = orig_node->router->if_incoming;
- memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
- spin_unlock_irqrestore(&orig_hash_lock, flags);
- if (!batman_if)
- goto dst_unreach;
- if (batman_if->if_status != IF_ACTIVE)
- goto dst_unreach;
- memcpy(auth_packet.orig,
- bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
- packet_len = sizeof(struct bat_auth_packet);
- printk("size of bat_auth_packet%zu",packet_len);
- send_raw_packet((unsigned char *)&auth_packet,
- packet_len, batman_if, dstaddr);
- goto out;
- unlock:
- spin_unlock_irqrestore(&orig_hash_lock, flags);
- dst_unreach:
- printk("Error - destination unreachable: \n");
- out:
- return len;
- }
- static const struct file_operations auth_fops = {
- .owner = THIS_MODULE,
- .open = bat_auth_open,
- .release = bat_auth_release,
- .read = bat_auth_read,
- .write = bat_auth_write,
- };
- int bat_auth_setup(struct bat_priv *bat_priv)
- {
- struct dentry *d;
- if (!bat_priv->debug_dir)
- return 1;
- printk("bat_auth_setup entered");
- d = debugfs_create_file(BATMAN_AUTH, S_IFREG | S_IWUSR | S_IRUSR,
- bat_priv->debug_dir, bat_priv, &auth_fops);
- if (d)
- {
- printk("error creating file");
- return 1;
- }
- else
- {
- printk("file created");
- bat_dbg(DBG_BATMAN, bat_priv,
- "File created from bat_auth.c: \n");
- return 0;
- }
- }
- in types.h
- struct ogm_client {
- struct list_head client_list;
- unsigned int client_len;
- unsigned char clientindex;
- spinlock_t clientlock;
- wait_queue_head_t client_wait;
- struct bat_priv *bat_priv;
- };
- struct ogm_packet {
- struct list_head ogm_list;
- size_t ogm_len;
- struct batman_packet bat_packet;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement