SHARE
TWEET

Untitled

a guest Jul 6th, 2017 119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "main.h"
  2. #include <linux/debugfs.h>
  3. #include <linux/slab.h>
  4. #include "bat_auth.h"
  5. #include "send.h"
  6. #include "types.h"
  7. #include "hash.h"
  8. #include "hard-interface.h"
  9.  
  10. #include "compat.h"
  11.  
  12. static struct ogm_client *ogm_client;
  13.  
  14. //static struct ogm_packet *ogm_packet;
  15.  
  16. void recv_batman_auth(struct batman_packet *batman_packet)
  17. {
  18.     unsigned long flags;
  19.     struct ogm_packet *ogm_packet;
  20.     ogm_packet = kmalloc(sizeof(struct ogm_packet), GFP_KERNEL);
  21.  
  22.     if (!ogm_packet)
  23.         printk(KERN_DEBUG "OGM is null");
  24.  
  25.     INIT_LIST_HEAD(&ogm_packet->ogm_list);
  26.     memcpy(&ogm_packet->bat_packet, &batman_packet, sizeof(struct batman_packet));
  27.         ogm_packet->ogm_len = sizeof(struct batman_packet);
  28.     printk(KERN_DEBUG "ogm_packet copy successful");
  29.  
  30.     spin_lock_irqsave(&ogm_client->clientlock, flags);
  31.  
  32.     list_add_tail(&ogm_packet->ogm_list, &ogm_client->client_list);
  33.     ogm_client->client_len++;
  34.     printk(KERN_DEBUG "ogm_packet added to list");
  35.     printk(KERN_DEBUG "ogm_client length %d",ogm_client->client_len);
  36.  
  37.     spin_unlock_irqrestore(&ogm_client->clientlock, flags);
  38.  
  39.     wake_up(&ogm_client->client_wait);
  40.    
  41.  
  42. }
  43.  
  44. static int bat_auth_open(struct inode *inode, struct file *file)
  45. {
  46.     ogm_client = kmalloc(sizeof(struct ogm_client), GFP_KERNEL);
  47.  
  48.     if (!ogm_client)
  49.         return -ENOMEM;
  50.  
  51.  
  52.     INIT_LIST_HEAD(&ogm_client->client_list);
  53.     ogm_client->client_len = 0;
  54. //  ogm_client->clientindex = i;
  55.     ogm_client->bat_priv = inode->i_private;
  56.     spin_lock_init(&ogm_client->clientlock);
  57.     init_waitqueue_head(&ogm_client->client_wait);
  58.  
  59.     file->private_data = ogm_client;
  60.  
  61.     inc_module_count();
  62.  
  63.     return 0;
  64. }
  65.  
  66. static int bat_auth_release(struct inode *inode, struct file *file)
  67. {
  68.     struct ogm_client *ogm_client = file->private_data;
  69.     struct ogm_packet *ogm_packet;
  70.     struct list_head *list_ogm, *list_ogm_tmp;
  71.     unsigned long flags;
  72.  
  73.     spin_lock_irqsave(&ogm_client->clientlock, flags);
  74.  
  75.     /* for all packets in the queue ... */
  76.     list_for_each_safe(list_ogm, list_ogm_tmp, &ogm_client->client_list) {
  77.         ogm_packet = list_entry(list_ogm,
  78.                        struct ogm_packet, ogm_list);
  79.  
  80.         list_del(list_ogm);
  81.         kfree(ogm_packet);
  82.     }
  83.  
  84.    
  85.     spin_unlock_irqrestore(&ogm_client->clientlock, flags);
  86.  
  87.     kfree(ogm_client);
  88.     return 0;
  89. }
  90.  
  91. static ssize_t bat_auth_read(struct file *file, char __user *buf,
  92.                    size_t count, loff_t *ppos)
  93. {
  94.     struct ogm_client *ogm_client = file->private_data;
  95.     struct ogm_packet *ogm_packet;
  96.     size_t packet_len;
  97.     int error;
  98.     unsigned long flags;
  99.     printk("bat_auth_read entered");
  100.  
  101.     if ((!buf) || (count < sizeof(struct batman_packet)))
  102.     {
  103.         printk("value in buffer %s",buf);
  104.         return -EINVAL;
  105.     }
  106.  
  107.     if (!access_ok(VERIFY_WRITE, buf, count))
  108.     {
  109.         printk("access not ok for read\n");
  110.         return -EFAULT;
  111.     }
  112.  
  113.     if(!ogm_packet)
  114.         printk(KERN_DEBUG "ogm_pointer is null");
  115.  
  116.     error = wait_event_interruptible(ogm_client->client_wait,
  117.                      ogm_client->client_len);
  118.  
  119.     if (error)
  120.         return error;
  121.  
  122.     spin_lock_irqsave(&ogm_client->clientlock, flags);
  123.  
  124.     ogm_packet = list_first_entry(&ogm_client->client_list,
  125.                      struct ogm_packet, ogm_list);
  126.     list_del(&ogm_packet->ogm_list);
  127.     ogm_client->client_len--;
  128.  
  129.     spin_unlock_irqrestore(&ogm_client->clientlock, flags);
  130.  
  131.     printk("call to copy to user");
  132.     error = __copy_to_user(buf, &ogm_packet->bat_packet,
  133.                ogm_packet->ogm_len);
  134.     printk("error while copying to user %d\n", error);
  135.        
  136.  
  137. /*  error = __copy_to_user(buf, &bat_socket_packet->auth_packet, bat_socket_packet->auth_len);
  138.     bat_dbg(DBG_BATMAN, bat_priv,
  139.             "batman authentication sent to user\n");*/
  140.  
  141.     packet_len = ogm_packet->ogm_len;
  142.     kfree(ogm_packet);
  143.  
  144.     if (error)
  145.         return -EFAULT;
  146.  
  147.     return packet_len;
  148. }
  149.  
  150. static ssize_t bat_auth_write(struct file *file, const char __user *buff,
  151.                 size_t len, loff_t *off)
  152. {
  153.    
  154.     struct bat_priv *bat_priv = ogm_client->bat_priv;
  155.     struct bat_auth_packet auth_packet;
  156.     struct orig_node *orig_node;
  157.     struct batman_if *batman_if;
  158.     size_t packet_len = sizeof(struct bat_auth_packet);
  159.     uint8_t dstaddr[ETH_ALEN];
  160.     unsigned long flags;
  161.  
  162.     printk("length of buffer %d",len);
  163.     if (len < sizeof(struct bat_auth_packet)) {
  164.        
  165.         bat_dbg(DBG_BATMAN, bat_priv,
  166.             "Error - can't send packet from char device: "
  167.             "invalid packet size\n");
  168.         return -EINVAL;
  169.     }
  170.  
  171.  
  172.     if (len >= sizeof(struct bat_auth_packet))
  173.         packet_len = sizeof(struct bat_auth_packet);
  174.  
  175.     if (!access_ok(VERIFY_READ, buff, packet_len))
  176.         return -EFAULT;
  177.     printk("starting to copy from user");
  178.     if (__copy_from_user(&auth_packet, buff, packet_len))
  179.     {
  180.         printk("copy from user successful");
  181.         return -EFAULT;
  182.     }
  183.  
  184.     if (auth_packet.packet_type != BAT_AUTH) {
  185.         printk( "Error - can't send packet from char device: (expected: BAT_AUTH)\n");
  186. //      return -EINVAL;
  187.     }
  188.  
  189.     if (auth_packet.version != COMPAT_VERSION) {
  190.         auth_packet.ttl = COMPAT_VERSION;
  191.         goto out;
  192.     }
  193.  
  194.     if (atomic_read(&module_state) != MODULE_ACTIVE)
  195.         goto dst_unreach;
  196.  
  197.     spin_lock_irqsave(&orig_hash_lock, flags);
  198.     orig_node = ((struct orig_node *)hash_find(orig_hash, auth_packet.dst));
  199.  
  200.     if (!orig_node)
  201.         goto unlock;
  202.  
  203.     if (!orig_node->router)
  204.         goto unlock;
  205.  
  206.     batman_if = orig_node->router->if_incoming;
  207.     memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
  208.  
  209.     spin_unlock_irqrestore(&orig_hash_lock, flags);
  210.  
  211.     if (!batman_if)
  212.         goto dst_unreach;
  213.  
  214.     if (batman_if->if_status != IF_ACTIVE)
  215.         goto dst_unreach;
  216.  
  217.     memcpy(auth_packet.orig,
  218.            bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
  219.  
  220.  
  221.     packet_len = sizeof(struct bat_auth_packet);
  222.  
  223.     printk("size of bat_auth_packet%zu",packet_len);
  224.  
  225.     send_raw_packet((unsigned char *)&auth_packet,
  226.             packet_len, batman_if, dstaddr);
  227.  
  228.     goto out;
  229.  
  230. unlock:
  231.     spin_unlock_irqrestore(&orig_hash_lock, flags);
  232. dst_unreach:
  233.     printk("Error - destination unreachable: \n");
  234. out:
  235.     return len;
  236. }
  237.  
  238.  
  239. static const struct file_operations auth_fops = {
  240.     .owner = THIS_MODULE,
  241.     .open = bat_auth_open,
  242.     .release = bat_auth_release,
  243.     .read = bat_auth_read,
  244.     .write = bat_auth_write,
  245.    
  246. };
  247.  
  248. int bat_auth_setup(struct bat_priv *bat_priv)
  249. {
  250.     struct dentry *d;
  251.  
  252.     if (!bat_priv->debug_dir)
  253.         return 1;
  254.  
  255.     printk("bat_auth_setup entered");
  256.     d = debugfs_create_file(BATMAN_AUTH, S_IFREG | S_IWUSR | S_IRUSR,
  257.                 bat_priv->debug_dir, bat_priv, &auth_fops);
  258.    
  259.    
  260.     if (d)
  261.     {
  262.         printk("error creating file");
  263.         return 1;
  264.     }
  265.     else
  266.     {
  267.         printk("file created");
  268.     bat_dbg(DBG_BATMAN, bat_priv,
  269.             "File created from bat_auth.c: \n");
  270.         return 0;
  271.     }
  272.  
  273. }
  274.  
  275. in types.h
  276. struct ogm_client {
  277.     struct list_head client_list;
  278.     unsigned int client_len;
  279.     unsigned char clientindex;
  280.     spinlock_t clientlock;
  281.     wait_queue_head_t client_wait;
  282.     struct bat_priv *bat_priv;
  283. };
  284.  
  285. struct ogm_packet {
  286.     struct list_head ogm_list;
  287.     size_t ogm_len;
  288.     struct batman_packet bat_packet;
  289. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top