Skip to content

Commit

Permalink
Main/TCP4 : ACK number in TCP RESET reply to SYN packet (#724)
Browse files Browse the repository at this point in the history
* Main/TCP4 : ACK number in TCP RESET reply to SYN packet

* Typo fix

* Add unit-test for coverage; Fix ntohl to htonl

* Fix unit-test

---------

Co-authored-by: Nikhil Kamath <[email protected]>
Co-authored-by: Aniruddha Kanhere <[email protected]>
  • Loading branch information
3 people authored Feb 19, 2023
1 parent 1a29e5b commit 14cf915
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
12 changes: 11 additions & 1 deletion source/FreeRTOS_TCP_Transmission.c
Original file line number Diff line number Diff line change
Expand Up @@ -1463,10 +1463,20 @@
TCPPacket_t * pxTCPPacket = ( ( TCPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer );
const uint32_t ulSendLength =
( ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER ); /* Plus 0 options. */

uint8_t ucFlagsReceived = pxTCPPacket->xTCPHeader.ucTCPFlags;
pxTCPPacket->xTCPHeader.ucTCPFlags = ucTCPFlags;
pxTCPPacket->xTCPHeader.ucTCPOffset = ( ipSIZE_OF_TCP_HEADER ) << 2;

if( ( ucFlagsReceived & tcpTCP_FLAG_SYN ) != 0U )
{
/* A synchronize packet is received. It counts as 1 pseudo byte of data,
* so increase the variable with 1. Before sending a reply, the values of
* 'ulSequenceNumber' and 'ulAckNr' will be swapped. */
uint32_t ulSequenceNumber = FreeRTOS_ntohl( pxTCPPacket->xTCPHeader.ulSequenceNumber );
ulSequenceNumber++;
pxTCPPacket->xTCPHeader.ulSequenceNumber = FreeRTOS_htonl( ulSequenceNumber );
}

prvTCPReturnPacket( NULL, pxNetworkBuffer, ulSendLength, pdFALSE );
}
#endif /* !ipconfigIGNORE_UNKNOWN_PACKETS */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,33 @@ void test_prvTCPSendSpecialPacketHelper( void )
TEST_ASSERT_EQUAL( 0x50, pxTCPPacket->xTCPHeader.ucTCPOffset );
}

/* test prvTCPSendSpecialPacketHelper function */
void test_prvTCPSendSpecialPacketHelper_flagSYN( void )
{
BaseType_t Return = pdTRUE;
const uint32_t ulSequenceNumber = 0xABC12300;

pxSocket = &xSocket;
pxNetworkBuffer = &xNetworkBuffer;
pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
TCPPacket_t * pxTCPPacket = ( ( const TCPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer );

pxTCPPacket->xTCPHeader.ucTCPFlags = tcpTCP_FLAG_SYN;
pxTCPPacket->xTCPHeader.ucTCPOffset = 0;
pxTCPPacket->xTCPHeader.ulSequenceNumber = FreeRTOS_htonl( ulSequenceNumber );

usGenerateChecksum_ExpectAnyArgsAndReturn( 0x1111 );
usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 0x2222 );
eARPGetCacheEntry_ExpectAnyArgsAndReturn( eARPCacheHit );
xNetworkInterfaceOutput_ExpectAnyArgsAndReturn( pdTRUE );

Return = prvTCPSendSpecialPacketHelper( pxNetworkBuffer, tcpTCP_FLAG_ACK );
TEST_ASSERT_EQUAL( pdFALSE, Return );
TEST_ASSERT_EQUAL( tcpTCP_FLAG_ACK, pxTCPPacket->xTCPHeader.ucTCPFlags );
TEST_ASSERT_EQUAL( 0x50, pxTCPPacket->xTCPHeader.ucTCPOffset );
TEST_ASSERT_EQUAL( ulSequenceNumber + 1, FreeRTOS_ntohl( pxTCPPacket->xTCPHeader.ulAckNr ) );
}

/* test prvTCPSendChallengeAck function */
void test_prvTCPSendChallengeAck( void )
{
Expand Down

0 comments on commit 14cf915

Please sign in to comment.