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
110
111
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
181
182
183
184
185
186
187
188
192
197
198
199
200
201
202
203
204
205
206
207
208
212
213
214
215
216
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
312
313
314
315
316
317
318
319
320
321
322
323
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
358
359
360
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
391
392
393
394
395
396
397
398
399
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
422
423
424
425
426
427
428
429
431
432
433
434
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
478
482
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
529
530
531
532
538
539
540
541
542
543
548
549
550
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
581
582
583
584
585
586
587
588
589
595
596
597
598
599
600
601
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_udp.h"
#include "nx_ip.h"
#include "nx_packet.h"
#ifdef FEATURE_NX_IPV6
#include "nx_ipv6.h"
#endif
#ifdef NX_IPSEC_ENABLE
#include "nx_ipsec.h"
#endif
#ifdef NX_ENABLE_TCPIP_OFFLOAD...
...
static UINT _nx_udp_socket_driver_send(NX_UDP_SOCKET *socket_ptr,
NX_PACKET *packet_ptr,
NXD_ADDRESS *ip_src_address,
NXD_ADDRESS *ip_dst_address,
UINT port)
{
UINT status;
NX_IP *ip_ptr;
NX_INTERFACE *interface_ptr = NX_NULL;
UCHAR *original_ptr = packet_ptr -> nx_packet_prepend_ptr + sizeof(NX_UDP_HEADER);
ULONG original_length = packet_ptr -> nx_packet_length - sizeof(NX_UDP_HEADER);
UINT packet_reset = NX_FALSE;
ip_ptr = socket_ptr -> nx_udp_socket_ip_ptr;
#ifndef NX_DISABLE_IPV4
if (ip_dst_address -> nxd_ip_version == NX_IP_VERSION_V4)
{
interface_ptr = packet_ptr -> nx_packet_address.nx_packet_interface_ptr;
}if (ip_dst_address -> nxd_ip_version == NX_IP_VERSION_V4) { ... }
/* ... */#endif
#ifdef FEATURE_NX_IPV6
if (ip_dst_address -> nxd_ip_version == NX_IP_VERSION_V6)
{
interface_ptr = packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address_attached;
}if (ip_dst_address -> nxd_ip_version == NX_IP_VERSION_V6) { ... }
/* ... */#endif
#ifdef NX_ENABLE_IP_PACKET_FILTER
if (ip_ptr -> nx_ip_packet_filter || ip_ptr -> nx_ip_packet_filter_extended)
{
#ifndef NX_DISABLE_IPV4
if (ip_dst_address -> nxd_ip_version == NX_IP_VERSION_V4)
{
_nx_ip_header_add(ip_ptr, packet_ptr,
ip_src_address -> nxd_ip_address.v4,
ip_dst_address -> nxd_ip_address.v4,
socket_ptr -> nx_udp_socket_type_of_service,
socket_ptr -> nx_udp_socket_time_to_live,
NX_IP_UDP, socket_ptr -> nx_udp_socket_fragment_enable);
}if (ip_dst_address -> nxd_ip_version == NX_IP_VERSION_V4) { ... }
/* ... */#endif
#ifdef FEATURE_NX_IPV6
if (ip_dst_address -> nxd_ip_version == NX_IP_VERSION_V6)
{
if (_nx_ipv6_header_add(ip_ptr, &packet_ptr, NX_PROTOCOL_UDP,
packet_ptr -> nx_packet_length,
ip_ptr -> nx_ipv6_hop_limit,
ip_src_address -> nxd_ip_address.v6,
ip_dst_address -> nxd_ip_address.v6, NX_NULL))
{
tx_mutex_put(&(ip_ptr -> nx_ip_protection));
return(NX_SUCCESS);
}if (_nx_ipv6_header_add(ip_ptr, &packet_ptr, NX_PROTOCOL_UDP, packet_ptr -> nx_packet_length, ip_ptr -> nx_ipv6_hop_limit, ip_src_address -> nxd_ip_address.v6, ip_dst_address -> nxd_ip_address.v6, NX_NULL)) { ... }
packet_ptr -> nx_packet_prepend_ptr = packet_ptr -> nx_packet_prepend_ptr + sizeof(NX_IPV6_HEADER);
packet_ptr -> nx_packet_length = packet_ptr -> nx_packet_length - sizeof(NX_IPV6_HEADER);
}if (ip_dst_address -> nxd_ip_version == NX_IP_VERSION_V6) { ... }
/* ... */#endif
if (ip_ptr -> nx_ip_packet_filter)
{
if (ip_ptr -> nx_ip_packet_filter((VOID *)(packet_ptr -> nx_packet_prepend_ptr),
NX_IP_PACKET_OUT) != NX_SUCCESS)
{
_nx_packet_transmit_release(packet_ptr);
return(NX_SUCCESS);
}if (ip_ptr -> nx_ip_packet_filter((VOID *)(packet_ptr -> nx_packet_prepend_ptr), NX_IP_PACKET_OUT) != NX_SUCCESS) { ... }
}if (ip_ptr -> nx_ip_packet_filter) { ... }
if (ip_ptr -> nx_ip_packet_filter_extended)
{
if (ip_ptr -> nx_ip_packet_filter_extended(ip_ptr, packet_ptr, NX_IP_PACKET_OUT) != NX_SUCCESS)
{
_nx_packet_transmit_release(packet_ptr);
return(NX_SUCCESS);
}if (ip_ptr -> nx_ip_packet_filter_extended(ip_ptr, packet_ptr, NX_IP_PACKET_OUT) != NX_SUCCESS) { ... }
}if (ip_ptr -> nx_ip_packet_filter_extended) { ... }
}if (ip_ptr -> nx_ip_packet_filter || ip_ptr -> nx_ip_packet_filter_extended) { ... }
/* ... */#endif
packet_ptr -> nx_packet_prepend_ptr = original_ptr;
packet_ptr -> nx_packet_length = original_length;
/* ... */
if ((packet_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next != ((NX_PACKET*)NX_PACKET_ALLOCATED)) &&
(packet_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next != ((NX_PACKET*)NX_PACKET_FREE)))
{
packet_reset = NX_TRUE;
}if ((packet_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next != ((NX_PACKET*)NX_PACKET_ALLOCATED)) && (packet_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next != ((NX_PACKET*)NX_PACKET_FREE))) { ... }
status = interface_ptr -> nx_interface_tcpip_offload_handler(ip_ptr, interface_ptr, socket_ptr,
NX_TCPIP_OFFLOAD_UDP_SOCKET_SEND,
packet_ptr, ip_src_address, ip_dst_address,
socket_ptr -> nx_udp_socket_port,
&port, NX_NO_WAIT);
tx_mutex_put(&(ip_ptr -> nx_ip_protection));
if (status)
{
return(NX_TCPIP_OFFLOAD_ERROR);
}if (status) { ... }
else
{
if (packet_reset == NX_TRUE)
{
packet_ptr -> nx_packet_prepend_ptr = original_ptr - sizeof(NX_UDP_HEADER);
packet_ptr -> nx_packet_length = original_length + sizeof(NX_UDP_HEADER);
}if (packet_reset == NX_TRUE) { ... }
return(NX_SUCCESS);
}else { ... }
}_nx_udp_socket_driver_send (NX_UDP_SOCKET *socket_ptr, NX_PACKET *packet_ptr, NXD_ADDRESS *ip_src_address, NXD_ADDRESS *ip_dst_address, UINT port) { ... }
/* ... */#endif
...
UINT _nxd_udp_socket_send(NX_UDP_SOCKET *socket_ptr,
NX_PACKET *packet_ptr,
NXD_ADDRESS *ip_address,
UINT port)
{
TX_INTERRUPT_SAVE_AREA
NX_IP *ip_ptr;
NX_UDP_HEADER *udp_header_ptr;
ULONG *ip_src_addr = NX_NULL, *ip_dest_addr = NX_NULL;
#ifndef NX_DISABLE_IPV4
ULONG next_hop_address = 0;
#endif
#if !defined(NX_DISABLE_IPV4) || (defined(FEATURE_NX_IPV6) && defined(NX_ENABLE_INTERFACE_CAPABILITY))
NX_INTERFACE *interface_ptr = NX_NULL;
#endif
#ifdef FEATURE_NX_IPV6
UINT status;
#endif
#ifdef NX_ENABLE_TCPIP_OFFLOAD
NXD_ADDRESS ip_src_address;
#endif
#ifdef NX_IPSEC_ENABLE
VOID *sa = NX_NULL;
ULONG data_offset;
NXD_ADDRESS src_addr;
UINT ret;/* ... */
#endif
#ifdef TX_ENABLE_EVENT_TRACE
UINT ip_address_log = 0;
#endif
#if defined(NX_DISABLE_UDP_TX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE)
UINT compute_checksum = 1;
#endif
#ifdef NX_DISABLE_UDP_TX_CHECKSUM
compute_checksum = 0;/* ... */
#endif
TX_DISABLE
NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);
if (!socket_ptr -> nx_udp_socket_bound_next)
{
TX_RESTORE
return(NX_NOT_BOUND);
}if (!socket_ptr -> nx_udp_socket_bound_next) { ... }
ip_ptr = socket_ptr -> nx_udp_socket_ip_ptr;
#ifdef TX_ENABLE_EVENT_TRACE
#ifndef NX_DISABLE_IPV4
if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4)
{
ip_address_log = ip_address -> nxd_ip_address.v4;
}if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4) { ... }
/* ... */#endif
#ifdef FEATURE_NX_IPV6
if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6)
{
ip_address_log = ip_address -> nxd_ip_address.v6[3];
}if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6) { ... }
/* ... */#endif
NX_TRACE_IN_LINE_INSERT(NX_TRACE_UDP_SOCKET_SEND, socket_ptr, packet_ptr, packet_ptr -> nx_packet_length, ip_address_log, NX_TRACE_UDP_EVENTS, 0, 0);
/* ... */
#endif
TX_RESTORE
#ifndef NX_DISABLE_IPV4
if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4)
{
_nx_ip_route_find(ip_ptr, ip_address -> nxd_ip_address.v4, &packet_ptr -> nx_packet_address.nx_packet_interface_ptr,
&next_hop_address);
if (!packet_ptr -> nx_packet_address.nx_packet_interface_ptr)
{
return(NX_IP_ADDRESS_ERROR);
}if (!packet_ptr -> nx_packet_address.nx_packet_interface_ptr) { ... }
interface_ptr = packet_ptr -> nx_packet_address.nx_packet_interface_ptr;
ip_dest_addr = &ip_address -> nxd_ip_address.v4;
ip_src_addr = &interface_ptr -> nx_interface_ip_address;
#ifdef NX_ENABLE_TCPIP_OFFLOAD
ip_src_address.nxd_ip_version = NX_IP_VERSION_V4;
ip_src_address.nxd_ip_address.v4 = interface_ptr -> nx_interface_ip_address;/* ... */
#endif
}if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4) { ... }
/* ... */#endif
#ifdef FEATURE_NX_IPV6
if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6)
{
if (packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr == NX_NULL)
{
status = _nxd_ipv6_interface_find(ip_ptr, ip_address -> nxd_ip_address.v6,
&packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr,
NX_NULL);
if (status != NX_SUCCESS)
{
return(status);
}if (status != NX_SUCCESS) { ... }
}if (packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr == NX_NULL) { ... }
ip_dest_addr = &ip_address -> nxd_ip_address.v6[0];
ip_src_addr = packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address;
#ifdef NX_ENABLE_INTERFACE_CAPABILITY
interface_ptr = packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address_attached;/* ... */
#endif
#ifdef NX_ENABLE_TCPIP_OFFLOAD
ip_src_address.nxd_ip_version = NX_IP_VERSION_V6;
COPY_IPV6_ADDRESS(ip_src_addr, ip_src_address.nxd_ip_address.v6);/* ... */
#endif
}if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6) { ... }
/* ... */#endif
#ifdef NX_IPSEC_ENABLE
#ifndef NX_DISABLE_IPV4
if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4)
{
src_addr.nxd_ip_version = NX_IP_VERSION_V4;
src_addr.nxd_ip_address.v4 = interface_ptr -> nx_interface_ip_address;
}if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4) { ... }
/* ... */#endif
#ifdef FEATURE_NX_IPV6
if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6)
{
src_addr.nxd_ip_version = NX_IP_VERSION_V6;
COPY_IPV6_ADDRESS(packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address, src_addr.nxd_ip_address.v6);
}if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6) { ... }
/* ... */#endif
if (ip_ptr -> nx_ip_packet_egress_sa_lookup != NX_NULL)
{
ret = ip_ptr -> nx_ip_packet_egress_sa_lookup(ip_ptr,
&src_addr,
ip_address,
NX_PROTOCOL_UDP,
socket_ptr -> nx_udp_socket_port,
port,
&data_offset, &sa, 0);
if (ret == NX_IPSEC_TRAFFIC_PROTECT)
{
packet_ptr -> nx_packet_ipsec_sa_ptr = sa;
}if (ret == NX_IPSEC_TRAFFIC_PROTECT) { ... }
else if (ret == NX_IPSEC_TRAFFIC_DROP || ret == NX_IPSEC_TRAFFIC_PENDING_IKEV2)
{
return(NX_IPSEC_REJECTED);
}else if (ret == NX_IPSEC_TRAFFIC_DROP || ret == NX_IPSEC_TRAFFIC_PENDING_IKEV2) { ... }
else
{
packet_ptr -> nx_packet_ipsec_sa_ptr = NX_NULL;
}else { ... }
...}/* ... */
#endif
packet_ptr -> nx_packet_prepend_ptr = packet_ptr -> nx_packet_prepend_ptr - sizeof(NX_UDP_HEADER);
packet_ptr -> nx_packet_ip_version = (UCHAR)(ip_address -> nxd_ip_version);
#ifndef NX_DISABLE_UDP_INFO
ip_ptr -> nx_ip_udp_packets_sent++;
ip_ptr -> nx_ip_udp_bytes_sent += packet_ptr -> nx_packet_length;
socket_ptr -> nx_udp_socket_packets_sent++;
socket_ptr -> nx_udp_socket_bytes_sent += packet_ptr -> nx_packet_length;/* ... */
#endif
packet_ptr -> nx_packet_length = packet_ptr -> nx_packet_length + (ULONG)sizeof(NX_UDP_HEADER);
udp_header_ptr = (NX_UDP_HEADER *)packet_ptr -> nx_packet_prepend_ptr;
udp_header_ptr -> nx_udp_header_word_0 =
(((ULONG)socket_ptr -> nx_udp_socket_port) << NX_SHIFT_BY_16) | (ULONG)port;
udp_header_ptr -> nx_udp_header_word_1 = (packet_ptr -> nx_packet_length << NX_SHIFT_BY_16);
NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_UDP_SEND, ip_ptr, socket_ptr, packet_ptr, udp_header_ptr -> nx_udp_header_word_0, NX_TRACE_INTERNAL_EVENTS, 0, 0);
/* ... */
NX_CHANGE_ULONG_ENDIAN(udp_header_ptr -> nx_udp_header_word_0);
NX_CHANGE_ULONG_ENDIAN(udp_header_ptr -> nx_udp_header_word_1);
/* ... */
if ((!socket_ptr -> nx_udp_socket_disable_checksum) ||
(ip_address -> nxd_ip_version == NX_IP_VERSION_V6))
{
#ifdef NX_ENABLE_INTERFACE_CAPABILITY
if (interface_ptr -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_UDP_TX_CHECKSUM)
{
compute_checksum = 0;
}if (interface_ptr -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_UDP_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_UDP_TX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE)
if (compute_checksum)
#endif
{
ULONG checksum;
checksum = _nx_ip_checksum_compute(packet_ptr,
NX_PROTOCOL_UDP,
(UINT)packet_ptr -> nx_packet_length,
ip_src_addr,
ip_dest_addr);
checksum = ~checksum & NX_LOWER_16_MASK;
if (checksum == 0)
{
checksum = 0xFFFF;
}if (checksum == 0) { ... }
NX_CHANGE_ULONG_ENDIAN(udp_header_ptr -> nx_udp_header_word_1);
udp_header_ptr -> nx_udp_header_word_1 = udp_header_ptr -> nx_udp_header_word_1 | checksum;
NX_CHANGE_ULONG_ENDIAN(udp_header_ptr -> nx_udp_header_word_1);
...}
#ifdef NX_ENABLE_INTERFACE_CAPABILITY
else
{
packet_ptr -> nx_packet_interface_capability_flag |= NX_INTERFACE_CAPABILITY_UDP_TX_CHECKSUM;
}else { ... }
/* ... */#endif
}if ((!socket_ptr -> nx_udp_socket_disable_checksum) || (ip_address -> nxd_ip_version == NX_IP_VERSION_V6)) { ... }
tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
#ifdef NX_ENABLE_TCPIP_OFFLOAD
if ((interface_ptr -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_TCPIP_OFFLOAD) &&
(interface_ptr -> nx_interface_tcpip_offload_handler))
{
return(_nx_udp_socket_driver_send(socket_ptr, packet_ptr, &ip_src_address, ip_address, port));
}if ((interface_ptr -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_TCPIP_OFFLOAD) && (interface_ptr -> nx_interface_tcpip_offload_handler)) { ... }
/* ... */#endif
#ifndef NX_DISABLE_IPV4
if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4)
{
_nx_ip_packet_send(ip_ptr, packet_ptr, ip_address -> nxd_ip_address.v4,
socket_ptr -> nx_udp_socket_type_of_service,
socket_ptr -> nx_udp_socket_time_to_live,
NX_IP_UDP, socket_ptr -> nx_udp_socket_fragment_enable,
next_hop_address);
}if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4) { ... }
/* ... */#endif
#ifdef FEATURE_NX_IPV6
if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6)
{
_nx_ipv6_packet_send(ip_ptr, packet_ptr, NX_PROTOCOL_UDP,
packet_ptr -> nx_packet_length, ip_ptr -> nx_ipv6_hop_limit,
ip_src_addr,
ip_dest_addr);
}if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6) { ... }
/* ... */#endif
tx_mutex_put(&(ip_ptr -> nx_ip_protection));
return(NX_SUCCESS);
}{ ... }