2

Edit: I have written following module to filter websites.I am able to sniff DNS Packet(question field Domain Name) requested by User and compare with block[]="www.facebook.com". If matched, drop the packet.Now I inserted a read code (see after //Read File here in bellow code)to read website list written in a file (instead of Hard coding block[]=) and compare it with DNS question.right Now I am able to compile module successfully but not able to load it properly.Is it because of make Warning given bellow and module code is.(I request you kindly please read code , thank you for time.)

#include </usr/src/kernels/2.6.32-573.8.1.el6.x86_64/include/linux/module.h>
#include </usr/src/kernels/2.6.32-573.8.1.el6.x86_64/include/linux/netfilter.h>
#include </usr/src/kernels/2.6.32-573.8.1.el6.x86_64/include/linux/netfilter_ipv4.h>
#include </usr/src/kernels/2.6.32-573.8.1.el6.x86_64/include/linux/ip.h>
#include </usr/src/kernels/2.6.32-573.8.1.el6.x86_64/include/linux/tcp.h>
#include </usr/src/kernels/2.6.32-573.8.1.el6.x86_64/include/linux/udp.h>
#include <linux/kernel.h>
#include <//linux/init.h>
#include <linux/syscalls.h>
#include <linux/fcntl.h>
#include <asm/uaccess.h>
//#define PUDP_WATCH_PORT     53  /* DNS (UDP) port */  
//char block[]="www.facebook.com";
static struct nf_hook_ops nfho;
char domain[100]={'\0'};
int Domain_Index=0;

int fd,n;
char offset=32;
unsigned short int low,mid,high;
char filename[]="/temp/websitelist.txt";
char total_wbste;
char buf[30];
unsigned char temp;

mm_segment_t old_fs;

static unsigned int ptcp_hook_func(const struct nf_hook_ops *ops,
                               struct sk_buff *skb,
                               const struct net_device *in,
                               const struct net_device *out,
                               int (*okfn)(struct sk_buff *))
{
      struct iphdr *iph;          /* IPv4 header */
      struct udphdr *udph;  /* UDP header */
       unsigned char *user_data;   /* TCP data begin pointer */
       unsigned char *tail;        /* TCP data end pointer */
       unsigned char *it;          /* TCP data iterator */

      if (!skb)          // checking for valid IP packet 
         return NF_ACCEPT;
      iph = ip_hdr(skb);          // getting  IP header 

  /* if(udph->source!=PUDP_WATCH_PORT )  // Udp and DNS port 53   destinaton/source 
  return NF_ACCEPT;*/

      if (iph->protocol != 17)   // for protocol recived IP packet. 
    return NF_ACCEPT;

      udph = udp_hdr(skb);           // getting  UDP header 
      user_data = (unsigned char *)((unsigned char *)udph + (21)); // seting  pointer to user_data=udp header+[(UDP=8byte)+12bytes DNS_fields)+1]=21
      tail = skb_tail_pointer(skb);         // setting pointer to end of the payload(data) in the DNS packet

    // Print UDP packet data (payload)      

     Domain_Index=0;
     for (it = user_data; it != tail; ++it) {
          char c = *(char *)it;
           if (c == '\0'){
               domain[Domain_Index]=c;
                break;
           }
           if((c>64 && c<91) ||(c>96 && c<123))
                  domain[Domain_Index]=c; // storing the query received by the packet
           else 
                  domain[Domain_Index]='.'; 
            Domain_Index++;
      }
      printk(KERN_INFO "\n%s",domain);  // Domain Name queried 
      printk(KERN_INFO "The value of Domain index--->\n%d",Domain_Index);
      /*
      if(strncmp(block,domain,Domain_Index-1)== 0){//compraing with      string to be block[](hardCoded as of now) 
      printk(KERN_INFO "Packet Droped\n");
      return NF_DROP;
      }*/

  //--------------------------------------------------------------------
     //Read File  here

      old_fs = get_fs();
      set_fs(KERNEL_DS);

      fd = sys_open(filename, O_RDONLY, 0);
      if (fd >= 0) { //if fd 
             printk(KERN_DEBUG);
             sys_read(fd, buf, 2);

             low = 1;
             high = 5;
             mid = (low+high)/2;
             while (low <= high) { //while loop1

                  sys_lseek(fd, (mid*offset), SEEK_SET);
                  sys_read(fd, buf, 30);

                  temp=strncmp(buf,domain,offset-2); // comaparing read string with Domain Name 

                  if (temp==0){  // 1
                         printk(KERN_INFO "Packet Droped\n");
                         sys_close(fd);
                         set_fs(old_fs);
                         return NF_DROP; 
                   }// 1
                  else if (temp>0)
                         high=mid-1;
                  else 
                         low=mid+1;

                  mid = (low+high)/2;   

              } //while loop1

         sys_close(fd);
        }//if fd
    set_fs(old_fs);

   }
//--------------------------------------------------------------------

     return NF_ACCEPT; // not any condition met then packet is accepted and let it to pass through network device for futher process
 }

