All of lore.kernel.org
 help / color / mirror / Atom feed
* How to calculate the TCP checksum
@ 2012-07-10  6:12 Adel Kodmani
  2012-07-16 10:30 ` पारस
       [not found] ` <CAHnt0GXSSP9oep0aUNbME62gJnYot7bSMzh5LQC2imbn0coLng@mail.gmail.com>
  0 siblings, 2 replies; 5+ messages in thread
From: Adel Kodmani @ 2012-07-10  6:12 UTC (permalink / raw)
  To: kernelnewbies

I am writing a kernel module that actually modifies the TCP header(both at
sending and receiving) and I need to re-calculate the header because of
that.
I am capturing the packets using netfilter, so I have access to the whole
SKB, I am using Linux 2.6.35-22

Looking into the source code of the TCP/IP, I found this function
void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
The comment above it says this routing computes the IPv4 TCP checksum

Trying to use this function, I called it passing to it skb->sk and skb
With no success so far.

So my question is, how do I correctly calculate the TCP checksum? I know
there's a psudoheader that must be created, and added to the calculation
but there must be a function in the kernel I can call that already does
that.


Thanks in advance,
Adel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120710/dc27d975/attachment.html 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* How to calculate the TCP checksum
  2012-07-10  6:12 How to calculate the TCP checksum Adel Kodmani
@ 2012-07-16 10:30 ` पारस
  2012-07-16 11:42   ` Adel Kodmani
       [not found] ` <CAHnt0GXSSP9oep0aUNbME62gJnYot7bSMzh5LQC2imbn0coLng@mail.gmail.com>
  1 sibling, 1 reply; 5+ messages in thread
From: पारस @ 2012-07-16 10:30 UTC (permalink / raw)
  To: kernelnewbies

On Tue, Jul 10, 2012 at 11:42 AM, Adel Kodmani <mpcadel@gmail.com> wrote:

> I am writing a kernel module that actually modifies the TCP header(both at
> sending and receiving) and I need to re-calculate the header because of
> that.
> I am capturing the packets using netfilter, so I have access to the whole
> SKB, I am using Linux 2.6.35-22
>
> Looking into the source code of the TCP/IP, I found this function
> void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
> The comment above it says this routing computes the IPv4 TCP checksum
>
> Trying to use this function, I called it passing to it skb->sk and skb
> With no success so far.
>
> So my question is, how do I correctly calculate the TCP checksum? I know
> there's a psudoheader that must be created, and added to the calculation
> but there must be a function in the kernel I can call that already does
> that.
>
>
> Thanks in advance,
> Adel
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
> I think this may be helpful to you
http://www.thegeekstuff.com/2012/05/ip-header-checksum/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120716/73d4add0/attachment.html 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* How to calculate the TCP checksum
  2012-07-16 10:30 ` पारस
@ 2012-07-16 11:42   ` Adel Kodmani
  2012-07-17  3:05     ` Pei Lin
  0 siblings, 1 reply; 5+ messages in thread
From: Adel Kodmani @ 2012-07-16 11:42 UTC (permalink / raw)
  To: kernelnewbies

On Mon, Jul 16, 2012 at 1:30 PM, ???? <beparas@gmail.com> wrote:

>
>
> On Tue, Jul 10, 2012 at 11:42 AM, Adel Kodmani <mpcadel@gmail.com> wrote:
>
>> I am writing a kernel module that actually modifies the TCP header(both
>> at sending and receiving) and I need to re-calculate the header because of
>> that.
>> I am capturing the packets using netfilter, so I have access to the whole
>> SKB, I am using Linux 2.6.35-22
>>
>> Looking into the source code of the TCP/IP, I found this function
>> void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
>> The comment above it says this routing computes the IPv4 TCP checksum
>>
>> Trying to use this function, I called it passing to it skb->sk and skb
>> With no success so far.
>>
>> So my question is, how do I correctly calculate the TCP checksum? I know
>> there's a psudoheader that must be created, and added to the calculation
>> but there must be a function in the kernel I can call that already does
>> that.
>>
>>
>> Thanks in advance,
>> Adel
>>
>> _______________________________________________
>> Kernelnewbies mailing list
>> Kernelnewbies at kernelnewbies.org
>> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>>
>> I think this may be helpful to you
> http://www.thegeekstuff.com/2012/05/ip-header-checksum/
>
>
>

Nope, what I need to calculate is the TCP checksum, not the IP checksum
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120716/f61a8392/attachment.html 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* How to calculate the TCP checksum
  2012-07-16 11:42   ` Adel Kodmani
@ 2012-07-17  3:05     ` Pei Lin
  0 siblings, 0 replies; 5+ messages in thread
