2 The implementation of EFI IPv4 Configuration II Protocol.
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 LIST_ENTRY mIp4Config2InstanceList
= {&mIp4Config2InstanceList
, &mIp4Config2InstanceList
};
22 The event process routine when the DHCPv4 service binding protocol is installed
25 @param[in] Event Not used.
26 @param[in] Context Pointer to the IP4 config2 instance data.
31 Ip4Config2OnDhcp4SbInstalled (
37 Destroy the Dhcp4 child in IP4_CONFIG2_INSTANCE and release the resources.
39 @param[in, out] Instance The buffer of IP4 config2 instance to be freed.
41 @retval EFI_SUCCESS The child was successfully destroyed.
42 @retval Others Failed to destroy the child.
46 Ip4Config2DestroyDhcp4 (
47 IN OUT IP4_CONFIG2_INSTANCE
*Instance
52 EFI_DHCP4_PROTOCOL
*Dhcp4
;
54 Dhcp4
= Instance
->Dhcp4
;
55 ASSERT (Dhcp4
!= NULL
);
58 Dhcp4
->Configure (Dhcp4
, NULL
);
59 Instance
->Dhcp4
= NULL
;
61 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
64 // Close DHCPv4 protocol and destroy the child.
66 Status
= gBS
->CloseProtocol (
67 Instance
->Dhcp4Handle
,
68 &gEfiDhcp4ProtocolGuid
,
72 if (EFI_ERROR (Status
)) {
76 Status
= NetLibDestroyServiceChild (
79 &gEfiDhcp4ServiceBindingProtocolGuid
,
83 Instance
->Dhcp4Handle
= NULL
;
89 Update the current policy to NewPolicy. During the transition
90 period, the default router list
91 and address list in all interfaces will be released.
93 @param[in] IpSb The IP4 service binding instance.
94 @param[in] NewPolicy The new policy to be updated to.
98 Ip4Config2OnPolicyChanged (
100 IN EFI_IP4_CONFIG2_POLICY NewPolicy
104 IP4_ROUTE_TABLE
*RouteTable
;
107 // Currently there are only two policies: static and dhcp. Regardless of
108 // what transition is going on, i.e., static -> dhcp and dhcp ->
109 // static, we have to free default router table and all addresses.
112 if (IpSb
->DefaultInterface
!= NULL
) {
113 if (IpSb
->DefaultRouteTable
!= NULL
) {
114 Ip4FreeRouteTable (IpSb
->DefaultRouteTable
);
115 IpSb
->DefaultRouteTable
= NULL
;
118 Ip4CancelReceive (IpSb
->DefaultInterface
);
120 Ip4FreeInterface (IpSb
->DefaultInterface
, NULL
);
121 IpSb
->DefaultInterface
= NULL
;
124 Ip4CleanAssembleTable (&IpSb
->Assemble
);
127 // Create new default interface and route table.
129 IpIf
= Ip4CreateInterface (IpSb
->Mnp
, IpSb
->Controller
, IpSb
->Image
);
134 RouteTable
= Ip4CreateRouteTable ();
135 if (RouteTable
== NULL
) {
136 Ip4FreeInterface (IpIf
, NULL
);
140 IpSb
->DefaultInterface
= IpIf
;
141 InsertHeadList (&IpSb
->Interfaces
, &IpIf
->Link
);
142 IpSb
->DefaultRouteTable
= RouteTable
;
143 Ip4ReceiveFrame (IpIf
, NULL
, Ip4AccpetFrame
, IpSb
);
145 if (IpSb
->State
== IP4_SERVICE_CONFIGED
|| IpSb
->State
== IP4_SERVICE_STARTED
) {
146 IpSb
->State
= IP4_SERVICE_UNSTARTED
;
150 // Start the dhcp configuration.
152 if (NewPolicy
== Ip4Config2PolicyDhcp
) {
153 Ip4StartAutoConfig (&IpSb
->Ip4Config2Instance
);
159 Signal the registered event. It is the callback routine for NetMapIterate.
161 @param[in] Map Points to the list of registered event.
162 @param[in] Item The registered event.
163 @param[in] Arg Not used.
165 @retval EFI_SUCCESS The event was signaled successfully.
169 Ip4Config2SignalEvent (
171 IN NET_MAP_ITEM
*Item
,
175 gBS
->SignalEvent ((EFI_EVENT
) Item
->Key
);
181 Read the configuration data from variable storage according to the VarName and
182 gEfiIp4Config2ProtocolGuid. It checks the integrity of variable data. If the
183 data is corrupted, it clears the variable data to ZERO. Othewise, it outputs the
184 configuration data to IP4_CONFIG2_INSTANCE.
186 @param[in] VarName The pointer to the variable name
187 @param[in, out] Instance The pointer to the IP4 config2 instance data.
189 @retval EFI_NOT_FOUND The variable can not be found or already corrupted.
190 @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation.
191 @retval EFI_SUCCESS The configuration data was retrieved successfully.
195 Ip4Config2ReadConfigData (
197 IN OUT IP4_CONFIG2_INSTANCE
*Instance
202 IP4_CONFIG2_VARIABLE
*Variable
;
203 IP4_CONFIG2_DATA_ITEM
*DataItem
;
205 IP4_CONFIG2_DATA_RECORD DataRecord
;
209 // Try to read the configuration variable.
212 Status
= gRT
->GetVariable (
214 &gEfiIp4Config2ProtocolGuid
,
220 if (Status
== EFI_BUFFER_TOO_SMALL
) {
222 // Allocate buffer and read the config variable.
224 Variable
= AllocatePool (VarSize
);
225 if (Variable
== NULL
) {
226 return EFI_OUT_OF_RESOURCES
;
229 Status
= gRT
->GetVariable (
231 &gEfiIp4Config2ProtocolGuid
,
236 if (EFI_ERROR (Status
) || (UINT16
) (~NetblockChecksum ((UINT8
*) Variable
, (UINT32
) VarSize
)) != 0) {
238 // GetVariable still error or the variable is corrupted.
239 // Fall back to the default value.
244 // Remove the problematic variable and return EFI_NOT_FOUND, a new
245 // variable will be set again.
249 &gEfiIp4Config2ProtocolGuid
,
250 IP4_CONFIG2_VARIABLE_ATTRIBUTE
,
255 return EFI_NOT_FOUND
;
259 for (Index
= 0; Index
< Variable
->DataRecordCount
; Index
++) {
261 CopyMem (&DataRecord
, &Variable
->DataRecord
[Index
], sizeof (DataRecord
));
263 DataItem
= &Instance
->DataItem
[DataRecord
.DataType
];
264 if (DATA_ATTRIB_SET (DataItem
->Attribute
, DATA_ATTRIB_SIZE_FIXED
) &&
265 (DataItem
->DataSize
!= DataRecord
.DataSize
)
268 // Perhaps a corrupted data record...
273 if (!DATA_ATTRIB_SET (DataItem
->Attribute
, DATA_ATTRIB_SIZE_FIXED
)) {
275 // This data item has variable length data.
277 DataItem
->Data
.Ptr
= AllocatePool (DataRecord
.DataSize
);
278 if (DataItem
->Data
.Ptr
== NULL
) {
280 // no memory resource
286 Data
= (CHAR8
*) Variable
+ DataRecord
.Offset
;
287 CopyMem (DataItem
->Data
.Ptr
, Data
, DataRecord
.DataSize
);
289 DataItem
->DataSize
= DataRecord
.DataSize
;
290 DataItem
->Status
= EFI_SUCCESS
;
301 Write the configuration data from IP4_CONFIG2_INSTANCE to variable storage.
303 @param[in] VarName The pointer to the variable name.
304 @param[in] Instance The pointer to the IP4 config2 instance data.
306 @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation.
307 @retval EFI_SUCCESS The configuration data is written successfully.
311 Ip4Config2WriteConfigData (
313 IN IP4_CONFIG2_INSTANCE
*Instance
318 IP4_CONFIG2_DATA_ITEM
*DataItem
;
319 IP4_CONFIG2_VARIABLE
*Variable
;
320 IP4_CONFIG2_DATA_RECORD
*DataRecord
;
324 VarSize
= sizeof (IP4_CONFIG2_VARIABLE
) - sizeof (IP4_CONFIG2_DATA_RECORD
);
326 for (Index
= 0; Index
< Ip4Config2DataTypeMaximum
; Index
++) {
328 DataItem
= &Instance
->DataItem
[Index
];
329 if (!DATA_ATTRIB_SET (DataItem
->Attribute
, DATA_ATTRIB_VOLATILE
) && !EFI_ERROR (DataItem
->Status
)) {
331 VarSize
+= sizeof (IP4_CONFIG2_DATA_RECORD
) + DataItem
->DataSize
;
335 Variable
= AllocatePool (VarSize
);
336 if (Variable
== NULL
) {
337 return EFI_OUT_OF_RESOURCES
;
340 Heap
= (CHAR8
*) Variable
+ VarSize
;
341 Variable
->DataRecordCount
= 0;
343 for (Index
= 0; Index
< Ip4Config2DataTypeMaximum
; Index
++) {
345 DataItem
= &Instance
->DataItem
[Index
];
346 if (!DATA_ATTRIB_SET (DataItem
->Attribute
, DATA_ATTRIB_VOLATILE
) && !EFI_ERROR (DataItem
->Status
)) {
348 Heap
-= DataItem
->DataSize
;
349 CopyMem (Heap
, DataItem
->Data
.Ptr
, DataItem
->DataSize
);
351 DataRecord
= &Variable
->DataRecord
[Variable
->DataRecordCount
];
352 DataRecord
->DataType
= (EFI_IP4_CONFIG2_DATA_TYPE
) Index
;
353 DataRecord
->DataSize
= (UINT32
) DataItem
->DataSize
;
354 DataRecord
->Offset
= (UINT16
) (Heap
- (CHAR8
*) Variable
);
356 Variable
->DataRecordCount
++;
360 Variable
->Checksum
= 0;
361 Variable
->Checksum
= (UINT16
) ~NetblockChecksum ((UINT8
*) Variable
, (UINT32
) VarSize
);
363 Status
= gRT
->SetVariable (
365 &gEfiIp4Config2ProtocolGuid
,
366 IP4_CONFIG2_VARIABLE_ATTRIBUTE
,
378 Build a EFI_IP4_ROUTE_TABLE to be returned to the caller of GetModeData.
379 The EFI_IP4_ROUTE_TABLE is clumsy to use in the internal operation of the
382 @param[in] IpSb The IP4 service binding instance.
383 @param[out] Table The built IP4 route table.
385 @retval EFI_SUCCESS The route table is successfully build
386 @retval EFI_NOT_FOUND Failed to allocate the memory for the rotue table.
390 Ip4Config2BuildDefaultRouteTable (
391 IN IP4_SERVICE
*IpSb
,
392 OUT EFI_IP4_ROUTE_TABLE
*Table
396 IP4_ROUTE_ENTRY
*RtEntry
;
400 if (IpSb
->DefaultRouteTable
== NULL
) {
401 return EFI_NOT_FOUND
;
404 Count
= IpSb
->DefaultRouteTable
->TotalNum
;
407 return EFI_NOT_FOUND
;
411 // Copy the route entry to EFI route table. Keep the order of
412 // route entry copied from most specific to default route. That
413 // is, interlevel the route entry from the instance's route area
414 // and those from the default route table's route area.
418 for (Index
= IP4_MASK_MAX
; Index
>= 0; Index
--) {
420 NET_LIST_FOR_EACH (Entry
, &(IpSb
->DefaultRouteTable
->RouteArea
[Index
])) {
421 RtEntry
= NET_LIST_USER_STRUCT (Entry
, IP4_ROUTE_ENTRY
, Link
);
423 EFI_IP4 (Table
[Count
].SubnetAddress
) = HTONL (RtEntry
->Dest
& RtEntry
->Netmask
);
424 EFI_IP4 (Table
[Count
].SubnetMask
) = HTONL (RtEntry
->Netmask
);
425 EFI_IP4 (Table
[Count
].GatewayAddress
) = HTONL (RtEntry
->NextHop
);
436 The event process routine when the DHCPv4 service binding protocol is installed
439 @param[in] Event Not used.
440 @param[in] Context The pointer to the IP4 config2 instance data.
445 Ip4Config2OnDhcp4SbInstalled (
450 IP4_CONFIG2_INSTANCE
*Instance
;
452 Instance
= (IP4_CONFIG2_INSTANCE
*) Context
;
454 if ((Instance
->Dhcp4Handle
!= NULL
) || (Instance
->Policy
!= Ip4Config2PolicyDhcp
)) {
456 // The DHCP4 child is already created or the policy is no longer DHCP.
461 Ip4StartAutoConfig (Instance
);
465 Set the station address and subnetmask for the default interface.
467 @param[in] IpSb The pointer to the IP4 service binding instance.
468 @param[in] StationAddress Ip address to be set.
469 @param[in] SubnetMask Subnet to be set.
471 @retval EFI_SUCCESS Set default address successful.
472 @retval Others Some errors occur in setting.
476 Ip4Config2SetDefaultAddr (
477 IN IP4_SERVICE
*IpSb
,
478 IN IP4_ADDR StationAddress
,
479 IN IP4_ADDR SubnetMask
484 IP4_PROTOCOL
*Ip4Instance
;
485 EFI_ARP_PROTOCOL
*Arp
;
488 IP4_ROUTE_TABLE
*RouteTable
;
490 IpIf
= IpSb
->DefaultInterface
;
491 ASSERT (IpIf
!= NULL
);
493 if ((IpIf
->Ip
== StationAddress
) && (IpIf
->SubnetMask
== SubnetMask
)) {
494 IpSb
->State
= IP4_SERVICE_CONFIGED
;
498 if (IpSb
->Reconfig
) {
500 // The default address is changed, free the previous interface first.
502 if (IpSb
->DefaultRouteTable
!= NULL
) {
503 Ip4FreeRouteTable (IpSb
->DefaultRouteTable
);
504 IpSb
->DefaultRouteTable
= NULL
;
507 Ip4CancelReceive (IpSb
->DefaultInterface
);
508 Ip4FreeInterface (IpSb
->DefaultInterface
, NULL
);
509 IpSb
->DefaultInterface
= NULL
;
511 // Create new default interface and route table.
513 IpIf
= Ip4CreateInterface (IpSb
->Mnp
, IpSb
->Controller
, IpSb
->Image
);
515 return EFI_OUT_OF_RESOURCES
;
518 RouteTable
= Ip4CreateRouteTable ();
519 if (RouteTable
== NULL
) {
520 Ip4FreeInterface (IpIf
, NULL
);
521 return EFI_OUT_OF_RESOURCES
;
524 IpSb
->DefaultInterface
= IpIf
;
525 InsertHeadList (&IpSb
->Interfaces
, &IpIf
->Link
);
526 IpSb
->DefaultRouteTable
= RouteTable
;
527 Ip4ReceiveFrame (IpIf
, NULL
, Ip4AccpetFrame
, IpSb
);
530 if (IpSb
->State
== IP4_SERVICE_CONFIGED
) {
531 IpSb
->State
= IP4_SERVICE_UNSTARTED
;
534 Status
= Ip4SetAddress (IpIf
, StationAddress
, SubnetMask
);
535 if (EFI_ERROR (Status
)) {
539 if (IpIf
->Arp
!= NULL
) {
541 // A non-NULL IpIf->Arp here means a new ARP child is created when setting default address,
542 // but some IP children may have referenced the default interface before it is configured,
543 // these IP instances also consume this ARP protocol so they need to open it BY_CHILD_CONTROLLER.
546 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
547 Ip4Instance
= NET_LIST_USER_STRUCT_S (Entry
, IP4_PROTOCOL
, AddrLink
, IP4_PROTOCOL_SIGNATURE
);
548 Status
= gBS
->OpenProtocol (
550 &gEfiArpProtocolGuid
,
552 gIp4DriverBinding
.DriverBindingHandle
,
554 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
556 if (EFI_ERROR (Status
)) {
563 // Add a route for the connected network.
565 Subnet
= StationAddress
& SubnetMask
;
568 IpSb
->DefaultRouteTable
,
574 IpSb
->State
= IP4_SERVICE_CONFIGED
;
575 IpSb
->Reconfig
= FALSE
;
581 Set the station address, subnetmask and gateway address for the default interface.
583 @param[in] Instance The pointer to the IP4 config2 instance data.
584 @param[in] StationAddress Ip address to be set.
585 @param[in] SubnetMask Subnet to be set.
586 @param[in] GatewayAddress Gateway to be set.
588 @retval EFI_SUCCESS Set default If successful.
589 @retval Others Errors occur as indicated.
593 Ip4Config2SetDefaultIf (
594 IN IP4_CONFIG2_INSTANCE
*Instance
,
595 IN IP4_ADDR StationAddress
,
596 IN IP4_ADDR SubnetMask
,
597 IN IP4_ADDR GatewayAddress
603 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
606 // Check whether the StationAddress/SubnetMask pair is valid.
608 if (!Ip4StationAddressValid (StationAddress
, SubnetMask
)) {
609 return EFI_INVALID_PARAMETER
;
612 Status
= Ip4Config2SetDefaultAddr (IpSb
, StationAddress
, SubnetMask
);
613 if (EFI_ERROR (Status
)) {
618 // Create a route if there is a default router.
620 if (GatewayAddress
!= IP4_ALLZERO_ADDRESS
) {
622 IpSb
->DefaultRouteTable
,
634 Release all the DHCP related resources.
636 @param Instance The IP4 config2 instance.
642 Ip4Config2CleanDhcp4 (
643 IN IP4_CONFIG2_INSTANCE
*Instance
648 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
650 if (Instance
->Dhcp4
!= NULL
) {
651 Instance
->Dhcp4
->Stop (Instance
->Dhcp4
);
654 Instance
->Dhcp4Handle
,
655 &gEfiDhcp4ProtocolGuid
,
660 Instance
->Dhcp4
= NULL
;
663 if (Instance
->Dhcp4Handle
!= NULL
) {
664 NetLibDestroyServiceChild (
667 &gEfiDhcp4ServiceBindingProtocolGuid
,
668 Instance
->Dhcp4Handle
671 Instance
->Dhcp4Handle
= NULL
;
674 if (Instance
->Dhcp4Event
!= NULL
) {
675 gBS
->CloseEvent (Instance
->Dhcp4Event
);
676 Instance
->Dhcp4Event
= NULL
;
681 This worker function sets the DNS server list for the EFI IPv4 network
682 stack running on the communication device that this EFI_IP4_CONFIG2_PROTOCOL
683 manages. The DNS server addresses must be unicast IPv4 addresses.
685 @param[in] Instance The pointer to the IP4 config2 instance data.
686 @param[in] DataSize The size of the buffer pointed to by Data in bytes.
687 @param[in] Data The data buffer to set, points to an array of
688 EFI_IPv4_ADDRESS instances.
690 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.
691 @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.
692 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation.
693 @retval EFI_ABORTED The DNS server addresses to be set equal the current
695 @retval EFI_SUCCESS The specified configuration data for the EFI IPv4
696 network stack was set.
700 Ip4Config2SetDnsServerWorker (
701 IN IP4_CONFIG2_INSTANCE
*Instance
,
708 EFI_IPv4_ADDRESS
*OldDns
;
709 EFI_IPv4_ADDRESS
*NewDns
;
712 IP4_CONFIG2_DATA_ITEM
*Item
;
717 if ((DataSize
% sizeof (EFI_IPv4_ADDRESS
) != 0) || (DataSize
== 0)) {
718 return EFI_BAD_BUFFER_SIZE
;
721 Item
= &Instance
->DataItem
[Ip4Config2DataTypeDnsServer
];
722 NewDns
= (EFI_IPv4_ADDRESS
*) Data
;
723 OldDns
= Item
->Data
.DnsServers
;
724 NewDnsCount
= DataSize
/ sizeof (EFI_IPv4_ADDRESS
);
725 OldDnsCount
= Item
->DataSize
/ sizeof (EFI_IPv4_ADDRESS
);
728 if (NewDnsCount
!= OldDnsCount
) {
729 Tmp
= AllocatePool (DataSize
);
731 return EFI_OUT_OF_RESOURCES
;
737 for (NewIndex
= 0; NewIndex
< NewDnsCount
; NewIndex
++) {
738 CopyMem (&DnsAddress
, NewDns
+ NewIndex
, sizeof (IP4_ADDR
));
739 if (IP4_IS_UNSPECIFIED (NTOHL (DnsAddress
)) || IP4_IS_LOCAL_BROADCAST (NTOHL (DnsAddress
))) {
741 // The dns server address must be unicast.
746 return EFI_INVALID_PARAMETER
;
751 // If any address in the new setting is not in the old settings, skip the
752 // comparision below.
757 for (OldIndex
= 0; OldIndex
< OldDnsCount
; OldIndex
++) {
758 if (EFI_IP4_EQUAL (NewDns
+ NewIndex
, OldDns
+ OldIndex
)) {
760 // If found break out.
766 if (OldIndex
== OldDnsCount
) {
771 if (!OneAdded
&& (DataSize
== Item
->DataSize
)) {
773 // No new item is added and the size is the same.
775 Item
->Status
= EFI_SUCCESS
;
779 if (Item
->Data
.Ptr
!= NULL
) {
780 FreePool (Item
->Data
.Ptr
);
782 Item
->Data
.Ptr
= Tmp
;
785 CopyMem (Item
->Data
.Ptr
, Data
, DataSize
);
786 Item
->DataSize
= DataSize
;
787 Item
->Status
= EFI_SUCCESS
;
795 Callback function when DHCP process finished. It will save the
796 retrieved IP configure parameter from DHCP to the NVRam.
798 @param Event The callback event
799 @param Context Opaque context to the callback
806 Ip4Config2OnDhcp4Complete (
811 IP4_CONFIG2_INSTANCE
*Instance
;
812 EFI_DHCP4_MODE_DATA Dhcp4Mode
;
814 IP4_ADDR StationAddress
;
816 IP4_ADDR GatewayAddress
;
819 EFI_DHCP4_PACKET_OPTION
**OptionList
;
821 Instance
= (IP4_CONFIG2_INSTANCE
*) Context
;
822 ASSERT (Instance
->Dhcp4
!= NULL
);
825 // Get the DHCP retrieved parameters
827 Status
= Instance
->Dhcp4
->GetModeData (Instance
->Dhcp4
, &Dhcp4Mode
);
829 if (EFI_ERROR (Status
)) {
833 if (Dhcp4Mode
.State
== Dhcp4Bound
) {
834 StationAddress
= EFI_NTOHL (Dhcp4Mode
.ClientAddress
);
835 SubnetMask
= EFI_NTOHL (Dhcp4Mode
.SubnetMask
);
836 GatewayAddress
= EFI_NTOHL (Dhcp4Mode
.RouterAddress
);
838 Status
= Ip4Config2SetDefaultIf (Instance
, StationAddress
, SubnetMask
, GatewayAddress
);
839 if (EFI_ERROR (Status
)) {
844 // Parse the ACK to get required DNS server information.
849 Status
= Instance
->Dhcp4
->Parse (Instance
->Dhcp4
, Dhcp4Mode
.ReplyPacket
, &OptionCount
, OptionList
);
850 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
854 OptionList
= AllocateZeroPool (OptionCount
* sizeof (EFI_DHCP4_PACKET_OPTION
*));
855 if (OptionList
== NULL
) {
859 Status
= Instance
->Dhcp4
->Parse (Instance
->Dhcp4
, Dhcp4Mode
.ReplyPacket
, &OptionCount
, OptionList
);
860 if (EFI_ERROR (Status
)) {
861 FreePool (OptionList
);
865 for (Index
= 0; Index
< OptionCount
; Index
++) {
867 // Look for DNS Server opcode (6).
869 if (OptionList
[Index
]->OpCode
== DHCP4_TAG_DNS_SERVER
) {
870 if (((OptionList
[Index
]->Length
& 0x3) != 0) || (OptionList
[Index
]->Length
== 0)) {
874 Ip4Config2SetDnsServerWorker (Instance
, OptionList
[Index
]->Length
, &OptionList
[Index
]->Data
[0]);
879 FreePool (OptionList
);
881 Instance
->DhcpSuccess
= TRUE
;
885 Ip4Config2CleanDhcp4 (Instance
);
891 Start the DHCP configuration for this IP service instance.
892 It will locates the EFI_IP4_CONFIG2_PROTOCOL, then start the
895 @param[in] Instance The IP4 config2 instance to configure
897 @retval EFI_SUCCESS The auto configuration is successfully started
898 @retval Others Failed to start auto configuration.
903 IN IP4_CONFIG2_INSTANCE
*Instance
907 EFI_DHCP4_PROTOCOL
*Dhcp4
;
908 EFI_DHCP4_MODE_DATA Dhcp4Mode
;
909 EFI_DHCP4_PACKET_OPTION
*OptionList
[1];
910 IP4_CONFIG2_DHCP4_OPTION ParaList
;
913 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
915 if (IpSb
->State
> IP4_SERVICE_UNSTARTED
) {
920 // A host must not invoke DHCP configuration if it is already
921 // participating in the DHCP configuraiton process.
923 if (Instance
->Dhcp4Handle
!= NULL
) {
927 Status
= NetLibCreateServiceChild (
930 &gEfiDhcp4ServiceBindingProtocolGuid
,
931 &Instance
->Dhcp4Handle
934 if (Status
== EFI_UNSUPPORTED
) {
936 // No DHCPv4 Service Binding protocol, register a notify.
938 if (Instance
->Dhcp4SbNotifyEvent
== NULL
) {
939 Instance
->Dhcp4SbNotifyEvent
= EfiCreateProtocolNotifyEvent (
940 &gEfiDhcp4ServiceBindingProtocolGuid
,
942 Ip4Config2OnDhcp4SbInstalled
,
944 &Instance
->Registration
949 if (EFI_ERROR (Status
)) {
953 if (Instance
->Dhcp4SbNotifyEvent
!= NULL
) {
954 gBS
->CloseEvent (Instance
->Dhcp4SbNotifyEvent
);
957 Status
= gBS
->OpenProtocol (
958 Instance
->Dhcp4Handle
,
959 &gEfiDhcp4ProtocolGuid
,
960 (VOID
**) &Instance
->Dhcp4
,
963 EFI_OPEN_PROTOCOL_BY_DRIVER
965 if (EFI_ERROR (Status
)) {
966 NetLibDestroyServiceChild (
969 &gEfiDhcp4ServiceBindingProtocolGuid
,
970 Instance
->Dhcp4Handle
973 Instance
->Dhcp4Handle
= NULL
;
979 // Check the current DHCP status, if the DHCP process has
980 // already finished, return now.
982 Dhcp4
= Instance
->Dhcp4
;
983 Status
= Dhcp4
->GetModeData (Dhcp4
, &Dhcp4Mode
);
984 if (Dhcp4Mode
.State
== Dhcp4Bound
) {
985 Ip4Config2OnDhcp4Complete (NULL
, Instance
);
991 // Try to start the DHCP process. Use most of the current
992 // DHCP configuration to avoid problems if some DHCP client
993 // yields the control of this DHCP service to us.
995 ParaList
.Head
.OpCode
= DHCP4_TAG_PARA_LIST
;
996 ParaList
.Head
.Length
= 3;
997 ParaList
.Head
.Data
[0] = DHCP4_TAG_NETMASK
;
998 ParaList
.Route
= DHCP4_TAG_ROUTER
;
999 ParaList
.Dns
= DHCP4_TAG_DNS_SERVER
;
1000 OptionList
[0] = &ParaList
.Head
;
1001 Dhcp4Mode
.ConfigData
.OptionCount
= 1;
1002 Dhcp4Mode
.ConfigData
.OptionList
= OptionList
;
1004 Status
= Dhcp4
->Configure (Dhcp4
, &Dhcp4Mode
.ConfigData
);
1005 if (EFI_ERROR (Status
)) {
1006 gBS
->CloseProtocol (
1007 Instance
->Dhcp4Handle
,
1008 &gEfiDhcp4ProtocolGuid
,
1013 NetLibDestroyServiceChild (
1016 &gEfiDhcp4ServiceBindingProtocolGuid
,
1017 Instance
->Dhcp4Handle
1020 Instance
->Dhcp4
= NULL
;
1022 Instance
->Dhcp4Handle
= NULL
;
1028 // Start the DHCP process
1030 Status
= gBS
->CreateEvent (
1033 Ip4Config2OnDhcp4Complete
,
1035 &Instance
->Dhcp4Event
1037 if (EFI_ERROR (Status
)) {
1038 Ip4Config2DestroyDhcp4 (Instance
);
1042 Status
= Dhcp4
->Start (Dhcp4
, Instance
->Dhcp4Event
);
1043 if (EFI_ERROR (Status
)) {
1044 Ip4Config2DestroyDhcp4 (Instance
);
1045 gBS
->CloseEvent (Instance
->Dhcp4Event
);
1046 Instance
->Dhcp4Event
= NULL
;
1051 IpSb
->State
= IP4_SERVICE_STARTED
;
1060 The work function is to get the interface information of the communication
1061 device this IP4_CONFIG2_INSTANCE manages.
1063 @param[in] Instance Pointer to the IP4 config2 instance data.
1064 @param[in, out] DataSize On input, in bytes, the size of Data. On output, in
1065 bytes, the size of buffer required to store the specified
1067 @param[in] Data The data buffer in which the configuration data is returned.
1068 Ignored if DataSize is ZERO.
1070 @retval EFI_BUFFER_TOO_SMALL The size of Data is too small for the specified
1071 configuration data, and the required size is
1072 returned in DataSize.
1073 @retval EFI_SUCCESS The specified configuration data was obtained.
1077 Ip4Config2GetIfInfo (
1078 IN IP4_CONFIG2_INSTANCE
*Instance
,
1079 IN OUT UINTN
*DataSize
,
1080 IN VOID
*Data OPTIONAL
1085 IP4_CONFIG2_DATA_ITEM
*Item
;
1086 EFI_IP4_CONFIG2_INTERFACE_INFO
*IfInfo
;
1089 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
1090 Length
= sizeof (EFI_IP4_CONFIG2_INTERFACE_INFO
);
1092 if (IpSb
->DefaultRouteTable
!= NULL
) {
1093 Length
+= IpSb
->DefaultRouteTable
->TotalNum
* sizeof (EFI_IP4_ROUTE_TABLE
);
1096 if (*DataSize
< Length
) {
1098 return EFI_BUFFER_TOO_SMALL
;
1102 // Copy the fixed size part of the interface info.
1104 Item
= &Instance
->DataItem
[Ip4Config2DataTypeInterfaceInfo
];
1105 IfInfo
= (EFI_IP4_CONFIG2_INTERFACE_INFO
*) Data
;
1106 CopyMem (IfInfo
, Item
->Data
.Ptr
, sizeof (EFI_IP4_CONFIG2_INTERFACE_INFO
));
1109 // Update the address info.
1111 if (IpSb
->DefaultInterface
!= NULL
) {
1112 Address
= HTONL (IpSb
->DefaultInterface
->Ip
);
1113 CopyMem (&IfInfo
->StationAddress
, &Address
, sizeof (EFI_IPv4_ADDRESS
));
1114 Address
= HTONL (IpSb
->DefaultInterface
->SubnetMask
);
1115 CopyMem (&IfInfo
->SubnetMask
, &Address
, sizeof (EFI_IPv4_ADDRESS
));
1118 if (IpSb
->DefaultRouteTable
!= NULL
) {
1119 IfInfo
->RouteTableSize
= IpSb
->DefaultRouteTable
->TotalNum
;
1120 IfInfo
->RouteTable
= (EFI_IP4_ROUTE_TABLE
*) ((UINT8
*) Data
+ sizeof (EFI_IP4_CONFIG2_INTERFACE_INFO
));
1122 Ip4Config2BuildDefaultRouteTable (IpSb
, IfInfo
->RouteTable
);
1129 The work function is to set the general configuration policy for the EFI IPv4 network
1130 stack that is running on the communication device managed by this IP4_CONFIG2_INSTANCE.
1131 The policy will affect other configuration settings.
1133 @param[in] Instance Pointer to the IP4 config2 instance data.
1134 @param[in] DataSize Size of the buffer pointed to by Data in bytes.
1135 @param[in] Data The data buffer to set.
1137 @retval EFI_INVALID_PARAMETER The to be set policy is invalid.
1138 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.
1139 @retval EFI_ABORTED The new policy equals the current policy.
1140 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6
1141 network stack was set.
1145 Ip4Config2SetPolicy (
1146 IN IP4_CONFIG2_INSTANCE
*Instance
,
1151 EFI_IP4_CONFIG2_POLICY NewPolicy
;
1152 IP4_CONFIG2_DATA_ITEM
*DataItem
;
1155 if (DataSize
!= sizeof (EFI_IP4_CONFIG2_POLICY
)) {
1156 return EFI_BAD_BUFFER_SIZE
;
1159 NewPolicy
= *((EFI_IP4_CONFIG2_POLICY
*) Data
);
1161 if (NewPolicy
>= Ip4Config2PolicyMax
) {
1162 return EFI_INVALID_PARAMETER
;
1165 if (NewPolicy
== Instance
->Policy
) {
1166 if (NewPolicy
!= Ip4Config2PolicyDhcp
|| Instance
->DhcpSuccess
) {
1171 // The policy is changed. Clean the ManualAddress, Gateway and DnsServers,
1172 // shrink the variable data size, and fire up all the related events.
1174 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeManualAddress
];
1175 if (DataItem
->Data
.Ptr
!= NULL
) {
1176 FreePool (DataItem
->Data
.Ptr
);
1178 DataItem
->Data
.Ptr
= NULL
;
1179 DataItem
->DataSize
= 0;
1180 DataItem
->Status
= EFI_NOT_FOUND
;
1181 NetMapIterate (&DataItem
->EventMap
, Ip4Config2SignalEvent
, NULL
);
1183 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeGateway
];
1184 if (DataItem
->Data
.Ptr
!= NULL
) {
1185 FreePool (DataItem
->Data
.Ptr
);
1187 DataItem
->Data
.Ptr
= NULL
;
1188 DataItem
->DataSize
= 0;
1189 DataItem
->Status
= EFI_NOT_FOUND
;
1190 NetMapIterate (&DataItem
->EventMap
, Ip4Config2SignalEvent
, NULL
);
1192 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeDnsServer
];
1193 if (DataItem
->Data
.Ptr
!= NULL
) {
1194 FreePool (DataItem
->Data
.Ptr
);
1196 DataItem
->Data
.Ptr
= NULL
;
1197 DataItem
->DataSize
= 0;
1198 DataItem
->Status
= EFI_NOT_FOUND
;
1199 NetMapIterate (&DataItem
->EventMap
, Ip4Config2SignalEvent
, NULL
);
1201 if (NewPolicy
== Ip4Config2PolicyDhcp
) {
1202 SET_DATA_ATTRIB (DataItem
->Attribute
, DATA_ATTRIB_VOLATILE
);
1205 // The policy is changed from dhcp to static. Stop the DHCPv4 process
1206 // and destroy the DHCPv4 child.
1208 if (Instance
->Dhcp4Handle
!= NULL
) {
1209 Ip4Config2DestroyDhcp4 (Instance
);
1215 if (Instance
->Dhcp4Event
!= NULL
) {
1216 gBS
->CloseEvent (Instance
->Dhcp4Event
);
1217 Instance
->Dhcp4Event
= NULL
;
1222 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
1223 Ip4Config2OnPolicyChanged (IpSb
, NewPolicy
);
1225 Instance
->Policy
= NewPolicy
;
1231 The work function is to set the station addresses manually for the EFI IPv4
1232 network stack. It is only configurable when the policy is Ip4Config2PolicyStatic.
1234 @param[in] Instance Pointer to the IP4 config2 instance data.
1235 @param[in] DataSize Size of the buffer pointed to by Data in bytes.
1236 @param[in] Data The data buffer to set.
1238 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.
1239 @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set
1240 under the current policy.
1241 @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.
1242 @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation.
1243 @retval EFI_NOT_READY An asynchrous process is invoked to set the specified
1244 configuration data, and the process is not finished.
1245 @retval EFI_ABORTED The manual addresses to be set equal current
1247 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6
1248 network stack was set.
1252 Ip4Config2SetManualAddress (
1253 IN IP4_CONFIG2_INSTANCE
*Instance
,
1258 EFI_IP4_CONFIG2_MANUAL_ADDRESS NewAddress
;
1259 IP4_CONFIG2_DATA_ITEM
*DataItem
;
1261 IP4_ADDR StationAddress
;
1262 IP4_ADDR SubnetMask
;
1265 IP4_INTERFACE
*IpIf
;
1266 IP4_ROUTE_TABLE
*RouteTable
;
1269 Status
= EFI_SUCCESS
;
1274 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
1276 ASSERT (Instance
->DataItem
[Ip4Config2DataTypeManualAddress
].Status
!= EFI_NOT_READY
);
1278 if ((DataSize
!= 0) && ((DataSize
% sizeof (EFI_IP4_CONFIG2_MANUAL_ADDRESS
)) != 0)) {
1279 return EFI_BAD_BUFFER_SIZE
;
1282 if (Instance
->Policy
!= Ip4Config2PolicyStatic
) {
1283 return EFI_WRITE_PROTECTED
;
1286 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeManualAddress
];
1288 if (Data
!= NULL
&& DataSize
!= 0) {
1289 NewAddress
= *((EFI_IP4_CONFIG2_MANUAL_ADDRESS
*) Data
);
1291 StationAddress
= EFI_NTOHL (NewAddress
.Address
);
1292 SubnetMask
= EFI_NTOHL (NewAddress
.SubnetMask
);
1295 // Check whether the StationAddress/SubnetMask pair is valid.
1297 if (!Ip4StationAddressValid (StationAddress
, SubnetMask
)) {
1298 return EFI_INVALID_PARAMETER
;
1302 // Store the new data, and init the DataItem status to EFI_NOT_READY because
1303 // we may have an asynchronous configuration process.
1305 Ptr
= AllocateCopyPool (DataSize
, Data
);
1307 return EFI_OUT_OF_RESOURCES
;
1310 if (DataItem
->Data
.Ptr
!= NULL
) {
1311 FreePool (DataItem
->Data
.Ptr
);
1314 DataItem
->Data
.Ptr
= Ptr
;
1315 DataItem
->DataSize
= DataSize
;
1316 DataItem
->Status
= EFI_NOT_READY
;
1318 IpSb
->Reconfig
= TRUE
;
1319 Status
= Ip4Config2SetDefaultAddr (IpSb
, StationAddress
, SubnetMask
);
1321 DataItem
->Status
= Status
;
1323 if (EFI_ERROR (DataItem
->Status
) && DataItem
->Status
!= EFI_NOT_READY
) {
1327 DataItem
->Data
.Ptr
= NULL
;
1331 // DataSize is 0 and Data is NULL, clean up the manual address.
1333 if (DataItem
->Data
.Ptr
!= NULL
) {
1334 FreePool (DataItem
->Data
.Ptr
);
1336 DataItem
->Data
.Ptr
= NULL
;
1337 DataItem
->DataSize
= 0;
1338 DataItem
->Status
= EFI_NOT_FOUND
;
1341 // Free the default router table and Interface, clean up the assemble table.
1343 if (IpSb
->DefaultInterface
!= NULL
) {
1344 if (IpSb
->DefaultRouteTable
!= NULL
) {
1345 Ip4FreeRouteTable (IpSb
->DefaultRouteTable
);
1346 IpSb
->DefaultRouteTable
= NULL
;
1349 Ip4CancelReceive (IpSb
->DefaultInterface
);
1351 Ip4FreeInterface (IpSb
->DefaultInterface
, NULL
);
1352 IpSb
->DefaultInterface
= NULL
;
1355 Ip4CleanAssembleTable (&IpSb
->Assemble
);
1358 // Create new default interface and route table.
1360 IpIf
= Ip4CreateInterface (IpSb
->Mnp
, IpSb
->Controller
, IpSb
->Image
);
1362 return EFI_OUT_OF_RESOURCES
;
1365 RouteTable
= Ip4CreateRouteTable ();
1366 if (RouteTable
== NULL
) {
1367 Ip4FreeInterface (IpIf
, NULL
);
1368 return EFI_OUT_OF_RESOURCES
;
1371 IpSb
->DefaultInterface
= IpIf
;
1372 InsertHeadList (&IpSb
->Interfaces
, &IpIf
->Link
);
1373 IpSb
->DefaultRouteTable
= RouteTable
;
1374 Ip4ReceiveFrame (IpIf
, NULL
, Ip4AccpetFrame
, IpSb
);
1377 // Reset the State to unstarted.
1379 if (IpSb
->State
== IP4_SERVICE_CONFIGED
|| IpSb
->State
== IP4_SERVICE_STARTED
) {
1380 IpSb
->State
= IP4_SERVICE_UNSTARTED
;
1388 The work function is to set the gateway addresses manually for the EFI IPv4
1389 network stack that is running on the communication device that this EFI IPv4
1390 Configuration Protocol manages. It is not configurable when the policy is
1391 Ip4Config2PolicyDhcp. The gateway addresses must be unicast IPv4 addresses.
1393 @param[in] Instance The pointer to the IP4 config2 instance data.
1394 @param[in] DataSize The size of the buffer pointed to by Data in bytes.
1395 @param[in] Data The data buffer to set. This points to an array of
1396 EFI_IPv6_ADDRESS instances.
1398 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.
1399 @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set
1400 under the current policy.
1401 @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.
1402 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to complete the operation.
1403 @retval EFI_ABORTED The manual gateway addresses to be set equal the
1404 current configuration.
1405 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6
1406 network stack was set.
1410 Ip4Config2SetGateway (
1411 IN IP4_CONFIG2_INSTANCE
*Instance
,
1417 IP4_CONFIG2_DATA_ITEM
*DataItem
;
1422 EFI_IPv4_ADDRESS
*OldGateway
;
1423 EFI_IPv4_ADDRESS
*NewGateway
;
1424 UINTN OldGatewayCount
;
1425 UINTN NewGatewayCount
;
1436 if ((DataSize
!= 0) && (DataSize
% sizeof (EFI_IPv4_ADDRESS
) != 0)) {
1437 return EFI_BAD_BUFFER_SIZE
;
1440 if (Instance
->Policy
!= Ip4Config2PolicyStatic
) {
1441 return EFI_WRITE_PROTECTED
;
1444 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
1446 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeGateway
];
1447 OldGateway
= DataItem
->Data
.Gateway
;
1448 OldGatewayCount
= DataItem
->DataSize
/ sizeof (EFI_IPv4_ADDRESS
);
1450 for (Index1
= 0; Index1
< OldGatewayCount
; Index1
++) {
1452 // Remove the old route entry.
1454 CopyMem (&Gateway
, OldGateway
+ Index1
, sizeof (IP4_ADDR
));
1456 IpSb
->DefaultRouteTable
,
1457 IP4_ALLZERO_ADDRESS
,
1458 IP4_ALLZERO_ADDRESS
,
1464 if (Data
!= NULL
&& DataSize
!= 0) {
1465 NewGateway
= (EFI_IPv4_ADDRESS
*) Data
;
1466 NewGatewayCount
= DataSize
/ sizeof (EFI_IPv4_ADDRESS
);
1467 for (Index1
= 0; Index1
< NewGatewayCount
; Index1
++) {
1468 CopyMem (&Gateway
, NewGateway
+ Index1
, sizeof (IP4_ADDR
));
1470 if ((IpSb
->DefaultInterface
->SubnetMask
!= 0) &&
1471 !NetIp4IsUnicast (NTOHL (Gateway
), IpSb
->DefaultInterface
->SubnetMask
)) {
1472 return EFI_INVALID_PARAMETER
;
1475 for (Index2
= Index1
+ 1; Index2
< NewGatewayCount
; Index2
++) {
1476 if (EFI_IP4_EQUAL (NewGateway
+ Index1
, NewGateway
+ Index2
)) {
1477 return EFI_INVALID_PARAMETER
;
1482 if (NewGatewayCount
!= OldGatewayCount
) {
1483 Tmp
= AllocatePool (DataSize
);
1485 return EFI_OUT_OF_RESOURCES
;
1491 for (Index1
= 0; Index1
< NewGatewayCount
; Index1
++) {
1493 // Add the new route entry.
1495 CopyMem (&Gateway
, NewGateway
+ Index1
, sizeof (IP4_ADDR
));
1497 IpSb
->DefaultRouteTable
,
1498 IP4_ALLZERO_ADDRESS
,
1499 IP4_ALLZERO_ADDRESS
,
1506 if (!OneRemoved
&& !OneAdded
) {
1507 DataItem
->Status
= EFI_SUCCESS
;
1511 if (DataItem
->Data
.Ptr
!= NULL
) {
1512 FreePool (DataItem
->Data
.Ptr
);
1514 DataItem
->Data
.Ptr
= Tmp
;
1517 CopyMem (DataItem
->Data
.Ptr
, Data
, DataSize
);
1518 DataItem
->DataSize
= DataSize
;
1519 DataItem
->Status
= EFI_SUCCESS
;
1523 // DataSize is 0 and Data is NULL, clean up the Gateway address.
1525 if (DataItem
->Data
.Ptr
!= NULL
) {
1526 FreePool (DataItem
->Data
.Ptr
);
1528 DataItem
->Data
.Ptr
= NULL
;
1529 DataItem
->DataSize
= 0;
1530 DataItem
->Status
= EFI_NOT_FOUND
;
1537 The work function is to set the DNS server list for the EFI IPv4 network
1538 stack running on the communication device that this EFI_IP4_CONFIG2_PROTOCOL
1539 manages. It is not configurable when the policy is Ip4Config2PolicyDhcp.
1540 The DNS server addresses must be unicast IPv4 addresses.
1542 @param[in] Instance The pointer to the IP4 config2 instance data.
1543 @param[in] DataSize The size of the buffer pointed to by Data in bytes.
1544 @param[in] Data The data buffer to set, points to an array of
1545 EFI_IPv4_ADDRESS instances.
1547 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.
1548 @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set
1549 under the current policy.
1550 @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.
1551 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation.
1552 @retval EFI_ABORTED The DNS server addresses to be set equal the current
1554 @retval EFI_SUCCESS The specified configuration data for the EFI IPv4
1555 network stack was set.
1559 Ip4Config2SetDnsServer (
1560 IN IP4_CONFIG2_INSTANCE
*Instance
,
1566 IP4_CONFIG2_DATA_ITEM
*Item
;
1568 Status
= EFI_SUCCESS
;
1571 if (Instance
->Policy
!= Ip4Config2PolicyStatic
) {
1572 return EFI_WRITE_PROTECTED
;
1575 Item
= &Instance
->DataItem
[Ip4Config2DataTypeDnsServer
];
1577 if (DATA_ATTRIB_SET (Item
->Attribute
, DATA_ATTRIB_VOLATILE
)) {
1578 REMOVE_DATA_ATTRIB (Item
->Attribute
, DATA_ATTRIB_VOLATILE
);
1581 if (Data
!= NULL
&& DataSize
!= 0) {
1582 Status
= Ip4Config2SetDnsServerWorker (Instance
, DataSize
, Data
);
1585 // DataSize is 0 and Data is NULL, clean up the DnsServer address.
1587 if (Item
->Data
.Ptr
!= NULL
) {
1588 FreePool (Item
->Data
.Ptr
);
1590 Item
->Data
.Ptr
= NULL
;
1592 Item
->Status
= EFI_NOT_FOUND
;
1599 Generate the operational state of the interface this IP4 config2 instance manages
1600 and output in EFI_IP4_CONFIG2_INTERFACE_INFO.
1602 @param[in] IpSb The pointer to the IP4 service binding instance.
1603 @param[out] IfInfo The pointer to the IP4 config2 interface information structure.
1607 Ip4Config2InitIfInfo (
1608 IN IP4_SERVICE
*IpSb
,
1609 OUT EFI_IP4_CONFIG2_INTERFACE_INFO
*IfInfo
1614 EFI_IP4_CONFIG2_INTERFACE_INFO_NAME_SIZE
,
1616 IpSb
->Ip4Config2Instance
.IfIndex
1619 IfInfo
->IfType
= IpSb
->SnpMode
.IfType
;
1620 IfInfo
->HwAddressSize
= IpSb
->SnpMode
.HwAddressSize
;
1621 CopyMem (&IfInfo
->HwAddress
, &IpSb
->SnpMode
.CurrentAddress
, IfInfo
->HwAddressSize
);
1627 Set the configuration for the EFI IPv4 network stack running on the communication
1628 device this EFI_IP4_CONFIG2_PROTOCOL instance manages.
1630 This function is used to set the configuration data of type DataType for the EFI
1631 IPv4 network stack that is running on the communication device that this EFI IPv4
1632 Configuration Protocol instance manages.
1634 DataSize is used to calculate the count of structure instances in the Data for
1635 a DataType in which multiple structure instances are allowed.
1637 This function is always non-blocking. When setting some type of configuration data,
1638 an asynchronous process is invoked to check the correctness of the data, such as
1639 performing Duplicate Address Detection on the manually set local IPv4 addresses.
1640 EFI_NOT_READY is returned immediately to indicate that such an asynchronous process
1641 is invoked, and the process is not finished yet. The caller wanting to get the result
1642 of the asynchronous process is required to call RegisterDataNotify() to register an
1643 event on the specified configuration data. Once the event is signaled, the caller
1644 can call GetData() to obtain the configuration data and know the result.
1645 For other types of configuration data that do not require an asynchronous configuration
1646 process, the result of the operation is immediately returned.
1648 @param[in] This The pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.
1649 @param[in] DataType The type of data to set.
1650 @param[in] DataSize Size of the buffer pointed to by Data in bytes.
1651 @param[in] Data The data buffer to set. The type of the data buffer is
1652 associated with the DataType.
1654 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6
1655 network stack was set successfully.
1656 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:
1658 - One or more fields in Data and DataSize do not match the
1659 requirement of the data type indicated by DataType.
1660 @retval EFI_WRITE_PROTECTED The specified configuration data is read-only or the specified
1661 configuration data cannot be set under the current policy.
1662 @retval EFI_ACCESS_DENIED Another set operation on the specified configuration
1663 data is already in process.
1664 @retval EFI_NOT_READY An asynchronous process was invoked to set the specified
1665 configuration data, and the process is not finished yet.
1666 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type
1667 indicated by DataType.
1668 @retval EFI_UNSUPPORTED This DataType is not supported.
1669 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
1670 @retval EFI_DEVICE_ERROR An unexpected system error or network error occurred.
1675 EfiIp4Config2SetData (
1676 IN EFI_IP4_CONFIG2_PROTOCOL
*This
,
1677 IN EFI_IP4_CONFIG2_DATA_TYPE DataType
,
1684 IP4_CONFIG2_INSTANCE
*Instance
;
1687 if ((This
== NULL
) || (Data
== NULL
&& DataSize
!= 0) || (Data
!= NULL
&& DataSize
== 0)) {
1688 return EFI_INVALID_PARAMETER
;
1691 if (DataType
>= Ip4Config2DataTypeMaximum
) {
1692 return EFI_UNSUPPORTED
;
1695 Instance
= IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This
);
1696 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
1697 NET_CHECK_SIGNATURE (IpSb
, IP4_SERVICE_SIGNATURE
);
1700 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1702 Status
= Instance
->DataItem
[DataType
].Status
;
1703 if (Status
!= EFI_NOT_READY
) {
1705 if (Instance
->DataItem
[DataType
].SetData
== NULL
) {
1707 // This type of data is readonly.
1709 Status
= EFI_WRITE_PROTECTED
;
1712 Status
= Instance
->DataItem
[DataType
].SetData (Instance
, DataSize
, Data
);
1713 if (!EFI_ERROR (Status
)) {
1715 // Fire up the events registered with this type of data.
1717 NetMapIterate (&Instance
->DataItem
[DataType
].EventMap
, Ip4Config2SignalEvent
, NULL
);
1718 Ip4Config2WriteConfigData (IpSb
->MacString
, Instance
);
1719 } else if (Status
== EFI_ABORTED
) {
1721 // The SetData is aborted because the data to set is the same with
1722 // the one maintained.
1724 Status
= EFI_SUCCESS
;
1725 NetMapIterate (&Instance
->DataItem
[DataType
].EventMap
, Ip4Config2SignalEvent
, NULL
);
1730 // Another asynchornous process is on the way.
1732 Status
= EFI_ACCESS_DENIED
;
1735 gBS
->RestoreTPL (OldTpl
);
1741 Get the configuration data for the EFI IPv4 network stack running on the communication
1742 device that this EFI_IP4_CONFIG2_PROTOCOL instance manages.
1744 This function returns the configuration data of type DataType for the EFI IPv4 network
1745 stack running on the communication device that this EFI IPv4 Configuration Protocol instance
1748 The caller is responsible for allocating the buffer used to return the specified
1749 configuration data. The required size will be returned to the caller if the size of
1750 the buffer is too small.
1752 EFI_NOT_READY is returned if the specified configuration data is not ready due to an
1753 asynchronous configuration process already in progress. The caller can call RegisterDataNotify()
1754 to register an event on the specified configuration data. Once the asynchronous configuration
1755 process is finished, the event will be signaled, and a subsequent GetData() call will return
1756 the specified configuration data.
1758 @param[in] This Pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.
1759 @param[in] DataType The type of data to get.
1760 @param[in, out] DataSize On input, in bytes, the size of Data. On output, in bytes, the
1761 size of buffer required to store the specified configuration data.
1762 @param[in] Data The data buffer in which the configuration data is returned. The
1763 type of the data buffer is associated with the DataType.
1764 This is an optional parameter that may be NULL.
1766 @retval EFI_SUCCESS The specified configuration data was obtained successfully.
1767 @retval EFI_INVALID_PARAMETER One or more of the followings are TRUE:
1770 - Data is NULL if *DataSize is not zero.
1771 @retval EFI_BUFFER_TOO_SMALL The size of Data is too small for the specified configuration data,
1772 and the required size is returned in DataSize.
1773 @retval EFI_NOT_READY The specified configuration data is not ready due to an
1774 asynchronous configuration process already in progress.
1775 @retval EFI_NOT_FOUND The specified configuration data is not found.
1780 EfiIp4Config2GetData (
1781 IN EFI_IP4_CONFIG2_PROTOCOL
*This
,
1782 IN EFI_IP4_CONFIG2_DATA_TYPE DataType
,
1783 IN OUT UINTN
*DataSize
,
1784 IN VOID
*Data OPTIONAL
1789 IP4_CONFIG2_INSTANCE
*Instance
;
1790 IP4_CONFIG2_DATA_ITEM
*DataItem
;
1792 if ((This
== NULL
) || (DataSize
== NULL
) || ((*DataSize
!= 0) && (Data
== NULL
))) {
1793 return EFI_INVALID_PARAMETER
;
1796 if (DataType
>= Ip4Config2DataTypeMaximum
) {
1797 return EFI_NOT_FOUND
;
1800 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1802 Instance
= IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This
);
1803 DataItem
= &Instance
->DataItem
[DataType
];
1805 Status
= Instance
->DataItem
[DataType
].Status
;
1806 if (!EFI_ERROR (Status
)) {
1808 if (DataItem
->GetData
!= NULL
) {
1810 Status
= DataItem
->GetData (Instance
, DataSize
, Data
);
1811 } else if (*DataSize
< Instance
->DataItem
[DataType
].DataSize
) {
1813 // Update the buffer length.
1815 *DataSize
= Instance
->DataItem
[DataType
].DataSize
;
1816 Status
= EFI_BUFFER_TOO_SMALL
;
1819 *DataSize
= Instance
->DataItem
[DataType
].DataSize
;
1820 CopyMem (Data
, Instance
->DataItem
[DataType
].Data
.Ptr
, *DataSize
);
1824 gBS
->RestoreTPL (OldTpl
);
1830 Register an event that is signaled whenever a configuration process on the specified
1831 configuration data is done.
1833 This function registers an event that is to be signaled whenever a configuration
1834 process on the specified configuration data is performed. An event can be registered
1835 for a different DataType simultaneously. The caller is responsible for determining
1836 which type of configuration data causes the signaling of the event in such an event.
1838 @param[in] This Pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.
1839 @param[in] DataType The type of data to unregister the event for.
1840 @param[in] Event The event to register.
1842 @retval EFI_SUCCESS The notification event for the specified configuration data is
1844 @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.
1845 @retval EFI_UNSUPPORTED The configuration data type specified by DataType is not
1847 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
1848 @retval EFI_ACCESS_DENIED The Event is already registered for the DataType.
1853 EfiIp4Config2RegisterDataNotify (
1854 IN EFI_IP4_CONFIG2_PROTOCOL
*This
,
1855 IN EFI_IP4_CONFIG2_DATA_TYPE DataType
,
1861 IP4_CONFIG2_INSTANCE
*Instance
;
1865 if ((This
== NULL
) || (Event
== NULL
)) {
1866 return EFI_INVALID_PARAMETER
;
1869 if (DataType
>= Ip4Config2DataTypeMaximum
) {
1870 return EFI_UNSUPPORTED
;
1873 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1875 Instance
= IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This
);
1876 EventMap
= &Instance
->DataItem
[DataType
].EventMap
;
1879 // Check whether this event is already registered for this DataType.
1881 Item
= NetMapFindKey (EventMap
, Event
);
1884 Status
= NetMapInsertTail (EventMap
, Event
, NULL
);
1886 if (EFI_ERROR (Status
)) {
1888 Status
= EFI_OUT_OF_RESOURCES
;
1893 Status
= EFI_ACCESS_DENIED
;
1896 gBS
->RestoreTPL (OldTpl
);
1902 Remove a previously registered event for the specified configuration data.
1904 @param This The pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.
1905 @param DataType The type of data to remove from the previously
1907 @param Event The event to be unregistered.
1909 @retval EFI_SUCCESS The event registered for the specified
1910 configuration data was removed.
1911 @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.
1912 @retval EFI_NOT_FOUND The Event has not been registered for the
1918 EfiIp4Config2UnregisterDataNotify (
1919 IN EFI_IP4_CONFIG2_PROTOCOL
*This
,
1920 IN EFI_IP4_CONFIG2_DATA_TYPE DataType
,
1926 IP4_CONFIG2_INSTANCE
*Instance
;
1929 if ((This
== NULL
) || (Event
== NULL
)) {
1930 return EFI_INVALID_PARAMETER
;
1933 if (DataType
>= Ip4Config2DataTypeMaximum
) {
1934 return EFI_NOT_FOUND
;
1937 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1939 Instance
= IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This
);
1941 Item
= NetMapFindKey (&Instance
->DataItem
[DataType
].EventMap
, Event
);
1944 NetMapRemoveItem (&Instance
->DataItem
[DataType
].EventMap
, Item
, NULL
);
1945 Status
= EFI_SUCCESS
;
1948 Status
= EFI_NOT_FOUND
;
1951 gBS
->RestoreTPL (OldTpl
);
1957 Initialize an IP4_CONFIG2_INSTANCE.
1959 @param[out] Instance The buffer of IP4_CONFIG2_INSTANCE to be initialized.
1961 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation.
1962 @retval EFI_SUCCESS The IP4_CONFIG2_INSTANCE initialized successfully.
1966 Ip4Config2InitInstance (
1967 OUT IP4_CONFIG2_INSTANCE
*Instance
1971 IP4_CONFIG2_INSTANCE
*TmpInstance
;
1976 IP4_CONFIG2_DATA_ITEM
*DataItem
;
1979 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
1981 Instance
->Signature
= IP4_CONFIG2_INSTANCE_SIGNATURE
;
1985 // Determine the index of this interface.
1988 NET_LIST_FOR_EACH (Entry
, &mIp4Config2InstanceList
) {
1989 TmpInstance
= NET_LIST_USER_STRUCT_S (Entry
, IP4_CONFIG2_INSTANCE
, Link
, IP4_CONFIG2_INSTANCE_SIGNATURE
);
1991 if (TmpInstance
->IfIndex
> IfIndex
) {
1993 // There is a sequence hole because some interface is down.
2001 Instance
->IfIndex
= IfIndex
;
2002 NetListInsertBefore (Entry
, &Instance
->Link
);
2004 for (Index
= 0; Index
< Ip4Config2DataTypeMaximum
; Index
++) {
2006 // Initialize the event map for each data item.
2008 NetMapInit (&Instance
->DataItem
[Index
].EventMap
);
2013 // Initialize each data type: associate storage and set data size for the
2014 // fixed size data types, hook the SetData function, set the data attribute.
2016 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeInterfaceInfo
];
2017 DataItem
->GetData
= Ip4Config2GetIfInfo
;
2018 DataItem
->Data
.Ptr
= &Instance
->InterfaceInfo
;
2019 DataItem
->DataSize
= sizeof (Instance
->InterfaceInfo
);
2020 SET_DATA_ATTRIB (DataItem
->Attribute
, DATA_ATTRIB_SIZE_FIXED
| DATA_ATTRIB_VOLATILE
);
2021 Ip4Config2InitIfInfo (IpSb
, &Instance
->InterfaceInfo
);
2023 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypePolicy
];
2024 DataItem
->SetData
= Ip4Config2SetPolicy
;
2025 DataItem
->Data
.Ptr
= &Instance
->Policy
;
2026 DataItem
->DataSize
= sizeof (Instance
->Policy
);
2027 Instance
->Policy
= Ip4Config2PolicyStatic
;
2028 SET_DATA_ATTRIB (DataItem
->Attribute
, DATA_ATTRIB_SIZE_FIXED
);
2030 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeManualAddress
];
2031 DataItem
->SetData
= Ip4Config2SetManualAddress
;
2032 DataItem
->Status
= EFI_NOT_FOUND
;
2034 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeGateway
];
2035 DataItem
->SetData
= Ip4Config2SetGateway
;
2036 DataItem
->Status
= EFI_NOT_FOUND
;
2038 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeDnsServer
];
2039 DataItem
->SetData
= Ip4Config2SetDnsServer
;
2040 DataItem
->Status
= EFI_NOT_FOUND
;
2042 Instance
->Configured
= TRUE
;
2045 // Try to read the config data from NV variable.
2046 // If not found, write initialized config data into NV variable
2047 // as a default config data.
2049 Status
= Ip4Config2ReadConfigData (IpSb
->MacString
, Instance
);
2050 if (Status
== EFI_NOT_FOUND
) {
2051 Status
= Ip4Config2WriteConfigData (IpSb
->MacString
, Instance
);
2054 if (EFI_ERROR (Status
)) {
2058 Instance
->Ip4Config2
.SetData
= EfiIp4Config2SetData
;
2059 Instance
->Ip4Config2
.GetData
= EfiIp4Config2GetData
;
2060 Instance
->Ip4Config2
.RegisterDataNotify
= EfiIp4Config2RegisterDataNotify
;
2061 Instance
->Ip4Config2
.UnregisterDataNotify
= EfiIp4Config2UnregisterDataNotify
;
2064 // Publish the IP4 configuration form
2066 return Ip4Config2FormInit (Instance
);
2071 Release an IP4_CONFIG2_INSTANCE.
2073 @param[in, out] Instance The buffer of IP4_CONFIG2_INSTANCE to be freed.
2077 Ip4Config2CleanInstance (
2078 IN OUT IP4_CONFIG2_INSTANCE
*Instance
2082 IP4_CONFIG2_DATA_ITEM
*DataItem
;
2084 if (Instance
->DeclineAddress
!= NULL
) {
2085 FreePool (Instance
->DeclineAddress
);
2088 if (!Instance
->Configured
) {
2092 if (Instance
->Dhcp4Handle
!= NULL
) {
2094 Ip4Config2DestroyDhcp4 (Instance
);
2100 if (Instance
->Dhcp4Event
!= NULL
) {
2101 gBS
->CloseEvent (Instance
->Dhcp4Event
);
2102 Instance
->Dhcp4Event
= NULL
;
2105 for (Index
= 0; Index
< Ip4Config2DataTypeMaximum
; Index
++) {
2107 DataItem
= &Instance
->DataItem
[Index
];
2109 if (!DATA_ATTRIB_SET (DataItem
->Attribute
, DATA_ATTRIB_SIZE_FIXED
)) {
2110 if (DataItem
->Data
.Ptr
!= NULL
) {
2111 FreePool (DataItem
->Data
.Ptr
);
2113 DataItem
->Data
.Ptr
= NULL
;
2114 DataItem
->DataSize
= 0;
2117 NetMapClean (&Instance
->DataItem
[Index
].EventMap
);
2120 Ip4Config2FormUnload (Instance
);
2122 RemoveEntryList (&Instance
->Link
);
2126 The event handle for IP4 auto reconfiguration. The original default
2127 interface and route table will be removed as the default.
2129 @param[in] Context The IP4 service binding instance.
2134 Ip4AutoReconfigCallBackDpc (
2140 IpSb
= (IP4_SERVICE
*) Context
;
2141 NET_CHECK_SIGNATURE (IpSb
, IP4_SERVICE_SIGNATURE
);
2143 if (IpSb
->State
> IP4_SERVICE_UNSTARTED
) {
2144 IpSb
->State
= IP4_SERVICE_UNSTARTED
;
2147 IpSb
->Reconfig
= TRUE
;
2149 Ip4StartAutoConfig (&IpSb
->Ip4Config2Instance
);
2156 Request Ip4AutoReconfigCallBackDpc as a DPC at TPL_CALLBACK.
2158 @param Event The event that is signalled.
2159 @param Context The IP4 service binding instance.
2164 Ip4AutoReconfigCallBack (
2170 // Request Ip4AutoReconfigCallBackDpc as a DPC at TPL_CALLBACK
2172 QueueDpc (TPL_CALLBACK
, Ip4AutoReconfigCallBackDpc
, Context
);