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