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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
* Copyright (c) 2025-present Eclipse ThreadX Contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** NetX Component */
/** */
/** Internet Control Message Protocol (ICMP) */
/** */
/**************************************************************************/
/**************************************************************************/
#define NX_SOURCE_CODE
/* Include necessary system files. */
#include "nx_api.h"
#include "nx_packet.h"
#include "nx_ipv6.h"
#include "nx_icmpv6.h"
#ifdef NX_IPSEC_ENABLE
#include "nx_ipsec.h"
#endif /* NX_IPSEC_ENABLE */
#ifdef FEATURE_NX_IPV6
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_icmpv6_process_echo_request PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This internal function processes incoming echo request message. */
/* It validates the echo request and sends an echo reply back to the */
/* sender. Note that when formulating an echo reply, the function */
/* updates the corresponding IPv6 and ICMPv6 header. The content */
/* of the ICMP echo request message is untouched. The ICMPv6 */
/* checksum computation also takes a shortcut by adjusting the */
/* original checksum values for ICMPv6 header field changes. */
/* */
/* INPUT */
/* */
/* ip_ptr IP stack instance */
/* packet_ptr Received echo request packet */
/* */
/* OUTPUT */
/* */
/* None */
/* */
/* CALLS */
/* */
/* _nx_packet_release Release packet back to the packet pool */
/* _nx_ipv6_packet_send Transmit IPv6 packet to remote host */
/* */
/* CALLED BY */
/* */
/* _nx_icmpv6_packet_process Main ICMPv6 packet handler */
/* */
/**************************************************************************/
VOID _nx_icmpv6_process_echo_request(NX_IP *ip_ptr, NX_PACKET *packet_ptr)
{
UINT status;
ULONG tmp;
USHORT checksum;
#if defined(NX_DISABLE_ICMPV6_TX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE)
UINT compute_checksum = 1;
#endif /* defined(NX_DISABLE_ICMPV6_TX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE) */
NX_ICMPV6_HEADER *header_ptr;
NX_IPV6_HEADER *ipv6_header;
ULONG hop_limit = 255;
NXD_ADDRESS dest_addr;
NX_INTERFACE *interface_ptr;
#ifdef NX_IPSEC_ENABLE
ULONG data_offset;
VOID *sa = NX_NULL;
NXD_ADDRESS src_addr;
UINT ret;
#endif /* NX_IPSEC_ENABLE */
/* Add debug information. */
NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);
#ifndef NX_DISABLE_RX_SIZE_CHECKING
/* Check packet length. */
if (packet_ptr -> nx_packet_length < sizeof(NX_ICMPV6_ECHO))
{
#ifndef NX_DISABLE_ICMP_INFO
/* Increment the ICMP invalid message count. */
ip_ptr -> nx_ip_icmp_invalid_packets++;
#endif
/* Invalid ICMP message, just release it. */
_nx_packet_release(packet_ptr);
return;
}
#endif /* NX_DISABLE_RX_SIZE_CHECKING */
/* Points to the ICMP message header. */
/*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */
header_ptr = (NX_ICMPV6_HEADER *)packet_ptr -> nx_packet_prepend_ptr;
/* Points to the IPv6 header. */
/*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */
ipv6_header = (NX_IPV6_HEADER *)packet_ptr -> nx_packet_ip_header;
/* Check if the destination address is multicast address. */
if (IPv6_Address_Type(ipv6_header -> nx_ip_header_destination_ip) & IPV6_ADDRESS_MULTICAST)
{
/* Yes, Set the interface. */
interface_ptr = packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address_attached;
/* Find a suitable outgoing address. */
status = _nxd_ipv6_interface_find(ip_ptr, ipv6_header -> nx_ip_header_source_ip,
&packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr, interface_ptr);
/* Cannot find usable outgoing interface. */
if (status != NX_SUCCESS)
{
/* Release the packet. */
_nx_packet_release(packet_ptr);
return;
}
}
else
{
/* Make sure the interface IP address has been validated. */
if (packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address_state != NX_IPV6_ADDR_STATE_VALID)
{
/* Not validated, so release the packet and abort.*/
_nx_packet_release(packet_ptr);
return;
}
}
/* Discard the packet if source address is unspecified (::). */
if (CHECK_UNSPECIFIED_ADDRESS(ipv6_header -> nx_ip_header_source_ip))
{
/* NULL address in the header. Release the packet and abort. */
_nx_packet_release(packet_ptr);
return;
}
#ifndef NX_DISABLE_ICMP_INFO
/* Increment the ICMP pings received count. */
ip_ptr -> nx_ip_pings_received++;
#endif
/* Respond to echo request packet. */
/* Set up the destination address. */
dest_addr.nxd_ip_version = NX_IP_VERSION_V6;
dest_addr.nxd_ip_address.v6[0] = ipv6_header -> nx_ip_header_source_ip[0];
dest_addr.nxd_ip_address.v6[1] = ipv6_header -> nx_ip_header_source_ip[1];
dest_addr.nxd_ip_address.v6[2] = ipv6_header -> nx_ip_header_source_ip[2];
dest_addr.nxd_ip_address.v6[3] = ipv6_header -> nx_ip_header_source_ip[3];
#ifdef NX_IPSEC_ENABLE
/* Set up the source address for IPSec SA lookup. */
src_addr.nxd_ip_version = NX_IP_VERSION_V6;
COPY_IPV6_ADDRESS(packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address,
src_addr.nxd_ip_address.v6);
/* Check if IPsec is enabled. */
if (ip_ptr -> nx_ip_packet_egress_sa_lookup != NX_NULL)
{
/* Check for possible SA match. */
ret = ip_ptr -> nx_ip_packet_egress_sa_lookup(ip_ptr, /* IP ptr */
&src_addr, /* src_addr */
&dest_addr, /* dest_addr */
NX_PROTOCOL_ICMPV6, /* protocol */
0, /* port, not used. */
0, /* port, not used. */
&data_offset, &sa, (NX_ICMPV6_ECHO_REPLY_TYPE << 8));
/* We have a match; apply IPSec processing on this packet. */
if (ret == NX_IPSEC_TRAFFIC_PROTECT)
{
/* Make sure the outgoing packet has enough space for IPsec header info. */
if ((ULONG)(packet_ptr -> nx_packet_prepend_ptr - packet_ptr -> nx_packet_data_start) <
(NX_IPv6_PACKET + data_offset))
{
/* Not enough space. Release the packet and return. */
_nx_packet_release(packet_ptr);
return;
}
/* Save the SA to the packet. */
packet_ptr -> nx_packet_ipsec_sa_ptr = sa;
}
else if (ret == NX_IPSEC_TRAFFIC_DROP || ret == NX_IPSEC_TRAFFIC_PENDING_IKEV2)
{
/* IPSec SA disallows this packet. Drop the packet and return. */
_nx_packet_release(packet_ptr);
return;
}
else
{
/* IPSec SA indicates the packet requires no IPSec processing.
Zero out sa information. */
packet_ptr -> nx_packet_ipsec_sa_ptr = NX_NULL;
}
}
#endif /* NX_IPSEC_ENABLE */
/* Change the type to Echo Reply and send back the message to the caller. */
header_ptr -> nx_icmpv6_header_type = NX_ICMPV6_ECHO_REPLY_TYPE;
#ifdef NX_DISABLE_ICMPV6_TX_CHECKSUM
compute_checksum = 0;
#endif /* NX_DISABLE_ICMPV6_TX_CHECKSUM */
#ifdef NX_ENABLE_INTERFACE_CAPABILITY
if (packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address_attached -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_ICMPV6_TX_CHECKSUM)
{
compute_checksum = 0;
}
#endif /* NX_ENABLE_INTERFACE_CAPABILITY */
#ifdef NX_IPSEC_ENABLE
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))
{
compute_checksum = 1;
}
#endif
#if defined(NX_DISABLE_ICMPV6_TX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE)
if (compute_checksum)
#endif /* defined(NX_DISABLE_ICMPV6_TX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE) */
{
/* Take a short cut to fix the checksum. */
checksum = header_ptr -> nx_icmpv6_header_checksum;
/* Change to host byte order. */
NX_CHANGE_USHORT_ENDIAN(checksum);
tmp = ((USHORT)(~checksum) & 0xFFFF);
/* The original ICMP type is ECHO_REQUEST. */
tmp -= (NX_ICMPV6_ECHO_REQUEST_TYPE << 8);
if (tmp > (ULONG)0x80000000)
{
tmp = (tmp & 0xFFFF) - 1;
}
tmp += (ULONG)(header_ptr -> nx_icmpv6_header_type << 8);
/* Compute the checksum differently depending if the echo request sends to
a multicast or unicast address. */
if ((IPv6_Address_Type(ipv6_header -> nx_ip_header_destination_ip) &
IPV6_ADDRESS_MULTICAST) == IPV6_ADDRESS_MULTICAST)
{
/* Compute the checksum for a multicast address. */
header_ptr -> nx_icmpv6_header_checksum = 0;
tmp = _nx_ip_checksum_compute(packet_ptr,
NX_PROTOCOL_ICMPV6,
(UINT)packet_ptr -> nx_packet_length,
packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address,
ipv6_header -> nx_ip_header_source_ip);
tmp = ~tmp;
header_ptr -> nx_icmpv6_header_checksum = (USHORT)(tmp);
NX_CHANGE_USHORT_ENDIAN(header_ptr -> nx_icmpv6_header_checksum);
hop_limit = 255;
}
else
{
/* Compute the checksum for a unicast address. */
hop_limit = ip_ptr -> nx_ipv6_hop_limit;
tmp = (tmp >> 16) + (tmp & 0xFFFF);
/* Do it again in case of carrying */
tmp = (tmp >> 16) + (tmp & 0xFFFF);
header_ptr -> nx_icmpv6_header_checksum = (USHORT)(~tmp);
NX_CHANGE_USHORT_ENDIAN(header_ptr -> nx_icmpv6_header_checksum);
}
}
#if defined(NX_DISABLE_ICMPV6_TX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY)
else
{
/* Fix the bug when NX_DISABLE_ICMP_TX_CHECKSUM has been set, the nx_icmpv6_header_type is not modified. */
/* Change the type to Echo Reply and send back the message to the caller. */
header_ptr -> nx_icmpv6_header_checksum = 0;
if ((IPv6_Address_Type(ipv6_header -> nx_ip_header_destination_ip) &
IPV6_ADDRESS_MULTICAST) == IPV6_ADDRESS_MULTICAST)
{
hop_limit = 255;
}
else
{
hop_limit = ip_ptr -> nx_ipv6_hop_limit;
}
#ifdef NX_ENABLE_INTERFACE_CAPABILITY
packet_ptr -> nx_packet_interface_capability_flag |= NX_INTERFACE_CAPABILITY_ICMPV6_TX_CHECKSUM;
#endif /* NX_ENABLE_INTERFACE_CAPABILITY */
}
#endif
#ifndef NX_DISABLE_ICMP_INFO
/* Increment the ICMP pings responded to count. */
ip_ptr -> nx_ip_pings_responded_to++;
#endif
/* Send the ICMP packet to the IP component. */
_nx_ipv6_packet_send(ip_ptr, packet_ptr, NX_PROTOCOL_ICMPV6,
packet_ptr -> nx_packet_length, hop_limit,
packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address,
dest_addr.nxd_ip_address.v6);
}
#endif /* FEATURE_NX_IPV6 */
|