summaryrefslogtreecommitdiff
path: root/common/src/nxd_udp_socket_send.c
blob: afb1269187875a3cc9e20ce0ca0f37b5e67e70de (plain)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Component                                                        */
/**                                                                       */
/**   User Datagram Protocol (UDP)                                        */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SOURCE_CODE


/* Include necessary system files.  */

#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 /* FEATURE_NX_IPV6 */

#ifdef NX_IPSEC_ENABLE
#include "nx_ipsec.h"
#endif /* NX_IPSEC_ENABLE */


#ifdef NX_ENABLE_TCPIP_OFFLOAD
/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_udp_socket_driver_send                          PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function sends a UDP packet through TCP/IP offload interface.  */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    socket_ptr                            Pointer to UDP socket         */
/*    packet_ptr                            Pointer to UDP packet         */
/*    ip_src_address                        Source IP address             */
/*    ip_dst_address                        Destination IP address        */
/*    port                                  16-bit UDP port number        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_ip_packet_send                    Packet send function          */
/*    _nx_ipv6_packet_send                  Packet send function          */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nxd_udp_socket_send                                                */
/*                                                                        */
/**************************************************************************/
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;

    /* Set up the pointer to the associated IP instance.  */
    ip_ptr =  socket_ptr -> nx_udp_socket_ip_ptr;

    /* Get the outgoing interface. */
#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;
    }
#endif /* NX_DISABLE_IPV4 */
#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;
    }
#endif /* FEATURE_NX_IPV6 */

#ifdef NX_ENABLE_IP_PACKET_FILTER
    /* Check if the IP packet filter is set. */
    if (ip_ptr -> nx_ip_packet_filter || ip_ptr -> nx_ip_packet_filter_extended)
    {

        /* Add the IP Header to trigger filtering.  */
#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);
        }
#endif /* !NX_DISABLE_IPV4  */

#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))
            {

                /* Packet consumed by IPv6 layer. Just return success.  */
                tx_mutex_put(&(ip_ptr -> nx_ip_protection));
                return(NX_SUCCESS);
            }

            /* Reset IP header.  */
            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);
        }
#endif /* FEATURE_NX_IPV6 */

        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)
            {

                /* Packet consumed by IP filter. Just return success.  */
                _nx_packet_transmit_release(packet_ptr);
                return(NX_SUCCESS);
            }
        }

        /* Check if the IP packet filter extended is set. */
        if (ip_ptr -> nx_ip_packet_filter_extended)
        {

            /* Yes, call the IP packet filter extended routine. */
            if (ip_ptr -> nx_ip_packet_filter_extended(ip_ptr, packet_ptr, NX_IP_PACKET_OUT) != NX_SUCCESS)
            {

                /* Packet consumed by IP filter. Just return success.  */
                _nx_packet_transmit_release(packet_ptr);
                return(NX_SUCCESS);
            }
        }
    }
