How to calculate TCP checksum

后端 未结 3 1128
自闭症患者
自闭症患者 2021-02-06 08:46

I am writing a Kernel Module that uses Netfilter hooks to modify some of the TCP header information and obviously, before sending, I want to re-calculate the checksum.
I al

3条回答
  •  不要未来只要你来
    2021-02-06 09:23

    Here is an example which combine netfilter API + checksum for TCP (not IP):

    http://www.linuxvirtualserver.org/software/tcpsp/index.html

    Look into the file called tcpsp_core.c.

        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;
    

    (notice it is assigned zero first, checksum calculated, then IP checksum indicated as not needed).

    Depends on which netfilter module u load (there are many!!!) they will work at different layer, eg, iptable working at IP layer is shown below (image):

    http://ars.sciencedirect.com/content/image/1-s2.0-S1389128608004040-gr3.jpg

提交回复
热议问题