1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Component                                                        */
16 /**                                                                       */
17 /**   Internet Protocol (IP)                                              */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "nx_api.h"
28 #include "nx_ip.h"
29 #include "nx_ipv6.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nxd_ip_raw_packet_source_send                      PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function sends a raw IP packet using specified interface IP    */
45 /*    address as source.                                                  */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    ip_ptr                                Pointer to IP control block   */
50 /*    packet_ptr                            Pointer to packet to send     */
51 /*    destination_ip                        Destination IP address        */
52 /*    address_index                         Index of IPv4 or IPv6 address */
53 /*                                            to use as the source address*/
54 /*    protocol                              Value for the protocol field  */
55 /*    ttl                                   Value for ttl or hop limit    */
56 /*    tos                                   Value for tos or traffic      */
57 /*                                            class and flow label        */
58 /*                                                                        */
59 /*  OUTPUT                                                                */
60 /*                                                                        */
61 /*    status                                Completion status             */
62 /*                                                                        */
63 /*  CALLS                                                                 */
64 /*                                                                        */
65 /*    nx_ip_packet_send                     Core IP packet send service   */
66 /*    nx_ip_route_find                      Find a suitable outgoing      */
67 /*                                            interface.                  */
68 /*    tx_mutex_get                          Get protection mutex          */
69 /*    tx_mutex_put                          Put protection mutex          */
70 /*    _nxd_ipv6_raw_packet_send_internal    IPv6 raw packet send          */
71 /*                                                                        */
72 /*  CALLED BY                                                             */
73 /*                                                                        */
74 /*    Application                                                         */
75 /*                                                                        */
76 /*  RELEASE HISTORY                                                       */
77 /*                                                                        */
78 /*    DATE              NAME                      DESCRIPTION             */
79 /*                                                                        */
80 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
81 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
82 /*                                            resulting in version 6.1    */
83 /*                                                                        */
84 /**************************************************************************/
_nxd_ip_raw_packet_source_send(NX_IP * ip_ptr,NX_PACKET * packet_ptr,NXD_ADDRESS * destination_ip,UINT address_index,ULONG protocol,UINT ttl,ULONG tos)85 UINT  _nxd_ip_raw_packet_source_send(NX_IP *ip_ptr,
86                                      NX_PACKET *packet_ptr,
87                                      NXD_ADDRESS *destination_ip,
88                                      UINT address_index,
89                                      ULONG protocol,
90                                      UINT ttl,
91                                      ULONG tos)
92 {
93 
94 #ifndef NX_DISABLE_IPV4
95 ULONG next_hop_address = NX_NULL;
96 #endif /* NX_DISABLE_IPV4  */
97 UINT  status = NX_SUCCESS;
98 
99 #ifdef NX_DISABLE_IPV4
100     NX_PARAMETER_NOT_USED(ttl);
101     NX_PARAMETER_NOT_USED(tos);
102 #endif /* NX_DISABLE_IPV4 */
103 
104     /* Add debug information. */
105     NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);
106 
107     /* Get mutex protection.  */
108     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
109 
110 #ifndef NX_DISABLE_IPV4
111 
112     /* Store address information into the packet structure. */
113     if (destination_ip -> nxd_ip_version == NX_IP_VERSION_V4)
114     {
115         packet_ptr -> nx_packet_address.nx_packet_interface_ptr = &(ip_ptr -> nx_ip_interface[address_index]);
116 
117         /* Figure out a suitable outgoing interface. */
118         /* Since next_hop_address is validated in _nx_ip_packet_send, there is no need to check the value here. */
119         _nx_ip_route_find(ip_ptr, destination_ip -> nxd_ip_address.v4, &packet_ptr -> nx_packet_address.nx_packet_interface_ptr, &next_hop_address);
120 
121         /* If trace is enabled, insert this event into the trace buffer.  */
122         NX_TRACE_IN_LINE_INSERT(NX_TRACE_IP_RAW_PACKET_SEND, ip_ptr, packet_ptr, destination_ip, 0, NX_TRACE_IP_EVENTS, 0, 0);
123 
124         /* Yes, raw packet sending and receiving is enabled send packet!  */
125         /*lint -e{644} suppress variable might not be initialized, since "next_hop_address" was initialized in _nx_ip_route_find. */
126         _nx_ip_packet_send(ip_ptr, packet_ptr, destination_ip -> nxd_ip_address.v4, (tos & 0xFF) << 16, (ttl & 0xFF), protocol << 16, NX_FRAGMENT_OKAY, next_hop_address);
127     }
128 #endif /* NX_DISABLE_IPV4  */
129 
130 #ifdef FEATURE_NX_IPV6
131     if (destination_ip -> nxd_ip_version == NX_IP_VERSION_V6)
132     {
133         packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr = &(ip_ptr -> nx_ipv6_address[address_index]);
134 
135         status =  _nxd_ipv6_raw_packet_send_internal(ip_ptr, packet_ptr, destination_ip, protocol);
136     }
137 #endif /* FEATURE_NX_IPV6 */
138 
139     /* Release mutex protection.  */
140     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
141 
142     /* Return a status!  */
143     return(status);
144 }
145 
146