xref: /FreeRTOS-Plus-TCP-v4.0.0/source/FreeRTOS_IPv4_Utils.c (revision b23fa86ac476770d3224c07213bec32f5b1628bd)
1 /*
2  * FreeRTOS+TCP <DEVELOPMENT BRANCH>
3  * Copyright (C) 2022 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * http://aws.amazon.com/freertos
25  * http://www.FreeRTOS.org
26  */
27 
28 /**
29  * @file FreeRTOS_IPv4_Utils.c
30  * @brief Implements the basic functionality for the FreeRTOS+TCP network stack functions for IPv4.
31  */
32 
33 /* Standard includes. */
34 #include <stdint.h>
35 #include <stdio.h>
36 
37 /* FreeRTOS includes. */
38 #include "FreeRTOS.h"
39 
40 /* FreeRTOS+TCP includes. */
41 #include "FreeRTOS_IP.h"
42 
43 /* Just make sure the contents doesn't get compiled if IPv4 is not enabled. */
44 /* *INDENT-OFF* */
45     #if( ipconfigUSE_IPv4 != 0 )
46 /* *INDENT-ON* */
47 
48 
49 /*-----------------------------------------------------------*/
50 
51 /**
52  * @brief Set multicast MAC address.
53  *
54  * @param[in] ulIPAddress IP address.
55  * @param[out] pxMACAddress Pointer to MAC address.
56  */
vSetMultiCastIPv4MacAddress(uint32_t ulIPAddress,MACAddress_t * pxMACAddress)57 void vSetMultiCastIPv4MacAddress( uint32_t ulIPAddress,
58                                   MACAddress_t * pxMACAddress )
59 {
60     uint32_t ulIP = FreeRTOS_ntohl( ulIPAddress );
61 
62     pxMACAddress->ucBytes[ 0 ] = ( uint8_t ) 0x01U;
63     pxMACAddress->ucBytes[ 1 ] = ( uint8_t ) 0x00U;
64     pxMACAddress->ucBytes[ 2 ] = ( uint8_t ) 0x5EU;
65     pxMACAddress->ucBytes[ 3 ] = ( uint8_t ) ( ( ulIP >> 16 ) & 0x7fU ); /* Use 7 bits. */
66     pxMACAddress->ucBytes[ 4 ] = ( uint8_t ) ( ( ulIP >> 8 ) & 0xffU );  /* Use 8 bits. */
67     pxMACAddress->ucBytes[ 5 ] = ( uint8_t ) ( ( ulIP ) & 0xffU );       /* Use 8 bits. */
68 }
69 /*-----------------------------------------------------------*/
70 
71 /** @brief Do the first IPv4 length checks at the IP-header level.
72  * @param[in] pucEthernetBuffer The buffer containing the packet.
73  * @param[in] uxBufferLength The number of bytes to be sent or received.
74  * @param[in] pxSet A struct describing this packet.
75  *
76  * @return Non-zero in case of an error.
77  */
prvChecksumIPv4Checks(uint8_t * pucEthernetBuffer,size_t uxBufferLength,struct xPacketSummary * pxSet)78 BaseType_t prvChecksumIPv4Checks( uint8_t * pucEthernetBuffer,
79                                   size_t uxBufferLength,
80                                   struct xPacketSummary * pxSet )
81 {
82     BaseType_t xReturn = 0;
83     uint8_t ucVersion;
84     uint16_t usLength;
85 
86     pxSet->xIsIPv6 = pdFALSE;
87 
88     usLength = pxSet->pxIPPacket->xIPHeader.usLength;
89     usLength = FreeRTOS_ntohs( usLength );
90 
91     /* IPv4 : the lower nibble in 'ucVersionHeaderLength' indicates the length
92      * of the IP-header, expressed in number of 4-byte words. Usually 5 words.
93      */
94     ucVersion = pxSet->pxIPPacket->xIPHeader.ucVersionHeaderLength & ( uint8_t ) 0x0FU;
95     pxSet->uxIPHeaderLength = ( size_t ) ucVersion;
96     pxSet->uxIPHeaderLength *= 4U;
97 
98     if( usLength < pxSet->uxIPHeaderLength )
99     {
100         pxSet->usChecksum = ipINVALID_LENGTH;
101         xReturn = 3;
102     }
103 
104     /* Check for minimum packet size. */
105     if( xReturn == 0 )
106     {
107         if( uxBufferLength < sizeof( IPPacket_t ) )
108         {
109             pxSet->usChecksum = ipINVALID_LENGTH;
110             xReturn = 4;
111         }
112     }
113 
114     if( xReturn == 0 )
115     {
116         /* Check for minimum packet size. */
117         if( uxBufferLength < ( ipSIZE_OF_ETH_HEADER + pxSet->uxIPHeaderLength ) )
118         {
119             /* The packet does not contain the full IP-headers so drop it. */
120             pxSet->usChecksum = ipINVALID_LENGTH;
121             xReturn = 5;
122         }
123     }
124 
125     if( xReturn == 0 )
126     {
127         /* xIPHeader.usLength is the total length, minus the Ethernet header. */
128         pxSet->usPayloadLength = FreeRTOS_ntohs( pxSet->pxIPPacket->xIPHeader.usLength );
129 
130         size_t uxNeeded = pxSet->usPayloadLength;
131         uxNeeded += ipSIZE_OF_ETH_HEADER;
132 
133         if( uxBufferLength < uxNeeded )
134         {
135             /* The payload is longer than the packet appears to contain. */
136             pxSet->usChecksum = ipINVALID_LENGTH;
137             xReturn = 6;
138         }
139     }
140 
141     if( xReturn == 0 )
142     {
143         /* Identify the next protocol. */
144         pxSet->ucProtocol = pxSet->pxIPPacket->xIPHeader.ucProtocol;
145         /* MISRA Ref 11.3.1 [Misaligned access] */
146         /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
147         /* coverity[misra_c_2012_rule_11_3_violation] */
148         pxSet->pxProtocolHeaders = ( ( ProtocolHeaders_t * ) &( pucEthernetBuffer[ pxSet->uxIPHeaderLength + ipSIZE_OF_ETH_HEADER ] ) );
149         /* For IPv4, the number of bytes in IP-header + the protocol is indicated. */
150         pxSet->usProtocolBytes = ( uint16_t ) ( pxSet->usPayloadLength - pxSet->uxIPHeaderLength );
151     }
152 
153     return xReturn;
154 }
155 /*-----------------------------------------------------------*/
156 
157 /* *INDENT-OFF* */
158     #endif /* ipconfigUSE_IPv4 != 0 ) */
159 /* *INDENT-ON* */
160