From: "Richard B. Johnson" <root@chaos.analogic.com>
To: Vishwas Raman <vishwas@eternal-systems.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: Incremental update of TCP Checksum
Date: Tue, 16 Sep 2003 15:47:50 -0400 (EDT) [thread overview]
Message-ID: <Pine.LNX.4.53.0309161533030.30081@chaos> (raw)
In-Reply-To: <3F675B68.8000109@eternal-systems.com>
On Tue, 16 Sep 2003, Vishwas Raman wrote:
> Hi all,
>
> I have a very simple question, which a lot of you would have solved. I
> am intercepting a TCP packet, which I would like to change slightly.
>
> Let's say, I change the doff field of the tcp-header (for eg: increase
> it by 1). I know it is wrong just to change the doff field without
> increasing the packet length, but lets say I do it just as a test. Since
> I changed a portion of the tcp header, I have to update the tcp checksum
> too right!!! If so, what is the best way to do so, without having to
> recalculate the entire tcp checksum (I know how to recalculate the
> checksum from scratch).
>
> Can anyone out there tell me the algorithm to update the checksum
> without having to recalculate it.
>
> I tried the following algorithm but it didnt work. The packet got
> rejected as a packet with bad cksum.
>
> void changePacket(struct sk_buff* skb)
> {
> struct tcphdr *tcpHdr = skb->h.th;
> // Verifying the tcp checksum works here...
> tcpHeader->doff += 1;
> long cksum = (~(tcpHdr->check))&0xffff;
> cksum += 1;
> while (cksum >> 16)
> {
> cksum = (cksum & 0xffff) + (cksum >> 16);
> }
> tcpHeader->check = ~cksum;
> // Verifying tcp checksum here fails with bad cksum
> }
>
> Any pointers/help in this regard will be highly appreciated...
The TCP/IP checksum is a WORD sum (unsigned short) in which
any overflow out of the word causes the word to be incremented.
The final sum is then inverted to become the checksum. Note that
many algorithms sum into a long then fold-back the bits. It's
the same thing, different method.
Therefore:
Given an existing checksum of 0xffff, if the
next word to be summed is 0x0001, the result
will be 0x0001 because adding 1 to 0xffff makes
it 0, causing an overflow which propagates to
become 0x0001.
So:
Clearly, information is lost because one doesn't
know how the 0x0001 was obtained.
If I were to modify a low byte somewhere by subtracting 1,
would I know that the new checksum, excluding the inversion,
was 0x0000? No. It could be 0xffff.
This presents a problem when trying to modify existing checksums.
It's certainly easier to set the existing checksum to 0, then
re-checksum the whole packet. It's probably faster than some
looping algorithm that attempts to unwind a previous checksum.
Cheers,
Dick Johnson
Penguin : Linux version 2.4.22 on an i686 machine (794.73 BogoMips).
Note 96.31% of all statistics are fiction.
next prev parent reply other threads:[~2003-09-16 19:45 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-08-14 22:06 Netfiltering - NF_IP_LOCAL_OUT - how it works??? Vishwas Raman
2003-08-21 13:49 ` Harald Welte
2003-08-21 16:44 ` Vishwas Raman
2003-09-16 18:50 ` Incremental update of TCP Checksum Vishwas Raman
2003-09-16 19:00 ` Valdis.Kletnieks
2003-09-16 20:32 ` Vishwas Raman
2003-09-16 20:47 ` Leo Mauro
2003-09-17 3:28 ` Raf D'Halleweyn
2003-09-17 4:43 ` David S. Miller
2003-09-17 13:20 ` Richard B. Johnson
2003-09-17 20:34 ` Jamie Lokier
2003-09-16 19:47 ` Richard B. Johnson [this message]
2003-09-16 20:21 ` Vishwas Raman
2003-09-16 20:34 ` Richard B. Johnson
2003-09-16 20:35 ` Jesper Juhl
2003-09-17 1:37 ` Lincoln Dale
2003-09-17 1:39 ` Jesper Juhl
2003-09-16 22:41 ` Jamie Lokier
2003-09-16 23:32 ` Vishwas Raman
2003-09-16 20:33 ` Patrick McHardy
[not found] <kysi.5h.17@gated-at.bofh.it>
[not found] ` <mZy6.3NX.7@gated-at.bofh.it>
[not found] ` <wtdD.3EP.13@gated-at.bofh.it>
[not found] ` <wtnf.3Zv.9@gated-at.bofh.it>
[not found] ` <wuMz.65Q.15@gated-at.bofh.it>
[not found] ` <wBkH.7Sv.3@gated-at.bofh.it>
[not found] ` <wKxN.5h0.7@gated-at.bofh.it>
2003-09-17 14:06 ` Ihar 'Philips' Filipau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.53.0309161533030.30081@chaos \
--to=root@chaos.analogic.com \
--cc=linux-kernel@vger.kernel.org \
--cc=vishwas@eternal-systems.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®