Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.66 KB | None | 0 0
  1. #include <linux/module.h>  
  2. #include <linux/kernel.h>  
  3. #include <linux/init.h>  
  4. #include <net/sock.h>  
  5. #include <linux/socket.h>  
  6. #include <linux/net.h>  
  7. #include <asm/types.h>  
  8. #include <linux/netlink.h>  
  9. #include <linux/skbuff.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/if_link.h>
  12.  
  13. #define MAX_PAYLOAD_SIZE 4096
  14. #define NETLINK_USER 2
  15.  
  16. /* Module info */
  17. MODULE_AUTHOR("Yury Sokolov <yura1703@yandex.ru>");
  18. MODULE_LICENSE("GPL");
  19.  
  20.  
  21. /* Global variables */
  22. struct sock *nl_sk = NULL;
  23. int i = 0;
  24.  
  25.  
  26. /* Logging info about all network devices */
  27. int get_dev_info_logged(char **res_info)
  28. {
  29.     int pos = 0;
  30.     *res_info = (char *) kcalloc(MAX_PAYLOAD_SIZE, sizeof(char), GFP_USER);
  31.     if (!*res_info)
  32.     {
  33.         printk(KERN_ERR "[NL_KERNEL] unable to allocate memory.");
  34.         return -ENOMEM;
  35.     }
  36.    
  37.     /* Proceeding dev_net */
  38.     struct net_device *dev;
  39.    
  40.     dev = first_net_device(&init_net);
  41.    
  42.     pos += snprintf(*res_info+pos, MAX_PAYLOAD_SIZE-pos,
  43.             "%6s %3s %1s %6s %2s %17s %6s %6s %6s %6s %7s %3s\n",
  44.             "Name", "T", "S", "MTU", "UP", "HWaddr", "TxP", "TxB", "RxP", "RxB", "Baddr", "Irq");
  45.  
  46.     while (dev)
  47.     {
  48.         int is_up;
  49.         if (IFF_UP & dev->flags)
  50.             is_up = 1;
  51.         else
  52.             is_up = 0;
  53.  
  54.         struct rtnl_link_stats64 rtnl_stats;
  55.         dev_get_stats(dev, &rtnl_stats);
  56.  
  57.         pos += snprintf(*res_info+pos, MAX_PAYLOAD_SIZE-pos,
  58.                 "%6s %3d %1u %6lu %2d %02x:%02x:%02x:%02x:%02x:%02x %6llu %6llu %6llu %6llu %7x %3d\n",
  59.                 dev->name,
  60.                 dev->type,
  61.                 dev->state,
  62.                 dev->mtu,
  63.                 is_up,
  64.                 dev->dev_addr[0],
  65.                 dev->dev_addr[1],
  66.                 dev->dev_addr[2],
  67.                 dev->dev_addr[3],
  68.                 dev->dev_addr[4],
  69.                 dev->dev_addr[5],
  70.                 rtnl_stats.tx_packets,
  71.                 rtnl_stats.tx_bytes,
  72.                 rtnl_stats.rx_packets,
  73.                 rtnl_stats.rx_bytes,
  74.                 dev->base_addr,
  75.                 dev->irq);
  76.  
  77.         dev = next_net_device(dev);
  78.     }
  79.  
  80.     return 0;
  81. }
  82.  
  83.  
  84. /* Getting netlink message from userspace application */
  85. static void my_nl_recv_msg(struct sk_buff *skb)  
  86. {
  87.         printk(KERN_INFO "[NL_KERNEL] Entering: %s\n", __FUNCTION__);  
  88.  
  89.         struct nlmsghdr *nlh;
  90.     int pid;
  91.     struct sk_buff *skb_out;
  92.     int msg_size;
  93.     char *msg;
  94.  
  95.     if (get_dev_info_logged(&msg) < 0)  // here we allocate memory
  96.         return;
  97.     int res;
  98.  
  99.     msg_size = strlen(msg);
  100.  
  101.     nlh = (struct nlmsghdr *)skb->data;
  102.     //printk(KERN_INFO "[NL_KERNEL] Netlink received msg payload:%s\n", (char *)nlmsg_data(nlh));
  103.     pid = nlh->nlmsg_pid; /*pid of sending process */
  104.  
  105.     skb_out = nlmsg_new(msg_size, 0);
  106.     if (!skb_out)
  107.     {
  108.         printk(KERN_ERR "[NL_KERNEL] Failed to allocate new skb\n");
  109.         return;
  110.     }
  111.  
  112.     nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, msg_size, 0);
  113.     NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */
  114.  
  115.     strncpy(nlmsg_data(nlh), msg, msg_size);   
  116.  
  117.     res = nlmsg_unicast(nl_sk, skb_out, pid);
  118.  
  119.     kfree(msg);
  120.     if (res < 0)
  121.         printk(KERN_ERR "[NL_KERNEL] Error while sending back to user\n");
  122. }
  123.  
  124.  
  125. /* Module init function */
  126. int __init nl_kernel_init(void)
  127. {
  128.     printk(KERN_INFO "[NL_KERNEL] Init module.\n");
  129.  
  130.     /* Creating netlink socket */
  131.     struct netlink_kernel_cfg cfg = {
  132.         .input = my_nl_recv_msg,
  133.     };
  134.    
  135.     nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
  136.  
  137.         if(!nl_sk)
  138.         {
  139.                 printk(KERN_ERR "[NL_KERNEL] Error creating socket.\n");  
  140.                 return -ENOMEM;
  141.         }
  142.  
  143.     printk(KERN_INFO "[NL_KERNEL] Now module is running.\n");
  144.  
  145.     return 0;
  146. }
  147.  
  148.  
  149. /* Module exit function */
  150. void __exit nl_kernel_exit(void)
  151. {
  152.     printk(KERN_INFO "[NL_KERNEL] Exit module.\n");
  153.     netlink_kernel_release(nl_sk);
  154.     printk(KERN_INFO "[NL_KERNEL] Module is not running anymore.\n");
  155. }
  156.  
  157.  
  158. /* Initializing module */
  159. module_init(nl_kernel_init);
  160. module_exit(nl_kernel_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement