]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Config2Impl.c
MdeModulePkg/PciBusDxe: Fix IA32 build failure
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Config2Impl.c
CommitLineData
1f6729ff 1/** @file\r
2 The implementation of EFI IPv4 Configuration II Protocol.\r
3\r
d52f9163 4 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>\r
4f0f2316 5 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r
1f6729ff 6\r
7 This program and the accompanying materials\r
8 are licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php.\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include "Ip4Impl.h"\r
18\r
19LIST_ENTRY mIp4Config2InstanceList = {&mIp4Config2InstanceList, &mIp4Config2InstanceList};\r
20\r
21/**\r
22 The event process routine when the DHCPv4 service binding protocol is installed\r
23 in the system.\r
24\r
25 @param[in] Event Not used.\r
26 @param[in] Context Pointer to the IP4 config2 instance data.\r
27\r
28**/\r
29VOID\r
30EFIAPI\r
31Ip4Config2OnDhcp4SbInstalled (\r
32 IN EFI_EVENT Event,\r
33 IN VOID *Context\r
34 );\r
35\r
36/**\r
37 Destroy the Dhcp4 child in IP4_CONFIG2_INSTANCE and release the resources.\r
38\r
39 @param[in, out] Instance The buffer of IP4 config2 instance to be freed.\r
40\r
41 @retval EFI_SUCCESS The child was successfully destroyed.\r
42 @retval Others Failed to destroy the child.\r
43\r
44**/\r
45EFI_STATUS\r
46Ip4Config2DestroyDhcp4 (\r
47 IN OUT IP4_CONFIG2_INSTANCE *Instance\r
48 )\r
49{\r
50 IP4_SERVICE *IpSb;\r
51 EFI_STATUS Status;\r
52 EFI_DHCP4_PROTOCOL *Dhcp4;\r
53\r
54 Dhcp4 = Instance->Dhcp4;\r
55 ASSERT (Dhcp4 != NULL);\r
56\r
57 Dhcp4->Stop (Dhcp4);\r
58 Dhcp4->Configure (Dhcp4, NULL);\r
59 Instance->Dhcp4 = NULL;\r
60\r
61 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
62\r
63 //\r
64 // Close DHCPv4 protocol and destroy the child.\r
65 //\r
66 Status = gBS->CloseProtocol (\r
67 Instance->Dhcp4Handle,\r
68 &gEfiDhcp4ProtocolGuid,\r
69 IpSb->Image,\r
70 IpSb->Controller\r
71 );\r
72 if (EFI_ERROR (Status)) {\r
73 return Status;\r
74 }\r
75\r
76 Status = NetLibDestroyServiceChild (\r
77 IpSb->Controller,\r
78 IpSb->Image,\r
79 &gEfiDhcp4ServiceBindingProtocolGuid,\r
80 Instance->Dhcp4Handle\r
81 );\r
82\r
83 Instance->Dhcp4Handle = NULL;\r
84\r
85 return Status; \r
86}\r
87\r
1f6729ff 88/**\r
89 Update the current policy to NewPolicy. During the transition\r
90 period, the default router list\r
91 and address list in all interfaces will be released.\r
92\r
93 @param[in] IpSb The IP4 service binding instance.\r
94 @param[in] NewPolicy The new policy to be updated to.\r
95\r
96**/\r
97VOID\r
98Ip4Config2OnPolicyChanged (\r
99 IN IP4_SERVICE *IpSb,\r
100 IN EFI_IP4_CONFIG2_POLICY NewPolicy\r
101 )\r
102{\r
103 IP4_INTERFACE *IpIf;\r
104 IP4_ROUTE_TABLE *RouteTable;\r
105\r
106 //\r
107 // Currently there are only two policies: static and dhcp. Regardless of\r
108 // what transition is going on, i.e., static -> dhcp and dhcp ->\r
109 // static, we have to free default router table and all addresses.\r
110 //\r
111\r
112 if (IpSb->DefaultInterface != NULL) {\r
113 if (IpSb->DefaultRouteTable != NULL) {\r
114 Ip4FreeRouteTable (IpSb->DefaultRouteTable);\r
115 IpSb->DefaultRouteTable = NULL; \r
116 }\r
117\r
118 Ip4CancelReceive (IpSb->DefaultInterface);\r
119\r
120 Ip4FreeInterface (IpSb->DefaultInterface, NULL);\r
121 IpSb->DefaultInterface = NULL;\r
122 }\r
123\r
124 Ip4CleanAssembleTable (&IpSb->Assemble);\r
125\r
126 //\r
127 // Create new default interface and route table.\r
128 // \r
129 IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);\r
130 if (IpIf == NULL) {\r
131 return ;\r
132 }\r
133\r
134 RouteTable = Ip4CreateRouteTable ();\r
135 if (RouteTable == NULL) {\r
136 Ip4FreeInterface (IpIf, NULL);\r
137 return ;\r
138 }\r
139 \r
140 IpSb->DefaultInterface = IpIf;\r
141 InsertHeadList (&IpSb->Interfaces, &IpIf->Link);\r
142 IpSb->DefaultRouteTable = RouteTable;\r
143 Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb);\r
144\r
5cb90e14 145 if (IpSb->State == IP4_SERVICE_CONFIGED || IpSb->State == IP4_SERVICE_STARTED) {\r
1f6729ff 146 IpSb->State = IP4_SERVICE_UNSTARTED;\r
147 }\r
148\r
149 //\r
150 // Start the dhcp configuration.\r
151 //\r
152 if (NewPolicy == Ip4Config2PolicyDhcp) {\r
153 Ip4StartAutoConfig (&IpSb->Ip4Config2Instance);\r
154 }\r
155\r
156}\r
157\r
158/**\r
159 Signal the registered event. It is the callback routine for NetMapIterate.\r
160\r
161 @param[in] Map Points to the list of registered event.\r
162 @param[in] Item The registered event.\r
163 @param[in] Arg Not used.\r
164\r
165 @retval EFI_SUCCESS The event was signaled successfully.\r
166**/\r
167EFI_STATUS\r
168EFIAPI\r
169Ip4Config2SignalEvent (\r
170 IN NET_MAP *Map,\r
171 IN NET_MAP_ITEM *Item,\r
172 IN VOID *Arg\r
173 )\r
174{\r
175 gBS->SignalEvent ((EFI_EVENT) Item->Key);\r
176\r
177 return EFI_SUCCESS;\r
178}\r
179\r
180/**\r
181 Read the configuration data from variable storage according to the VarName and\r
182 gEfiIp4Config2ProtocolGuid. It checks the integrity of variable data. If the\r
183 data is corrupted, it clears the variable data to ZERO. Othewise, it outputs the\r
184 configuration data to IP4_CONFIG2_INSTANCE.\r
185\r
186 @param[in] VarName The pointer to the variable name\r
187 @param[in, out] Instance The pointer to the IP4 config2 instance data.\r
188\r
189 @retval EFI_NOT_FOUND The variable can not be found or already corrupted.\r
190 @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation.\r
191 @retval EFI_SUCCESS The configuration data was retrieved successfully.\r
192\r
193**/\r
194EFI_STATUS\r
195Ip4Config2ReadConfigData (\r
196 IN CHAR16 *VarName,\r
197 IN OUT IP4_CONFIG2_INSTANCE *Instance\r
198 )\r
199{\r
200 EFI_STATUS Status;\r
201 UINTN VarSize;\r
202 IP4_CONFIG2_VARIABLE *Variable;\r
203 IP4_CONFIG2_DATA_ITEM *DataItem;\r
204 UINTN Index;\r
205 IP4_CONFIG2_DATA_RECORD DataRecord;\r
206 CHAR8 *Data;\r
207\r
208 //\r
209 // Try to read the configuration variable.\r
210 //\r
211 VarSize = 0;\r
212 Status = gRT->GetVariable (\r
213 VarName,\r
214 &gEfiIp4Config2ProtocolGuid,\r
215 NULL,\r
216 &VarSize,\r
217 NULL\r
218 );\r
219\r
220 if (Status == EFI_BUFFER_TOO_SMALL) {\r
221 //\r
222 // Allocate buffer and read the config variable.\r
223 //\r
224 Variable = AllocatePool (VarSize);\r
225 if (Variable == NULL) {\r
226 return EFI_OUT_OF_RESOURCES;\r
227 }\r
228\r
229 Status = gRT->GetVariable (\r
230 VarName,\r
231 &gEfiIp4Config2ProtocolGuid,\r
232 NULL,\r
233 &VarSize,\r
234 Variable\r
235 );\r
236 if (EFI_ERROR (Status) || (UINT16) (~NetblockChecksum ((UINT8 *) Variable, (UINT32) VarSize)) != 0) {\r
237 //\r
238 // GetVariable still error or the variable is corrupted.\r
239 // Fall back to the default value.\r
240 //\r
241 FreePool (Variable);\r
242\r
243 //\r
244 // Remove the problematic variable and return EFI_NOT_FOUND, a new\r
245 // variable will be set again.\r
246 //\r
247 gRT->SetVariable (\r
248 VarName,\r
249 &gEfiIp4Config2ProtocolGuid,\r
250 IP4_CONFIG2_VARIABLE_ATTRIBUTE,\r
251 0,\r
252 NULL\r
253 );\r
254\r
255 return EFI_NOT_FOUND;\r
256 }\r
257\r
258 \r
259 for (Index = 0; Index < Variable->DataRecordCount; Index++) {\r
260\r
261 CopyMem (&DataRecord, &Variable->DataRecord[Index], sizeof (DataRecord));\r
262\r
263 DataItem = &Instance->DataItem[DataRecord.DataType];\r
264 if (DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED) &&\r
265 (DataItem->DataSize != DataRecord.DataSize)\r
266 ) {\r
267 //\r
268 // Perhaps a corrupted data record...\r
269 //\r
270 continue;\r
271 }\r
272\r
273 if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED)) {\r
274 //\r
275 // This data item has variable length data.\r
276 //\r
277 DataItem->Data.Ptr = AllocatePool (DataRecord.DataSize);\r
278 if (DataItem->Data.Ptr == NULL) {\r
279 //\r
280 // no memory resource\r
281 //\r
282 continue;\r
283 }\r
284 }\r
285\r
286 Data = (CHAR8 *) Variable + DataRecord.Offset;\r
287 CopyMem (DataItem->Data.Ptr, Data, DataRecord.DataSize);\r
288\r
289 DataItem->DataSize = DataRecord.DataSize;\r
290 DataItem->Status = EFI_SUCCESS;\r
291 }\r
292\r
293 FreePool (Variable);\r
294 return EFI_SUCCESS;\r
295 }\r
296\r
297 return Status;\r
298}\r
299\r
300/**\r
301 Write the configuration data from IP4_CONFIG2_INSTANCE to variable storage.\r
302\r
303 @param[in] VarName The pointer to the variable name.\r
304 @param[in] Instance The pointer to the IP4 config2 instance data.\r
305\r
306 @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation.\r
307 @retval EFI_SUCCESS The configuration data is written successfully.\r
308\r
309**/\r
310EFI_STATUS\r
311Ip4Config2WriteConfigData (\r
312 IN CHAR16 *VarName,\r
313 IN IP4_CONFIG2_INSTANCE *Instance\r
314 )\r
315{\r
316 UINTN Index;\r
317 UINTN VarSize;\r
318 IP4_CONFIG2_DATA_ITEM *DataItem;\r
319 IP4_CONFIG2_VARIABLE *Variable;\r
320 IP4_CONFIG2_DATA_RECORD *DataRecord;\r
321 CHAR8 *Heap;\r
322 EFI_STATUS Status;\r
323\r
324 VarSize = sizeof (IP4_CONFIG2_VARIABLE) - sizeof (IP4_CONFIG2_DATA_RECORD);\r
325\r
326 for (Index = 0; Index < Ip4Config2DataTypeMaximum; Index++) {\r
327\r
328 DataItem = &Instance->DataItem[Index];\r
329 if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_VOLATILE) && !EFI_ERROR (DataItem->Status)) {\r
330\r
331 VarSize += sizeof (IP4_CONFIG2_DATA_RECORD) + DataItem->DataSize;\r
332 }\r
333 }\r
334\r
335 Variable = AllocatePool (VarSize);\r
336 if (Variable == NULL) {\r
337 return EFI_OUT_OF_RESOURCES;\r
338 }\r
339\r
340 Heap = (CHAR8 *) Variable + VarSize;\r
341 Variable->DataRecordCount = 0;\r
342\r
343 for (Index = 0; Index < Ip4Config2DataTypeMaximum; Index++) {\r
344\r
345 DataItem = &Instance->DataItem[Index];\r
346 if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_VOLATILE) && !EFI_ERROR (DataItem->Status)) {\r
347\r
348 Heap -= DataItem->DataSize;\r
349 CopyMem (Heap, DataItem->Data.Ptr, DataItem->DataSize);\r
350\r
351 DataRecord = &Variable->DataRecord[Variable->DataRecordCount];\r
352 DataRecord->DataType = (EFI_IP4_CONFIG2_DATA_TYPE) Index;\r
353 DataRecord->DataSize = (UINT32) DataItem->DataSize;\r
354 DataRecord->Offset = (UINT16) (Heap - (CHAR8 *) Variable);\r
355\r
356 Variable->DataRecordCount++;\r
357 }\r
358 }\r
359\r
360 Variable->Checksum = 0;\r
361 Variable->Checksum = (UINT16) ~NetblockChecksum ((UINT8 *) Variable, (UINT32) VarSize);\r
362\r
363 Status = gRT->SetVariable (\r
364 VarName,\r
365 &gEfiIp4Config2ProtocolGuid,\r
366 IP4_CONFIG2_VARIABLE_ATTRIBUTE,\r
367 VarSize,\r
368 Variable\r
369 );\r
370\r
371 FreePool (Variable);\r
372\r
373 return Status;\r
374}\r
375\r
376\r
377/**\r
378 Build a EFI_IP4_ROUTE_TABLE to be returned to the caller of GetModeData. \r
379 The EFI_IP4_ROUTE_TABLE is clumsy to use in the internal operation of the \r
380 IP4 driver.\r
381\r
382 @param[in] IpSb The IP4 service binding instance.\r
383 @param[out] Table The built IP4 route table.\r
384\r
385 @retval EFI_SUCCESS The route table is successfully build\r
386 @retval EFI_NOT_FOUND Failed to allocate the memory for the rotue table.\r
387\r
388**/\r
389EFI_STATUS\r
390Ip4Config2BuildDefaultRouteTable (\r
391 IN IP4_SERVICE *IpSb,\r
392 OUT EFI_IP4_ROUTE_TABLE *Table\r
393 )\r
394{\r
395 LIST_ENTRY *Entry; \r
396 IP4_ROUTE_ENTRY *RtEntry;\r
397 UINT32 Count;\r
398 INT32 Index;\r
399\r
400 if (IpSb->DefaultRouteTable == NULL) {\r
401 return EFI_NOT_FOUND;\r
402 }\r
403\r
404 Count = IpSb->DefaultRouteTable->TotalNum;\r
405\r
406 if (Count == 0) {\r
407 return EFI_NOT_FOUND;\r
408 }\r
409\r
410 //\r
411 // Copy the route entry to EFI route table. Keep the order of\r
412 // route entry copied from most specific to default route. That\r
413 // is, interlevel the route entry from the instance's route area\r
414 // and those from the default route table's route area.\r
415 //\r
416 Count = 0;\r
417\r
364f4efa 418 for (Index = IP4_MASK_MAX; Index >= 0; Index--) {\r
1f6729ff 419\r
420 NET_LIST_FOR_EACH (Entry, &(IpSb->DefaultRouteTable->RouteArea[Index])) {\r
421 RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link);\r
422\r
423 EFI_IP4 (Table[Count].SubnetAddress) = HTONL (RtEntry->Dest & RtEntry->Netmask);\r
424 EFI_IP4 (Table[Count].SubnetMask) = HTONL (RtEntry->Netmask);\r
425 EFI_IP4 (Table[Count].GatewayAddress) = HTONL (RtEntry->NextHop);\r
426\r
427 Count++;\r
428 }\r
429\r
430 }\r
431\r
432 return EFI_SUCCESS;\r
433}\r
434\r
435/**\r
436 The event process routine when the DHCPv4 service binding protocol is installed\r
437 in the system.\r
438\r
439 @param[in] Event Not used.\r
440 @param[in] Context The pointer to the IP4 config2 instance data.\r
441\r
442**/\r
443VOID\r
444EFIAPI\r
445Ip4Config2OnDhcp4SbInstalled (\r
446 IN EFI_EVENT Event,\r
447 IN VOID *Context\r
448 )\r
449{\r
450 IP4_CONFIG2_INSTANCE *Instance;\r
451\r
452 Instance = (IP4_CONFIG2_INSTANCE *) Context;\r
453\r
454 if ((Instance->Dhcp4Handle != NULL) || (Instance->Policy != Ip4Config2PolicyDhcp)) {\r
455 //\r
456 // The DHCP4 child is already created or the policy is no longer DHCP.\r
457 //\r
458 return ;\r
459 }\r
460\r
461 Ip4StartAutoConfig (Instance);\r
462}\r
463\r
464/**\r
465 Set the station address and subnetmask for the default interface.\r
466\r
e371cc14 467 @param[in] IpSb The pointer to the IP4 service binding instance.\r
1f6729ff 468 @param[in] StationAddress Ip address to be set.\r
469 @param[in] SubnetMask Subnet to be set.\r
470\r
471 @retval EFI_SUCCESS Set default address successful. \r
472 @retval Others Some errors occur in setting. \r
473\r
474**/\r
475EFI_STATUS\r
476Ip4Config2SetDefaultAddr (\r
e371cc14 477 IN IP4_SERVICE *IpSb,\r
1f6729ff 478 IN IP4_ADDR StationAddress,\r
479 IN IP4_ADDR SubnetMask\r
480 )\r
481{\r
482 EFI_STATUS Status;\r
1f6729ff 483 IP4_INTERFACE *IpIf;\r
484 IP4_PROTOCOL *Ip4Instance;\r
485 EFI_ARP_PROTOCOL *Arp;\r
486 LIST_ENTRY *Entry;\r
487 IP4_ADDR Subnet;\r
488 IP4_ROUTE_TABLE *RouteTable;\r
489\r
1f6729ff 490 IpIf = IpSb->DefaultInterface;\r
491 ASSERT (IpIf != NULL);\r
492\r
493 if ((IpIf->Ip == StationAddress) && (IpIf->SubnetMask == SubnetMask)) {\r
2c320007 494 IpSb->State = IP4_SERVICE_CONFIGED;\r
1f6729ff 495 return EFI_SUCCESS;\r
496 }\r
497\r
e371cc14
JW
498 if (IpSb->Reconfig) {\r
499 //\r
500 // The default address is changed, free the previous interface first.\r
501 //\r
502 if (IpSb->DefaultRouteTable != NULL) {\r
503 Ip4FreeRouteTable (IpSb->DefaultRouteTable);\r
504 IpSb->DefaultRouteTable = NULL; \r
505 }\r
1f6729ff 506\r
e371cc14
JW
507 Ip4CancelReceive (IpSb->DefaultInterface);\r
508 Ip4FreeInterface (IpSb->DefaultInterface, NULL);\r
509 IpSb->DefaultInterface = NULL;\r
510 //\r
511 // Create new default interface and route table.\r
512 // \r
513 IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);\r
514 if (IpIf == NULL) {\r
515 return EFI_OUT_OF_RESOURCES;\r
516 }\r
1f6729ff 517\r
e371cc14
JW
518 RouteTable = Ip4CreateRouteTable ();\r
519 if (RouteTable == NULL) {\r
520 Ip4FreeInterface (IpIf, NULL);\r
521 return EFI_OUT_OF_RESOURCES;\r
522 }\r
523 \r
524 IpSb->DefaultInterface = IpIf;\r
525 InsertHeadList (&IpSb->Interfaces, &IpIf->Link);\r
526 IpSb->DefaultRouteTable = RouteTable;\r
527 Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb);\r
1f6729ff 528 }\r
1f6729ff 529\r
530 if (IpSb->State == IP4_SERVICE_CONFIGED) {\r
531 IpSb->State = IP4_SERVICE_UNSTARTED;\r
532 }\r
533\r
534 Status = Ip4SetAddress (IpIf, StationAddress, SubnetMask);\r
535 if (EFI_ERROR (Status)) {\r
536 return Status;\r
537 }\r
538\r
539 if (IpIf->Arp != NULL) {\r
540 // \r
541 // A non-NULL IpIf->Arp here means a new ARP child is created when setting default address, \r
542 // but some IP children may have referenced the default interface before it is configured,\r
543 // these IP instances also consume this ARP protocol so they need to open it BY_CHILD_CONTROLLER.\r
544 //\r
545 Arp = NULL;\r
546 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {\r
547 Ip4Instance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, AddrLink, IP4_PROTOCOL_SIGNATURE);\r
548 Status = gBS->OpenProtocol (\r
549 IpIf->ArpHandle,\r
550 &gEfiArpProtocolGuid,\r
551 (VOID **) &Arp,\r
552 gIp4DriverBinding.DriverBindingHandle,\r
553 Ip4Instance->Handle,\r
554 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
555 );\r
556 if (EFI_ERROR (Status)) {\r
557 return Status;\r
558 }\r
559 }\r
560 }\r
561\r
562 Ip4AddRoute (\r
563 IpSb->DefaultRouteTable,\r
564 StationAddress,\r
565 SubnetMask,\r
566 IP4_ALLZERO_ADDRESS\r
567 );\r
568\r
569 //\r
570 // Add a route for the connected network.\r
571 //\r
572 Subnet = StationAddress & SubnetMask;\r
573\r
574 Ip4AddRoute (\r
575 IpSb->DefaultRouteTable,\r
576 Subnet,\r
577 SubnetMask,\r
578 IP4_ALLZERO_ADDRESS\r
579 );\r
580\r
581 IpSb->State = IP4_SERVICE_CONFIGED;\r
e371cc14
JW
582 IpSb->Reconfig = FALSE;\r
583 \r
1f6729ff 584 return EFI_SUCCESS;\r
585}\r
586\r
587/**\r
588 Set the station address, subnetmask and gateway address for the default interface.\r
589\r
590 @param[in] Instance The pointer to the IP4 config2 instance data. \r
591 @param[in] StationAddress Ip address to be set.\r
592 @param[in] SubnetMask Subnet to be set.\r
593 @param[in] GatewayAddress Gateway to be set.\r
594\r
595 @retval EFI_SUCCESS Set default If successful. \r
596 @retval Others Errors occur as indicated. \r
597\r
598**/\r
599EFI_STATUS\r
600Ip4Config2SetDefaultIf (\r
601 IN IP4_CONFIG2_INSTANCE *Instance,\r
602 IN IP4_ADDR StationAddress,\r
603 IN IP4_ADDR SubnetMask,\r
604 IN IP4_ADDR GatewayAddress\r
605 )\r
606{\r
607 EFI_STATUS Status;\r
608 IP4_SERVICE *IpSb;\r
609\r
e371cc14
JW
610 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
611\r
612 Status = Ip4Config2SetDefaultAddr (IpSb, StationAddress, SubnetMask);\r
1f6729ff 613 if (EFI_ERROR (Status)) {\r
614 return Status;\r
615 }\r
616\r
617 //\r
618 // Create a route if there is a default router.\r
619 //\r
1f6729ff 620 if (GatewayAddress != IP4_ALLZERO_ADDRESS) {\r
621 Ip4AddRoute (\r
622 IpSb->DefaultRouteTable,\r
623 IP4_ALLZERO_ADDRESS,\r
624 IP4_ALLZERO_ADDRESS,\r
625 GatewayAddress\r
626 ); \r
627 }\r
628\r
629 return EFI_SUCCESS;\r
630}\r
631\r
632\r
633/**\r
634 Release all the DHCP related resources.\r
635\r
636 @param Instance The IP4 config2 instance.\r
637\r
638 @return None\r
639\r
640**/\r
641VOID\r
642Ip4Config2CleanDhcp4 (\r
643 IN IP4_CONFIG2_INSTANCE *Instance\r
644 )\r
645{\r
646 IP4_SERVICE *IpSb;\r
647\r
648 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
649\r
650 if (Instance->Dhcp4 != NULL) {\r
651 Instance->Dhcp4->Stop (Instance->Dhcp4);\r
652\r
653 gBS->CloseProtocol (\r
654 Instance->Dhcp4Handle,\r
655 &gEfiDhcp4ProtocolGuid,\r
656 IpSb->Image,\r
657 IpSb->Controller\r
658 );\r
659\r
660 Instance->Dhcp4 = NULL;\r
661 }\r
662\r
663 if (Instance->Dhcp4Handle != NULL) {\r
664 NetLibDestroyServiceChild (\r
665 IpSb->Controller,\r
666 IpSb->Image,\r
667 &gEfiDhcp4ServiceBindingProtocolGuid,\r
668 Instance->Dhcp4Handle\r
669 );\r
670\r
671 Instance->Dhcp4Handle = NULL;\r
672 }\r
673\r
674 if (Instance->Dhcp4Event != NULL) {\r
675 gBS->CloseEvent (Instance->Dhcp4Event);\r
676 Instance->Dhcp4Event = NULL;\r
677 }\r
678}\r
679\r
9feefde0
SEHM
680/**\r
681 This worker function sets the DNS server list for the EFI IPv4 network\r
682 stack running on the communication device that this EFI_IP4_CONFIG2_PROTOCOL\r
683 manages. The DNS server addresses must be unicast IPv4 addresses. \r
684\r
685 @param[in] Instance The pointer to the IP4 config2 instance data.\r
686 @param[in] DataSize The size of the buffer pointed to by Data in bytes.\r
687 @param[in] Data The data buffer to set, points to an array of\r
688 EFI_IPv4_ADDRESS instances.\r
689\r
690 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.\r
691 @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.\r
692 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation.\r
693 @retval EFI_ABORTED The DNS server addresses to be set equal the current\r
694 configuration.\r
695 @retval EFI_SUCCESS The specified configuration data for the EFI IPv4\r
696 network stack was set.\r
697\r
698**/\r
699EFI_STATUS\r
700Ip4Config2SetDnsServerWorker (\r
701 IN IP4_CONFIG2_INSTANCE *Instance,\r
702 IN UINTN DataSize,\r
703 IN VOID *Data\r
704 )\r
705{\r
706 UINTN OldIndex;\r
707 UINTN NewIndex;\r
708 UINTN Index1;\r
709 EFI_IPv4_ADDRESS *OldDns;\r
710 EFI_IPv4_ADDRESS *NewDns;\r
711 UINTN OldDnsCount;\r
712 UINTN NewDnsCount;\r
713 IP4_CONFIG2_DATA_ITEM *Item;\r
714 BOOLEAN OneAdded;\r
715 VOID *Tmp;\r
716 IP4_ADDR DnsAddress;\r
717\r
718 if ((DataSize % sizeof (EFI_IPv4_ADDRESS) != 0) || (DataSize == 0)) {\r
719 return EFI_BAD_BUFFER_SIZE;\r
720 }\r
721\r
722 Item = &Instance->DataItem[Ip4Config2DataTypeDnsServer];\r
723 NewDns = (EFI_IPv4_ADDRESS *) Data;\r
724 OldDns = Item->Data.DnsServers;\r
725 NewDnsCount = DataSize / sizeof (EFI_IPv4_ADDRESS); \r
726 OldDnsCount = Item->DataSize / sizeof (EFI_IPv4_ADDRESS);\r
727 OneAdded = FALSE;\r
728\r
729 if (NewDnsCount != OldDnsCount) {\r
730 Tmp = AllocatePool (DataSize);\r
731 if (Tmp == NULL) {\r
732 return EFI_OUT_OF_RESOURCES;\r
733 }\r
734 } else {\r
735 Tmp = NULL;\r
736 }\r
737\r
738 for (NewIndex = 0; NewIndex < NewDnsCount; NewIndex++) {\r
739 CopyMem (&DnsAddress, NewDns + NewIndex, sizeof (IP4_ADDR));\r
01b5ac88 740 if (IP4_IS_UNSPECIFIED (NTOHL (DnsAddress)) || IP4_IS_LOCAL_BROADCAST (NTOHL (DnsAddress))) {\r
9feefde0
SEHM
741 //\r
742 // The dns server address must be unicast.\r
743 //\r
d52f9163
JW
744 if (Tmp != NULL) {\r
745 FreePool (Tmp);\r
746 }\r
9feefde0
SEHM
747 return EFI_INVALID_PARAMETER;\r
748 }\r
749\r
750 for (Index1 = NewIndex + 1; Index1 < NewDnsCount; Index1++) {\r
751 if (EFI_IP4_EQUAL (NewDns + NewIndex, NewDns + Index1)) {\r
d52f9163
JW
752 if (Tmp != NULL) {\r
753 FreePool (Tmp);\r
754 }\r
9feefde0
SEHM
755 return EFI_INVALID_PARAMETER;\r
756 }\r
757 }\r
758\r
759 if (OneAdded) {\r
760 //\r
761 // If any address in the new setting is not in the old settings, skip the\r
762 // comparision below.\r
763 //\r
764 continue;\r
765 }\r
766\r
767 for (OldIndex = 0; OldIndex < OldDnsCount; OldIndex++) {\r
768 if (EFI_IP4_EQUAL (NewDns + NewIndex, OldDns + OldIndex)) {\r
769 //\r
770 // If found break out.\r
771 //\r
772 break;\r
773 }\r
774 }\r
775\r
776 if (OldIndex == OldDnsCount) {\r
777 OneAdded = TRUE;\r
778 }\r
779 }\r
780\r
781 if (!OneAdded && (DataSize == Item->DataSize)) {\r
782 //\r
783 // No new item is added and the size is the same.\r
784 //\r
785 Item->Status = EFI_SUCCESS;\r
786 return EFI_ABORTED;\r
787 } else {\r
788 if (Tmp != NULL) {\r
789 if (Item->Data.Ptr != NULL) {\r
790 FreePool (Item->Data.Ptr);\r
791 } \r
792 Item->Data.Ptr = Tmp;\r
793 }\r
794\r
795 CopyMem (Item->Data.Ptr, Data, DataSize);\r
796 Item->DataSize = DataSize;\r
797 Item->Status = EFI_SUCCESS;\r
798 return EFI_SUCCESS;\r
799 }\r
800}\r
801\r
802\r
1f6729ff 803\r
804/**\r
805 Callback function when DHCP process finished. It will save the\r
806 retrieved IP configure parameter from DHCP to the NVRam.\r
807\r
808 @param Event The callback event\r
809 @param Context Opaque context to the callback\r
810\r
811 @return None\r
812\r
813**/\r
814VOID\r
815EFIAPI\r
816Ip4Config2OnDhcp4Complete (\r
817 IN EFI_EVENT Event,\r
818 IN VOID *Context\r
819 )\r
820{\r
821 IP4_CONFIG2_INSTANCE *Instance;\r
822 EFI_DHCP4_MODE_DATA Dhcp4Mode;\r
823 EFI_STATUS Status;\r
824 IP4_ADDR StationAddress;\r
825 IP4_ADDR SubnetMask;\r
826 IP4_ADDR GatewayAddress;\r
9feefde0
SEHM
827 UINT32 Index;\r
828 UINT32 OptionCount;\r
829 EFI_DHCP4_PACKET_OPTION **OptionList;\r
1f6729ff 830\r
831 Instance = (IP4_CONFIG2_INSTANCE *) Context;\r
832 ASSERT (Instance->Dhcp4 != NULL);\r
833\r
834 //\r
835 // Get the DHCP retrieved parameters\r
836 //\r
837 Status = Instance->Dhcp4->GetModeData (Instance->Dhcp4, &Dhcp4Mode);\r
838\r
839 if (EFI_ERROR (Status)) {\r
840 goto Exit;\r
841 }\r
842\r
843 if (Dhcp4Mode.State == Dhcp4Bound) {\r
844 StationAddress = EFI_NTOHL (Dhcp4Mode.ClientAddress);\r
845 SubnetMask = EFI_NTOHL (Dhcp4Mode.SubnetMask);\r
846 GatewayAddress = EFI_NTOHL (Dhcp4Mode.RouterAddress);\r
847\r
848 Status = Ip4Config2SetDefaultIf (Instance, StationAddress, SubnetMask, GatewayAddress);\r
849 if (EFI_ERROR (Status)) {\r
850 goto Exit;\r
851 }\r
852 \r
9feefde0
SEHM
853 //\r
854 // Parse the ACK to get required DNS server information.\r
855 //\r
856 OptionCount = 0;\r
857 OptionList = NULL;\r
858\r
859 Status = Instance->Dhcp4->Parse (Instance->Dhcp4, Dhcp4Mode.ReplyPacket, &OptionCount, OptionList);\r
860 if (Status != EFI_BUFFER_TOO_SMALL) {\r
861 goto Exit;\r
862 }\r
863\r
864 OptionList = AllocateZeroPool (OptionCount * sizeof (EFI_DHCP4_PACKET_OPTION *));\r
865 if (OptionList == NULL) {\r
866 goto Exit;\r
867 }\r
868\r
869 Status = Instance->Dhcp4->Parse (Instance->Dhcp4, Dhcp4Mode.ReplyPacket, &OptionCount, OptionList);\r
870 if (EFI_ERROR (Status)) {\r
871 FreePool (OptionList);\r
872 goto Exit;\r
873 }\r
874\r
875 for (Index = 0; Index < OptionCount; Index++) {\r
876 //\r
877 // Look for DNS Server opcode (6).\r
878 //\r
ac6c3d90 879 if (OptionList[Index]->OpCode == DHCP4_TAG_DNS_SERVER) {\r
9feefde0
SEHM
880 if (((OptionList[Index]->Length & 0x3) != 0) || (OptionList[Index]->Length == 0)) {\r
881 break;\r
882 }\r
883\r
884 Ip4Config2SetDnsServerWorker (Instance, OptionList[Index]->Length, &OptionList[Index]->Data[0]);\r
885 break;\r
886 }\r
887 }\r
888\r
889 FreePool (OptionList);\r
890\r
1f6729ff 891 Instance->DhcpSuccess = TRUE;\r
892 }\r
893\r
894Exit:\r
895 Ip4Config2CleanDhcp4 (Instance);\r
896 DispatchDpc ();\r
897}\r
898\r
899\r
900/**\r
901 Start the DHCP configuration for this IP service instance.\r
902 It will locates the EFI_IP4_CONFIG2_PROTOCOL, then start the\r
903 DHCP configuration.\r
904\r
905 @param[in] Instance The IP4 config2 instance to configure\r
906\r
0a18956d 907 @retval EFI_SUCCESS The auto configuration is successfully started\r
1f6729ff 908 @retval Others Failed to start auto configuration.\r
909\r
910**/\r
911EFI_STATUS\r
912Ip4StartAutoConfig (\r
913 IN IP4_CONFIG2_INSTANCE *Instance\r
914 )\r
915{\r
916 IP4_SERVICE *IpSb;\r
917 EFI_DHCP4_PROTOCOL *Dhcp4;\r
918 EFI_DHCP4_MODE_DATA Dhcp4Mode;\r
919 EFI_DHCP4_PACKET_OPTION *OptionList[1];\r
920 IP4_CONFIG2_DHCP4_OPTION ParaList;\r
921 EFI_STATUS Status;\r
922 \r
923\r
924 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
925\r
926 if (IpSb->State > IP4_SERVICE_UNSTARTED) {\r
927 return EFI_SUCCESS;\r
928 }\r
929\r
930 //\r
931 // A host must not invoke DHCP configuration if it is already\r
932 // participating in the DHCP configuraiton process.\r
933 //\r
934 if (Instance->Dhcp4Handle != NULL) {\r
935 return EFI_SUCCESS;\r
936 }\r
937\r
938 Status = NetLibCreateServiceChild (\r
939 IpSb->Controller,\r
940 IpSb->Image,\r
941 &gEfiDhcp4ServiceBindingProtocolGuid,\r
942 &Instance->Dhcp4Handle\r
943 );\r
944\r
945 if (Status == EFI_UNSUPPORTED) {\r
946 //\r
947 // No DHCPv4 Service Binding protocol, register a notify.\r
948 //\r
949 if (Instance->Dhcp4SbNotifyEvent == NULL) {\r
950 Instance->Dhcp4SbNotifyEvent = EfiCreateProtocolNotifyEvent (\r
951 &gEfiDhcp4ServiceBindingProtocolGuid,\r
952 TPL_CALLBACK,\r
953 Ip4Config2OnDhcp4SbInstalled,\r
954 (VOID *) Instance,\r
955 &Instance->Registration\r
956 );\r
957 }\r
958 }\r
959\r
960 if (EFI_ERROR (Status)) {\r
961 return Status;\r
962 }\r
963\r
964 if (Instance->Dhcp4SbNotifyEvent != NULL) {\r
965 gBS->CloseEvent (Instance->Dhcp4SbNotifyEvent);\r
966 }\r
967\r
968 Status = gBS->OpenProtocol (\r
969 Instance->Dhcp4Handle,\r
970 &gEfiDhcp4ProtocolGuid,\r
971 (VOID **) &Instance->Dhcp4,\r
972 IpSb->Image,\r
973 IpSb->Controller,\r
974 EFI_OPEN_PROTOCOL_BY_DRIVER\r
975 );\r
976 ASSERT_EFI_ERROR (Status);\r
977\r
978\r
979 //\r
980 // Check the current DHCP status, if the DHCP process has\r
981 // already finished, return now.\r
982 //\r
983 Dhcp4 = Instance->Dhcp4;\r
984 Status = Dhcp4->GetModeData (Dhcp4, &Dhcp4Mode);\r
985\r
986 if (Dhcp4Mode.State == Dhcp4Bound) {\r
987 Ip4Config2OnDhcp4Complete (NULL, Instance);\r
988 return EFI_SUCCESS;\r
989\r
990 }\r
991\r
992 //\r
993 // Try to start the DHCP process. Use most of the current\r
994 // DHCP configuration to avoid problems if some DHCP client\r
995 // yields the control of this DHCP service to us.\r
996 //\r
ac6c3d90 997 ParaList.Head.OpCode = DHCP4_TAG_PARA_LIST;\r
9feefde0 998 ParaList.Head.Length = 3;\r
ac6c3d90
ZL
999 ParaList.Head.Data[0] = DHCP4_TAG_NETMASK;\r
1000 ParaList.Route = DHCP4_TAG_ROUTER;\r
1001 ParaList.Dns = DHCP4_TAG_DNS_SERVER;\r
1f6729ff 1002 OptionList[0] = &ParaList.Head;\r
1003 Dhcp4Mode.ConfigData.OptionCount = 1;\r
1004 Dhcp4Mode.ConfigData.OptionList = OptionList;\r
1005\r
1006 Status = Dhcp4->Configure (Dhcp4, &Dhcp4Mode.ConfigData);\r
1007\r
1008 if (EFI_ERROR (Status)) {\r
1009 return Status;\r
1010 }\r
1011 \r
1012 //\r
1013 // Start the DHCP process\r
1014 //\r
1015 Status = gBS->CreateEvent (\r
1016 EVT_NOTIFY_SIGNAL,\r
1017 TPL_CALLBACK,\r
1018 Ip4Config2OnDhcp4Complete,\r
1019 Instance,\r
1020 &Instance->Dhcp4Event\r
1021 );\r
1022\r
1023 if (EFI_ERROR (Status)) {\r
1024 return Status;\r
1025 }\r
1026\r
1027 Status = Dhcp4->Start (Dhcp4, Instance->Dhcp4Event);\r
1028\r
1029 if (EFI_ERROR (Status)) {\r
1030 return Status;\r
1031 }\r
1032 \r
1033 IpSb->State = IP4_SERVICE_STARTED;\r
1034 DispatchDpc ();\r
1035 return EFI_SUCCESS;\r
1036\r
1037}\r
1038\r
1039\r
1040\r
1041/**\r
1042 The work function is to get the interface information of the communication \r
1043 device this IP4_CONFIG2_INSTANCE manages.\r
1044\r
1045 @param[in] Instance Pointer to the IP4 config2 instance data.\r
1046 @param[in, out] DataSize On input, in bytes, the size of Data. On output, in\r
1047 bytes, the size of buffer required to store the specified\r
1048 configuration data.\r
1049 @param[in] Data The data buffer in which the configuration data is returned.\r
1050 Ignored if DataSize is ZERO.\r
1051\r
1052 @retval EFI_BUFFER_TOO_SMALL The size of Data is too small for the specified\r
1053 configuration data, and the required size is\r
1054 returned in DataSize.\r
1055 @retval EFI_SUCCESS The specified configuration data was obtained.\r
1056\r
1057**/\r
1058EFI_STATUS\r
1059Ip4Config2GetIfInfo (\r
1060 IN IP4_CONFIG2_INSTANCE *Instance,\r
1061 IN OUT UINTN *DataSize,\r
1062 IN VOID *Data OPTIONAL\r
1063 )\r
1064{\r
1f6729ff 1065 IP4_SERVICE *IpSb;\r
1066 UINTN Length;\r
1067 IP4_CONFIG2_DATA_ITEM *Item;\r
1068 EFI_IP4_CONFIG2_INTERFACE_INFO *IfInfo;\r
1069 IP4_ADDR Address;\r
1070\r
1071 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
1072 Length = sizeof (EFI_IP4_CONFIG2_INTERFACE_INFO);\r
1073\r
1074 if (IpSb->DefaultRouteTable != NULL) {\r
1075 Length += IpSb->DefaultRouteTable->TotalNum * sizeof (EFI_IP4_ROUTE_TABLE);\r
1076 }\r
1077 \r
1078 if (*DataSize < Length) {\r
1079 *DataSize = Length;\r
1080 return EFI_BUFFER_TOO_SMALL;\r
1081 }\r
1082\r
1083 //\r
1084 // Copy the fixed size part of the interface info.\r
1085 //\r
1086 Item = &Instance->DataItem[Ip4Config2DataTypeInterfaceInfo];\r
1087 IfInfo = (EFI_IP4_CONFIG2_INTERFACE_INFO *) Data;\r
1088 CopyMem (IfInfo, Item->Data.Ptr, sizeof (EFI_IP4_CONFIG2_INTERFACE_INFO));\r
1089\r
1090 //\r
1091 // Update the address info.\r
1092 //\r
1093 if (IpSb->DefaultInterface != NULL) {\r
1094 Address = HTONL (IpSb->DefaultInterface->Ip);\r
1095 CopyMem (&IfInfo->StationAddress, &Address, sizeof (EFI_IPv4_ADDRESS));\r
1096 Address = HTONL (IpSb->DefaultInterface->SubnetMask);\r
1097 CopyMem (&IfInfo->SubnetMask, &Address, sizeof (EFI_IPv4_ADDRESS));\r
1098 }\r
1099\r
1100 if (IpSb->DefaultRouteTable != NULL) {\r
1101 IfInfo->RouteTableSize = IpSb->DefaultRouteTable->TotalNum;\r
1102 IfInfo->RouteTable = (EFI_IP4_ROUTE_TABLE *) ((UINT8 *) Data + sizeof (EFI_IP4_CONFIG2_INTERFACE_INFO));\r
1103\r
1104 Ip4Config2BuildDefaultRouteTable (IpSb, IfInfo->RouteTable); \r
1105 }\r
1106 \r
1107 return EFI_SUCCESS;\r
1108}\r
1109\r
1110/**\r
1111 The work function is to set the general configuration policy for the EFI IPv4 network \r
1112 stack that is running on the communication device managed by this IP4_CONFIG2_INSTANCE.\r
1113 The policy will affect other configuration settings.\r
1114\r
1115 @param[in] Instance Pointer to the IP4 config2 instance data.\r
1116 @param[in] DataSize Size of the buffer pointed to by Data in bytes.\r
1117 @param[in] Data The data buffer to set.\r
1118\r
1119 @retval EFI_INVALID_PARAMETER The to be set policy is invalid.\r
1120 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.\r
1121 @retval EFI_ABORTED The new policy equals the current policy.\r
1122 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6\r
1123 network stack was set.\r
1124\r
1125**/\r
1126EFI_STATUS\r
1127Ip4Config2SetPolicy (\r
1128 IN IP4_CONFIG2_INSTANCE *Instance,\r
1129 IN UINTN DataSize,\r
1130 IN VOID *Data\r
1131 )\r
1132{\r
1133 EFI_IP4_CONFIG2_POLICY NewPolicy;\r
1134 IP4_CONFIG2_DATA_ITEM *DataItem;\r
1135 IP4_SERVICE *IpSb;\r
1136\r
1137 if (DataSize != sizeof (EFI_IP4_CONFIG2_POLICY)) {\r
1138 return EFI_BAD_BUFFER_SIZE;\r
1139 }\r
1140\r
1141 NewPolicy = *((EFI_IP4_CONFIG2_POLICY *) Data);\r
1142\r
1143 if (NewPolicy >= Ip4Config2PolicyMax) {\r
1144 return EFI_INVALID_PARAMETER;\r
1145 }\r
1146\r
1147 if (NewPolicy == Instance->Policy) {\r
7648748e
JW
1148 if (NewPolicy != Ip4Config2PolicyDhcp || Instance->DhcpSuccess) {\r
1149 return EFI_ABORTED;\r
1150 }\r
1f6729ff 1151 } else {\r
24b41b75
JW
1152 //\r
1153 // The policy is changed. Clean the ManualAddress, Gateway and DnsServers, \r
1154 // shrink the variable data size, and fire up all the related events.\r
1155 //\r
1156 DataItem = &Instance->DataItem[Ip4Config2DataTypeManualAddress];\r
1157 if (DataItem->Data.Ptr != NULL) {\r
1158 FreePool (DataItem->Data.Ptr);\r
1159 }\r
1160 DataItem->Data.Ptr = NULL;\r
1161 DataItem->DataSize = 0;\r
1162 DataItem->Status = EFI_NOT_FOUND;\r
1163 NetMapIterate (&DataItem->EventMap, Ip4Config2SignalEvent, NULL);\r
1164\r
1165 DataItem = &Instance->DataItem[Ip4Config2DataTypeGateway];\r
1166 if (DataItem->Data.Ptr != NULL) {\r
1167 FreePool (DataItem->Data.Ptr);\r
1168 }\r
1169 DataItem->Data.Ptr = NULL;\r
1170 DataItem->DataSize = 0;\r
1171 DataItem->Status = EFI_NOT_FOUND;\r
1172 NetMapIterate (&DataItem->EventMap, Ip4Config2SignalEvent, NULL);\r
1173\r
1174 DataItem = &Instance->DataItem[Ip4Config2DataTypeDnsServer];\r
1175 if (DataItem->Data.Ptr != NULL) {\r
1176 FreePool (DataItem->Data.Ptr);\r
1177 }\r
1178 DataItem->Data.Ptr = NULL;\r
1179 DataItem->DataSize = 0;\r
1180 DataItem->Status = EFI_NOT_FOUND;\r
1181 NetMapIterate (&DataItem->EventMap, Ip4Config2SignalEvent, NULL);\r
1182 \r
1f6729ff 1183 if (NewPolicy == Ip4Config2PolicyDhcp) {\r
eab40164 1184 SET_DATA_ATTRIB (DataItem->Attribute, DATA_ATTRIB_VOLATILE);\r
1f6729ff 1185 } else {\r
1186 //\r
1187 // The policy is changed from dhcp to static. Stop the DHCPv4 process\r
1188 // and destroy the DHCPv4 child.\r
1189 //\r
1190 if (Instance->Dhcp4Handle != NULL) {\r
1191 Ip4Config2DestroyDhcp4 (Instance);\r
1192 }\r
1193 \r
1194 //\r
1195 // Close the event.\r
1196 //\r
1197 if (Instance->Dhcp4Event != NULL) {\r
1198 gBS->CloseEvent (Instance->Dhcp4Event);\r
4f0f2316 1199 Instance->Dhcp4Event = NULL;\r
1f6729ff 1200 }\r
1201 }\r
1202 }\r
1203\r
1204 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
1205 Ip4Config2OnPolicyChanged (IpSb, NewPolicy);\r
1206\r
1207 Instance->Policy = NewPolicy;\r
1208\r
1209 return EFI_SUCCESS;\r
1210}\r
1211\r
1212/**\r
1213 The work function is to set the station addresses manually for the EFI IPv4 \r
1214 network stack. It is only configurable when the policy is Ip4Config2PolicyStatic.\r
1215\r
1216 @param[in] Instance Pointer to the IP4 config2 instance data.\r
1217 @param[in] DataSize Size of the buffer pointed to by Data in bytes.\r
1218 @param[in] Data The data buffer to set.\r
1219\r
1220 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.\r
1221 @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set\r
1222 under the current policy.\r
1223 @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.\r
1224 @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation.\r
1225 @retval EFI_NOT_READY An asynchrous process is invoked to set the specified\r
1226 configuration data, and the process is not finished.\r
1227 @retval EFI_ABORTED The manual addresses to be set equal current\r
1228 configuration.\r
1229 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6\r
1230 network stack was set.\r
1231\r
1232**/\r
1233EFI_STATUS\r
1234Ip4Config2SetMaunualAddress (\r
1235 IN IP4_CONFIG2_INSTANCE *Instance,\r
1236 IN UINTN DataSize,\r
1237 IN VOID *Data\r
1238 )\r
1239{\r
1240 EFI_IP4_CONFIG2_MANUAL_ADDRESS NewAddress;\r
1241 IP4_CONFIG2_DATA_ITEM *DataItem;\r
1242 EFI_STATUS Status;\r
1243 IP4_ADDR StationAddress;\r
1244 IP4_ADDR SubnetMask;\r
1245 VOID *Ptr;\r
e371cc14
JW
1246 IP4_SERVICE *IpSb;\r
1247\r
1248 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
1f6729ff 1249\r
1250 ASSERT (Instance->DataItem[Ip4Config2DataTypeManualAddress].Status != EFI_NOT_READY);\r
1251\r
1252 if (((DataSize % sizeof (EFI_IP4_CONFIG2_MANUAL_ADDRESS)) != 0) || (DataSize == 0)) {\r
1253 return EFI_BAD_BUFFER_SIZE;\r
1254 }\r
1255\r
1256 if (Instance->Policy != Ip4Config2PolicyStatic) {\r
1257 return EFI_WRITE_PROTECTED;\r
1258 }\r
1259\r
1260 NewAddress = *((EFI_IP4_CONFIG2_MANUAL_ADDRESS *) Data);\r
1261\r
818ba0ef
JW
1262 StationAddress = EFI_NTOHL (NewAddress.Address);\r
1263 SubnetMask = EFI_NTOHL (NewAddress.SubnetMask);\r
1264\r
1265 if (NetGetMaskLength (SubnetMask) == IP4_MASK_NUM) {\r
1266 return EFI_INVALID_PARAMETER;\r
1267 }\r
1268\r
1f6729ff 1269 //\r
1270 // Store the new data, and init the DataItem status to EFI_NOT_READY because\r
1271 // we may have an asynchronous configuration process.\r
1272 //\r
1273 Ptr = AllocateCopyPool (DataSize, Data);\r
1274 if (Ptr == NULL) {\r
1275 return EFI_OUT_OF_RESOURCES;\r
1276 }\r
1277\r
1278 DataItem = &Instance->DataItem[Ip4Config2DataTypeManualAddress];\r
1279 if (DataItem->Data.Ptr != NULL) {\r
1280 FreePool (DataItem->Data.Ptr);\r
1281 }\r
1282 \r
1283 DataItem->Data.Ptr = Ptr;\r
1284 DataItem->DataSize = DataSize;\r
1285 DataItem->Status = EFI_NOT_READY;\r
1286\r
e371cc14
JW
1287 IpSb->Reconfig = TRUE;\r
1288 Status = Ip4Config2SetDefaultAddr (IpSb, StationAddress, SubnetMask);\r
1f6729ff 1289\r
7278ce8c 1290 DataItem->Status = Status; \r
1f6729ff 1291\r
7278ce8c 1292 if (EFI_ERROR (DataItem->Status) && DataItem->Status != EFI_NOT_READY) {\r
1f6729ff 1293 if (Ptr != NULL) {\r
1294 FreePool (Ptr);\r
1295 }\r
1296 DataItem->Data.Ptr = NULL; \r
1297 }\r
1298\r
7278ce8c 1299 return Status;\r
1f6729ff 1300}\r
1301\r
1302/**\r
1303 The work function is to set the gateway addresses manually for the EFI IPv4 \r
1304 network stack that is running on the communication device that this EFI IPv4 \r
1305 Configuration Protocol manages. It is not configurable when the policy is\r
1306 Ip4Config2PolicyDhcp. The gateway addresses must be unicast IPv4 addresses.\r
1307\r
1308 @param[in] Instance The pointer to the IP4 config2 instance data.\r
1309 @param[in] DataSize The size of the buffer pointed to by Data in bytes.\r
1310 @param[in] Data The data buffer to set. This points to an array of\r
1311 EFI_IPv6_ADDRESS instances.\r
1312\r
1313 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.\r
1314 @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set\r
1315 under the current policy.\r
1316 @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.\r
1317 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to complete the operation.\r
1318 @retval EFI_ABORTED The manual gateway addresses to be set equal the\r
1319 current configuration.\r
1320 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6\r
1321 network stack was set.\r
1322\r
1323**/\r
1324EFI_STATUS\r
1325Ip4Config2SetGateway (\r
1326 IN IP4_CONFIG2_INSTANCE *Instance,\r
1327 IN UINTN DataSize,\r
1328 IN VOID *Data\r
1329 )\r
1330{\r
1331 IP4_SERVICE *IpSb;\r
1332 IP4_CONFIG2_DATA_ITEM *DataItem;\r
1333 IP4_ADDR Gateway;\r
1334\r
1335 UINTN Index1;\r
1336 UINTN Index2;\r
1337 EFI_IPv4_ADDRESS *OldGateway;\r
1338 EFI_IPv4_ADDRESS *NewGateway;\r
1339 UINTN OldGatewayCount;\r
1340 UINTN NewGatewayCount;\r
1341 BOOLEAN OneRemoved;\r
1342 BOOLEAN OneAdded;\r
1343 VOID *Tmp;\r
1344\r
1345 if ((DataSize % sizeof (EFI_IPv4_ADDRESS) != 0) || (DataSize == 0)) {\r
1346 return EFI_BAD_BUFFER_SIZE;\r
1347 }\r
1348\r
1349 if (Instance->Policy != Ip4Config2PolicyStatic) {\r
1350 return EFI_WRITE_PROTECTED;\r
1351 }\r
1352\r
01b5ac88 1353 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
1f6729ff 1354\r
1355 NewGateway = (EFI_IPv4_ADDRESS *) Data;\r
1356 NewGatewayCount = DataSize / sizeof (EFI_IPv4_ADDRESS);\r
1357 for (Index1 = 0; Index1 < NewGatewayCount; Index1++) {\r
1358 CopyMem (&Gateway, NewGateway + Index1, sizeof (IP4_ADDR));\r
1f6729ff 1359\r
01b5ac88
FS
1360 if ((IpSb->DefaultInterface->SubnetMask != 0) && \r
1361 !NetIp4IsUnicast (NTOHL (Gateway), IpSb->DefaultInterface->SubnetMask)) {\r
1f6729ff 1362 return EFI_INVALID_PARAMETER;\r
1363 }\r
1364\r
1365 for (Index2 = Index1 + 1; Index2 < NewGatewayCount; Index2++) {\r
1366 if (EFI_IP4_EQUAL (NewGateway + Index1, NewGateway + Index2)) {\r
1367 return EFI_INVALID_PARAMETER;\r
1368 }\r
1369 }\r
1370 }\r
1371 \r
1f6729ff 1372 DataItem = &Instance->DataItem[Ip4Config2DataTypeGateway];\r
1373 OldGateway = DataItem->Data.Gateway;\r
1374 OldGatewayCount = DataItem->DataSize / sizeof (EFI_IPv4_ADDRESS);\r
1375 OneRemoved = FALSE;\r
1376 OneAdded = FALSE;\r
1377\r
1378 if (NewGatewayCount != OldGatewayCount) {\r
1379 Tmp = AllocatePool (DataSize);\r
1380 if (Tmp == NULL) {\r
1381 return EFI_OUT_OF_RESOURCES;\r
1382 }\r
1383 } else {\r
1384 Tmp = NULL;\r
1385 }\r
1386\r
1387 for (Index1 = 0; Index1 < OldGatewayCount; Index1++) {\r
1388 //\r
1389 // Remove this route entry.\r
1390 //\r
1391 CopyMem (&Gateway, OldGateway + Index1, sizeof (IP4_ADDR));\r
1392 Ip4DelRoute (\r
1393 IpSb->DefaultRouteTable,\r
1394 IP4_ALLZERO_ADDRESS,\r
1395 IP4_ALLZERO_ADDRESS,\r
1396 NTOHL (Gateway)\r
1397 );\r
1398 OneRemoved = TRUE;\r
1399\r
1400 }\r
1401\r
1402 for (Index1 = 0; Index1 < NewGatewayCount; Index1++) {\r
1403 CopyMem (&Gateway, NewGateway + Index1, sizeof (IP4_ADDR));\r
1404 Ip4AddRoute (\r
1405 IpSb->DefaultRouteTable,\r
1406 IP4_ALLZERO_ADDRESS,\r
1407 IP4_ALLZERO_ADDRESS,\r
1408 NTOHL (Gateway)\r
1409 ); \r
1410\r
1411 OneAdded = TRUE;\r
1412 }\r
1413\r
1414\r
1415 if (!OneRemoved && !OneAdded) {\r
1416 DataItem->Status = EFI_SUCCESS;\r
1417 return EFI_ABORTED;\r
1418 } else {\r
1419\r
1420 if (Tmp != NULL) {\r
1421 if (DataItem->Data.Ptr != NULL) {\r
1422 FreePool (DataItem->Data.Ptr);\r
1423 }\r
1424 DataItem->Data.Ptr = Tmp;\r
1425 }\r
1426\r
1427 CopyMem (DataItem->Data.Ptr, Data, DataSize);\r
1428 DataItem->DataSize = DataSize;\r
1429 DataItem->Status = EFI_SUCCESS;\r
1430 return EFI_SUCCESS;\r
1431 }\r
1432\r
1433}\r
1434\r
1435/**\r
1436 The work function is to set the DNS server list for the EFI IPv4 network \r
1437 stack running on the communication device that this EFI_IP4_CONFIG2_PROTOCOL \r
1438 manages. It is not configurable when the policy is Ip4Config2PolicyDhcp. \r
1439 The DNS server addresses must be unicast IPv4 addresses.\r
1440\r
1441 @param[in] Instance The pointer to the IP4 config2 instance data.\r
1442 @param[in] DataSize The size of the buffer pointed to by Data in bytes.\r
1443 @param[in] Data The data buffer to set, points to an array of\r
1444 EFI_IPv4_ADDRESS instances.\r
1445\r
1446 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.\r
1447 @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set\r
1448 under the current policy.\r
1449 @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.\r
1450 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation.\r
1451 @retval EFI_ABORTED The DNS server addresses to be set equal the current\r
1452 configuration.\r
1453 @retval EFI_SUCCESS The specified configuration data for the EFI IPv4\r
1454 network stack was set.\r
1455\r
1456**/\r
1457EFI_STATUS\r
1458Ip4Config2SetDnsServer (\r
1459 IN IP4_CONFIG2_INSTANCE *Instance,\r
1460 IN UINTN DataSize,\r
1461 IN VOID *Data\r
1462 )\r
1463{\r
eab40164
JW
1464 IP4_CONFIG2_DATA_ITEM *Item;\r
1465\r
1466 Item = NULL;\r
1467\r
1f6729ff 1468 if (Instance->Policy != Ip4Config2PolicyStatic) {\r
1469 return EFI_WRITE_PROTECTED;\r
1470 }\r
1471\r
eab40164
JW
1472 Item = &Instance->DataItem[Ip4Config2DataTypeDnsServer];\r
1473\r
1474 if (DATA_ATTRIB_SET (Item->Attribute, DATA_ATTRIB_VOLATILE)) {\r
1475 REMOVE_DATA_ATTRIB (Item->Attribute, DATA_ATTRIB_VOLATILE);\r
1476 }\r
1477\r
9feefde0 1478 return Ip4Config2SetDnsServerWorker (Instance, DataSize, Data);\r
1f6729ff 1479}\r
1480\r
1481/**\r
1482 Generate the operational state of the interface this IP4 config2 instance manages\r
1483 and output in EFI_IP4_CONFIG2_INTERFACE_INFO.\r
1484\r
1485 @param[in] IpSb The pointer to the IP4 service binding instance.\r
1486 @param[out] IfInfo The pointer to the IP4 config2 interface information structure.\r
1487\r
1488**/\r
1489VOID\r
1490Ip4Config2InitIfInfo (\r
1491 IN IP4_SERVICE *IpSb,\r
1492 OUT EFI_IP4_CONFIG2_INTERFACE_INFO *IfInfo\r
1493 )\r
1494{\r
1495 IfInfo->Name[0] = L'e';\r
1496 IfInfo->Name[1] = L't';\r
1497 IfInfo->Name[2] = L'h';\r
1498 IfInfo->Name[3] = (CHAR16) (L'0' + IpSb->Ip4Config2Instance.IfIndex);\r
1499 IfInfo->Name[4] = 0;\r
1500\r
1501 IfInfo->IfType = IpSb->SnpMode.IfType;\r
1502 IfInfo->HwAddressSize = IpSb->SnpMode.HwAddressSize;\r
1503 CopyMem (&IfInfo->HwAddress, &IpSb->SnpMode.CurrentAddress, IfInfo->HwAddressSize);\r
1504}\r
1505\r
1506/**\r
1507 The event handle routine when DHCPv4 process is finished or is updated.\r
1508\r
1509 @param[in] Event Not used.\r
1510 @param[in] Context The pointer to the IP4 configuration instance data.\r
1511\r
1512**/\r
1513VOID\r
1514EFIAPI\r
1515Ip4Config2OnDhcp4Event (\r
1516 IN EFI_EVENT Event,\r
1517 IN VOID *Context\r
1518 )\r
1519{\r
1520 return ;\r
1521}\r
1522\r
1523\r
1524/**\r
1525 Set the configuration for the EFI IPv4 network stack running on the communication\r
1526 device this EFI_IP4_CONFIG2_PROTOCOL instance manages.\r
1527\r
1528 This function is used to set the configuration data of type DataType for the EFI\r
1529 IPv4 network stack that is running on the communication device that this EFI IPv4\r
1530 Configuration Protocol instance manages.\r
1531\r
1532 DataSize is used to calculate the count of structure instances in the Data for\r
1533 a DataType in which multiple structure instances are allowed.\r
1534\r
1535 This function is always non-blocking. When setting some type of configuration data,\r
1536 an asynchronous process is invoked to check the correctness of the data, such as\r
1537 performing Duplicate Address Detection on the manually set local IPv4 addresses.\r
1538 EFI_NOT_READY is returned immediately to indicate that such an asynchronous process\r
1539 is invoked, and the process is not finished yet. The caller wanting to get the result\r
1540 of the asynchronous process is required to call RegisterDataNotify() to register an\r
1541 event on the specified configuration data. Once the event is signaled, the caller\r
1542 can call GetData() to obtain the configuration data and know the result.\r
1543 For other types of configuration data that do not require an asynchronous configuration\r
1544 process, the result of the operation is immediately returned.\r
1545\r
1546 @param[in] This The pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.\r
1547 @param[in] DataType The type of data to set.\r
1548 @param[in] DataSize Size of the buffer pointed to by Data in bytes.\r
1549 @param[in] Data The data buffer to set. The type of the data buffer is\r
1550 associated with the DataType.\r
1551\r
1552 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6\r
1553 network stack was set successfully.\r
1554 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:\r
1555 - This is NULL.\r
1556 - Data is NULL.\r
1557 - One or more fields in Data do not match the requirement of the\r
1558 data type indicated by DataType.\r
1559 @retval EFI_WRITE_PROTECTED The specified configuration data is read-only or the specified\r
1560 configuration data cannot be set under the current policy.\r
1561 @retval EFI_ACCESS_DENIED Another set operation on the specified configuration\r
1562 data is already in process.\r
1563 @retval EFI_NOT_READY An asynchronous process was invoked to set the specified\r
1564 configuration data, and the process is not finished yet.\r
1565 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type\r
1566 indicated by DataType.\r
1567 @retval EFI_UNSUPPORTED This DataType is not supported.\r
1568 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.\r
1569 @retval EFI_DEVICE_ERROR An unexpected system error or network error occurred.\r
1570\r
1571**/\r
1572EFI_STATUS\r
1573EFIAPI\r
1574EfiIp4Config2SetData (\r
1575 IN EFI_IP4_CONFIG2_PROTOCOL *This,\r
1576 IN EFI_IP4_CONFIG2_DATA_TYPE DataType,\r
1577 IN UINTN DataSize,\r
1578 IN VOID *Data\r
1579 )\r
1580{\r
1581 EFI_TPL OldTpl;\r
1582 EFI_STATUS Status;\r
1583 IP4_CONFIG2_INSTANCE *Instance;\r
1584 IP4_SERVICE *IpSb;\r
1585\r
1586 if ((This == NULL) || (Data == NULL)) {\r
1587 return EFI_INVALID_PARAMETER;\r
1588 }\r
1589\r
1590 if (DataType >= Ip4Config2DataTypeMaximum) {\r
1591 return EFI_UNSUPPORTED;\r
1592 }\r
1593\r
1594 Instance = IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This);\r
1595 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
1596 NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);\r
1597\r
1598\r
1599 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1600\r
1601 Status = Instance->DataItem[DataType].Status;\r
1602 if (Status != EFI_NOT_READY) {\r
1603\r
1604 if (Instance->DataItem[DataType].SetData == NULL) {\r
1605 //\r
1606 // This type of data is readonly.\r
1607 //\r
1608 Status = EFI_WRITE_PROTECTED;\r
1609 } else {\r
1610\r
1611 Status = Instance->DataItem[DataType].SetData (Instance, DataSize, Data);\r
1612 if (!EFI_ERROR (Status)) {\r
1613 //\r
1614 // Fire up the events registered with this type of data.\r
1615 //\r
1616 NetMapIterate (&Instance->DataItem[DataType].EventMap, Ip4Config2SignalEvent, NULL);\r
1617 Ip4Config2WriteConfigData (IpSb->MacString, Instance);\r
1618 } else if (Status == EFI_ABORTED) {\r
1619 //\r
1620 // The SetData is aborted because the data to set is the same with\r
1621 // the one maintained.\r
1622 //\r
1623 Status = EFI_SUCCESS;\r
1624 NetMapIterate (&Instance->DataItem[DataType].EventMap, Ip4Config2SignalEvent, NULL);\r
1625 }\r
1626 }\r
1627 } else {\r
1628 //\r
1629 // Another asynchornous process is on the way.\r
1630 //\r
1631 Status = EFI_ACCESS_DENIED;\r
1632 }\r
1633\r
1634 gBS->RestoreTPL (OldTpl);\r
1635\r
1636 return Status;\r
1637}\r
1638\r
1639/**\r
1640 Get the configuration data for the EFI IPv4 network stack running on the communication\r
1641 device that this EFI_IP4_CONFIG2_PROTOCOL instance manages.\r
1642\r
1643 This function returns the configuration data of type DataType for the EFI IPv4 network\r
1644 stack running on the communication device that this EFI IPv4 Configuration Protocol instance\r
1645 manages.\r
1646\r
1647 The caller is responsible for allocating the buffer used to return the specified\r
1648 configuration data. The required size will be returned to the caller if the size of\r
1649 the buffer is too small.\r
1650\r
1651 EFI_NOT_READY is returned if the specified configuration data is not ready due to an\r
1652 asynchronous configuration process already in progress. The caller can call RegisterDataNotify()\r
1653 to register an event on the specified configuration data. Once the asynchronous configuration\r
1654 process is finished, the event will be signaled, and a subsequent GetData() call will return\r
1655 the specified configuration data.\r
1656\r
1657 @param[in] This Pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.\r
1658 @param[in] DataType The type of data to get.\r
1659 @param[in, out] DataSize On input, in bytes, the size of Data. On output, in bytes, the\r
1660 size of buffer required to store the specified configuration data.\r
1661 @param[in] Data The data buffer in which the configuration data is returned. The\r
1662 type of the data buffer is associated with the DataType.\r
1663 This is an optional parameter that may be NULL.\r
1664\r
1665 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1666 @retval EFI_INVALID_PARAMETER One or more of the followings are TRUE:\r
1667 - This is NULL.\r
1668 - DataSize is NULL.\r
1669 - Data is NULL if *DataSize is not zero.\r
1670 @retval EFI_BUFFER_TOO_SMALL The size of Data is too small for the specified configuration data,\r
1671 and the required size is returned in DataSize.\r
1672 @retval EFI_NOT_READY The specified configuration data is not ready due to an\r
1673 asynchronous configuration process already in progress.\r
1674 @retval EFI_NOT_FOUND The specified configuration data is not found.\r
1675\r
1676**/\r
1677EFI_STATUS\r
1678EFIAPI\r
1679EfiIp4Config2GetData (\r
1680 IN EFI_IP4_CONFIG2_PROTOCOL *This,\r
1681 IN EFI_IP4_CONFIG2_DATA_TYPE DataType,\r
1682 IN OUT UINTN *DataSize,\r
1683 IN VOID *Data OPTIONAL\r
1684 )\r
1685{\r
1686 EFI_TPL OldTpl;\r
1687 EFI_STATUS Status;\r
1688 IP4_CONFIG2_INSTANCE *Instance;\r
1689 IP4_CONFIG2_DATA_ITEM *DataItem;\r
1690\r
1691 if ((This == NULL) || (DataSize == NULL) || ((*DataSize != 0) && (Data == NULL))) {\r
1692 return EFI_INVALID_PARAMETER;\r
1693 }\r
1694\r
1695 if (DataType >= Ip4Config2DataTypeMaximum) {\r
1696 return EFI_NOT_FOUND;\r
1697 }\r
1698\r
1699 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1700\r
1701 Instance = IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This);\r
1702 DataItem = &Instance->DataItem[DataType];\r
1703\r
1704 Status = Instance->DataItem[DataType].Status;\r
1705 if (!EFI_ERROR (Status)) {\r
1706\r
1707 if (DataItem->GetData != NULL) {\r
1708\r
1709 Status = DataItem->GetData (Instance, DataSize, Data);\r
1710 } else if (*DataSize < Instance->DataItem[DataType].DataSize) {\r
1711 //\r
1712 // Update the buffer length.\r
1713 //\r
1714 *DataSize = Instance->DataItem[DataType].DataSize;\r
1715 Status = EFI_BUFFER_TOO_SMALL;\r
1716 } else {\r
1717\r
1718 *DataSize = Instance->DataItem[DataType].DataSize;\r
1719 CopyMem (Data, Instance->DataItem[DataType].Data.Ptr, *DataSize);\r
1720 }\r
1721 }\r
1722\r
1723 gBS->RestoreTPL (OldTpl);\r
1724\r
1725 return Status;\r
1726}\r
1727\r
1728/**\r
1729 Register an event that is signaled whenever a configuration process on the specified\r
1730 configuration data is done.\r
1731\r
1732 This function registers an event that is to be signaled whenever a configuration\r
1733 process on the specified configuration data is performed. An event can be registered\r
1734 for a different DataType simultaneously. The caller is responsible for determining\r
1735 which type of configuration data causes the signaling of the event in such an event.\r
1736\r
1737 @param[in] This Pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.\r
1738 @param[in] DataType The type of data to unregister the event for.\r
1739 @param[in] Event The event to register.\r
1740\r
1741 @retval EFI_SUCCESS The notification event for the specified configuration data is\r
1742 registered.\r
1743 @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.\r
1744 @retval EFI_UNSUPPORTED The configuration data type specified by DataType is not\r
1745 supported.\r
1746 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.\r
1747 @retval EFI_ACCESS_DENIED The Event is already registered for the DataType.\r
1748\r
1749**/\r
1750EFI_STATUS\r
1751EFIAPI\r
1752EfiIp4Config2RegisterDataNotify (\r
1753 IN EFI_IP4_CONFIG2_PROTOCOL *This,\r
1754 IN EFI_IP4_CONFIG2_DATA_TYPE DataType,\r
1755 IN EFI_EVENT Event\r
1756 )\r
1757{\r
1758 EFI_TPL OldTpl;\r
1759 EFI_STATUS Status;\r
1760 IP4_CONFIG2_INSTANCE *Instance;\r
1761 NET_MAP *EventMap;\r
1762 NET_MAP_ITEM *Item;\r
1763\r
1764 if ((This == NULL) || (Event == NULL)) {\r
1765 return EFI_INVALID_PARAMETER;\r
1766 }\r
1767\r
1768 if (DataType >= Ip4Config2DataTypeMaximum) {\r
1769 return EFI_UNSUPPORTED;\r
1770 }\r
1771\r
1772 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1773\r
1774 Instance = IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This);\r
1775 EventMap = &Instance->DataItem[DataType].EventMap;\r
1776\r
1777 //\r
1778 // Check whether this event is already registered for this DataType.\r
1779 //\r
1780 Item = NetMapFindKey (EventMap, Event);\r
1781 if (Item == NULL) {\r
1782\r
1783 Status = NetMapInsertTail (EventMap, Event, NULL);\r
1784\r
1785 if (EFI_ERROR (Status)) {\r
1786\r
1787 Status = EFI_OUT_OF_RESOURCES;\r
1788 }\r
1789\r
1790 } else {\r
1791\r
1792 Status = EFI_ACCESS_DENIED;\r
1793 }\r
1794\r
1795 gBS->RestoreTPL (OldTpl);\r
1796\r
1797 return Status;\r
1798}\r
1799\r
1800/**\r
1801 Remove a previously registered event for the specified configuration data.\r
1802\r
1803 @param This The pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.\r
1804 @param DataType The type of data to remove from the previously\r
1805 registered event.\r
1806 @param Event The event to be unregistered.\r
1807\r
1808 @retval EFI_SUCCESS The event registered for the specified\r
1809 configuration data was removed.\r
1810 @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.\r
1811 @retval EFI_NOT_FOUND The Event has not been registered for the\r
1812 specified DataType.\r
1813\r
1814**/\r
1815EFI_STATUS\r
1816EFIAPI\r
1817EfiIp4Config2UnregisterDataNotify (\r
1818 IN EFI_IP4_CONFIG2_PROTOCOL *This,\r
1819 IN EFI_IP4_CONFIG2_DATA_TYPE DataType,\r
1820 IN EFI_EVENT Event\r
1821 )\r
1822{\r
1823 EFI_TPL OldTpl;\r
1824 EFI_STATUS Status;\r
1825 IP4_CONFIG2_INSTANCE *Instance;\r
1826 NET_MAP_ITEM *Item;\r
1827\r
1828 if ((This == NULL) || (Event == NULL)) {\r
1829 return EFI_INVALID_PARAMETER;\r
1830 }\r
1831\r
1832 if (DataType >= Ip4Config2DataTypeMaximum) {\r
1833 return EFI_NOT_FOUND;\r
1834 }\r
1835\r
1836 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1837\r
1838 Instance = IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This);\r
1839\r
1840 Item = NetMapFindKey (&Instance->DataItem[DataType].EventMap, Event);\r
1841 if (Item != NULL) {\r
1842\r
1843 NetMapRemoveItem (&Instance->DataItem[DataType].EventMap, Item, NULL);\r
1844 Status = EFI_SUCCESS;\r
1845 } else {\r
1846\r
1847 Status = EFI_NOT_FOUND;\r
1848 }\r
1849\r
1850 gBS->RestoreTPL (OldTpl);\r
1851\r
1852 return Status;\r
1853}\r
1854\r
1855/**\r
1856 Initialize an IP4_CONFIG2_INSTANCE.\r
1857\r
1858 @param[out] Instance The buffer of IP4_CONFIG2_INSTANCE to be initialized.\r
1859\r
1860 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation.\r
1861 @retval EFI_SUCCESS The IP4_CONFIG2_INSTANCE initialized successfully.\r
1862\r
1863**/\r
1864EFI_STATUS\r
1865Ip4Config2InitInstance (\r
1866 OUT IP4_CONFIG2_INSTANCE *Instance\r
1867 )\r
1868{\r
1869 IP4_SERVICE *IpSb;\r
1870 IP4_CONFIG2_INSTANCE *TmpInstance;\r
1871 LIST_ENTRY *Entry;\r
1872 EFI_STATUS Status;\r
1873 UINTN Index;\r
1874 UINT16 IfIndex;\r
1875 IP4_CONFIG2_DATA_ITEM *DataItem;\r
1876\r
1877\r
1878 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
1879\r
1880 Instance->Signature = IP4_CONFIG2_INSTANCE_SIGNATURE;\r
1881\r
1882\r
1883 //\r
1884 // Determine the index of this interface.\r
1885 //\r
1886 IfIndex = 0;\r
1887 NET_LIST_FOR_EACH (Entry, &mIp4Config2InstanceList) {\r
1888 TmpInstance = NET_LIST_USER_STRUCT_S (Entry, IP4_CONFIG2_INSTANCE, Link, IP4_CONFIG2_INSTANCE_SIGNATURE);\r
1889\r
1890 if (TmpInstance->IfIndex > IfIndex) {\r
1891 //\r
1892 // There is a sequence hole because some interface is down.\r
1893 //\r
1894 break;\r
1895 }\r
1896\r
1897 IfIndex++;\r
1898 }\r
1899\r
1900 Instance->IfIndex = IfIndex;\r
1901 NetListInsertBefore (Entry, &Instance->Link);\r
1902\r
1903 for (Index = 0; Index < Ip4Config2DataTypeMaximum; Index++) {\r
1904 //\r
1905 // Initialize the event map for each data item.\r
1906 //\r
1907 NetMapInit (&Instance->DataItem[Index].EventMap);\r
1908 }\r
1909\r
1910 \r
1911 //\r
1912 // Initialize each data type: associate storage and set data size for the\r
1913 // fixed size data types, hook the SetData function, set the data attribute.\r
1914 //\r
1915 DataItem = &Instance->DataItem[Ip4Config2DataTypeInterfaceInfo];\r
1916 DataItem->GetData = Ip4Config2GetIfInfo;\r
1917 DataItem->Data.Ptr = &Instance->InterfaceInfo;\r
1918 DataItem->DataSize = sizeof (Instance->InterfaceInfo);\r
1919 SET_DATA_ATTRIB (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED | DATA_ATTRIB_VOLATILE);\r
1920 Ip4Config2InitIfInfo (IpSb, &Instance->InterfaceInfo);\r
1921\r
1922 DataItem = &Instance->DataItem[Ip4Config2DataTypePolicy];\r
1923 DataItem->SetData = Ip4Config2SetPolicy;\r
1924 DataItem->Data.Ptr = &Instance->Policy;\r
1925 DataItem->DataSize = sizeof (Instance->Policy);\r
7648748e 1926 Instance->Policy = Ip4Config2PolicyStatic;\r
1f6729ff 1927 SET_DATA_ATTRIB (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED);\r
1928\r
1929 DataItem = &Instance->DataItem[Ip4Config2DataTypeManualAddress];\r
1930 DataItem->SetData = Ip4Config2SetMaunualAddress;\r
1931 DataItem->Status = EFI_NOT_FOUND;\r
1932\r
1933 DataItem = &Instance->DataItem[Ip4Config2DataTypeGateway];\r
1934 DataItem->SetData = Ip4Config2SetGateway;\r
1935 DataItem->Status = EFI_NOT_FOUND;\r
1936\r
1937 DataItem = &Instance->DataItem[Ip4Config2DataTypeDnsServer];\r
1938 DataItem->SetData = Ip4Config2SetDnsServer;\r
1939 DataItem->Status = EFI_NOT_FOUND;\r
1940\r
1941 //\r
1942 // Create the event used for DHCP.\r
1943 //\r
1944 Status = gBS->CreateEvent (\r
1945 EVT_NOTIFY_SIGNAL,\r
1946 TPL_CALLBACK,\r
1947 Ip4Config2OnDhcp4Event,\r
1948 Instance,\r
1949 &Instance->Dhcp4Event\r
1950 );\r
1951 ASSERT_EFI_ERROR (Status);\r
1952\r
1953 Instance->Configured = TRUE;\r
1954\r
1955 //\r
1956 // Try to read the config data from NV variable.\r
7648748e
JW
1957 // If not found, write initialized config data into NV variable \r
1958 // as a default config data.\r
1f6729ff 1959 //\r
1960 Status = Ip4Config2ReadConfigData (IpSb->MacString, Instance);\r
1961 if (Status == EFI_NOT_FOUND) {\r
1962 Status = Ip4Config2WriteConfigData (IpSb->MacString, Instance);\r
1963 }\r
1964\r
1965 if (EFI_ERROR (Status)) {\r
1966 return Status;\r
1967 }\r
7648748e 1968 \r
1f6729ff 1969 Instance->Ip4Config2.SetData = EfiIp4Config2SetData;\r
1970 Instance->Ip4Config2.GetData = EfiIp4Config2GetData;\r
1971 Instance->Ip4Config2.RegisterDataNotify = EfiIp4Config2RegisterDataNotify;\r
1972 Instance->Ip4Config2.UnregisterDataNotify = EfiIp4Config2UnregisterDataNotify;\r
1973\r
1974 //\r
1975 // Publish the IP4 configuration form\r
1976 //\r
1977 return Ip4Config2FormInit (Instance);\r
1978}\r
1979\r
1980\r
1981/**\r
1982 Release an IP4_CONFIG2_INSTANCE.\r
1983\r
1984 @param[in, out] Instance The buffer of IP4_CONFIG2_INSTANCE to be freed.\r
1985\r
1986**/\r
1987VOID\r
1988Ip4Config2CleanInstance (\r
1989 IN OUT IP4_CONFIG2_INSTANCE *Instance\r
1990 )\r
1991{\r
1992 UINTN Index;\r
1993 IP4_CONFIG2_DATA_ITEM *DataItem;\r
1994\r
1995 if (Instance->DeclineAddress != NULL) {\r
1996 FreePool (Instance->DeclineAddress);\r
1997 }\r
1998\r
1999 if (!Instance->Configured) {\r
2000 return ;\r
2001 }\r
2002\r
2003 if (Instance->Dhcp4Handle != NULL) {\r
2004\r
2005 Ip4Config2DestroyDhcp4 (Instance);\r
2006 }\r
2007\r
2008 //\r
2009 // Close the event.\r
2010 //\r
2011 if (Instance->Dhcp4Event != NULL) {\r
2012 gBS->CloseEvent (Instance->Dhcp4Event);\r
4f0f2316 2013 Instance->Dhcp4Event = NULL;\r
1f6729ff 2014 }\r
2015\r
2016 for (Index = 0; Index < Ip4Config2DataTypeMaximum; Index++) {\r
2017\r
2018 DataItem = &Instance->DataItem[Index];\r
2019\r
2020 if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED)) {\r
2021 if (DataItem->Data.Ptr != NULL) {\r
2022 FreePool (DataItem->Data.Ptr);\r
2023 }\r
2024 DataItem->Data.Ptr = NULL;\r
2025 DataItem->DataSize = 0;\r
2026 }\r
2027\r
2028 NetMapClean (&Instance->DataItem[Index].EventMap);\r
2029 }\r
2030\r
2031 Ip4Config2FormUnload (Instance);\r
2032\r
2033 RemoveEntryList (&Instance->Link);\r
2034}\r
2035\r
7648748e
JW
2036/**\r
2037 The event handle for IP4 auto reconfiguration. The original default\r
2038 interface and route table will be removed as the default.\r
2039\r
2040 @param[in] Context The IP4 service binding instance.\r
2041\r
2042**/\r
2043VOID\r
2044EFIAPI\r
2045Ip4AutoReconfigCallBackDpc (\r
2046 IN VOID *Context\r
2047 )\r
2048{\r
2049 IP4_SERVICE *IpSb;\r
2050\r
2051 IpSb = (IP4_SERVICE *) Context;\r
2052 NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);\r
2053\r
2054 if (IpSb->State > IP4_SERVICE_UNSTARTED) {\r
2055 IpSb->State = IP4_SERVICE_UNSTARTED;\r
2056 }\r
2057 \r
2058 IpSb->Reconfig = TRUE;\r
2059\r
2060 Ip4StartAutoConfig (&IpSb->Ip4Config2Instance);\r
2061\r
2062 return ;\r
2063}\r
2064\r
2065\r
2066/**\r
2067 Request Ip4AutoReconfigCallBackDpc as a DPC at TPL_CALLBACK.\r
2068\r
2069 @param Event The event that is signalled.\r
2070 @param Context The IP4 service binding instance.\r
2071\r
2072**/\r
2073VOID\r
2074EFIAPI\r
2075Ip4AutoReconfigCallBack (\r
2076 IN EFI_EVENT Event,\r
2077 IN VOID *Context\r
2078 )\r
2079{\r
2080 //\r
2081 // Request Ip4AutoReconfigCallBackDpc as a DPC at TPL_CALLBACK\r
2082 //\r
2083 QueueDpc (TPL_CALLBACK, Ip4AutoReconfigCallBackDpc, Context);\r
2084}\r
2085\r