]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4ConfigDxe/NicIp4Variable.c
Fixed build failed.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4ConfigDxe / NicIp4Variable.c
1 /** @file
2 Routines used to operate the Ip4 configure variable.
3
4 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at<BR>
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Ip4Config.h"
16 #include "NicIp4Variable.h"
17
18 BOOLEAN mIp4ConfigVariableReclaimed = FALSE;
19
20 /**
21 Check whether the configure parameter is valid.
22
23 @param NicConfig The configure parameter to check
24
25 @return TRUE if the parameter is valid for the interface, otherwise FALSE.
26
27 **/
28 BOOLEAN
29 Ip4ConfigIsValid (
30 IN NIC_IP4_CONFIG_INFO *NicConfig
31 )
32 {
33 EFI_IP4_IPCONFIG_DATA *IpConfig;
34 IP4_ADDR Station;
35 IP4_ADDR Netmask;
36 IP4_ADDR Gateway;
37 UINT32 Index;
38
39 IpConfig = &NicConfig->Ip4Info;
40
41 if (NicConfig->Source == IP4_CONFIG_SOURCE_STATIC) {
42 //
43 // Validate that the addresses are unicast and mask
44 // is properly formated
45 //
46 Station = EFI_NTOHL (IpConfig->StationAddress);
47 Netmask = EFI_NTOHL (IpConfig->SubnetMask);
48
49 if ((Netmask == 0) || !IP4_IS_VALID_NETMASK (Netmask) ||
50 (Station == 0) || !NetIp4IsUnicast (Station, Netmask)) {
51 return FALSE;
52 }
53
54 //
55 // Validate that the next hops are on the connected network
56 // or that is a direct route (Gateway == 0).
57 //
58 for (Index = 0; Index < IpConfig->RouteTableSize; Index++) {
59 Gateway = EFI_NTOHL (IpConfig->RouteTable[Index].GatewayAddress);
60
61 if ((Gateway != 0) && (!IP4_NET_EQUAL (Station, Gateway, Netmask) ||
62 !NetIp4IsUnicast (Gateway, Netmask))) {
63 return FALSE;
64 }
65 }
66
67 return TRUE;
68 }
69
70 //
71 // return false if it is an unkown configure source. Valid
72 // sources are static and dhcp.
73 //
74 return (BOOLEAN) (NicConfig->Source == IP4_CONFIG_SOURCE_DHCP);
75 }
76
77
78
79 /**
80 Read the ip4 configure variable from the EFI variable.
81
82 @param Instance The IP4 CONFIG instance.
83
84 @return The IP4 configure read if it is there and is valid, otherwise NULL.
85
86 **/
87 NIC_IP4_CONFIG_INFO *
88 Ip4ConfigReadVariable (
89 IN IP4_CONFIG_INSTANCE *Instance
90 )
91 {
92 NIC_IP4_CONFIG_INFO *NicConfig;
93
94 GetVariable2 (Instance->MacString, &gEfiNicIp4ConfigVariableGuid, (VOID**)&NicConfig, NULL);
95 if (NicConfig != NULL) {
96 Ip4ConfigFixRouteTablePointer (&NicConfig->Ip4Info);
97 }
98
99 return NicConfig;
100 }
101
102 /**
103 Write the IP4 configure variable to the NVRAM. If Config
104 is NULL, remove the variable.
105
106 @param Instance The IP4 CONFIG instance.
107 @param NicConfig The IP4 configure data to write.
108
109 @retval EFI_SUCCESS The variable is written to the NVRam.
110 @retval Others Failed to write the variable.
111
112 **/
113 EFI_STATUS
114 Ip4ConfigWriteVariable (
115 IN IP4_CONFIG_INSTANCE *Instance,
116 IN NIC_IP4_CONFIG_INFO *NicConfig OPTIONAL
117 )
118 {
119 EFI_STATUS Status;
120
121 Status = gRT->SetVariable (
122 Instance->MacString,
123 &gEfiNicIp4ConfigVariableGuid,
124 IP4_CONFIG_VARIABLE_ATTRIBUTES,
125 (NicConfig == NULL) ? 0 : SIZEOF_NIC_IP4_CONFIG_INFO (NicConfig),
126 NicConfig
127 );
128
129 return Status;
130 }
131
132 /**
133 Check whether a NIC exist in the platform given its MAC address.
134
135 @param NicAddr The MAC address for the NIC to be checked.
136
137 @retval TRUE The NIC exist in the platform.
138 @retval FALSE The NIC doesn't exist in the platform.
139
140 **/
141 BOOLEAN
142 Ip4ConfigIsNicExist (
143 IN NIC_ADDR *NicAddr
144 )
145 {
146 EFI_STATUS Status;
147 EFI_HANDLE *HandleBuffer;
148 UINTN NumberOfHandles;
149 UINTN Index;
150 BOOLEAN Found;
151 UINTN AddrSize;
152 EFI_MAC_ADDRESS MacAddr;
153
154 //
155 // Locate Service Binding handles.
156 //
157 Status = gBS->LocateHandleBuffer (
158 ByProtocol,
159 &gEfiManagedNetworkServiceBindingProtocolGuid,
160 NULL,
161 &NumberOfHandles,
162 &HandleBuffer
163 );
164 if (EFI_ERROR (Status)) {
165 return FALSE;
166 }
167
168 Found = FALSE;
169 for (Index = 0; Index < NumberOfHandles; Index++) {
170 //
171 // Get MAC address.
172 //
173 AddrSize = 0;
174 Status = NetLibGetMacAddress (HandleBuffer[Index], &MacAddr, &AddrSize);
175 if (EFI_ERROR (Status)) {
176 Found = FALSE;
177 goto Exit;
178 }
179
180 if ((NicAddr->Len == AddrSize) && (CompareMem (NicAddr->MacAddr.Addr, MacAddr.Addr, AddrSize) == 0)) {
181 Found = TRUE;
182 goto Exit;
183 }
184 }
185
186 Exit:
187 FreePool (HandleBuffer);
188 return Found;
189 }
190
191 /**
192 Reclaim Ip4Config Variables for NIC which has been removed from the platform.
193
194 **/
195 VOID
196 Ip4ConfigReclaimVariable (
197 VOID
198 )
199 {
200 EFI_STATUS Status;
201 UINTN VariableNameSize;
202 CHAR16 *VariableName;
203 CHAR16 *CurrentVariableName;
204 EFI_GUID VendorGuid;
205 UINTN VariableNameBufferSize;
206 NIC_IP4_CONFIG_INFO *NicConfig;
207
208 //
209 // Check whether we need perform reclaim.
210 //
211 if (mIp4ConfigVariableReclaimed) {
212 return;
213 }
214 mIp4ConfigVariableReclaimed = TRUE;
215
216 //
217 // Get all Ip4Config Variable.
218 //
219 VariableNameSize = sizeof (CHAR16);
220 VariableName = AllocateZeroPool (VariableNameSize);
221 VariableNameBufferSize = VariableNameSize;
222
223 while (TRUE) {
224 Status = gRT->GetNextVariableName (
225 &VariableNameSize,
226 VariableName,
227 &VendorGuid
228 );
229
230 Check:
231 if (Status == EFI_BUFFER_TOO_SMALL) {
232 VariableName = ReallocatePool (VariableNameBufferSize, VariableNameSize, VariableName);
233 VariableNameBufferSize = VariableNameSize;
234 //
235 // Try again using the new buffer.
236 //
237 Status = gRT->GetNextVariableName (
238 &VariableNameSize,
239 VariableName,
240 &VendorGuid
241 );
242 }
243
244 if (EFI_ERROR (Status)) {
245 //
246 // No more variable available, finish search.
247 //
248 break;
249 }
250
251 //
252 // Check variable GUID.
253 //
254 if (!CompareGuid (&VendorGuid, &gEfiNicIp4ConfigVariableGuid)) {
255 continue;
256 }
257
258 GetVariable2 (VariableName, &gEfiNicIp4ConfigVariableGuid, (VOID**)&NicConfig, NULL);
259 if (NicConfig == NULL) {
260 break;
261 }
262
263 if (!Ip4ConfigIsNicExist (&NicConfig->NicAddr)) {
264 //
265 // No NIC found for this Ip4Config variable, remove it.
266 // Since we are in loop of GetNextVariableName(), we need move on to next
267 // Variable first and then delete current Variable.
268 //
269 CurrentVariableName = AllocateCopyPool (VariableNameSize, VariableName);
270 Status = gRT->GetNextVariableName (
271 &VariableNameSize,
272 VariableName,
273 &VendorGuid
274 );
275
276 gRT->SetVariable (
277 CurrentVariableName,
278 &gEfiNicIp4ConfigVariableGuid,
279 IP4_CONFIG_VARIABLE_ATTRIBUTES,
280 0,
281 NULL
282 );
283 FreePool (CurrentVariableName);
284
285 //
286 // We already get next variable, go to check it.
287 //
288 goto Check;
289 }
290 }
291
292 FreePool (VariableName);
293 }
294
295 /**
296 Fix the RouteTable pointer in an EFI_IP4_IPCONFIG_DATA structure.
297
298 The pointer is set to be immediately follow the ConfigData if there're entries
299 in the RouteTable. Otherwise it is set to NULL.
300
301 @param ConfigData The IP4 IP configure data.
302
303 **/
304 VOID
305 Ip4ConfigFixRouteTablePointer (
306 IN OUT EFI_IP4_IPCONFIG_DATA *ConfigData
307 )
308 {
309 //
310 // The memory used for route table entries must immediately follow
311 // the ConfigData and be not packed.
312 //
313 if (ConfigData->RouteTableSize > 0) {
314 ConfigData->RouteTable = (EFI_IP4_ROUTE_TABLE *) (ConfigData + 1);
315 } else {
316 ConfigData->RouteTable = NULL;
317 }
318 }
319