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