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