1 /**************************************************************************/
2 /* */
3 /* Copyright (c) Microsoft Corporation. All rights reserved. */
4 /* */
5 /* This software is licensed under the Microsoft Software License */
6 /* Terms for Microsoft Azure RTOS. Full text of the license can be */
7 /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */
8 /* and in the root directory of this software. */
9 /* */
10 /**************************************************************************/
11
12
13 /**************************************************************************/
14 /**************************************************************************/
15 /** */
16 /** NetX Component */
17 /** */
18 /** Internet Control Message Protocol (ICMP) */
19 /** */
20 /**************************************************************************/
21 /**************************************************************************/
22
23 #define NX_SOURCE_CODE
24
25
26 /* Include necessary system files. */
27
28 #include "nx_api.h"
29 #include "nx_packet.h"
30 #include "nx_ip.h"
31 #include "nx_icmp.h"
32
33 #ifdef NX_IPSEC_ENABLE
34 #include "nx_ipsec.h"
35 #endif /* NX_IPSEC_ENABLE */
36
37 #ifndef NX_DISABLE_IPV4
38 /**************************************************************************/
39 /* */
40 /* FUNCTION RELEASE */
41 /* */
42 /* _nx_icmpv4_packet_process PORTABLE C */
43 /* 6.1 */
44 /* AUTHOR */
45 /* */
46 /* Yuxin Zhou, Microsoft Corporation */
47 /* */
48 /* DESCRIPTION */
49 /* */
50 /* This function processes the ICMPv4 received packet and lifts any */
51 /* associated threads suspended on it. */
52 /* */
53 /* INPUT */
54 /* */
55 /* ip_ptr Pointer to IP control block */
56 /* packet_ptr ICMP packet pointer */
57 /* */
58 /* OUTPUT */
59 /* */
60 /* None */
61 /* */
62 /* CALLS */
63 /* */
64 /* _nx_ip_checksum_compute Compute checksum */
65 /* _nx_ip_packet_send Send ICMP packet out */
66 /* _nx_packet_release Release packet to packet pool */
67 /* _tx_thread_system_resume Resume suspended thread */
68 /* _tx_thread_system_preempt_check Check for preemption */
69 /* */
70 /* CALLED BY */
71 /* */
72 /* _nx_icmp_packet_process Main ICMP packet pocess */
73 /* */
74 /* RELEASE HISTORY */
75 /* */
76 /* DATE NAME DESCRIPTION */
77 /* */
78 /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */
79 /* 09-30-2020 Yuxin Zhou Modified comment(s), */
80 /* resulting in version 6.1 */
81 /* */
82 /**************************************************************************/
_nx_icmpv4_packet_process(NX_IP * ip_ptr,NX_PACKET * packet_ptr)83 VOID _nx_icmpv4_packet_process(NX_IP *ip_ptr, NX_PACKET *packet_ptr)
84 {
85
86 NX_ICMPV4_HEADER *header_ptr;
87 USHORT checksum;
88 #if defined(NX_DISABLE_ICMPV4_RX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE)
89 UINT compute_checksum = 1;
90 #endif /* defined(NX_DISABLE_ICMPV4_RX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE) */
91 #ifdef TX_ENABLE_EVENT_TRACE
92 NX_IPV4_HEADER *ip_header_ptr;
93 #endif /* TX_ENABLE_EVENT_TRACE */
94
95
96 /* Add debug information. */
97 NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);
98
99 /* Point to the ICMP message header. */
100 /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */
101 header_ptr = (NX_ICMPV4_HEADER *)packet_ptr -> nx_packet_prepend_ptr;
102
103 #ifdef NX_DISABLE_ICMPV4_RX_CHECKSUM
104 compute_checksum = 0;
105 #endif /* NX_DISABLE_ICMPV4_RX_CHECKSUM */
106
107 #ifdef NX_ENABLE_INTERFACE_CAPABILITY
108 if (packet_ptr -> nx_packet_address.nx_packet_interface_ptr -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_ICMPV4_RX_CHECKSUM)
109 {
110 compute_checksum = 0;
111 }
112 #endif /* NX_ENABLE_INTERFACE_CAPABILITY */
113 #ifdef NX_IPSEC_ENABLE
114 if ((packet_ptr -> nx_packet_ipsec_sa_ptr != NX_NULL) && (((NX_IPSEC_SA *)(packet_ptr -> nx_packet_ipsec_sa_ptr)) -> nx_ipsec_sa_encryption_method != NX_CRYPTO_NONE))
115 {
116 compute_checksum = 1;
117 }
118 #endif
119 #if defined(NX_DISABLE_ICMPV4_RX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE)
120 if (compute_checksum)
121 #endif /* defined(NX_DISABLE_ICMPV4_RX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE) */
122 {
123
124 /* Calculate the ICMP message checksum. */
125 checksum = _nx_ip_checksum_compute(packet_ptr, NX_IP_ICMP,
126 (UINT)packet_ptr -> nx_packet_length,
127 /* ICMPv4 checksum does not include
128 src/dest addresses */
129 NX_NULL, NX_NULL);
130
131 checksum = ((USHORT) ~checksum) & NX_LOWER_16_MASK;
132
133 /* Determine if the checksum is valid. */
134 if (checksum)
135 {
136
137 #ifndef NX_DISABLE_ICMP_INFO
138
139 /* Increment the ICMP invalid packet error. */
140 ip_ptr -> nx_ip_icmp_invalid_packets++;
141
142 /* Increment the ICMP checksum error count. */
143 ip_ptr -> nx_ip_icmp_checksum_errors++;
144 #endif
145
146 /* Nope, the checksum is invalid. Toss this ICMP packet out. */
147 _nx_packet_release(packet_ptr);
148 return;
149 }
150 }
151
152 /* Determine the message type and call the appropriate handler. */
153 if (header_ptr -> nx_icmpv4_header_type == NX_ICMP_ECHO_REPLY_TYPE)
154 {
155 _nx_icmpv4_process_echo_reply(ip_ptr, packet_ptr);
156 }
157 else if (header_ptr -> nx_icmpv4_header_type == NX_ICMP_ECHO_REQUEST_TYPE)
158 {
159 _nx_icmpv4_process_echo_request(ip_ptr, packet_ptr);
160 }
161 else
162 {
163
164 #ifndef NX_DISABLE_ICMP_INFO
165
166 /* Increment the ICMP unhandled message count. */
167 ip_ptr -> nx_ip_icmp_unhandled_messages++;
168 #endif
169
170 #ifdef TX_ENABLE_EVENT_TRACE
171
172 /* Set the IP header. */
173 ip_header_ptr = (NX_IPV4_HEADER *)packet_ptr -> nx_packet_ip_header;
174
175 /* If trace is enabled, insert this event into the trace buffer. */
176 NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_ICMP_RECEIVE, ip_ptr, ip_header_ptr -> nx_ip_header_source_ip, packet_ptr, 0, NX_TRACE_INTERNAL_EVENTS, 0, 0);
177 #endif /* TX_ENABLE_EVENT_TRACE */
178
179 /* Unhandled ICMP message, just release it. */
180 _nx_packet_release(packet_ptr);
181 }
182 }
183 #endif /* !NX_DISABLE_IPV4 */
184
185