This forum is in archive mode. You will not be able to post new content.

Author Topic: [C, REQ] Netfilter re-calculating checksum [SOLVED!]  (Read 1401 times)

0 Members and 1 Guest are viewing this topic.

Offline NeX

  • Peasant
  • *
  • Posts: 74
  • Cookies: 5
    • View Profile
[C, REQ] Netfilter re-calculating checksum [SOLVED!]
« on: June 27, 2012, 12:30:55 AM »
I didn't know where to put this... I hope it's in the right section..
Anyway, my issue is:
I'm working on a project where I need to change the destination IP address.. But when I do, I also need to re-calculate the checksum of the IP header.
Does anyone know a sufficient and a working way to do this? I'm using a function to calculate the checksum which I found it on the internet.
And, yes, I'm doing this in kernel space (kernel module)..


The function:
Code: [Select]

unsigned short csum(unsigned short *buf, int nwords){


   unsigned long sum;


   for(sum=0; nwords>0; nwords--)
      sum += *buf++;
     
   sum = (sum >> 16) + (sum &0xffff);
   sum += (sum >> 16);


   return ~sum;


}


How I calculate the checksum of the IP header:
Code: (C) [Select]

   ip_send_check(ip_header);
     ip_header->check = csum((unsigned short *)ip_header, ip_header->ihl * 4);
And this makes bad checksum (checked it with Wireshark)
« Last Edit: June 27, 2012, 03:07:05 AM by NeX »

Offline NeX

  • Peasant
  • *
  • Posts: 74
  • Cookies: 5
    • View Profile
Re: [C, REQ] Netfilter re-calculating checksum
« Reply #1 on: June 27, 2012, 03:06:32 AM »
Code: (c) [Select]
ip_fast_csum(&ip_header, ip_header->ihl);

Fixed the checksum errors, no need for csum, actually.. Oh, and you'll have to include asm/checksum.h ;)

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.