Guest
Public paste!

Andrei

By: a guest | Feb 14th, 2010 | Syntax: C | Size: 2.76 KB | Hits: 576 | Expires: Never
Copy text to clipboard
  1. /*
  2.  *      Author: andrei.sambra@telecom-sudparis.eu
  3.  *      GPLv3 License applies to this code.
  4.  *
  5.  * */
  6.  
  7. #include <linux/module.h>       /* Needed by all modules */
  8. #include <linux/kernel.h>       /* Needed for KERN_INFO */
  9. #include <linux/init.h>         /* Needed for the macros */
  10. #include <linux/skbuff.h>
  11. #include <linux/netfilter.h>
  12. #include <linux/netfilter_ipv4.h>
  13. #include <linux/ip.h>
  14. #include <linux/udp.h>
  15. #include "rpmp.h"               /* Needed for our structures */
  16.  
  17.  
  18. #define DEBUG 0
  19.  
  20. struct sk_buff *sock_buff;
  21. struct iphdr *ip_header;
  22. struct udphdr *udp_header;
  23. struct rpmphdr *rpmp_header;
  24. static struct nf_hook_ops nfho;
  25.  
  26. static unsigned int hook_func(unsigned int hooknum,
  27.                         struct sk_buff *skb,
  28.                                 const struct net_device *in,
  29.                                 const struct net_device *out,
  30.                                 int (*okfn)(struct sk_buff *))
  31. {
  32.         sock_buff = skb;
  33.  
  34.         if (!sock_buff) {
  35.                 return NF_ACCEPT;
  36.         } else {
  37.                 ip_header = (struct iphdr *)skb_network_header(sock_buff);
  38.                 if (!ip_header) {
  39.                         return NF_ACCEPT;
  40.                 } else {
  41.                         if (ip_header->protocol == IPPROTO_RPMP) {
  42.                        
  43.                                 rpmp_header = (struct rpmphdr *)(skb_transport_header(sock_buff)+sizeof(struct iphdr));
  44. #if DEBUG > 0                          
  45.                                 printk(KERN_INFO "[RPMP] DEBUG: th: 0p%p\n", rpmp_header);
  46.                                 printk(KERN_INFO "[RPMP] DEBUG: nh: 0p%p\n", skb_network_header(sock_buff));
  47.                                 printk(KERN_INFO "[RPMP] DEBUG: mh: 0p%p\n", skb_mac_header(sock_buff));       
  48.                                 printk(KERN_INFO "[RPMP] DEBUG: Length: rpmp_header=%d | dport=%d | type=%d.\n",
  49.                                                 sizeof(rpmp_header),
  50.                                                 sizeof(rpmp_header->dport),
  51.                                                 sizeof(rpmp_header->type));
  52.                                 printk(KERN_INFO "[RPMP] DEBUG: From IP address: %d.%d.%d.%dn",
  53.                                           ip_header-saddr & 0x000000FF,
  54.                                           (ip_header->saddr & 0x0000FF00) >> 8,
  55.                                           (ip_header->saddr & 0x00FF0000) >> 16,
  56.                                           (ip_header->saddr & 0xFF000000) >> 24);
  57.  
  58. #endif
  59.                                 printk(KERN_INFO "[RPMP] Got a RPMP packet for port=%d (type:%d).\n",
  60.                                                 ntohs(rpmp_header->dport),
  61.                                                 ntohs(rpmp_header->type));
  62.  
  63.                                 /* Callback function here*/
  64.  
  65.                                 return NF_DROP;
  66.                         } else {
  67.                                 return NF_ACCEPT;
  68.                         }
  69.                 }
  70.         }
  71. }
  72.  
  73. static int __init init_main(void)
  74. {
  75.         nfho.hook     = hook_func;
  76.         nfho.hooknum  = 1;
  77.         nfho.pf       = PF_INET;
  78.         nfho.priority = NF_IP_PRI_FIRST;
  79.         nf_register_hook(&nfho);
  80.  
  81. #if DEBUG > 0
  82.         printk(KERN_INFO "[RPMP] Successfully inserted protocol module into kernel.\n");
  83. #endif
  84.  
  85.         return 0;
  86. }
  87.  
  88. static void __exit cleanup_main(void)
  89. {
  90.         nf_unregister_hook(&nfho);
  91.  
  92. #if DEBUG > 0
  93.         printk(KERN_INFO "[RPMP] Successfully unloaded protocol module.\n");
  94. #endif
  95. }
  96.  
  97. module_init(init_main);
  98. module_exit(cleanup_main);
  99.  
  100. /*
  101.  *      Declaring code as GPL.
  102.  */
  103. MODULE_LICENSE("GPLv3");
  104. MODULE_AUTHOR(DRIVER_AUTHOR);           /* Who wrote this module? */
  105. MODULE_DESCRIPTION(DRIVER_DESC);        /* What does this module do */