#endif /* NX_ENABLE_IP_PACKET_FILTER */

    /* Reset UDP and IP header.  */
    packet_ptr -> nx_packet_prepend_ptr = original_ptr;
    packet_ptr -> nx_packet_length = original_length;

    /* Determine if the packet is a queued data packet. _nx_packet_transmit_release in Offload handler
       does not release the packet immediately and only adjusts the prepend pointer to User data,
       since the packet may need to be resent. To keep the same logic for retransmission in upper layer,
       the prepend pointer must be reset to UDP header.  */
    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;
    }

    /* Let TCP/IP offload interface send the packet.  */
    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);

    /* Release the IP protection.  */
    tx_mutex_put(&(ip_ptr -> nx_ip_protection));

    if (status)
    {
        return(NX_TCPIP_OFFLOAD_ERROR);
    }
    else
    {

        /* Reset prepend pointer to UDP header for queued packet.  */
        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);
        }

        return(NX_SUCCESS);
    }
}
#endif /* NX_ENABLE_TCPIP_OFFLOAD */

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxd_udp_socket_send                                PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function sends a UDP packet through the specified socket       */
/*    with the input IP address and port.                                 */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    socket_ptr                            Pointer to UDP socket         */
/*    packet_ptr                            Pointer to UDP packet         */
/*    ip_address                            IP address                    */
/*    port                                  16-bit UDP port number        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*    NX_IPSEC_REJECTED                     Failed IPSec check            */
/*    NX_NOT_BOUND                          Socket not bound to a port    */
/*    NX_NO_INTERFACE_ADDRESS               Socket interface not marked   */
/*                                             valid                      */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_ip_packet_send                    Send UDP packet over IPv4     */
/*    _nx_ipv6_packet_send                  Send UDP packet over IPv6     */
/*    nx_ip_checksum_compute                Compute UDP header checksum   */
/*    tx_mutex_get                          Get protection mutex          */
/*    tx_mutex_put                          Put protection mutex          */
/*    _nxd_ipv6_interface_find              Find interface for input      */
/*                                             address in IP address table*/
/*    [_nx_packet_egress_sa_lookup]         IPsec process                 */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
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 /* !NX_DISABLE_IPV4  */
#if !defined(NX_DISABLE_IPV4) || (defined(FEATURE_NX_IPV6) && defined(NX_ENABLE_INTERFACE_CAPABILITY))
NX_INTERFACE  *interface_ptr = NX_NULL;
#endif /* !NX_DISABLE_IPV4 || (FEATURE_NX_IPV6 && NX_ENABLE_INTERFACE_CAPABILITY) */
#ifdef FEATURE_NX_IPV6
UINT           status;
#endif /* FEATURE_NX_IPV6 */

#ifdef NX_ENABLE_TCPIP_OFFLOAD
NXD_ADDRESS    ip_src_address;
#endif /* NX_ENABLE_TCPIP_OFFLOAD */

#ifdef NX_IPSEC_ENABLE
VOID          *sa = NX_NULL;
ULONG          data_offset;
NXD_ADDRESS    src_addr;
UINT           ret;
#endif /* NX_IPSEC_ENABLE */

#ifdef TX_ENABLE_EVENT_TRACE
UINT           ip_address_log = 0;
#endif /* TX_ENABLE_EVENT_TRACE */

#if defined(NX_DISABLE_UDP_TX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE)
UINT           compute_checksum = 1;
#endif /* defined(NX_DISABLE_UDP_TX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE) */

#ifdef NX_DISABLE_UDP_TX_CHECKSUM
    /* Disable UDP TX checksum. */
    compute_checksum = 0;
#endif /* NX_DISABLE_UDP_TX_CHECKSUM */

    /* Lockout interrupts.  */
    TX_DISABLE

    /* Add debug information. */
    NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);

    /* Determine if the socket is currently bound to a UDP port.  */
    if (!socket_ptr ->  nx_udp_socket_bound_next)
    {

        /* Restore interrupts.  */
        TX_RESTORE

        /* Socket is not bound, return an error message.  */
        return(NX_NOT_BOUND);
    }

    /* Pickup the important information from the socket.  */

    /* Set up the pointer to the associated IP instance.  */
    ip_ptr =  socket_ptr -> nx_udp_socket_ip_ptr;

#ifdef TX_ENABLE_EVENT_TRACE

#ifndef NX_DISABLE_IPV4
    /* For IPv4 packets, log the whole IP address. */
    if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4)
    {
        ip_address_log = ip_address -> nxd_ip_address.v4;
    }
#endif /* !NX_DISABLE_IPV4  */

#ifdef FEATURE_NX_IPV6

    /* For IPv6 packets, log the least significant 32-bit of the IP address. */
    if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6)
    {
        ip_address_log = ip_address -> nxd_ip_address.v6[3];
    }
#endif /* FEATURE_NX_IPV6  */

    /* If trace is enabled, insert this event into the trace buffer.  */
    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_ENABLE_EVENT_TRACE */

    /* Restore interrupts.  */
    TX_RESTORE

