]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/Network/Ip4ConfigDxe/NicIp4Variable.c
Update the copyright notice format
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4ConfigDxe / NicIp4Variable.c
... / ...
CommitLineData
1/** @file\r
2 Routines used to operate the Ip4 configure variable.\r
3\r
4Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at<BR>\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "NicIp4Variable.h"\r
16\r
17\r
18/**\r
19 Check whether the configure parameter is valid.\r
20\r
21 @param NicConfig The configure parameter to check\r
22\r
23 @return TRUE if the parameter is valid for the interface, otherwise FALSE.\r
24\r
25**/\r
26BOOLEAN\r
27Ip4ConfigIsValid (\r
28 IN NIC_IP4_CONFIG_INFO *NicConfig\r
29 )\r
30{\r
31 EFI_IP4_IPCONFIG_DATA *IpConfig;\r
32 IP4_ADDR Station;\r
33 IP4_ADDR Netmask;\r
34 IP4_ADDR Gateway;\r
35 UINT32 Index;\r
36\r
37 IpConfig = &NicConfig->Ip4Info;\r
38\r
39 if (NicConfig->Source == IP4_CONFIG_SOURCE_STATIC) {\r
40 //\r
41 // Validate that the addresses are unicast and mask\r
42 // is properly formated\r
43 //\r
44 Station = EFI_NTOHL (IpConfig->StationAddress);\r
45 Netmask = EFI_NTOHL (IpConfig->SubnetMask);\r
46\r
47 if ((Netmask == 0) || !IP4_IS_VALID_NETMASK (Netmask) ||\r
48 (Station == 0) || !NetIp4IsUnicast (Station, Netmask)) {\r
49 return FALSE;\r
50 }\r
51\r
52 //\r
53 // Validate that the next hops are on the connected network\r
54 // or that is a direct route (Gateway == 0).\r
55 //\r
56 for (Index = 0; Index < IpConfig->RouteTableSize; Index++) {\r
57 Gateway = EFI_NTOHL (IpConfig->RouteTable[Index].GatewayAddress);\r
58\r
59 if ((Gateway != 0) && (!IP4_NET_EQUAL (Station, Gateway, Netmask) ||\r
60 !NetIp4IsUnicast (Gateway, Netmask))) {\r
61 return FALSE;\r
62 }\r
63 }\r
64\r
65 return TRUE;\r
66 }\r
67\r
68 //\r
69 // return false if it is an unkown configure source. Valid\r
70 // sources are static and dhcp.\r
71 //\r
72 return (BOOLEAN) (NicConfig->Source == IP4_CONFIG_SOURCE_DHCP);\r
73}\r
74\r
75\r
76\r
77/**\r
78 Read the ip4 configure variable from the EFI variable.\r
79\r
80 None\r
81\r
82 @return The IP4 configure read if it is there and is valid, otherwise NULL\r
83\r
84**/\r
85IP4_CONFIG_VARIABLE *\r
86Ip4ConfigReadVariable (\r
87 VOID\r
88 )\r
89{\r
90 IP4_CONFIG_VARIABLE *Variable;\r
91 EFI_STATUS Status;\r
92 UINTN Size;\r
93 UINT16 CheckSum;\r
94\r
95 //\r
96 // Get the size of variable, then allocate a buffer to read the variable.\r
97 //\r
98 Size = 0;\r
99 Variable = NULL;\r
100 Status = gRT->GetVariable (\r
101 EFI_NIC_IP4_CONFIG_VARIABLE,\r
102 &gEfiNicIp4ConfigVariableGuid,\r
103 NULL,\r
104 &Size,\r
105 NULL\r
106 );\r
107\r
108 if (Status != EFI_BUFFER_TOO_SMALL) {\r
109 return NULL;\r
110 }\r
111\r
112 if (Size < sizeof (IP4_CONFIG_VARIABLE)) {\r
113 goto REMOVE_VARIABLE;\r
114 }\r
115\r
116 Variable = AllocatePool (Size);\r
117\r
118 if (Variable == NULL) {\r
119 return NULL;\r
120 }\r
121\r
122 Status = gRT->GetVariable (\r
123 EFI_NIC_IP4_CONFIG_VARIABLE,\r
124 &gEfiNicIp4ConfigVariableGuid,\r
125 NULL,\r
126 &Size,\r
127 Variable\r
128 );\r
129\r
130 if (EFI_ERROR (Status)) {\r
131 goto ON_ERROR;\r
132 }\r
133\r
134 //\r
135 // Verify the checksum, variable size and count\r
136 //\r
137 CheckSum = (UINT16) (~NetblockChecksum ((UINT8 *) Variable, (UINT32)Size));\r
138\r
139 if ((CheckSum != 0) || (Size != Variable->Len)) {\r
140 goto REMOVE_VARIABLE;\r
141 }\r
142\r
143 if ((Variable->Count < 1) || (Variable->Count > MAX_IP4_CONFIG_IN_VARIABLE)) {\r
144 goto REMOVE_VARIABLE;\r
145 }\r
146\r
147 return Variable;\r
148\r
149REMOVE_VARIABLE:\r
150 Ip4ConfigWriteVariable (NULL);\r
151\r
152ON_ERROR:\r
153 if (Variable != NULL) {\r
154 FreePool (Variable);\r
155 }\r
156\r
157 return NULL;\r
158}\r
159\r
160\r
161/**\r
162 Write the IP4 configure variable to the NVRAM. If Config\r
163 is NULL, remove the variable.\r
164\r
165 @param Config The IP4 configure data to write\r
166\r
167 @retval EFI_SUCCESS The variable is written to the NVRam\r
168 @retval Others Failed to write the variable.\r
169\r
170**/\r
171EFI_STATUS\r
172Ip4ConfigWriteVariable (\r
173 IN IP4_CONFIG_VARIABLE *Config OPTIONAL\r
174 )\r
175{\r
176 EFI_STATUS Status;\r
177\r
178 Status = gRT->SetVariable (\r
179 EFI_NIC_IP4_CONFIG_VARIABLE,\r
180 &gEfiNicIp4ConfigVariableGuid,\r
181 IP4_CONFIG_VARIABLE_ATTRIBUTES,\r
182 (Config == NULL) ? 0 : Config->Len,\r
183 Config\r
184 );\r
185\r
186 return Status;\r
187}\r
188\r
189\r
190/**\r
191 Locate the IP4 configure parameters from the variable.If a\r
192 configuration is found, copy it to a newly allocated block\r
193 of memory to avoid the alignment problem. Caller should\r
194 release the memory after use.\r
195\r
196 @param Variable The IP4 configure variable to search in\r
197 @param NicAddr The interface address to check\r
198\r
199 @return The point to the NIC's IP4 configure info if it is found\r
200 in the IP4 variable, otherwise NULL.\r
201\r
202**/\r
203NIC_IP4_CONFIG_INFO *\r
204Ip4ConfigFindNicVariable (\r
205 IN IP4_CONFIG_VARIABLE *Variable,\r
206 IN NIC_ADDR *NicAddr\r
207 )\r
208{\r
209 NIC_IP4_CONFIG_INFO Temp;\r
210 NIC_IP4_CONFIG_INFO *Config;\r
211 UINT32 Index;\r
212 UINT8 *Cur;\r
213 UINT32 Len;\r
214\r
215 Cur = (UINT8*)&Variable->ConfigInfo;\r
216\r
217 for (Index = 0; Index < Variable->Count; Index++) {\r
218 //\r
219 // Copy the data to Temp to avoid the alignment problems\r
220 //\r
221 CopyMem (&Temp, Cur, sizeof (NIC_IP4_CONFIG_INFO));\r
222 Len = SIZEOF_NIC_IP4_CONFIG_INFO (&Temp);\r
223\r
224 //\r
225 // Found the matching configuration parameters, allocate\r
226 // a block of memory then copy it out.\r
227 //\r
228 if (NIC_ADDR_EQUAL (&Temp.NicAddr, NicAddr)) {\r
229 Config = AllocatePool (Len);\r
230\r
231 if (Config == NULL) {\r
232 return NULL;\r
233 }\r
234\r
235 CopyMem (Config, Cur, Len);\r
236 Ip4ConfigFixRouteTablePointer (&Config->Ip4Info);\r
237 return Config;\r
238 }\r
239\r
240 Cur += Len;\r
241 }\r
242\r
243 return NULL;\r
244}\r
245\r
246\r
247/**\r
248 Modify the configuration parameter for the NIC in the variable.\r
249 If Config is NULL, old configuration will be remove from the new\r
250 variable. Otherwise, append it or replace the old one.\r
251\r
252 @param Variable The IP4 variable to change\r
253 @param NicAddr The interface to search\r
254 @param Config The new configuration parameter (NULL to remove the old)\r
255\r
256 @return The new IP4_CONFIG_VARIABLE variable if the new variable has at\r
257 least one NIC configure and no EFI_OUT_OF_RESOURCES failure.\r
258 Return NULL either because failed to locate memory for new variable\r
259 or the only NIC configure is removed from the Variable.\r
260\r
261**/\r
262IP4_CONFIG_VARIABLE *\r
263Ip4ConfigModifyVariable (\r
264 IN IP4_CONFIG_VARIABLE *Variable OPTIONAL,\r
265 IN NIC_ADDR *NicAddr,\r
266 IN NIC_IP4_CONFIG_INFO *Config OPTIONAL\r
267 )\r
268{\r
269 NIC_IP4_CONFIG_INFO Temp;\r
270 NIC_IP4_CONFIG_INFO *Old;\r
271 IP4_CONFIG_VARIABLE *NewVar;\r
272 UINT32 Len;\r
273 UINT32 TotalLen;\r
274 UINT32 Count;\r
275 UINT8 *Next;\r
276 UINT8 *Cur;\r
277 UINT32 Index;\r
278\r
279 ASSERT ((Variable != NULL) || (Config != NULL));\r
280\r
281 //\r
282 // Compute the total length\r
283 //\r
284 if (Variable != NULL) {\r
285 //\r
286 // Variable != NULL, then Config can be NULL or not. and so is\r
287 // the Old. If old configure exists, it is removed from the\r
288 // Variable. New configure is append to the variable.\r
289 //\r
290 //\r
291 Count = Variable->Count;\r
292 Cur = (UINT8 *)&Variable->ConfigInfo;\r
293 TotalLen = Variable->Len;\r
294\r
295 Old = Ip4ConfigFindNicVariable (Variable, NicAddr);\r
296\r
297 if (Old != NULL) {\r
298 TotalLen -= SIZEOF_NIC_IP4_CONFIG_INFO (Old);\r
299 FreePool (Old);\r
300 }\r
301\r
302 if (Config != NULL) {\r
303 TotalLen += SIZEOF_NIC_IP4_CONFIG_INFO (Config);\r
304 }\r
305\r
306 //\r
307 // Return NULL if the only NIC_IP4_CONFIG_INFO is being removed.\r
308 //\r
309 if (TotalLen < sizeof (IP4_CONFIG_VARIABLE)) {\r
310 return NULL;\r
311 }\r
312\r
313 } else {\r
314 //\r
315 // Variable == NULL and Config != NULL, Create a new variable with\r
316 // this NIC configure.\r
317 //\r
318 Count = 0;\r
319 Cur = NULL;\r
320 TotalLen = sizeof (IP4_CONFIG_VARIABLE) - sizeof (NIC_IP4_CONFIG_INFO)\r
321 + SIZEOF_NIC_IP4_CONFIG_INFO (Config);\r
322 }\r
323\r
324 ASSERT (TotalLen >= sizeof (IP4_CONFIG_VARIABLE));\r
325\r
326 NewVar = AllocateZeroPool (TotalLen);\r
327\r
328 if (NewVar == NULL) {\r
329 return NULL;\r
330 }\r
331\r
332 NewVar->Len = TotalLen;\r
333\r
334 //\r
335 // Copy the other configure parameters from the old variable\r
336 //\r
337 Next = (UINT8 *)&NewVar->ConfigInfo;\r
338\r
339 for (Index = 0; Index < Count; Index++) {\r
340 CopyMem (&Temp, Cur, sizeof (NIC_IP4_CONFIG_INFO));\r
341 Len = SIZEOF_NIC_IP4_CONFIG_INFO (&Temp);\r
342\r
343 if (!NIC_ADDR_EQUAL (&Temp.NicAddr, NicAddr)) {\r
344 CopyMem (Next, Cur, Len);\r
345 Next += Len;\r
346 NewVar->Count++;\r
347 }\r
348\r
349 Cur += Len;\r
350 }\r
351\r
352 //\r
353 // Append the new configure if it isn't NULL.\r
354 //\r
355 Len = 0;\r
356\r
357 if (Config != NULL) {\r
358 Len = SIZEOF_NIC_IP4_CONFIG_INFO (Config);\r
359\r
360 CopyMem (Next, Config, Len);\r
361 NewVar->Count++;\r
362 }\r
363\r
364 ASSERT (Next + Len == (UINT8 *) NewVar + TotalLen);\r
365\r
366 NewVar->CheckSum = (UINT16) (~NetblockChecksum ((UINT8 *) NewVar, TotalLen));\r
367 return NewVar;\r
368}\r
369\r
370/**\r
371 Fix the RouteTable pointer in an EFI_IP4_IPCONFIG_DATA structure. \r
372 \r
373 The pointer is set to be immediately follow the ConfigData if there're entries\r
374 in the RouteTable. Otherwise it is set to NULL.\r
375 \r
376 @param ConfigData The IP4 IP configure data.\r
377\r
378**/\r
379VOID\r
380Ip4ConfigFixRouteTablePointer (\r
381 IN OUT EFI_IP4_IPCONFIG_DATA *ConfigData\r
382 )\r
383{\r
384 //\r
385 // The memory used for route table entries must immediately follow \r
386 // the ConfigData and be not packed.\r
387 //\r
388 if (ConfigData->RouteTableSize > 0) {\r
389 ConfigData->RouteTable = (EFI_IP4_ROUTE_TABLE *) (ConfigData + 1);\r
390 } else {\r
391 ConfigData->RouteTable = NULL;\r
392 }\r
393}\r
394\r