nht_cntt

lab4 nkmod.c

Nov 3rd, 2016
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.32 KB | None | 0 0
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/netfilter.h>
  4. #include <linux/netfilter_ipv4.h>
  5. #include <linux/udp.h>
  6. #include <linux/tcp.h>
  7. #include <linux/skbuff.h>
  8. #include <linux/ip.h>
  9. #include <linux/proc_fs.h>
  10. #include <linux/list.h>
  11. #include <asm/uaccess.h>
  12.  
  13. static struct nf_hook_ops hk;
  14. struct sk_buff *sock_buff;
  15. //struct udphdr *udp_header;          //udp header struct (not used)
  16.  
  17.  
  18. unsigned int nf_hook_ex(const struct nf_hook_ops *ops,
  19.                         struct sk_buff *skb,
  20.                         const struct net_device *in,
  21.                         const struct net_device *out,
  22.                         int (*okfn)(struct sk_buff *)){
  23.  
  24.     struct iphdr *ip_header = (struct iphdr *)skb_network_header(skb);
  25.     unsigned int src_ip;
  26.  
  27.     // ===================================> Cau 1 <=================================================
  28.     if (  ip_header->protocol == 17 )
  29.     {
  30.          return NF_DROP;   
  31.     }    
  32.     return NF_ACCEPT;
  33.    
  34.    
  35.     // ===================================> Cau 2a <=================================================
  36.     src_ip = (unsigned int)ip_header->saddr;  
  37.     int i ;
  38.     for (i= 0; i < 256; ++i)
  39.     {
  40.         if ( src_ip == (1677764800+i*65536 )) //192+168*256+i*256*256+100*256*256*256 <-> 192.168.0.100
  41.         {
  42.             return NF_DROP;
  43.         }
  44.     }
  45.     return NF_ACCEPT;
  46.  
  47.    
  48.     // ===================================> Cau 2b <=================================================
  49.     struct udphdr *udp_header ;
  50.     unsigned int dest_port ;
  51.     struct tcphdr *tcp_header;      
  52.     switch(ip_header->protocol)
  53.     {
  54.         case 17: //udp
  55.            udp_header = (struct udphdr *)((__u32 *)ip_header+ip_header->ihl);            
  56.             dest_port = (unsigned int)ntohs(udp_header->dest);    
  57.             if ( dest_port == 80 ) return NF_DROP;
  58.    
  59.         case 6: //tcp
  60.             tcp_header = (struct tcphdr *)((__u32 *)ip_header+ip_header->ihl);            
  61.             dest_port = (unsigned int)ntohs(tcp_header->dest);    
  62.             if ( dest_port == 80 ) return NF_DROP;
  63.     }
  64.     return NF_ACCEPT;
  65.  
  66.      
  67.     // ===================================> Cau 2c <=================================================
  68.     unsigned int dest_ip = (unsigned int)ip_header->daddr;  
  69.     src_ip = (unsigned int)ip_header->saddr;
  70.     if ( dest_ip == (134744072)  || src_ip == (134744072) )  //8.8.8.8
  71.     {
  72.         return NF_ACCEPT;
  73.     }
  74.     return NF_DROP;
  75.    
  76. }
  77.  
  78. /* Được gọi khi sử dụng lệnh 'insmod' */
  79. int kmod_init(void){
  80.         /* gán thông tin cho biến `hk` */
  81.  
  82.         hk = (struct nf_hook_ops){
  83.                 .hook = nf_hook_ex,
  84.                 /* đây là hàm callback `nf_hook_ex` kiểu nf_hookfn - định nghĩa trong include/linux/netfilter.h, line 47
  85.                 - các tham số của hook mà người dùng định nghĩa phải khớp với kiểu nf_hookfn */
  86.                 .hooknum = NF_INET_PRE_ROUTING,
  87.                 /* Sự kiện mà hook này đăng ký  */
  88.                 .pf = PF_INET,
  89.                 /* Chỉ xử lý các Internet (IPv4) packet  */
  90.                 .priority = NF_IP_PRI_FIRST
  91.                 /* Cài đặt độ ưu tiên của hook này ở mức độ cao nhất*/
  92.         };
  93.         nf_register_hook(&hk);
  94.   return 0;
  95. }
  96.  
  97. /* Được gọi khi sử dụng lệnh 'rmmod' */
  98. void kmod_exit(void){
  99.         nf_unregister_hook(&hk);
  100. }
  101.  
  102. /* Some standard macros to pass the kernel compile script some information */
  103. module_init(kmod_init);
  104. module_exit(kmod_exit);
  105. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment