1
10
13
14
20
21
22
23
24
25
26
27
28
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
100
101
102
103
104
110
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
381
382
383
384
385
386
387
388
389
390
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_ip.h"
#include "nx_tcp.h"
#include "nx_udp.h"
#include "nx_icmp.h"
#include "nx_igmp.h"
6 includes
#ifdef NX_ENABLE_INTERFACE_CAPABILITY...
...
VOID _nx_ip_packet_checksum_compute(NX_PACKET *packet_ptr)
{
ULONG next_protocol;
UCHAR *org_prepend_ptr;
ULONG checksum;
ULONG val;
UCHAR is_done = NX_FALSE;
ULONG ip_header_length;
ULONG ip_src_addr[4];
ULONG ip_dst_addr[4];
ULONG data_length = 0;
NX_IPV4_HEADER *ip_header_ptr;
NX_TCP_HEADER *tcp_header_ptr;
NX_UDP_HEADER *udp_header_ptr;
NX_ICMP_HEADER *icmpv4_header_ptr;
NX_IGMP_HEADER *igmp_header_ptr;
#ifdef FEATURE_NX_IPV6
USHORT short_val;
NX_ICMPV6_HEADER *icmpv6_header_ptr;
NX_IPV6_HEADER *ipv6_header_ptr;/* ... */
#endif
#ifdef FEATURE_NX_IPV6
if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V4)
{
#endif
next_protocol = NX_PROTOCOL_IPV4;
#ifdef FEATURE_NX_IPV6
}if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V4) { ... }
else
{
next_protocol = NX_PROTOCOL_IPV6;
}else { ... }
#endif
org_prepend_ptr = packet_ptr -> nx_packet_prepend_ptr;
while (!is_done)
{
switch (next_protocol)
{
case NX_PROTOCOL_IPV4:
{
/* ... */
ip_header_ptr = (NX_IPV4_HEADER *)packet_ptr -> nx_packet_prepend_ptr;
val = ip_header_ptr -> nx_ip_header_word_0;
NX_CHANGE_ULONG_ENDIAN(val);
ip_header_length = (val & NX_IP_LENGTH_MASK) >> 24;
if (packet_ptr -> nx_packet_interface_capability_flag & NX_INTERFACE_CAPABILITY_IPV4_TX_CHECKSUM)
{
checksum = _nx_ip_checksum_compute(packet_ptr, NX_IP_VERSION_V4,
ip_header_length << 2,
NULL, NULL);
val = (ULONG)(~checksum);
val = val & NX_LOWER_16_MASK;
NX_CHANGE_ULONG_ENDIAN(val);
ip_header_ptr -> nx_ip_header_word_2 = ip_header_ptr -> nx_ip_header_word_2 | val;
packet_ptr -> nx_packet_interface_capability_flag &= (ULONG)(~NX_INTERFACE_CAPABILITY_IPV4_TX_CHECKSUM);
}if (packet_ptr -> nx_packet_interface_capability_flag & NX_INTERFACE_CAPABILITY_IPV4_TX_CHECKSUM) { ... }
ip_src_addr[0] = ip_header_ptr -> nx_ip_header_source_ip;
ip_dst_addr[0] = ip_header_ptr -> nx_ip_header_destination_ip;
NX_CHANGE_ULONG_ENDIAN(ip_src_addr[0]);
NX_CHANGE_ULONG_ENDIAN(ip_dst_addr[0]);
val = ip_header_ptr -> nx_ip_header_word_2;
NX_CHANGE_ULONG_ENDIAN(val);
next_protocol = (val >> 16) & 0xFF;
packet_ptr -> nx_packet_prepend_ptr = packet_ptr -> nx_packet_prepend_ptr + (ip_header_length << 2);
data_length = packet_ptr -> nx_packet_length - (ip_header_length << 2);
break;
...}
case NX_PROTOCOL_IPV4:
case NX_PROTOCOL_TCP:
{
if (packet_ptr -> nx_packet_interface_capability_flag & NX_INTERFACE_CAPABILITY_TCP_TX_CHECKSUM)
{
checksum = _nx_ip_checksum_compute(packet_ptr, NX_PROTOCOL_TCP,
data_length,
ip_src_addr, ip_dst_addr);
tcp_header_ptr = (NX_TCP_HEADER *)packet_ptr -> nx_packet_prepend_ptr;
checksum = ~checksum & NX_LOWER_16_MASK;
NX_CHANGE_ULONG_ENDIAN(tcp_header_ptr -> nx_tcp_header_word_4);
tcp_header_ptr -> nx_tcp_header_word_4 |= (checksum << NX_SHIFT_BY_16);
NX_CHANGE_ULONG_ENDIAN(tcp_header_ptr -> nx_tcp_header_word_4);
packet_ptr -> nx_packet_interface_capability_flag &= (ULONG)(~NX_INTERFACE_CAPABILITY_TCP_TX_CHECKSUM);
}if (packet_ptr -> nx_packet_interface_capability_flag & NX_INTERFACE_CAPABILITY_TCP_TX_CHECKSUM) { ... }
is_done = NX_TRUE;
break;
...}
case NX_PROTOCOL_TCP:
case NX_PROTOCOL_UDP:
{
if (packet_ptr -> nx_packet_interface_capability_flag & NX_INTERFACE_CAPABILITY_UDP_TX_CHECKSUM)
{
checksum = _nx_ip_checksum_compute(packet_ptr, NX_PROTOCOL_UDP,
data_length,
ip_src_addr, ip_dst_addr);
udp_header_ptr = (NX_UDP_HEADER *)(packet_ptr -> nx_packet_prepend_ptr);
NX_CHANGE_ULONG_ENDIAN(udp_header_ptr -> nx_udp_header_word_1);
udp_header_ptr -> nx_udp_header_word_1 = udp_header_ptr -> nx_udp_header_word_1 | (~checksum & NX_LOWER_16_MASK);
NX_CHANGE_ULONG_ENDIAN(udp_header_ptr -> nx_udp_header_word_1);
packet_ptr -> nx_packet_interface_capability_flag &= (ULONG)(~NX_INTERFACE_CAPABILITY_UDP_TX_CHECKSUM);
}if (packet_ptr -> nx_packet_interface_capability_flag & NX_INTERFACE_CAPABILITY_UDP_TX_CHECKSUM) { ... }
is_done = NX_TRUE;
break;
...}
case NX_PROTOCOL_UDP:
case NX_PROTOCOL_ICMP:
{
if (packet_ptr -> nx_packet_interface_capability_flag & NX_INTERFACE_CAPABILITY_ICMPV4_TX_CHECKSUM)
{
checksum = _nx_ip_checksum_compute(packet_ptr, NX_IP_ICMP,
data_length,
NULL, NULL);
icmpv4_header_ptr = (NX_ICMP_HEADER *)packet_ptr -> nx_packet_prepend_ptr;
NX_CHANGE_ULONG_ENDIAN(icmpv4_header_ptr -> nx_icmp_header_word_0);
icmpv4_header_ptr -> nx_icmp_header_word_0 = icmpv4_header_ptr -> nx_icmp_header_word_0 | (~checksum & NX_LOWER_16_MASK);
NX_CHANGE_ULONG_ENDIAN(icmpv4_header_ptr -> nx_icmp_header_word_0);
packet_ptr -> nx_packet_interface_capability_flag &= (ULONG)(~NX_INTERFACE_CAPABILITY_ICMPV4_TX_CHECKSUM);
}if (packet_ptr -> nx_packet_interface_capability_flag & NX_INTERFACE_CAPABILITY_ICMPV4_TX_CHECKSUM) { ... }
is_done = NX_TRUE;
break;
...}
case NX_PROTOCOL_ICMP:
case NX_PROTOCOL_IGMP:
{
if (packet_ptr -> nx_packet_interface_capability_flag & NX_INTERFACE_CAPABILITY_IGMP_TX_CHECKSUM)
{
igmp_header_ptr = (NX_IGMP_HEADER *)packet_ptr -> nx_packet_prepend_ptr;
NX_CHANGE_ULONG_ENDIAN(igmp_header_ptr -> nx_igmp_header_word_0);
NX_CHANGE_ULONG_ENDIAN(igmp_header_ptr -> nx_igmp_header_word_1);
val = igmp_header_ptr -> nx_igmp_header_word_0;
checksum = (val >> NX_SHIFT_BY_16);
checksum += (val & NX_LOWER_16_MASK);
val = igmp_header_ptr -> nx_igmp_header_word_1;
checksum += (val >> NX_SHIFT_BY_16);
checksum += (val & NX_LOWER_16_MASK);
checksum = (checksum >> NX_SHIFT_BY_16) + (checksum & NX_LOWER_16_MASK);
checksum = (checksum >> NX_SHIFT_BY_16) + (checksum & NX_LOWER_16_MASK);
igmp_header_ptr -> nx_igmp_header_word_0 = igmp_header_ptr -> nx_igmp_header_word_0 | (~checksum & NX_LOWER_16_MASK);
NX_CHANGE_ULONG_ENDIAN(igmp_header_ptr -> nx_igmp_header_word_0);
NX_CHANGE_ULONG_ENDIAN(igmp_header_ptr -> nx_igmp_header_word_1);
packet_ptr -> nx_packet_interface_capability_flag &= (ULONG)(~NX_INTERFACE_CAPABILITY_IGMP_TX_CHECKSUM);
}if (packet_ptr -> nx_packet_interface_capability_flag & NX_INTERFACE_CAPABILITY_IGMP_TX_CHECKSUM) { ... }
is_done = NX_TRUE;
break;
...}
#ifdef FEATURE_NX_IPV6case NX_PROTOCOL_IGMP:
case NX_PROTOCOL_ICMPV6:
{
if (packet_ptr -> nx_packet_interface_capability_flag & NX_INTERFACE_CAPABILITY_ICMPV6_TX_CHECKSUM)
{
checksum = _nx_ip_checksum_compute(packet_ptr, NX_PROTOCOL_ICMPV6,
data_length,
ip_src_addr, ip_dst_addr);
icmpv6_header_ptr = (NX_ICMPV6_HEADER *)packet_ptr -> nx_packet_prepend_ptr;
short_val = (USHORT) ~checksum;
NX_CHANGE_USHORT_ENDIAN(short_val);
icmpv6_header_ptr -> nx_icmpv6_header_checksum = short_val;
packet_ptr -> nx_packet_interface_capability_flag &= (ULONG)(~NX_INTERFACE_CAPABILITY_ICMPV6_TX_CHECKSUM);
}if (packet_ptr -> nx_packet_interface_capability_flag & NX_INTERFACE_CAPABILITY_ICMPV6_TX_CHECKSUM) { ... }
is_done = NX_TRUE;
break;
...}
case NX_PROTOCOL_ICMPV6:
case NX_PROTOCOL_IPV6:
{
ipv6_header_ptr = (NX_IPV6_HEADER *)packet_ptr -> nx_packet_prepend_ptr;
COPY_IPV6_ADDRESS(ipv6_header_ptr -> nx_ip_header_source_ip, ip_src_addr);
COPY_IPV6_ADDRESS(ipv6_header_ptr -> nx_ip_header_destination_ip, ip_dst_addr);
NX_IPV6_ADDRESS_CHANGE_ENDIAN(ip_src_addr);
NX_IPV6_ADDRESS_CHANGE_ENDIAN(ip_dst_addr);
val = ipv6_header_ptr -> nx_ip_header_word_1;
NX_CHANGE_ULONG_ENDIAN(val);
next_protocol = (val >> 8) & 0xFF;
packet_ptr -> nx_packet_prepend_ptr += (ULONG)sizeof(NX_IPV6_HEADER);
data_length = packet_ptr -> nx_packet_length - (ULONG)sizeof(NX_IPV6_HEADER);
break;
...}/* ... */
#endif
default:
is_done = NX_TRUE;
break;default
}switch (next_protocol) { ... }
}while (!is_done) { ... }
packet_ptr -> nx_packet_prepend_ptr = org_prepend_ptr;
return;
}{ ... }
#endif/* ... */