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
110
111
112
113
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
141
142
143
145
146
147
148
149
150
151
152
153
164
165
172
173
174
175
182
183
184
185
186
187
188
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
226
227
228
229
230
231
232
233
240
241
242
243
244
245
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
266
267
268
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
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
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_ip.h"
#ifdef FEATURE_NX_IPV6
#include "nx_ipv6.h"
#endif
#include "nx_packet.h"
#include "nx_tcp.h"
#ifdef NX_IPSEC_ENABLE
#include "nx_ipsec.h"
#endif
...
...
VOID _nx_tcp_packet_send_control(NX_TCP_SOCKET *socket_ptr, ULONG control_bits, ULONG tx_sequence,
ULONG ack_number, ULONG option_word_1, ULONG option_word_2, UCHAR *data)
{
NX_IP *ip_ptr;
NX_PACKET *packet_ptr;
NX_TCP_HEADER *tcp_header_ptr;
ULONG checksum;
ULONG data_offset = 0;
ULONG *source_ip = NX_NULL, *dest_ip = NX_NULL;
#if defined(NX_DISABLE_TCP_TX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE)
UINT compute_checksum = 1;
#endif
ULONG header_size;
ULONG window_size;
#ifdef NX_DISABLE_TCP_TX_CHECKSUM
compute_checksum = 0;
#endif
ip_ptr = socket_ptr -> nx_tcp_socket_ip_ptr;
if (control_bits & NX_TCP_SYN_BIT)
{
header_size = NX_TCP_SYN_HEADER;
window_size = socket_ptr -> nx_tcp_socket_rx_window_current;
}if (control_bits & NX_TCP_SYN_BIT) { ... }
else
{
header_size = NX_TCP_HEADER_SIZE;
#ifdef NX_ENABLE_TCP_WINDOW_SCALING
window_size = socket_ptr -> nx_tcp_socket_rx_window_current >> socket_ptr -> nx_tcp_rcv_win_scale_value;
#else
window_size = socket_ptr -> nx_tcp_socket_rx_window_current;
#endif
}else { ... }
#ifdef NX_ENABLE_TCP_WINDOW_SCALING
if (window_size > 0xFFFF)
{
window_size = 0xFFFF;
}if (window_size > 0xFFFF) { ... }
/* ... */#endif
#ifdef NX_IPSEC_ENABLE
data_offset = socket_ptr -> nx_tcp_socket_egress_sa_data_offset;/* ... */
#endif
#ifdef NX_ENABLE_DUAL_PACKET_POOL
if (_nx_packet_allocate(ip_ptr -> nx_ip_auxiliary_packet_pool, &packet_ptr, NX_IP_PACKET + data_offset, NX_NO_WAIT))
{
if (ip_ptr -> nx_ip_auxiliary_packet_pool != ip_ptr -> nx_ip_default_packet_pool)
#endif
{
if (_nx_packet_allocate(ip_ptr -> nx_ip_default_packet_pool,
&packet_ptr, NX_IP_PACKET + data_offset, NX_NO_WAIT) != NX_SUCCESS)
{
return;
}if (_nx_packet_allocate(ip_ptr -> nx_ip_default_packet_pool, &packet_ptr, NX_IP_PACKET + data_offset, NX_NO_WAIT) != NX_SUCCESS) { ... }
...}
#ifdef NX_ENABLE_DUAL_PACKET_POOL
else
{
return;
}else { ... }
}/* ... */
if (_nx_packet_allocate(ip_ptr -> nx_ip_auxiliary_packet_pool, &packet_ptr, NX_IP_PACKET + data_offset, NX_NO_WAIT)) { ... }#endif
if ((UINT)(packet_ptr -> nx_packet_data_end - packet_ptr -> nx_packet_prepend_ptr) < (NX_TCP_SYN_SIZE + 1))
{
_nx_packet_release(packet_ptr);
return;
}if ((UINT)(packet_ptr -> nx_packet_data_end - packet_ptr -> nx_packet_prepend_ptr) < (NX_TCP_SYN_SIZE + 1)) { ... }
packet_ptr -> nx_packet_ip_version = (UCHAR)(socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version);
#ifndef NX_DISABLE_IPV4
if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V4)
{
packet_ptr -> nx_packet_address.nx_packet_interface_ptr = socket_ptr -> nx_tcp_socket_connect_interface;
}if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V4) { ... }
/* ... */#endif
NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);
#ifdef NX_IPSEC_ENABLE
packet_ptr -> nx_packet_ipsec_sa_ptr = socket_ptr -> nx_tcp_socket_egress_sa;
#endif
packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_prepend_ptr + sizeof(NX_TCP_HEADER);
packet_ptr -> nx_packet_length = sizeof(NX_TCP_HEADER);
tcp_header_ptr = (NX_TCP_HEADER *)packet_ptr -> nx_packet_prepend_ptr;
tcp_header_ptr -> nx_tcp_header_word_0 = (((ULONG)(socket_ptr -> nx_tcp_socket_port)) << NX_SHIFT_BY_16) | (ULONG)socket_ptr -> nx_tcp_socket_connect_port;
tcp_header_ptr -> nx_tcp_sequence_number = tx_sequence;
tcp_header_ptr -> nx_tcp_acknowledgment_number = ack_number;
tcp_header_ptr -> nx_tcp_header_word_3 = header_size | control_bits | window_size;
tcp_header_ptr -> nx_tcp_header_word_4 = 0;
socket_ptr -> nx_tcp_socket_rx_sequence_acked = ack_number;
socket_ptr -> nx_tcp_socket_rx_window_last_sent = socket_ptr -> nx_tcp_socket_rx_window_current;
/* ... */
NX_CHANGE_ULONG_ENDIAN(tcp_header_ptr -> nx_tcp_header_word_0);
NX_CHANGE_ULONG_ENDIAN(tcp_header_ptr -> nx_tcp_sequence_number);
NX_CHANGE_ULONG_ENDIAN(tcp_header_ptr -> nx_tcp_acknowledgment_number);
NX_CHANGE_ULONG_ENDIAN(tcp_header_ptr -> nx_tcp_header_word_3);
NX_CHANGE_ULONG_ENDIAN(tcp_header_ptr -> nx_tcp_header_word_4);
if (data)
{
*packet_ptr -> nx_packet_append_ptr++ = *data;
packet_ptr -> nx_packet_length++;
}if (data) { ... }
if (control_bits & NX_TCP_SYN_BIT)
{
/* ... */
NX_CHANGE_ULONG_ENDIAN(option_word_1);
NX_CHANGE_ULONG_ENDIAN(option_word_2);
*((ULONG *)packet_ptr -> nx_packet_append_ptr) = option_word_1;
*(((ULONG *)packet_ptr -> nx_packet_append_ptr) + 1) = option_word_2;
packet_ptr -> nx_packet_append_ptr += (sizeof(ULONG) << 1);
packet_ptr -> nx_packet_length += (ULONG)(sizeof(ULONG) << 1);
}if (control_bits & NX_TCP_SYN_BIT) { ... }
#ifdef NX_ENABLE_INTERFACE_CAPABILITY
if (socket_ptr -> nx_tcp_socket_connect_interface -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_TCP_TX_CHECKSUM)
{
compute_checksum = 0;
}if (socket_ptr -> nx_tcp_socket_connect_interface -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_TCP_TX_CHECKSUM) { ... }
/* ... */#endif
#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;
}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)) { ... }
/* ... */#endif
#if defined(NX_DISABLE_TCP_TX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE)
if (compute_checksum)
#endif
{
#ifndef NX_DISABLE_IPV4
if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V4)
{
source_ip = &socket_ptr -> nx_tcp_socket_connect_interface -> nx_interface_ip_address;
dest_ip = &socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_address.v4;
}if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V4) { ... }
/* ... */#endif
#ifdef FEATURE_NX_IPV6
if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V6)
{
source_ip = socket_ptr -> nx_tcp_socket_ipv6_addr -> nxd_ipv6_address;
dest_ip = socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_address.v6;
}if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V6) { ... }
/* ... */#endif
checksum = _nx_ip_checksum_compute(packet_ptr, NX_PROTOCOL_TCP,
(UINT)packet_ptr -> nx_packet_length, source_ip, dest_ip);
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);
...}
#ifdef NX_ENABLE_INTERFACE_CAPABILITY
else
{
packet_ptr -> nx_packet_interface_capability_flag |= NX_INTERFACE_CAPABILITY_TCP_TX_CHECKSUM;
}else { ... }
/* ... */#endif
#ifndef NX_DISABLE_IPV4
if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V4)
{
_nx_ip_packet_send(ip_ptr, packet_ptr, socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_address.v4,
socket_ptr -> nx_tcp_socket_type_of_service, socket_ptr -> nx_tcp_socket_time_to_live, NX_IP_TCP,
socket_ptr -> nx_tcp_socket_fragment_enable,
socket_ptr -> nx_tcp_socket_next_hop_address);
}if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V4) { ... }
/* ... */#endif
#ifdef FEATURE_NX_IPV6
if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V6)
{
packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr = socket_ptr -> nx_tcp_socket_ipv6_addr;
_nx_ipv6_packet_send(ip_ptr, packet_ptr, NX_PROTOCOL_TCP, packet_ptr -> nx_packet_length, ip_ptr -> nx_ipv6_hop_limit,
socket_ptr -> nx_tcp_socket_ipv6_addr -> nxd_ipv6_address,
socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_address.v6);
}if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V6) { ... }
/* ... */#endif
}{ ... }