c - How to calculate TCP checksum -


I am writing a kernel module that uses the netfilter hook to modify some TCP header information and obviously Before sending, I want to calculate the checksum again.
I also edit headers in the recipient side, so I also need to re-calculate there too.

On searching online, some people say that I can just set it up on 0 and it will be calculated for me, apparently it does not work
I have also got this function

  tcp_v4_send_check (struct sock * sk, struct sk_buff * skb); Although   

Although someone explained how it is used, and can I actually use it when I receive / send it the same way.
My first attempt was to set checksum to 0. I have a Scab and I have Scab-> Spey I have, there is still nothing I can

So, what is the direct way to calculate the checksum of TCP datagram? To re-calculate the checksum, you can calculate an incremental checksum - just modify it based on modifying the existing checksum. Instead of reading the entire packet, the fields you changed

When you change the packet, then it should be done, when you know the old values ​​and new values.

The basic idea is tcp-> Check + = (new_value - old_value) . This is a bit more complicated than this because:
1. old_val and new_val need to be a 16-bit value, which are aligning on 2 bytes (For example, changing a port number). 2. Checksum uses complementary people of arithmetic, so you need to "take feedback" basically means that if tcp-> Check + new_val - old_val is negative, so you have to subtract 1 from the result.

Comments