TCP Checksums in mbufs

Lee Brotherston lee at nerds.org.uk
Mon Jan 8 23:29:03 UTC 2007


On Mon, Jan 08, 2007 at 02:00:14PM -0800, Julian Elischer wrote:
> there is an algorythm to recalculate the tcp/ip
> checksum when you replace a byte. you subtract the old value from the 
> csum and add the new one, but not quite a as easy as that.
> 
> I think it's given in one of the RFCs but  I think it may also
> be used in the tcpmss port, or possibly the mss fixup code in ppp.
> I know I've used it somewhere but forget where :-)

Aha!  Would it be this one per chance?

/*-
* The following macro is used to update an
* internet checksum.  "acc" is a 32-bit
* accumulation of all the changes to the
* checksum (adding in old 16-bit words and
* subtracting out new words), and "cksum"
* is the checksum value to be updated.
*/
#define ADJUST_CHECKSUM(acc, cksum) { \
        acc += cksum; \
        if (acc < 0) { \
                acc = -acc; \
                acc = (acc >> 16) + (acc & 0xffff); \
                acc += acc >> 16; \
                cksum = (u_short) ~acc; \
        } else { \
                acc = (acc >> 16) + (acc & 0xffff); \
                acc += acc >> 16; \
                cksum = (u_short) acc; \
        } \
}


If so I'll set about using this... once I work out what the 32-bit
accumulation bit is :)

Thanks!

  Lee

-- 
Lee Brotherston - <lee at nerds.org.uk>


More information about the freebsd-hackers mailing list