static int  __init ptcp_init(void)
{
     int res;

     nfho.hook = (nf_hookfn *)ptcp_hook_func;    /* hook function */
  // nfho.hooknum = NF_INET_PRE_ROUTING;         /* received packets  */ 
     nfho.hooknum = NF_INET_POST_ROUTING;   /*transmitted packets */
     nfho.pf = PF_INET;                          /* IPv4 */
     nfho.priority = NF_IP_PRI_FIRST;            /* max hook priority */

     res = nf_register_hook(&nfho);
     if (res < 0) {
      printk(KERN_INFO "print_udp: error in nf_register_hook()\n");
      return res;
   }

     printk(KERN_INFO "module loaded\n");
     return 0;
 }

  static void  __exit ptcp_exit(void)
{
  nf_unregister_hook(&nfho);
  printk(KERN_INFO "module  unloaded\n");
 }

 module_init(ptcp_init);
 module_exit(ptcp_exit);

 MODULE_AUTHOR("Sam Protsenko");
 MODULE_DESCRIPTION("Module for printing TCP packet data");
 MODULE_LICENSE("GPL");

But I am getting error in insmod and error is

[root@localhost dns_sniffer]# make 
make -C /lib/modules/2.6.32-573.8.1.el6.x86_64/build M=/home/praveen/dns_sniffer modules
make[1]: Entering directory `/usr/src/kernels/2.6.32-573.8.1.el6.x86_64'
CC [M]  /home/praveen/dns_sniffer/dns_sniff.o
Building modules, stage 2.
MODPOST 1 modules
WARNING: "sys_open" [/home/praveen/dns_sniffer/dns_sniff.ko] undefined!
WARNING: "sys_read" [/home/praveen/dns_sniffer/dns_sniff.ko] undefined!
WARNING: "sys_lseek" [/home/praveen/dns_sniffer/dns_sniff.ko] undefined!
CC      /home/praveen/dns_sniffer/dns_sniff.mod.o
LD [M]  /home/praveen/dns_sniffer/dns_sniff.ko.unsigned
NO SIGN [M] /home/praveen/dns_sniffer/dns_sniff.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.32-573.8.1.el6.x86_64'
[root@localhost dns_sniffer]#

[root@localhost dns_sniffer]# insmod dns_sniff.ko 
insmod: error inserting 'dns_sniff.ko': -1 Unknown symbol in module

Also I wanted to know a safe way to read a file within linux-module. I've gone through some of previous example. Is this way is safe to read frequently? Thank you for reply

Community
  • 1
  • 1
user55111
  • 322
  • 1
  • 3
  • 12
  • Possible duplicate of [How to read/write files within a Linux kernel module?](http://stackoverflow.com/questions/1184274/how-to-read-write-files-within-a-linux-kernel-module) – Tsyvarev Dec 16 '15 at 08:37
  • you getting error while loading modules and reading-writing files will come in picture once modules in loaded successfully. – Pradeep Goswami Dec 16 '15 at 08:40
  • @PradeepGoswami: Original question stated explicitely that author guess his problem in *incorrect using of `sys_*` functions*. And this is **correct guessing**. But your edit removed that guess both from the title and the question's body. I am curious why your edit has been approved, as it **changed question in a manner not intended by the author**. **Only author himself is allowed to make such changes**. – Tsyvarev Dec 16 '15 at 09:23
  • @Tsyvarev see the error specified by author , which are not related to read or write files, I think author is not clear why these errors are coming, that is why I modified the question and author also approved the same – Pradeep Goswami Dec 16 '15 at 09:30
  • @PradeepGoswami thank you for the Edition. I thought it's bcz of using 'sys_*' function in code. But before inserting code to read/write, I was able to load and unload the module with no error. Now I am getting error. Is there any mistake using these function to read/write in code above? thank you. – user55111 Dec 16 '15 at 09:37
  • @PradeepGoswami: It is not an author who has approved your edit. See this [Suggested Edits page](http://stackoverflow.com/review/suggested-edits/10580427) (it is shown in revision history within your edit). `I think author is not clear why these errors are coming` - in such cases you may add comment to the question post. It is possible, that he(she) knows what this error means, but he looks for origin problem. Or he(she) doesn't know about error meaning, and expects description of it in answer/comment. – Tsyvarev Dec 16 '15 at 09:39
  • @user123 please ask another question with error logs ,this problem is different and not related to this post. – Pradeep Goswami Dec 16 '15 at 09:40
  • @Tsyvarev author also agreed , hope you don't mind now. – Pradeep Goswami Dec 16 '15 at 09:41
  • @PradeepGoswami:Okay i'll edit my question properly. – user55111 Dec 16 '15 at 09:45
  • @Tsyvarev: Yes , I am having a problem in _incorrect_ using of 'sys_* 'functions. and I am sorry for not mentioning it properly. Now I edited my question hope you all understand what is my problem. Thank you. – user55111 Dec 16 '15 at 10:46
  • Yes, answer to the related question describes correct way for read files. As for other problems with your code: 1. `__KERNEL__` and `MODULE` macros are defined by build system internally, they shouldn't be defined by the code itself. 2. Better way for initialize `filename` is `char filename[] = "...";`. 3. Declaration of `mm_segment_t old_fs` should be at the beginning of the function (among other declarations). 4. `#include` normally uses short(relative) path to the header: `#include `. – Tsyvarev Dec 16 '15 at 10:55
  • BTW, I have understood your question in the original revision: it precisely described your *actual* problem (and **title was meaningful**). Current title is not so usefull, as it describes error message, which meaning is already known for you (or should be known after reading related question). – Tsyvarev Dec 16 '15 at 11:03

0 Answers0