重新计算Linux内核模块中的TCP校验和

时间:2017-08-31 16:35:03

标签: c linux tcp linux-kernel netfilter

我正在构建一个Netfilter模块,它修改发往特定端口的数据包的TCP有效负载。我既不修改IP标头也不修改TCP标头。在收到数据包(NF_INET_PRE_ROUTING)后,直接在Netfilter的第一个点调用该模块。因此,我必须重新计算每个修改过的数据包中的TCP校验和字段。我已经在这里看到了一些帖子,并在那里使用方法来重新计算TCP校验和,但这些方法都不起作用。以下是我使用的两种方法:

方法1:

tcplen = (skb->len - (iph->ihl << 2)); /* tcplen is the length of the 
                                        * skb - the ip-header length */
tcph->check = 0;
tcph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
                                tcplen, IPPROTO_TCP,
                                csum_partial(tcph, tcplen, 0));

skb->ip_summed = CHECKSUM_UNNECESSARY;

方法2:

tcph->check = ~(~tcph->check + ~new_field + new_field);
skb->ip_summed = CHECKSUM_UNNECESSARY;

在这两种情况下,我收到以下错误:

  

verif_dss_csum csum错误:0x874a data_seq 2760801057   dss_csum_added 1溢出0次迭代1

针对此问题的任何解决方案?我正在为Linux Kernel 4.4.83

开发我的模块

2 个答案:

答案 0 :(得分:1)

**以上代码仅用于计算伪标头的校验和** 这是用于计算IP头校验和和TCP / UDP校验和

的代码
void UpdateChecksum(char *prefix,struct sk_buff *skb){
struct iphdr *ip_header;

ip_header = ip_hdr(skb);
skb->ip_summed = CHECKSUM_NONE; //stop offloading
skb->csum_valid = 0;
ip_header->check = 0;
ip_header->check = ip_fast_csum((u8 *)ip_header, ip_header->ihl);


if ( (ip_header->protocol == IPPROTO_TCP) || (ip_header->protocol == IPPROTO_UDP) ) {

  if(skb_is_nonlinear(skb))
      skb_linearize(skb);  // very important.. You need this.

  if (ip_header->protocol == IPPROTO_TCP) {
     struct tcphdr *tcpHdr;
     unsigned int tcplen;

     tcpHdr = tcp_hdr(skb);
     skb->csum =0;
     tcplen = ntohs(ip_header->tot_len) - ip_header->ihl*4;
     tcpHdr->check = 0;
     tcpHdr->check = tcp_v4_check(tcplen, ip_header->saddr, ip_header->daddr, csum_partial((char *)tcpHdr, tcplen, 0));

     //printk(KERN_INFO "%s: TCP Len :%d, Computed TCP Checksum :%x : Network : %x\n",prefix,tcplen,tcpHdr->check,htons(tcpHdr->check));

  } else if (ip_header->protocol == IPPROTO_UDP) {
       struct udphdr *udpHdr;
       unsigned int udplen;

       udpHdr = udp_hdr(skb);
       skb->csum =0;
       udplen = ntohs(ip_header->tot_len) - ip_header->ihl*4;
       udpHdr->check = 0;
       udpHdr->check = udp_v4_check(udplen,ip_header->saddr, ip_header->daddr,csum_partial((char *)udpHdr, udplen, 0));;

       //printk(KERN_INFO "%s: UDP Len :%d, Computed UDP Checksum :%x : Network : %x\n",prefix,udplen,udpHdr->check,htons(udpHdr->check));
  }

}

}

答案 1 :(得分:1)

如果仅更改tcp有效负载,则无需重新计算 ipv4标头校验和。 因此,如果我对您的理解正确,那应该只是:

/* ... */
struct iphdr *ip_header = ip_hdr(skb);
if (ip_header->protocol == IPPROTO_TCP) {
    struct tcphdr *tcp_header = tcp_hdr(skb);
    if ((unsigned int)ntohs(tcp_header->dest) == some_port) {
        unsigned char *data = (char *)tcp_header + tcp_hdrlen(skb);
        /* altering TCP-payload */
        tcp_header->check = 0;
        tcp_header->check = tcp_v4_check(ntohs(ip_header->tot_len) - (ip_header->ihl << 2),
                                     ip_header->saddr, ip_header->daddr,
                                     csum_partial(tcp_header, tcp_header->doff << 2, 0));
/* ... */
/* e.g return NF_ACCEPT */