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_packet.h"
30 
31 
32 /* Bring in externs for caller checking code.  */
33 
34 NX_CALLER_CHECKING_EXTERNS
35 
36 
37 /**************************************************************************/
38 /*                                                                        */
39 /*  FUNCTION                                               RELEASE        */
40 /*                                                                        */
41 /*    _nxe_ip_raw_packet_source_send                      PORTABLE C      */
42 /*                                                           6.1          */
43 /*  AUTHOR                                                                */
44 /*                                                                        */
45 /*    Yuxin Zhou, Microsoft Corporation                                   */
46 /*                                                                        */
47 /*  DESCRIPTION                                                           */
48 /*                                                                        */
49 /*    This function checks for errors in the IP raw packet send           */
50 /*    function call.                                                      */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    ip_ptr                                Pointer to IP control block   */
55 /*    packet_ptr                            Pointer to packet to send     */
56 /*    destination_ip                        Destination IP address        */
57 /*    address_index                         Index of IPv4 or IPv6 address */
58 /*                                            to use as the source address*/
59 /*    type_of_service                       Type of service for packet    */
60 /*                                                                        */
61 /*  OUTPUT                                                                */
62 /*                                                                        */
63 /*    status                                Completion status             */
64 /*                                                                        */
65 /*  CALLS                                                                 */
66 /*                                                                        */
67 /*    _nx_ip_raw_packet_source_send         Actual IP raw packet send     */
68 /*                                            function                    */
69 /*                                                                        */
70 /*  CALLED BY                                                             */
71 /*                                                                        */
72 /*    Application                                                         */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
79 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
80 /*                                            resulting in version 6.1    */
81 /*                                                                        */
82 /**************************************************************************/
_nxe_ip_raw_packet_source_send(NX_IP * ip_ptr,NX_PACKET ** packet_ptr_ptr,ULONG destination_ip,UINT address_index,ULONG type_of_service)83 UINT  _nxe_ip_raw_packet_source_send(NX_IP *ip_ptr, NX_PACKET **packet_ptr_ptr,
84                                      ULONG destination_ip, UINT address_index, ULONG type_of_service)
85 {
86 
87 #ifndef NX_DISABLE_IPV4
88 NX_PACKET *packet_ptr;
89 
90 
91     /* Setup packet pointer.  */
92     packet_ptr =  *packet_ptr_ptr;
93 
94     /* Check for invalid input pointers.  */
95     /*lint -e{923} suppress cast of ULONG to pointer.  */
96     if ((ip_ptr == NX_NULL) || (ip_ptr -> nx_ip_id != NX_IP_ID) ||
97         (packet_ptr == NX_NULL) || (packet_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next != ((NX_PACKET *)NX_PACKET_ALLOCATED)))
98     {
99         return(NX_PTR_ERROR);
100     }
101 
102     /* Check to see if IP raw packet processing is enabled.  */
103     if (!ip_ptr -> nx_ip_raw_ip_processing)
104     {
105         return(NX_NOT_ENABLED);
106     }
107 
108     /* Check for invalid IP address.  */
109     if (!destination_ip)
110     {
111         return(NX_IP_ADDRESS_ERROR);
112     }
113 
114     /* Check for valid type of service.  */
115     if (type_of_service & ~(NX_IP_TOS_MASK))
116     {
117         return(NX_OPTION_ERROR);
118     }
119 
120     /* Check for an invalid packet prepend pointer.  */
121     /*lint -e{946} suppress pointer subtraction, since it is necessary. */
122     if ((packet_ptr -> nx_packet_prepend_ptr - sizeof(NX_IPV4_HEADER)) < packet_ptr -> nx_packet_data_start)
123     {
124         return(NX_UNDERFLOW);
125     }
126 
127     /* Check for an invalid packet append pointer.  */
128     /*lint -e{946} suppress pointer subtraction, since it is necessary. */
129     if (packet_ptr -> nx_packet_append_ptr > packet_ptr -> nx_packet_data_end)
130     {
131         return(NX_OVERFLOW);
132     }
133 
134 
135     /* Validate the interface index. */
136     if (address_index >= NX_MAX_IP_INTERFACES)
137     {
138         return(NX_INVALID_INTERFACE);
139     }
140 
141     if (!(ip_ptr -> nx_ip_interface[address_index].nx_interface_valid))
142     {
143         return(NX_INVALID_INTERFACE);
144     }
145 
146 
147     /* Check for appropriate caller.  */
148     NX_THREADS_ONLY_CALLER_CHECKING
149 
150     /* Call actual IP raw packet send function.  */
151     _nx_ip_raw_packet_source_send(ip_ptr, packet_ptr, destination_ip, address_index, type_of_service);
152 
153     /* Now clear the application's packet pointer so it can't be accidentally
154         used again by the application.  This is only done when error checking is
155         enabled.  */
156     *packet_ptr_ptr =  NX_NULL;
157 
158     /* Return completion status.  */
159     return(NX_SUCCESS);
160 #else /* NX_DISABLE_IPV4  */
161     NX_PARAMETER_NOT_USED(ip_ptr);
162     NX_PARAMETER_NOT_USED(packet_ptr_ptr);
163     NX_PARAMETER_NOT_USED(destination_ip);
164     NX_PARAMETER_NOT_USED(address_index);
165     NX_PARAMETER_NOT_USED(type_of_service);
166 
167     return(NX_NOT_SUPPORTED);
168 #endif /* !NX_DISABLE_IPV4  */
169 }
170 
171