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 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _nx_arp_enable                                      PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function enables the ARP management component for the          */
44 /*    specified IP instance.                                              */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    ip_ptr                                IP instance pointer           */
49 /*    arp_cache_memory                      Start of ARP cache memory     */
50 /*    arp_cache_size                        Size in bytes of cache memory */
51 /*    memset                                Set the memory                */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                                Completion status             */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    None                                                                */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application Code                                                    */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
70 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*                                                                        */
73 /**************************************************************************/
_nx_arp_enable(NX_IP * ip_ptr,VOID * arp_cache_memory,ULONG arp_cache_size)74 UINT  _nx_arp_enable(NX_IP *ip_ptr, VOID *arp_cache_memory, ULONG arp_cache_size)
75 {
76 
77 #ifndef NX_DISABLE_IPV4
78 ULONG   i;
79 ULONG   arp_entries;
80 NX_ARP *entry_ptr;
81 
82 
83     /* If trace is enabled, insert this event into the trace buffer.  */
84     NX_TRACE_IN_LINE_INSERT(NX_TRACE_ARP_ENABLE, ip_ptr, arp_cache_memory, arp_cache_size, 0, NX_TRACE_ARP_EVENTS, 0, 0);
85 
86     /* Clear the entire ARP cache.  */
87     memset((void *)arp_cache_memory, 0, arp_cache_size);
88 
89     /* Pickup starting address of ARP entry array.  */
90     entry_ptr =  (NX_ARP *)arp_cache_memory;
91 
92     /* Determine how many ARP entries will fit in this cache area.  */
93     arp_entries =  arp_cache_size / sizeof(NX_ARP);
94 
95     /* Initialize the forward pointers of available ARP entries.  */
96     for (i = 0; i < (arp_entries - 1); i++)
97     {
98         /* Setup each entry to point to the next entry.  */
99         entry_ptr -> nx_arp_pool_next =  entry_ptr + 1;
100         entry_ptr++;
101     }
102 
103     /* The entry now points to the last entry in the ARP array.  Set its
104        next pointer to the first entry.  */
105     entry_ptr -> nx_arp_pool_next =  (NX_ARP *)arp_cache_memory;
106 
107     /* Initialize the backward pointers of available ARP entries.  */
108     for (i = 0; i < (arp_entries - 1); i++)
109     {
110         /* Setup each entry to point to the previous entry.  */
111         entry_ptr -> nx_arp_pool_previous =  entry_ptr - 1;
112         entry_ptr--;
113     }
114 
115     /* The entry now points to the first entry, set the previous pointer
116        to the last entry.  */
117     entry_ptr -> nx_arp_pool_previous =  (entry_ptr + (arp_entries - 1));
118 
119     /* At this point, everything is okay in the ARP enable call.. populate the
120        information in the IP structure.  */
121 
122     /* Setup the list head pointers in the IP instance.  At first all ARP
123        entries are associated with the dynamic ARP list.  The static ARP list
124        is NULL until static ARP entry calls are made.  */
125     ip_ptr -> nx_ip_arp_static_list =   NX_NULL;
126     ip_ptr -> nx_ip_arp_dynamic_list =  (NX_ARP *)arp_cache_memory;
127 
128     /* Store the initial ARP cache information in the IP control block.  */
129     ip_ptr -> nx_ip_arp_cache_memory  =  arp_cache_memory;
130     ip_ptr -> nx_ip_arp_total_entries =  arp_entries;
131 
132     /* Setup the ARP periodic update routine.  */
133     ip_ptr -> nx_ip_arp_periodic_update =  _nx_arp_periodic_update;
134 
135     /* Setup the ARP queue process routine.  */
136     ip_ptr -> nx_ip_arp_queue_process =  _nx_arp_queue_process;
137 
138     /* Setup the ARP send packet routine.  */
139     ip_ptr -> nx_ip_arp_packet_send =  _nx_arp_packet_send;
140 
141     /* Setup the ARP allocate service request pointer.  */
142     ip_ptr -> nx_ip_arp_allocate =  _nx_arp_entry_allocate;
143 
144     /* Return successful completion.  */
145     return(NX_SUCCESS);
146 #else /* NX_DISABLE_IPV4  */
147     NX_PARAMETER_NOT_USED(ip_ptr);
148     NX_PARAMETER_NOT_USED(arp_cache_memory);
149     NX_PARAMETER_NOT_USED(arp_cache_size);
150 
151     return(NX_NOT_SUPPORTED);
152 #endif /* !NX_DISABLE_IPV4  */
153 }
154 
155