]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4ConfigDxe/Ip4Config.c
2c74a80308827bb246652ec1e9ff5653177275c9
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4ConfigDxe / Ip4Config.c
1 /** @file
2 This code implements the IP4Config and NicIp4Config protocols.
3
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
9
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.
12
13 **/
14
15 #include "Ip4Config.h"
16 #include "NicIp4Variable.h"
17
18 //
19 // Ip4 Config Protocol
20 //
21 GLOBAL_REMOVE_IF_UNREFERENCED EFI_IP4_CONFIG_PROTOCOL mIp4ConfigProtocolTemplate = {
22 EfiIp4ConfigStart,
23 EfiIp4ConfigStop,
24 EfiIp4ConfigGetData
25 };
26
27 /**
28 Get the NIC's configure information from the IP4 configure variable.
29 It will remove the invalid variable.
30
31 @param Instance The IP4 CONFIG instance.
32
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.
35
36 **/
37 NIC_IP4_CONFIG_INFO *
38 EfiNicIp4ConfigGetInfo (
39 IN IP4_CONFIG_INSTANCE *Instance
40 )
41 {
42 NIC_IP4_CONFIG_INFO *NicConfig;
43
44 //
45 // Read the configuration parameter for this NIC from
46 // the EFI variable
47 //
48 NicConfig = Ip4ConfigReadVariable (Instance);
49 if (NicConfig == NULL) {
50 return NULL;
51 }
52
53 //
54 // Validate the configuration, if the configuration is invalid,
55 // remove it from the variable.
56 //
57 if (!Ip4ConfigIsValid (NicConfig)) {
58 Ip4ConfigWriteVariable (Instance, NULL);
59
60 FreePool (NicConfig);
61 NicConfig = NULL;
62 }
63
64 return NicConfig;
65 }
66
67 /**
68 Set the IP configure parameters for this NIC.
69
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.
76
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
80 configuration.
81
82 @retval EFI_SUCCESS The configure parameter for this NIC was
83 set successfully.
84 @retval EFI_INVALID_PARAMETER This is NULL or the configure parameter is
85 invalid.
86 @retval EFI_ALREADY_STARTED There is a pending auto configuration.
87 @retval EFI_NOT_FOUND No auto configure parameter is found.
88
89 **/
90 EFI_STATUS
91 EFIAPI
92 EfiNicIp4ConfigSetInfo (
93 IN IP4_CONFIG_INSTANCE *Instance,
94 IN NIC_IP4_CONFIG_INFO *NicConfig OPTIONAL,
95 IN BOOLEAN Reconfig
96 )
97 {
98 EFI_STATUS Status;
99
100 //
101 // Validate the parameters
102 //
103 if (Instance == NULL) {
104 return EFI_INVALID_PARAMETER;
105 }
106
107 if ((NicConfig != NULL) && (!Ip4ConfigIsValid (NicConfig) ||
108 !NIC_ADDR_EQUAL (&NicConfig->NicAddr, &Instance->NicAddr))) {
109 return EFI_INVALID_PARAMETER;
110 }
111
112 if (Instance->State == IP4_CONFIG_STATE_STARTED) {
113 return EFI_ALREADY_STARTED;
114 }
115
116 //
117 // Update the parameter in the configure variable
118 //
119 Status = Ip4ConfigWriteVariable (Instance, NicConfig);
120 if (EFI_ERROR (Status)) {
121 return Status;
122 }
123
124 //
125 // Signal the IP4 to run the auto configuration again
126 //
127 if (Reconfig && (Instance->ReconfigEvent != NULL)) {
128 Status = gBS->SignalEvent (Instance->ReconfigEvent);
129 DispatchDpc ();
130 }
131
132 return Status;
133 }
134
135 /**
136 Callback function when DHCP process finished. It will save the
137 retrieved IP configure parameter from DHCP to the NVRam.
138
139 @param Event The callback event
140 @param Context Opaque context to the callback
141
142 @return None
143
144 **/
145 VOID
146 EFIAPI
147 Ip4ConfigOnDhcp4Complete (
148 IN EFI_EVENT Event,
149 IN VOID *Context
150 )
151 {
152 IP4_CONFIG_INSTANCE *Instance;
153 EFI_DHCP4_MODE_DATA Dhcp4Mode;
154 EFI_IP4_IPCONFIG_DATA *Ip4Config;
155 EFI_STATUS Status;
156 BOOLEAN Perment;
157 IP4_ADDR Subnet;
158 IP4_ADDR Ip1;
159 IP4_ADDR Ip2;
160
161 Instance = (IP4_CONFIG_INSTANCE *) Context;
162 ASSERT (Instance->Dhcp4 != NULL);
163
164 Instance->State = IP4_CONFIG_STATE_CONFIGURED;
165 Instance->Result = EFI_TIMEOUT;
166
167 //
168 // Get the DHCP retrieved parameters
169 //
170 Status = Instance->Dhcp4->GetModeData (Instance->Dhcp4, &Dhcp4Mode);
171
172 if (EFI_ERROR (Status)) {
173 goto ON_EXIT;
174 }
175
176 if (Dhcp4Mode.State == Dhcp4Bound) {
177 //
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.
181 //
182 Perment = FALSE;
183
184 if (Instance->NicConfig != NULL) {
185 ASSERT (Instance->NicConfig->Source == IP4_CONFIG_SOURCE_DHCP);
186 Perment = Instance->NicConfig->Perment;
187 FreePool (Instance->NicConfig);
188 }
189
190 Instance->NicConfig = AllocatePool (sizeof (NIC_IP4_CONFIG_INFO) + 2* sizeof (EFI_IP4_ROUTE_TABLE));
191
192 if (Instance->NicConfig == NULL) {
193 Instance->Result = EFI_OUT_OF_RESOURCES;
194 goto ON_EXIT;
195 }
196
197 Instance->NicConfig->Ip4Info.RouteTable = (EFI_IP4_ROUTE_TABLE *) (Instance->NicConfig + 1);
198
199 CopyMem (&Instance->NicConfig->NicAddr, &Instance->NicAddr, sizeof (Instance->NicConfig->NicAddr));
200 Instance->NicConfig->Source = IP4_CONFIG_SOURCE_DHCP;
201 Instance->NicConfig->Perment = Perment;
202
203 Ip4Config = &Instance->NicConfig->Ip4Info;
204 Ip4Config->StationAddress = Dhcp4Mode.ClientAddress;
205 Ip4Config->SubnetMask = Dhcp4Mode.SubnetMask;
206
207 //
208 // Create a route for the connected network
209 //
210 Ip4Config->RouteTableSize = 1;
211
212 CopyMem (&Ip1, &Dhcp4Mode.ClientAddress, sizeof (IP4_ADDR));
213 CopyMem (&Ip2, &Dhcp4Mode.SubnetMask, sizeof (IP4_ADDR));
214
215 Subnet = Ip1 & Ip2;
216
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));
220
221 //
222 // Create a route if there is a default router.
223 //
224 if (!EFI_IP4_EQUAL (&Dhcp4Mode.RouterAddress, &mZeroIp4Addr)) {
225 Ip4Config->RouteTableSize = 2;
226
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));
230 }
231
232 Instance->Result = EFI_SUCCESS;
233
234 //
235 // ignore the return status of EfiNicIp4ConfigSetInfo. Network
236 // stack can operate even that failed.
237 //
238 EfiNicIp4ConfigSetInfo (Instance, Instance->NicConfig, FALSE);
239 }
240
241 ON_EXIT:
242 gBS->SignalEvent (Instance->DoneEvent);
243 Ip4ConfigCleanDhcp4 (Instance);
244
245 DispatchDpc ();
246
247 return ;
248 }
249
250 /**
251 Starts running the configuration policy for the EFI IPv4 Protocol driver.
252
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
272 been rerun.
273
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.
281
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:
285 This
286 DoneEvent
287 ReconfigEvent
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.
294
295 **/
296 EFI_STATUS
297 EFIAPI
298 EfiIp4ConfigStart (
299 IN EFI_IP4_CONFIG_PROTOCOL *This,
300 IN EFI_EVENT DoneEvent,
301 IN EFI_EVENT ReconfigEvent
302 )
303 {
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;
309 EFI_STATUS Status;
310 UINT32 Source;
311 EFI_TPL OldTpl;
312
313 if ((This == NULL) || (DoneEvent == NULL) || (ReconfigEvent == NULL)) {
314 return EFI_INVALID_PARAMETER;
315 }
316
317 Instance = IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (This);
318
319 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
320
321 if (Instance->State != IP4_CONFIG_STATE_IDLE) {
322 Status = EFI_ALREADY_STARTED;
323
324 goto ON_EXIT;
325 }
326
327 Instance->DoneEvent = DoneEvent;
328 Instance->ReconfigEvent = ReconfigEvent;
329
330 Instance->NicConfig = EfiNicIp4ConfigGetInfo (Instance);
331
332 if (Instance->NicConfig == NULL) {
333 Source = IP4_CONFIG_SOURCE_DHCP;
334 } else {
335 Source = Instance->NicConfig->Source;
336 }
337
338 //
339 // If the source is static, the auto configuration is done.
340 // return now.
341 //
342 if (Source == IP4_CONFIG_SOURCE_STATIC) {
343 Instance->State = IP4_CONFIG_STATE_CONFIGURED;
344 Instance->Result = EFI_SUCCESS;
345
346 gBS->SignalEvent (Instance->DoneEvent);
347 Status = EFI_SUCCESS;
348 goto ON_EXIT;
349 }
350
351 //
352 // Start the dhcp process
353 //
354 ASSERT ((Source == IP4_CONFIG_SOURCE_DHCP) && (Instance->Dhcp4 == NULL));
355
356 Status = NetLibCreateServiceChild (
357 Instance->Controller,
358 Instance->Image,
359 &gEfiDhcp4ServiceBindingProtocolGuid,
360 &Instance->Dhcp4Handle
361 );
362
363 if (EFI_ERROR (Status)) {
364 goto ON_ERROR;
365 }
366
367 Status = gBS->OpenProtocol (
368 Instance->Dhcp4Handle,
369 &gEfiDhcp4ProtocolGuid,
370 (VOID **) &Instance->Dhcp4,
371 Instance->Image,
372 Instance->Controller,
373 EFI_OPEN_PROTOCOL_BY_DRIVER
374 );
375
376 if (EFI_ERROR (Status)) {
377 goto ON_ERROR;
378 }
379
380 //
381 // Check the current DHCP status, if the DHCP process has
382 // already finished, return now.
383 //
384 Dhcp4 = Instance->Dhcp4;
385 Status = Dhcp4->GetModeData (Dhcp4, &Dhcp4Mode);
386
387 if (EFI_ERROR (Status)) {
388 goto ON_ERROR;
389 }
390
391 if (Dhcp4Mode.State == Dhcp4Bound) {
392 Ip4ConfigOnDhcp4Complete (NULL, Instance);
393
394 goto ON_EXIT;
395 }
396
397 //
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.
401 //
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;
409
410 Status = Dhcp4->Configure (Dhcp4, &Dhcp4Mode.ConfigData);
411
412 if (EFI_ERROR (Status)) {
413 goto ON_ERROR;
414 }
415
416 //
417 // Start the DHCP process
418 //
419 Status = gBS->CreateEvent (
420 EVT_NOTIFY_SIGNAL,
421 TPL_CALLBACK,
422 Ip4ConfigOnDhcp4Complete,
423 Instance,
424 &Instance->Dhcp4Event
425 );
426
427 if (EFI_ERROR (Status)) {
428 goto ON_ERROR;
429 }
430
431 Status = Dhcp4->Start (Dhcp4, Instance->Dhcp4Event);
432
433 if (EFI_ERROR (Status)) {
434 goto ON_ERROR;
435 }
436
437 Instance->State = IP4_CONFIG_STATE_STARTED;
438 Instance->Result = EFI_NOT_READY;
439
440 ON_ERROR:
441 if (EFI_ERROR (Status)) {
442 Ip4ConfigCleanConfig (Instance);
443 }
444
445 ON_EXIT:
446 gBS->RestoreTPL (OldTpl);
447
448 DispatchDpc ();
449
450 return Status;
451 }
452
453
454 /**
455 Stops running the configuration policy for the EFI IPv4 Protocol driver.
456
457 The Stop() function stops the configuration policy for the EFI IPv4 Protocol driver.
458 All configuration data will be lost after calling Stop().
459
460 @param This Pointer to the EFI_IP4_CONFIG_PROTOCOL instance.
461
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.
467
468 **/
469 EFI_STATUS
470 EFIAPI
471 EfiIp4ConfigStop (
472 IN EFI_IP4_CONFIG_PROTOCOL *This
473 )
474 {
475 IP4_CONFIG_INSTANCE *Instance;
476 EFI_STATUS Status;
477 EFI_TPL OldTpl;
478
479 if (This == NULL) {
480 return EFI_INVALID_PARAMETER;
481 }
482
483 Instance = IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (This);
484
485 Status = EFI_SUCCESS;
486 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
487
488 if (Instance->State == IP4_CONFIG_STATE_IDLE) {
489 Status = EFI_NOT_STARTED;
490 goto ON_EXIT;
491 }
492
493 //
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.
497 //
498 Ip4ConfigCleanConfig (Instance);
499
500 ON_EXIT:
501 gBS->RestoreTPL (OldTpl);
502
503 return Status;
504 }
505
506
507 /**
508 Returns the default configuration data (if any) for the EFI IPv4 Protocol driver.
509
510 The GetData() function returns the current configuration data for the EFI IPv4
511 Protocol driver after the configuration policy has completed.
512
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.
521
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.
531
532 **/
533 EFI_STATUS
534 EFIAPI
535 EfiIp4ConfigGetData (
536 IN EFI_IP4_CONFIG_PROTOCOL *This,
537 IN OUT UINTN *ConfigDataSize,
538 OUT EFI_IP4_IPCONFIG_DATA *ConfigData OPTIONAL
539 )
540 {
541 IP4_CONFIG_INSTANCE *Instance;
542 NIC_IP4_CONFIG_INFO *NicConfig;
543 EFI_STATUS Status;
544 EFI_TPL OldTpl;
545 UINTN Len;
546
547 if ((This == NULL) || (ConfigDataSize == NULL)) {
548 return EFI_INVALID_PARAMETER;
549 }
550
551 Instance = IP4_CONFIG_INSTANCE_FROM_IP4CONFIG (This);
552
553 Status = EFI_SUCCESS;
554 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
555
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;
560 }
561
562 if (EFI_ERROR (Status)) {
563 goto ON_EXIT;
564 }
565
566 //
567 // Copy the configure data if auto configuration succeeds.
568 //
569 Status = Instance->Result;
570
571 if (Status == EFI_SUCCESS) {
572 ASSERT (Instance->NicConfig != NULL);
573
574 NicConfig = Instance->NicConfig;
575 Len = SIZEOF_IP4_CONFIG_INFO (&NicConfig->Ip4Info);
576
577 if ((*ConfigDataSize < Len) || (ConfigData == NULL)) {
578 Status = EFI_BUFFER_TOO_SMALL;
579 } else {
580 CopyMem (ConfigData, &NicConfig->Ip4Info, Len);
581 Ip4ConfigFixRouteTablePointer (ConfigData);
582 }
583
584 *ConfigDataSize = Len;
585 }
586
587 ON_EXIT:
588 gBS->RestoreTPL (OldTpl);
589
590 return Status;
591 }
592
593 /**
594 Release all the DHCP related resources.
595
596 @param This The IP4 configure instance
597
598 @return None
599
600 **/
601 VOID
602 Ip4ConfigCleanDhcp4 (
603 IN IP4_CONFIG_INSTANCE *This
604 )
605 {
606 if (This->Dhcp4 != NULL) {
607 This->Dhcp4->Stop (This->Dhcp4);
608
609 gBS->CloseProtocol (
610 This->Dhcp4Handle,
611 &gEfiDhcp4ProtocolGuid,
612 This->Image,
613 This->Controller
614 );
615
616 This->Dhcp4 = NULL;
617 }
618
619 if (This->Dhcp4Handle != NULL) {
620 NetLibDestroyServiceChild (
621 This->Controller,
622 This->Image,
623 &gEfiDhcp4ServiceBindingProtocolGuid,
624 This->Dhcp4Handle
625 );
626
627 This->Dhcp4Handle = NULL;
628 }
629
630 if (This->Dhcp4Event == NULL) {
631 gBS->CloseEvent (This->Dhcp4Event);
632 This->Dhcp4Event = NULL;
633 }
634 }
635
636
637 /**
638 Clean up all the configuration parameters.
639
640 @param Instance The IP4 configure instance
641
642 @return None
643
644 **/
645 VOID
646 Ip4ConfigCleanConfig (
647 IN IP4_CONFIG_INSTANCE *Instance
648 )
649 {
650 if (Instance->NicConfig != NULL) {
651 FreePool (Instance->NicConfig);
652 Instance->NicConfig = NULL;
653 }
654
655 Instance->State = IP4_CONFIG_STATE_IDLE;
656 Instance->DoneEvent = NULL;
657 Instance->ReconfigEvent = NULL;
658
659 Ip4ConfigCleanDhcp4 (Instance);
660 }
661