URN Logo
UNIX Resources » Linux » Linux Forum » Linux Kernel Support » Page.10 » Some Help Needed With Programming My Kernel Module
announcement The content of this page is collected from Linux Forum, All copyrights and other associated rights are reserved by the original authors of the articles.
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.com
  LinuxForum General Chat
  Linux Advocacy
  LinuxForum Polls
  Introductions
  Linux Kernel Support
  Patch Management
  Development Release
  Linux Programming
  Linux Security
  Linux Software
  Linux Hardware Problems
    Linux Video Problems
    Linux Sound Problems
  Linux Networking Support
  Linux Printing Support
  Linux Human Interface Devices Support
  Linux Data Storage Support
  Linux Applications Support
  Linux Installation Support
  Linux Laptops Support
  Linux Motherboard, Chipsets, CPU, Memory
  Miscellaneous
  Debian Linux Support
  Ubuntu Linux Support
  LiveCD Discussions
  Gentoo Linux Support
  Mandrake Linux Support
  Redhat / Fedora Linux Support
  Slackware Linux Support
  SuSE Linux Support
  CentOS Linux Support
  Linux Web Servers
  Linux DNS Servers
  Linux Database Servers
  Linux Email Servers
  Linux FTP Servers
  Linux Squid Proxy Server
  Linux Samba Help
  Linux cPanel Help
  Linux Ensim Help
  Linux Plesk Help
  Linux Webmin / Usermin Help
  Qmail Toaster Help
  Linux Games
  Windows Game Emulation
  Linux Discussions
  General Linux Discussions
  Red Hat Linux Discussions
  More Red Hat Linux Discussions
  Mandrake Linux Discussions
  Slackware Linux Discussions
  SuSE Linux Discussions
  Debian Discussions
  Samba Help
  Linux Security
  Linux Networking
  Gentoo Help
  Operating System Rant Forum
  Hardware Rants
   
Some Help Needed With Programming My Kernel Module
Subject: Some Help Needed With Programming My Kernel Module
Author: kannerke    Posted: 2005-04-19 15:24:37    Length: 6,624 byte(s)
[Original] [Print] [Top]

Hello,

I'm programming a kernel module to intercept certain UDP-packets and add some new headers to them.  The new headers

must come before the original IP-header.  So, after the operation, I want to send a packet like this:

new IP header|new UPD header|overlay header|original IP header|original UDP header|original payload

When I try my code, I get a kernel panic.  I can't find what I'm doing wrong here.


Here's a fragment of my code:


....

   if ((sk->nh.iph->protocol == IPPROTO_UDP) && (enter_network)) {
      
      print_string("UDP datagram entering overlay network");
      
      /* allocate space for temporary headers */
      tmp_iph = kmalloc(sizeof(struct iphdr),GFP_NFS);
      tmp_udph = kmalloc(sizeof(struct udphdr),GFP_NFS);
      
      /* copy original IP header */
      __memcpy(tmp_iph,sk->nh.iph,sizeof(struct iphdr));
      
      
      /* copy original UDP header */
      __memcpy(tmp_udph,(struct udphdr *)(sk->data +(sk->nh.iph->ihl * 4)),sizeof(struct udphdr));

      
      overlayh = (struct overlayhdr *)skb_push(sk,sizeof(struct overlayhdr));
      //complete overlay-header
      overlayh->qos0 = qos0;
      overlayh->qos1 = qos1;
      overlayh->dst = tmp_iph->daddr;         
      overlayh->dstport = tmp_udph->dest;
      overlayh->lsthp = *(unsigned int*)lasthop_ip;
      
               /*expand for new udp header */
               udph = (struct udphdr *)skb_push(sk,sizeof(struct udphdr));
      
      //complete udp-header
      udph->source = tmp_udph->source;
      udph->dest = htons(overlay_port);
      udph->len = htons(htons(tmp_iph->tot_len) + sizeof(struct udphdr) + sizeof(struct overlayhdr));
      udph->check = 0;
      print_string("SFSG 7\n");

      /* calculate checksum */
      udph->check = csum ((unsigned short *)udph, htons(udph->len) >> 1);
      
      
      /* expand for new ip header */
      iph = (struct iphdr *)skb_push(sk,sizeof(struct iphdr));
      
      //complete new ip-header
      iph->ihl = 5;
      iph->version = 4;
      iph->tos = 0;
      iph->tot_len = htons(htons(tmp_iph->tot_len) + sizeof(struct iphdr) + sizeof(struct udphdr) +

sizeof(struct overlayhdr));
      iph->id = tmp_iph->id;
      iph->frag_off = tmp_iph->frag_off;
      iph->ttl = tmp_iph->ttl;
      iph->protocol = 17;      /* UDP */
      iph->check = 0;   /* set it to 0 before computing the actual checksum later */
      iph->saddr = tmp_iph->saddr;
      iph->daddr = *(unsigned int*)overlay_ip;
   
      /* calculate checksum */
      iph->check = csum ((unsigned short *)iph, sizeof(struct iphdr) >> 1);
      
      
      //free used memory
      kfree(tmp_iph);
      kfree(tmp_udph);
      
      return NF_ACCEPT;

}


