From 14cf915805b03f71fe9092fcbadb8c587b42858d Mon Sep 17 00:00:00 2001 From: Hein Tibosch Date: Sun, 19 Feb 2023 16:32:23 +0800 Subject: [PATCH] Main/TCP4 : ACK number in TCP RESET reply to SYN packet (#724) * 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 <110539926+amazonKamath@users.noreply.github.com> Co-authored-by: Aniruddha Kanhere <60444055+AniruddhaKanhere@users.noreply.github.com> --- source/FreeRTOS_TCP_Transmission.c | 12 ++++++++- .../FreeRTOS_TCP_Transmission_utest.c | 27 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/source/FreeRTOS_TCP_Transmission.c b/source/FreeRTOS_TCP_Transmission.c index 154d0ac5b..bf1be2370 100644 --- a/source/FreeRTOS_TCP_Transmission.c +++ b/source/FreeRTOS_TCP_Transmission.c @@ -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 */ diff --git a/test/unit-test/FreeRTOS_TCP_Transmission/FreeRTOS_TCP_Transmission_utest.c b/test/unit-test/FreeRTOS_TCP_Transmission/FreeRTOS_TCP_Transmission_utest.c index a99d102e0..fdefe0689 100644 --- a/test/unit-test/FreeRTOS_TCP_Transmission/FreeRTOS_TCP_Transmission_utest.c +++ b/test/unit-test/FreeRTOS_TCP_Transmission/FreeRTOS_TCP_Transmission_utest.c @@ -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 ) {