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 /* Bring in externs for caller checking code.  */
32 
33 NX_CALLER_CHECKING_EXTERNS
34 
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    _nxde_ip_raw_packet_source_send                     PORTABLE C      */
40 /*                                                           6.1          */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    Yuxin Zhou, Microsoft Corporation                                   */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This function does error checking for the raw IP packet send serivce*/
48 /*    out the specified IP source.                                        */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    ip_ptr                                Pointer to IP control block   */
53 /*    packet_ptr                            Pointer to packet to send     */
54 /*    destination_ip                        Destination IP address        */
55 /*    address_index                         Index to the IPv6 address     */
56 /*    protocol                              Value for the protocol field  */
57 /*    ttl                                   Value for ttl or hop limit    */
58 /*    tos                                   Value for tos or traffic      */
59 /*                                            class and flow label        */
60 /*                                                                        */
61 /*  OUTPUT                                                                */
62 /*                                                                        */
63 /*    NX_PTR_ERROR                          Invalid pointer input         */
64 /*    NX_NOT_ENABLED                        Raw packet send not enabled   */
65 /*    NX_IP_ADDRESS_ERROR                   Invalid input address         */
66 /*    NX_INVALID_INTERFACE                  Invalid interface input       */
67 /*    status                                Completion status             */
68 /*                                                                        */
69 /*  CALLS                                                                 */
70 /*                                                                        */
71 /*    nxd_ip_raw_packet_source_send         Actual raw packet send service*/
72 /*                                                                        */
73 /*  CALLED BY                                                             */
74 /*                                                                        */
75 /*    Application                                                         */
76 /*                                                                        */
77 /*  RELEASE HISTORY                                                       */
78 /*                                                                        */
79 /*    DATE              NAME                      DESCRIPTION             */
80 /*                                                                        */
81 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
82 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
83 /*                                            resulting in version 6.1    */
84 /*                                                                        */
85 /**************************************************************************/
_nxde_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)86 UINT  _nxde_ip_raw_packet_source_send(NX_IP *ip_ptr, NX_PACKET *packet_ptr,
87                                       NXD_ADDRESS *destination_ip, UINT address_index, ULONG protocol, UINT ttl, ULONG tos)
88 {
89 
90 UINT status;
91 
92     /* Check for invalid input pointers.  */
93     if ((ip_ptr == NX_NULL) || (packet_ptr == NX_NULL) || (destination_ip == NX_NULL))
94     {
95         return(NX_PTR_ERROR);
96     }
97 
98     /* Determine if raw IP packet sending/receiving is enabled.  */
99     if (!ip_ptr -> nx_ip_raw_ip_processing)
100     {
101         return(NX_NOT_ENABLED);
102     }
103 
104     /* Check that the destination address version is either IPv4 or IPv6. */
105     if ((destination_ip -> nxd_ip_version != NX_IP_VERSION_V4) &&
106         (destination_ip -> nxd_ip_version != NX_IP_VERSION_V6))
107     {
108         return(NX_IP_ADDRESS_ERROR);
109     }
110 
111 #ifndef NX_DISABLE_IPV4
112     if (destination_ip -> nxd_ip_version == NX_IP_VERSION_V4)
113     {
114 
115         if (destination_ip -> nxd_ip_address.v4 == 0)
116         {
117             return(NX_IP_ADDRESS_ERROR);
118         }
119 
120         /* Check for valid interface. */
121         if (address_index >= NX_MAX_IP_INTERFACES)
122         {
123             return(NX_INVALID_INTERFACE);
124         }
125 
126         /* Check for an invalid packet prepend pointer for IPv4 packet.  */
127         /*lint -e{946} suppress pointer subtraction, since it is necessary. */
128         if ((packet_ptr -> nx_packet_prepend_ptr - sizeof(NX_IPV4_HEADER)) < packet_ptr -> nx_packet_data_start)
129         {
130             return(NX_UNDERFLOW);
131         }
132     }
133 #endif /* !NX_DISABLE_IPV4  */
134 
135 #ifdef FEATURE_NX_IPV6
136     if (destination_ip -> nxd_ip_version == NX_IP_VERSION_V6)
137     {
138 
139         /* destination_ip -> nxd_ip_version == NX_IP_VERSION_V6.  */
140         /* Check for valid destination address. */
141         if (CHECK_UNSPECIFIED_ADDRESS(&destination_ip -> nxd_ip_address.v6[0]))
142         {
143             return(NX_IP_ADDRESS_ERROR);
144         }
145 
146         /* Check for valid interface. */
147         if (address_index >= (NX_MAX_IPV6_ADDRESSES + NX_LOOPBACK_IPV6_ENABLED))
148         {
149             return(NX_IP_ADDRESS_ERROR);
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     }
159 #endif /* FEATURE_NX_IPV6 */
160 
161     /* Check for an invalid packet append pointer.  */
162     /*lint -e{946} suppress pointer subtraction, since it is necessary. */
163     if (packet_ptr -> nx_packet_append_ptr > packet_ptr -> nx_packet_data_end)
164     {
165         return(NX_OVERFLOW);
166     }
167 
168     /* Check for appropriate caller.  */
169     NX_THREADS_ONLY_CALLER_CHECKING
170 
171     /* Call the actual service. */
172     status = _nxd_ip_raw_packet_source_send(ip_ptr, packet_ptr, destination_ip, address_index, protocol, ttl, tos);
173 
174     /* Return completion status. */
175     return(status);
176 }
177 
178