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 /**   Internet Protocol (IP)                                              */
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_ip.h"
30 #include "nx_packet.h"
31 
32 /* Bring in externs for caller checking code.  */
33 
34 NX_CALLER_CHECKING_EXTERNS
35 
36 
37 /**************************************************************************/
38 /*                                                                        */
39 /*  FUNCTION                                               RELEASE        */
40 /*                                                                        */
41 /*    _nxe_ip_create                                      PORTABLE C      */
42 /*                                                           6.1          */
43 /*  AUTHOR                                                                */
44 /*                                                                        */
45 /*    Yuxin Zhou, Microsoft Corporation                                   */
46 /*                                                                        */
47 /*  DESCRIPTION                                                           */
48 /*                                                                        */
49 /*    This function checks for errors in the IP instance create           */
50 /*    function call.                                                      */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    ip_ptr                                Pointer to IP control block   */
55 /*    name                                  Name of this IP instance      */
56 /*    ip_address                            Internet address for this IP  */
57 /*    network_mask                          Network mask for IP address   */
58 /*    default_pool                          Default packet pool           */
59 /*    ip_link_driver                        User supplied IP link driver  */
60 /*    memory_ptr                            Pointer memory area for IP    */
61 /*    memory_size                           Size of IP memory area        */
62 /*    priority                              Priority of IP helper thread  */
63 /*    ip_control_block_size                 Size of NX_IP structure       */
64 /*                                                                        */
65 /*  OUTPUT                                                                */
66 /*                                                                        */
67 /*    status                                Completion status             */
68 /*                                                                        */
69 /*  CALLS                                                                 */
70 /*                                                                        */
71 /*    _nx_ip_create                         Actual IP instance create     */
72 /*                                            function                    */
73 /*    tx_thread_identify                    Get current thread pointer    */
74 /*    tx_thread_preemption_change           Change preemption for thread  */
75 /*                                                                        */
76 /*  CALLED BY                                                             */
77 /*                                                                        */
78 /*    Application                                                         */
79 /*                                                                        */
80 /*  RELEASE HISTORY                                                       */
81 /*                                                                        */
82 /*    DATE              NAME                      DESCRIPTION             */
83 /*                                                                        */
84 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
85 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
86 /*                                            resulting in version 6.1    */
87 /*                                                                        */
88 /**************************************************************************/
_nxe_ip_create(NX_IP * ip_ptr,CHAR * name,ULONG ip_address,ULONG network_mask,NX_PACKET_POOL * default_pool,VOID (* ip_link_driver)(struct NX_IP_DRIVER_STRUCT *),VOID * memory_ptr,ULONG memory_size,UINT priority,UINT ip_control_block_size)89 UINT  _nxe_ip_create(NX_IP *ip_ptr, CHAR *name, ULONG ip_address, ULONG network_mask,
90                      NX_PACKET_POOL *default_pool, VOID (*ip_link_driver)(struct NX_IP_DRIVER_STRUCT *),
91                      VOID *memory_ptr, ULONG memory_size, UINT priority, UINT ip_control_block_size)
92 {
93 
94 UINT       status;
95 UINT       old_threshold = 0;
96 NX_IP     *created_ip;
97 ULONG      created_count;
98 UCHAR     *end_stack;
99 TX_THREAD *current_thread;
100 
101 
102     /* Check for invalid input pointers.  */
103     if ((ip_ptr == NX_NULL) || (default_pool == NX_NULL) ||
104         (default_pool -> nx_packet_pool_id != NX_PACKET_POOL_ID) || (ip_link_driver == NX_NULL) ||
105         (memory_ptr == NX_NULL) || (ip_control_block_size != (UINT)sizeof(NX_IP)))
106     {
107         return(NX_PTR_ERROR);
108     }
109 
110     /* Check for a memory size error.  */
111     if (memory_size < TX_MINIMUM_STACK)
112     {
113         return(NX_SIZE_ERROR);
114     }
115 
116     /* Check the priority specified.  */
117     if (priority >= TX_MAX_PRIORITIES)
118     {
119         return(NX_OPTION_ERROR);
120     }
121 
122     /* Calculate the end of the stack area.  */
123     end_stack =  ((UCHAR *)memory_ptr) + (memory_size - 1);
124 
125     /* Pickup current thread pointer.  */
126     current_thread =  tx_thread_identify();
127 
128     /* Disable preemption temporarily.  */
129     if (current_thread)
130     {
131         tx_thread_preemption_change(current_thread, 0, &old_threshold);
132     }
133 
134     /* Loop to check for the IP instance already created.  */
135     created_ip =     _nx_ip_created_ptr;
136     created_count =  _nx_ip_created_count;
137     while (created_count--)
138     {
139 
140         /* Is the new ip already created?  */
141         /*lint -e{946} suppress pointer subtraction, since it is necessary. */
142         if ((ip_ptr == created_ip) ||
143             ((memory_ptr >= created_ip -> nx_ip_thread.tx_thread_stack_start) && (memory_ptr < created_ip -> nx_ip_thread.tx_thread_stack_end)) ||
144             ((((VOID *)end_stack)  >= created_ip -> nx_ip_thread.tx_thread_stack_start) && (((VOID *)end_stack)  < created_ip -> nx_ip_thread.tx_thread_stack_end)))
145         {
146 
147             /* Restore preemption.  */
148             if (current_thread)
149             {
150 
151                 /*lint -e{644} suppress variable might not be initialized, since "old_threshold" was initialized by previous tx_thread_preemption_change. */
152                 tx_thread_preemption_change(current_thread, old_threshold, &old_threshold);
153             }
154 
155             /* Duplicate ip created, return an error!  */
156             return(NX_PTR_ERROR);
157         }
158 
159         /* Move to next entry.  */
160         created_ip =  created_ip -> nx_ip_created_next;
161     }
162 
163     /* Restore preemption.  */
164     if (current_thread)
165     {
166 
167         /*lint -e{644} suppress variable might not be initialized, since "old_threshold" was initialized by previous tx_thread_preemption_change. */
168         tx_thread_preemption_change(current_thread, old_threshold, &old_threshold);
169     }
170 
171     /* Check for invalid IP address.  Note that Interface with DHCP enabled
172        would start with 0.0.0.0.  Therefore the 0 IP address is allowed. */
173     if ((ip_address != 0) &&
174         ((ip_address & NX_IP_CLASS_A_MASK) != NX_IP_CLASS_A_TYPE) &&
175         ((ip_address & NX_IP_CLASS_B_MASK) != NX_IP_CLASS_B_TYPE) &&
176         ((ip_address & NX_IP_CLASS_C_MASK) != NX_IP_CLASS_C_TYPE))
177     {
178         return(NX_IP_ADDRESS_ERROR);
179     }
180 
181     /* Check for appropriate caller.  */
182     NX_INIT_AND_THREADS_CALLER_CHECKING
183 
184     /* Call actual IP instance create function.  */
185     status =  _nx_ip_create(ip_ptr, name, ip_address, network_mask, default_pool, ip_link_driver,
186                             memory_ptr, memory_size, priority);
187 
188     /* Return completion status.  */
189     return(status);
190 }
191 
192