Skip to content

Commit 3d3ffc5

Browse files
Merge branch 'main' into unittest-warnings
2 parents 9593dab + b3f586f commit 3d3ffc5

File tree

16 files changed

+419
-17
lines changed

16 files changed

+419
-17
lines changed

source/FreeRTOS_DHCP.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@
224224
{
225225
FreeRTOS_printf( ( "vDHCPProcess: FreeRTOS_recvfrom returns %d\n", ( int ) lBytes ) );
226226
}
227+
else if( lBytes >= 0 )
228+
{
229+
vReleaseSinglePacketFromUDPSocket( EP_DHCPData.xDHCPSocket );
230+
}
231+
else
232+
{
233+
}
227234

228235
break;
229236
}
@@ -257,7 +264,7 @@
257264
pxIterator = NULL;
258265
}
259266

260-
if( pxIterator != NULL )
267+
if( ( pxIterator != NULL ) && ( pxIterator->xDHCPData.eDHCPState == pxIterator->xDHCPData.eExpectedState ) )
261268
{
262269
/* The second parameter pdTRUE tells to check for a UDP message. */
263270
vDHCPProcessEndPoint( pdFALSE, pdTRUE, pxIterator );
@@ -269,15 +276,24 @@
269276
}
270277
else
271278
{
272-
/* Target not found, fetch the message and delete it. */
279+
/* Target not found or there is a state mismatch, fetch the message and delete it. */
273280
/* PAss the address of a pointer pucUDPPayload, because zero-copy is used. */
274281
lBytes = FreeRTOS_recvfrom( EP_DHCPData.xDHCPSocket, &( pucUDPPayload ), 0, FREERTOS_ZERO_COPY, NULL, NULL );
275282

276283
if( ( lBytes > 0 ) && ( pucUDPPayload != NULL ) )
277284
{
278285
/* Remove it now, destination not found. */
279286
FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayload );
280-
FreeRTOS_printf( ( "vDHCPProcess: Removed a %d-byte message: target not found\n", ( int ) lBytes ) );
287+
288+
if( pxIterator == NULL )
289+
{
290+
FreeRTOS_printf( ( "vDHCPProcess: Removed a %d-byte message: target not found\n", ( int ) lBytes ) );
291+
}
292+
else
293+
{
294+
FreeRTOS_printf( ( "vDHCPProcess: Wrong state: expected: %d got: %d : ignore\n",
295+
pxIterator->xDHCPData.eExpectedState, pxIterator->xDHCPData.eDHCPState ) );
296+
}
281297
}
282298
}
283299
}
@@ -489,6 +505,11 @@
489505
{
490506
/* Give up, start again. */
491507
EP_DHCPData.eDHCPState = eInitialWait;
508+
509+
/* Reset expected state so that DHCP packets from
510+
* different DHCP servers if available already in the DHCP socket can
511+
* be processed */
512+
EP_DHCPData.eExpectedState = eInitialWait;
492513
}
493514
}
494515
}
@@ -992,6 +1013,11 @@
9921013
{
9931014
/* Start again. */
9941015
EP_DHCPData.eDHCPState = eInitialWait;
1016+
1017+
/* Reset expected state so that DHCP packets from
1018+
* different DHCP servers if available already in the DHCP socket can
1019+
* be processed */
1020+
EP_DHCPData.eExpectedState = eInitialWait;
9951021
}
9961022
}
9971023

source/FreeRTOS_DHCPv6.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,13 @@ void vDHCPv6Process( BaseType_t xReset,
436436
{
437437
FreeRTOS_printf( ( "vDHCPProcess: FreeRTOS_recvfrom returns %d\n", ( int ) lBytes ) );
438438
}
439+
else if( lBytes == 0 )
440+
{
441+
vReleaseSinglePacketFromUDPSocket( EP_DHCPData.xDHCPSocket );
442+
}
443+
else
444+
{
445+
}
439446

440447
break;
441448
}
@@ -450,6 +457,8 @@ void vDHCPv6Process( BaseType_t xReset,
450457
{
451458
xDoProcess = xDHCPv6Process_PassReplyToEndPoint( pxEndPoint );
452459
}
460+
461+
FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayload );
453462
}
454463
}
455464

source/FreeRTOS_IP_Utils.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,27 @@ uint32_t FreeRTOS_round_down( uint32_t a,
18251825
}
18261826
/*-----------------------------------------------------------*/
18271827

