Re: tcp checksum calculation incorrect - off by 2 - Solved
Re: tcp checksum calculation incorrect - off by 2 - Solved
- Subject: Re: tcp checksum calculation incorrect - off by 2 - Solved
- From: Raj Hariginadoni <email@hidden>
- Date: Fri, 27 Jul 2007 22:00:17 -0400
I have solved the checksum issue that I had encountered.
The way to calcuate tcp pseudo checksum for all packet sizes is to use
tcph->th_sum = in_pseudo(iph->ip_src.s_addr, iph->ip_dst.s_addr,
htonl((iph->ip_p << 16) | (tcph->th_off * 4);
instead of using the calculated (tcplen & 0xffff).
Explanation: (from what I understand)
The ethernet frame has to be 64 bytes long, if not zeros are padded at the
end just before the ethernet FCS. In my case the packet was 44 bytes long.
44 (ip datagram) + 14(ethernet header) + 4 (FCS) = 62. Which is 2 less than
64. Zeros are padded to make it 64. In my code, the tcplen included the
zeros also. So the tcplen was wrong. So it is better to use the tcp offset
to calculate pseudo header.
Raj Hariginadoni wrote....
Hi Folks,
I am looking at a driver code for OS X 10.3 where the checksum is
being calculated on an inbound syn-ack. First by zeroing out the
checksum, then calculating the checksum of the pseudo header and
finally calculating the checksum. The checksum functions used are
available in the xnu sources.
Here is the snippet
printf("tcplen =%x",tcplen);
printf(" checksum Before=%x,\n",tcph->th_sum);
tcph->th_sum=0;
tcph->th_sum = in_pseudo(iph->ip_src.s_addr, iph->ip_dst.s_addr,
htonl((iph->ip_p << 16) | (tcplen & 0xffff)));
tcph->th_sum = in_cksum_skip(mbuf, packet_len, iph->ip_hl << 2);
printf("final checksum tcph=%x,\n",tcph->th_sum);
The final checksum is less by 2 than the original. Here is the sample
output
tcplen=26
checksum before =7182
final checksum=7180
tcplen=26
checksum before =2d87
final checksum=2d85
tcplen=26
checksum before =5ab0
final checksum=5aae
I have also observed that the checksum is calculated correctly when
the length of the tcp segment in the packet is 40.
tcplen=40
checksum before =d1cf
final checksum=d1cf
tcplen=40
checksum before =e237
final checksum=e237
Can someone guide me in solving this problem?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden