Advertisement
Guest User

test 29 mei

a guest
May 28th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #define __KERNEL__
  2. #define MODULE
  3. #include <linux/module.h>
  4. #include <linux/kernel.h>
  5. #include <linux/netdevice.h>
  6. #include <linux/netfilter.h>
  7. #include <linux/netfilter_ipv4.h>
  8. #include <net/ip.h>
  9. struct my_head_struct {
  10. int a;
  11. };
  12.  
  13.  
  14. /* This is the structure we shall use to register our function */
  15. static struct nf_hook_ops nfho;
  16.  
  17. /* Name of the interface we want to drop packets from */
  18. static char *drop_if = "eth1";
  19.  
  20. /* This is the hook function itself */
  21. unsigned int hook_func(unsigned int hooknum,
  22. struct sk_buff *skb,
  23. const struct net_device *in,
  24. const struct net_device *out,
  25. int (*okfn)(struct sk_buff *))
  26. {
  27.  
  28. unsigned int length,truesize;
  29. printk("Inside the hook function\n");
  30.  
  31. if (strcmp(in->name, drop_if) == 0) {
  32. struct iphdr *iph = NULL;
  33.  
  34. struct tcphdr *tcph = NULL;
  35. length = skb->len;
  36. truesize=skb->truesize;
  37.  
  38. iph = ip_hdr((skb));
  39. tcph = (struct tcphdr *)(skb_network_header((skb)) + ip_hdrlen((skb))); // access tcp header.
  40.  
  41. printk(KERN_ALERT "INFO: Source IP Address: %pI4\n",&iph->saddr);
  42.  
  43. printk(KERN_ALERT "INFO: Destination IP Address: %pI4\n",&iph->daddr);
  44.  
  45. printk(KERN_ALERT "INFO: Source Port: %u.\n",tcph->source);
  46.  
  47. printk(KERN_ALERT "INFO: Destination Port: %u.\n",tcph->dest);
  48. printk("length is %d\n",length);
  49. printk("truesize is %d\n",truesize);
  50. struct my_head_struct * my_head= NULL;
  51.  
  52. struct sk_buff *newskb;
  53. newskb = skb_copy_expand(skb, sizeof(struct my_head_struct), 0, GFP_ATOMIC);
  54. /*if(newskb==NULL)
  55. {
  56. printk("Failed to allocate mem\n");
  57. return SEND_FAIL_MEMORY;
  58. }
  59. else
  60. {
  61. // /* need add check of newskb value for error control */
  62. /*my_head->a = 21; //want to push this in new skb
  63. */
  64.  
  65. printk("Dropped packet on %s...\n", drop_if);
  66. return NF_ACCEPT;
  67. } else {
  68. return NF_ACCEPT;
  69. }
  70. }
  71.  
  72. /* Initialisation routine */
  73. int init_module()
  74. {
  75.  
  76. printk("netfilter interface module inserted\n");
  77. /* Fill in our hook structure */
  78. nfho.hook = hook_func; /* Handler function */
  79. nfho.hooknum = 0;// NF_IP_PRE_ROUTING; /* First hook for IPv4 */
  80. nfho.pf = PF_INET;
  81. nfho.priority = NF_IP_PRI_FIRST; /* Make our function first */
  82.  
  83. nf_register_hook(&nfho);
  84.  
  85. return 0;
  86. }
  87.  
  88. /* Cleanup routine */
  89. void cleanup_module()
  90. {
  91. printk("Netfilter interface module removed\n");
  92. nf_unregister_hook(&nfho);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement