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 
28 #include "nx_api.h"
29 #include "nx_ip.h"
30 #include "nx_ipv6.h"
31 #include "nx_packet.h"
32 
33 /* Bring in externs for caller checking code.  */
34 
35 
36 NX_CALLER_CHECKING_EXTERNS
37 
38 /**************************************************************************/
39 /*                                                                        */
40 /*  FUNCTION                                               RELEASE        */
41 /*                                                                        */
42 /*    _nxde_ip_raw_packet_send                             PORTABLE C     */
43 /*                                                           6.1          */
44 /*  AUTHOR                                                                */
45 /*                                                                        */
46 /*    Yuxin Zhou, Microsoft Corporation                                   */
47 /*                                                                        */
48 /*  DESCRIPTION                                                           */
49 /*                                                                        */
50 /*    This function sends a raw IP packet through the specified IPv6      */
51 /*    interface.                                                          */
52 /*                                                                        */
53 /*  INPUT                                                                 */
54 /*                                                                        */
55 /*    ip_ptr                                Pointer to IP control block   */
56 /*    packet_ptr                            Pointer to packet to send     */
57 /*    destination_ip                        Destination IP address        */
58 /*    protocol                              Value for the protocol field  */
59 /*    ttl                                   Value for ttl or hop limit    */
60 /*    tos                                   Value for tos or traffic      */
61 /*                                            class and flow label        */
62 /*                                                                        */
63 /*  OUTPUT                                                                */
64 /*                                                                        */
65 /*    status                                Actual completion status      */
66 /*    NX_PTR_ERROR                          Invalid pointer input         */
67 /*    NX_NOT_ENABLED                        Raw IP not enabled            */
68 /*    NX_IP_ADDRESS_ERROR                   Invalid address version       */
69 /*    NX_INVALID_PARAMETERS                 Invalid protocol specified    */
70 /*    NX_UNDERFLOW                          Invalid packet header         */
71 /*                                                                        */
72 /*  CALLS                                                                 */
73 /*                                                                        */
74 /*    _nxd_ip_raw_packet_source_send        Actual raw packet send        */
75 /*                                             function.                  */
76 /*                                                                        */
77 /*  CALLED BY                                                             */
78 /*                                                                        */
79 /*    Application                                                         */
80 /*                                                                        */
81 /*  RELEASE HISTORY                                                       */
82 /*                                                                        */
83 /*    DATE              NAME                      DESCRIPTION             */
84 /*                                                                        */
85 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
86 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
87 /*                                            resulting in version 6.1    */
88 /*                                                                        */
89 /**************************************************************************/
_nxde_ip_raw_packet_send(NX_IP * ip_ptr,NX_PACKET ** packet_ptr_ptr,NXD_ADDRESS * destination_ip,ULONG protocol,UINT ttl,ULONG tos)90 UINT  _nxde_ip_raw_packet_send(NX_IP *ip_ptr, NX_PACKET **packet_ptr_ptr,
91                                NXD_ADDRESS *destination_ip, ULONG protocol, UINT ttl, ULONG tos)
92 {
93 
94 
95 NX_PACKET *packet_ptr;
96 
97 
98     /* Setup packet pointer.  */
99     packet_ptr =  *packet_ptr_ptr;
100 
101     /* Check for invalid input pointers.  */
102     /* Cast the ULONG into a packet pointer. Since this is exactly what we wish to do, disable the lint warning with the following comment:  */
103     /*lint -e{923} suppress cast of ULONG to pointer.  */
104     if ((ip_ptr == NX_NULL) || (ip_ptr -> nx_ip_id != NX_IP_ID) || (packet_ptr == NX_NULL) ||
105         (packet_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next != ((NX_PACKET *)NX_PACKET_ALLOCATED)))
106     {
107 
108         return(NX_PTR_ERROR);
109     }
110 
111     /* Check to see if IP raw packet processing is enabled.  */
112     if (!ip_ptr -> nx_ip_raw_ip_processing)
113     {
114         return(NX_NOT_ENABLED);
115     }
116 
117     /* Check for invalid IP address.  */
118     if (!destination_ip || ((destination_ip -> nxd_ip_version != NX_IP_VERSION_V6) &&
119                             (destination_ip -> nxd_ip_version != NX_IP_VERSION_V4)))
120     {
121 
122         return(NX_IP_ADDRESS_ERROR);
123     }
124 
125     /* Check for valid protocol.  */
126     if ((protocol & 0x000000FF) != protocol)
127     {
128         return(NX_INVALID_PARAMETERS);
129     }
130 
131 #ifndef NX_DISABLE_IPV4
132     if (destination_ip -> nxd_ip_version == NX_IP_VERSION_V4)
133     {
134 
135         /* Check for an invalid packet prepend pointer for IPv4 packet.  */
136         /*lint -e{946} suppress pointer subtraction, since it is necessary. */
137         if ((packet_ptr -> nx_packet_prepend_ptr - sizeof(NX_IPV4_HEADER)) < packet_ptr -> nx_packet_data_start)
138         {
139             return(NX_UNDERFLOW);
140         }
141         if (destination_ip -> nxd_ip_address.v4 == 0)
142         {
143             return(NX_IP_ADDRESS_ERROR);
144         }
145     }
146 #endif /* !NX_DISABLE_IPV4  */
147 
148 #ifdef FEATURE_NX_IPV6
149     if (destination_ip -> nxd_ip_version == NX_IP_VERSION_V6)
150     {
151 
152         /* Check for an invalid packet prepend pointer for IPv6 packet.  */
153         /*lint -e{946} suppress pointer subtraction, since it is necessary. */
154         if ((packet_ptr -> nx_packet_prepend_ptr - sizeof(NX_IPV6_HEADER)) < packet_ptr -> nx_packet_data_start)
155         {
156             return(NX_UNDERFLOW);
157         }
158         if (CHECK_UNSPECIFIED_ADDRESS(&destination_ip -> nxd_ip_address.v6[0]))
159         {
160             return(NX_IP_ADDRESS_ERROR);
161         }
162     }
163 #endif /* FEATURE_NX_IPV6  */
164 
165 
166     /* Check for an invalid packet append pointer.  */
167     /*lint -e{946} suppress pointer subtraction, since it is necessary. */
168     if (packet_ptr -> nx_packet_append_ptr > packet_ptr -> nx_packet_data_end)
169     {
170         return(NX_OVERFLOW);
171     }
172 
173     /* Check for appropriate caller.  */
174     NX_THREADS_ONLY_CALLER_CHECKING
175 
176     _nxd_ip_raw_packet_source_send(ip_ptr, packet_ptr, destination_ip, 0, protocol, ttl, tos);
177 
178     /* Now clear the application's packet pointer so it can't be accidentally
179        used again by the application.  This is only done when error checking is
180        enabled.  */
181     *packet_ptr_ptr =  NX_NULL;
182 
183     /* Return completion status.  */
184     return(NX_SUCCESS);
185 }
186 
187