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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
/***************************************************************************
* 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_ip.h"
#include "nx_ipv6.h"
#include "nx_icmpv6.h"
#ifdef FEATURE_NX_IPV6
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_icmpv6_process_redirect PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function processes an incoming ICMPv6 redirect message. */
/* */
/* INPUT */
/* */
/* ip_ptr Pointer to IP control block */
/* packet_ptr ICMP packet pointer */
/* */
/* OUTPUT */
/* */
/* NONE */
/* */
/* CALLS */
/* */
/* IPv6_Address_Type Find IP address type. */
/* _nxd_ipv6_destination_table_find_next_hop */
/* Finds next hop address. */
/* _nxd_ipv6_router_lookup Routine that finds default */
/* router. */
/* _nx_icmpv6_validate_options Check for valid option headers */
/* _nx_nd_cache_find_entry Find entry in ND cache by IP */
/* address */
/* _nx_nd_cache_add Add entry to ND cache */
/* _nxd_ipv6_default_router_add_internal Update router list with */
/* redirect next hop */
/* _nx_icmpv6_send_queued_packets Transmit queued packets waiting */
/* for physical mapping */
/* */
/* CALLED BY */
/* */
/* _nx_icmpv6_packet_process Main ICMPv6 packet pocess */
/* */
/**************************************************************************/
#ifndef NX_DISABLE_ICMPV6_REDIRECT_PROCESS
VOID _nx_icmpv6_process_redirect(NX_IP *ip_ptr, NX_PACKET *packet_ptr)
{
NX_ICMPV6_REDIRECT_MESSAGE *redirect_ptr;
NX_IPV6_HEADER *ip_header;
UINT status;
ULONG source_address_type;
ULONG router_address[4];
ULONG error = 0;
ND_CACHE_ENTRY *nd_entry = NX_NULL;
ND_CACHE_ENTRY *NDCacheEntry = NX_NULL;
UINT i;
NX_ICMPV6_OPTION *option_ptr;
ULONG packet_length, option_length;
NX_IPV6_DEFAULT_ROUTER_ENTRY *rt_entry;
NX_INTERFACE *interface_ptr;
#ifdef NX_ENABLE_IPV6_PATH_MTU_DISCOVERY
NX_ICMPV6_OPTION_MTU *mtu_ptr;
#endif
ULONG mtu;
ULONG mtu_timeout;
NX_IPV6_DESTINATION_ENTRY *dest_entry_ptr;
/* Add debug information. */
NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);
#ifndef NX_DISABLE_RX_SIZE_CHECKING
if ((packet_ptr -> nx_packet_length < sizeof(NX_ICMPV6_REDIRECT_MESSAGE))
#ifndef NX_DISABLE_PACKET_CHAIN
|| (packet_ptr -> nx_packet_next) /* Ignore chained packet. */
#endif /* NX_DISABLE_PACKET_CHAIN */
)
{
#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 */
/* Get interface pointer. */
interface_ptr = packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address_attached;
/* Initialize the MTU. */
mtu = interface_ptr -> nx_interface_ip_mtu_size;
mtu_timeout = NX_WAIT_FOREVER;
/* Locate the IPv6 header. */
/*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */
ip_header = (NX_IPV6_HEADER *)(packet_ptr -> nx_packet_ip_header);
/* Locate the redirect message. */
/*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */
redirect_ptr = (NX_ICMPV6_REDIRECT_MESSAGE *)(packet_ptr -> nx_packet_prepend_ptr);
/* RFC 2461 4.5 the IPv6 and ICMPv6 header fields. */
/* Convert the destination and target address to host byte order. */
NX_IPV6_ADDRESS_CHANGE_ENDIAN(redirect_ptr -> nx_icmpv6_redirect_target_address);
NX_IPV6_ADDRESS_CHANGE_ENDIAN(redirect_ptr -> nx_icmpv6_redirect_destination_address);
/* Verify that sender address is link-local address. */
source_address_type = IPv6_Address_Type(ip_header -> nx_ip_header_source_ip);
if ((source_address_type & 0xFF) != IPV6_ADDRESS_LINKLOCAL)
{
error = 1;
}
/* Verify that hop limit is 255 */
else if ((ip_header -> nx_ip_header_word_1 & 0xFF) != 0xFF)
{
error = 1;
}
/* Verifty that ICMP code is zero. */
else if (redirect_ptr -> nx_icmpv6_redirect_icmpv6_header.nx_icmpv6_header_code != 0)
{
error = 1;
}
/* The IP source address of the redirect is the same as the current first-hop
router for the specified ICMP destination address. */
/* First look for redirect in the destination table. */
if (_nxd_ipv6_destination_table_find_next_hop(ip_ptr, redirect_ptr -> nx_icmpv6_redirect_destination_address,
router_address) == NX_SUCCESS)
{
/* Make sure the source is the current 1st hop router. */
if (!CHECK_IPV6_ADDRESSES_SAME(router_address, ip_header -> nx_ip_header_source_ip))
{
error = 1;
}
}
/* Next check if the redirect is the default router. */
/* Suppress cast of pointer to pointer, since it is necessary */
else if (_nxd_ipv6_router_lookup(ip_ptr, interface_ptr, router_address, /*lint -e{929}*/ (void **)&NDCacheEntry))
{
/* No default router. */
error = 1;
}
/* Make sure the source is the current 1st hop router. */
if (!CHECK_IPV6_ADDRESSES_SAME(ip_header -> nx_ip_header_source_ip, router_address))
{
error = 1;
}
/* The destination address field may not be multicast. */
else if (IPv6_Address_Type(redirect_ptr -> nx_icmpv6_redirect_destination_address) & IPV6_ADDRESS_MULTICAST)
{
error = 1;
}
/* Is target address is different from destination address? */
else if (!(CHECK_IPV6_ADDRESSES_SAME(redirect_ptr -> nx_icmpv6_redirect_destination_address,
redirect_ptr -> nx_icmpv6_redirect_target_address)))
{
/* Yes, so the target is a router, therefore the target address must be link-local.*/
if ((IPv6_Address_Type(redirect_ptr -> nx_icmpv6_redirect_target_address) & IPV6_ADDRESS_LINKLOCAL) == 0)
{
/* It isn't. */
error = 1;
}
}
/* Release the packet if it fails these validation checks. */
if (error)
{
#ifndef NX_DISABLE_ICMP_INFO
/* Increment the ICMP invalid packet error. */
ip_ptr -> nx_ip_icmp_invalid_packets++;
#endif /* NX_DISABLE_ICMP_INFO */
_nx_packet_release(packet_ptr);
return;
}
/* If there are additional options, it could be TLLA or the redirected header. */
if (packet_ptr -> nx_packet_length - sizeof(NX_ICMPV6_REDIRECT_MESSAGE))
{
/*lint -e{923} suppress cast between pointer and ULONG, since it is necessary */
option_ptr = (NX_ICMPV6_OPTION *)NX_UCHAR_POINTER_ADD(redirect_ptr, sizeof(NX_ICMPV6_REDIRECT_MESSAGE));
packet_length = packet_ptr -> nx_packet_length - (ULONG)sizeof(NX_ICMPV6_REDIRECT_MESSAGE);
/* Validate option fields. */
if (_nx_icmpv6_validate_options(option_ptr, (INT)packet_length, 0) == NX_NOT_SUCCESSFUL)
{
#ifndef NX_DISABLE_ICMP_INFO
/* Increment the ICMP invalid packet error. */
ip_ptr -> nx_ip_icmp_invalid_packets++;
#endif /* NX_DISABLE_ICMP_INFO */
_nx_packet_release(packet_ptr);
return;
}
while (packet_length)
{
/* The packet contains target link layer address. */
if (option_ptr -> nx_icmpv6_option_type == ICMPV6_OPTION_TYPE_TRG_LINK_ADDR)
{
status = _nx_nd_cache_find_entry(ip_ptr, redirect_ptr -> nx_icmpv6_redirect_target_address, &nd_entry);
if (status != NX_SUCCESS)
{
/* The entry is not found in the ND cache table. Simply add it, and we are done. */
/*lint -e{929} -e{740} -e{826} suppress cast of pointer to pointer, since it is necessary */
if (_nx_nd_cache_add(ip_ptr, redirect_ptr -> nx_icmpv6_redirect_target_address,
interface_ptr,
(CHAR *)&option_ptr -> nx_icmpv6_option_data, 0, ND_CACHE_STATE_STALE,
packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr, &nd_entry) == NX_SUCCESS)
{
/* If the destination and target addresses are different, the redirect is to a router. */
if (!CHECK_IPV6_ADDRESSES_SAME(redirect_ptr -> nx_icmpv6_redirect_target_address,
redirect_ptr -> nx_icmpv6_redirect_destination_address))
{
/* We need to fill in the IsRouter field in the cache entry
with the default router. */
_nxd_ipv6_default_router_add_internal(ip_ptr,
redirect_ptr -> nx_icmpv6_redirect_target_address, /* TLLA in Redirect ICMPv6 options */
0,
interface_ptr,
NX_IPV6_ROUTE_TYPE_UNSOLICITATED, &rt_entry);
/* And cross link this entry with the entry in the router list. */
/*lint -e{644} suppress variable might not be initialized, since "rt_entry" was initialized in _nxd_ipv6_default_router_add_internal. */
/*lint -e{613} suppress possible use of null pointer, since "nd_entry" was set in _nx_nd_cache_add. */
nd_entry -> nx_nd_cache_is_router = rt_entry;
}
}
}
else
{
/* This entry already exists. If the mac address is the same, do not update the entry. Otherwise,
update the entry and set the state to STALE (RFC2461 7.2.3) */
ULONG mac_msw, mac_lsw, new_msw, new_lsw;
/*lint -e{928} suppress cast of pointer to pointer, since it is necessary */
UCHAR *new_mac = (UCHAR *)&option_ptr -> nx_icmpv6_option_data;
mac_msw = ((ULONG)(nd_entry -> nx_nd_cache_mac_addr[0]) << 8) | (nd_entry -> nx_nd_cache_mac_addr[1]);
new_msw = ((ULONG)(new_mac[0]) << 8) | (new_mac[1]);
mac_lsw = ((ULONG)(nd_entry -> nx_nd_cache_mac_addr[2]) << 24) | ((ULONG)(nd_entry -> nx_nd_cache_mac_addr[3]) << 16) |
((ULONG)(nd_entry -> nx_nd_cache_mac_addr[4]) << 8) | nd_entry -> nx_nd_cache_mac_addr[5];
new_lsw = ((ULONG)(new_mac[2]) << 24) | ((ULONG)(new_mac[3]) << 16) | ((ULONG)(new_mac[4]) << 8) | new_mac[5]; /* lgtm[cpp/overflow-buffer] */
if ((mac_msw != new_msw) || (mac_lsw != new_lsw)) /* If the new MAC is different. */
{
/* Update the MAC address. */
for (i = 0; i < 6; i++)
{
nd_entry -> nx_nd_cache_mac_addr[i] = new_mac[i];
}
/* Set the state to STALE. */
nd_entry -> nx_nd_cache_nd_status = ND_CACHE_STATE_STALE;
/* Set the interface. */
nd_entry -> nx_nd_cache_interface_ptr = interface_ptr;
}
/* If there are packets chained on the entry waiting to be transmitted: */
if (nd_entry -> nx_nd_cache_packet_waiting_head) /* There are packets waiting to be transmitted */
{
_nx_icmpv6_send_queued_packets(ip_ptr, nd_entry);
}
}
}
#ifdef NX_ENABLE_IPV6_PATH_MTU_DISCOVERY
else if (option_ptr -> nx_icmpv6_option_type == ICMPV6_OPTION_TYPE_MTU)
{
/* Get a local pointer to the MTU option. */
/*lint -e{929} -e{740} -e{826} suppress cast of pointer to pointer, since it is necessary */
mtu_ptr = (NX_ICMPV6_OPTION_MTU *)option_ptr;
/* Apply the router's next hop (on link) path MTU. */
mtu = mtu_ptr -> nx_icmpv6_option_mtu_path_mtu;
NX_CHANGE_ULONG_ENDIAN(mtu);
if (mtu < NX_MINIMUM_IPV6_PATH_MTU)
{
/* Update the timeout of MTU. */
mtu_timeout = NX_PATH_MTU_INCREASE_WAIT_INTERVAL_TICKS;
}
}
#endif /* NX_ENABLE_IPV6_PATH_MTU_DISCOVERY */
option_length = (UINT)(option_ptr -> nx_icmpv6_option_length << 3);
packet_length -= option_length;
/*lint -e{923} suppress cast between pointer and ULONG, since it is necessary */
option_ptr = (NX_ICMPV6_OPTION *)NX_UCHAR_POINTER_ADD(option_ptr, option_length);
}
}
/*
* If the packet contains target link layer address, we should have added the link layer address
* to the ND cache, and the next block is not executed.
* However if the packet does not contain target link layer address (thus nd_cache is NULL at
* this point, we create an entry.
*/
if (nd_entry == NX_NULL)
{
status = _nx_nd_cache_find_entry(ip_ptr, redirect_ptr -> nx_icmpv6_redirect_target_address, &nd_entry);
if (status != NX_SUCCESS)
{
if (_nx_nd_cache_add_entry(ip_ptr, redirect_ptr -> nx_icmpv6_redirect_target_address,
packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr, &nd_entry) != NX_SUCCESS)
{
#ifndef NX_DISABLE_ICMP_INFO
/* Increment the ICMP invalid packet error. */
ip_ptr -> nx_ip_icmp_invalid_packets++;
#endif /* NX_DISABLE_ICMP_INFO */
/* Release the packet. */
_nx_packet_release(packet_ptr);
return;
}
}
}
/* Find/update existing entry or add a new one for this router in the destination table.
A new entry will be assigned default path MTU and MTU timeout values. */
status = _nx_icmpv6_dest_table_add(ip_ptr, redirect_ptr -> nx_icmpv6_redirect_destination_address,
&dest_entry_ptr, redirect_ptr -> nx_icmpv6_redirect_target_address,
mtu, mtu_timeout,
packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr);
if (status)
{
#ifndef NX_DISABLE_ICMP_INFO
/* Increment the ICMP invalid packet error. */
ip_ptr -> nx_ip_icmp_invalid_packets++;
#endif /* NX_DISABLE_ICMP_INFO */
/* Release the packet. */
_nx_packet_release(packet_ptr);
return;
}
/* Cross link the nd_entry. */
/*lint -e{644} suppress variable might not be initialized, since "dest_entry_ptr" was initialized _nx_icmpv6_dest_table_find or _nx_icmpv6_dest_table_add. */
dest_entry_ptr -> nx_ipv6_destination_entry_nd_entry = nd_entry;
/* Release the packet. */
_nx_packet_release(packet_ptr);
}
#endif /* NX_DISABLE_ICMPV6_REDIRECT_PROCESS */
#endif /* FEATURE_NX_IPV6 */
|