简体   繁体   中英

TCP checksum C code returns wrong result

I am getting a large checksum result for TCP, when it is supposed to be 0. I am approaching this by copying the TCP psuedoheader into the first 12 bytes of an array, then copy the TCP header and data into the next several bytes of the array, and pass that array and its length to the checksum function. I cannot figure out why the checksum is significantly different from the correct value (ie 9180 for one of the packets packet when it should be 0).

Here are my IP, TCP and pseudo TCP headers:

typedef struct __attribute__((__packed__)) IPHeader {
  #if __BYTE_ORDER__ == __LITTLE_ENDIAN__
  uint8_t hdrLen:4;
  uint8_t version:4;
  #else
  uint8_t version:4;
  uint8_t hdrLen:4;
  #endif
  uint8_t TOS;
  uint16_t totLen;
  uint16_t id;
  uint16_t offset;
  #define DF 0x4            
  #define MF 0x2           
  #define OFF 0 
  uint8_t TTL;
  uint8_t protocol;
  uint16_t checksum;
  struct in_addr srcIP;
  struct in_addr destIP; 
}IPHeader;

typedef struct __attribute__((__packed__)) TCPHeader {
  uint16_t srcPort;
  uint16_t destPort;
  uint32_t seqNum;
  uint32_t ackNum;
  uint8_t offset:4;
  uint8_t res:4;
  uint8_t flags;
  uint16_t window;
  uint16_t checksum;
  uint16_t urg;
}TCPHeader;

typedef struct __attribute__((__packed__)) TCPPseudo {
  struct in_addr srcAddr;
  struct in_addr destAddr;
  uint8_t zeroes;
  uint8_t protocol;
  uint16_t len;
}TCPPseudo;

Here is how I am copying the data into the array:

IPHeader *iph
TCPPseudo ts;
TCPHeader *tcp;
u_char checkData[1000];
uint16_t checkLen = 0;
---
tcp = (TCPHeader *)(packet + ETHER_SIZE + iph->hdrLen * 4);
ts.srcAddr = iph->srcIP;
ts.destAddr = iph->destIP;
ts.zeroes = 0;
ts.protocol = iph->protocol;
checkLen = ntohs(iph->totLen) - (iph->hdrLen * 4);
ts.len = checkLen;
memcpy(checkData, &ts, 12);
memcpy(checkData + 12, tcp, checkLen);
getTCPInfo(tcp, iph, checkData, checkLen);
---
unsigned short checksum = in_cksum((unsigned short *)checkData, len + 12);
printf("\t\tChecksum: %d, (0x%x)", checksum, htons(tcp->checksum)); 

Here is how I am calculating the checksum:

unsigned short in_cksum(unsigned short *addr,int len)
{
    register int sum = 0;
    u_short answer = 0;
    register u_short *w = addr;
    register int nleft = len;

    /*
     * Our algorithm is simple, using a 32 bit accumulator (sum), we add
     * sequential 16 bit words to it, and at the end, fold back all the
     * carry bits from the top 16 bits into the lower 16 bits.
     */
    while (nleft > 1)  {
            sum += *w++;
            nleft -= 2;
    }

    /* mop up an odd byte, if necessary */
    if (nleft == 1) {
            *(u_char *)(&answer) = *(u_char *)w ;
            sum += answer;
    }

    /* add back carry outs from top 16 bits to low 16 bits */
    sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
    sum += (sum >> 16);                     /* add carry */
    answer = ~sum;                          /* truncate to 16 bits */
    return(answer);
}

You should save the TCP checksum of the received packet, then set the checksum field to zero in the tcp header before calculating the checksum over the pseudo header and TCP segment. Then match the calculated checksum with the previous saved value. Both should be equal if there are no errors in transmission over the network. Do this

recvPktChkSum = tcp->checksum
tcp->checksum = 0;

also before calculating the checksum you should make sure that length is in network bytes order

ts.len = htons(checkLen);

before

memcpy(checkData + 12, tcp, checkLen);

then after

checksum = in_cksum((unsigned short *)checkData, len + 12);

compare

if(htons(recvPktChkSum) == htons(checksum))
   printf("checksum valid"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM