]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Config2Impl.c
MdeModulePkg/Ip4Dxe: Ignore duplicated DNS address check
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Config2Impl.c
... / ...
CommitLineData
1/** @file\r
2 The implementation of EFI IPv4 Configuration II Protocol.\r
3\r
4 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>\r
5 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r
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
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
145 if (IpSb->State == IP4_SERVICE_CONFIGED || IpSb->State == IP4_SERVICE_STARTED) {\r
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
418 for (Index = IP4_MASK_MAX; Index >= 0; Index--) {\r
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
467 @param[in] IpSb The pointer to the IP4 service binding instance.\r
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
477 IN IP4_SERVICE *IpSb,\r
478 IN IP4_ADDR StationAddress,\r
479 IN IP4_ADDR SubnetMask\r
480 )\r
481{\r
482 EFI_STATUS Status;\r
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
490 IpIf = IpSb->DefaultInterface;\r
491 ASSERT (IpIf != NULL);\r
492\r
493 if ((IpIf->Ip == StationAddress) && (IpIf->SubnetMask == SubnetMask)) {\r
494 IpSb->State = IP4_SERVICE_CONFIGED;\r
495 return EFI_SUCCESS;\r
496 }\r
497\r
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
506\r
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
517\r
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
528 }\r
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
582 IpSb->Reconfig = FALSE;\r
583 \r
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
610 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
611\r
612 Status = Ip4Config2SetDefaultAddr (IpSb, StationAddress, SubnetMask);\r
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
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
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 EFI_IPv4_ADDRESS *OldDns;\r
709 EFI_IPv4_ADDRESS *NewDns;\r
710 UINTN OldDnsCount;\r
711 UINTN NewDnsCount;\r
712 IP4_CONFIG2_DATA_ITEM *Item;\r
713 BOOLEAN OneAdded;\r
714 VOID *Tmp;\r
715 IP4_ADDR DnsAddress;\r
716\r
717 if ((DataSize % sizeof (EFI_IPv4_ADDRESS) != 0) || (DataSize == 0)) {\r
718 return EFI_BAD_BUFFER_SIZE;\r
719 }\r
720\r
721 Item = &Instance->DataItem[Ip4Config2DataTypeDnsServer];\r
722 NewDns = (EFI_IPv4_ADDRESS *) Data;\r
723 OldDns = Item->Data.DnsServers;\r
724 NewDnsCount = DataSize / sizeof (EFI_IPv4_ADDRESS); \r
725 OldDnsCount = Item->DataSize / sizeof (EFI_IPv4_ADDRESS);\r
726 OneAdded = FALSE;\r
727\r
728 if (NewDnsCount != OldDnsCount) {\r
729 Tmp = AllocatePool (DataSize);\r
730 if (Tmp == NULL) {\r
731 return EFI_OUT_OF_RESOURCES;\r
732 }\r
733 } else {\r
734 Tmp = NULL;\r
735 }\r
736\r
737 for (NewIndex = 0; NewIndex < NewDnsCount; NewIndex++) {\r
738 CopyMem (&DnsAddress, NewDns + NewIndex, sizeof (IP4_ADDR));\r
739 if (IP4_IS_UNSPECIFIED (NTOHL (DnsAddress)) || IP4_IS_LOCAL_BROADCAST (NTOHL (DnsAddress))) {\r
740 //\r
741 // The dns server address must be unicast.\r
742 //\r
743 if (Tmp != NULL) {\r
744 FreePool (Tmp);\r
745 }\r
746 return EFI_INVALID_PARAMETER;\r
747 }\r
748\r
749 if (OneAdded) {\r
750 //\r
751 // If any address in the new setting is not in the old settings, skip the\r
752 // comparision below.\r
753 //\r
754 continue;\r
755 }\r
756\r
757 for (OldIndex = 0; OldIndex < OldDnsCount; OldIndex++) {\r
758 if (EFI_IP4_EQUAL (NewDns + NewIndex, OldDns + OldIndex)) {\r
759 //\r
760 // If found break out.\r
761 //\r
762 break;\r
763 }\r
764 }\r
765\r
766 if (OldIndex == OldDnsCount) {\r
767 OneAdded = TRUE;\r
768 }\r
769 }\r
770\r
771 if (!OneAdded && (DataSize == Item->DataSize)) {\r
772 //\r
773 // No new item is added and the size is the same.\r
774 //\r
775 Item->Status = EFI_SUCCESS;\r
776 return EFI_ABORTED;\r
777 } else {\r
778 if (Tmp != NULL) {\r
779 if (Item->Data.Ptr != NULL) {\r
780 FreePool (Item->Data.Ptr);\r
781 } \r
782 Item->Data.Ptr = Tmp;\r
783 }\r
784\r
785 CopyMem (Item->Data.Ptr, Data, DataSize);\r
786 Item->DataSize = DataSize;\r
787 Item->Status = EFI_SUCCESS;\r
788 return EFI_SUCCESS;\r
789 }\r
790}\r
791\r
792\r
793\r
794/**\r
795 Callback function when DHCP process finished. It will save the\r
796 retrieved IP configure parameter from DHCP to the NVRam.\r
797\r
798 @param Event The callback event\r
799 @param Context Opaque context to the callback\r
800\r
801 @return None\r
802\r
803**/\r
804VOID\r
805EFIAPI\r
806Ip4Config2OnDhcp4Complete (\r
807 IN EFI_EVENT Event,\r
808 IN VOID *Context\r
809 )\r
810{\r
811 IP4_CONFIG2_INSTANCE *Instance;\r
812 EFI_DHCP4_MODE_DATA Dhcp4Mode;\r
813 EFI_STATUS Status;\r
814 IP4_ADDR StationAddress;\r
815 IP4_ADDR SubnetMask;\r
816 IP4_ADDR GatewayAddress;\r
817 UINT32 Index;\r
818 UINT32 OptionCount;\r
819 EFI_DHCP4_PACKET_OPTION **OptionList;\r
820\r
821 Instance = (IP4_CONFIG2_INSTANCE *) Context;\r
822 ASSERT (Instance->Dhcp4 != NULL);\r
823\r
824 //\r
825 // Get the DHCP retrieved parameters\r
826 //\r
827 Status = Instance->Dhcp4->GetModeData (Instance->Dhcp4, &Dhcp4Mode);\r
828\r
829 if (EFI_ERROR (Status)) {\r
830 goto Exit;\r
831 }\r
832\r
833 if (Dhcp4Mode.State == Dhcp4Bound) {\r
834 StationAddress = EFI_NTOHL (Dhcp4Mode.ClientAddress);\r
835 SubnetMask = EFI_NTOHL (Dhcp4Mode.SubnetMask);\r
836 GatewayAddress = EFI_NTOHL (Dhcp4Mode.RouterAddress);\r
837\r
838 Status = Ip4Config2SetDefaultIf (Instance, StationAddress, SubnetMask, GatewayAddress);\r
839 if (EFI_ERROR (Status)) {\r
840 goto Exit;\r
841 }\r
842 \r
843 //\r
844 // Parse the ACK to get required DNS server information.\r
845 //\r
846 OptionCount = 0;\r
847 OptionList = NULL;\r
848\r
849 Status = Instance->Dhcp4->Parse (Instance->Dhcp4, Dhcp4Mode.ReplyPacket, &OptionCount, OptionList);\r
850 if (Status != EFI_BUFFER_TOO_SMALL) {\r
851 goto Exit;\r
852 }\r
853\r
854 OptionList = AllocateZeroPool (OptionCount * sizeof (EFI_DHCP4_PACKET_OPTION *));\r
855 if (OptionList == NULL) {\r
856 goto Exit;\r
857 }\r
858\r
859 Status = Instance->Dhcp4->Parse (Instance->Dhcp4, Dhcp4Mode.ReplyPacket, &OptionCount, OptionList);\r
860 if (EFI_ERROR (Status)) {\r
861 FreePool (OptionList);\r
862 goto Exit;\r
863 }\r
864\r
865 for (Index = 0; Index < OptionCount; Index++) {\r
866 //\r
867 // Look for DNS Server opcode (6).\r
868 //\r
869 if (OptionList[Index]->OpCode == DHCP4_TAG_DNS_SERVER) {\r
870 if (((OptionList[Index]->Length & 0x3) != 0) || (OptionList[Index]->Length == 0)) {\r
871 break;\r
872 }\r
873\r
874 Ip4Config2SetDnsServerWorker (Instance, OptionList[Index]->Length, &OptionList[Index]->Data[0]);\r
875 break;\r
876 }\r
877 }\r
878\r
879 FreePool (OptionList);\r
880\r
881 Instance->DhcpSuccess = TRUE;\r
882 }\r
883\r
884Exit:\r
885 Ip4Config2CleanDhcp4 (Instance);\r
886 DispatchDpc ();\r
887}\r
888\r
889\r
890/**\r
891 Start the DHCP configuration for this IP service instance.\r
892 It will locates the EFI_IP4_CONFIG2_PROTOCOL, then start the\r
893 DHCP configuration.\r
894\r
895 @param[in] Instance The IP4 config2 instance to configure\r
896\r
897 @retval EFI_SUCCESS The auto configuration is successfully started\r
898 @retval Others Failed to start auto configuration.\r
899\r
900**/\r
901EFI_STATUS\r
902Ip4StartAutoConfig (\r
903 IN IP4_CONFIG2_INSTANCE *Instance\r
904 )\r
905{\r
906 IP4_SERVICE *IpSb;\r
907 EFI_DHCP4_PROTOCOL *Dhcp4;\r
908 EFI_DHCP4_MODE_DATA Dhcp4Mode;\r
909 EFI_DHCP4_PACKET_OPTION *OptionList[1];\r
910 IP4_CONFIG2_DHCP4_OPTION ParaList;\r
911 EFI_STATUS Status;\r
912 \r
913\r
914 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
915\r
916 if (IpSb->State > IP4_SERVICE_UNSTARTED) {\r
917 return EFI_SUCCESS;\r
918 }\r
919\r
920 //\r
921 // A host must not invoke DHCP configuration if it is already\r
922 // participating in the DHCP configuraiton process.\r
923 //\r
924 if (Instance->Dhcp4Handle != NULL) {\r
925 return EFI_SUCCESS;\r
926 }\r
927\r
928 Status = NetLibCreateServiceChild (\r
929 IpSb->Controller,\r
930 IpSb->Image,\r
931 &gEfiDhcp4ServiceBindingProtocolGuid,\r
932 &Instance->Dhcp4Handle\r
933 );\r
934\r
935 if (Status == EFI_UNSUPPORTED) {\r
936 //\r
937 // No DHCPv4 Service Binding protocol, register a notify.\r
938 //\r
939 if (Instance->Dhcp4SbNotifyEvent == NULL) {\r
940 Instance->Dhcp4SbNotifyEvent = EfiCreateProtocolNotifyEvent (\r
941 &gEfiDhcp4ServiceBindingProtocolGuid,\r
942 TPL_CALLBACK,\r
943 Ip4Config2OnDhcp4SbInstalled,\r
944 (VOID *) Instance,\r
945 &Instance->Registration\r
946 );\r
947 }\r
948 }\r
949\r
950 if (EFI_ERROR (Status)) {\r
951 return Status;\r
952 }\r
953\r
954 if (Instance->Dhcp4SbNotifyEvent != NULL) {\r
955 gBS->CloseEvent (Instance->Dhcp4SbNotifyEvent);\r
956 }\r
957\r
958 Status = gBS->OpenProtocol (\r
959 Instance->Dhcp4Handle,\r
960 &gEfiDhcp4ProtocolGuid,\r
961 (VOID **) &Instance->Dhcp4,\r
962 IpSb->Image,\r
963 IpSb->Controller,\r
964 EFI_OPEN_PROTOCOL_BY_DRIVER\r
965 );\r
966 ASSERT_EFI_ERROR (Status);\r
967\r
968\r
969 //\r
970 // Check the current DHCP status, if the DHCP process has\r
971 // already finished, return now.\r
972 //\r
973 Dhcp4 = Instance->Dhcp4;\r
974 Status = Dhcp4->GetModeData (Dhcp4, &Dhcp4Mode);\r
975\r
976 if (Dhcp4Mode.State == Dhcp4Bound) {\r
977 Ip4Config2OnDhcp4Complete (NULL, Instance);\r
978 return EFI_SUCCESS;\r
979\r
980 }\r
981\r
982 //\r
983 // Try to start the DHCP process. Use most of the current\r
984 // DHCP configuration to avoid problems if some DHCP client\r
985 // yields the control of this DHCP service to us.\r
986 //\r
987 ParaList.Head.OpCode = DHCP4_TAG_PARA_LIST;\r
988 ParaList.Head.Length = 3;\r
989 ParaList.Head.Data[0] = DHCP4_TAG_NETMASK;\r
990 ParaList.Route = DHCP4_TAG_ROUTER;\r
991 ParaList.Dns = DHCP4_TAG_DNS_SERVER;\r
992 OptionList[0] = &ParaList.Head;\r
993 Dhcp4Mode.ConfigData.OptionCount = 1;\r
994 Dhcp4Mode.ConfigData.OptionList = OptionList;\r
995\r
996 Status = Dhcp4->Configure (Dhcp4, &Dhcp4Mode.ConfigData);\r
997\r
998 if (EFI_ERROR (Status)) {\r
999 return Status;\r
1000 }\r
1001 \r
1002 //\r
1003 // Start the DHCP process\r
1004 //\r
1005 Status = gBS->CreateEvent (\r
1006 EVT_NOTIFY_SIGNAL,\r
1007 TPL_CALLBACK,\r
1008 Ip4Config2OnDhcp4Complete,\r
1009 Instance,\r
1010 &Instance->Dhcp4Event\r
1011 );\r
1012\r
1013 if (EFI_ERROR (Status)) {\r
1014 return Status;\r
1015 }\r
1016\r
1017 Status = Dhcp4->Start (Dhcp4, Instance->Dhcp4Event);\r
1018\r
1019 if (EFI_ERROR (Status)) {\r
1020 return Status;\r
1021 }\r
1022 \r
1023 IpSb->State = IP4_SERVICE_STARTED;\r
1024 DispatchDpc ();\r
1025 return EFI_SUCCESS;\r
1026\r
1027}\r
1028\r
1029\r
1030\r
1031/**\r
1032 The work function is to get the interface information of the communication \r
1033 device this IP4_CONFIG2_INSTANCE manages.\r
1034\r
1035 @param[in] Instance Pointer to the IP4 config2 instance data.\r
1036 @param[in, out] DataSize On input, in bytes, the size of Data. On output, in\r
1037 bytes, the size of buffer required to store the specified\r
1038 configuration data.\r
1039 @param[in] Data The data buffer in which the configuration data is returned.\r
1040 Ignored if DataSize is ZERO.\r
1041\r
1042 @retval EFI_BUFFER_TOO_SMALL The size of Data is too small for the specified\r
1043 configuration data, and the required size is\r
1044 returned in DataSize.\r
1045 @retval EFI_SUCCESS The specified configuration data was obtained.\r
1046\r
1047**/\r
1048EFI_STATUS\r
1049Ip4Config2GetIfInfo (\r
1050 IN IP4_CONFIG2_INSTANCE *Instance,\r
1051 IN OUT UINTN *DataSize,\r
1052 IN VOID *Data OPTIONAL\r
1053 )\r
1054{\r
1055 IP4_SERVICE *IpSb;\r
1056 UINTN Length;\r
1057 IP4_CONFIG2_DATA_ITEM *Item;\r
1058 EFI_IP4_CONFIG2_INTERFACE_INFO *IfInfo;\r
1059 IP4_ADDR Address;\r
1060\r
1061 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
1062 Length = sizeof (EFI_IP4_CONFIG2_INTERFACE_INFO);\r
1063\r
1064 if (IpSb->DefaultRouteTable != NULL) {\r
1065 Length += IpSb->DefaultRouteTable->TotalNum * sizeof (EFI_IP4_ROUTE_TABLE);\r
1066 }\r
1067 \r
1068 if (*DataSize < Length) {\r
1069 *DataSize = Length;\r
1070 return EFI_BUFFER_TOO_SMALL;\r
1071 }\r
1072\r
1073 //\r
1074 // Copy the fixed size part of the interface info.\r
1075 //\r
1076 Item = &Instance->DataItem[Ip4Config2DataTypeInterfaceInfo];\r
1077 IfInfo = (EFI_IP4_CONFIG2_INTERFACE_INFO *) Data;\r
1078 CopyMem (IfInfo, Item->Data.Ptr, sizeof (EFI_IP4_CONFIG2_INTERFACE_INFO));\r
1079\r
1080 //\r
1081 // Update the address info.\r
1082 //\r
1083 if (IpSb->DefaultInterface != NULL) {\r
1084 Address = HTONL (IpSb->DefaultInterface->Ip);\r
1085 CopyMem (&IfInfo->StationAddress, &Address, sizeof (EFI_IPv4_ADDRESS));\r
1086 Address = HTONL (IpSb->DefaultInterface->SubnetMask);\r
1087 CopyMem (&IfInfo->SubnetMask, &Address, sizeof (EFI_IPv4_ADDRESS));\r
1088 }\r
1089\r
1090 if (IpSb->DefaultRouteTable != NULL) {\r
1091 IfInfo->RouteTableSize = IpSb->DefaultRouteTable->TotalNum;\r
1092 IfInfo->RouteTable = (EFI_IP4_ROUTE_TABLE *) ((UINT8 *) Data + sizeof (EFI_IP4_CONFIG2_INTERFACE_INFO));\r
1093\r
1094 Ip4Config2BuildDefaultRouteTable (IpSb, IfInfo->RouteTable); \r
1095 }\r
1096 \r
1097 return EFI_SUCCESS;\r
1098}\r
1099\r
1100/**\r
1101 The work function is to set the general configuration policy for the EFI IPv4 network \r
1102 stack that is running on the communication device managed by this IP4_CONFIG2_INSTANCE.\r
1103 The policy will affect other configuration settings.\r
1104\r
1105 @param[in] Instance Pointer to the IP4 config2 instance data.\r
1106 @param[in] DataSize Size of the buffer pointed to by Data in bytes.\r
1107 @param[in] Data The data buffer to set.\r
1108\r
1109 @retval EFI_INVALID_PARAMETER The to be set policy is invalid.\r
1110 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.\r
1111 @retval EFI_ABORTED The new policy equals the current policy.\r
1112 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6\r
1113 network stack was set.\r
1114\r
1115**/\r
1116EFI_STATUS\r
1117Ip4Config2SetPolicy (\r
1118 IN IP4_CONFIG2_INSTANCE *Instance,\r
1119 IN UINTN DataSize,\r
1120 IN VOID *Data\r
1121 )\r
1122{\r
1123 EFI_IP4_CONFIG2_POLICY NewPolicy;\r
1124 IP4_CONFIG2_DATA_ITEM *DataItem;\r
1125 IP4_SERVICE *IpSb;\r
1126\r
1127 if (DataSize != sizeof (EFI_IP4_CONFIG2_POLICY)) {\r
1128 return EFI_BAD_BUFFER_SIZE;\r
1129 }\r
1130\r
1131 NewPolicy = *((EFI_IP4_CONFIG2_POLICY *) Data);\r
1132\r
1133 if (NewPolicy >= Ip4Config2PolicyMax) {\r
1134 return EFI_INVALID_PARAMETER;\r
1135 }\r
1136\r
1137 if (NewPolicy == Instance->Policy) {\r
1138 if (NewPolicy != Ip4Config2PolicyDhcp || Instance->DhcpSuccess) {\r
1139 return EFI_ABORTED;\r
1140 }\r
1141 } else {\r
1142 //\r
1143 // The policy is changed. Clean the ManualAddress, Gateway and DnsServers, \r
1144 // shrink the variable data size, and fire up all the related events.\r
1145 //\r
1146 DataItem = &Instance->DataItem[Ip4Config2DataTypeManualAddress];\r
1147 if (DataItem->Data.Ptr != NULL) {\r
1148 FreePool (DataItem->Data.Ptr);\r
1149 }\r
1150 DataItem->Data.Ptr = NULL;\r
1151 DataItem->DataSize = 0;\r
1152 DataItem->Status = EFI_NOT_FOUND;\r
1153 NetMapIterate (&DataItem->EventMap, Ip4Config2SignalEvent, NULL);\r
1154\r
1155 DataItem = &Instance->DataItem[Ip4Config2DataTypeGateway];\r
1156 if (DataItem->Data.Ptr != NULL) {\r
1157 FreePool (DataItem->Data.Ptr);\r
1158 }\r
1159 DataItem->Data.Ptr = NULL;\r
1160 DataItem->DataSize = 0;\r
1161 DataItem->Status = EFI_NOT_FOUND;\r
1162 NetMapIterate (&DataItem->EventMap, Ip4Config2SignalEvent, NULL);\r
1163\r
1164 DataItem = &Instance->DataItem[Ip4Config2DataTypeDnsServer];\r
1165 if (DataItem->Data.Ptr != NULL) {\r
1166 FreePool (DataItem->Data.Ptr);\r
1167 }\r
1168 DataItem->Data.Ptr = NULL;\r
1169 DataItem->DataSize = 0;\r
1170 DataItem->Status = EFI_NOT_FOUND;\r
1171 NetMapIterate (&DataItem->EventMap, Ip4Config2SignalEvent, NULL);\r
1172 \r
1173 if (NewPolicy == Ip4Config2PolicyDhcp) {\r
1174 SET_DATA_ATTRIB (DataItem->Attribute, DATA_ATTRIB_VOLATILE);\r
1175 } else {\r
1176 //\r
1177 // The policy is changed from dhcp to static. Stop the DHCPv4 process\r
1178 // and destroy the DHCPv4 child.\r
1179 //\r
1180 if (Instance->Dhcp4Handle != NULL) {\r
1181 Ip4Config2DestroyDhcp4 (Instance);\r
1182 }\r
1183 \r
1184 //\r
1185 // Close the event.\r
1186 //\r
1187 if (Instance->Dhcp4Event != NULL) {\r
1188 gBS->CloseEvent (Instance->Dhcp4Event);\r
1189 Instance->Dhcp4Event = NULL;\r
1190 }\r
1191 }\r
1192 }\r
1193\r
1194 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
1195 Ip4Config2OnPolicyChanged (IpSb, NewPolicy);\r
1196\r
1197 Instance->Policy = NewPolicy;\r
1198\r
1199 return EFI_SUCCESS;\r
1200}\r
1201\r
1202/**\r
1203 The work function is to set the station addresses manually for the EFI IPv4 \r
1204 network stack. It is only configurable when the policy is Ip4Config2PolicyStatic.\r
1205\r
1206 @param[in] Instance Pointer to the IP4 config2 instance data.\r
1207 @param[in] DataSize Size of the buffer pointed to by Data in bytes.\r
1208 @param[in] Data The data buffer to set.\r
1209\r
1210 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.\r
1211 @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set\r
1212 under the current policy.\r
1213 @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.\r
1214 @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation.\r
1215 @retval EFI_NOT_READY An asynchrous process is invoked to set the specified\r
1216 configuration data, and the process is not finished.\r
1217 @retval EFI_ABORTED The manual addresses to be set equal current\r
1218 configuration.\r
1219 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6\r
1220 network stack was set.\r
1221\r
1222**/\r
1223EFI_STATUS\r
1224Ip4Config2SetMaunualAddress (\r
1225 IN IP4_CONFIG2_INSTANCE *Instance,\r
1226 IN UINTN DataSize,\r
1227 IN VOID *Data\r
1228 )\r
1229{\r
1230 EFI_IP4_CONFIG2_MANUAL_ADDRESS NewAddress;\r
1231 IP4_CONFIG2_DATA_ITEM *DataItem;\r
1232 EFI_STATUS Status;\r
1233 IP4_ADDR StationAddress;\r
1234 IP4_ADDR SubnetMask;\r
1235 VOID *Ptr;\r
1236 IP4_SERVICE *IpSb;\r
1237\r
1238 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
1239\r
1240 ASSERT (Instance->DataItem[Ip4Config2DataTypeManualAddress].Status != EFI_NOT_READY);\r
1241\r
1242 if (((DataSize % sizeof (EFI_IP4_CONFIG2_MANUAL_ADDRESS)) != 0) || (DataSize == 0)) {\r
1243 return EFI_BAD_BUFFER_SIZE;\r
1244 }\r
1245\r
1246 if (Instance->Policy != Ip4Config2PolicyStatic) {\r
1247 return EFI_WRITE_PROTECTED;\r
1248 }\r
1249\r
1250 NewAddress = *((EFI_IP4_CONFIG2_MANUAL_ADDRESS *) Data);\r
1251\r
1252 StationAddress = EFI_NTOHL (NewAddress.Address);\r
1253 SubnetMask = EFI_NTOHL (NewAddress.SubnetMask);\r
1254\r
1255 if (NetGetMaskLength (SubnetMask) == IP4_MASK_NUM) {\r
1256 return EFI_INVALID_PARAMETER;\r
1257 }\r
1258\r
1259 //\r
1260 // Store the new data, and init the DataItem status to EFI_NOT_READY because\r
1261 // we may have an asynchronous configuration process.\r
1262 //\r
1263 Ptr = AllocateCopyPool (DataSize, Data);\r
1264 if (Ptr == NULL) {\r
1265 return EFI_OUT_OF_RESOURCES;\r
1266 }\r
1267\r
1268 DataItem = &Instance->DataItem[Ip4Config2DataTypeManualAddress];\r
1269 if (DataItem->Data.Ptr != NULL) {\r
1270 FreePool (DataItem->Data.Ptr);\r
1271 }\r
1272 \r
1273 DataItem->Data.Ptr = Ptr;\r
1274 DataItem->DataSize = DataSize;\r
1275 DataItem->Status = EFI_NOT_READY;\r
1276\r
1277 IpSb->Reconfig = TRUE;\r
1278 Status = Ip4Config2SetDefaultAddr (IpSb, StationAddress, SubnetMask);\r
1279\r
1280 DataItem->Status = Status; \r
1281\r
1282 if (EFI_ERROR (DataItem->Status) && DataItem->Status != EFI_NOT_READY) {\r
1283 if (Ptr != NULL) {\r
1284 FreePool (Ptr);\r
1285 }\r
1286 DataItem->Data.Ptr = NULL; \r
1287 }\r
1288\r
1289 return Status;\r
1290}\r
1291\r
1292/**\r
1293 The work function is to set the gateway addresses manually for the EFI IPv4 \r
1294 network stack that is running on the communication device that this EFI IPv4 \r
1295 Configuration Protocol manages. It is not configurable when the policy is\r
1296 Ip4Config2PolicyDhcp. The gateway addresses must be unicast IPv4 addresses.\r
1297\r
1298 @param[in] Instance The pointer to the IP4 config2 instance data.\r
1299 @param[in] DataSize The size of the buffer pointed to by Data in bytes.\r
1300 @param[in] Data The data buffer to set. This points to an array of\r
1301 EFI_IPv6_ADDRESS instances.\r
1302\r
1303 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.\r
1304 @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set\r
1305 under the current policy.\r
1306 @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.\r
1307 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to complete the operation.\r
1308 @retval EFI_ABORTED The manual gateway addresses to be set equal the\r
1309 current configuration.\r
1310 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6\r
1311 network stack was set.\r
1312\r
1313**/\r
1314EFI_STATUS\r
1315Ip4Config2SetGateway (\r
1316 IN IP4_CONFIG2_INSTANCE *Instance,\r
1317 IN UINTN DataSize,\r
1318 IN VOID *Data\r
1319 )\r
1320{\r
1321 IP4_SERVICE *IpSb;\r
1322 IP4_CONFIG2_DATA_ITEM *DataItem;\r
1323 IP4_ADDR Gateway;\r
1324\r
1325 UINTN Index1;\r
1326 UINTN Index2;\r
1327 EFI_IPv4_ADDRESS *OldGateway;\r
1328 EFI_IPv4_ADDRESS *NewGateway;\r
1329 UINTN OldGatewayCount;\r
1330 UINTN NewGatewayCount;\r
1331 BOOLEAN OneRemoved;\r
1332 BOOLEAN OneAdded;\r
1333 VOID *Tmp;\r
1334\r
1335 if ((DataSize % sizeof (EFI_IPv4_ADDRESS) != 0) || (DataSize == 0)) {\r
1336 return EFI_BAD_BUFFER_SIZE;\r
1337 }\r
1338\r
1339 if (Instance->Policy != Ip4Config2PolicyStatic) {\r
1340 return EFI_WRITE_PROTECTED;\r
1341 }\r
1342\r
1343 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
1344\r
1345 NewGateway = (EFI_IPv4_ADDRESS *) Data;\r
1346 NewGatewayCount = DataSize / sizeof (EFI_IPv4_ADDRESS);\r
1347 for (Index1 = 0; Index1 < NewGatewayCount; Index1++) {\r
1348 CopyMem (&Gateway, NewGateway + Index1, sizeof (IP4_ADDR));\r
1349\r
1350 if ((IpSb->DefaultInterface->SubnetMask != 0) && \r
1351 !NetIp4IsUnicast (NTOHL (Gateway), IpSb->DefaultInterface->SubnetMask)) {\r
1352 return EFI_INVALID_PARAMETER;\r
1353 }\r
1354\r
1355 for (Index2 = Index1 + 1; Index2 < NewGatewayCount; Index2++) {\r
1356 if (EFI_IP4_EQUAL (NewGateway + Index1, NewGateway + Index2)) {\r
1357 return EFI_INVALID_PARAMETER;\r
1358 }\r
1359 }\r
1360 }\r
1361 \r
1362 DataItem = &Instance->DataItem[Ip4Config2DataTypeGateway];\r
1363 OldGateway = DataItem->Data.Gateway;\r
1364 OldGatewayCount = DataItem->DataSize / sizeof (EFI_IPv4_ADDRESS);\r
1365 OneRemoved = FALSE;\r
1366 OneAdded = FALSE;\r
1367\r
1368 if (NewGatewayCount != OldGatewayCount) {\r
1369 Tmp = AllocatePool (DataSize);\r
1370 if (Tmp == NULL) {\r
1371 return EFI_OUT_OF_RESOURCES;\r
1372 }\r
1373 } else {\r
1374 Tmp = NULL;\r
1375 }\r
1376\r
1377 for (Index1 = 0; Index1 < OldGatewayCount; Index1++) {\r
1378 //\r
1379 // Remove this route entry.\r
1380 //\r
1381 CopyMem (&Gateway, OldGateway + Index1, sizeof (IP4_ADDR));\r
1382 Ip4DelRoute (\r
1383 IpSb->DefaultRouteTable,\r
1384 IP4_ALLZERO_ADDRESS,\r
1385 IP4_ALLZERO_ADDRESS,\r
1386 NTOHL (Gateway)\r
1387 );\r
1388 OneRemoved = TRUE;\r
1389\r
1390 }\r
1391\r
1392 for (Index1 = 0; Index1 < NewGatewayCount; Index1++) {\r
1393 CopyMem (&Gateway, NewGateway + Index1, sizeof (IP4_ADDR));\r
1394 Ip4AddRoute (\r
1395 IpSb->DefaultRouteTable,\r
1396 IP4_ALLZERO_ADDRESS,\r
1397 IP4_ALLZERO_ADDRESS,\r
1398 NTOHL (Gateway)\r
1399 ); \r
1400\r
1401 OneAdded = TRUE;\r
1402 }\r
1403\r
1404\r
1405 if (!OneRemoved && !OneAdded) {\r
1406 DataItem->Status = EFI_SUCCESS;\r
1407 return EFI_ABORTED;\r
1408 } else {\r
1409\r
1410 if (Tmp != NULL) {\r
1411 if (DataItem->Data.Ptr != NULL) {\r
1412 FreePool (DataItem->Data.Ptr);\r
1413 }\r
1414 DataItem->Data.Ptr = Tmp;\r
1415 }\r
1416\r
1417 CopyMem (DataItem->Data.Ptr, Data, DataSize);\r
1418 DataItem->DataSize = DataSize;\r
1419 DataItem->Status = EFI_SUCCESS;\r
1420 return EFI_SUCCESS;\r
1421 }\r
1422\r
1423}\r
1424\r
1425/**\r
1426 The work function is to set the DNS server list for the EFI IPv4 network \r
1427 stack running on the communication device that this EFI_IP4_CONFIG2_PROTOCOL \r
1428 manages. It is not configurable when the policy is Ip4Config2PolicyDhcp. \r
1429 The DNS server addresses must be unicast IPv4 addresses.\r
1430\r
1431 @param[in] Instance The pointer to the IP4 config2 instance data.\r
1432 @param[in] DataSize The size of the buffer pointed to by Data in bytes.\r
1433 @param[in] Data The data buffer to set, points to an array of\r
1434 EFI_IPv4_ADDRESS instances.\r
1435\r
1436 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.\r
1437 @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set\r
1438 under the current policy.\r
1439 @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.\r
1440 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation.\r
1441 @retval EFI_ABORTED The DNS server addresses to be set equal the current\r
1442 configuration.\r
1443 @retval EFI_SUCCESS The specified configuration data for the EFI IPv4\r
1444 network stack was set.\r
1445\r
1446**/\r
1447EFI_STATUS\r
1448Ip4Config2SetDnsServer (\r
1449 IN IP4_CONFIG2_INSTANCE *Instance,\r
1450 IN UINTN DataSize,\r
1451 IN VOID *Data\r
1452 )\r
1453{\r
1454 IP4_CONFIG2_DATA_ITEM *Item;\r
1455\r
1456 Item = NULL;\r
1457\r
1458 if (Instance->Policy != Ip4Config2PolicyStatic) {\r
1459 return EFI_WRITE_PROTECTED;\r
1460 }\r
1461\r
1462 Item = &Instance->DataItem[Ip4Config2DataTypeDnsServer];\r
1463\r
1464 if (DATA_ATTRIB_SET (Item->Attribute, DATA_ATTRIB_VOLATILE)) {\r
1465 REMOVE_DATA_ATTRIB (Item->Attribute, DATA_ATTRIB_VOLATILE);\r
1466 }\r
1467\r
1468 return Ip4Config2SetDnsServerWorker (Instance, DataSize, Data);\r
1469}\r
1470\r
1471/**\r
1472 Generate the operational state of the interface this IP4 config2 instance manages\r
1473 and output in EFI_IP4_CONFIG2_INTERFACE_INFO.\r
1474\r
1475 @param[in] IpSb The pointer to the IP4 service binding instance.\r
1476 @param[out] IfInfo The pointer to the IP4 config2 interface information structure.\r
1477\r
1478**/\r
1479VOID\r
1480Ip4Config2InitIfInfo (\r
1481 IN IP4_SERVICE *IpSb,\r
1482 OUT EFI_IP4_CONFIG2_INTERFACE_INFO *IfInfo\r
1483 )\r
1484{\r
1485 UnicodeSPrint (\r
1486 IfInfo->Name,\r
1487 EFI_IP4_CONFIG2_INTERFACE_INFO_NAME_SIZE,\r
1488 L"eth%d",\r
1489 IpSb->Ip4Config2Instance.IfIndex\r
1490 );\r
1491\r
1492 IfInfo->IfType = IpSb->SnpMode.IfType;\r
1493 IfInfo->HwAddressSize = IpSb->SnpMode.HwAddressSize;\r
1494 CopyMem (&IfInfo->HwAddress, &IpSb->SnpMode.CurrentAddress, IfInfo->HwAddressSize);\r
1495}\r
1496\r
1497/**\r
1498 The event handle routine when DHCPv4 process is finished or is updated.\r
1499\r
1500 @param[in] Event Not used.\r
1501 @param[in] Context The pointer to the IP4 configuration instance data.\r
1502\r
1503**/\r
1504VOID\r
1505EFIAPI\r
1506Ip4Config2OnDhcp4Event (\r
1507 IN EFI_EVENT Event,\r
1508 IN VOID *Context\r
1509 )\r
1510{\r
1511 return ;\r
1512}\r
1513\r
1514\r
1515/**\r
1516 Set the configuration for the EFI IPv4 network stack running on the communication\r
1517 device this EFI_IP4_CONFIG2_PROTOCOL instance manages.\r
1518\r
1519 This function is used to set the configuration data of type DataType for the EFI\r
1520 IPv4 network stack that is running on the communication device that this EFI IPv4\r
1521 Configuration Protocol instance manages.\r
1522\r
1523 DataSize is used to calculate the count of structure instances in the Data for\r
1524 a DataType in which multiple structure instances are allowed.\r
1525\r
1526 This function is always non-blocking. When setting some type of configuration data,\r
1527 an asynchronous process is invoked to check the correctness of the data, such as\r
1528 performing Duplicate Address Detection on the manually set local IPv4 addresses.\r
1529 EFI_NOT_READY is returned immediately to indicate that such an asynchronous process\r
1530 is invoked, and the process is not finished yet. The caller wanting to get the result\r
1531 of the asynchronous process is required to call RegisterDataNotify() to register an\r
1532 event on the specified configuration data. Once the event is signaled, the caller\r
1533 can call GetData() to obtain the configuration data and know the result.\r
1534 For other types of configuration data that do not require an asynchronous configuration\r
1535 process, the result of the operation is immediately returned.\r
1536\r
1537 @param[in] This The pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.\r
1538 @param[in] DataType The type of data to set.\r
1539 @param[in] DataSize Size of the buffer pointed to by Data in bytes.\r
1540 @param[in] Data The data buffer to set. The type of the data buffer is\r
1541 associated with the DataType.\r
1542\r
1543 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6\r
1544 network stack was set successfully.\r
1545 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:\r
1546 - This is NULL.\r
1547 - Data is NULL.\r
1548 - One or more fields in Data do not match the requirement of the\r
1549 data type indicated by DataType.\r
1550 @retval EFI_WRITE_PROTECTED The specified configuration data is read-only or the specified\r
1551 configuration data cannot be set under the current policy.\r
1552 @retval EFI_ACCESS_DENIED Another set operation on the specified configuration\r
1553 data is already in process.\r
1554 @retval EFI_NOT_READY An asynchronous process was invoked to set the specified\r
1555 configuration data, and the process is not finished yet.\r
1556 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type\r
1557 indicated by DataType.\r
1558 @retval EFI_UNSUPPORTED This DataType is not supported.\r
1559 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.\r
1560 @retval EFI_DEVICE_ERROR An unexpected system error or network error occurred.\r
1561\r
1562**/\r
1563EFI_STATUS\r
1564EFIAPI\r
1565EfiIp4Config2SetData (\r
1566 IN EFI_IP4_CONFIG2_PROTOCOL *This,\r
1567 IN EFI_IP4_CONFIG2_DATA_TYPE DataType,\r
1568 IN UINTN DataSize,\r
1569 IN VOID *Data\r
1570 )\r
1571{\r
1572 EFI_TPL OldTpl;\r
1573 EFI_STATUS Status;\r
1574 IP4_CONFIG2_INSTANCE *Instance;\r
1575 IP4_SERVICE *IpSb;\r
1576\r
1577 if ((This == NULL) || (Data == NULL)) {\r
1578 return EFI_INVALID_PARAMETER;\r
1579 }\r
1580\r
1581 if (DataType >= Ip4Config2DataTypeMaximum) {\r
1582 return EFI_UNSUPPORTED;\r
1583 }\r
1584\r
1585 Instance = IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This);\r
1586 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
1587 NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);\r
1588\r
1589\r
1590 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1591\r
1592 Status = Instance->DataItem[DataType].Status;\r
1593 if (Status != EFI_NOT_READY) {\r
1594\r
1595 if (Instance->DataItem[DataType].SetData == NULL) {\r
1596 //\r
1597 // This type of data is readonly.\r
1598 //\r
1599 Status = EFI_WRITE_PROTECTED;\r
1600 } else {\r
1601\r
1602 Status = Instance->DataItem[DataType].SetData (Instance, DataSize, Data);\r
1603 if (!EFI_ERROR (Status)) {\r
1604 //\r
1605 // Fire up the events registered with this type of data.\r
1606 //\r
1607 NetMapIterate (&Instance->DataItem[DataType].EventMap, Ip4Config2SignalEvent, NULL);\r
1608 Ip4Config2WriteConfigData (IpSb->MacString, Instance);\r
1609 } else if (Status == EFI_ABORTED) {\r
1610 //\r
1611 // The SetData is aborted because the data to set is the same with\r
1612 // the one maintained.\r
1613 //\r
1614 Status = EFI_SUCCESS;\r
1615 NetMapIterate (&Instance->DataItem[DataType].EventMap, Ip4Config2SignalEvent, NULL);\r
1616 }\r
1617 }\r
1618 } else {\r
1619 //\r
1620 // Another asynchornous process is on the way.\r
1621 //\r
1622 Status = EFI_ACCESS_DENIED;\r
1623 }\r
1624\r
1625 gBS->RestoreTPL (OldTpl);\r
1626\r
1627 return Status;\r
1628}\r
1629\r
1630/**\r
1631 Get the configuration data for the EFI IPv4 network stack running on the communication\r
1632 device that this EFI_IP4_CONFIG2_PROTOCOL instance manages.\r
1633\r
1634 This function returns the configuration data of type DataType for the EFI IPv4 network\r
1635 stack running on the communication device that this EFI IPv4 Configuration Protocol instance\r
1636 manages.\r
1637\r
1638 The caller is responsible for allocating the buffer used to return the specified\r
1639 configuration data. The required size will be returned to the caller if the size of\r
1640 the buffer is too small.\r
1641\r
1642 EFI_NOT_READY is returned if the specified configuration data is not ready due to an\r
1643 asynchronous configuration process already in progress. The caller can call RegisterDataNotify()\r
1644 to register an event on the specified configuration data. Once the asynchronous configuration\r
1645 process is finished, the event will be signaled, and a subsequent GetData() call will return\r
1646 the specified configuration data.\r
1647\r
1648 @param[in] This Pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.\r
1649 @param[in] DataType The type of data to get.\r
1650 @param[in, out] DataSize On input, in bytes, the size of Data. On output, in bytes, the\r
1651 size of buffer required to store the specified configuration data.\r
1652 @param[in] Data The data buffer in which the configuration data is returned. The\r
1653 type of the data buffer is associated with the DataType.\r
1654 This is an optional parameter that may be NULL.\r
1655\r
1656 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1657 @retval EFI_INVALID_PARAMETER One or more of the followings are TRUE:\r
1658 - This is NULL.\r
1659 - DataSize is NULL.\r
1660 - Data is NULL if *DataSize is not zero.\r
1661 @retval EFI_BUFFER_TOO_SMALL The size of Data is too small for the specified configuration data,\r
1662 and the required size is returned in DataSize.\r
1663 @retval EFI_NOT_READY The specified configuration data is not ready due to an\r
1664 asynchronous configuration process already in progress.\r
1665 @retval EFI_NOT_FOUND The specified configuration data is not found.\r
1666\r
1667**/\r
1668EFI_STATUS\r
1669EFIAPI\r
1670EfiIp4Config2GetData (\r
1671 IN EFI_IP4_CONFIG2_PROTOCOL *This,\r
1672 IN EFI_IP4_CONFIG2_DATA_TYPE DataType,\r
1673 IN OUT UINTN *DataSize,\r
1674 IN VOID *Data OPTIONAL\r
1675 )\r
1676{\r
1677 EFI_TPL OldTpl;\r
1678 EFI_STATUS Status;\r
1679 IP4_CONFIG2_INSTANCE *Instance;\r
1680 IP4_CONFIG2_DATA_ITEM *DataItem;\r
1681\r
1682 if ((This == NULL) || (DataSize == NULL) || ((*DataSize != 0) && (Data == NULL))) {\r
1683 return EFI_INVALID_PARAMETER;\r
1684 }\r
1685\r
1686 if (DataType >= Ip4Config2DataTypeMaximum) {\r
1687 return EFI_NOT_FOUND;\r
1688 }\r
1689\r
1690 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1691\r
1692 Instance = IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This);\r
1693 DataItem = &Instance->DataItem[DataType];\r
1694\r
1695 Status = Instance->DataItem[DataType].Status;\r
1696 if (!EFI_ERROR (Status)) {\r
1697\r
1698 if (DataItem->GetData != NULL) {\r
1699\r
1700 Status = DataItem->GetData (Instance, DataSize, Data);\r
1701 } else if (*DataSize < Instance->DataItem[DataType].DataSize) {\r
1702 //\r
1703 // Update the buffer length.\r
1704 //\r
1705 *DataSize = Instance->DataItem[DataType].DataSize;\r
1706 Status = EFI_BUFFER_TOO_SMALL;\r
1707 } else {\r
1708\r
1709 *DataSize = Instance->DataItem[DataType].DataSize;\r
1710 CopyMem (Data, Instance->DataItem[DataType].Data.Ptr, *DataSize);\r
1711 }\r
1712 }\r
1713\r
1714 gBS->RestoreTPL (OldTpl);\r
1715\r
1716 return Status;\r
1717}\r
1718\r
1719/**\r
1720 Register an event that is signaled whenever a configuration process on the specified\r
1721 configuration data is done.\r
1722\r
1723 This function registers an event that is to be signaled whenever a configuration\r
1724 process on the specified configuration data is performed. An event can be registered\r
1725 for a different DataType simultaneously. The caller is responsible for determining\r
1726 which type of configuration data causes the signaling of the event in such an event.\r
1727\r
1728 @param[in] This Pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.\r
1729 @param[in] DataType The type of data to unregister the event for.\r
1730 @param[in] Event The event to register.\r
1731\r
1732 @retval EFI_SUCCESS The notification event for the specified configuration data is\r
1733 registered.\r
1734 @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.\r
1735 @retval EFI_UNSUPPORTED The configuration data type specified by DataType is not\r
1736 supported.\r
1737 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.\r
1738 @retval EFI_ACCESS_DENIED The Event is already registered for the DataType.\r
1739\r
1740**/\r
1741EFI_STATUS\r
1742EFIAPI\r
1743EfiIp4Config2RegisterDataNotify (\r
1744 IN EFI_IP4_CONFIG2_PROTOCOL *This,\r
1745 IN EFI_IP4_CONFIG2_DATA_TYPE DataType,\r
1746 IN EFI_EVENT Event\r
1747 )\r
1748{\r
1749 EFI_TPL OldTpl;\r
1750 EFI_STATUS Status;\r
1751 IP4_CONFIG2_INSTANCE *Instance;\r
1752 NET_MAP *EventMap;\r
1753 NET_MAP_ITEM *Item;\r
1754\r
1755 if ((This == NULL) || (Event == NULL)) {\r
1756 return EFI_INVALID_PARAMETER;\r
1757 }\r
1758\r
1759 if (DataType >= Ip4Config2DataTypeMaximum) {\r
1760 return EFI_UNSUPPORTED;\r
1761 }\r
1762\r
1763 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1764\r
1765 Instance = IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This);\r
1766 EventMap = &Instance->DataItem[DataType].EventMap;\r
1767\r
1768 //\r
1769 // Check whether this event is already registered for this DataType.\r
1770 //\r
1771 Item = NetMapFindKey (EventMap, Event);\r
1772 if (Item == NULL) {\r
1773\r
1774 Status = NetMapInsertTail (EventMap, Event, NULL);\r
1775\r
1776 if (EFI_ERROR (Status)) {\r
1777\r
1778 Status = EFI_OUT_OF_RESOURCES;\r
1779 }\r
1780\r
1781 } else {\r
1782\r
1783 Status = EFI_ACCESS_DENIED;\r
1784 }\r
1785\r
1786 gBS->RestoreTPL (OldTpl);\r
1787\r
1788 return Status;\r
1789}\r
1790\r
1791/**\r
1792 Remove a previously registered event for the specified configuration data.\r
1793\r
1794 @param This The pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.\r
1795 @param DataType The type of data to remove from the previously\r
1796 registered event.\r
1797 @param Event The event to be unregistered.\r
1798\r
1799 @retval EFI_SUCCESS The event registered for the specified\r
1800 configuration data was removed.\r
1801 @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.\r
1802 @retval EFI_NOT_FOUND The Event has not been registered for the\r
1803 specified DataType.\r
1804\r
1805**/\r
1806EFI_STATUS\r
1807EFIAPI\r
1808EfiIp4Config2UnregisterDataNotify (\r
1809 IN EFI_IP4_CONFIG2_PROTOCOL *This,\r
1810 IN EFI_IP4_CONFIG2_DATA_TYPE DataType,\r
1811 IN EFI_EVENT Event\r
1812 )\r
1813{\r
1814 EFI_TPL OldTpl;\r
1815 EFI_STATUS Status;\r
1816 IP4_CONFIG2_INSTANCE *Instance;\r
1817 NET_MAP_ITEM *Item;\r
1818\r
1819 if ((This == NULL) || (Event == NULL)) {\r
1820 return EFI_INVALID_PARAMETER;\r
1821 }\r
1822\r
1823 if (DataType >= Ip4Config2DataTypeMaximum) {\r
1824 return EFI_NOT_FOUND;\r
1825 }\r
1826\r
1827 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1828\r
1829 Instance = IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This);\r
1830\r
1831 Item = NetMapFindKey (&Instance->DataItem[DataType].EventMap, Event);\r
1832 if (Item != NULL) {\r
1833\r
1834 NetMapRemoveItem (&Instance->DataItem[DataType].EventMap, Item, NULL);\r
1835 Status = EFI_SUCCESS;\r
1836 } else {\r
1837\r
1838 Status = EFI_NOT_FOUND;\r
1839 }\r
1840\r
1841 gBS->RestoreTPL (OldTpl);\r
1842\r
1843 return Status;\r
1844}\r
1845\r
1846/**\r
1847 Initialize an IP4_CONFIG2_INSTANCE.\r
1848\r
1849 @param[out] Instance The buffer of IP4_CONFIG2_INSTANCE to be initialized.\r
1850\r
1851 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation.\r
1852 @retval EFI_SUCCESS The IP4_CONFIG2_INSTANCE initialized successfully.\r
1853\r
1854**/\r
1855EFI_STATUS\r
1856Ip4Config2InitInstance (\r
1857 OUT IP4_CONFIG2_INSTANCE *Instance\r
1858 )\r
1859{\r
1860 IP4_SERVICE *IpSb;\r
1861 IP4_CONFIG2_INSTANCE *TmpInstance;\r
1862 LIST_ENTRY *Entry;\r
1863 EFI_STATUS Status;\r
1864 UINTN Index;\r
1865 UINT16 IfIndex;\r
1866 IP4_CONFIG2_DATA_ITEM *DataItem;\r
1867\r
1868\r
1869 IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);\r
1870\r
1871 Instance->Signature = IP4_CONFIG2_INSTANCE_SIGNATURE;\r
1872\r
1873\r
1874 //\r
1875 // Determine the index of this interface.\r
1876 //\r
1877 IfIndex = 0;\r
1878 NET_LIST_FOR_EACH (Entry, &mIp4Config2InstanceList) {\r
1879 TmpInstance = NET_LIST_USER_STRUCT_S (Entry, IP4_CONFIG2_INSTANCE, Link, IP4_CONFIG2_INSTANCE_SIGNATURE);\r
1880\r
1881 if (TmpInstance->IfIndex > IfIndex) {\r
1882 //\r
1883 // There is a sequence hole because some interface is down.\r
1884 //\r
1885 break;\r
1886 }\r
1887\r
1888 IfIndex++;\r
1889 }\r
1890\r
1891 Instance->IfIndex = IfIndex;\r
1892 NetListInsertBefore (Entry, &Instance->Link);\r
1893\r
1894 for (Index = 0; Index < Ip4Config2DataTypeMaximum; Index++) {\r
1895 //\r
1896 // Initialize the event map for each data item.\r
1897 //\r
1898 NetMapInit (&Instance->DataItem[Index].EventMap);\r
1899 }\r
1900\r
1901 \r
1902 //\r
1903 // Initialize each data type: associate storage and set data size for the\r
1904 // fixed size data types, hook the SetData function, set the data attribute.\r
1905 //\r
1906 DataItem = &Instance->DataItem[Ip4Config2DataTypeInterfaceInfo];\r
1907 DataItem->GetData = Ip4Config2GetIfInfo;\r
1908 DataItem->Data.Ptr = &Instance->InterfaceInfo;\r
1909 DataItem->DataSize = sizeof (Instance->InterfaceInfo);\r
1910 SET_DATA_ATTRIB (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED | DATA_ATTRIB_VOLATILE);\r
1911 Ip4Config2InitIfInfo (IpSb, &Instance->InterfaceInfo);\r
1912\r
1913 DataItem = &Instance->DataItem[Ip4Config2DataTypePolicy];\r
1914 DataItem->SetData = Ip4Config2SetPolicy;\r
1915 DataItem->Data.Ptr = &Instance->Policy;\r
1916 DataItem->DataSize = sizeof (Instance->Policy);\r
1917 Instance->Policy = Ip4Config2PolicyStatic;\r
1918 SET_DATA_ATTRIB (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED);\r
1919\r
1920 DataItem = &Instance->DataItem[Ip4Config2DataTypeManualAddress];\r
1921 DataItem->SetData = Ip4Config2SetMaunualAddress;\r
1922 DataItem->Status = EFI_NOT_FOUND;\r
1923\r
1924 DataItem = &Instance->DataItem[Ip4Config2DataTypeGateway];\r
1925 DataItem->SetData = Ip4Config2SetGateway;\r
1926 DataItem->Status = EFI_NOT_FOUND;\r
1927\r
1928 DataItem = &Instance->DataItem[Ip4Config2DataTypeDnsServer];\r
1929 DataItem->SetData = Ip4Config2SetDnsServer;\r
1930 DataItem->Status = EFI_NOT_FOUND;\r
1931\r
1932 //\r
1933 // Create the event used for DHCP.\r
1934 //\r
1935 Status = gBS->CreateEvent (\r
1936 EVT_NOTIFY_SIGNAL,\r
1937 TPL_CALLBACK,\r
1938 Ip4Config2OnDhcp4Event,\r
1939 Instance,\r
1940 &Instance->Dhcp4Event\r
1941 );\r
1942 ASSERT_EFI_ERROR (Status);\r
1943\r
1944 Instance->Configured = TRUE;\r
1945\r
1946 //\r
1947 // Try to read the config data from NV variable.\r
1948 // If not found, write initialized config data into NV variable \r
1949 // as a default config data.\r
1950 //\r
1951 Status = Ip4Config2ReadConfigData (IpSb->MacString, Instance);\r
1952 if (Status == EFI_NOT_FOUND) {\r
1953 Status = Ip4Config2WriteConfigData (IpSb->MacString, Instance);\r
1954 }\r
1955\r
1956 if (EFI_ERROR (Status)) {\r
1957 return Status;\r
1958 }\r
1959 \r
1960 Instance->Ip4Config2.SetData = EfiIp4Config2SetData;\r
1961 Instance->Ip4Config2.GetData = EfiIp4Config2GetData;\r
1962 Instance->Ip4Config2.RegisterDataNotify = EfiIp4Config2RegisterDataNotify;\r
1963 Instance->Ip4Config2.UnregisterDataNotify = EfiIp4Config2UnregisterDataNotify;\r
1964\r
1965 //\r
1966 // Publish the IP4 configuration form\r
1967 //\r
1968 return Ip4Config2FormInit (Instance);\r
1969}\r
1970\r
1971\r
1972/**\r
1973 Release an IP4_CONFIG2_INSTANCE.\r
1974\r
1975 @param[in, out] Instance The buffer of IP4_CONFIG2_INSTANCE to be freed.\r
1976\r
1977**/\r
1978VOID\r
1979Ip4Config2CleanInstance (\r
1980 IN OUT IP4_CONFIG2_INSTANCE *Instance\r
1981 )\r
1982{\r
1983 UINTN Index;\r
1984 IP4_CONFIG2_DATA_ITEM *DataItem;\r
1985\r
1986 if (Instance->DeclineAddress != NULL) {\r
1987 FreePool (Instance->DeclineAddress);\r
1988 }\r
1989\r
1990 if (!Instance->Configured) {\r
1991 return ;\r
1992 }\r
1993\r
1994 if (Instance->Dhcp4Handle != NULL) {\r
1995\r
1996 Ip4Config2DestroyDhcp4 (Instance);\r
1997 }\r
1998\r
1999 //\r
2000 // Close the event.\r
2001 //\r
2002 if (Instance->Dhcp4Event != NULL) {\r
2003 gBS->CloseEvent (Instance->Dhcp4Event);\r
2004 Instance->Dhcp4Event = NULL;\r
2005 }\r
2006\r
2007 for (Index = 0; Index < Ip4Config2DataTypeMaximum; Index++) {\r
2008\r
2009 DataItem = &Instance->DataItem[Index];\r
2010\r
2011 if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED)) {\r
2012 if (DataItem->Data.Ptr != NULL) {\r
2013 FreePool (DataItem->Data.Ptr);\r
2014 }\r
2015 DataItem->Data.Ptr = NULL;\r
2016 DataItem->DataSize = 0;\r
2017 }\r
2018\r
2019 NetMapClean (&Instance->DataItem[Index].EventMap);\r
2020 }\r
2021\r
2022 Ip4Config2FormUnload (Instance);\r
2023\r
2024 RemoveEntryList (&Instance->Link);\r
2025}\r
2026\r
2027/**\r
2028 The event handle for IP4 auto reconfiguration. The original default\r
2029 interface and route table will be removed as the default.\r
2030\r
2031 @param[in] Context The IP4 service binding instance.\r
2032\r
2033**/\r
2034VOID\r
2035EFIAPI\r
2036Ip4AutoReconfigCallBackDpc (\r
2037 IN VOID *Context\r
2038 )\r
2039{\r
2040 IP4_SERVICE *IpSb;\r
2041\r
2042 IpSb = (IP4_SERVICE *) Context;\r
2043 NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);\r
2044\r
2045 if (IpSb->State > IP4_SERVICE_UNSTARTED) {\r
2046 IpSb->State = IP4_SERVICE_UNSTARTED;\r
2047 }\r
2048 \r
2049 IpSb->Reconfig = TRUE;\r
2050\r
2051 Ip4StartAutoConfig (&IpSb->Ip4Config2Instance);\r
2052\r
2053 return ;\r
2054}\r
2055\r
2056\r
2057/**\r
2058 Request Ip4AutoReconfigCallBackDpc as a DPC at TPL_CALLBACK.\r
2059\r
2060 @param Event The event that is signalled.\r
2061 @param Context The IP4 service binding instance.\r
2062\r
2063**/\r
2064VOID\r
2065EFIAPI\r
2066Ip4AutoReconfigCallBack (\r
2067 IN EFI_EVENT Event,\r
2068 IN VOID *Context\r
2069 )\r
2070{\r
2071 //\r
2072 // Request Ip4AutoReconfigCallBackDpc as a DPC at TPL_CALLBACK\r
2073 //\r
2074 QueueDpc (TPL_CALLBACK, Ip4AutoReconfigCallBackDpc, Context);\r
2075}\r
2076\r