2 The implementation of EFI IPv4 Configuration II Protocol.
4 Copyright (c) 2015 - 2017, 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 IpSb
->DefaultRouteTable
,
570 // Add a route for the connected network.
572 Subnet
= StationAddress
& SubnetMask
;
575 IpSb
->DefaultRouteTable
,
581 IpSb
->State
= IP4_SERVICE_CONFIGED
;
582 IpSb
->Reconfig
= FALSE
;
588 Set the station address, subnetmask and gateway address for the default interface.
590 @param[in] Instance The pointer to the IP4 config2 instance data.
591 @param[in] StationAddress Ip address to be set.
592 @param[in] SubnetMask Subnet to be set.
593 @param[in] GatewayAddress Gateway to be set.
595 @retval EFI_SUCCESS Set default If successful.
596 @retval Others Errors occur as indicated.
600 Ip4Config2SetDefaultIf (
601 IN IP4_CONFIG2_INSTANCE
*Instance
,
602 IN IP4_ADDR StationAddress
,
603 IN IP4_ADDR SubnetMask
,
604 IN IP4_ADDR GatewayAddress
610 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
613 // Check whether the StationAddress/SubnetMask pair is valid.
615 if (!Ip4StationAddressValid (StationAddress
, SubnetMask
)) {
616 return EFI_INVALID_PARAMETER
;
619 Status
= Ip4Config2SetDefaultAddr (IpSb
, StationAddress
, SubnetMask
);
620 if (EFI_ERROR (Status
)) {
625 // Create a route if there is a default router.
627 if (GatewayAddress
!= IP4_ALLZERO_ADDRESS
) {
629 IpSb
->DefaultRouteTable
,
641 Release all the DHCP related resources.
643 @param Instance The IP4 config2 instance.
649 Ip4Config2CleanDhcp4 (
650 IN IP4_CONFIG2_INSTANCE
*Instance
655 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
657 if (Instance
->Dhcp4
!= NULL
) {
658 Instance
->Dhcp4
->Stop (Instance
->Dhcp4
);
661 Instance
->Dhcp4Handle
,
662 &gEfiDhcp4ProtocolGuid
,
667 Instance
->Dhcp4
= NULL
;
670 if (Instance
->Dhcp4Handle
!= NULL
) {
671 NetLibDestroyServiceChild (
674 &gEfiDhcp4ServiceBindingProtocolGuid
,
675 Instance
->Dhcp4Handle
678 Instance
->Dhcp4Handle
= NULL
;
681 if (Instance
->Dhcp4Event
!= NULL
) {
682 gBS
->CloseEvent (Instance
->Dhcp4Event
);
683 Instance
->Dhcp4Event
= NULL
;
688 This worker function sets the DNS server list for the EFI IPv4 network
689 stack running on the communication device that this EFI_IP4_CONFIG2_PROTOCOL
690 manages. The DNS server addresses must be unicast IPv4 addresses.
692 @param[in] Instance The pointer to the IP4 config2 instance data.
693 @param[in] DataSize The size of the buffer pointed to by Data in bytes.
694 @param[in] Data The data buffer to set, points to an array of
695 EFI_IPv4_ADDRESS instances.
697 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.
698 @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.
699 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation.
700 @retval EFI_ABORTED The DNS server addresses to be set equal the current
702 @retval EFI_SUCCESS The specified configuration data for the EFI IPv4
703 network stack was set.
707 Ip4Config2SetDnsServerWorker (
708 IN IP4_CONFIG2_INSTANCE
*Instance
,
715 EFI_IPv4_ADDRESS
*OldDns
;
716 EFI_IPv4_ADDRESS
*NewDns
;
719 IP4_CONFIG2_DATA_ITEM
*Item
;
724 if ((DataSize
% sizeof (EFI_IPv4_ADDRESS
) != 0) || (DataSize
== 0)) {
725 return EFI_BAD_BUFFER_SIZE
;
728 Item
= &Instance
->DataItem
[Ip4Config2DataTypeDnsServer
];
729 NewDns
= (EFI_IPv4_ADDRESS
*) Data
;
730 OldDns
= Item
->Data
.DnsServers
;
731 NewDnsCount
= DataSize
/ sizeof (EFI_IPv4_ADDRESS
);
732 OldDnsCount
= Item
->DataSize
/ sizeof (EFI_IPv4_ADDRESS
);
735 if (NewDnsCount
!= OldDnsCount
) {
736 Tmp
= AllocatePool (DataSize
);
738 return EFI_OUT_OF_RESOURCES
;
744 for (NewIndex
= 0; NewIndex
< NewDnsCount
; NewIndex
++) {
745 CopyMem (&DnsAddress
, NewDns
+ NewIndex
, sizeof (IP4_ADDR
));
746 if (IP4_IS_UNSPECIFIED (NTOHL (DnsAddress
)) || IP4_IS_LOCAL_BROADCAST (NTOHL (DnsAddress
))) {
748 // The dns server address must be unicast.
753 return EFI_INVALID_PARAMETER
;
758 // If any address in the new setting is not in the old settings, skip the
759 // comparision below.
764 for (OldIndex
= 0; OldIndex
< OldDnsCount
; OldIndex
++) {
765 if (EFI_IP4_EQUAL (NewDns
+ NewIndex
, OldDns
+ OldIndex
)) {
767 // If found break out.
773 if (OldIndex
== OldDnsCount
) {
778 if (!OneAdded
&& (DataSize
== Item
->DataSize
)) {
780 // No new item is added and the size is the same.
782 Item
->Status
= EFI_SUCCESS
;
786 if (Item
->Data
.Ptr
!= NULL
) {
787 FreePool (Item
->Data
.Ptr
);
789 Item
->Data
.Ptr
= Tmp
;
792 CopyMem (Item
->Data
.Ptr
, Data
, DataSize
);
793 Item
->DataSize
= DataSize
;
794 Item
->Status
= EFI_SUCCESS
;
802 Callback function when DHCP process finished. It will save the
803 retrieved IP configure parameter from DHCP to the NVRam.
805 @param Event The callback event
806 @param Context Opaque context to the callback
813 Ip4Config2OnDhcp4Complete (
818 IP4_CONFIG2_INSTANCE
*Instance
;
819 EFI_DHCP4_MODE_DATA Dhcp4Mode
;
821 IP4_ADDR StationAddress
;
823 IP4_ADDR GatewayAddress
;
826 EFI_DHCP4_PACKET_OPTION
**OptionList
;
828 Instance
= (IP4_CONFIG2_INSTANCE
*) Context
;
829 ASSERT (Instance
->Dhcp4
!= NULL
);
832 // Get the DHCP retrieved parameters
834 Status
= Instance
->Dhcp4
->GetModeData (Instance
->Dhcp4
, &Dhcp4Mode
);
836 if (EFI_ERROR (Status
)) {
840 if (Dhcp4Mode
.State
== Dhcp4Bound
) {
841 StationAddress
= EFI_NTOHL (Dhcp4Mode
.ClientAddress
);
842 SubnetMask
= EFI_NTOHL (Dhcp4Mode
.SubnetMask
);
843 GatewayAddress
= EFI_NTOHL (Dhcp4Mode
.RouterAddress
);
845 Status
= Ip4Config2SetDefaultIf (Instance
, StationAddress
, SubnetMask
, GatewayAddress
);
846 if (EFI_ERROR (Status
)) {
851 // Parse the ACK to get required DNS server information.
856 Status
= Instance
->Dhcp4
->Parse (Instance
->Dhcp4
, Dhcp4Mode
.ReplyPacket
, &OptionCount
, OptionList
);
857 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
861 OptionList
= AllocateZeroPool (OptionCount
* sizeof (EFI_DHCP4_PACKET_OPTION
*));
862 if (OptionList
== NULL
) {
866 Status
= Instance
->Dhcp4
->Parse (Instance
->Dhcp4
, Dhcp4Mode
.ReplyPacket
, &OptionCount
, OptionList
);
867 if (EFI_ERROR (Status
)) {
868 FreePool (OptionList
);
872 for (Index
= 0; Index
< OptionCount
; Index
++) {
874 // Look for DNS Server opcode (6).
876 if (OptionList
[Index
]->OpCode
== DHCP4_TAG_DNS_SERVER
) {
877 if (((OptionList
[Index
]->Length
& 0x3) != 0) || (OptionList
[Index
]->Length
== 0)) {
881 Ip4Config2SetDnsServerWorker (Instance
, OptionList
[Index
]->Length
, &OptionList
[Index
]->Data
[0]);
886 FreePool (OptionList
);
888 Instance
->DhcpSuccess
= TRUE
;
892 Ip4Config2CleanDhcp4 (Instance
);
898 Start the DHCP configuration for this IP service instance.
899 It will locates the EFI_IP4_CONFIG2_PROTOCOL, then start the
902 @param[in] Instance The IP4 config2 instance to configure
904 @retval EFI_SUCCESS The auto configuration is successfully started
905 @retval Others Failed to start auto configuration.
910 IN IP4_CONFIG2_INSTANCE
*Instance
914 EFI_DHCP4_PROTOCOL
*Dhcp4
;
915 EFI_DHCP4_MODE_DATA Dhcp4Mode
;
916 EFI_DHCP4_PACKET_OPTION
*OptionList
[1];
917 IP4_CONFIG2_DHCP4_OPTION ParaList
;
920 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
922 if (IpSb
->State
> IP4_SERVICE_UNSTARTED
) {
927 // A host must not invoke DHCP configuration if it is already
928 // participating in the DHCP configuraiton process.
930 if (Instance
->Dhcp4Handle
!= NULL
) {
934 Status
= NetLibCreateServiceChild (
937 &gEfiDhcp4ServiceBindingProtocolGuid
,
938 &Instance
->Dhcp4Handle
941 if (Status
== EFI_UNSUPPORTED
) {
943 // No DHCPv4 Service Binding protocol, register a notify.
945 if (Instance
->Dhcp4SbNotifyEvent
== NULL
) {
946 Instance
->Dhcp4SbNotifyEvent
= EfiCreateProtocolNotifyEvent (
947 &gEfiDhcp4ServiceBindingProtocolGuid
,
949 Ip4Config2OnDhcp4SbInstalled
,
951 &Instance
->Registration
956 if (EFI_ERROR (Status
)) {
960 if (Instance
->Dhcp4SbNotifyEvent
!= NULL
) {
961 gBS
->CloseEvent (Instance
->Dhcp4SbNotifyEvent
);
964 Status
= gBS
->OpenProtocol (
965 Instance
->Dhcp4Handle
,
966 &gEfiDhcp4ProtocolGuid
,
967 (VOID
**) &Instance
->Dhcp4
,
970 EFI_OPEN_PROTOCOL_BY_DRIVER
972 if (EFI_ERROR (Status
)) {
973 NetLibDestroyServiceChild (
976 &gEfiDhcp4ServiceBindingProtocolGuid
,
977 Instance
->Dhcp4Handle
980 Instance
->Dhcp4Handle
= NULL
;
986 // Check the current DHCP status, if the DHCP process has
987 // already finished, return now.
989 Dhcp4
= Instance
->Dhcp4
;
990 Status
= Dhcp4
->GetModeData (Dhcp4
, &Dhcp4Mode
);
991 if (Dhcp4Mode
.State
== Dhcp4Bound
) {
992 Ip4Config2OnDhcp4Complete (NULL
, Instance
);
998 // Try to start the DHCP process. Use most of the current
999 // DHCP configuration to avoid problems if some DHCP client
1000 // yields the control of this DHCP service to us.
1002 ParaList
.Head
.OpCode
= DHCP4_TAG_PARA_LIST
;
1003 ParaList
.Head
.Length
= 3;
1004 ParaList
.Head
.Data
[0] = DHCP4_TAG_NETMASK
;
1005 ParaList
.Route
= DHCP4_TAG_ROUTER
;
1006 ParaList
.Dns
= DHCP4_TAG_DNS_SERVER
;
1007 OptionList
[0] = &ParaList
.Head
;
1008 Dhcp4Mode
.ConfigData
.OptionCount
= 1;
1009 Dhcp4Mode
.ConfigData
.OptionList
= OptionList
;
1011 Status
= Dhcp4
->Configure (Dhcp4
, &Dhcp4Mode
.ConfigData
);
1012 if (EFI_ERROR (Status
)) {
1013 gBS
->CloseProtocol (
1014 Instance
->Dhcp4Handle
,
1015 &gEfiDhcp4ProtocolGuid
,
1020 NetLibDestroyServiceChild (
1023 &gEfiDhcp4ServiceBindingProtocolGuid
,
1024 Instance
->Dhcp4Handle
1027 Instance
->Dhcp4
= NULL
;
1029 Instance
->Dhcp4Handle
= NULL
;
1035 // Start the DHCP process
1037 Status
= gBS
->CreateEvent (
1040 Ip4Config2OnDhcp4Complete
,
1042 &Instance
->Dhcp4Event
1044 if (EFI_ERROR (Status
)) {
1045 Ip4Config2DestroyDhcp4 (Instance
);
1049 Status
= Dhcp4
->Start (Dhcp4
, Instance
->Dhcp4Event
);
1050 if (EFI_ERROR (Status
)) {
1051 Ip4Config2DestroyDhcp4 (Instance
);
1052 gBS
->CloseEvent (Instance
->Dhcp4Event
);
1053 Instance
->Dhcp4Event
= NULL
;
1058 IpSb
->State
= IP4_SERVICE_STARTED
;
1067 The work function is to get the interface information of the communication
1068 device this IP4_CONFIG2_INSTANCE manages.
1070 @param[in] Instance Pointer to the IP4 config2 instance data.
1071 @param[in, out] DataSize On input, in bytes, the size of Data. On output, in
1072 bytes, the size of buffer required to store the specified
1074 @param[in] Data The data buffer in which the configuration data is returned.
1075 Ignored if DataSize is ZERO.
1077 @retval EFI_BUFFER_TOO_SMALL The size of Data is too small for the specified
1078 configuration data, and the required size is
1079 returned in DataSize.
1080 @retval EFI_SUCCESS The specified configuration data was obtained.
1084 Ip4Config2GetIfInfo (
1085 IN IP4_CONFIG2_INSTANCE
*Instance
,
1086 IN OUT UINTN
*DataSize
,
1087 IN VOID
*Data OPTIONAL
1092 IP4_CONFIG2_DATA_ITEM
*Item
;
1093 EFI_IP4_CONFIG2_INTERFACE_INFO
*IfInfo
;
1096 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
1097 Length
= sizeof (EFI_IP4_CONFIG2_INTERFACE_INFO
);
1099 if (IpSb
->DefaultRouteTable
!= NULL
) {
1100 Length
+= IpSb
->DefaultRouteTable
->TotalNum
* sizeof (EFI_IP4_ROUTE_TABLE
);
1103 if (*DataSize
< Length
) {
1105 return EFI_BUFFER_TOO_SMALL
;
1109 // Copy the fixed size part of the interface info.
1111 Item
= &Instance
->DataItem
[Ip4Config2DataTypeInterfaceInfo
];
1112 IfInfo
= (EFI_IP4_CONFIG2_INTERFACE_INFO
*) Data
;
1113 CopyMem (IfInfo
, Item
->Data
.Ptr
, sizeof (EFI_IP4_CONFIG2_INTERFACE_INFO
));
1116 // Update the address info.
1118 if (IpSb
->DefaultInterface
!= NULL
) {
1119 Address
= HTONL (IpSb
->DefaultInterface
->Ip
);
1120 CopyMem (&IfInfo
->StationAddress
, &Address
, sizeof (EFI_IPv4_ADDRESS
));
1121 Address
= HTONL (IpSb
->DefaultInterface
->SubnetMask
);
1122 CopyMem (&IfInfo
->SubnetMask
, &Address
, sizeof (EFI_IPv4_ADDRESS
));
1125 if (IpSb
->DefaultRouteTable
!= NULL
) {
1126 IfInfo
->RouteTableSize
= IpSb
->DefaultRouteTable
->TotalNum
;
1127 IfInfo
->RouteTable
= (EFI_IP4_ROUTE_TABLE
*) ((UINT8
*) Data
+ sizeof (EFI_IP4_CONFIG2_INTERFACE_INFO
));
1129 Ip4Config2BuildDefaultRouteTable (IpSb
, IfInfo
->RouteTable
);
1136 The work function is to set the general configuration policy for the EFI IPv4 network
1137 stack that is running on the communication device managed by this IP4_CONFIG2_INSTANCE.
1138 The policy will affect other configuration settings.
1140 @param[in] Instance Pointer to the IP4 config2 instance data.
1141 @param[in] DataSize Size of the buffer pointed to by Data in bytes.
1142 @param[in] Data The data buffer to set.
1144 @retval EFI_INVALID_PARAMETER The to be set policy is invalid.
1145 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.
1146 @retval EFI_ABORTED The new policy equals the current policy.
1147 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6
1148 network stack was set.
1152 Ip4Config2SetPolicy (
1153 IN IP4_CONFIG2_INSTANCE
*Instance
,
1158 EFI_IP4_CONFIG2_POLICY NewPolicy
;
1159 IP4_CONFIG2_DATA_ITEM
*DataItem
;
1162 if (DataSize
!= sizeof (EFI_IP4_CONFIG2_POLICY
)) {
1163 return EFI_BAD_BUFFER_SIZE
;
1166 NewPolicy
= *((EFI_IP4_CONFIG2_POLICY
*) Data
);
1168 if (NewPolicy
>= Ip4Config2PolicyMax
) {
1169 return EFI_INVALID_PARAMETER
;
1172 if (NewPolicy
== Instance
->Policy
) {
1173 if (NewPolicy
!= Ip4Config2PolicyDhcp
|| Instance
->DhcpSuccess
) {
1178 // The policy is changed. Clean the ManualAddress, Gateway and DnsServers,
1179 // shrink the variable data size, and fire up all the related events.
1181 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeManualAddress
];
1182 if (DataItem
->Data
.Ptr
!= NULL
) {
1183 FreePool (DataItem
->Data
.Ptr
);
1185 DataItem
->Data
.Ptr
= NULL
;
1186 DataItem
->DataSize
= 0;
1187 DataItem
->Status
= EFI_NOT_FOUND
;
1188 NetMapIterate (&DataItem
->EventMap
, Ip4Config2SignalEvent
, NULL
);
1190 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeGateway
];
1191 if (DataItem
->Data
.Ptr
!= NULL
) {
1192 FreePool (DataItem
->Data
.Ptr
);
1194 DataItem
->Data
.Ptr
= NULL
;
1195 DataItem
->DataSize
= 0;
1196 DataItem
->Status
= EFI_NOT_FOUND
;
1197 NetMapIterate (&DataItem
->EventMap
, Ip4Config2SignalEvent
, NULL
);
1199 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeDnsServer
];
1200 if (DataItem
->Data
.Ptr
!= NULL
) {
1201 FreePool (DataItem
->Data
.Ptr
);
1203 DataItem
->Data
.Ptr
= NULL
;
1204 DataItem
->DataSize
= 0;
1205 DataItem
->Status
= EFI_NOT_FOUND
;
1206 NetMapIterate (&DataItem
->EventMap
, Ip4Config2SignalEvent
, NULL
);
1208 if (NewPolicy
== Ip4Config2PolicyDhcp
) {
1209 SET_DATA_ATTRIB (DataItem
->Attribute
, DATA_ATTRIB_VOLATILE
);
1212 // The policy is changed from dhcp to static. Stop the DHCPv4 process
1213 // and destroy the DHCPv4 child.
1215 if (Instance
->Dhcp4Handle
!= NULL
) {
1216 Ip4Config2DestroyDhcp4 (Instance
);
1222 if (Instance
->Dhcp4Event
!= NULL
) {
1223 gBS
->CloseEvent (Instance
->Dhcp4Event
);
1224 Instance
->Dhcp4Event
= NULL
;
1229 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
1230 Ip4Config2OnPolicyChanged (IpSb
, NewPolicy
);
1232 Instance
->Policy
= NewPolicy
;
1238 The work function is to set the station addresses manually for the EFI IPv4
1239 network stack. It is only configurable when the policy is Ip4Config2PolicyStatic.
1241 @param[in] Instance Pointer to the IP4 config2 instance data.
1242 @param[in] DataSize Size of the buffer pointed to by Data in bytes.
1243 @param[in] Data The data buffer to set.
1245 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.
1246 @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set
1247 under the current policy.
1248 @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.
1249 @retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation.
1250 @retval EFI_NOT_READY An asynchrous process is invoked to set the specified
1251 configuration data, and the process is not finished.
1252 @retval EFI_ABORTED The manual addresses to be set equal current
1254 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6
1255 network stack was set.
1259 Ip4Config2SetManualAddress (
1260 IN IP4_CONFIG2_INSTANCE
*Instance
,
1265 EFI_IP4_CONFIG2_MANUAL_ADDRESS NewAddress
;
1266 IP4_CONFIG2_DATA_ITEM
*DataItem
;
1268 IP4_ADDR StationAddress
;
1269 IP4_ADDR SubnetMask
;
1272 IP4_INTERFACE
*IpIf
;
1273 IP4_ROUTE_TABLE
*RouteTable
;
1276 Status
= EFI_SUCCESS
;
1281 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
1283 ASSERT (Instance
->DataItem
[Ip4Config2DataTypeManualAddress
].Status
!= EFI_NOT_READY
);
1285 if ((DataSize
!= 0) && ((DataSize
% sizeof (EFI_IP4_CONFIG2_MANUAL_ADDRESS
)) != 0)) {
1286 return EFI_BAD_BUFFER_SIZE
;
1289 if (Instance
->Policy
!= Ip4Config2PolicyStatic
) {
1290 return EFI_WRITE_PROTECTED
;
1293 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeManualAddress
];
1295 if (Data
!= NULL
&& DataSize
!= 0) {
1296 NewAddress
= *((EFI_IP4_CONFIG2_MANUAL_ADDRESS
*) Data
);
1298 StationAddress
= EFI_NTOHL (NewAddress
.Address
);
1299 SubnetMask
= EFI_NTOHL (NewAddress
.SubnetMask
);
1302 // Check whether the StationAddress/SubnetMask pair is valid.
1304 if (!Ip4StationAddressValid (StationAddress
, SubnetMask
)) {
1305 return EFI_INVALID_PARAMETER
;
1309 // Store the new data, and init the DataItem status to EFI_NOT_READY because
1310 // we may have an asynchronous configuration process.
1312 Ptr
= AllocateCopyPool (DataSize
, Data
);
1314 return EFI_OUT_OF_RESOURCES
;
1317 if (DataItem
->Data
.Ptr
!= NULL
) {
1318 FreePool (DataItem
->Data
.Ptr
);
1321 DataItem
->Data
.Ptr
= Ptr
;
1322 DataItem
->DataSize
= DataSize
;
1323 DataItem
->Status
= EFI_NOT_READY
;
1325 IpSb
->Reconfig
= TRUE
;
1326 Status
= Ip4Config2SetDefaultAddr (IpSb
, StationAddress
, SubnetMask
);
1328 DataItem
->Status
= Status
;
1330 if (EFI_ERROR (DataItem
->Status
) && DataItem
->Status
!= EFI_NOT_READY
) {
1334 DataItem
->Data
.Ptr
= NULL
;
1338 // DataSize is 0 and Data is NULL, clean up the manual address.
1340 if (DataItem
->Data
.Ptr
!= NULL
) {
1341 FreePool (DataItem
->Data
.Ptr
);
1343 DataItem
->Data
.Ptr
= NULL
;
1344 DataItem
->DataSize
= 0;
1345 DataItem
->Status
= EFI_NOT_FOUND
;
1348 // Free the default router table and Interface, clean up the assemble table.
1350 if (IpSb
->DefaultInterface
!= NULL
) {
1351 if (IpSb
->DefaultRouteTable
!= NULL
) {
1352 Ip4FreeRouteTable (IpSb
->DefaultRouteTable
);
1353 IpSb
->DefaultRouteTable
= NULL
;
1356 Ip4CancelReceive (IpSb
->DefaultInterface
);
1358 Ip4FreeInterface (IpSb
->DefaultInterface
, NULL
);
1359 IpSb
->DefaultInterface
= NULL
;
1362 Ip4CleanAssembleTable (&IpSb
->Assemble
);
1365 // Create new default interface and route table.
1367 IpIf
= Ip4CreateInterface (IpSb
->Mnp
, IpSb
->Controller
, IpSb
->Image
);
1369 return EFI_OUT_OF_RESOURCES
;
1372 RouteTable
= Ip4CreateRouteTable ();
1373 if (RouteTable
== NULL
) {
1374 Ip4FreeInterface (IpIf
, NULL
);
1375 return EFI_OUT_OF_RESOURCES
;
1378 IpSb
->DefaultInterface
= IpIf
;
1379 InsertHeadList (&IpSb
->Interfaces
, &IpIf
->Link
);
1380 IpSb
->DefaultRouteTable
= RouteTable
;
1381 Ip4ReceiveFrame (IpIf
, NULL
, Ip4AccpetFrame
, IpSb
);
1384 // Reset the State to unstarted.
1386 if (IpSb
->State
== IP4_SERVICE_CONFIGED
|| IpSb
->State
== IP4_SERVICE_STARTED
) {
1387 IpSb
->State
= IP4_SERVICE_UNSTARTED
;
1395 The work function is to set the gateway addresses manually for the EFI IPv4
1396 network stack that is running on the communication device that this EFI IPv4
1397 Configuration Protocol manages. It is not configurable when the policy is
1398 Ip4Config2PolicyDhcp. The gateway addresses must be unicast IPv4 addresses.
1400 @param[in] Instance The pointer to the IP4 config2 instance data.
1401 @param[in] DataSize The size of the buffer pointed to by Data in bytes.
1402 @param[in] Data The data buffer to set. This points to an array of
1403 EFI_IPv6_ADDRESS instances.
1405 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.
1406 @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set
1407 under the current policy.
1408 @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.
1409 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to complete the operation.
1410 @retval EFI_ABORTED The manual gateway addresses to be set equal the
1411 current configuration.
1412 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6
1413 network stack was set.
1417 Ip4Config2SetGateway (
1418 IN IP4_CONFIG2_INSTANCE
*Instance
,
1424 IP4_CONFIG2_DATA_ITEM
*DataItem
;
1429 EFI_IPv4_ADDRESS
*OldGateway
;
1430 EFI_IPv4_ADDRESS
*NewGateway
;
1431 UINTN OldGatewayCount
;
1432 UINTN NewGatewayCount
;
1443 if ((DataSize
!= 0) && (DataSize
% sizeof (EFI_IPv4_ADDRESS
) != 0)) {
1444 return EFI_BAD_BUFFER_SIZE
;
1447 if (Instance
->Policy
!= Ip4Config2PolicyStatic
) {
1448 return EFI_WRITE_PROTECTED
;
1451 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
1453 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeGateway
];
1454 OldGateway
= DataItem
->Data
.Gateway
;
1455 OldGatewayCount
= DataItem
->DataSize
/ sizeof (EFI_IPv4_ADDRESS
);
1457 for (Index1
= 0; Index1
< OldGatewayCount
; Index1
++) {
1459 // Remove the old route entry.
1461 CopyMem (&Gateway
, OldGateway
+ Index1
, sizeof (IP4_ADDR
));
1463 IpSb
->DefaultRouteTable
,
1464 IP4_ALLZERO_ADDRESS
,
1465 IP4_ALLZERO_ADDRESS
,
1471 if (Data
!= NULL
&& DataSize
!= 0) {
1472 NewGateway
= (EFI_IPv4_ADDRESS
*) Data
;
1473 NewGatewayCount
= DataSize
/ sizeof (EFI_IPv4_ADDRESS
);
1474 for (Index1
= 0; Index1
< NewGatewayCount
; Index1
++) {
1475 CopyMem (&Gateway
, NewGateway
+ Index1
, sizeof (IP4_ADDR
));
1477 if ((IpSb
->DefaultInterface
->SubnetMask
!= 0) &&
1478 !NetIp4IsUnicast (NTOHL (Gateway
), IpSb
->DefaultInterface
->SubnetMask
)) {
1479 return EFI_INVALID_PARAMETER
;
1482 for (Index2
= Index1
+ 1; Index2
< NewGatewayCount
; Index2
++) {
1483 if (EFI_IP4_EQUAL (NewGateway
+ Index1
, NewGateway
+ Index2
)) {
1484 return EFI_INVALID_PARAMETER
;
1489 if (NewGatewayCount
!= OldGatewayCount
) {
1490 Tmp
= AllocatePool (DataSize
);
1492 return EFI_OUT_OF_RESOURCES
;
1498 for (Index1
= 0; Index1
< NewGatewayCount
; Index1
++) {
1500 // Add the new route entry.
1502 CopyMem (&Gateway
, NewGateway
+ Index1
, sizeof (IP4_ADDR
));
1504 IpSb
->DefaultRouteTable
,
1505 IP4_ALLZERO_ADDRESS
,
1506 IP4_ALLZERO_ADDRESS
,
1513 if (!OneRemoved
&& !OneAdded
) {
1514 DataItem
->Status
= EFI_SUCCESS
;
1518 if (DataItem
->Data
.Ptr
!= NULL
) {
1519 FreePool (DataItem
->Data
.Ptr
);
1521 DataItem
->Data
.Ptr
= Tmp
;
1524 CopyMem (DataItem
->Data
.Ptr
, Data
, DataSize
);
1525 DataItem
->DataSize
= DataSize
;
1526 DataItem
->Status
= EFI_SUCCESS
;
1530 // DataSize is 0 and Data is NULL, clean up the Gateway address.
1532 if (DataItem
->Data
.Ptr
!= NULL
) {
1533 FreePool (DataItem
->Data
.Ptr
);
1535 DataItem
->Data
.Ptr
= NULL
;
1536 DataItem
->DataSize
= 0;
1537 DataItem
->Status
= EFI_NOT_FOUND
;
1544 The work function is to set the DNS server list for the EFI IPv4 network
1545 stack running on the communication device that this EFI_IP4_CONFIG2_PROTOCOL
1546 manages. It is not configurable when the policy is Ip4Config2PolicyDhcp.
1547 The DNS server addresses must be unicast IPv4 addresses.
1549 @param[in] Instance The pointer to the IP4 config2 instance data.
1550 @param[in] DataSize The size of the buffer pointed to by Data in bytes.
1551 @param[in] Data The data buffer to set, points to an array of
1552 EFI_IPv4_ADDRESS instances.
1554 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.
1555 @retval EFI_WRITE_PROTECTED The specified configuration data cannot be set
1556 under the current policy.
1557 @retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.
1558 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation.
1559 @retval EFI_ABORTED The DNS server addresses to be set equal the current
1561 @retval EFI_SUCCESS The specified configuration data for the EFI IPv4
1562 network stack was set.
1566 Ip4Config2SetDnsServer (
1567 IN IP4_CONFIG2_INSTANCE
*Instance
,
1573 IP4_CONFIG2_DATA_ITEM
*Item
;
1575 Status
= EFI_SUCCESS
;
1578 if (Instance
->Policy
!= Ip4Config2PolicyStatic
) {
1579 return EFI_WRITE_PROTECTED
;
1582 Item
= &Instance
->DataItem
[Ip4Config2DataTypeDnsServer
];
1584 if (DATA_ATTRIB_SET (Item
->Attribute
, DATA_ATTRIB_VOLATILE
)) {
1585 REMOVE_DATA_ATTRIB (Item
->Attribute
, DATA_ATTRIB_VOLATILE
);
1588 if (Data
!= NULL
&& DataSize
!= 0) {
1589 Status
= Ip4Config2SetDnsServerWorker (Instance
, DataSize
, Data
);
1592 // DataSize is 0 and Data is NULL, clean up the DnsServer address.
1594 if (Item
->Data
.Ptr
!= NULL
) {
1595 FreePool (Item
->Data
.Ptr
);
1597 Item
->Data
.Ptr
= NULL
;
1599 Item
->Status
= EFI_NOT_FOUND
;
1606 Generate the operational state of the interface this IP4 config2 instance manages
1607 and output in EFI_IP4_CONFIG2_INTERFACE_INFO.
1609 @param[in] IpSb The pointer to the IP4 service binding instance.
1610 @param[out] IfInfo The pointer to the IP4 config2 interface information structure.
1614 Ip4Config2InitIfInfo (
1615 IN IP4_SERVICE
*IpSb
,
1616 OUT EFI_IP4_CONFIG2_INTERFACE_INFO
*IfInfo
1621 EFI_IP4_CONFIG2_INTERFACE_INFO_NAME_SIZE
,
1623 IpSb
->Ip4Config2Instance
.IfIndex
1626 IfInfo
->IfType
= IpSb
->SnpMode
.IfType
;
1627 IfInfo
->HwAddressSize
= IpSb
->SnpMode
.HwAddressSize
;
1628 CopyMem (&IfInfo
->HwAddress
, &IpSb
->SnpMode
.CurrentAddress
, IfInfo
->HwAddressSize
);
1632 The event handle routine when DHCPv4 process is finished or is updated.
1634 @param[in] Event Not used.
1635 @param[in] Context The pointer to the IP4 configuration instance data.
1640 Ip4Config2OnDhcp4Event (
1650 Set the configuration for the EFI IPv4 network stack running on the communication
1651 device this EFI_IP4_CONFIG2_PROTOCOL instance manages.
1653 This function is used to set the configuration data of type DataType for the EFI
1654 IPv4 network stack that is running on the communication device that this EFI IPv4
1655 Configuration Protocol instance manages.
1657 DataSize is used to calculate the count of structure instances in the Data for
1658 a DataType in which multiple structure instances are allowed.
1660 This function is always non-blocking. When setting some type of configuration data,
1661 an asynchronous process is invoked to check the correctness of the data, such as
1662 performing Duplicate Address Detection on the manually set local IPv4 addresses.
1663 EFI_NOT_READY is returned immediately to indicate that such an asynchronous process
1664 is invoked, and the process is not finished yet. The caller wanting to get the result
1665 of the asynchronous process is required to call RegisterDataNotify() to register an
1666 event on the specified configuration data. Once the event is signaled, the caller
1667 can call GetData() to obtain the configuration data and know the result.
1668 For other types of configuration data that do not require an asynchronous configuration
1669 process, the result of the operation is immediately returned.
1671 @param[in] This The pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.
1672 @param[in] DataType The type of data to set.
1673 @param[in] DataSize Size of the buffer pointed to by Data in bytes.
1674 @param[in] Data The data buffer to set. The type of the data buffer is
1675 associated with the DataType.
1677 @retval EFI_SUCCESS The specified configuration data for the EFI IPv6
1678 network stack was set successfully.
1679 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:
1681 - One or more fields in Data and DataSize do not match the
1682 requirement of the data type indicated by DataType.
1683 @retval EFI_WRITE_PROTECTED The specified configuration data is read-only or the specified
1684 configuration data cannot be set under the current policy.
1685 @retval EFI_ACCESS_DENIED Another set operation on the specified configuration
1686 data is already in process.
1687 @retval EFI_NOT_READY An asynchronous process was invoked to set the specified
1688 configuration data, and the process is not finished yet.
1689 @retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type
1690 indicated by DataType.
1691 @retval EFI_UNSUPPORTED This DataType is not supported.
1692 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
1693 @retval EFI_DEVICE_ERROR An unexpected system error or network error occurred.
1698 EfiIp4Config2SetData (
1699 IN EFI_IP4_CONFIG2_PROTOCOL
*This
,
1700 IN EFI_IP4_CONFIG2_DATA_TYPE DataType
,
1707 IP4_CONFIG2_INSTANCE
*Instance
;
1710 if ((This
== NULL
) || (Data
== NULL
&& DataSize
!= 0) || (Data
!= NULL
&& DataSize
== 0)) {
1711 return EFI_INVALID_PARAMETER
;
1714 if (DataType
>= Ip4Config2DataTypeMaximum
) {
1715 return EFI_UNSUPPORTED
;
1718 Instance
= IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This
);
1719 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
1720 NET_CHECK_SIGNATURE (IpSb
, IP4_SERVICE_SIGNATURE
);
1723 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1725 Status
= Instance
->DataItem
[DataType
].Status
;
1726 if (Status
!= EFI_NOT_READY
) {
1728 if (Instance
->DataItem
[DataType
].SetData
== NULL
) {
1730 // This type of data is readonly.
1732 Status
= EFI_WRITE_PROTECTED
;
1735 Status
= Instance
->DataItem
[DataType
].SetData (Instance
, DataSize
, Data
);
1736 if (!EFI_ERROR (Status
)) {
1738 // Fire up the events registered with this type of data.
1740 NetMapIterate (&Instance
->DataItem
[DataType
].EventMap
, Ip4Config2SignalEvent
, NULL
);
1741 Ip4Config2WriteConfigData (IpSb
->MacString
, Instance
);
1742 } else if (Status
== EFI_ABORTED
) {
1744 // The SetData is aborted because the data to set is the same with
1745 // the one maintained.
1747 Status
= EFI_SUCCESS
;
1748 NetMapIterate (&Instance
->DataItem
[DataType
].EventMap
, Ip4Config2SignalEvent
, NULL
);
1753 // Another asynchornous process is on the way.
1755 Status
= EFI_ACCESS_DENIED
;
1758 gBS
->RestoreTPL (OldTpl
);
1764 Get the configuration data for the EFI IPv4 network stack running on the communication
1765 device that this EFI_IP4_CONFIG2_PROTOCOL instance manages.
1767 This function returns the configuration data of type DataType for the EFI IPv4 network
1768 stack running on the communication device that this EFI IPv4 Configuration Protocol instance
1771 The caller is responsible for allocating the buffer used to return the specified
1772 configuration data. The required size will be returned to the caller if the size of
1773 the buffer is too small.
1775 EFI_NOT_READY is returned if the specified configuration data is not ready due to an
1776 asynchronous configuration process already in progress. The caller can call RegisterDataNotify()
1777 to register an event on the specified configuration data. Once the asynchronous configuration
1778 process is finished, the event will be signaled, and a subsequent GetData() call will return
1779 the specified configuration data.
1781 @param[in] This Pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.
1782 @param[in] DataType The type of data to get.
1783 @param[in, out] DataSize On input, in bytes, the size of Data. On output, in bytes, the
1784 size of buffer required to store the specified configuration data.
1785 @param[in] Data The data buffer in which the configuration data is returned. The
1786 type of the data buffer is associated with the DataType.
1787 This is an optional parameter that may be NULL.
1789 @retval EFI_SUCCESS The specified configuration data was obtained successfully.
1790 @retval EFI_INVALID_PARAMETER One or more of the followings are TRUE:
1793 - Data is NULL if *DataSize is not zero.
1794 @retval EFI_BUFFER_TOO_SMALL The size of Data is too small for the specified configuration data,
1795 and the required size is returned in DataSize.
1796 @retval EFI_NOT_READY The specified configuration data is not ready due to an
1797 asynchronous configuration process already in progress.
1798 @retval EFI_NOT_FOUND The specified configuration data is not found.
1803 EfiIp4Config2GetData (
1804 IN EFI_IP4_CONFIG2_PROTOCOL
*This
,
1805 IN EFI_IP4_CONFIG2_DATA_TYPE DataType
,
1806 IN OUT UINTN
*DataSize
,
1807 IN VOID
*Data OPTIONAL
1812 IP4_CONFIG2_INSTANCE
*Instance
;
1813 IP4_CONFIG2_DATA_ITEM
*DataItem
;
1815 if ((This
== NULL
) || (DataSize
== NULL
) || ((*DataSize
!= 0) && (Data
== NULL
))) {
1816 return EFI_INVALID_PARAMETER
;
1819 if (DataType
>= Ip4Config2DataTypeMaximum
) {
1820 return EFI_NOT_FOUND
;
1823 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1825 Instance
= IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This
);
1826 DataItem
= &Instance
->DataItem
[DataType
];
1828 Status
= Instance
->DataItem
[DataType
].Status
;
1829 if (!EFI_ERROR (Status
)) {
1831 if (DataItem
->GetData
!= NULL
) {
1833 Status
= DataItem
->GetData (Instance
, DataSize
, Data
);
1834 } else if (*DataSize
< Instance
->DataItem
[DataType
].DataSize
) {
1836 // Update the buffer length.
1838 *DataSize
= Instance
->DataItem
[DataType
].DataSize
;
1839 Status
= EFI_BUFFER_TOO_SMALL
;
1842 *DataSize
= Instance
->DataItem
[DataType
].DataSize
;
1843 CopyMem (Data
, Instance
->DataItem
[DataType
].Data
.Ptr
, *DataSize
);
1847 gBS
->RestoreTPL (OldTpl
);
1853 Register an event that is signaled whenever a configuration process on the specified
1854 configuration data is done.
1856 This function registers an event that is to be signaled whenever a configuration
1857 process on the specified configuration data is performed. An event can be registered
1858 for a different DataType simultaneously. The caller is responsible for determining
1859 which type of configuration data causes the signaling of the event in such an event.
1861 @param[in] This Pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.
1862 @param[in] DataType The type of data to unregister the event for.
1863 @param[in] Event The event to register.
1865 @retval EFI_SUCCESS The notification event for the specified configuration data is
1867 @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.
1868 @retval EFI_UNSUPPORTED The configuration data type specified by DataType is not
1870 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
1871 @retval EFI_ACCESS_DENIED The Event is already registered for the DataType.
1876 EfiIp4Config2RegisterDataNotify (
1877 IN EFI_IP4_CONFIG2_PROTOCOL
*This
,
1878 IN EFI_IP4_CONFIG2_DATA_TYPE DataType
,
1884 IP4_CONFIG2_INSTANCE
*Instance
;
1888 if ((This
== NULL
) || (Event
== NULL
)) {
1889 return EFI_INVALID_PARAMETER
;
1892 if (DataType
>= Ip4Config2DataTypeMaximum
) {
1893 return EFI_UNSUPPORTED
;
1896 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1898 Instance
= IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This
);
1899 EventMap
= &Instance
->DataItem
[DataType
].EventMap
;
1902 // Check whether this event is already registered for this DataType.
1904 Item
= NetMapFindKey (EventMap
, Event
);
1907 Status
= NetMapInsertTail (EventMap
, Event
, NULL
);
1909 if (EFI_ERROR (Status
)) {
1911 Status
= EFI_OUT_OF_RESOURCES
;
1916 Status
= EFI_ACCESS_DENIED
;
1919 gBS
->RestoreTPL (OldTpl
);
1925 Remove a previously registered event for the specified configuration data.
1927 @param This The pointer to the EFI_IP4_CONFIG2_PROTOCOL instance.
1928 @param DataType The type of data to remove from the previously
1930 @param Event The event to be unregistered.
1932 @retval EFI_SUCCESS The event registered for the specified
1933 configuration data was removed.
1934 @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.
1935 @retval EFI_NOT_FOUND The Event has not been registered for the
1941 EfiIp4Config2UnregisterDataNotify (
1942 IN EFI_IP4_CONFIG2_PROTOCOL
*This
,
1943 IN EFI_IP4_CONFIG2_DATA_TYPE DataType
,
1949 IP4_CONFIG2_INSTANCE
*Instance
;
1952 if ((This
== NULL
) || (Event
== NULL
)) {
1953 return EFI_INVALID_PARAMETER
;
1956 if (DataType
>= Ip4Config2DataTypeMaximum
) {
1957 return EFI_NOT_FOUND
;
1960 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1962 Instance
= IP4_CONFIG2_INSTANCE_FROM_PROTOCOL (This
);
1964 Item
= NetMapFindKey (&Instance
->DataItem
[DataType
].EventMap
, Event
);
1967 NetMapRemoveItem (&Instance
->DataItem
[DataType
].EventMap
, Item
, NULL
);
1968 Status
= EFI_SUCCESS
;
1971 Status
= EFI_NOT_FOUND
;
1974 gBS
->RestoreTPL (OldTpl
);
1980 Initialize an IP4_CONFIG2_INSTANCE.
1982 @param[out] Instance The buffer of IP4_CONFIG2_INSTANCE to be initialized.
1984 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation.
1985 @retval EFI_SUCCESS The IP4_CONFIG2_INSTANCE initialized successfully.
1989 Ip4Config2InitInstance (
1990 OUT IP4_CONFIG2_INSTANCE
*Instance
1994 IP4_CONFIG2_INSTANCE
*TmpInstance
;
1999 IP4_CONFIG2_DATA_ITEM
*DataItem
;
2002 IpSb
= IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance
);
2004 Instance
->Signature
= IP4_CONFIG2_INSTANCE_SIGNATURE
;
2008 // Determine the index of this interface.
2011 NET_LIST_FOR_EACH (Entry
, &mIp4Config2InstanceList
) {
2012 TmpInstance
= NET_LIST_USER_STRUCT_S (Entry
, IP4_CONFIG2_INSTANCE
, Link
, IP4_CONFIG2_INSTANCE_SIGNATURE
);
2014 if (TmpInstance
->IfIndex
> IfIndex
) {
2016 // There is a sequence hole because some interface is down.
2024 Instance
->IfIndex
= IfIndex
;
2025 NetListInsertBefore (Entry
, &Instance
->Link
);
2027 for (Index
= 0; Index
< Ip4Config2DataTypeMaximum
; Index
++) {
2029 // Initialize the event map for each data item.
2031 NetMapInit (&Instance
->DataItem
[Index
].EventMap
);
2036 // Initialize each data type: associate storage and set data size for the
2037 // fixed size data types, hook the SetData function, set the data attribute.
2039 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeInterfaceInfo
];
2040 DataItem
->GetData
= Ip4Config2GetIfInfo
;
2041 DataItem
->Data
.Ptr
= &Instance
->InterfaceInfo
;
2042 DataItem
->DataSize
= sizeof (Instance
->InterfaceInfo
);
2043 SET_DATA_ATTRIB (DataItem
->Attribute
, DATA_ATTRIB_SIZE_FIXED
| DATA_ATTRIB_VOLATILE
);
2044 Ip4Config2InitIfInfo (IpSb
, &Instance
->InterfaceInfo
);
2046 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypePolicy
];
2047 DataItem
->SetData
= Ip4Config2SetPolicy
;
2048 DataItem
->Data
.Ptr
= &Instance
->Policy
;
2049 DataItem
->DataSize
= sizeof (Instance
->Policy
);
2050 Instance
->Policy
= Ip4Config2PolicyStatic
;
2051 SET_DATA_ATTRIB (DataItem
->Attribute
, DATA_ATTRIB_SIZE_FIXED
);
2053 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeManualAddress
];
2054 DataItem
->SetData
= Ip4Config2SetManualAddress
;
2055 DataItem
->Status
= EFI_NOT_FOUND
;
2057 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeGateway
];
2058 DataItem
->SetData
= Ip4Config2SetGateway
;
2059 DataItem
->Status
= EFI_NOT_FOUND
;
2061 DataItem
= &Instance
->DataItem
[Ip4Config2DataTypeDnsServer
];
2062 DataItem
->SetData
= Ip4Config2SetDnsServer
;
2063 DataItem
->Status
= EFI_NOT_FOUND
;
2065 Instance
->Configured
= TRUE
;
2068 // Try to read the config data from NV variable.
2069 // If not found, write initialized config data into NV variable
2070 // as a default config data.
2072 Status
= Ip4Config2ReadConfigData (IpSb
->MacString
, Instance
);
2073 if (Status
== EFI_NOT_FOUND
) {
2074 Status
= Ip4Config2WriteConfigData (IpSb
->MacString
, Instance
);
2077 if (EFI_ERROR (Status
)) {
2081 Instance
->Ip4Config2
.SetData
= EfiIp4Config2SetData
;
2082 Instance
->Ip4Config2
.GetData
= EfiIp4Config2GetData
;
2083 Instance
->Ip4Config2
.RegisterDataNotify
= EfiIp4Config2RegisterDataNotify
;
2084 Instance
->Ip4Config2
.UnregisterDataNotify
= EfiIp4Config2UnregisterDataNotify
;
2087 // Publish the IP4 configuration form
2089 return Ip4Config2FormInit (Instance
);
2094 Release an IP4_CONFIG2_INSTANCE.
2096 @param[in, out] Instance The buffer of IP4_CONFIG2_INSTANCE to be freed.
2100 Ip4Config2CleanInstance (
2101 IN OUT IP4_CONFIG2_INSTANCE
*Instance
2105 IP4_CONFIG2_DATA_ITEM
*DataItem
;
2107 if (Instance
->DeclineAddress
!= NULL
) {
2108 FreePool (Instance
->DeclineAddress
);
2111 if (!Instance
->Configured
) {
2115 if (Instance
->Dhcp4Handle
!= NULL
) {
2117 Ip4Config2DestroyDhcp4 (Instance
);
2123 if (Instance
->Dhcp4Event
!= NULL
) {
2124 gBS
->CloseEvent (Instance
->Dhcp4Event
);
2125 Instance
->Dhcp4Event
= NULL
;
2128 for (Index
= 0; Index
< Ip4Config2DataTypeMaximum
; Index
++) {
2130 DataItem
= &Instance
->DataItem
[Index
];
2132 if (!DATA_ATTRIB_SET (DataItem
->Attribute
, DATA_ATTRIB_SIZE_FIXED
)) {
2133 if (DataItem
->Data
.Ptr
!= NULL
) {
2134 FreePool (DataItem
->Data
.Ptr
);
2136 DataItem
->Data
.Ptr
= NULL
;
2137 DataItem
->DataSize
= 0;
2140 NetMapClean (&Instance
->DataItem
[Index
].EventMap
);
2143 Ip4Config2FormUnload (Instance
);
2145 RemoveEntryList (&Instance
->Link
);
2149 The event handle for IP4 auto reconfiguration. The original default
2150 interface and route table will be removed as the default.
2152 @param[in] Context The IP4 service binding instance.
2157 Ip4AutoReconfigCallBackDpc (
2163 IpSb
= (IP4_SERVICE
*) Context
;
2164 NET_CHECK_SIGNATURE (IpSb
, IP4_SERVICE_SIGNATURE
);
2166 if (IpSb
->State
> IP4_SERVICE_UNSTARTED
) {
2167 IpSb
->State
= IP4_SERVICE_UNSTARTED
;
2170 IpSb
->Reconfig
= TRUE
;
2172 Ip4StartAutoConfig (&IpSb
->Ip4Config2Instance
);
2179 Request Ip4AutoReconfigCallBackDpc as a DPC at TPL_CALLBACK.
2181 @param Event The event that is signalled.
2182 @param Context The IP4 service binding instance.
2187 Ip4AutoReconfigCallBack (
2193 // Request Ip4AutoReconfigCallBackDpc as a DPC at TPL_CALLBACK
2195 QueueDpc (TPL_CALLBACK
, Ip4AutoReconfigCallBackDpc
, Context
);