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 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _nx_ip_raw_packet_send                              PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function checks for raw packet send enabled on the input IP    */
44 /*    instance, then finds a valid interface for sending the packet. If   */
45 /*    those requirements are met it sends a raw IP packet out the         */
46 /*    appropriate interface.                                              */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    ip_ptr                                Pointer to IP control block   */
51 /*    packet_ptr                            Pointer to packet to send     */
52 /*    destination_ip                        Destination IP address        */
53 /*    type_of_service                       Type of service for packet    */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    status                                Completion status             */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _nx_ip_route_find                     Find out-going interface and  */
62 /*                                            next hop                    */
63 /*    nx_ip_packet_send                     Core IP packet send service   */
64 /*    tx_mutex_get                          Get protection mutex          */
65 /*    tx_mutex_put                          Put protection mutex          */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    Application                                                         */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
76 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*                                                                        */
79 /**************************************************************************/
_nx_ip_raw_packet_send(NX_IP * ip_ptr,NX_PACKET * packet_ptr,ULONG destination_ip,ULONG type_of_service)80 UINT  _nx_ip_raw_packet_send(NX_IP *ip_ptr, NX_PACKET *packet_ptr,
81                              ULONG destination_ip, ULONG type_of_service)
82 {
83 
84 #ifndef NX_DISABLE_IPV4
85 ULONG next_hop_address = NX_NULL;
86 
87     /* Add debug information. */
88     NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);
89 
90     /* If trace is enabled, insert this event into the trace buffer.  */
91     NX_TRACE_IN_LINE_INSERT(NX_TRACE_IP_RAW_PACKET_SEND, ip_ptr, destination_ip, type_of_service, packet_ptr, NX_TRACE_IP_EVENTS, 0, 0);
92 
93     /* Get mutex protection.  */
94     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
95 
96     /* Make sure a valid interface exists for the destination. */
97     if (_nx_ip_route_find(ip_ptr, destination_ip, &packet_ptr -> nx_packet_address.nx_packet_interface_ptr, &next_hop_address) != NX_SUCCESS)
98     {
99 
100         /* Release protection. */
101         tx_mutex_put(&ip_ptr -> nx_ip_protection);
102 
103         /* There is not; return the error status. */
104         return(NX_IP_ADDRESS_ERROR);
105     }
106     else
107     {
108 
109         /* Yes, a valid interface exists. Send the packet!  */
110         /*lint -e{644} suppress variable might not be initialized, since "next_hop_address" was initialized as long as return value is NX_SUCCESS. */
111         _nx_ip_packet_send(ip_ptr, packet_ptr, destination_ip, type_of_service, NX_IP_TIME_TO_LIVE, NX_IP_RAW, NX_FRAGMENT_OKAY, next_hop_address);
112     }
113 
114     /* Release mutex protection.  */
115     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
116 
117     /* Return a successful status!  */
118     return(NX_SUCCESS);
119 #else /* NX_DISABLE_IPV4  */
120     NX_PARAMETER_NOT_USED(ip_ptr);
121     NX_PARAMETER_NOT_USED(packet_ptr);
122     NX_PARAMETER_NOT_USED(destination_ip);
123     NX_PARAMETER_NOT_USED(type_of_service);
124 
125     return(NX_NOT_SUPPORTED);
126 #endif /* !NX_DISABLE_IPV4  */
127 }
128 
129