1
10
13
14
20
21
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
108
109
110
111
112
113
114
118
119
120
121
122
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
178
179
180
181
182
183
184
185
186
187
188
192
193
194
195
196
197
198
199
200
201
202
203
207
211
212
213
214
218
219
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
268
269
270
271
272
273
274
278
279
280
281
282
283
284
285
287
288
289
290
291
292
293
294
298
299
300
301
302
309
310
311
315
325
326
327
328
329
330
336
337
338
345
346
347
348
349
350
351
352
353
356
357
358
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_dtls.h"
#include "nx_udp.h"
...
...
VOID _nx_secure_dtls_receive_callback(NX_UDP_SOCKET *socket_ptr)
{
#ifdef NX_SECURE_ENABLE_DTLS
TX_INTERRUPT_SAVE_AREA
NXD_ADDRESS ip_address;
UINT remote_port;
UINT local_port;
UINT status;
ULONG packet_type;
NX_PACKET *packet_ptr;
UCHAR *send_data;
NX_PACKET *temp_packet_ptr;
NX_SECURE_DTLS_SERVER *dtls_server;
NX_SECURE_DTLS_SESSION *dtls_session;
if(socket_ptr -> nx_udp_socket_receive_head == NX_NULL)
{
return;
}if (socket_ptr -> nx_udp_socket_receive_head == NX_NULL) { ... }
socket_ptr -> nx_udp_socket_receive_head -> nx_packet_prepend_ptr += sizeof(NX_UDP_HEADER);
status = _nxd_udp_source_extract(socket_ptr->nx_udp_socket_receive_head, &ip_address, &remote_port);
socket_ptr -> nx_udp_socket_receive_head -> nx_packet_prepend_ptr -= sizeof(NX_UDP_HEADER);
if(status != NX_SUCCESS)
{
return;
}if (status != NX_SUCCESS) { ... }
status = _nx_udp_socket_port_get(socket_ptr, &local_port);
if(status != NX_SUCCESS)
{
return;
}if (status != NX_SUCCESS) { ... }
dtls_server = (NX_SECURE_DTLS_SERVER*)(socket_ptr -> nx_udp_socket_reserved_ptr);
status = nx_secure_dtls_session_cache_find(dtls_server, &dtls_session, &ip_address, remote_port, local_port);
if(status == NX_SECURE_DTLS_SESSION_NOT_FOUND)
{
if ((socket_ptr -> nx_udp_socket_receive_head -> nx_packet_length < NX_SECURE_DTLS_RECORD_HEADER_SIZE) ||
(socket_ptr -> nx_udp_socket_receive_head -> nx_packet_prepend_ptr[8] == NX_SECURE_TLS_ALERT))
{
TX_DISABLE
packet_ptr = socket_ptr -> nx_udp_socket_receive_head;
socket_ptr -> nx_udp_socket_receive_head = packet_ptr -> nx_packet_queue_next;
packet_ptr -> nx_packet_queue_next = NX_NULL;
if (socket_ptr -> nx_udp_socket_receive_head == NX_NULL)
{
socket_ptr -> nx_udp_socket_receive_tail = NX_NULL;
}if (socket_ptr -> nx_udp_socket_receive_head == NX_NULL) { ... }
socket_ptr -> nx_udp_socket_receive_count--;
TX_RESTORE
nx_secure_tls_packet_release(packet_ptr);
return;
}if ((socket_ptr -> nx_udp_socket_receive_head -> nx_packet_length < NX_SECURE_DTLS_RECORD_HEADER_SIZE) || (socket_ptr -> nx_udp_socket_receive_head -> nx_packet_prepend_ptr[8] == NX_SECURE_TLS_ALERT)) { ... }
status = nx_secure_dtls_session_cache_get_new(dtls_server, &dtls_session, &ip_address, remote_port, local_port);
if(status == NX_SECURE_TLS_NO_FREE_DTLS_SESSIONS)
{
/* ... */
TX_DISABLE
packet_ptr = socket_ptr -> nx_udp_socket_receive_head;
socket_ptr -> nx_udp_socket_receive_head = packet_ptr -> nx_packet_queue_next;
packet_ptr -> nx_packet_queue_next = NX_NULL;
if (socket_ptr -> nx_udp_socket_receive_head == NX_NULL)
{
socket_ptr -> nx_udp_socket_receive_tail = NX_NULL;
}if (socket_ptr -> nx_udp_socket_receive_head == NX_NULL) { ... }
socket_ptr -> nx_udp_socket_receive_count--;
TX_RESTORE
nx_secure_tls_packet_release(packet_ptr);
if (ip_address.nxd_ip_version == NX_IP_VERSION_V4)
{
packet_type = NX_IPv4_UDP_PACKET;
}if (ip_address.nxd_ip_version == NX_IP_VERSION_V4) { ... }
else
{
packet_type = NX_IPv6_UDP_PACKET;
}else { ... }
status = nx_packet_allocate(socket_ptr->nx_udp_socket_ip_ptr->nx_ip_default_packet_pool,
&packet_ptr, packet_type, NX_NO_WAIT);
if(status != NX_SUCCESS)
{
return;
}if (status != NX_SUCCESS) { ... }
if (((ULONG)(packet_ptr -> nx_packet_data_end) - (ULONG)(packet_ptr -> nx_packet_append_ptr)) < 15)
{
nx_secure_tls_packet_release(packet_ptr);
return;
}if (((ULONG)(packet_ptr -> nx_packet_data_end) - (ULONG)(packet_ptr -> nx_packet_append_ptr)) < 15) { ... }
send_data = packet_ptr -> nx_packet_append_ptr;
send_data[0] = NX_SECURE_TLS_ALERT;
send_data[1] = (UCHAR)(NX_SECURE_DTLS_VERSION_MAJOR);
send_data[2] = (UCHAR)(NX_SECURE_DTLS_VERSION_MINOR_1_2);
send_data[3] = 0;
send_data[4] = 0;
send_data[5] = 0;
send_data[6] = 0;
send_data[7] = 0;
send_data[8] = 0;
send_data[9] = 0;
send_data[10] = 0;
send_data[11] = 0;
send_data[12] = 2;
send_data[13] = (UCHAR)(NX_SECURE_TLS_ALERT_LEVEL_FATAL);
send_data[14] = (UCHAR)(NX_SECURE_TLS_ALERT_INTERNAL_ERROR);
packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_append_ptr + 15;
packet_ptr -> nx_packet_length = 15;
status = _nxd_udp_socket_send(socket_ptr, packet_ptr, &ip_address, remote_port);
if(dtls_server->nx_secure_dtls_error_notify != NX_NULL)
{
dtls_server->nx_secure_dtls_error_notify(NX_NULL, NX_SECURE_TLS_NO_FREE_DTLS_SESSIONS);
}if (dtls_server->nx_secure_dtls_error_notify != NX_NULL) { ... }
return;
}if (status == NX_SECURE_TLS_NO_FREE_DTLS_SESSIONS) { ... }
status = nx_udp_socket_receive(socket_ptr, &packet_ptr, NX_NO_WAIT);
if (status)
{
return;
}if (status) { ... }
dtls_session -> nx_secure_dtls_receive_queue_head = packet_ptr;
packet_ptr -> nx_packet_queue_next = NX_NULL;
/* ... */
dtls_server -> nx_secure_dtls_connect_notify(dtls_session, &ip_address, remote_port);
}if (status == NX_SECURE_DTLS_SESSION_NOT_FOUND) { ... }
else
{
status = nx_udp_socket_receive(socket_ptr, &packet_ptr, NX_NO_WAIT);
if (status)
{
return;
}if (status) { ... }
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
if (!dtls_session -> nx_secure_dtls_session_in_use)
{
tx_mutex_put(&_nx_secure_tls_protection);
return;
}if (!dtls_session -> nx_secure_dtls_session_in_use) { ... }
if (dtls_session -> nx_secure_dtls_receive_queue_head == NX_NULL)
{
dtls_session -> nx_secure_dtls_receive_queue_head = packet_ptr;
}if (dtls_session -> nx_secure_dtls_receive_queue_head == NX_NULL) { ... }
else
{
temp_packet_ptr = dtls_session -> nx_secure_dtls_receive_queue_head;
while(temp_packet_ptr -> nx_packet_queue_next != NX_NULL)
{
temp_packet_ptr = temp_packet_ptr -> nx_packet_queue_next;
}while (temp_packet_ptr -> nx_packet_queue_next != NX_NULL) { ... }
temp_packet_ptr -> nx_packet_queue_next = packet_ptr;
}else { ... }
packet_ptr -> nx_packet_queue_next = NX_NULL;
if (dtls_session -> nx_secure_dtls_thread_suspended)
{
tx_thread_wait_abort(dtls_session -> nx_secure_dtls_thread_suspended);
dtls_session -> nx_secure_dtls_thread_suspended = NX_NULL;
}if (dtls_session -> nx_secure_dtls_thread_suspended) { ... }
if (dtls_session -> nx_secure_dtls_tls_session.nx_secure_tls_server_state < NX_SECURE_TLS_SERVER_STATE_HANDSHAKE_FINISHED)
{
tx_mutex_put(&_nx_secure_tls_protection);
return;
}if (dtls_session -> nx_secure_dtls_tls_session.nx_secure_tls_server_state < NX_SECURE_TLS_SERVER_STATE_HANDSHAKE_FINISHED) { ... }
tx_mutex_put(&_nx_secure_tls_protection);
dtls_server -> nx_secure_dtls_receive_notify(dtls_session);
}else { ... }
/* ... */#else
NX_PARAMETER_NOT_USED(socket_ptr);
return;/* ... */
#endif
}{ ... }