Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_tcp.h"
#include "nx_ip.h"
#include "nx_ipv6.h"
#include "nx_ipsec.h"
...
...
_nx_tcp_packet_send_syn(NX_TCP_SOCKET *, ULONG)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduocommon/src/nx_tcp_packet_send_syn.c
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** NetX Component */ /** */ /** Transmission Control Protocol (TCP) */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SOURCE_CODE /* Include necessary system files. */ #include "nx_api.h" #include "nx_tcp.h" #include "nx_ip.h" #ifdef FEATURE_NX_IPV6 #include "nx_ipv6.h" #endif /* FEATURE_NX_IPV6 */ #ifdef NX_IPSEC_ENABLE #include "nx_ipsec.h" #endif /* NX_IPSEC_ENABLE */ ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_tcp_packet_send_syn PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Yuxin Zhou, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function sends a SYN from the specified socket. */ /* */ /* INPUT */ /* */ /* socket_ptr Pointer to socket */ /* tx_sequence Transmit sequence number */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* _nx_tcp_packet_send_control Send TCP control packet */ /* _nx_packet_egress_sa_lookup IPsec process */ /* */ /* CALLED BY */ /* */ /* _nx_tcp_client_socket_connect Client connect processing */ /* _nx_tcp_periodic_processing Connection retry processing */ /* _nx_tcp_packet_process Server connect response */ /* processing */ /* _nx_tcp_server_socket_accept Server socket accept */ /* processing */ /* _nx_tcp_socket_state_syn_sent Socket SYN sent processing */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */ /* 09-30-2020 Yuxin Zhou Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ VOID _nx_tcp_packet_send_syn(NX_TCP_SOCKET *socket_ptr, ULONG tx_sequence) { #ifdef NX_IPSEC_ENABLE ULONG data_offset = 0; NXD_ADDRESS src_addr; UINT ret; NX_IPSEC_SA *cur_sa_ptr = NX_NULL;/* ... */ #endif /* NX_IPSEC_ENABLE */ ULONG option_word_1; ULONG option_word_2; #ifdef NX_ENABLE_TCP_WINDOW_SCALING UINT include_window_scaling = NX_FALSE; UINT scale_factor;/* ... */ #endif /* NX_ENABLE_TCP_WINDOW_SCALING */ ULONG mss = 0; #ifdef NX_IPSEC_ENABLE #ifndef NX_DISABLE_IPV4 /* Look for egress SA first. */ if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V4) { src_addr.nxd_ip_version = NX_IP_VERSION_V4; src_addr.nxd_ip_address.v4 = socket_ptr -> nx_tcp_socket_connect_interface -> nx_interface_ip_address; }if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V4) { ... } /* ... */#endif /* !NX_DISABLE_IPV4 */ #ifdef FEATURE_NX_IPV6 if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V6) { /* IPv6 case. */ src_addr.nxd_ip_version = NX_IP_VERSION_V6; COPY_IPV6_ADDRESS(socket_ptr -> nx_tcp_socket_ipv6_addr -> nxd_ipv6_address, src_addr.nxd_ip_address.v6); }if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V6) { ... } /* ... */#endif /* FEATURE_NX_IPV6 */ /* Check for possible SA match. */ if (socket_ptr -> nx_tcp_socket_ip_ptr -> nx_ip_packet_egress_sa_lookup != NX_NULL) /* IPsec is enabled. */ { /* If the SA has not been set. */ ret = socket_ptr -> nx_tcp_socket_ip_ptr -> nx_ip_packet_egress_sa_lookup(socket_ptr -> nx_tcp_socket_ip_ptr, /* IP ptr */ &src_addr, /* src_addr */ &socket_ptr -> nx_tcp_socket_connect_ip, /* dest_addr */ NX_PROTOCOL_TCP, /* protocol */ socket_ptr -> nx_tcp_socket_port, /* src_port */ socket_ptr -> nx_tcp_socket_connect_port, /* dest_port */ &data_offset, (VOID *)&cur_sa_ptr, 0); if (ret == NX_IPSEC_TRAFFIC_PROTECT) { /* Save the SA to the socket. */ socket_ptr -> nx_tcp_socket_egress_sa = cur_sa_ptr; socket_ptr -> nx_tcp_socket_egress_sa_data_offset = data_offset; }if (ret == NX_IPSEC_TRAFFIC_PROTECT) { ... } else if (ret == NX_IPSEC_TRAFFIC_DROP || ret == NX_IPSEC_TRAFFIC_PENDING_IKEV2) { return; }else if (ret == NX_IPSEC_TRAFFIC_DROP || ret == NX_IPSEC_TRAFFIC_PENDING_IKEV2) { ... } else { /* Zero out SA information. */ socket_ptr -> nx_tcp_socket_egress_sa = NX_NULL; socket_ptr -> nx_tcp_socket_egress_sa_data_offset = 0; }else { ... } ...} else { socket_ptr -> nx_tcp_socket_egress_sa = NX_NULL; }else { ... } /* ... */#endif /* NX_IPSEC_ENABLE */ #ifndef NX_DISABLE_IPV4 /* Update the mss value based on IP version type. */ if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V4) { mss = (ULONG)((socket_ptr -> nx_tcp_socket_connect_interface -> nx_interface_ip_mtu_size - sizeof(NX_IPV4_HEADER)) - sizeof(NX_TCP_HEADER)); #ifdef NX_IPSEC_ENABLE if (cur_sa_ptr != NX_NULL) { /* Update the mss value based on sa mode. */ if (cur_sa_ptr -> nx_ipsec_sa_protocol == NX_PROTOCOL_NEXT_HEADER_ENCAP_SECURITY) { /* Update the mss value. minus the ESP HEADER's pad length and IV size , */ mss = mss - sizeof(NX_IPSEC_ESP_HEADER) - (cur_sa_ptr -> nx_ipsec_sa_encryption_method -> nx_crypto_block_size_in_bytes) - ((cur_sa_ptr -> nx_ipsec_sa_encryption_method -> nx_crypto_IV_size_in_bits) >> 3) - ((cur_sa_ptr -> nx_ipsec_sa_integrity_method -> nx_crypto_ICV_size_in_bits) >> 3); }if (cur_sa_ptr -> nx_ipsec_sa_protocol == NX_PROTOCOL_NEXT_HEADER_ENCAP_SECURITY) { ... } if (cur_sa_ptr -> nx_ipsec_sa_protocol == NX_PROTOCOL_NEXT_HEADER_AUTHENTICATION) { /* Update the mss value. minus the ESP HEADER's IV size and ICV size. */ mss = mss - sizeof(NX_IPSEC_AUTHENTICATION_HEADER) - (cur_sa_ptr -> nx_ipsec_sa_integrity_method -> nx_crypto_IV_size_in_bits >> 3) - (cur_sa_ptr -> nx_ipsec_sa_integrity_method -> nx_crypto_ICV_size_in_bits >> 3); }if (cur_sa_ptr -> nx_ipsec_sa_protocol == NX_PROTOCOL_NEXT_HEADER_AUTHENTICATION) { ... } /* If the sa is tunnel mode, the mss value should minus the IPV4 header size . */ if (cur_sa_ptr -> nx_ipsec_sa_mode == NX_IPSEC_TUNNEL_MODE) { mss -= (sizeof(NX_IPV4_HEADER)); }if (cur_sa_ptr -> nx_ipsec_sa_mode == NX_IPSEC_TUNNEL_MODE) { ... } }if (cur_sa_ptr != NX_NULL) { ... } /* ... */#endif /* NX_IPSEC_ENABLE */ }if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V4) { ... } /* ... */#endif /* !NX_DISABLE_IPV4 */ #ifdef FEATURE_NX_IPV6 if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V6) { mss = (ULONG)((socket_ptr -> nx_tcp_socket_connect_interface -> nx_interface_ip_mtu_size - sizeof(NX_IPV6_HEADER)) - sizeof(NX_TCP_HEADER)); #ifdef NX_IPSEC_ENABLE if (cur_sa_ptr != NX_NULL) { /* Update the mss value based on sa mode. */ if (cur_sa_ptr -> nx_ipsec_sa_protocol == NX_PROTOCOL_NEXT_HEADER_ENCAP_SECURITY) { /* Update the mss value. minus the ESP header's pad length and IV size , */ mss = mss - sizeof(NX_IPSEC_ESP_HEADER) - (cur_sa_ptr -> nx_ipsec_sa_encryption_method -> nx_crypto_block_size_in_bytes) - ((cur_sa_ptr -> nx_ipsec_sa_encryption_method -> nx_crypto_IV_size_in_bits) >> 3) - ((cur_sa_ptr -> nx_ipsec_sa_integrity_method -> nx_crypto_ICV_size_in_bits) >> 3); }if (cur_sa_ptr -> nx_ipsec_sa_protocol == NX_PROTOCOL_NEXT_HEADER_ENCAP_SECURITY) { ... } if (cur_sa_ptr -> nx_ipsec_sa_protocol == NX_PROTOCOL_NEXT_HEADER_AUTHENTICATION) { /* Update the mss value. minus the ESP HEADER's IV size and ICV size. */ mss = mss - sizeof(NX_IPSEC_AUTHENTICATION_HEADER) - (cur_sa_ptr -> nx_ipsec_sa_integrity_method -> nx_crypto_IV_size_in_bits >> 3) - (cur_sa_ptr -> nx_ipsec_sa_integrity_method -> nx_crypto_ICV_size_in_bits >> 3); }if (cur_sa_ptr -> nx_ipsec_sa_protocol == NX_PROTOCOL_NEXT_HEADER_AUTHENTICATION) { ... } /* If the sa mode is tunnel mode,the mss value should minus the IPV6 header size . */ if (cur_sa_ptr -> nx_ipsec_sa_mode == NX_IPSEC_TUNNEL_MODE) { mss -= (sizeof(NX_IPV6_HEADER)); }if (cur_sa_ptr -> nx_ipsec_sa_mode == NX_IPSEC_TUNNEL_MODE) { ... } }if (cur_sa_ptr != NX_NULL) { ... } /* ... */#endif /* NX_IPSEC_ENABLE */ }if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V6) { ... } /* ... */#endif /* FEATURE_NX_IPV6 */ mss &= 0x0000FFFFUL; if ((socket_ptr -> nx_tcp_socket_mss < mss) && socket_ptr -> nx_tcp_socket_mss) { /* Use the custom MSS. */ mss = socket_ptr -> nx_tcp_socket_mss; }if ((socket_ptr -> nx_tcp_socket_mss < mss) && socket_ptr -> nx_tcp_socket_mss) { ... } if (socket_ptr -> nx_tcp_socket_state == NX_TCP_SYN_RECEIVED) { /* Update the connect MSS for TCP server socket. */ if (mss < socket_ptr -> nx_tcp_socket_peer_mss) { socket_ptr -> nx_tcp_socket_connect_mss = mss; }if (mss < socket_ptr -> nx_tcp_socket_peer_mss) { ... } else { socket_ptr -> nx_tcp_socket_connect_mss = socket_ptr -> nx_tcp_socket_peer_mss; }else { ... } /* Compute the SMSS * SMSS value, so later TCP module doesn't need to redo the multiplication. */ socket_ptr -> nx_tcp_socket_connect_mss2 = socket_ptr -> nx_tcp_socket_connect_mss * socket_ptr -> nx_tcp_socket_connect_mss; }if (socket_ptr -> nx_tcp_socket_state == NX_TCP_SYN_RECEIVED) { ... } else { /* Set the MSS. */ socket_ptr -> nx_tcp_socket_connect_mss = mss; }else { ... } /* Build the MSS option. */ option_word_1 = NX_TCP_MSS_OPTION | mss; /* Set default option word2. */ option_word_2 = NX_TCP_OPTION_END; #ifdef NX_ENABLE_TCP_WINDOW_SCALING /* Include window scaling option if we initiates the SYN, or the peer supports Window Scaling. */ if (socket_ptr -> nx_tcp_socket_state == NX_TCP_SYN_SENT) { include_window_scaling = NX_TRUE; }if (socket_ptr -> nx_tcp_socket_state == NX_TCP_SYN_SENT) { ... } else if (socket_ptr -> nx_tcp_snd_win_scale_value != 0xFF) { include_window_scaling = NX_TRUE; }else if (socket_ptr -> nx_tcp_snd_win_scale_value != 0xFF) { ... } if (include_window_scaling) { /* Sets the window scaling option. */ option_word_2 = NX_TCP_RWIN_OPTION; /* Compute the window scaling factor */ for (scale_factor = 0; scale_factor < 15; scale_factor++) { if ((socket_ptr -> nx_tcp_socket_rx_window_current >> scale_factor) < 65536) { break; }if ((socket_ptr -> nx_tcp_socket_rx_window_current >> scale_factor) < 65536) { ... } }for (scale_factor = 0; scale_factor < 15; scale_factor++) { ... } /* Make sure window scale is limited to 14, per RFC 1323 pp.11. */ if (scale_factor == 15) { scale_factor = 14; socket_ptr -> nx_tcp_socket_rx_window_default = (1 << 30) - 1; socket_ptr -> nx_tcp_socket_rx_window_current = (1 << 30) - 1; }if (scale_factor == 15) { ... } option_word_2 |= scale_factor << 8; /* Update the socket with the scale factor. */ socket_ptr -> nx_tcp_rcv_win_scale_value = scale_factor; }if (include_window_scaling) { ... } /* ... */#endif /* NX_ENABLE_TCP_WINDOW_SCALING */ /* Send SYN or SYN+ACK packet according to socket state. */ if (socket_ptr -> nx_tcp_socket_state == NX_TCP_SYN_SENT) { _nx_tcp_packet_send_control(socket_ptr, NX_TCP_SYN_BIT, tx_sequence, 0, option_word_1, option_word_2, NX_NULL); }if (socket_ptr -> nx_tcp_socket_state == NX_TCP_SYN_SENT) { ... } else { _nx_tcp_packet_send_control(socket_ptr, (NX_TCP_SYN_BIT | NX_TCP_ACK_BIT), tx_sequence, socket_ptr -> nx_tcp_socket_rx_sequence, option_word_1, option_word_2, NX_NULL); }else { ... } /* Initialize recover sequence and previous cumulative acknowledgment. */ socket_ptr -> nx_tcp_socket_tx_sequence_recover = tx_sequence; socket_ptr -> nx_tcp_socket_previous_highest_ack = tx_sequence; }{ ... }