#ifndef NX_DISABLE_IPV4
    if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4)
    {

        /* Look for a suitable interface. */
        _nx_ip_route_find(ip_ptr, ip_address -> nxd_ip_address.v4, &packet_ptr -> nx_packet_address.nx_packet_interface_ptr,
                          &next_hop_address);

        /* Check the packet interface.  */
        if (!packet_ptr -> nx_packet_address.nx_packet_interface_ptr)
        {

            /* None found; return the error status. */
            return(NX_IP_ADDRESS_ERROR);
        }

        interface_ptr = packet_ptr -> nx_packet_address.nx_packet_interface_ptr;

        /* Fill in the IP src/dest address */
        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 /* NX_ENABLE_TCPIP_OFFLOAD */
    }
#endif /* NX_DISABLE_IPV4 */

#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)
        {

            /* Determine if the IP instance has a matching address for the packet destination. */
            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 not, return the error status. */
            if (status != NX_SUCCESS)
            {
                return(status);
            }
        }

        /* Fill in the IP src/dest address */
        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
        /* Get the packet interface information. */
        interface_ptr = packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address_attached;
#endif /* NX_ENABLE_INTERFACE_CAPABILITY  */

#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 /* NX_ENABLE_TCPIP_OFFLOAD */
    }
#endif /* FEATURE_NX_IPV6 */

#ifdef NX_IPSEC_ENABLE
#ifndef NX_DISABLE_IPV4
    if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4)
    {

        /* Copy IP version and address for internal IPSec (SA) processing. */
        src_addr.nxd_ip_version = NX_IP_VERSION_V4;
        src_addr.nxd_ip_address.v4 = interface_ptr -> nx_interface_ip_address;
    }
#endif /* NX_DISABLE_IPV4 */

#ifdef FEATURE_NX_IPV6
    if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6)
    {

        /* Handle for IPv6 packets. */
        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);
    }
#endif /* FEATURE_NX_IPV6 */

    /* Check for possible SA match. */
    if (ip_ptr -> nx_ip_packet_egress_sa_lookup != NX_NULL)               /* IPsec is enabled. */
    {
        /* If the SA has not been set. */
        ret = ip_ptr -> nx_ip_packet_egress_sa_lookup(ip_ptr,                                    /* IP ptr */
                                                      &src_addr,                                 /* src_addr */
                                                      ip_address,                                /* dest_addr */
                                                      NX_PROTOCOL_UDP,                           /* protocol */
                                                      socket_ptr -> nx_udp_socket_port,          /* src_port */
                                                      port,                                      /* dest_port */
                                                      &data_offset, &sa, 0);
        if (ret == NX_IPSEC_TRAFFIC_PROTECT)
        {

            /* Save the SA to the packet. */
            packet_ptr -> nx_packet_ipsec_sa_ptr = sa;
        }
        else if (ret == NX_IPSEC_TRAFFIC_DROP || ret == NX_IPSEC_TRAFFIC_PENDING_IKEV2)
        {
            return(NX_IPSEC_REJECTED);
        }
        else
        {
            /* Zero out sa information. */
            packet_ptr -> nx_packet_ipsec_sa_ptr = NX_NULL;
        }
    }
#endif /* NX_IPSEC_ENABLE */
        
    /* Prepend the UDP header to the packet.  First, make room for the UDP header.  */
    packet_ptr -> nx_packet_prepend_ptr =  packet_ptr -> nx_packet_prepend_ptr - sizeof(NX_UDP_HEADER);

    /* Set the correct IP version. */
    packet_ptr -> nx_packet_ip_version = (UCHAR)(ip_address -> nxd_ip_version);

#ifndef NX_DISABLE_UDP_INFO
    /* Increment the total UDP packets sent count.  */
    ip_ptr -> nx_ip_udp_packets_sent++;

    /* Increment the total UDP bytes sent.  */
    ip_ptr -> nx_ip_udp_bytes_sent +=  packet_ptr -> nx_packet_length;

    /* Increment the total UDP packets sent count for this socket.  */
    socket_ptr -> nx_udp_socket_packets_sent++;

    /* Increment the total UDP bytes sent for this socket.  */
    socket_ptr -> nx_udp_socket_bytes_sent +=  packet_ptr -> nx_packet_length;
