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 /**   Address Resolution Protocol (ARP)                                   */
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_arp.h"
29 #include "nx_packet.h"
30 
31 #ifndef NX_DISABLE_IPV4
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_arp_probe_send                                  PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function builds an ARP Probe packet and calls the associated   */
45 /*    driver to send it out on the specified network interface.           */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    ip_ptr                                Pointer to IP instance        */
50 /*    interface_index                       IP Interface Index            */
51 /*    probe_address                         Probe address                 */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    NX_SUCCESS                            Successful completion status  */
56 /*    NX_NO_PACKET                          No packet available to send   */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _nx_packet_allocate                   Allocate a packet for the     */
61 /*                                            ARP Probe                   */
62 /*    [ip_link_driver]                      User supplied link driver     */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application Code                                                    */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
73 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*                                                                        */
76 /**************************************************************************/
_nx_arp_probe_send(NX_IP * ip_ptr,UINT interface_index,ULONG probe_address)77 UINT  _nx_arp_probe_send(NX_IP *ip_ptr, UINT interface_index, ULONG probe_address)
78 {
79 
80 NX_INTERFACE *nx_interface;
81 NX_PACKET    *request_ptr;
82 ULONG        *message_ptr;
83 NX_IP_DRIVER  driver_request;
84 
85     /* Allocate a packet to build the ARP Probe message in.  */
86 #ifdef NX_ENABLE_DUAL_PACKET_POOL
87     /* Allocate from auxiliary packet pool first. */
88     if (_nx_packet_allocate(ip_ptr -> nx_ip_auxiliary_packet_pool, &request_ptr, (NX_PHYSICAL_HEADER + NX_ARP_MESSAGE_SIZE), NX_NO_WAIT))
89     {
90         if (ip_ptr -> nx_ip_auxiliary_packet_pool != ip_ptr -> nx_ip_default_packet_pool)
91 #endif /* NX_ENABLE_DUAL_PACKET_POOL */
92         {
93             if (_nx_packet_allocate(ip_ptr -> nx_ip_default_packet_pool, &request_ptr, (NX_PHYSICAL_HEADER + NX_ARP_MESSAGE_SIZE), NX_NO_WAIT))
94             {
95 
96                 /* Error getting packet, so just get out!  */
97                 return(NX_NO_PACKET);
98             }
99         }
100 #ifdef NX_ENABLE_DUAL_PACKET_POOL
101         else
102         {
103 
104             /* Error getting packet, so just get out!  */
105             return(NX_NO_PACKET);
106         }
107     }
108 #endif /* NX_ENABLE_DUAL_PACKET_POOL */
109 
110     /* Add debug information. */
111     NX_PACKET_DEBUG(__FILE__, __LINE__, request_ptr);
112 
113     /* Get mutex protection.  */
114     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
115 
116     /* Set nx_interface.  */
117     nx_interface = &(ip_ptr -> nx_ip_interface[interface_index]);
118 
119     /* Store the probe address.  */
120     nx_interface -> nx_interface_ip_probe_address = probe_address;
121 
122     /* Stamp the packet with the outgoing interface information. */
123     /*lint -e{644} suppress variable might not be initialized, since "request_ptr" was initialized in _nx_packet_allocate. */
124     request_ptr -> nx_packet_address.nx_packet_interface_ptr = nx_interface;
125 
126 #ifndef NX_DISABLE_ARP_INFO
127     /* Increment the ARP requests sent count.  */
128     ip_ptr -> nx_ip_arp_requests_sent++;
129 #endif
130 
131     /* If trace is enabled, insert this event into the trace buffer.  */
132     NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_ARP_REQUEST_SEND, ip_ptr, probe_address, request_ptr, 0, NX_TRACE_INTERNAL_EVENTS, 0, 0);
133 
134     /* Build the ARP Probe packet.  */
135 
136     /* Setup the size of the ARP message.  */
137     request_ptr -> nx_packet_length =  NX_ARP_MESSAGE_SIZE;
138 
139     /* Setup the prepend pointer.  */
140     request_ptr -> nx_packet_prepend_ptr -= NX_ARP_MESSAGE_SIZE;
141 
142     /* Setup the pointer to the message area.  */
143     /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
144     message_ptr =  (ULONG *)request_ptr -> nx_packet_prepend_ptr;
145 
146     /* Write the Hardware type into the message.  */
147     *message_ptr =      (ULONG)(NX_ARP_HARDWARE_TYPE << 16) | (NX_ARP_PROTOCOL_TYPE);
148     *(message_ptr + 1) =  (ULONG)(NX_ARP_HARDWARE_SIZE << 24) | (NX_ARP_PROTOCOL_SIZE << 16) |
149         NX_ARP_OPTION_REQUEST;
150 
151     /*lint -e{613} suppress possible use of null pointer, since nx_interface must not be NULL.  */
152     *(message_ptr + 2) =  (ULONG)(nx_interface -> nx_interface_physical_address_msw << 16) |
153         (nx_interface -> nx_interface_physical_address_lsw >> 16);
154     /*lint -e{613} suppress possible use of null pointer, since nx_interface must not be NULL.  */
155     *(message_ptr + 3) =  (ULONG)(nx_interface -> nx_interface_physical_address_lsw << 16);
156     *(message_ptr + 4) =  (ULONG)0;
157     *(message_ptr + 5) =  (ULONG)0;
158     *(message_ptr + 6) =  (ULONG)probe_address;
159 
160     /* Endian swapping logic.  If NX_LITTLE_ENDIAN is specified, these macros will
161        swap the endian of the ARP message.  */
162     NX_CHANGE_ULONG_ENDIAN(*(message_ptr));
163     NX_CHANGE_ULONG_ENDIAN(*(message_ptr + 1));
164     NX_CHANGE_ULONG_ENDIAN(*(message_ptr + 2));
165     NX_CHANGE_ULONG_ENDIAN(*(message_ptr + 3));
166     NX_CHANGE_ULONG_ENDIAN(*(message_ptr + 4));
167     NX_CHANGE_ULONG_ENDIAN(*(message_ptr + 5));
168     NX_CHANGE_ULONG_ENDIAN(*(message_ptr + 6));
169 
170     /* Set up the driver request. */
171     driver_request.nx_ip_driver_ptr =                   ip_ptr;
172     driver_request.nx_ip_driver_command =               NX_LINK_ARP_SEND;
173     driver_request.nx_ip_driver_packet =                request_ptr;
174     driver_request.nx_ip_driver_physical_address_msw =  0xFFFFUL;
175     driver_request.nx_ip_driver_physical_address_lsw =  0xFFFFFFFFUL;
176     driver_request.nx_ip_driver_interface            =  nx_interface;
177 
178     /* If trace is enabled, insert this event into the trace buffer.  */
179     NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_IO_DRIVER_ARP_SEND, ip_ptr, request_ptr, request_ptr -> nx_packet_length, 0, NX_TRACE_INTERNAL_EVENTS, 0, 0);
180 
181     /* Add debug information. */
182     NX_PACKET_DEBUG(__FILE__, __LINE__, request_ptr);
183 
184     /* Send the ARP Probe packet to the driver.  */
185     /*lint -e{613} suppress possible use of null pointer, since nx_interface must not be NULL.  */
186     (nx_interface -> nx_interface_link_driver_entry)(&driver_request);
187 
188     /* Release mutex protection.  */
189     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
190 
191     /* Return a successful completion.  */
192     return(NX_SUCCESS);
193 }
194 #endif /* !NX_DISABLE_IPV4  */
195 
196