Skip to contentSkip to conversation

checksum calculation for an OUTPUT DNATted packet

DISCUSSION Giacomo 2 participants 2 messages Jan 2, 2009 netfilter

lore
Share & tools
lore

Giacomo

author
#0 lore raw

Hi to everybody.

Implementing a kernel module that changes an IP or port in a packet in OUTPUT (realizing the so
called OUTPUT DNAT), I have to recalculate the checksum of the packet.

It is not clear to me if I have to use tcp_v4_check or csum_tcpudp_magic.

csum_tcpudp_magic works fine like this:

iph->check = 0;
iph->check = ip_fast_csum((u8 *) iph, iph->ihl);

th->check = csum_tcpudp_magic(iph->saddr, iph->daddr, datalen, IPPROTO_TCP, skb_checksum(skb, iph->ihl * 4, datalen, 0));

where datalen = skb->len - iph->ihl * 4;

and skb is struct sk_buff.

Instead the tcp_v4_check(), used as follows, does not work:

struct tcphdr tcphead;
iph->check = 0;
iph->check = ip_fast_csum((u8 *) iph, iph->ihl);

th = skb_header_pointer(skb, iph->ihl * 4,

                                       sizeof(tcphead), &tcphead);

th->check = tcp_v4_check(skb->len - 4 * iph->ihl, iph->saddr, iph->daddr,

                               csum_partial((char *) th, len - 4 *

iph->ihl, 0));

Can someone tell me where I am wrong?

I need to use a generic function to (re) calculate the checksum, also compatible with the
older kernels (and so I cannot use inet_proto_csum_replace4() - or inet_proto_csum_replace2 - for instance).

Another particular: tcp_v4_check() used as above, works when applied to packet DNATted in PRE-ROUTING,
while it does not in OUTPUT DNAT, as in the subject of this message.

Thanks in advance.

Giacomo.

Signature · 21 lines
Giacomo S.
http://www.giacomos.it

- - - - - - - - - - - - - - - - - - - - - -

* Aprile 2008: iqfire-wall, un progetto
  open source che implementa un
  filtro di pacchetti di rete per Linux,
  e` disponibile per il download qui:
  http://sourceforge.net/projects/ipfire-wall

* Informazioni e pagina web ufficiale:
  http://www.giacomos.it/iqfire/index.html

- - - - - - - - - - - - - - - - - - - - - -

 . ''  `.
:   :'    :
 `.  ` '
    `- Debian GNU/Linux -- The power of freedom
        http://www.debian.org
#1 lore raw
Quoted 7 lines · Giacomo

Hi to everybody.

Implementing a kernel module that changes an IP or port in a packet in OUTPUT (realizing the so
called OUTPUT DNAT), I have to recalculate the checksum of the packet.

It is not clear to me if I have to use tcp_v4_check or csum_tcpudp_magic.

See the RAWDNAT branch of Xtables-addons where this is already done (at least I hope it's correct - have not done much with it lately):

http://dev.medozas.de/gitweb.cgi?p=xtables-addons;a=blob;f=extensions/xt_RAWNAT.c;hb=RAWNAT#l73