#endif

    /* Increase the packet length.  */
    packet_ptr -> nx_packet_length =  packet_ptr -> nx_packet_length + (ULONG)sizeof(NX_UDP_HEADER);

    /* Setup the UDP header pointer.  */
    /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
    udp_header_ptr =  (NX_UDP_HEADER *)packet_ptr -> nx_packet_prepend_ptr;

    /* Build the first 32-bit word of the UDP header.  */
    udp_header_ptr -> nx_udp_header_word_0 =
        (((ULONG)socket_ptr -> nx_udp_socket_port) << NX_SHIFT_BY_16) | (ULONG)port;

    /* Build the second 32-bit word of the UDP header.  */
    udp_header_ptr -> nx_udp_header_word_1 =  (packet_ptr -> nx_packet_length << NX_SHIFT_BY_16);

    /* If trace is enabled, insert this event into the trace buffer.  */
    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);

    /* Endian swapping logic.  If NX_LITTLE_ENDIAN is specified, these macros will
    swap the endian of the UDP header.  */
    NX_CHANGE_ULONG_ENDIAN(udp_header_ptr -> nx_udp_header_word_0);
    NX_CHANGE_ULONG_ENDIAN(udp_header_ptr -> nx_udp_header_word_1);

    /* Determine if we need to compute the UDP checksum.

    Note that with IPv6, UDP packet checksum is mandatory. However if the underly device
    driver is able to compute UDP checksum in hardware, let the driver handle the checksum
    computation.
    */

    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;
        }
#endif /* NX_ENABLE_INTERFACE_CAPABILITY */

#ifdef NX_IPSEC_ENABLE
        /* In case this packet is going through the IPsec protected channel, the checksum would have to be computed
        in software even if the hardware checksum is available at driver layer.  The checksum value must be present
        in order when applying IPsec process. */

        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;
        }
#endif /* NX_IPSEC_ENABLE */

#if defined(NX_DISABLE_UDP_TX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE)
        if (compute_checksum)
#endif /* defined(NX_DISABLE_UDP_TX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE) */
        {

        ULONG checksum;

            /* Yes, we need to compute the UDP 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 the computed checksum is zero, it will be transmitted as all ones. */
            /* RFC 768, page 2. */
            if (checksum == 0)
            {
                checksum = 0xFFFF;
            }

            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
        {
            /* Set CHECKSUM flag so the driver would invoke the HW checksum. */
            packet_ptr -> nx_packet_interface_capability_flag |= NX_INTERFACE_CAPABILITY_UDP_TX_CHECKSUM;
        }
#endif
    }

    /* Get mutex protection.  */
    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));
    }
#endif /* NX_ENABLE_TCPIP_OFFLOAD */

#ifdef NX_ENABLE_VLAN
    if (socket_ptr -> nx_udp_socket_vlan_priority != NX_VLAN_PRIORITY_INVALID)
    {
        packet_ptr -> nx_packet_vlan_priority = socket_ptr -> nx_udp_socket_vlan_priority;
    }
#endif /* NX_ENABLE_VLAN */

#ifndef NX_DISABLE_IPV4
    /* Send the UDP packet to the IPv4 component.  */
    if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4)
    {

        /*lint -e{644} suppress variable might not be initialized, since "next_hop_address" was initialized in _nx_ip_route_find. */
        _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);
    }
#endif /* NX_DISABLE_IPV4 */

#ifdef FEATURE_NX_IPV6
    if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6)
    {

        /* Set the source IPv6 address */
        _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);
    }
#endif /* FEATURE_NX_IPV6 */

    /* Release mutex protection.  */
    tx_mutex_put(&(ip_ptr -> nx_ip_protection));

    /* Return a successful status.  */
    return(NX_SUCCESS);
}