EvilZone

Programming and Scripting => C - C++ => : NeX June 27, 2012, 12:30:55 AM

: [C, REQ] Netfilter re-calculating checksum [SOLVED!]
: NeX 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:
:

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:
: (C)

   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)
: Re: [C, REQ] Netfilter re-calculating checksum
: NeX June 27, 2012, 03:06:32 AM
: (c)
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 ;)