From: Pei Lin @ 2012-07-17  3:05 UTC (permalink / raw)
  To: kernelnewbies

http://lxr.oss.org.cn/source/lib/checksum.c

2012/7/16 Adel Kodmani <mpcadel@gmail.com>:
>
>
> On Mon, Jul 16, 2012 at 1:30 PM, ???? <beparas@gmail.com> wrote:
>>
>>
>>
>> On Tue, Jul 10, 2012 at 11:42 AM, Adel Kodmani <mpcadel@gmail.com> wrote:
>>>
>>> I am writing a kernel module that actually modifies the TCP header(both
>>> at sending and receiving) and I need to re-calculate the header because of
>>> that.
>>> I am capturing the packets using netfilter, so I have access to the whole
>>> SKB, I am using Linux 2.6.35-22
>>>
>>> Looking into the source code of the TCP/IP, I found this function
>>> void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
>>> The comment above it says this routing computes the IPv4 TCP checksum
>>>
>>> Trying to use this function, I called it passing to it skb->sk and skb
>>> With no success so far.
>>>
>>> So my question is, how do I correctly calculate the TCP checksum? I know
>>> there's a psudoheader that must be created, and added to the calculation but
>>> there must be a function in the kernel I can call that already does that.
>>>
>>>
>>> Thanks in advance,
>>> Adel
>>>
>>> _______________________________________________
>>> Kernelnewbies mailing list
>>> Kernelnewbies at kernelnewbies.org
>>> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>>>
>> I think this may be helpful to you
>> http://www.thegeekstuff.com/2012/05/ip-header-checksum/
>>
>>
>
>
> Nope, what I need to calculate is the TCP checksum, not the IP checksum
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>



-- 
Best Regards
Lin

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Fwd: How to calculate the TCP checksum
       [not found]   ` <CA+=80ChEd-TCEYhtj68kMp8vXsxyAoOprsU2FAxCyTj3DQ1sLw@mail.gmail.com>
@ 2013-06-25  3:27     ` Adel Qodmani
  0 siblings, 0 replies; 5+ messages in thread
From: Adel Qodmani @ 2013-06-25  3:27 UTC (permalink / raw)
  To: kernelnewbies

On Tue, Jun 25, 2013 at 6:19 AM, Peter Teoh <htmldeveloper@gmail.com> wrote:

> c.   if your hardware is not capable to do TCP offloading, then the LONG
> way is to start from beginning of packet to end of packet, and calculate
> checksum.
>
I've tested that, my hardware apparently doesn't support TCP offloading.


>  d.   if u keep the previous checksum, ie (th->checksum=0 is not needed
> at all), and u know exactly which byte u modify, then just do a xor of the
> modified byte with the previous checksum, and u get the new
> checksum.....this is the preferred and fastest way.
>
> so in summary:
>
> th->check = 0;
> th->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
> datalen, iph->protocol,
> csum_partial((char *)th, datalen, 0));
> skb->ip_summed = CHECKSUM_UNNECESSARY;
>
> your setting of "0" above is correct, is because u want to calculate the
> checksum OVERALL....ie, packet start to packet end......demarcate correctly
> and u should get the answer....NOT csum_partial().....partial checksumming
> does not apply here.
>
> --
> Regards,
> Peter Teoh
>

Well, the problem is now solved, it seems that the error was with the fact
that some skbs are not linear and I was ignoring this fact.
Fixed it by the following:

                tcph = tcp_hdr(skb);
                if(skb_is_nonlinear(skb))
                                 skb_linearize(skb)

                tcplen = (skb->len - (iph->ihl << 2));/* tcplen is the
length of the skb - the ip-header length */
                tcph->check = 0;
                tcph->check = tcp_v4_check(tcplen,
                                iph->saddr,
                                iph->daddr,
                                csum_partial((char*) tcph, tcplen, 0));
                skb->ip_summed = CHECKSUM_NONE;/* in case another hardware
has TCP offloading */

Thanks a lot, anyhow :D
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130625/28b7e437/attachment.html 

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-06-25  3:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-10  6:12 How to calculate the TCP checksum Adel Kodmani
2012-07-16 10:30 ` पारस
2012-07-16 11:42   ` Adel Kodmani
2012-07-17  3:05     ` Pei Lin
     [not found] ` <CAHnt0GXSSP9oep0aUNbME62gJnYot7bSMzh5LQC2imbn0coLng@mail.gmail.com>
     [not found]   ` <CA+=80ChEd-TCEYhtj68kMp8vXsxyAoOprsU2FAxCyTj3DQ1sLw@mail.gmail.com>
2013-06-25  3:27     ` Fwd: " Adel Qodmani

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.