]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Driver.c
73b4e246e0dc66b50f4bdb4502d6b786d525b87d
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Driver.c
1 /** @file
2 The driver binding and service binding protocol for IP4 driver.
3
4 Copyright (c) 2005 - 2013, 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
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 "Ip4Impl.h"
16
17 EFI_DRIVER_BINDING_PROTOCOL gIp4DriverBinding = {
18 Ip4DriverBindingSupported,
19 Ip4DriverBindingStart,
20 Ip4DriverBindingStop,
21 0xa,
22 NULL,
23 NULL
24 };
25
26 /**
27 This is the declaration of an EFI image entry point. This entry point is
28 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
29 both device drivers and bus drivers.
30
31 The entry point for IP4 driver which install the driver
32 binding and component name protocol on its image.
33
34 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
35 @param[in] SystemTable A pointer to the EFI System Table.
36
37 @retval EFI_SUCCESS The operation completed successfully.
38 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
39
40 **/
41 EFI_STATUS
42 EFIAPI
43 Ip4DriverEntryPoint (
44 IN EFI_HANDLE ImageHandle,
45 IN EFI_SYSTEM_TABLE *SystemTable
46 )
47 {
48 return EfiLibInstallDriverBindingComponentName2 (
49 ImageHandle,
50 SystemTable,
51 &gIp4DriverBinding,
52 ImageHandle,
53 &gIp4ComponentName,
54 &gIp4ComponentName2
55 );
56 }
57
58 /**
59 Test to see if this driver supports ControllerHandle. This service
60 is called by the EFI boot service ConnectController(). In
61 order to make drivers as small as possible, there are a few calling
62 restrictions for this service. ConnectController() must
63 follow these calling restrictions. If any other agent wishes to call
64 Supported() it must also follow these calling restrictions.
65
66 @param[in] This Protocol instance pointer.
67 @param[in] ControllerHandle Handle of device to test
68 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
69 device to start.
70
71 @retval EFI_SUCCESS This driver supports this device
72 @retval EFI_ALREADY_STARTED This driver is already running on this device
73 @retval other This driver does not support this device
74
75 **/
76 EFI_STATUS
77 EFIAPI
78 Ip4DriverBindingSupported (
79 IN EFI_DRIVER_BINDING_PROTOCOL * This,
80 IN EFI_HANDLE ControllerHandle,
81 IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL
82 )
83 {
84 EFI_STATUS Status;
85
86 //
87 // Test for the MNP service binding Protocol
88 //
89 Status = gBS->OpenProtocol (
90 ControllerHandle,
91 &gEfiManagedNetworkServiceBindingProtocolGuid,
92 NULL,
93 This->DriverBindingHandle,
94 ControllerHandle,
95 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
96 );
97
98 if (EFI_ERROR (Status)) {
99 return Status;
100 }
101
102 //
103 // Test for the Arp service binding Protocol
104 //
105 Status = gBS->OpenProtocol (
106 ControllerHandle,
107 &gEfiArpServiceBindingProtocolGuid,
108 NULL,
109 This->DriverBindingHandle,
110 ControllerHandle,
111 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
112 );
113
114 return Status;
115 }
116
117 /**
118 Clean up a IP4 service binding instance. It will release all
119 the resource allocated by the instance. The instance may be
120 partly initialized, or partly destroyed. If a resource is
121 destroyed, it is marked as that in case the destroy failed and
122 being called again later.
123
124 @param[in] IpSb The IP4 serviceing binding instance to clean up
125
126 @retval EFI_SUCCESS The resource used by the instance are cleaned up
127 @retval other Failed to clean up some of the resources.
128
129 **/
130 EFI_STATUS
131 Ip4CleanService (
132 IN IP4_SERVICE *IpSb
133 );
134
135
136 /**
137 Create a new IP4 driver service binding private instance.
138
139 @param Controller The controller that has MNP service binding
140 installed
141 @param ImageHandle The IP4 driver's image handle
142 @param Service The variable to receive the newly created IP4
143 service.
144
145 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
146 @retval EFI_SUCCESS A new IP4 service binding private is created.
147 @retval other Other error occurs.
148
149 **/
150 EFI_STATUS
151 Ip4CreateService (
152 IN EFI_HANDLE Controller,
153 IN EFI_HANDLE ImageHandle,
154 OUT IP4_SERVICE **Service
155 )
156 {
157 IP4_SERVICE *IpSb;
158 EFI_STATUS Status;
159
160 ASSERT (Service != NULL);
161
162 *Service = NULL;
163
164 //
165 // allocate a service private data then initialize all the filed to
166 // empty resources, so if any thing goes wrong when allocating
167 // resources, Ip4CleanService can be called to clean it up.
168 //
169 IpSb = AllocatePool (sizeof (IP4_SERVICE));
170
171 if (IpSb == NULL) {
172 return EFI_OUT_OF_RESOURCES;
173 }
174
175 IpSb->Signature = IP4_SERVICE_SIGNATURE;
176 IpSb->ServiceBinding.CreateChild = Ip4ServiceBindingCreateChild;
177 IpSb->ServiceBinding.DestroyChild = Ip4ServiceBindingDestroyChild;
178 IpSb->State = IP4_SERVICE_UNSTARTED;
179
180 IpSb->NumChildren = 0;
181 InitializeListHead (&IpSb->Children);
182
183 InitializeListHead (&IpSb->Interfaces);
184 IpSb->DefaultInterface = NULL;
185 IpSb->DefaultRouteTable = NULL;
186
187 Ip4InitAssembleTable (&IpSb->Assemble);
188
189 IpSb->IgmpCtrl.Igmpv1QuerySeen = 0;
190 InitializeListHead (&IpSb->IgmpCtrl.Groups);
191
192 IpSb->Image = ImageHandle;
193 IpSb->Controller = Controller;
194
195 IpSb->MnpChildHandle = NULL;
196 IpSb->Mnp = NULL;
197
198 IpSb->MnpConfigData.ReceivedQueueTimeoutValue = 0;
199 IpSb->MnpConfigData.TransmitQueueTimeoutValue = 0;
200 IpSb->MnpConfigData.ProtocolTypeFilter = IP4_ETHER_PROTO;
201 IpSb->MnpConfigData.EnableUnicastReceive = TRUE;
202 IpSb->MnpConfigData.EnableMulticastReceive = TRUE;
203 IpSb->MnpConfigData.EnableBroadcastReceive = TRUE;
204 IpSb->MnpConfigData.EnablePromiscuousReceive = FALSE;
205 IpSb->MnpConfigData.FlushQueuesOnReset = TRUE;
206 IpSb->MnpConfigData.EnableReceiveTimestamps = FALSE;
207 IpSb->MnpConfigData.DisableBackgroundPolling = FALSE;
208
209 ZeroMem (&IpSb->SnpMode, sizeof (EFI_SIMPLE_NETWORK_MODE));
210
211 IpSb->Timer = NULL;
212 IpSb->Ip4Config = NULL;
213 IpSb->DoneEvent = NULL;
214 IpSb->ReconfigEvent = NULL;
215 IpSb->ActiveEvent = NULL;
216
217 //
218 // Create various resources. First create the route table, timer
219 // event and MNP child. IGMP, interface's initialization depend
220 // on the MNP child.
221 //
222 IpSb->DefaultRouteTable = Ip4CreateRouteTable ();
223
224 if (IpSb->DefaultRouteTable == NULL) {
225 Status = EFI_OUT_OF_RESOURCES;
226 goto ON_ERROR;
227 }
228
229 Status = gBS->CreateEvent (
230 EVT_NOTIFY_SIGNAL | EVT_TIMER,
231 TPL_CALLBACK,
232 Ip4TimerTicking,
233 IpSb,
234 &IpSb->Timer
235 );
236
237 if (EFI_ERROR (Status)) {
238 goto ON_ERROR;
239 }
240
241 Status = NetLibCreateServiceChild (
242 Controller,
243 ImageHandle,
244 &gEfiManagedNetworkServiceBindingProtocolGuid,
245 &IpSb->MnpChildHandle
246 );
247
248 if (EFI_ERROR (Status)) {
249 goto ON_ERROR;
250 }
251
252 Status = gBS->OpenProtocol (
253 IpSb->MnpChildHandle,
254 &gEfiManagedNetworkProtocolGuid,
255 (VOID **) &IpSb->Mnp,
256 ImageHandle,
257 Controller,
258 EFI_OPEN_PROTOCOL_BY_DRIVER
259 );
260
261 if (EFI_ERROR (Status)) {
262 goto ON_ERROR;
263 }
264
265 Status = Ip4ServiceConfigMnp (IpSb, TRUE);
266
267 if (EFI_ERROR (Status)) {
268 goto ON_ERROR;
269 }
270
271 Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &IpSb->SnpMode);
272
273 if (EFI_ERROR (Status)) {
274 goto ON_ERROR;
275 }
276
277 Status = Ip4InitIgmp (IpSb);
278
279 if (EFI_ERROR (Status)) {
280 goto ON_ERROR;
281 }
282
283 IpSb->DefaultInterface = Ip4CreateInterface (IpSb->Mnp, Controller, ImageHandle);
284
285 if (IpSb->DefaultInterface == NULL) {
286 Status = EFI_OUT_OF_RESOURCES;
287 goto ON_ERROR;
288 }
289
290 InsertHeadList (&IpSb->Interfaces, &IpSb->DefaultInterface->Link);
291
292 IpSb->MaxPacketSize = IpSb->SnpMode.MaxPacketSize - sizeof (IP4_HEAD);
293 if (NetLibGetVlanId (IpSb->Controller) != 0) {
294 //
295 // This is a VLAN device, reduce MTU by VLAN tag length
296 //
297 IpSb->MaxPacketSize -= NET_VLAN_TAG_LEN;
298 }
299 IpSb->OldMaxPacketSize = IpSb->MaxPacketSize;
300 IpSb->MacString = NULL;
301
302 *Service = IpSb;
303 return EFI_SUCCESS;
304
305 ON_ERROR:
306 Ip4CleanService (IpSb);
307 FreePool (IpSb);
308
309 return Status;
310 }
311
312
313 /**
314 Clean up a IP4 service binding instance. It will release all
315 the resource allocated by the instance. The instance may be
316 partly initialized, or partly destroyed. If a resource is
317 destroyed, it is marked as that in case the destroy failed and
318 being called again later.
319
320 @param[in] IpSb The IP4 serviceing binding instance to clean up
321
322 @retval EFI_SUCCESS The resource used by the instance are cleaned up
323 @retval other Failed to clean up some of the resources.
324
325 **/
326 EFI_STATUS
327 Ip4CleanService (
328 IN IP4_SERVICE *IpSb
329 )
330 {
331 EFI_STATUS Status;
332
333 if (IpSb->DefaultInterface != NULL) {
334 Status = Ip4FreeInterface (IpSb->DefaultInterface, NULL);
335
336 if (EFI_ERROR (Status)) {
337 return Status;
338 }
339
340 IpSb->DefaultInterface = NULL;
341 }
342
343 if (IpSb->DefaultRouteTable != NULL) {
344 Ip4FreeRouteTable (IpSb->DefaultRouteTable);
345 IpSb->DefaultRouteTable = NULL;
346 }
347
348 Ip4CleanAssembleTable (&IpSb->Assemble);
349
350 if (IpSb->MnpChildHandle != NULL) {
351 if (IpSb->Mnp != NULL) {
352 gBS->CloseProtocol (
353 IpSb->MnpChildHandle,
354 &gEfiManagedNetworkProtocolGuid,
355 IpSb->Image,
356 IpSb->Controller
357 );
358
359 IpSb->Mnp = NULL;
360 }
361
362 NetLibDestroyServiceChild (
363 IpSb->Controller,
364 IpSb->Image,
365 &gEfiManagedNetworkServiceBindingProtocolGuid,
366 IpSb->MnpChildHandle
367 );
368
369 IpSb->MnpChildHandle = NULL;
370 }
371
372 if (IpSb->Timer != NULL) {
373 gBS->SetTimer (IpSb->Timer, TimerCancel, 0);
374 gBS->CloseEvent (IpSb->Timer);
375
376 IpSb->Timer = NULL;
377 }
378
379 if (IpSb->Ip4Config != NULL) {
380 IpSb->Ip4Config->Stop (IpSb->Ip4Config);
381
382 gBS->CloseProtocol (
383 IpSb->Controller,
384 &gEfiIp4ConfigProtocolGuid,
385 IpSb->Image,
386 IpSb->Controller
387 );
388
389 gBS->CloseEvent (IpSb->DoneEvent);
390 gBS->CloseEvent (IpSb->ReconfigEvent);
391 IpSb->ActiveEvent = NULL;
392 IpSb->Ip4Config = NULL;
393 }
394
395 return EFI_SUCCESS;
396 }
397
398 /**
399 Callback function which provided by user to remove one node in NetDestroyLinkList process.
400
401 @param[in] Entry The entry to be removed.
402 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
403
404 @retval EFI_SUCCESS The entry has been removed successfully.
405 @retval Others Fail to remove the entry.
406
407 **/
408 EFI_STATUS
409 EFIAPI
410 Ip4DestroyChildEntryInHandleBuffer (
411 IN LIST_ENTRY *Entry,
412 IN VOID *Context
413 )
414 {
415 IP4_PROTOCOL *IpInstance;
416 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
417 UINTN NumberOfChildren;
418 EFI_HANDLE *ChildHandleBuffer;
419
420 if (Entry == NULL || Context == NULL) {
421 return EFI_INVALID_PARAMETER;
422 }
423
424 IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, Link, IP4_PROTOCOL_SIGNATURE);
425 ServiceBinding = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding;
426 NumberOfChildren = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren;
427 ChildHandleBuffer = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer;
428
429 if (!NetIsInHandleBuffer (IpInstance->Handle, NumberOfChildren, ChildHandleBuffer)) {
430 return EFI_SUCCESS;
431 }
432
433 return ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle);
434 }
435
436 /**
437 Start this driver on ControllerHandle. This service is called by the
438 EFI boot service ConnectController(). In order to make
439 drivers as small as possible, there are a few calling restrictions for
440 this service. ConnectController() must follow these
441 calling restrictions. If any other agent wishes to call Start() it
442 must also follow these calling restrictions.
443
444 @param[in] This Protocol instance pointer.
445 @param[in] ControllerHandle Handle of device to bind driver to
446 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
447 device to start.
448
449 @retval EFI_SUCCESS This driver is added to ControllerHandle
450 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
451 @retval other This driver does not support this device
452
453 **/
454 EFI_STATUS
455 EFIAPI
456 Ip4DriverBindingStart (
457 IN EFI_DRIVER_BINDING_PROTOCOL * This,
458 IN EFI_HANDLE ControllerHandle,
459 IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL
460 )
461 {
462 IP4_SERVICE *IpSb;
463 EFI_STATUS Status;
464
465 //
466 // Test for the Ip4 service binding protocol
467 //
468 Status = gBS->OpenProtocol (
469 ControllerHandle,
470 &gEfiIp4ServiceBindingProtocolGuid,
471 NULL,
472 This->DriverBindingHandle,
473 ControllerHandle,
474 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
475 );
476
477 if (Status == EFI_SUCCESS) {
478 return EFI_ALREADY_STARTED;
479 }
480
481 Status = Ip4CreateService (ControllerHandle, This->DriverBindingHandle, &IpSb);
482
483 if (EFI_ERROR (Status)) {
484 return Status;
485 }
486 ASSERT (IpSb != NULL);
487
488 //
489 // Install the Ip4ServiceBinding Protocol onto ControlerHandle
490 //
491 Status = gBS->InstallMultipleProtocolInterfaces (
492 &ControllerHandle,
493 &gEfiIp4ServiceBindingProtocolGuid,
494 &IpSb->ServiceBinding,
495 NULL
496 );
497
498 if (EFI_ERROR (Status)) {
499 goto FREE_SERVICE;
500 }
501
502 //
503 // ready to go: start the receiving and timer
504 //
505 Status = Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);
506
507 if (EFI_ERROR (Status)) {
508 goto UNINSTALL_PROTOCOL;
509 }
510
511 Status = gBS->SetTimer (IpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);
512
513 if (EFI_ERROR (Status)) {
514 goto UNINSTALL_PROTOCOL;
515 }
516
517 //
518 // Initialize the IP4 ID
519 //
520 mIp4Id = (UINT16)NET_RANDOM (NetRandomInitSeed ());
521
522 Ip4SetVariableData (IpSb);
523
524 return Status;
525
526 UNINSTALL_PROTOCOL:
527 gBS->UninstallProtocolInterface (
528 ControllerHandle,
529 &gEfiIp4ServiceBindingProtocolGuid,
530 &IpSb->ServiceBinding
531 );
532
533 FREE_SERVICE:
534 Ip4CleanService (IpSb);
535 FreePool (IpSb);
536
537 return Status;
538 }
539
540
541 /**
542 Stop this driver on ControllerHandle. This service is called by the
543 EFI boot service DisconnectController(). In order to
544 make drivers as small as possible, there are a few calling
545 restrictions for this service. DisconnectController()
546 must follow these calling restrictions. If any other agent wishes
547 to call Stop() it must also follow these calling restrictions.
548
549 @param[in] This Protocol instance pointer.
550 @param[in] ControllerHandle Handle of device to stop driver on
551 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number
552 of children is zero stop the entire bus driver.
553 @param[in] ChildHandleBuffer List of Child Handles to Stop.
554
555 @retval EFI_SUCCESS This driver is removed ControllerHandle
556 @retval other This driver was not removed from this device
557
558 **/
559 EFI_STATUS
560 EFIAPI
561 Ip4DriverBindingStop (
562 IN EFI_DRIVER_BINDING_PROTOCOL *This,
563 IN EFI_HANDLE ControllerHandle,
564 IN UINTN NumberOfChildren,
565 IN EFI_HANDLE *ChildHandleBuffer
566 )
567 {
568 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
569 IP4_SERVICE *IpSb;
570 EFI_HANDLE NicHandle;
571 EFI_STATUS Status;
572 INTN State;
573 LIST_ENTRY *List;
574 IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context;
575 IP4_INTERFACE *IpIf;
576 IP4_ROUTE_TABLE *RouteTable;
577
578 //
579 // IP4 driver opens the MNP child, ARP children or the IP4_CONFIG protocol
580 // by driver. So the ControllerHandle may be the MNP child handle, ARP child
581 // handle, or the NIC (UNDI) handle because IP4_CONFIG protocol is installed
582 // in the NIC handle.
583 //
584 //
585 // First, check whether it is the IP4_CONFIG protocol being uninstalled.
586 // IP4_CONFIG protocol is installed on the NIC handle. It isn't necessary
587 // to clean up the default configuration if IP4_CONFIG is being stopped.
588 //
589 Status = gBS->OpenProtocol (
590 ControllerHandle,
591 &gEfiIp4ConfigProtocolGuid,
592 NULL,
593 This->DriverBindingHandle,
594 ControllerHandle,
595 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
596 );
597 if (Status == EFI_SUCCESS) {
598 //
599 // Retrieve the IP4 service binding protocol. If failed, it is
600 // likely that Ip4 ServiceBinding is uninstalled already. In this
601 // case, return immediately.
602 //
603 Status = gBS->OpenProtocol (
604 ControllerHandle,
605 &gEfiIp4ServiceBindingProtocolGuid,
606 (VOID **) &ServiceBinding,
607 This->DriverBindingHandle,
608 ControllerHandle,
609 EFI_OPEN_PROTOCOL_GET_PROTOCOL
610 );
611 if (EFI_ERROR (Status)) {
612 return EFI_DEVICE_ERROR;
613 }
614
615 IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);
616 if (IpSb->Ip4Config != NULL && (IpSb->State != IP4_SERVICE_DESTROY)) {
617
618 IpSb->Ip4Config->Stop (IpSb->Ip4Config);
619
620 Status = gBS->CloseProtocol (
621 ControllerHandle,
622 &gEfiIp4ConfigProtocolGuid,
623 IpSb->Image,
624 ControllerHandle
625 );
626 if (EFI_ERROR (Status)) {
627 return Status;
628 }
629
630 //
631 // If the auto configure hasn't complete, mark it as not started.
632 //
633 if (IpSb->State == IP4_SERVICE_STARTED) {
634 IpSb->State = IP4_SERVICE_UNSTARTED;
635 }
636
637 IpSb->Ip4Config = NULL;
638 gBS->CloseEvent (IpSb->DoneEvent);
639 gBS->CloseEvent (IpSb->ReconfigEvent);
640 }
641
642 return EFI_SUCCESS;
643 }
644
645 //
646 // Either MNP or ARP protocol is being uninstalled. The controller
647 // handle is either the MNP child or ARP child. But, the IP4's
648 // service binding is installed on the NIC handle. So, need to open
649 // the protocol info to find the NIC handle.
650 //
651 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);
652 if (NicHandle == NULL) {
653 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiArpProtocolGuid);
654 if (NicHandle == NULL) {
655 return EFI_SUCCESS;
656 }
657 }
658
659 //
660 // Retrieve the IP4 service binding protocol
661 //
662 Status = gBS->OpenProtocol (
663 NicHandle,
664 &gEfiIp4ServiceBindingProtocolGuid,
665 (VOID **) &ServiceBinding,
666 This->DriverBindingHandle,
667 NicHandle,
668 EFI_OPEN_PROTOCOL_GET_PROTOCOL
669 );
670 if (EFI_ERROR (Status)) {
671 return EFI_DEVICE_ERROR;
672 }
673
674 IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);
675 if (NumberOfChildren != 0) {
676 List = &IpSb->Children;
677 Context.ServiceBinding = ServiceBinding;
678 Context.NumberOfChildren = NumberOfChildren;
679 Context.ChildHandleBuffer = ChildHandleBuffer;
680 Status = NetDestroyLinkList (
681 List,
682 Ip4DestroyChildEntryInHandleBuffer,
683 &Context,
684 NULL
685 );
686 } else if (IpSb->DefaultInterface->ArpHandle == ControllerHandle) {
687 //
688 // The ARP protocol for the default interface is being uninstalled and all
689 // its IP child handles should have been destroyed before. So, release the
690 // default interface and route table, create a new one and mark it as not started.
691 //
692 Ip4CancelReceive (IpSb->DefaultInterface);
693 Ip4FreeInterface (IpSb->DefaultInterface, NULL);
694 Ip4FreeRouteTable (IpSb->DefaultRouteTable);
695
696 IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);
697 if (IpIf == NULL) {
698 goto ON_ERROR;
699 }
700 RouteTable = Ip4CreateRouteTable ();
701 if (RouteTable == NULL) {
702 Ip4FreeInterface (IpIf, NULL);
703 goto ON_ERROR;;
704 }
705
706 IpSb->DefaultInterface = IpIf;
707 InsertHeadList (&IpSb->Interfaces, &IpIf->Link);
708 IpSb->DefaultRouteTable = RouteTable;
709 Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb);
710
711 if (IpSb->Ip4Config != NULL && IpSb->State != IP4_SERVICE_DESTROY) {
712 IpSb->Ip4Config->Stop (IpSb->Ip4Config);
713 }
714 IpSb->State = IP4_SERVICE_UNSTARTED;
715 } else if (IsListEmpty (&IpSb->Children)) {
716 State = IpSb->State;
717 IpSb->State = IP4_SERVICE_DESTROY;
718
719 //
720 // Clear the variable data.
721 //
722 Ip4ClearVariableData (IpSb);
723
724 //
725 // OK, clean other resources then uninstall the service binding protocol.
726 //
727 Status = Ip4CleanService (IpSb);
728 if (EFI_ERROR (Status)) {
729 IpSb->State = State;
730 goto ON_ERROR;
731 }
732
733 gBS->UninstallProtocolInterface (
734 NicHandle,
735 &gEfiIp4ServiceBindingProtocolGuid,
736 ServiceBinding
737 );
738
739 if (gIp4ControllerNameTable != NULL) {
740 FreeUnicodeStringTable (gIp4ControllerNameTable);
741 gIp4ControllerNameTable = NULL;
742 }
743 FreePool (IpSb);
744 }
745
746 ON_ERROR:
747 return Status;
748 }
749
750
751 /**
752 Creates a child handle and installs a protocol.
753
754 The CreateChild() function installs a protocol on ChildHandle.
755 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
756 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
757
758 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
759 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
760 then a new handle is created. If it is a pointer to an existing UEFI handle,
761 then the protocol is added to the existing UEFI handle.
762
763 @retval EFI_SUCCES The protocol was added to ChildHandle.
764 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
765 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
766 the child
767 @retval other The child handle was not created
768
769 **/
770 EFI_STATUS
771 EFIAPI
772 Ip4ServiceBindingCreateChild (
773 IN EFI_SERVICE_BINDING_PROTOCOL *This,
774 IN OUT EFI_HANDLE *ChildHandle
775 )
776 {
777 IP4_SERVICE *IpSb;
778 IP4_PROTOCOL *IpInstance;
779 EFI_TPL OldTpl;
780 EFI_STATUS Status;
781 VOID *Mnp;
782
783 if ((This == NULL) || (ChildHandle == NULL)) {
784 return EFI_INVALID_PARAMETER;
785 }
786
787 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
788 IpInstance = AllocatePool (sizeof (IP4_PROTOCOL));
789
790 if (IpInstance == NULL) {
791 return EFI_OUT_OF_RESOURCES;
792 }
793
794 Ip4InitProtocol (IpSb, IpInstance);
795
796 //
797 // Install Ip4 onto ChildHandle
798 //
799 Status = gBS->InstallMultipleProtocolInterfaces (
800 ChildHandle,
801 &gEfiIp4ProtocolGuid,
802 &IpInstance->Ip4Proto,
803 NULL
804 );
805
806 if (EFI_ERROR (Status)) {
807 goto ON_ERROR;
808 }
809
810 IpInstance->Handle = *ChildHandle;
811
812 //
813 // Open the Managed Network protocol BY_CHILD.
814 //
815 Status = gBS->OpenProtocol (
816 IpSb->MnpChildHandle,
817 &gEfiManagedNetworkProtocolGuid,
818 (VOID **) &Mnp,
819 gIp4DriverBinding.DriverBindingHandle,
820 IpInstance->Handle,
821 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
822 );
823 if (EFI_ERROR (Status)) {
824 gBS->UninstallMultipleProtocolInterfaces (
825 ChildHandle,
826 &gEfiIp4ProtocolGuid,
827 &IpInstance->Ip4Proto,
828 NULL
829 );
830
831 goto ON_ERROR;
832 }
833
834 //
835 // Insert it into the service binding instance.
836 //
837 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
838
839 InsertTailList (&IpSb->Children, &IpInstance->Link);
840 IpSb->NumChildren++;
841
842 gBS->RestoreTPL (OldTpl);
843
844 ON_ERROR:
845
846 if (EFI_ERROR (Status)) {
847
848 Ip4CleanProtocol (IpInstance);
849
850 FreePool (IpInstance);
851 }
852
853 return Status;
854 }
855
856
857 /**
858 Destroys a child handle with a protocol installed on it.
859
860 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
861 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
862 last protocol on ChildHandle, then ChildHandle is destroyed.
863
864 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
865 @param ChildHandle Handle of the child to destroy
866
867 @retval EFI_SUCCES The protocol was removed from ChildHandle.
868 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
869 @retval EFI_INVALID_PARAMETER Child handle is NULL.
870 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
871 because its services are being used.
872 @retval other The child handle was not destroyed
873
874 **/
875 EFI_STATUS
876 EFIAPI
877 Ip4ServiceBindingDestroyChild (
878 IN EFI_SERVICE_BINDING_PROTOCOL *This,
879 IN EFI_HANDLE ChildHandle
880 )
881 {
882 EFI_STATUS Status;
883 IP4_SERVICE *IpSb;
884 IP4_PROTOCOL *IpInstance;
885 EFI_IP4_PROTOCOL *Ip4;
886 EFI_TPL OldTpl;
887 INTN State;
888
889 if ((This == NULL) || (ChildHandle == NULL)) {
890 return EFI_INVALID_PARAMETER;
891 }
892
893 //
894 // Retrieve the private context data structures
895 //
896 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
897
898 Status = gBS->OpenProtocol (
899 ChildHandle,
900 &gEfiIp4ProtocolGuid,
901 (VOID **) &Ip4,
902 gIp4DriverBinding.DriverBindingHandle,
903 ChildHandle,
904 EFI_OPEN_PROTOCOL_GET_PROTOCOL
905 );
906
907 if (EFI_ERROR (Status)) {
908 return EFI_UNSUPPORTED;
909 }
910
911 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (Ip4);
912
913 if (IpInstance->Service != IpSb) {
914 return EFI_INVALID_PARAMETER;
915 }
916
917 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
918
919 //
920 // A child can be destroyed more than once. For example,
921 // Ip4DriverBindingStop will destroy all of its children.
922 // when UDP driver is being stopped, it will destroy all
923 // the IP child it opens.
924 //
925 if (IpInstance->State == IP4_STATE_DESTROY) {
926 gBS->RestoreTPL (OldTpl);
927 return EFI_SUCCESS;
928 }
929
930 State = IpInstance->State;
931 IpInstance->State = IP4_STATE_DESTROY;
932
933 //
934 // Close the Managed Network protocol.
935 //
936 gBS->CloseProtocol (
937 IpSb->MnpChildHandle,
938 &gEfiManagedNetworkProtocolGuid,
939 gIp4DriverBinding.DriverBindingHandle,
940 ChildHandle
941 );
942
943 if (IpInstance->Interface != NULL && IpInstance->Interface->Arp != NULL) {
944 gBS->CloseProtocol (
945 IpInstance->Interface->ArpHandle,
946 &gEfiArpProtocolGuid,
947 gIp4DriverBinding.DriverBindingHandle,
948 ChildHandle
949 );
950 }
951
952 //
953 // Uninstall the IP4 protocol first. Many thing happens during
954 // this:
955 // 1. The consumer of the IP4 protocol will be stopped if it
956 // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is
957 // stopped, IP driver's stop function will be called, and uninstall
958 // EFI_IP4_PROTOCOL will trigger the UDP's stop function. This
959 // makes it possible to create the network stack bottom up, and
960 // stop it top down.
961 // 2. the upper layer will recycle the received packet. The recycle
962 // event's TPL is higher than this function. The recycle events
963 // will be called back before preceeding. If any packets not recycled,
964 // that means there is a resource leak.
965 //
966 gBS->RestoreTPL (OldTpl);
967 Status = gBS->UninstallProtocolInterface (
968 ChildHandle,
969 &gEfiIp4ProtocolGuid,
970 &IpInstance->Ip4Proto
971 );
972 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
973 if (EFI_ERROR (Status)) {
974 goto ON_ERROR;
975 }
976
977 Status = Ip4CleanProtocol (IpInstance);
978
979 Ip4SetVariableData (IpSb);
980
981 if (EFI_ERROR (Status)) {
982 gBS->InstallMultipleProtocolInterfaces (
983 &ChildHandle,
984 &gEfiIp4ProtocolGuid,
985 Ip4,
986 NULL
987 );
988
989 goto ON_ERROR;
990 }
991
992 RemoveEntryList (&IpInstance->Link);
993 IpSb->NumChildren--;
994
995 gBS->RestoreTPL (OldTpl);
996
997 FreePool (IpInstance);
998 return EFI_SUCCESS;
999
1000 ON_ERROR:
1001 IpInstance->State = State;
1002 gBS->RestoreTPL (OldTpl);
1003
1004 return Status;
1005 }