FreeRTOS_IP.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * FreeRTOS+TCP V2.3.2 LTS Patch 1
  3. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://aws.amazon.com/freertos
  23. * http://www.FreeRTOS.org
  24. */
  25. #ifndef FREERTOS_IP_H
  26. #define FREERTOS_IP_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #include "FreeRTOS.h"
  31. #include "task.h"
  32. /* Application level configuration options. */
  33. #include "FreeRTOSIPConfig.h"
  34. #include "FreeRTOSIPConfigDefaults.h"
  35. #include "IPTraceMacroDefaults.h"
  36. /* Some constants defining the sizes of several parts of a packet.
  37. * These defines come before including the configuration header files. */
  38. /* The size of the Ethernet header is 14, meaning that 802.1Q VLAN tags
  39. * are not ( yet ) supported. */
  40. #define ipSIZE_OF_ETH_HEADER 14U
  41. #define ipSIZE_OF_IPv4_HEADER 20U
  42. #define ipSIZE_OF_IGMP_HEADER 8U
  43. #define ipSIZE_OF_ICMP_HEADER 8U
  44. #define ipSIZE_OF_UDP_HEADER 8U
  45. #define ipSIZE_OF_TCP_HEADER 20U
  46. #define ipSIZE_OF_IPv4_ADDRESS 4U
  47. /*
  48. * Generate a randomized TCP Initial Sequence Number per RFC.
  49. * This function must be provided by the application builder.
  50. */
  51. /* This function is defined generally by the application. */
  52. extern uint32_t ulApplicationGetNextSequenceNumber( uint32_t ulSourceAddress,
  53. uint16_t usSourcePort,
  54. uint32_t ulDestinationAddress,
  55. uint16_t usDestinationPort );
  56. /* The number of octets in the MAC and IP addresses respectively. */
  57. #define ipMAC_ADDRESS_LENGTH_BYTES ( 6 )
  58. #define ipIP_ADDRESS_LENGTH_BYTES ( 4 )
  59. /* IP protocol definitions. */
  60. #define ipPROTOCOL_ICMP ( 1U )
  61. #define ipPROTOCOL_IGMP ( 2U )
  62. #define ipPROTOCOL_TCP ( 6U )
  63. #define ipPROTOCOL_UDP ( 17U )
  64. /* The character used to fill ICMP echo requests, and therefore also the
  65. * character expected to fill ICMP echo replies. */
  66. #define ipECHO_DATA_FILL_BYTE 'x'
  67. /* Dimensions the buffers that are filled by received Ethernet frames. */
  68. #define ipSIZE_OF_ETH_CRC_BYTES ( 4UL )
  69. #define ipSIZE_OF_ETH_OPTIONAL_802_1Q_TAG_BYTES ( 4UL )
  70. #define ipTOTAL_ETHERNET_FRAME_SIZE ( ( ( uint32_t ) ipconfigNETWORK_MTU ) + ( ( uint32_t ) ipSIZE_OF_ETH_HEADER ) + ipSIZE_OF_ETH_CRC_BYTES + ipSIZE_OF_ETH_OPTIONAL_802_1Q_TAG_BYTES )
  71. /* Space left at the beginning of a network buffer storage area to store a
  72. * pointer back to the network buffer. Should be a multiple of 8 to ensure 8 byte
  73. * alignment is maintained on architectures that require it.
  74. *
  75. * In order to get a 32-bit alignment of network packets, an offset of 2 bytes
  76. * would be desirable, as defined by ipconfigPACKET_FILLER_SIZE. So the malloc'd
  77. * buffer will have the following contents:
  78. * uint32_t pointer; // word-aligned
  79. * uchar_8 filler[6];
  80. * << ETH-header >> // half-word-aligned
  81. * uchar_8 dest[6]; // start of pucEthernetBuffer
  82. * uchar_8 dest[6];
  83. * uchar16_t type;
  84. * << IP-header >> // word-aligned
  85. * uint8_t ucVersionHeaderLength;
  86. * etc
  87. */
  88. #if ( ipconfigBUFFER_PADDING != 0 )
  89. #define ipBUFFER_PADDING ipconfigBUFFER_PADDING
  90. #else
  91. #define ipBUFFER_PADDING ( 8U + ipconfigPACKET_FILLER_SIZE )
  92. #endif
  93. /**
  94. * The structure used to store buffers and pass them around the network stack.
  95. * Buffers can be in use by the stack, in use by the network interface hardware
  96. * driver, or free (not in use).
  97. */
  98. typedef struct xNETWORK_BUFFER
  99. {
  100. ListItem_t xBufferListItem; /**< Used to reference the buffer form the free buffer list or a socket. */
  101. uint32_t ulIPAddress; /**< Source or destination IP address, depending on usage scenario. */
  102. uint8_t * pucEthernetBuffer; /**< Pointer to the start of the Ethernet frame. */
  103. size_t xDataLength; /**< Starts by holding the total Ethernet frame length, then the UDP/TCP payload length. */
  104. uint16_t usPort; /**< Source or destination port, depending on usage scenario. */
  105. uint16_t usBoundPort; /**< The port to which a transmitting socket is bound. */
  106. #if ( ipconfigUSE_LINKED_RX_MESSAGES != 0 )
  107. struct xNETWORK_BUFFER * pxNextBuffer; /**< Possible optimisation for expert users - requires network driver support. */
  108. #endif
  109. } NetworkBufferDescriptor_t;
  110. #include "pack_struct_start.h"
  111. /**
  112. * MAC address structure.
  113. */
  114. struct xMAC_ADDRESS
  115. {
  116. uint8_t ucBytes[ ipMAC_ADDRESS_LENGTH_BYTES ]; /**< Byte array of the MAC address */
  117. };
  118. #include "pack_struct_end.h"
  119. typedef struct xMAC_ADDRESS MACAddress_t;
  120. typedef enum eNETWORK_EVENTS
  121. {
  122. eNetworkUp, /* The network is configured. */
  123. eNetworkDown /* The network connection has been lost. */
  124. } eIPCallbackEvent_t;
  125. /* MISRA check: some modules refer to this typedef even though
  126. * ipconfigSUPPORT_OUTGOING_PINGS is not enabled. */
  127. typedef enum ePING_REPLY_STATUS
  128. {
  129. eSuccess = 0, /**< A correct reply has been received for an outgoing ping. */
  130. eInvalidChecksum, /**< A reply was received for an outgoing ping but the checksum of the reply was incorrect. */
  131. eInvalidData /**< A reply was received to an outgoing ping but the payload of the reply was not correct. */
  132. } ePingReplyStatus_t;
  133. /**
  134. * The software timer struct for various IP functions
  135. */
  136. typedef struct xIP_TIMER
  137. {
  138. uint32_t
  139. bActive : 1, /**< This timer is running and must be processed. */
  140. bExpired : 1; /**< Timer has expired and a task must be processed. */
  141. TimeOut_t xTimeOut; /**< The timeout value. */
  142. TickType_t ulRemainingTime; /**< The amount of time remaining. */
  143. TickType_t ulReloadTime; /**< The value of reload time. */
  144. } IPTimer_t;
  145. /* Endian related definitions. */
  146. #if ( ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN )
  147. /* FreeRTOS_htons / FreeRTOS_htonl: some platforms might have built-in versions
  148. * using a single instruction so allow these versions to be overridden. */
  149. #ifndef FreeRTOS_htons
  150. #define FreeRTOS_htons( usIn ) ( ( uint16_t ) ( ( ( usIn ) << 8U ) | ( ( usIn ) >> 8U ) ) )
  151. #endif
  152. #ifndef FreeRTOS_htonl
  153. #define FreeRTOS_htonl( ulIn ) \
  154. ( \
  155. ( uint32_t ) \
  156. ( \
  157. ( ( ( ( uint32_t ) ( ulIn ) ) ) << 24 ) | \
  158. ( ( ( ( uint32_t ) ( ulIn ) ) & 0x0000ff00UL ) << 8 ) | \
  159. ( ( ( ( uint32_t ) ( ulIn ) ) & 0x00ff0000UL ) >> 8 ) | \
  160. ( ( ( ( uint32_t ) ( ulIn ) ) ) >> 24 ) \
  161. ) \
  162. )
  163. #endif /* ifndef FreeRTOS_htonl */
  164. #else /* ipconfigBYTE_ORDER */
  165. #define FreeRTOS_htons( x ) ( ( uint16_t ) ( x ) )
  166. #define FreeRTOS_htonl( x ) ( ( uint32_t ) ( x ) )
  167. #endif /* ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN */
  168. #define FreeRTOS_ntohs( x ) FreeRTOS_htons( x )
  169. #define FreeRTOS_ntohl( x ) FreeRTOS_htonl( x )
  170. #if ( ipconfigHAS_INLINE_FUNCTIONS == 1 )
  171. static portINLINE int32_t FreeRTOS_max_int32( int32_t a,
  172. int32_t b );
  173. static portINLINE uint32_t FreeRTOS_max_uint32( uint32_t a,
  174. uint32_t b );
  175. static portINLINE int32_t FreeRTOS_min_int32( int32_t a,
  176. int32_t b );
  177. static portINLINE uint32_t FreeRTOS_min_uint32( uint32_t a,
  178. uint32_t b );
  179. static portINLINE uint32_t FreeRTOS_round_up( uint32_t a,
  180. uint32_t d );
  181. static portINLINE uint32_t FreeRTOS_round_down( uint32_t a,
  182. uint32_t d );
  183. static portINLINE BaseType_t FreeRTOS_min_BaseType( BaseType_t a,
  184. BaseType_t b );
  185. static portINLINE int32_t FreeRTOS_max_int32( int32_t a,
  186. int32_t b )
  187. {
  188. return ( a >= b ) ? a : b;
  189. }
  190. static portINLINE uint32_t FreeRTOS_max_uint32( uint32_t a,
  191. uint32_t b )
  192. {
  193. return ( a >= b ) ? a : b;
  194. }
  195. static portINLINE int32_t FreeRTOS_min_int32( int32_t a,
  196. int32_t b )
  197. {
  198. return ( a <= b ) ? a : b;
  199. }
  200. static portINLINE uint32_t FreeRTOS_min_uint32( uint32_t a,
  201. uint32_t b )
  202. {
  203. return ( a <= b ) ? a : b;
  204. }
  205. static portINLINE uint32_t FreeRTOS_round_up( uint32_t a,
  206. uint32_t d )
  207. {
  208. return d * ( ( a + d - 1U ) / d );
  209. }
  210. static portINLINE uint32_t FreeRTOS_round_down( uint32_t a,
  211. uint32_t d )
  212. {
  213. return d * ( a / d );
  214. }
  215. static portINLINE BaseType_t FreeRTOS_min_BaseType( BaseType_t a,
  216. BaseType_t b )
  217. {
  218. return ( a <= b ) ? a : b;
  219. }
  220. #else /* if ( ipconfigHAS_INLINE_FUNCTIONS == 1 ) */
  221. #define FreeRTOS_max_int32( a, b ) ( ( ( ( int32_t ) ( a ) ) >= ( ( int32_t ) ( b ) ) ) ? ( ( int32_t ) ( a ) ) : ( ( int32_t ) ( b ) ) )
  222. #define FreeRTOS_max_uint32( a, b ) ( ( ( ( uint32_t ) ( a ) ) >= ( ( uint32_t ) ( b ) ) ) ? ( ( uint32_t ) ( a ) ) : ( ( uint32_t ) ( b ) ) )
  223. #define FreeRTOS_min_int32( a, b ) ( ( ( ( int32_t ) a ) <= ( ( int32_t ) b ) ) ? ( ( int32_t ) a ) : ( ( int32_t ) b ) )
  224. #define FreeRTOS_min_uint32( a, b ) ( ( ( ( uint32_t ) a ) <= ( ( uint32_t ) b ) ) ? ( ( uint32_t ) a ) : ( ( uint32_t ) b ) )
  225. /* Round-up: divide a by d and round=up the result. */
  226. #define FreeRTOS_round_up( a, d ) ( ( ( uint32_t ) ( d ) ) * ( ( ( ( uint32_t ) ( a ) ) + ( ( uint32_t ) ( d ) ) - 1UL ) / ( ( uint32_t ) ( d ) ) ) )
  227. #define FreeRTOS_round_down( a, d ) ( ( ( uint32_t ) ( d ) ) * ( ( ( uint32_t ) ( a ) ) / ( ( uint32_t ) ( d ) ) ) )
  228. #define FreeRTOS_min_BaseType( a, b ) ( ( ( BaseType_t ) ( a ) ) <= ( ( BaseType_t ) ( b ) ) ? ( ( BaseType_t ) ( a ) ) : ( ( BaseType_t ) ( b ) ) )
  229. #endif /* ipconfigHAS_INLINE_FUNCTIONS */
  230. #define ipMS_TO_MIN_TICKS( xTimeInMs ) ( ( pdMS_TO_TICKS( ( xTimeInMs ) ) < ( ( TickType_t ) 1U ) ) ? ( ( TickType_t ) 1U ) : pdMS_TO_TICKS( ( xTimeInMs ) ) )
  231. /* For backward compatibility. */
  232. #define pdMS_TO_MIN_TICKS( xTimeInMs ) ipMS_TO_MIN_TICKS( xTimeInMs )
  233. #ifndef pdTRUE_SIGNED
  234. /* Temporary solution: eventually the defines below will appear in 'Source\include\projdefs.h' */
  235. #define pdTRUE_SIGNED pdTRUE
  236. #define pdFALSE_SIGNED pdFALSE
  237. #define pdTRUE_UNSIGNED ( 1U )
  238. #define pdFALSE_UNSIGNED ( 0U )
  239. #define ipTRUE_BOOL ( 1 == 1 )
  240. #define ipFALSE_BOOL ( 1 == 2 )
  241. #endif
  242. /*
  243. * FULL, UP-TO-DATE AND MAINTAINED REFERENCE DOCUMENTATION FOR ALL THESE
  244. * FUNCTIONS IS AVAILABLE ON THE FOLLOWING URL:
  245. * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/FreeRTOS_TCP_API_Functions.html
  246. */
  247. BaseType_t FreeRTOS_IPInit( const uint8_t ucIPAddress[ ipIP_ADDRESS_LENGTH_BYTES ],
  248. const uint8_t ucNetMask[ ipIP_ADDRESS_LENGTH_BYTES ],
  249. const uint8_t ucGatewayAddress[ ipIP_ADDRESS_LENGTH_BYTES ],
  250. const uint8_t ucDNSServerAddress[ ipIP_ADDRESS_LENGTH_BYTES ],
  251. const uint8_t ucMACAddress[ ipMAC_ADDRESS_LENGTH_BYTES ] );
  252. void * FreeRTOS_GetUDPPayloadBuffer( size_t uxRequestedSizeBytes,
  253. TickType_t uxBlockTimeTicks );
  254. void FreeRTOS_GetAddressConfiguration( uint32_t * pulIPAddress,
  255. uint32_t * pulNetMask,
  256. uint32_t * pulGatewayAddress,
  257. uint32_t * pulDNSServerAddress );
  258. void FreeRTOS_SetAddressConfiguration( const uint32_t * pulIPAddress,
  259. const uint32_t * pulNetMask,
  260. const uint32_t * pulGatewayAddress,
  261. const uint32_t * pulDNSServerAddress );
  262. /* MISRA defining 'FreeRTOS_SendPingRequest' should be dependent on 'ipconfigSUPPORT_OUTGOING_PINGS'.
  263. * In order not to break some existing project, define it unconditionally. */
  264. BaseType_t FreeRTOS_SendPingRequest( uint32_t ulIPAddress,
  265. size_t uxNumberOfBytesToSend,
  266. TickType_t uxBlockTimeTicks );
  267. void FreeRTOS_ReleaseUDPPayloadBuffer( void const * pvBuffer );
  268. const uint8_t * FreeRTOS_GetMACAddress( void );
  269. void FreeRTOS_UpdateMACAddress( const uint8_t ucMACAddress[ ipMAC_ADDRESS_LENGTH_BYTES ] );
  270. #if ( ipconfigUSE_NETWORK_EVENT_HOOK == 1 )
  271. /* This function shall be defined by the application. */
  272. void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent );
  273. #endif
  274. #if ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
  275. void vApplicationPingReplyHook( ePingReplyStatus_t eStatus,
  276. uint16_t usIdentifier );
  277. #endif
  278. uint32_t FreeRTOS_GetIPAddress( void );
  279. void FreeRTOS_SetIPAddress( uint32_t ulIPAddress );
  280. void FreeRTOS_SetNetmask( uint32_t ulNetmask );
  281. void FreeRTOS_SetGatewayAddress( uint32_t ulGatewayAddress );
  282. uint32_t FreeRTOS_GetGatewayAddress( void );
  283. uint32_t FreeRTOS_GetDNSServerAddress( void );
  284. uint32_t FreeRTOS_GetNetmask( void );
  285. void FreeRTOS_OutputARPRequest( uint32_t ulIPAddress );
  286. BaseType_t FreeRTOS_IsNetworkUp( void );
  287. #if ( ipconfigCHECK_IP_QUEUE_SPACE != 0 )
  288. UBaseType_t uxGetMinimumIPQueueSpace( void );
  289. #endif
  290. #if ( ipconfigHAS_PRINTF != 0 )
  291. extern void vPrintResourceStats( void );
  292. #else
  293. #define vPrintResourceStats() do {} while( ipFALSE_BOOL )
  294. #endif
  295. /*
  296. * Defined in FreeRTOS_Sockets.c
  297. * //_RB_ Don't think this comment is correct. If this is for internal use only it should appear after all the public API functions and not start with FreeRTOS_.
  298. * Socket has had activity, reset the timer so it will not be closed
  299. * because of inactivity
  300. */
  301. #if ( ( ipconfigHAS_DEBUG_PRINTF != 0 ) || ( ipconfigHAS_PRINTF != 0 ) )
  302. const char * FreeRTOS_GetTCPStateName( UBaseType_t ulState );
  303. #endif
  304. /* _HT_ Temporary: show all valid ARP entries
  305. */
  306. #if ( ipconfigHAS_PRINTF != 0 ) || ( ipconfigHAS_DEBUG_PRINTF != 0 )
  307. void FreeRTOS_PrintARPCache( void );
  308. #endif
  309. void FreeRTOS_ClearARP( void );
  310. /* Return pdTRUE if the IPv4 address is a multicast address. */
  311. BaseType_t xIsIPv4Multicast( uint32_t ulIPAddress );
  312. /* Set the MAC-address that belongs to a given IPv4 multi-cast address. */
  313. void vSetMultiCastIPv4MacAddress( uint32_t ulIPAddress,
  314. MACAddress_t * pxMACAddress );
  315. #if ( ipconfigDHCP_REGISTER_HOSTNAME == 1 )
  316. /* DHCP has an option for clients to register their hostname. It doesn't
  317. * have much use, except that a device can be found in a router along with its
  318. * name. If this option is used the callback below must be provided by the
  319. * application writer to return a const string, denoting the device's name. */
  320. /* Typically this function is defined in a user module. */
  321. const char * pcApplicationHostnameHook( void );
  322. #endif /* ipconfigDHCP_REGISTER_HOSTNAME */
  323. /* This xApplicationGetRandomNumber() will set *pulNumber to a random number,
  324. * and return pdTRUE. When the random number generator is broken, it shall return
  325. * pdFALSE.
  326. * The function is defined in 'iot_secure_sockets.c'.
  327. * If that module is not included in the project, the application must provide an
  328. * implementation of it.
  329. * The macro's ipconfigRAND32() and configRAND32() are not in use anymore. */
  330. /* "xApplicationGetRandomNumber" is declared but never defined, because it may
  331. * be defined in a user module. */
  332. extern BaseType_t xApplicationGetRandomNumber( uint32_t * pulNumber );
  333. /* For backward compatibility define old structure names to the newer equivalent
  334. * structure name. */
  335. #ifndef ipconfigENABLE_BACKWARD_COMPATIBILITY
  336. #define ipconfigENABLE_BACKWARD_COMPATIBILITY 1
  337. #endif
  338. #if ( ipconfigENABLE_BACKWARD_COMPATIBILITY == 1 )
  339. #define xIPStackEvent_t IPStackEvent_t
  340. #define xNetworkBufferDescriptor_t NetworkBufferDescriptor_t
  341. #define xMACAddress_t MACAddress_t
  342. #define xWinProperties_t WinProperties_t
  343. #define xSocket_t Socket_t
  344. #define xSocketSet_t SocketSet_t
  345. #define ipSIZE_OF_IP_HEADER ipSIZE_OF_IPv4_HEADER
  346. /* Since August 2016, the public types and fields below have changed name:
  347. * abbreviations TCP/UDP are now written in capitals, and type names now end with "_t". */
  348. #define FOnConnected FOnConnected_t
  349. #define FOnTcpReceive FOnTCPReceive_t
  350. #define FOnTcpSent FOnTCPSent_t
  351. #define FOnUdpReceive FOnUDPReceive_t
  352. #define FOnUdpSent FOnUDPSent_t
  353. #define pOnTcpConnected pxOnTCPConnected
  354. #define pOnTcpReceive pxOnTCPReceive
  355. #define pOnTcpSent pxOnTCPSent
  356. #define pOnUdpReceive pxOnUDPReceive
  357. #define pOnUdpSent pxOnUDPSent
  358. #define FOnUdpSent FOnUDPSent_t
  359. #define FOnTcpSent FOnTCPSent_t
  360. #endif /* ipconfigENABLE_BACKWARD_COMPATIBILITY */
  361. #ifdef __cplusplus
  362. } /* extern "C" */
  363. #endif
  364. void setDhcpClientState(BaseType_t on);
  365. int getDhcpClientState();
  366. void dump_net_pack(NetworkBufferDescriptor_t * pxNetworkBuffer, int port, int is_send);
  367. #endif /* FREERTOS_IP_H */