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_ip_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 hardware address in the    */
45 /*    ARP lists.  If found, the associated IP address is returned.        */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    ip_ptr                                IP instance pointer           */
50 /*    ip_address                            IP Address return pointer     */
51 /*    physical_msw                          Physical address MSW          */
52 /*    physical_lsw                          Physical address LSW          */
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_ip_address_find(NX_IP * ip_ptr,ULONG * ip_address,ULONG physical_msw,ULONG physical_lsw)76 UINT  _nx_arp_ip_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_IP_ADDRESS_FIND, ip_ptr, 0, physical_msw, physical_lsw, 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 matching hardware mapping.  */
98     arp_entry =  ip_ptr -> nx_ip_arp_static_list;
99     while (arp_entry)
100     {
101 
102         /* Determine if we have a hardware address match.  */
103         if ((arp_entry -> nx_arp_physical_address_msw == physical_msw) &&
104             (arp_entry -> nx_arp_physical_address_lsw == physical_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 IP address in the return field.  */
137         *ip_address =  arp_entry -> nx_arp_ip_address;
138 
139         /* Update the trace event with the status.  */
140         NX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, NX_TRACE_ARP_IP_ADDRESS_FIND, 0, *ip_address, 0, 0);
141 
142         /* Release the protection on the ARP list.  */
143         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
144 
145         /* Return status to the caller.  */
146         return(NX_SUCCESS);
147     }
148 
149     /* Otherwise, we need to search the ARP dynamic list for a match.  */
150     arp_entry =  ip_ptr -> nx_ip_arp_dynamic_list;
151     count =      1;
152     while (arp_entry)
153     {
154 
155         /* Determine if we have a hardware address match.  */
156         if ((arp_entry -> nx_arp_physical_address_msw == physical_msw) &&
157             (arp_entry -> nx_arp_physical_address_lsw == physical_lsw))
158         {
159 
160             /* Yes, we have found the ARP entry we are looking for.  */
161             break;
162         }
163         else
164         {
165 
166             /* Determine if we are at the end of the list of active ARP entries.  */
167             if (count >=  ip_ptr -> nx_ip_arp_dynamic_active_count)
168             {
169 
170                 /* Set the arp_entry to NULL to signify nothing was found and get
171                    out of the search loop.  */
172                 arp_entry =  NX_NULL;
173                 break;
174             }
175             else
176             {
177 
178                 /* Just move to the next ARP entry on the dynamic list.  */
179                 arp_entry =  arp_entry -> nx_arp_pool_next;
180 
181                 /* Increment the active count.  */
182                 count++;
183             }
184         }
185     }
186 
187     /* Determine if an entry has been found.  If so, we are finished and it needs to be
188        returned to the caller.  */
189     if (arp_entry)
190     {
191 
192         /* Store the IP address in the return fields.  */
193         *ip_address =  arp_entry -> nx_arp_ip_address;
194 
195         /* Update the trace event with the status.  */
196         NX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, NX_TRACE_ARP_IP_ADDRESS_FIND, 0, *ip_address, 0, 0);
197 
198         /* Release the protection on the ARP list.  */
199         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
200 
201         /* Return status to the caller.  */
202         return(NX_SUCCESS);
203     }
204     else
205     {
206 
207         /* Release the protection on the ARP list.  */
208         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
209 
210         /* Return status to the caller.  */
211         return(NX_ENTRY_NOT_FOUND);
212     }
213 #else /* NX_DISABLE_IPV4  */
214     NX_PARAMETER_NOT_USED(ip_ptr);
215     NX_PARAMETER_NOT_USED(ip_address);
216     NX_PARAMETER_NOT_USED(physical_msw);
217     NX_PARAMETER_NOT_USED(physical_lsw);
218 
219     return(NX_NOT_SUPPORTED);
220 #endif /* !NX_DISABLE_IPV4  */
221 }
222 
223