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