1
C - C++ / [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:
How I calculate the checksum of the IP header:
And this makes bad checksum (checked it with Wireshark)
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
- ip_send_check(ip_header);
- ip_header->check = csum((unsigned short *)ip_header, ip_header->ihl * 4);
