-
Notifications
You must be signed in to change notification settings - Fork 206
Preparing library to use loopback device #1020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1803d90
91badc9
a57b099
e5a8f60
e9f9126
87c517d
2422335
9104b2e
f7d2e35
83f34fd
5669fd8
855d6d5
085741c
35a61c1
c706b49
364bc05
5b24042
a0b59ff
3c97367
7cdbef2
c29f055
5240680
31da177
c2cd991
3b9a259
d695740
d7ee3c2
c05141f
a73a8c1
20f16c2
1150510
16352be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -210,6 +210,58 @@ BaseType_t xIsIPv4Multicast( uint32_t ulIPAddress ) | |
| } | ||
| /*-----------------------------------------------------------*/ | ||
|
|
||
| /** | ||
| * @brief Check if the packet is an illegal loopback packet. | ||
| * | ||
| * @param[in] pxIPHeader The IP-header being checked. | ||
| * | ||
| * @return Returns pdTRUE if the packet should be stopped, because either the source | ||
| * or the target address is a loopback address. | ||
| */ | ||
| BaseType_t xBadIPv4Loopback( const IPHeader_t * const pxIPHeader ) | ||
| { | ||
| BaseType_t xReturn = pdFALSE; | ||
| const NetworkEndPoint_t * pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv4( pxIPHeader->ulSourceIPAddress, 3 ); | ||
|
|
||
| /* Allow loopback packets from this node itself only. */ | ||
| if( pxEndPoint != NULL ) | ||
| { | ||
| BaseType_t x1 = ( xIsIPv4Loopback( pxIPHeader->ulDestinationIPAddress ) != 0 ) ? pdTRUE : pdFALSE; | ||
| BaseType_t x2 = ( xIsIPv4Loopback( pxIPHeader->ulSourceIPAddress ) != 0 ) ? pdTRUE : pdFALSE; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic is not very clear.Isn't it enough to check only for destination address.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shubnil wrote: + if( pxEndPoint != NULL )
+ {
+ BaseType_t x1 = ( xIsIPv4Loopback( pxIPHeader->ulDestinationIPAddress ) != 0 ) ? pdTRUE : pdFALSE;
+ BaseType_t x2 = ( xIsIPv4Loopback( pxIPHeader->ulSourceIPAddress ) != 0 ) ? pdTRUE : pdFALSE;
The check is done in It will return
Before we had a loopback interface, loopback addresses had to be dropped: which would be the same as : I think that we should have tested for both Now we have added a loopback device, and so 127.x.x.x addresses must be allowed, with the exception of a packet leaving or entering the host. Loopback packets may only travel internally. So that is why: when either the destination or the source address is a loopback address, the packet must be dropped.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like understand a little more on Packets will loopback as Source address. What will be the use case for this? Is this for the packets originated from the loopback interface? In that case the packet should not go out of the system. Please suggest if this understanding is correct. |
||
|
|
||
| if( x1 != x2 ) | ||
| { | ||
| /* Either the source or the destination address is an IPv4 loopback address. */ | ||
| xReturn = pdTRUE; | ||
| } | ||
| } | ||
|
|
||
| return xReturn; | ||
| } | ||
| /*-----------------------------------------------------------*/ | ||
|
|
||
| /** | ||
| * @brief Is the IP address an IPv4 loopback address. | ||
| * | ||
| * @param[in] ulAddress The IP address being checked. | ||
| * | ||
| * @return pdTRUE if the IP address is a loopback address or else, pdFALSE. | ||
| */ | ||
| BaseType_t xIsIPv4Loopback( uint32_t ulAddress ) | ||
| { | ||
| BaseType_t xReturn = pdFALSE; | ||
| uint32_t ulIP = FreeRTOS_ntohl( ulAddress ); | ||
|
|
||
| if( ( ulIP >= ipFIRST_LOOPBACK_IPv4 ) && | ||
| ( ulIP < ipLAST_LOOPBACK_IPv4 ) ) | ||
| { | ||
| xReturn = pdTRUE; | ||
| } | ||
|
|
||
| return xReturn; | ||
| } | ||
| /*-----------------------------------------------------------*/ | ||
|
|
||
| /** | ||
| * @brief Check whether this IPv4 packet is to be allowed or to be dropped. | ||
| * | ||
|
|
@@ -260,6 +312,12 @@ enum eFrameProcessingResult prvAllowIPPacketIPv4( const struct xIP_PACKET * cons | |
| /* Can not handle, unknown or invalid header version. */ | ||
| eReturn = eReleaseBuffer; | ||
| } | ||
| else if( xBadIPv4Loopback( &( pxIPPacket->xIPHeader ) ) == pdTRUE ) | ||
| { | ||
| /* The local loopback addresses must never appear outside a host. See RFC 1122 | ||
| * section 3.2.1.3. */ | ||
| eReturn = eReleaseBuffer; | ||
| } | ||
| else if( | ||
| ( FreeRTOS_FindEndPointOnIP_IPv4( ulDestinationIPAddress, 4 ) == NULL ) && | ||
| ( pxNetworkBuffer->pxEndPoint == NULL ) && | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,7 +62,7 @@ const struct xIPv6_Address FreeRTOS_in6addr_any = { 0 }; | |
| /** | ||
| * This variable is initialized by the system to contain the loopback IPv6 address. | ||
| */ | ||
| const struct xIPv6_Address FreeRTOS_in6addr_loopback = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 } }; | ||
| const struct xIPv6_Address FreeRTOS_in6addr_loopback = { { 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U } }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line no 65 and 243 both have a constant IPv6 address, however one is just const and other is static const. We should ideally have same for both.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Good point. const struct xIPv6_Address FreeRTOS_in6addr_any = { 0 };
const struct xIPv6_Address FreeRTOS_in6addr_loopback = { { 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U } };
- static const struct xIPv6_Address xIPv6UnspecifiedAddress = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; |
||
|
|
||
| #if ( ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM == 1 ) | ||
| /* Check IPv6 packet length. */ | ||
|
|
@@ -237,19 +237,6 @@ const struct xIPv6_Address FreeRTOS_in6addr_loopback = { { 0, 0, 0, 0, 0, 0, 0, | |
| #endif /* ( ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM == 1 ) */ | ||
| /*-----------------------------------------------------------*/ | ||
|
|
||
| /** | ||
| * This variable is initialized by the system to contain the unspecified IPv6 address. | ||
| */ | ||
| static const struct xIPv6_Address xIPv6UnspecifiedAddress = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; | ||
|
|
||
| #if ( ipconfigETHERNET_DRIVER_FILTERS_PACKETS == 0 ) | ||
|
|
||
| /* | ||
| * Check if the packet is a loopback packet. | ||
| */ | ||
| static BaseType_t xIsIPv6Loopback( const IPHeader_IPv6_t * const pxIPv6Header ); | ||
| #endif /* ipconfigETHERNET_DRIVER_FILTERS_PACKETS == 0 */ | ||
|
|
||
| /** | ||
| * @brief Get the group ID and stored into IPv6_Address_t. | ||
| * | ||
|
|
@@ -270,34 +257,60 @@ static void xGetIPv6MulticastGroupID( const IPv6_Address_t * pxIPv6Address, | |
|
|
||
| /*-----------------------------------------------------------*/ | ||
|
|
||
| /** | ||
| * @brief Check if the IP-address is an IPv6 loopback address. | ||
| * | ||
| * @param[in] pxAddress The IP-address being checked. | ||
| * | ||
| * @return pdTRUE if the IP-address is a loopback address or else, pdFALSE. | ||
| */ | ||
| BaseType_t xIsIPv6Loopback( const IPv6_Address_t * pxAddress ) | ||
| { | ||
| BaseType_t xReturn = pdFALSE; | ||
|
|
||
| if( memcmp( pxAddress->ucBytes, FreeRTOS_in6addr_loopback.ucBytes, ipSIZE_OF_IPv6_ADDRESS ) == 0 ) | ||
| { | ||
| xReturn = pdTRUE; | ||
| } | ||
|
|
||
| return xReturn; | ||
| } | ||
|
|
||
| #if ( ipconfigETHERNET_DRIVER_FILTERS_PACKETS == 0 ) | ||
|
|
||
| /** | ||
| * @brief Check if the packet is a loopback packet. | ||
| * @brief Check if the packet is an illegal loopback packet. | ||
| * | ||
| * @param[in] pxIPv6Header The IP packet in pxNetworkBuffer. | ||
| * @param[in] pxIPv6Header The IP-header of the packet. | ||
| * | ||
| * @return Returns pdTRUE if it's a legal loopback packet, pdFALSE if not . | ||
| * @return Returns pdTRUE if the packet should be stopped, because either the source | ||
| * or the target address is a loopback address. | ||
| */ | ||
| /* MISRA Ref 8.9.1 [File scoped variables] */ | ||
| /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-89 */ | ||
| /* coverity[misra_c_2012_rule_8_9_violation] */ | ||
| /* coverity[single_use] */ | ||
| static BaseType_t xIsIPv6Loopback( const IPHeader_IPv6_t * const pxIPv6Header ) | ||
| BaseType_t xBadIPv6Loopback( const IPHeader_IPv6_t * const pxIPv6Header ) | ||
| { | ||
| BaseType_t xReturn = pdFALSE; | ||
| const NetworkEndPoint_t * pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv6( &( pxIPv6Header->xSourceAddress ) ); | ||
|
|
||
| /* Allow loopback packets from this node itself only. */ | ||
| if( ( pxEndPoint != NULL ) && | ||
| ( memcmp( pxIPv6Header->xDestinationAddress.ucBytes, FreeRTOS_in6addr_loopback.ucBytes, sizeof( IPv6_Address_t ) ) == 0 ) && | ||
| ( memcmp( pxIPv6Header->xSourceAddress.ucBytes, pxEndPoint->ipv6_settings.xIPAddress.ucBytes, sizeof( IPv6_Address_t ) ) == 0 ) ) | ||
| if( pxEndPoint != NULL ) | ||
| { | ||
| xReturn = pdTRUE; | ||
| BaseType_t x1 = ( xIsIPv6Loopback( &( pxIPv6Header->xDestinationAddress ) ) != 0 ) ? pdTRUE : pdFALSE; | ||
| BaseType_t x2 = ( xIsIPv6Loopback( &( pxIPv6Header->xSourceAddress ) ) != 0 ) ? pdTRUE : pdFALSE; | ||
|
|
||
| if( x1 != x2 ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic is not very clear.Isn't it enough to check only for destination address.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I already answered to this question here above. |
||
| { | ||
| /* Either source or the destination address is a loopback address. */ | ||
| xReturn = pdTRUE; | ||
| } | ||
| } | ||
|
|
||
| return xReturn; | ||
| } | ||
|
|
||
| #endif /* ipconfigETHERNET_DRIVER_FILTERS_PACKETS == 0 */ | ||
|
|
||
|
|
||
|
|
@@ -332,7 +345,7 @@ BaseType_t xIsIPv6AllowedMulticast( const IPv6_Address_t * pxIPAddress ) | |
| * - .. | ||
| * - 0xFF0F:: */ | ||
| else if( ( IPv6MC_GET_FLAGS_VALUE( pxIPAddress ) == 0U ) && | ||
| ( memcmp( xGroupIDAddress.ucBytes, xIPv6UnspecifiedAddress.ucBytes, sizeof( IPv6_Address_t ) ) == 0 ) ) | ||
| ( memcmp( xGroupIDAddress.ucBytes, FreeRTOS_in6addr_any.ucBytes, sizeof( IPv6_Address_t ) ) == 0 ) ) | ||
| { | ||
| xReturn = pdFALSE; | ||
| } | ||
|
|
@@ -462,8 +475,8 @@ eFrameProcessingResult_t prvAllowIPPacketIPv6( const IPHeader_IPv6_t * const pxI | |
|
|
||
| /* Drop if packet has unspecified IPv6 address (defined in RFC4291 - sec 2.5.2) | ||
| * either in source or destination address. */ | ||
| if( ( memcmp( pxDestinationIPAddress->ucBytes, xIPv6UnspecifiedAddress.ucBytes, sizeof( IPv6_Address_t ) ) == 0 ) || | ||
| ( memcmp( pxSourceIPAddress->ucBytes, xIPv6UnspecifiedAddress.ucBytes, sizeof( IPv6_Address_t ) ) == 0 ) ) | ||
| if( ( memcmp( pxDestinationIPAddress->ucBytes, FreeRTOS_in6addr_any.ucBytes, sizeof( IPv6_Address_t ) ) == 0 ) || | ||
| ( memcmp( pxSourceIPAddress->ucBytes, FreeRTOS_in6addr_any.ucBytes, sizeof( IPv6_Address_t ) ) == 0 ) ) | ||
| { | ||
| xHasUnspecifiedAddress = pdTRUE; | ||
| } | ||
|
|
@@ -476,10 +489,9 @@ eFrameProcessingResult_t prvAllowIPPacketIPv6( const IPHeader_IPv6_t * const pxI | |
| eReturn = eProcessBuffer; | ||
| } | ||
| /* Is it the legal multicast address? */ | ||
| else if( ( xHasUnspecifiedAddress == pdFALSE ) && | ||
| else if( ( ( xHasUnspecifiedAddress == pdFALSE ) && | ||
| ( xBadIPv6Loopback( pxIPv6Header ) == pdFALSE ) ) && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Codewise it makes more sense to have xBadIPv6Loopback and xBadIPv4Loopback checks at the same level. Here, xBadIPv4Loopback check is happening at FreeRTOS_IP.c but xBadIPv6Loopback check is in IPv6 specfic file. Can we re-arrange the code.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I answered this and rearranged the code like this:
|
||
| ( ( xIsIPv6AllowedMulticast( pxDestinationIPAddress ) != pdFALSE ) || | ||
| /* Is it loopback address sent from this node? */ | ||
| ( xIsIPv6Loopback( pxIPv6Header ) != pdFALSE ) || | ||
| /* Or (during DHCP negotiation) we have no IP-address yet? */ | ||
| ( FreeRTOS_IsNetworkUp() == 0 ) ) ) | ||
| { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.