1
10
13
14
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
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
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
182
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
234
235
236
242
243
244
245
246
247
248
249
250
251
252
253
257
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_icmp.h"
#include "nx_icmpv6.h"
#include "nx_ip.h"
...
...
USHORT _nx_ip_checksum_compute(NX_PACKET *packet_ptr, ULONG protocol,
UINT data_length, ULONG *src_ip_addr,
ULONG *dest_ip_addr)
{
ULONG checksum = 0;
USHORT tmp;
USHORT *short_ptr;
ULONG *long_ptr;
#ifndef NX_DISABLE_PACKET_CHAIN
ULONG packet_size;
#endif
NX_PACKET *current_packet;
ALIGN_TYPE end_ptr;
#ifdef FEATURE_NX_IPV6
UINT i;
#endif
/* ... */
if ((protocol == NX_PROTOCOL_UDP) ||
#ifdef FEATURE_NX_IPV6
(protocol == NX_PROTOCOL_ICMPV6) ||
#endif
(protocol == NX_PROTOCOL_TCP))
{
USHORT *src_ip_short, *dest_ip_short;
checksum = protocol;
NX_ASSERT((src_ip_addr != NX_NULL) && (dest_ip_addr != NX_NULL));
src_ip_short = (USHORT *)src_ip_addr;
dest_ip_short = (USHORT *)dest_ip_addr;
checksum += src_ip_short[0];
checksum += src_ip_short[1];
checksum += dest_ip_short[0];
checksum += dest_ip_short[1];
#ifdef FEATURE_NX_IPV6
/* ... */
if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V6)
{
for (i = 2; i < 8; i++)
{
checksum += dest_ip_short[i];
checksum += src_ip_short[i];
}for (i = 2; i < 8; i++) { ... }
}if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V6) { ... }
/* ... */#endif
checksum += data_length;
checksum = (checksum >> 16) + (checksum & 0xFFFF);
checksum = (checksum >> 16) + (checksum & 0xFFFF);
tmp = (USHORT)checksum;
NX_CHANGE_USHORT_ENDIAN(tmp);
checksum = tmp;
}if ((protocol == NX_PROTOCOL_UDP) || #ifdef FEATURE_NX_IPV6 (protocol == NX_PROTOCOL_ICMPV6) || #endif /* FEATURE_NX_IPV6 */ (protocol == NX_PROTOCOL_TCP)) { ... }
long_ptr = (ULONG *)packet_ptr -> nx_packet_prepend_ptr;
current_packet = packet_ptr;
#ifndef NX_DISABLE_PACKET_CHAIN
while (current_packet)
{
packet_size = (ULONG)(current_packet -> nx_packet_append_ptr - current_packet -> nx_packet_prepend_ptr);
if (data_length > (UINT)packet_size)
{
end_ptr = ((ALIGN_TYPE)current_packet -> nx_packet_append_ptr) & (ALIGN_TYPE)(~3);
}if (data_length > (UINT)packet_size) { ... }
else
{
#endif
end_ptr = (ALIGN_TYPE)current_packet -> nx_packet_prepend_ptr + data_length - 3;
#ifndef NX_DISABLE_PACKET_CHAIN
}else { ... }
#endif
long_ptr = (ULONG *)current_packet -> nx_packet_prepend_ptr;
if ((ALIGN_TYPE)long_ptr < end_ptr)
{
data_length -= (UINT)(((end_ptr + 3) & (ALIGN_TYPE)(~3llu)) - (ALIGN_TYPE)long_ptr);
while ((ALIGN_TYPE)long_ptr < end_ptr)
{
checksum += (*long_ptr & NX_LOWER_16_MASK);
checksum += (*long_ptr >> NX_SHIFT_BY_16);
long_ptr++;
}while ((ALIGN_TYPE)long_ptr < end_ptr) { ... }
}if ((ALIGN_TYPE)long_ptr < end_ptr) { ... }
#ifndef NX_DISABLE_PACKET_CHAIN
if ((data_length > 0) && (current_packet -> nx_packet_next))
{
if ((((ALIGN_TYPE)current_packet -> nx_packet_append_ptr) & 3) == 2)
{
short_ptr = (USHORT *)long_ptr;
checksum += *short_ptr;
data_length -= 2;
}if ((((ALIGN_TYPE)current_packet -> nx_packet_append_ptr) & 3) == 2) { ... }
/* ... */
current_packet = current_packet -> nx_packet_next;
}if ((data_length > 0) && (current_packet -> nx_packet_next)) { ... }
else
{
current_packet = NX_NULL;
}else { ... }
}/* ... */
while (current_packet) { ... }#endif
if (data_length)
{
short_ptr = (USHORT *)(long_ptr);
if (data_length == 1)
{
*((UCHAR *)short_ptr + 1) = 0;
}if (data_length == 1) { ... }
else if (data_length == 3)
{
checksum += *short_ptr;
short_ptr++;
*((UCHAR *)short_ptr + 1) = 0;
}else if (data_length == 3) { ... }
checksum += *short_ptr;
}if (data_length) { ... }
checksum = (checksum >> 16) + (checksum & 0xFFFF);
checksum = (checksum >> 16) + (checksum & 0xFFFF);
tmp = (USHORT)checksum;
NX_CHANGE_USHORT_ENDIAN(tmp);
return(tmp);
}{ ... }