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