2 This code implements the IP4Config and NicIp4Config protocols.
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at<BR>
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "Ip4Config.h"
16 #include "NicIp4Variable.h"
19 // Ip4 Config Protocol
21 GLOBAL_REMOVE_IF_UNREFERENCED EFI_IP4_CONFIG_PROTOCOL mIp4ConfigProtocolTemplate
= {
28 Get the NIC's configure information from the IP4 configure variable.
29 It will remove the invalid variable.
31 @param Instance The IP4 CONFIG instance.
33 @return NULL if no configure for the NIC in the variable, or it is invalid.
34 Otherwise the pointer to the NIC's IP configure parameter will be returned.
38 EfiNicIp4ConfigGetInfo (
39 IN IP4_CONFIG_INSTANCE
*Instance
42 NIC_IP4_CONFIG_INFO
*NicConfig
;
45 // Read the configuration parameter for this NIC from
48 NicConfig
= Ip4ConfigReadVariable (Instance
);
49 if (NicConfig
== NULL
) {
54 // Validate the configuration, if the configuration is invalid,
55 // remove it from the variable.
57 if (!Ip4ConfigIsValid (NicConfig
)) {
58 Ip4ConfigWriteVariable (Instance
, NULL
);
68 Set the IP configure parameters for this NIC.
70 If Reconfig is TRUE, the IP driver will be informed to discard current
71 auto configure parameter and restart the auto configuration process.
72 If current there is a pending auto configuration, EFI_ALREADY_STARTED is
73 returned. You can only change the configure setting when either
74 the configure has finished or not started yet. If NicConfig, the
75 NIC's configure parameter is removed from the variable.
77 @param Instance The IP4 CONFIG instance.
78 @param NicConfig The new NIC IP4 configure parameter.
79 @param Reconfig Inform the IP4 driver to restart the auto
82 @retval EFI_SUCCESS The configure parameter for this NIC was
84 @retval EFI_INVALID_PARAMETER This is NULL or the configure parameter is
86 @retval EFI_ALREADY_STARTED There is a pending auto configuration.
87 @retval EFI_NOT_FOUND No auto configure parameter is found.
92 EfiNicIp4ConfigSetInfo (
93 IN IP4_CONFIG_INSTANCE
*Instance
,
94 IN NIC_IP4_CONFIG_INFO
*NicConfig OPTIONAL
,
101 // Validate the parameters
103 if (Instance
== NULL
) {
104 return EFI_INVALID_PARAMETER
;
107 if ((NicConfig
!= NULL
) && (!Ip4ConfigIsValid (NicConfig
) ||
108 !NIC_ADDR_EQUAL (&NicConfig
->NicAddr
, &Instance
->NicAddr
))) {
109 return EFI_INVALID_PARAMETER
;
112 if (Instance
->State
== IP4_CONFIG_STATE_STARTED
) {
113 return EFI_ALREADY_STARTED
;
117 // Update the parameter in the configure variable
119 Status
= Ip4ConfigWriteVariable (Instance
, NicConfig
);
120 if (EFI_ERROR (Status
)) {
125 // Signal the IP4 to run the auto configuration again
127 if (Reconfig
&& (Instance
->ReconfigEvent
!= NULL
)) {
128 Status
= gBS
->SignalEvent (Instance
->ReconfigEvent
);
136 Callback function when DHCP process finished. It will save the
137 retrieved IP configure parameter from DHCP to the NVRam.
139 @param Event The callback event
140 @param Context Opaque context to the callback
147 Ip4ConfigOnDhcp4Complete (
152 IP4_CONFIG_INSTANCE
*Instance
;
153 EFI_DHCP4_MODE_DATA Dhcp4Mode
;
154 EFI_IP4_IPCONFIG_DATA
*Ip4Config
;
161 Instance
= (IP4_CONFIG_INSTANCE
*) Context
;
162 ASSERT (Instance
->Dhcp4
!= NULL
);
164 Instance
->State
= IP4_CONFIG_STATE_CONFIGURED
;
165 Instance
->Result
= EFI_TIMEOUT
;
168 // Get the DHCP retrieved parameters
170 Status
= Instance
->Dhcp4
->GetModeData (Instance
->Dhcp4
, &Dhcp4Mode
);
172 if (EFI_ERROR (Status
)) {
176 if (Dhcp4Mode
.State
== Dhcp4Bound
) {
178 // Save the new configuration retrieved by DHCP both in
179 // the instance and to NVRam. So, both the IP4 driver and
180 // other user can get that address.
184 if (Instance
->NicConfig
!= NULL
) {
185 ASSERT (Instance
->NicConfig
->Source
== IP4_CONFIG_SOURCE_DHCP
);
186 Perment
= Instance
->NicConfig
->Perment
;
187 FreePool (Instance
->NicConfig
);
190 Instance
->NicConfig
= AllocatePool (sizeof (NIC_IP4_CONFIG_INFO
) + 2* sizeof (EFI_IP4_ROUTE_TABLE
));
192 if (Instance
->NicConfig
== NULL
) {
193 Instance
->Result
= EFI_OUT_OF_RESOURCES
;
197 Instance
->NicConfig
->Ip4Info
.RouteTable
= (EFI_IP4_ROUTE_TABLE
*) (Instance
->NicConfig
+ 1);
199 CopyMem (&Instance
->NicConfig
->NicAddr
, &Instance
->NicAddr
, sizeof (Instance
->NicConfig
->NicAddr
));
200 Instance
->NicConfig
->Source
= IP4_CONFIG_SOURCE_DHCP
;
201 Instance
->NicConfig
->Perment
= Perment
;
203 Ip4Config
= &Instance
->NicConfig
->Ip4Info
;
204 Ip4Config
->StationAddress
= Dhcp4Mode
.ClientAddress
;
205 Ip4Config
->SubnetMask
= Dhcp4Mode
.SubnetMask
;
208 // Create a route for the connected network
210 Ip4Config
->RouteTableSize
= 1;
212 CopyMem (&Ip1
, &Dhcp4Mode
.ClientAddress
, sizeof (IP4_ADDR
));
213 CopyMem (&Ip2
, &Dhcp4Mode
.SubnetMask
, sizeof (IP4_ADDR
));
217 CopyMem (&Ip4Config
->RouteTable
[0].SubnetAddress
, &Subnet
, sizeof (EFI_IPv4_ADDRESS
));
218 CopyMem (&Ip4Config
->RouteTable
[0].SubnetMask
, &Dhcp4Mode
.SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
219 ZeroMem (&Ip4Config
->RouteTable
[0].GatewayAddress
, sizeof (EFI_IPv4_ADDRESS
));
222 // Create a route if there is a default router.
224 if (!EFI_IP4_EQUAL (&Dhcp4Mode
.RouterAddress
, &mZeroIp4Addr
)) {
225 Ip4Config
->RouteTableSize
= 2;
227 ZeroMem (&Ip4Config
->RouteTable
[1].SubnetAddress
, sizeof (EFI_IPv4_ADDRESS
));
228 ZeroMem (&Ip4Config
->RouteTable
[1].SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
229 CopyMem (&Ip4Config
->RouteTable
[1].GatewayAddress
, &Dhcp4Mode
.RouterAddress
, sizeof (EFI_IPv4_ADDRESS
));
232 Instance
->Result
= EFI_SUCCESS
;
235 // ignore the return status of EfiNicIp4ConfigSetInfo. Network
236 // stack can operate even that failed.
238 EfiNicIp4ConfigSetInfo (Instance
, Instance
->NicConfig
, FALSE
);
242 gBS
->SignalEvent (Instance
->DoneEvent
);
243 Ip4ConfigCleanDhcp4 (Instance
);
251 Starts running the configuration policy for the EFI IPv4 Protocol driver.
253 The Start() function is called to determine and to begin the platform
254 configuration policy by the EFI IPv4 Protocol driver. This determination may
255 be as simple as returning EFI_UNSUPPORTED if there is no EFI IPv4 Protocol
256 driver configuration policy. It may be as involved as loading some defaults
257 from nonvolatile storage, downloading dynamic data from a DHCP server, and
258 checking permissions with a site policy server.
259 Starting the configuration policy is just the beginning. It may finish almost
260 instantly or it may take several minutes before it fails to retrieve configuration
261 information from one or more servers. Once the policy is started, drivers
262 should use the DoneEvent parameter to determine when the configuration policy
263 has completed. EFI_IP4_CONFIG_PROTOCOL.GetData() must then be called to
264 determine if the configuration succeeded or failed.
265 Until the configuration completes successfully, EFI IPv4 Protocol driver instances
266 that are attempting to use default configurations must return EFI_NO_MAPPING.
267 Once the configuration is complete, the EFI IPv4 Configuration Protocol driver
268 signals DoneEvent. The configuration may need to be updated in the future,
269 however; in this case, the EFI IPv4 Configuration Protocol driver must signal
270 ReconfigEvent, and all EFI IPv4 Protocol driver instances that are using default
271 configurations must return EFI_NO_MAPPING until the configuration policy has
274 @param This Pointer to the EFI_IP4_CONFIG_PROTOCOL instance.
275 @param DoneEvent Event that will be signaled when the EFI IPv4
276 Protocol driver configuration policy completes
277 execution. This event must be of type EVT_NOTIFY_SIGNAL.
278 @param ReconfigEvent Event that will be signaled when the EFI IPv4
279 Protocol driver configuration needs to be updated.
280 This event must be of type EVT_NOTIFY_SIGNAL.
282 @retval EFI_SUCCESS The configuration policy for the EFI IPv4 Protocol
283 driver is now running.
284 @retval EFI_INVALID_PARAMETER One or more of the following parameters is NULL:
288 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
289 @retval EFI_ALREADY_STARTED The configuration policy for the EFI IPv4 Protocol
290 driver was already started.
291 @retval EFI_DEVICE_ERROR An unexpected system error or network error occurred.
292 @retval EFI_UNSUPPORTED This interface does not support the EFI IPv4 Protocol
293 driver configuration.
299 IN EFI_IP4_CONFIG_PROTOCOL
*This
,
300 IN EFI_EVENT DoneEvent
,
301 IN EFI_EVENT ReconfigEvent
304 IP4_CONFIG_INSTANCE
*Instance
;
305 EFI_DHCP4_PROTOCOL
*Dhcp4
;
306 EFI_DHCP4_MODE_DATA Dhcp4Mode
;
307 EFI_DHCP4_PACKET_OPTION
*OptionList
[1];
308 IP4_CONFIG_DHCP4_OPTION ParaList
;
313 if ((This
== NULL
) || (DoneEvent
== NULL
) || (ReconfigEvent
== NULL
)) {
314 return EFI_INVALID_PARAMETER
;
317 Instance
= IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (This
);
319 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
321 if (Instance
->State
!= IP4_CONFIG_STATE_IDLE
) {
322 Status
= EFI_ALREADY_STARTED
;
327 Instance
->DoneEvent
= DoneEvent
;
328 Instance
->ReconfigEvent
= ReconfigEvent
;
330 Instance
->NicConfig
= EfiNicIp4ConfigGetInfo (Instance
);
332 if (Instance
->NicConfig
== NULL
) {
333 Source
= IP4_CONFIG_SOURCE_DHCP
;
335 Source
= Instance
->NicConfig
->Source
;
339 // If the source is static, the auto configuration is done.
342 if (Source
== IP4_CONFIG_SOURCE_STATIC
) {
343 Instance
->State
= IP4_CONFIG_STATE_CONFIGURED
;
344 Instance
->Result
= EFI_SUCCESS
;
346 gBS
->SignalEvent (Instance
->DoneEvent
);
347 Status
= EFI_SUCCESS
;
352 // Start the dhcp process
354 ASSERT ((Source
== IP4_CONFIG_SOURCE_DHCP
) && (Instance
->Dhcp4
== NULL
));
356 Status
= NetLibCreateServiceChild (
357 Instance
->Controller
,
359 &gEfiDhcp4ServiceBindingProtocolGuid
,
360 &Instance
->Dhcp4Handle
363 if (EFI_ERROR (Status
)) {
367 Status
= gBS
->OpenProtocol (
368 Instance
->Dhcp4Handle
,
369 &gEfiDhcp4ProtocolGuid
,
370 (VOID
**) &Instance
->Dhcp4
,
372 Instance
->Controller
,
373 EFI_OPEN_PROTOCOL_BY_DRIVER
376 if (EFI_ERROR (Status
)) {
381 // Check the current DHCP status, if the DHCP process has
382 // already finished, return now.
384 Dhcp4
= Instance
->Dhcp4
;
385 Status
= Dhcp4
->GetModeData (Dhcp4
, &Dhcp4Mode
);
387 if (EFI_ERROR (Status
)) {
391 if (Dhcp4Mode
.State
== Dhcp4Bound
) {
392 Ip4ConfigOnDhcp4Complete (NULL
, Instance
);
398 // Try to start the DHCP process. Use most of the current
399 // DHCP configuration to avoid problems if some DHCP client
400 // yields the control of this DHCP service to us.
402 ParaList
.Head
.OpCode
= DHCP_TAG_PARA_LIST
;
403 ParaList
.Head
.Length
= 2;
404 ParaList
.Head
.Data
[0] = DHCP_TAG_NETMASK
;
405 ParaList
.Route
= DHCP_TAG_ROUTER
;
406 OptionList
[0] = &ParaList
.Head
;
407 Dhcp4Mode
.ConfigData
.OptionCount
= 1;
408 Dhcp4Mode
.ConfigData
.OptionList
= OptionList
;
410 Status
= Dhcp4
->Configure (Dhcp4
, &Dhcp4Mode
.ConfigData
);
412 if (EFI_ERROR (Status
)) {
417 // Start the DHCP process
419 Status
= gBS
->CreateEvent (
422 Ip4ConfigOnDhcp4Complete
,
424 &Instance
->Dhcp4Event
427 if (EFI_ERROR (Status
)) {
431 Status
= Dhcp4
->Start (Dhcp4
, Instance
->Dhcp4Event
);
433 if (EFI_ERROR (Status
)) {
437 Instance
->State
= IP4_CONFIG_STATE_STARTED
;
438 Instance
->Result
= EFI_NOT_READY
;
441 if (EFI_ERROR (Status
)) {
442 Ip4ConfigCleanConfig (Instance
);
446 gBS
->RestoreTPL (OldTpl
);
455 Stops running the configuration policy for the EFI IPv4 Protocol driver.
457 The Stop() function stops the configuration policy for the EFI IPv4 Protocol driver.
458 All configuration data will be lost after calling Stop().
460 @param This Pointer to the EFI_IP4_CONFIG_PROTOCOL instance.
462 @retval EFI_SUCCESS The configuration policy for the EFI IPv4 Protocol
463 driver has been stopped.
464 @retval EFI_INVALID_PARAMETER This is NULL.
465 @retval EFI_NOT_STARTED The configuration policy for the EFI IPv4 Protocol
466 driver was not started.
472 IN EFI_IP4_CONFIG_PROTOCOL
*This
475 IP4_CONFIG_INSTANCE
*Instance
;
480 return EFI_INVALID_PARAMETER
;
483 Instance
= IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (This
);
485 Status
= EFI_SUCCESS
;
486 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
488 if (Instance
->State
== IP4_CONFIG_STATE_IDLE
) {
489 Status
= EFI_NOT_STARTED
;
494 // Release all the configure parameters. Don't signal the user
495 // event. The user wants to abort the configuration, this isn't
496 // the configuration done or reconfiguration.
498 Ip4ConfigCleanConfig (Instance
);
501 gBS
->RestoreTPL (OldTpl
);
508 Returns the default configuration data (if any) for the EFI IPv4 Protocol driver.
510 The GetData() function returns the current configuration data for the EFI IPv4
511 Protocol driver after the configuration policy has completed.
513 @param This Pointer to the EFI_IP4_CONFIG_PROTOCOL instance.
514 @param ConfigDataSize On input, the size of the ConfigData buffer.
515 On output, the count of bytes that were written
516 into the ConfigData buffer.
517 @param ConfigData Pointer to the EFI IPv4 Configuration Protocol
518 driver configuration data structure.
519 Type EFI_IP4_IPCONFIG_DATA is defined in
520 "Related Definitions" below.
522 @retval EFI_SUCCESS The EFI IPv4 Protocol driver configuration has been returned.
523 @retval EFI_INVALID_PARAMETER This is NULL.
524 @retval EFI_NOT_STARTED The configuration policy for the EFI IPv4 Protocol
525 driver is not running.
526 @retval EFI_NOT_READY EFI IPv4 Protocol driver configuration is still running.
527 @retval EFI_ABORTED EFI IPv4 Protocol driver configuration could not complete.
528 Currently not implemented.
529 @retval EFI_BUFFER_TOO_SMALL *ConfigDataSize is smaller than the configuration
530 data buffer or ConfigData is NULL.
535 EfiIp4ConfigGetData (
536 IN EFI_IP4_CONFIG_PROTOCOL
*This
,
537 IN OUT UINTN
*ConfigDataSize
,
538 OUT EFI_IP4_IPCONFIG_DATA
*ConfigData OPTIONAL
541 IP4_CONFIG_INSTANCE
*Instance
;
542 NIC_IP4_CONFIG_INFO
*NicConfig
;
547 if ((This
== NULL
) || (ConfigDataSize
== NULL
)) {
548 return EFI_INVALID_PARAMETER
;
551 Instance
= IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (This
);
553 Status
= EFI_SUCCESS
;
554 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
556 if (Instance
->State
== IP4_CONFIG_STATE_IDLE
) {
557 Status
= EFI_NOT_STARTED
;
558 } else if (Instance
->State
== IP4_CONFIG_STATE_STARTED
) {
559 Status
= EFI_NOT_READY
;
562 if (EFI_ERROR (Status
)) {
567 // Copy the configure data if auto configuration succeeds.
569 Status
= Instance
->Result
;
571 if (Status
== EFI_SUCCESS
) {
572 ASSERT (Instance
->NicConfig
!= NULL
);
574 NicConfig
= Instance
->NicConfig
;
575 Len
= SIZEOF_IP4_CONFIG_INFO (&NicConfig
->Ip4Info
);
577 if ((*ConfigDataSize
< Len
) || (ConfigData
== NULL
)) {
578 Status
= EFI_BUFFER_TOO_SMALL
;
580 CopyMem (ConfigData
, &NicConfig
->Ip4Info
, Len
);
581 Ip4ConfigFixRouteTablePointer (ConfigData
);
584 *ConfigDataSize
= Len
;
588 gBS
->RestoreTPL (OldTpl
);
594 Release all the DHCP related resources.
596 @param This The IP4 configure instance
602 Ip4ConfigCleanDhcp4 (
603 IN IP4_CONFIG_INSTANCE
*This
606 if (This
->Dhcp4
!= NULL
) {
607 This
->Dhcp4
->Stop (This
->Dhcp4
);
611 &gEfiDhcp4ProtocolGuid
,
619 if (This
->Dhcp4Handle
!= NULL
) {
620 NetLibDestroyServiceChild (
623 &gEfiDhcp4ServiceBindingProtocolGuid
,
627 This
->Dhcp4Handle
= NULL
;
630 if (This
->Dhcp4Event
== NULL
) {
631 gBS
->CloseEvent (This
->Dhcp4Event
);
632 This
->Dhcp4Event
= NULL
;
638 Clean up all the configuration parameters.
640 @param Instance The IP4 configure instance
646 Ip4ConfigCleanConfig (
647 IN IP4_CONFIG_INSTANCE
*Instance
650 if (Instance
->NicConfig
!= NULL
) {
651 FreePool (Instance
->NicConfig
);
652 Instance
->NicConfig
= NULL
;
655 Instance
->State
= IP4_CONFIG_STATE_IDLE
;
656 Instance
->DoneEvent
= NULL
;
657 Instance
->ReconfigEvent
= NULL
;
659 Ip4ConfigCleanDhcp4 (Instance
);