/* Initialisation routine */
static int hooks_init() {
   /* Fill in our hook structure */
   nfho.hook      = hook_func;         /* Handler function */
   nfho.hooknum   = NF_IP_PRE_ROUTING; /* First hook for IPv4 */
   nfho.pf        = PF_INET;
   nfho.priority  = NF_IP_PRI_FIRST;   /* Make our function first */
   
   nf_register_hook(&nfho);
   
   return 0;
   
};


       
/* Cleanup routine */
static void hooks_exit() {
   nf_unregister_hook(&nfho);
};



module_init(hooks_init);
module_exit(hooks_exit);



enter_network = 1 when the packet needs to be expanded.
sk is a (struct sk_buff *)

I guess I'm doing some things wrong because of the kernel panic I get, but I don't know what I did wrong.  I think

it has something to do with the skb_push operation.  All help is welcome or when someone has a good example for

doing this kind of operations, please let me know


regards,
Peter
[Original] [Print] [Top]
Subject: Some Help Needed With Programming My Kernel Module
Author: x86processor    Posted: 2005-04-20 04:21:03    Length: 168 byte(s)
[Original] [Print] [Top]
You can put printk statements after every statement and see where you hit the kernel panic. Not the best debugging solution, but, something you can try,

x86
----
Linux is the kernel. The entire system is called GNU/Linux.
http://www.gnu.org/gnu/linux-and-gnu.html

My domain: shakthimaan.com (Offline)
orkut ID: shakthimaan
IRC nick: mbuf
[Original] [Print] [Top]
Subject: Some Help Needed With Programming My Kernel Module
Author: kannerke    Posted: 2005-04-24 15:48:38    Length: 806 byte(s)
[Original] [Print] [Top]
Hi,

I could solve the problem, but now I allready have another one.

first, I'll tell how I solved it: instead of a push, I used the put-operation.  Then I copied the whole packet to memory and copied it back to de sk_buff, but with an offset, as big as the size of the header I want to put in front of the packet.  I'll try to change this with an operation that can copy memory that overlaps.

The problem I have now is that the modified packet doesn't arrive.  There are two possibilities I can find: 1) the checksum is't correct, 2) there's a problem with the way I copy the original headers (because I use this data to fill in the new headers), but I checked some fields (daddr and ttl from iphdr and dest from udphdr and they were ok.
[Original] [Print] [Top]
« Previous thread
How "build Option" In Menuconfig Work
Linux Kernel Support
Page. 10
Next thread »
Issues Upgrading From Kernel 2.4 To 2.6
     

Copyright © 2007 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
Powered by FreeBSD    webmaster: webmaster@unixresources.net
This page created on 2007-08-01 11:43:06, cost 0.043226957321167 ms.