1
10
13
14
20
21
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
113
114
115
116
117
118
119
120
121
122
123
124
125
127
128
129
131
132
133
134
135
136
137
138
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
167
168
169
170
171
172
173
174
175
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
218
219
220
221
222
225
231
257
258
259
260
261
262
263
264
265
266
267
268
269
285
286
289
295
296
297
298
299
300
301
302
304
305
306
307
308
309
310
311
312
313
314
315
321
322
326
327
328
329
330
331
332
333
334
335
336
337
338
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
397
398
399
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_dtls.h"
...
...
UINT _nx_secure_dtls_session_receive(NX_SECURE_DTLS_SESSION *dtls_session,
NX_PACKET **packet_ptr_ptr, ULONG wait_option)
{
#ifdef NX_SECURE_ENABLE_DTLS
UINT status;
NX_PACKET *packet_ptr = NX_NULL;
NX_PACKET *send_packet = NX_NULL;
ULONG bytes_processed;
#if 0
UINT remote_port;
#endif
ULONG packet_length;
UINT error_number;
UINT alert_number;
UINT alert_level;
NX_SECURE_TLS_SESSION *tls_session;
NX_UDP_SOCKET *udp_socket;
UINT old_threshold;
NXD_ADDRESS source_address;
UINT source_port;
/* ... */
tls_session = &(dtls_session -> nx_secure_dtls_tls_session);
status = NX_CONTINUE;
while (status == NX_CONTINUE)
{
#ifndef NX_SECURE_TLS_SERVER_DISABLED
/* ... */
if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER)
{
/* ... */
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
if (dtls_session -> nx_secure_dtls_receive_queue_head == NX_NULL)
{
if (wait_option == NX_NO_WAIT)
{
tx_mutex_put(&_nx_secure_tls_protection);
return(NX_NO_PACKET);
}if (wait_option == NX_NO_WAIT) { ... }
tx_thread_preemption_change(tx_thread_identify(), 0, &old_threshold);
if (dtls_session -> nx_secure_dtls_thread_suspended)
{
tx_thread_preemption_change(tx_thread_identify(), old_threshold, &old_threshold);
tx_mutex_put(&_nx_secure_tls_protection);
return(NX_ALREADY_SUSPENDED);
}if (dtls_session -> nx_secure_dtls_thread_suspended) { ... }
dtls_session -> nx_secure_dtls_thread_suspended = tx_thread_identify();
tx_mutex_put(&_nx_secure_tls_protection);
/* ... */
tx_thread_sleep(wait_option);
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
tx_thread_preemption_change(tx_thread_identify(), old_threshold, &old_threshold);
if (!dtls_session -> nx_secure_dtls_session_in_use)
{
tx_mutex_put(&_nx_secure_tls_protection);
return(NX_SECURE_TLS_SESSION_UNINITIALIZED);
}if (!dtls_session -> nx_secure_dtls_session_in_use) { ... }
if ((volatile NX_PACKET *)dtls_session -> nx_secure_dtls_receive_queue_head == NX_NULL)
{
dtls_session -> nx_secure_dtls_thread_suspended = NX_NULL;
tx_mutex_put(&_nx_secure_tls_protection);
return(NX_NO_PACKET);
}if ((volatile NX_PACKET *)dtls_session -> nx_secure_dtls_receive_queue_head == NX_NULL) { ... }
}if (dtls_session -> nx_secure_dtls_receive_queue_head == NX_NULL) { ... }
packet_ptr = dtls_session -> nx_secure_dtls_receive_queue_head;
dtls_session -> nx_secure_dtls_receive_queue_head = packet_ptr -> nx_packet_queue_next;
packet_ptr -> nx_packet_queue_next = NX_NULL;
tx_mutex_put(&_nx_secure_tls_protection);
status = NX_SUCCESS;
}if (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER) { ... }
else
#endif
{
udp_socket = dtls_session -> nx_secure_dtls_udp_socket;
status = nx_udp_socket_receive(udp_socket, &packet_ptr, wait_option);
if (status)
{
return(status);
}if (status) { ... }
status = nxd_udp_source_extract(packet_ptr, &source_address, &source_port);
/* ... */
if ((status) ||
(source_port != dtls_session -> nx_secure_dtls_remote_port) ||
(source_address.nxd_ip_version != dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_version))
{
status = NX_CONTINUE;
}if ((status) || (source_port != dtls_session -> nx_secure_dtls_remote_port) || (source_address.nxd_ip_version != dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_version)) { ... }
else
{
if (dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_version == NX_IP_VERSION_V4)
{
if (dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_address.v4 != source_address.nxd_ip_address.v4)
{
status = NX_CONTINUE;
}if (dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_address.v4 != source_address.nxd_ip_address.v4) { ... }
}if (dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_version == NX_IP_VERSION_V4) { ... }
#ifdef FEATURE_NX_IPV6
else if (dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_version == NX_IP_VERSION_V6)
{
if ((dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_address.v6[0] != source_address.nxd_ip_address.v6[0]) ||
(dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_address.v6[1] != source_address.nxd_ip_address.v6[1]) ||
(dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_address.v6[2] != source_address.nxd_ip_address.v6[2]) ||
(dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_address.v6[3] != source_address.nxd_ip_address.v6[3]))
{
status = NX_CONTINUE;
}if ((dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_address.v6[0] != source_address.nxd_ip_address.v6[0]) || (dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_address.v6[1] != source_address.nxd_ip_address.v6[1]) || (dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_address.v6[2] != source_address.nxd_ip_address.v6[2]) || (dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_address.v6[3] != source_address.nxd_ip_address.v6[3])) { ... }
}else if (dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_version == NX_IP_VERSION_V6) { ... }
/* ... */#endif
}else { ... }
if (status)
{
nx_secure_tls_packet_release(packet_ptr);
status = NX_CONTINUE;
continue;
}if (status) { ... }
}else { ... }
if (dtls_session -> nx_secure_dtls_local_ip_address_index == 0xffffffff)
{
if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V4)
{
dtls_session -> nx_secure_dtls_local_ip_address_index = packet_ptr -> nx_packet_address.nx_packet_interface_ptr -> nx_interface_index;
}if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V4) { ... }
#ifdef FEATURE_NX_IPV6
else if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V6)
{
if (packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr)
{
dtls_session -> nx_secure_dtls_local_ip_address_index = packet_ptr->nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address_index;
}if (packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr) { ... }
}else if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V6) { ... }
/* ... */#endif
}if (dtls_session -> nx_secure_dtls_local_ip_address_index == 0xffffffff) { ... }
/* ... */
if (packet_ptr -> nx_packet_next)
{
status = NX_SECURE_TLS_INVALID_PACKET;
}if (packet_ptr -> nx_packet_next) { ... }
else
{
packet_length = packet_ptr -> nx_packet_length;
bytes_processed = 0;
while (packet_length > 0)
{
/* ... */
packet_ptr -> nx_packet_prepend_ptr += bytes_processed;
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
status = _nx_secure_dtls_process_record(dtls_session, packet_ptr, 0, &bytes_processed, wait_option);
tx_mutex_put(&_nx_secure_tls_protection);
if (status && (status != NX_CONTINUE))
{
break;
}if (status && (status != NX_CONTINUE)) { ... }
if (bytes_processed > packet_length)
{
break;
}if (bytes_processed > packet_length) { ... }
packet_length -= bytes_processed;
}while (packet_length > 0) { ... }
}else { ... }
if (status != NX_SUCCESS)
{
nx_secure_tls_packet_release(packet_ptr);
if (status == NX_SECURE_TLS_ALERT_RECEIVED)
{
if(tls_session -> nx_secure_tls_received_alert_level == NX_SECURE_TLS_ALERT_LEVEL_WARNING &&
tls_session -> nx_secure_tls_received_alert_value == NX_SECURE_TLS_ALERT_CLOSE_NOTIFY)
{
status = NX_SECURE_TLS_CLOSE_NOTIFY_RECEIVED;
}if (tls_session -> nx_secure_tls_received_alert_level == NX_SECURE_TLS_ALERT_LEVEL_WARNING && tls_session -> nx_secure_tls_received_alert_value == NX_SECURE_TLS_ALERT_CLOSE_NOTIFY) { ... }
}if (status == NX_SECURE_TLS_ALERT_RECEIVED) { ... }
else if (status != NX_CONTINUE)
{
error_number = status;
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
_nx_secure_tls_map_error_to_alert(error_number, &alert_number, &alert_level);
tx_mutex_put(&_nx_secure_tls_protection);
status = _nx_secure_dtls_packet_allocate(dtls_session,
tls_session -> nx_secure_tls_packet_pool,
&send_packet, wait_option);
if (status == NX_SUCCESS)
{
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
_nx_secure_tls_send_alert(tls_session, send_packet, (UCHAR)alert_number, (UCHAR)alert_level);
_nx_secure_dtls_send_record(dtls_session, send_packet, NX_SECURE_TLS_ALERT, wait_option);
tx_mutex_put(&_nx_secure_tls_protection);
}if (status == NX_SUCCESS) { ... }
status = error_number;
return(status);
}else if (status != NX_CONTINUE) { ... }
}if (status != NX_SUCCESS) { ... }
}while (status == NX_CONTINUE) { ... }
*packet_ptr_ptr = packet_ptr;
return(status);/* ... */
#else
NX_PARAMETER_NOT_USED(dtls_session);
NX_PARAMETER_NOT_USED(packet_ptr_ptr);
NX_PARAMETER_NOT_USED(wait_option);
return(NX_NOT_SUPPORTED);/* ... */
#endif
}{ ... }