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
99
100
101
102
103
104
105
106
107
108
109
113
114
115
116
117
118
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
164
165
166
167
168
169
170
174
175
176
177
178
179
182
183
184
185
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
210
216
217
218
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
305
306
308
309
310
311
312
313
314
315
322
323
324
325
326
327
328
329
330
331
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
351
352
353
354
360
361
362
363
364
365
366
368
369
370
371
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
414
415
416
417
418
419
420
421
422
423
424
426
427
428
429
430
431
432
433
434
435
436
438
439
440
441
442
443
444
445
447
448
449
450
451
452
457
459
460
461
462
463
464
465
466
467
468
469
470
471
472
479
480
481
482
488
489
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_tcp.h"
#include "nx_packet.h"
...
...
VOID _nx_tcp_socket_packet_process(NX_TCP_SOCKET *socket_ptr, NX_PACKET *packet_ptr)
{
UINT packet_queued = NX_FALSE;
NX_TCP_HEADER tcp_header_copy;
VOID (*urgent_callback)(NX_TCP_SOCKET *socket_ptr);
ULONG header_length;
ULONG packet_data_length;
ULONG packet_sequence;
ULONG rx_sequence;
ULONG rx_window;
UINT outside_of_window;
ULONG mss = 0;
#ifdef NX_ENABLE_TCPIP_OFFLOAD
ULONG tcpip_offload;
tcpip_offload = socket_ptr -> nx_tcp_socket_connect_interface -> nx_interface_capability_flag &
NX_INTERFACE_CAPABILITY_TCPIP_OFFLOAD;/* ... */
#endif
NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);
/* ... */
tcp_header_copy = *((NX_TCP_HEADER *)packet_ptr -> nx_packet_prepend_ptr);
header_length = (tcp_header_copy.nx_tcp_header_word_3 >> NX_TCP_HEADER_SHIFT) * (ULONG)sizeof(ULONG);
if ((socket_ptr -> nx_tcp_socket_state >= NX_TCP_SYN_RECEIVED)
#ifdef NX_ENABLE_TCPIP_OFFLOAD
&& (!tcpip_offload)
#endif
)
{
packet_sequence = tcp_header_copy.nx_tcp_sequence_number;
packet_data_length = packet_ptr -> nx_packet_length - header_length;
rx_sequence = socket_ptr -> nx_tcp_socket_rx_sequence;
#ifdef NX_ENABLE_LOW_WATERMARK
if ((socket_ptr -> nx_tcp_socket_rx_window_current == 0) &&
(socket_ptr -> nx_tcp_socket_receive_queue_head == NX_NULL) &&
(packet_ptr -> nx_packet_pool_owner -> nx_packet_pool_available >=
packet_ptr -> nx_packet_pool_owner -> nx_packet_pool_low_watermark))
{
socket_ptr -> nx_tcp_socket_rx_window_current = socket_ptr -> nx_tcp_socket_rx_window_default;
}if ((socket_ptr -> nx_tcp_socket_rx_window_current == 0) && (socket_ptr -> nx_tcp_socket_receive_queue_head == NX_NULL) && (packet_ptr -> nx_packet_pool_owner -> nx_packet_pool_available >= packet_ptr -> nx_packet_pool_owner -> nx_packet_pool_low_watermark)) { ... }
/* ... */#endif
rx_window = socket_ptr -> nx_tcp_socket_rx_window_current;
/* ... */
outside_of_window = NX_TRUE;
if (packet_data_length == 0)
{
if (rx_window == 0)
{
if (packet_sequence == rx_sequence)
{
outside_of_window = NX_FALSE;
}if (packet_sequence == rx_sequence) { ... }
else if ((tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_RST_BIT) ||
(tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_URG_BIT) ||
((tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_CONTROL_MASK) == NX_TCP_ACK_BIT))
{
/* ... */
outside_of_window = NX_FALSE;
}else if ((tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_RST_BIT) || (tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_URG_BIT) || ((tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_CONTROL_MASK) == NX_TCP_ACK_BIT)) { ... }
}if (rx_window == 0) { ... }
else if (((INT)(packet_sequence - rx_sequence) >= 0) &&
((INT)(rx_sequence + rx_window - packet_sequence) > 0))
{
outside_of_window = NX_FALSE;
}else if (((INT)(packet_sequence - rx_sequence) >= 0) && ((INT)(rx_sequence + rx_window - packet_sequence) > 0)) { ... }
}if (packet_data_length == 0) { ... }
else
{
if ((rx_window > 0) &&
((((INT)(packet_sequence - rx_sequence) >= 0) &&
((INT)(rx_sequence + rx_window - packet_sequence) > 0)) ||
(((INT)(packet_sequence + (packet_data_length - 1) - rx_sequence) >= 0) &&
((INT)(rx_sequence + 1 + (rx_window - packet_sequence) - packet_data_length) > 0))))
{
outside_of_window = NX_FALSE;
}if ((rx_window > 0) && ((((INT)(packet_sequence - rx_sequence) >= 0) && ((INT)(rx_sequence + rx_window - packet_sequence) > 0)) || (((INT)(packet_sequence + (packet_data_length - 1) - rx_sequence) >= 0) && ((INT)(rx_sequence + 1 + (rx_window - packet_sequence) - packet_data_length) > 0)))) { ... }
}else { ... }
if (outside_of_window)
{
/* ... */
if (!(tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_RST_BIT))
{
_nx_tcp_packet_send_ack(socket_ptr, socket_ptr -> nx_tcp_socket_tx_sequence);
}if (!(tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_RST_BIT)) { ... }
#ifndef NX_DISABLE_TCP_INFO
socket_ptr -> nx_tcp_socket_ip_ptr -> nx_ip_tcp_receive_packets_dropped++;/* ... */
#endif
_nx_packet_release(packet_ptr);
return;
}if (outside_of_window) { ... }
if (tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_RST_BIT)
{
#ifndef NX_DISABLE_TCP_INFO
(socket_ptr -> nx_tcp_socket_ip_ptr) -> nx_ip_tcp_resets_received++;/* ... */
#endif
NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_TCP_RESET_RECEIVE, socket_ptr -> nx_tcp_socket_ip_ptr, socket_ptr, packet_ptr, tcp_header_copy.nx_tcp_sequence_number, NX_TRACE_INTERNAL_EVENTS, 0, 0);
_nx_tcp_socket_connection_reset(socket_ptr);
_nx_packet_release(packet_ptr);
return;
}if (tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_RST_BIT) { ... }
if (tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_SYN_BIT)
{
tcp_header_copy.nx_tcp_sequence_number++;
_nx_tcp_packet_send_rst(socket_ptr, &tcp_header_copy);
_nx_tcp_socket_connection_reset(socket_ptr);
_nx_packet_release(packet_ptr);
return;
}if (tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_SYN_BIT) { ... }
if (socket_ptr -> nx_tcp_socket_state != NX_TCP_SYN_RECEIVED)
{
if (_nx_tcp_socket_state_ack_check(socket_ptr, &tcp_header_copy) == NX_FALSE)
{
_nx_packet_release(packet_ptr);
return;
}if (_nx_tcp_socket_state_ack_check(socket_ptr, &tcp_header_copy) == NX_FALSE) { ... }
}if (socket_ptr -> nx_tcp_socket_state != NX_TCP_SYN_RECEIVED) { ... }
}if ((socket_ptr -> nx_tcp_socket_state >= NX_TCP_SYN_RECEIVED) #ifdef NX_ENABLE_TCPIP_OFFLOAD && (!tcpip_offload) #endif /* NX_ENABLE_TCPIP_OFFLOAD */) { ... }
if (header_length > sizeof(NX_TCP_HEADER))
{
if (!_nx_tcp_mss_option_get((packet_ptr -> nx_packet_prepend_ptr + sizeof(NX_TCP_HEADER)),
header_length - (ULONG)sizeof(NX_TCP_HEADER), &mss))
{
/* ... */
/* ... */
if (!(tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_ACK_BIT))
{
tcp_header_copy.nx_tcp_sequence_number += (packet_ptr -> nx_packet_length - header_length);
if ((tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_SYN_BIT) ||
(tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_FIN_BIT))
{
tcp_header_copy.nx_tcp_sequence_number++;
}if ((tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_SYN_BIT) || (tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_FIN_BIT)) { ... }
}if (!(tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_ACK_BIT)) { ... }
_nx_tcp_packet_send_rst(socket_ptr, &tcp_header_copy);
_nx_tcp_socket_connection_reset(socket_ptr);
#ifndef NX_DISABLE_TCP_INFO
socket_ptr -> nx_tcp_socket_ip_ptr -> nx_ip_tcp_invalid_packets++;/* ... */
#endif
_nx_packet_release(packet_ptr);
return;
}if (!_nx_tcp_mss_option_get((packet_ptr -> nx_packet_prepend_ptr + sizeof(NX_TCP_HEADER)), header_length - (ULONG)sizeof(NX_TCP_HEADER), &mss)) { ... }
}if (header_length > sizeof(NX_TCP_HEADER)) { ... }
switch (socket_ptr -> nx_tcp_socket_state)
{
case NX_TCP_SYN_SENT:
/* ... */
_nx_tcp_socket_state_syn_sent(socket_ptr, &tcp_header_copy, packet_ptr);
if (socket_ptr -> nx_tcp_socket_state == NX_TCP_ESTABLISHED)
{
packet_queued = _nx_tcp_socket_state_data_check(socket_ptr, packet_ptr);
}if (socket_ptr -> nx_tcp_socket_state == NX_TCP_ESTABLISHED) { ... }
break;
case NX_TCP_SYN_SENT:
case NX_TCP_SYN_RECEIVED:
/* ... */
_nx_tcp_socket_state_syn_received(socket_ptr, &tcp_header_copy);
if (socket_ptr -> nx_tcp_socket_state == NX_TCP_ESTABLISHED)
{
packet_queued = _nx_tcp_socket_state_data_check(socket_ptr, packet_ptr);
}if (socket_ptr -> nx_tcp_socket_state == NX_TCP_ESTABLISHED) { ... }
break;
case NX_TCP_SYN_RECEIVED:
case NX_TCP_ESTABLISHED:
packet_queued = _nx_tcp_socket_state_data_check(socket_ptr, packet_ptr);
#ifdef NX_ENABLE_TCPIP_OFFLOAD
if (!tcpip_offload)
#endif
{
/* ... */
_nx_tcp_socket_state_established(socket_ptr);
_nx_tcp_socket_state_transmit_check(socket_ptr);
...}
break;
case NX_TCP_ESTABLISHED:
case NX_TCP_CLOSE_WAIT:
_nx_tcp_socket_state_transmit_check(socket_ptr);
break;
case NX_TCP_CLOSE_WAIT:
case NX_TCP_LAST_ACK:
/* ... */
_nx_tcp_socket_state_last_ack(socket_ptr, &tcp_header_copy);
break;
case NX_TCP_LAST_ACK:
case NX_TCP_FIN_WAIT_1:
packet_queued = _nx_tcp_socket_state_data_check(socket_ptr, packet_ptr);
/* ... */
_nx_tcp_socket_state_fin_wait1(socket_ptr);
break;
case NX_TCP_FIN_WAIT_1:
case NX_TCP_FIN_WAIT_2:
packet_queued = _nx_tcp_socket_state_data_check(socket_ptr, packet_ptr);
/* ... */
_nx_tcp_socket_state_fin_wait2(socket_ptr);
break;
case NX_TCP_FIN_WAIT_2:
case NX_TCP_CLOSING:
/* ... */
_nx_tcp_socket_state_closing(socket_ptr, &tcp_header_copy);
break;
case NX_TCP_CLOSING:
case NX_TCP_TIMED_WAIT:
break;
case NX_TCP_TIMED_WAIT:
default:
break;default
}switch (socket_ptr -> nx_tcp_socket_state) { ... }
if (tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_URG_BIT)
{
urgent_callback = socket_ptr -> nx_tcp_urgent_data_callback;
if (urgent_callback)
{
/* ... */
(urgent_callback)(socket_ptr);
}if (urgent_callback) { ... }
}if (tcp_header_copy.nx_tcp_header_word_3 & NX_TCP_URG_BIT) { ... }
if (!packet_queued)
{
_nx_packet_release(packet_ptr);
}if (!packet_queued) { ... }
}{ ... }