Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1560951
  • 博文数量: 61
  • 博客积分: 472
  • 博客等级: 下士
  • 技术积分: 548
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-26 11:52
文章分类
文章存档

2018年(1)

2017年(2)

2016年(6)

2015年(3)

2014年(19)

2013年(10)

2012年(20)

分类: C/C++

2012-08-22 16:52:19


点击(此处)折叠或打开

  1. #include <linux/module.h> /* Specifically, a module */
  2. #include <linux/kernel.h> /* We're doing kernel work */
  3. #include <linux/proc_fs.h>
  4. #include <linux/netfilter.h>
  5. #include <linux/netfilter_ipv4.h>
  6. #include <linux/types.h>
  7. #include <linux/if_ether.h>
  8. #include<linux/tcp.h>
  9. #include<linux/ip.h>
  10. #include <linux/skbuff.h>
  11. #define IP 0x800
  12. #define TCP 0x6
  13. /* Necessary because we use the proc fs */
  14. #define procfs_name "port"
  15. char *buf;
  16. struct nf_hook_ops nfho;
  17. struct proc_dir_entry *Our_Proc_File;
  18. int len=0;
  19. unsigned int
  20. hook_func (unsigned int hooknum,
  21.            struct sk_buff **skb,
  22.            const struct net_device *in,
  23.  const struct net_device *out, int (*okfn) (struct sk_buff *))
  24. {
  25.   struct ethhdr *eth;
  26.   struct iphdr *iph;
  27.   struct tcphdr *tcp;
  28.   struct sk_buff *SKB;


  29.   int ips,ipd;
  30.   SKB = *skb;
  31.   len = 0;
  32.  
  33.   eth = (struct ethhdr *) SKB->mac_header;
  34.   iph = (struct iphdr *) SKB->network_header;
  35.   tcp = (struct tcphdr *) SKB->transport_header;
  36.   if (ntohs (eth->h_proto) == IP)
  37.     {
  38.       if (iph->protocol == TCP)
  39.         {
  40.  
  41.          len += sprintf(buf + len, "smac = %02x:%02x:%02x:%02x:%02x:%02x\n", eth->h_source,eth->h_source,eth->h_source,eth->h_source,eth->h_source,eth->h_source);
  42.          len += sprintf(buf + len, "dmac = %02x:%02x:%02x:%02x:%02x:%02x\n", eth->h_dest,eth->h_dest,eth->h_dest,eth->h_dest,eth->h_dest,eth->h_dest);


  43.         len += sprintf(buf + len, "dip = %u.%u.%u.%u\n", NIPQUAD(iph->daddr));
  44.         len += sprintf(buf + len, "sip = %u.%u.%u.%u\n", NIPQUAD(iph->daddr));
  45.          len += sprintf(buf + len, "sport = %d \n",ntohs(tcp -> source));
  46.          len += sprintf(buf + len, "dport = %d \n",ntohs(tcp -> dest));
  47.               }
  48.     }
  49.   return NF_ACCEPT;
  50. }
  51.  
  52. int
  53. procfile_read (char *buffer,
  54.                char **buffer_location,
  55.                off_t offset, int buffer_length, int *eof, void *data)
  56. {
  57.        memcpy(buffer,buf,len);
  58.   return len;
  59. }
  60.  
  61. int
  62. init_module ()
  63. {
  64.   buf = kmalloc(1024,GFP_KERNEL);
  65.   nfho.hook = hook_func; /* 处理函数 */
  66.   nfho.hooknum = NF_IP_PRE_ROUTING; /* 使用IPv4的第一个hook */
  67.   nfho.pf = PF_INET;
  68.   nfho.priority = NF_IP_PRI_FIRST; /* 让我们的函数首先执行 */
  69.   nf_register_hook (&nfho);
  70.   Our_Proc_File = create_proc_entry (procfs_name, 0644, NULL);
  71.   Our_Proc_File->read_proc = procfile_read;
  72.   Our_Proc_File->owner = THIS_MODULE;
  73.   Our_Proc_File->mode = S_IFREG | S_IRUGO;
  74.   Our_Proc_File->uid = 0;
  75.   Our_Proc_File->gid = 0;
  76.   Our_Proc_File->size = 37;
  77.   return 0; /* everything is ok */
  78. }
  79.                                   
  80. void
  81. cleanup_module ()
  82. {
  83.   kfree(buf);
  84.   nf_unregister_hook (&nfho);
  85.   remove_proc_entry (procfs_name, &proc_root);
  86. }
  87.  
  88. makefile代码:
  89. ifeq ($(KERNELRELEASE),)
  90.     KERNELDIR ?= /lib/modules/$(shell uname -r)/build
  91.      PWD := $(shell pwd)
  92. modules:
  93.         $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
  94. modules_install:
  95.         $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
  96. clean:
  97.         rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
  98. .PHONY: modules modules_install clean
  99. else
  100.     # called from kernel build system: just declare what our modules are
  101.     obj-m := proc.o
  102. endif

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