Skip to content

Fix TCP checksum calculation to include Ethernet header#1331

Closed
marcusb wants to merge 1 commit intoFreeRTOS:mainfrom
marcusb:fix-tcp-checksum
Closed

Fix TCP checksum calculation to include Ethernet header#1331
marcusb wants to merge 1 commit intoFreeRTOS:mainfrom
marcusb:fix-tcp-checksum

Conversation

@marcusb
Copy link
Copy Markdown
Contributor

@marcusb marcusb commented Apr 12, 2026

Summary

Fixes a bug in FreeRTOS+TCP where the TCP checksum was calculated with incorrect data length when ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0.

Problem

In prvTCPReturnPacket_IPV4(), the TCP checksum was calculated using pxNetworkBuffer->xDataLength which at that point did NOT include the Ethernet header size, resulting in incorrect checksums on outgoing TCP packets.

Fix

The fix calculate the total length (ulLen + ipSIZE_OF_ETH_HEADER) before calling usGenerateProtocolChecksum(), paralleling the IPv6 implementation which already correctly includes the Ethernet header.

Test

Added regression test test_prvTCPReturnPacket_TxChecksum_DataLength that verifies usGenerateProtocolChecksum is called with the correct data length (ulLen + ipSIZE_OF_ETH_HEADER).

  • Without fix: Test FAILS (wrong length passed)
  • With fix: Test PASSES (correct length passed)

When ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM is 0, the TCP checksum was
calculated with xDataLength that did not include the Ethernet header
size, resulting in incorrect checksums on outgoing TCP packets.

This fix parallels the IPv6 implementation which already correctly
includes the Ethernet header size (ipSIZE_OF_ETH_HEADER) in the
checksum calculation.

Add regression test that verifies usGenerateProtocolChecksum is called
with the correct data length (ulLen + ipSIZE_OF_ETH_HEADER).
@marcusb marcusb closed this Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant