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_UDP_IP.c
30 * @brief This file has the source code for the UDP-IP functionality of the FreeRTOS+TCP
31 * network stack.
32 */
33
34 /* Standard includes. */
35 #include <stdint.h>
36 #include <stdio.h>
37
38 /* FreeRTOS includes. */
39 #include "FreeRTOS.h"
40 #include "task.h"
41 #include "queue.h"
42 #include "semphr.h"
43 #include "event_groups.h"
44 #include "list.h"
45
46 /* FreeRTOS+TCP includes. */
47 #include "FreeRTOS_IP.h"
48 #include "FreeRTOS_Sockets.h"
49 #include "FreeRTOS_IP_Private.h"
50 #include "FreeRTOS_UDP_IP.h"
51 #include "FreeRTOS_ARP.h"
52 #include "FreeRTOS_DNS.h"
53 #include "FreeRTOS_DHCP.h"
54 #include "FreeRTOS_ND.h"
55 #include "FreeRTOS_IP_Utils.h"
56 #include "NetworkInterface.h"
57 #include "NetworkBufferManagement.h"
58
59 #if ( ipconfigUSE_DNS == 1 )
60 #include "FreeRTOS_DNS.h"
61 #endif
62
63 /** @brief The expected IP version and header length coded into the IP header itself. */
64 #define ipIP_VERSION_AND_HEADER_LENGTH_BYTE ( ( uint8_t ) 0x45 )
65
66 /** @brief Part of the Ethernet and IP headers are always constant when sending an IPv4
67 * UDP packet. This array defines the constant parts, allowing this part of the
68 * packet to be filled in using a simple memcpy() instead of individual writes. */
69 /*lint -e708 (Info -- union initialization). */
70 UDPPacketHeader_t xDefaultPartUDPPacketHeader =
71 {
72 /* .ucBytes : */
73 {
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Ethernet source MAC address. */
75 0x08, 0x00, /* Ethernet frame type. */
76 ipIP_VERSION_AND_HEADER_LENGTH_BYTE, /* ucVersionHeaderLength. */
77 0x00, /* ucDifferentiatedServicesCode. */
78 0x00, 0x00, /* usLength. */
79 0x00, 0x00, /* usIdentification. */
80 0x00, 0x00, /* usFragmentOffset. */
81 ipconfigUDP_TIME_TO_LIVE, /* ucTimeToLive */
82 ipPROTOCOL_UDP, /* ucProtocol. */
83 0x00, 0x00, /* usHeaderChecksum. */
84 0x00, 0x00, 0x00, 0x00 /* Source IP address. */
85 }
86 };
87 /*-----------------------------------------------------------*/
88
89 /**
90 * @brief Process the generated UDP packet and do other checks before sending the
91 * packet such as ARP cache check and address resolution.
92 *
93 * @param[in] pxNetworkBuffer The network buffer carrying the packet.
94 */
vProcessGeneratedUDPPacket(NetworkBufferDescriptor_t * const pxNetworkBuffer)95 void vProcessGeneratedUDPPacket( NetworkBufferDescriptor_t * const pxNetworkBuffer )
96 {
97 const UDPPacket_t * pxUDPPacket;
98
99 if( pxNetworkBuffer != NULL )
100 {
101 /* Map the UDP packet onto the start of the frame. */
102
103 /* MISRA Ref 11.3.1 [Misaligned access] */
104 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
105 /* coverity[misra_c_2012_rule_11_3_violation] */
106 pxUDPPacket = ( ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer );
107
108 switch( pxUDPPacket->xEthernetHeader.usFrameType )
109 {
110 #if ( ipconfigUSE_IPv4 != 0 )
111 case ipIPv4_FRAME_TYPE:
112 vProcessGeneratedUDPPacket_IPv4( pxNetworkBuffer );
113 break;
114 #endif
115 #if ( ipconfigUSE_IPv6 != 0 )
116 case ipIPv6_FRAME_TYPE:
117 vProcessGeneratedUDPPacket_IPv6( pxNetworkBuffer );
118 break;
119 #endif
120 default:
121 /* do nothing, coverity happy */
122 break;
123 }
124 }
125 }
126 /*-----------------------------------------------------------*/
127
128 /**
129 * @brief Process the received UDP packet.
130 *
131 * @param[in] pxNetworkBuffer The network buffer carrying the UDP packet.
132 * @param[in] usPort The port number on which this packet was received.
133 * @param[out] pxIsWaitingForARPResolution If the packet is awaiting ARP resolution,
134 * this pointer will be set to pdTRUE. pdFALSE otherwise.
135 *
136 * @return pdPASS in case the UDP packet could be processed. Else pdFAIL is returned.
137 */
xProcessReceivedUDPPacket(NetworkBufferDescriptor_t * pxNetworkBuffer,uint16_t usPort,BaseType_t * pxIsWaitingForARPResolution)138 BaseType_t xProcessReceivedUDPPacket( NetworkBufferDescriptor_t * pxNetworkBuffer,
139 uint16_t usPort,
140 BaseType_t * pxIsWaitingForARPResolution )
141 {
142 /* Returning pdPASS means that the packet was consumed, released. */
143 BaseType_t xReturn = pdFAIL;
144
145 configASSERT( pxNetworkBuffer != NULL );
146 configASSERT( pxNetworkBuffer->pucEthernetBuffer != NULL );
147
148 /* Map the ethernet buffer to the UDPPacket_t struct for easy access to the fields. */
149
150 /* MISRA Ref 11.3.1 [Misaligned access] */
151 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
152 /* coverity[misra_c_2012_rule_11_3_violation] */
153 const UDPPacket_t * pxUDPPacket = ( ( const UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer );
154
155 switch( pxUDPPacket->xEthernetHeader.usFrameType )
156 {
157 #if ( ipconfigUSE_IPv4 != 0 )
158 case ipIPv4_FRAME_TYPE:
159 xReturn = xProcessReceivedUDPPacket_IPv4( pxNetworkBuffer,
160 usPort, pxIsWaitingForARPResolution );
161 break;
162 #endif
163 #if ( ipconfigUSE_IPv6 != 0 )
164 case ipIPv6_FRAME_TYPE:
165 xReturn = xProcessReceivedUDPPacket_IPv6( pxNetworkBuffer,
166 usPort, pxIsWaitingForARPResolution );
167 break;
168 #endif
169 default:
170 /* do nothing, coverity happy */
171 break;
172 }
173
174 return xReturn;
175 }
176 /*-----------------------------------------------------------*/
177