Chinaunix首页 | 论坛 | 博客
  • 博客访问: 340306
  • 博文数量: 40
  • 博客积分: 826
  • 博客等级: 准尉
  • 技术积分: 727
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-22 15:18
文章分类

全部博文(40)

文章存档

2016年(1)

2015年(1)

2013年(12)

2012年(5)

2011年(21)

分类: LINUX

2011-08-03 09:29:44

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/skbuff.h>
  5. #include <linux/ip.h>
  6. #include <linux/netfilter.h>
  7. #include <linux/netfilter_ipv4.h>
  8. #include <net/tcp.h>
  9. #include <linux/if_ether.h>
  10. #include <linux/if_packet.h>
  11. #include <linux/inet.h>
  12. #include <net/checksum.h>

  13. MODULE_LICENSE("GPL");
  14. MODULE_AUTHOR("dragon");
  15. MODULE_DESCRIPTION("test");
  16. MODULE_ALIAS("module test netfiler");

  17. static unsigned int nf_hook_in(unsigned int hooknum,
  18.                                 struct sk_buff *sk,
  19.                                 const struct net_device *in,
  20.                                 const struct net_device *out,
  21.                                 int (*okfn)(struct sk_buff *))
  22. {
  23.     __be32 saddr, daddr;
  24.     struct sk_buff *sb = sk;
  25.     struct tcphdr *tcph = NULL;
  26.     struct ucphdr *udph = NULL;

  27.     struct iphdr *iph = ip_hdr(sk);
  28.     unsigned int src_ip = iph->saddr;

  29.     saddr = in_aton("192.168.1.101");
  30.     daddr = in_aton("192.168.2.101");

  31.     if(saddr == iph->saddr && daddr == iph->daddr){
  32.         printk("input src:%d.%d.%d.%d dst:%d.%d.%d.%d \n",
  33.                 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));

  34.         daddr = in_aton("192.168.1.100");
  35.         iph->daddr = daddr;
  36.         iph->check = ip_fast_csum((unsigned char*)iph, iph->ihl);

  37.         printk("input changed src:%d.%d.%d.%d dst:%d.%d.%d.%d \n",
  38.                 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
  39.     }

  40.     //printk("filter local in \n");
  41.     return NF_ACCEPT;

  42. }

  43. static unsigned int nf_hook_out(unsigned int hooknum,
  44.                                 struct sk_buff *sk,
  45.                                 const struct net_device *in,
  46.                                 const struct net_device *out,
  47.                                 int (*okfn)(struct sk_buff *))
  48. {
  49.     __be32 saddr, daddr;
  50.     struct sk_buff *sb = sk;
  51.     struct tcphdr *tcph = NULL;
  52.     struct ucphdr *udph = NULL;

  53.     struct iphdr *iph = ip_hdr(sk);
  54.     unsigned int src_ip = iph->saddr;

  55.     saddr = in_aton("192.168.1.100");
  56.     daddr = in_aton("192.168.1.101");

  57.     if(saddr == iph->saddr && daddr == iph->daddr){
  58.         printk("output src:%d.%d.%d.%d dst:%d.%d.%d.%d \n",
  59.                 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));

  60.         saddr = in_aton("192.168.2.101");
  61.         iph->daddr = saddr;
  62.         iph->check = ip_fast_csum((unsigned char*)iph, iph->ihl);

  63.         printk("input changed src:%d.%d.%d.%d dst:%d.%d.%d.%d \n",
  64.                 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
  65.     }

  66.     //printk("filter local out \n");
  67.     return NF_ACCEPT;
  68. }

  69. static struct nf_hook_ops nfin = {
  70.     .hook = nf_hook_in,
  71.     .hooknum = NF_INET_PRE_ROUTING,
  72.     .pf = PF_INET,
  73.     .priority = NF_IP_PRI_FIRST,
  74. };

  75. static struct nf_hook_ops nfout = {
  76.     .hook = nf_hook_out,
  77.     .hooknum = NF_INET_POST_ROUTING,
  78.     .pf = PF_INET,
  79.     .priority = NF_IP_PRI_FIRST,
  80. };


  81. int __init test_init(void)
  82. {
  83.     nf_register_hook(&nfin);
  84.     nf_register_hook(&nfout);

  85.     printk("test module init\n");

  86.     return 0;
  87. }

  88. void __exit test_exit(void)
  89. {
  90.     nf_unregister_hook(&nfin);
  91.     nf_unregister_hook(&nfout);

  92.     printk("test module exit\n");

  93.     return;
  94. }

  95. module_init(test_init);
  96. module_exit(test_exit);

阅读(2206) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~