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_announce_send                               PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Yuxin Zhou, Microsoft Corporation                                   */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function builds an ARP Announce 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 /*                                                                        */
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 Announce                */
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_announce_send(NX_IP * ip_ptr,UINT interface_index)77 UINT  _nx_arp_announce_send(NX_IP *ip_ptr, UINT interface_index)
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 Announce 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     /* Stamp the packet with the outgoing interface information. */
120     /*lint -e{644} suppress variable might not be initialized, since "request_ptr" was initialized in _nx_packet_allocate. */
121     request_ptr -> nx_packet_address.nx_packet_interface_ptr = nx_interface;
122 
123 #ifndef NX_DISABLE_ARP_INFO
124     /* Increment the ARP requests sent count.  */
125     ip_ptr -> nx_ip_arp_requests_sent++;
126 #endif
127 
128     /* If trace is enabled, insert this event into the trace buffer.  */
129     NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_ARP_REQUEST_SEND, ip_ptr, nx_interface -> nx_interface_ip_address, request_ptr, 0, NX_TRACE_INTERNAL_EVENTS, 0, 0);
130 
131     /* Build the ARP Announce packet.  */
132 
133     /* Setup the size of the ARP message.  */
134     request_ptr -> nx_packet_length =  NX_ARP_MESSAGE_SIZE;
135 
136     /* Setup the prepend pointer.  */
137     request_ptr -> nx_packet_prepend_ptr -= NX_ARP_MESSAGE_SIZE;
138 
139     /* Setup the pointer to the message area.  */
140     /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
141     message_ptr =  (ULONG *)request_ptr -> nx_packet_prepend_ptr;
142 
143     /* Write the Hardware type into the message.  */
144     *message_ptr =        (ULONG)(NX_ARP_HARDWARE_TYPE << 16) | (NX_ARP_PROTOCOL_TYPE);
145     *(message_ptr+1) =    (ULONG)(NX_ARP_HARDWARE_SIZE << 24) | (NX_ARP_PROTOCOL_SIZE << 16) |
146                           NX_ARP_OPTION_REQUEST;
147 
148     /*lint -e{613} suppress possible use of null pointer, since nx_interface must not be NULL.  */
149     *(message_ptr + 2) =  (ULONG)(nx_interface -> nx_interface_physical_address_msw << 16) |
150                           (nx_interface -> nx_interface_physical_address_lsw >> 16);
151     /*lint -e{613} suppress possible use of null pointer, since nx_interface must not be NULL.  */
152     *(message_ptr + 3) =  (ULONG)(nx_interface -> nx_interface_physical_address_lsw << 16) |
153                           (nx_interface -> nx_interface_ip_address >> 16);
154     /*lint -e{613} suppress possible use of null pointer, since nx_interface must not be NULL.  */
155     *(message_ptr + 4) =  (ULONG)(nx_interface -> nx_interface_ip_address << 16);
156     *(message_ptr + 5) =  (ULONG)0;
157     *(message_ptr + 6) =  (ULONG)nx_interface -> nx_interface_ip_address;
158 
159     /* Endian swapping logic.  If NX_LITTLE_ENDIAN is specified, these macros will
160        swap the endian of the ARP message.  */
161     NX_CHANGE_ULONG_ENDIAN(*(message_ptr));
162     NX_CHANGE_ULONG_ENDIAN(*(message_ptr + 1));
163     NX_CHANGE_ULONG_ENDIAN(*(message_ptr + 2));
164     NX_CHANGE_ULONG_ENDIAN(*(message_ptr + 3));
165     NX_CHANGE_ULONG_ENDIAN(*(message_ptr + 4));
166     NX_CHANGE_ULONG_ENDIAN(*(message_ptr + 5));
167     NX_CHANGE_ULONG_ENDIAN(*(message_ptr + 6));
168 
169     /* Set up the driver request. */
170     driver_request.nx_ip_driver_ptr =                   ip_ptr;
171     driver_request.nx_ip_driver_command =               NX_LINK_ARP_SEND;
172     driver_request.nx_ip_driver_packet =                request_ptr;
173     driver_request.nx_ip_driver_physical_address_msw =  0xFFFFUL;
174     driver_request.nx_ip_driver_physical_address_lsw =  0xFFFFFFFFUL;
175     driver_request.nx_ip_driver_interface            =  nx_interface;
176 
177     /* If trace is enabled, insert this event into the trace buffer.  */
178     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);
179 
180     /* Add debug information. */
181     NX_PACKET_DEBUG(__FILE__, __LINE__, request_ptr);
182 
183     /* Send the ARP Announce packet to the driver.  */
184     /*lint -e{613} suppress possible use of null pointer, since nx_interface must not be NULL.  */
185     (nx_interface -> nx_interface_link_driver_entry)(&driver_request);
186 
187     /* Release mutex protection.  */
188     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
189 
190     /* Return a successful completion.  */
191     return(NX_SUCCESS);
192 }
193 #endif /* !NX_DISABLE_IPV4  */
194 
195