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 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_arp_hardware_address_find                       PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function searches for the specified IP address in the ARP      */
45 /*    lists.  If found, the associated hardware address is returned.      */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    ip_ptr                                IP instance pointer           */
50 /*    ip_address                            IP Address to search for      */
51 /*    physical_msw                          Physical address MSW pointer  */
52 /*    physical_lsw                          Physical address LSW pointer  */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    status                                Completion status             */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    tx_mutex_get                          Obtain protection mutex       */
61 /*    tx_mutex_put                          Release protection mutex      */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application Code                                                    */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
72 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
_nx_arp_hardware_address_find(NX_IP * ip_ptr,ULONG ip_address,ULONG * physical_msw,ULONG * physical_lsw)76 UINT  _nx_arp_hardware_address_find(NX_IP *ip_ptr, ULONG ip_address,
77                                     ULONG *physical_msw, ULONG *physical_lsw)
78 {
79 
80 #ifndef NX_DISABLE_IPV4
81 NX_ARP *arp_entry;
82 ULONG   count;
83 
84 #ifdef TX_ENABLE_EVENT_TRACE
85 TX_TRACE_BUFFER_ENTRY *trace_event;
86 ULONG                  trace_timestamp;
87 #endif
88 
89 
90     /* If trace is enabled, insert this event into the trace buffer.  */
91     NX_TRACE_IN_LINE_INSERT(NX_TRACE_ARP_HARDWARE_ADDRESS_FIND, ip_ptr, ip_address, 0, 0, NX_TRACE_ARP_EVENTS, &trace_event, &trace_timestamp);
92 
93     /* Obtain protection on this IP instance for access into the ARP static
94        list.  */
95     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
96 
97     /* Search the static list for a matching IP and hardware mapping.  */
98     arp_entry =  ip_ptr -> nx_ip_arp_static_list;
99     while (arp_entry)
100     {
101 
102         /* Determine if we have a match.  */
103         if ((arp_entry -> nx_arp_ip_address == ip_address) &&
104             (arp_entry -> nx_arp_physical_address_msw | arp_entry -> nx_arp_physical_address_lsw))
105         {
106 
107             /* Yes, we have found the ARP entry we are looking for.  */
108             break;
109         }
110         else
111         {
112 
113             /* Determine if we are at the end of the list.  */
114             if (arp_entry -> nx_arp_pool_next == ip_ptr -> nx_ip_arp_static_list)
115             {
116 
117                 /* Set the arp_entry to NULL to signify nothing was found and get
118                    out of the search loop.  */
119                 arp_entry =  NX_NULL;
120                 break;
121             }
122             else
123             {
124 
125                 /* Just move to the next ARP entry on the static list.  */
126                 arp_entry =  arp_entry -> nx_arp_pool_next;
127             }
128         }
129     }
130 
131     /* Determine if an entry has been found.  If so, we are finished and it needs to be
132        returned to the caller.  */
133     if (arp_entry)
134     {
135 
136         /* Store the hardware address in the return fields.  */
137         *physical_msw =  arp_entry -> nx_arp_physical_address_msw;
138         *physical_lsw =  arp_entry -> nx_arp_physical_address_lsw;
139 
140         /* Update the trace event with the status.  */
141         NX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, NX_TRACE_ARP_HARDWARE_ADDRESS_FIND, 0, 0, *physical_msw, *physical_lsw);
142 
143         /* Release the protection on the ARP list.  */
144         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
145 
146         /* Return status to the caller.  */
147         return(NX_SUCCESS);
148     }
149 
150     /* Otherwise, we need to search the ARP dynamic list for a match.  */
151     arp_entry =  ip_ptr -> nx_ip_arp_dynamic_list;
152     count =      1;
153     while (arp_entry)
154     {
155 
156         /* Determine if we have a match.  */
157         if ((arp_entry -> nx_arp_ip_address == ip_address) &&
158             (arp_entry -> nx_arp_physical_address_msw | arp_entry -> nx_arp_physical_address_lsw))
159         {
160 
161             /* Yes, we have found the ARP entry we are looking for.  */
162             break;
163         }
164         else
165         {
166 
167             /* Determine if we are at the end of the list of active ARP entries.  */
168             if (count >=  ip_ptr -> nx_ip_arp_dynamic_active_count)
169             {
170 
171                 /* Set the arp_entry to NULL to signify nothing was found and get
172                    out of the search loop.  */
173                 arp_entry =  NX_NULL;
174                 break;
175             }
176             else
177             {
178 
179                 /* Just move to the next ARP entry on the dynamic list.  */
180                 arp_entry =  arp_entry -> nx_arp_pool_next;
181 
182                 /* Increment the active count.  */
183                 count++;
184             }
185         }
186     }
187 
188     /* Determine if an entry has been found.  If so, we are finished and it needs to be
189        returned to the caller.  */
190     if (arp_entry)
191     {
192 
193         /* Store the hardware address in the return fields.  */
194         *physical_msw =  arp_entry -> nx_arp_physical_address_msw;
195         *physical_lsw =  arp_entry -> nx_arp_physical_address_lsw;
196 
197         /* Update the trace event with the status.  */
198         NX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, NX_TRACE_ARP_HARDWARE_ADDRESS_FIND, 0, 0, *physical_msw, *physical_lsw);
199 
200         /* Release the protection on the ARP list.  */
201         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
202 
203         /* Return status to the caller.  */
204         return(NX_SUCCESS);
205     }
206     else
207     {
208 
209         /* Release the protection on the ARP list.  */
210         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
211 
212         /* Return status to the caller.  */
213         return(NX_ENTRY_NOT_FOUND);
214     }
215 #else /* NX_DISABLE_IPV4  */
216     NX_PARAMETER_NOT_USED(ip_ptr);
217     NX_PARAMETER_NOT_USED(ip_address);
218     NX_PARAMETER_NOT_USED(physical_msw);
219     NX_PARAMETER_NOT_USED(physical_lsw);
220 
221     return(NX_NOT_SUPPORTED);
222 #endif /* !NX_DISABLE_IPV4  */
223 }
224 
225