1828+
/**
1829+
* @brief Release single UDP packet from a given socket
1830+
* @param[in] xSocket UDP Socket from which the packet should be released.
1831+
*/
1832+
void vReleaseSinglePacketFromUDPSocket( const ConstSocket_t xSocket )
1833+
{
1834+
uint8_t * pucUDPPayload = NULL;
1835+
int32_t lBytes;
1836+
1837+
/* Passing the address of a pointer (pucUDPPayload) because FREERTOS_ZERO_COPY is used. */
1838+
lBytes = FreeRTOS_recvfrom( xSocket, &pucUDPPayload, 0U, FREERTOS_ZERO_COPY, NULL, NULL );
1839+
1840+
( void ) lBytes;
1841+
1842+
if( pucUDPPayload != NULL )
1843+
{
1844+
FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayload );
1845+
}
1846+
}
1847+
/*-----------------------------------------------------------*/
1848+
18281849
/**
18291850
* @brief Convert character array (of size 4) to equivalent 32-bit value.
18301851
* @param[in] pucPtr The character array.

source/include/FreeRTOS_IP_Utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ void vPreCheckConfigs( void );
109109
*/
110110
void prvProcessNetworkDownEvent( struct xNetworkInterface * pxInterface );
111111

112+
/**
113+
* @brief Release single UDP packet from a given socket
114+
*/
115+
void vReleaseSinglePacketFromUDPSocket( const ConstSocket_t xSocket );
112116

113117
/* *INDENT-OFF* */
114118
#ifdef __cplusplus

test/cbmc/proofs/DHCPv6/DHCPv6Process/DHCPv6Process_harness.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ int32_t FreeRTOS_recvfrom( Socket_t xSocket,
153153
return retVal;
154154
}
155155

156+
/* For the purpose of this proof we stub the
157+
* implementation of FreeRTOS_ReleaseUDPPayloadBuffer here, but make sure that
158+
* the pvBuffer is not NULL, its not verified here if the pointer is a valid
159+
* payload buffer as its proved in other proofs */
160+
void FreeRTOS_ReleaseUDPPayloadBuffer( void * pvBuffer )
161+
{
162+
__CPROVER_assert( pvBuffer != NULL,
163+
"FreeRTOS precondition: pvBuffer != NULL" );
164+
}
165+
156166
void harness()
157167
{
158168
BaseType_t xReset;

test/cbmc/proofs/DHCPv6/DHCPv6Process/Makefile.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"CBMCFLAGS":
66
[
77
"--unwind {LOOP_UNWIND_COUNT}",
8+
"--unwindset memcmp.0:17",
89
"--nondet-static --flush"
910
],
1011
"OPT":

test/unit-test/FreeRTOS_DHCP/FreeRTOS_DHCP_stubs.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,4 +715,38 @@ static int32_t FreeRTOS_recvfrom_eWaitingOfferRecvfromSuccess_LocalMACAddrNotMat
715715

716716
return xSizeofUDPBuffer;
717717
}
718+
719+
static int32_t FreeRTOS_recvfrom_LoopedCall( const ConstSocket_t xSocket,
720+
void * pvBuffer,
721+
size_t uxBufferLength,
722+
BaseType_t xFlags,
723+
struct freertos_sockaddr * pxSourceAddress,
724+
socklen_t * pxSourceAddressLength,
725+
int callbacks )
726+
{
727+
NetworkEndPoint_t * pxIterator = pxNetworkEndPoints;
728+
size_t xSizeRetBufferSize = xSizeofUDPBuffer;
729+
730+
if( callbacks == 2 )
731+
{
732+
pxNetworkEndPoints->xDHCPData.eDHCPState = eInitialWait;
733+
}
734+
else if( callbacks == 4 )
735+
{
736+
xSizeRetBufferSize = 200;
737+
}
738+
739+
if( ( xFlags & FREERTOS_ZERO_COPY ) != 0 )
740+
{
741+
*( ( uint8_t ** ) pvBuffer ) = pucUDPBuffer;
742+
}
743+
744+
memset( pucUDPBuffer, 0, xSizeofUDPBuffer );
745+
/* Put in correct DHCP cookie. */
746+
( ( struct xDHCPMessage_IPv4 * ) pucUDPBuffer )->ulDHCPCookie = dhcpCOOKIE;
747+
( ( struct xDHCPMessage_IPv4 * ) pucUDPBuffer )->ucOpcode = dhcpREPLY_OPCODE;
748+
( ( struct xDHCPMessage_IPv4 * ) pucUDPBuffer )->ulTransactionID = FreeRTOS_htonl( 0x01ABCDEF );
749+
750+
return xSizeRetBufferSize;
751+
}
718752
/*-----------------------------------------------------------*/

0 commit comments

Comments
 (0)