]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Driver.c
MdeModulePkg: INF/DEC file updates to EDK II packages
[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 - 2014, 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 *Service = IpSb;
301 return EFI_SUCCESS;
302
303 ON_ERROR:
304 Ip4CleanService (IpSb);
305 FreePool (IpSb);
306
307 return Status;
308 }
309
310
311 /**
312 Clean up a IP4 service binding instance. It will release all
313 the resource allocated by the instance. The instance may be
314 partly initialized, or partly destroyed. If a resource is
315 destroyed, it is marked as that in case the destroy failed and
316 being called again later.
317
318 @param[in] IpSb The IP4 serviceing binding instance to clean up
319
320 @retval EFI_SUCCESS The resource used by the instance are cleaned up
321 @retval other Failed to clean up some of the resources.
322
323 **/
324 EFI_STATUS
325 Ip4CleanService (
326 IN IP4_SERVICE *IpSb
327 )
328 {
329 EFI_STATUS Status;
330
331 if (IpSb->DefaultInterface != NULL) {
332 Status = Ip4FreeInterface (IpSb->DefaultInterface, NULL);
333
334 if (EFI_ERROR (Status)) {
335 return Status;
336 }
337
338 IpSb->DefaultInterface = NULL;
339 }
340
341 if (IpSb->DefaultRouteTable != NULL) {
342 Ip4FreeRouteTable (IpSb->DefaultRouteTable);
343 IpSb->DefaultRouteTable = NULL;
344 }
345
346 Ip4CleanAssembleTable (&IpSb->Assemble);
347
348 if (IpSb->MnpChildHandle != NULL) {
349 if (IpSb->Mnp != NULL) {
350 gBS->CloseProtocol (
351 IpSb->MnpChildHandle,
352 &gEfiManagedNetworkProtocolGuid,
353 IpSb->Image,
354 IpSb->Controller
355 );
356
357 IpSb->Mnp = NULL;
358 }
359
360 NetLibDestroyServiceChild (
361 IpSb->Controller,
362 IpSb->Image,
363 &gEfiManagedNetworkServiceBindingProtocolGuid,
364 IpSb->MnpChildHandle
365 );
366
367 IpSb->MnpChildHandle = NULL;
368 }
369
370 if (IpSb->Timer != NULL) {
371 gBS->SetTimer (IpSb->Timer, TimerCancel, 0);
372 gBS->CloseEvent (IpSb->Timer);
373
374 IpSb->Timer = NULL;
375 }
376
377 if (IpSb->Ip4Config != NULL) {
378 IpSb->Ip4Config->Stop (IpSb->Ip4Config);
379
380 gBS->CloseProtocol (
381 IpSb->Controller,
382 &gEfiIp4ConfigProtocolGuid,
383 IpSb->Image,
384 IpSb->Controller
385 );
386
387 gBS->CloseEvent (IpSb->DoneEvent);
388 gBS->CloseEvent (IpSb->ReconfigEvent);
389 IpSb->ActiveEvent = NULL;
390 IpSb->Ip4Config = NULL;
391 }
392
393 return EFI_SUCCESS;
394 }
395
396 /**
397 Callback function which provided by user to remove one node in NetDestroyLinkList process.
398
399 @param[in] Entry The entry to be removed.
400 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
401
402 @retval EFI_SUCCESS The entry has been removed successfully.
403 @retval Others Fail to remove the entry.
404
405 **/
406 EFI_STATUS
407 EFIAPI
408 Ip4DestroyChildEntryInHandleBuffer (
409 IN LIST_ENTRY *Entry,
410 IN VOID *Context
411 )
412 {
413 IP4_PROTOCOL *IpInstance;
414 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
415 UINTN NumberOfChildren;
416 EFI_HANDLE *ChildHandleBuffer;
417
418 if (Entry == NULL || Context == NULL) {
419 return EFI_INVALID_PARAMETER;
420 }
421
422 IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, Link, IP4_PROTOCOL_SIGNATURE);
423 ServiceBinding = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding;
424 NumberOfChildren = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren;
425 ChildHandleBuffer = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer;
426
427 if (!NetIsInHandleBuffer (IpInstance->Handle, NumberOfChildren, ChildHandleBuffer)) {
428 return EFI_SUCCESS;
429 }
430
431 return ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle);
432 }
433
434 /**
435 Start this driver on ControllerHandle. This service is called by the
436 EFI boot service ConnectController(). In order to make
437 drivers as small as possible, there are a few calling restrictions for
438 this service. ConnectController() must follow these
439 calling restrictions. If any other agent wishes to call Start() it
440 must also follow these calling restrictions.
441
442 @param[in] This Protocol instance pointer.
443 @param[in] ControllerHandle Handle of device to bind driver to
444 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
445 device to start.
446
447 @retval EFI_SUCCESS This driver is added to ControllerHandle
448 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
449 @retval other This driver does not support this device
450
451 **/
452 EFI_STATUS
453 EFIAPI
454 Ip4DriverBindingStart (
455 IN EFI_DRIVER_BINDING_PROTOCOL * This,
456 IN EFI_HANDLE ControllerHandle,
457 IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL
458 )
459 {
460 IP4_SERVICE *IpSb;
461 EFI_STATUS Status;
462
463 //
464 // Test for the Ip4 service binding protocol
465 //
466 Status = gBS->OpenProtocol (
467 ControllerHandle,
468 &gEfiIp4ServiceBindingProtocolGuid,
469 NULL,
470 This->DriverBindingHandle,
471 ControllerHandle,
472 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
473 );
474
475 if (Status == EFI_SUCCESS) {
476 return EFI_ALREADY_STARTED;
477 }
478
479 Status = Ip4CreateService (ControllerHandle, This->DriverBindingHandle, &IpSb);
480
481 if (EFI_ERROR (Status)) {
482 return Status;
483 }
484 ASSERT (IpSb != NULL);
485
486 //
487 // Install the Ip4ServiceBinding Protocol onto ControlerHandle
488 //
489 Status = gBS->InstallMultipleProtocolInterfaces (
490 &ControllerHandle,
491 &gEfiIp4ServiceBindingProtocolGuid,
492 &IpSb->ServiceBinding,
493 NULL
494 );
495
496 if (EFI_ERROR (Status)) {
497 goto FREE_SERVICE;
498 }
499
500 //
501 // ready to go: start the receiving and timer
502 //
503 Status = Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);
504
505 if (EFI_ERROR (Status)) {
506 goto UNINSTALL_PROTOCOL;
507 }
508
509 Status = gBS->SetTimer (IpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);
510
511 if (EFI_ERROR (Status)) {
512 goto UNINSTALL_PROTOCOL;
513 }
514
515 //
516 // Initialize the IP4 ID
517 //
518 mIp4Id = (UINT16)NET_RANDOM (NetRandomInitSeed ());
519
520 return Status;
521
522 UNINSTALL_PROTOCOL:
523 gBS->UninstallProtocolInterface (
524 ControllerHandle,
525 &gEfiIp4ServiceBindingProtocolGuid,
526 &IpSb->ServiceBinding
527 );
528
529 FREE_SERVICE:
530 Ip4CleanService (IpSb);
531 FreePool (IpSb);
532
533 return Status;
534 }
535
536
537 /**
538 Stop this driver on ControllerHandle. This service is called by the
539 EFI boot service DisconnectController(). In order to
540 make drivers as small as possible, there are a few calling
541 restrictions for this service. DisconnectController()
542 must follow these calling restrictions. If any other agent wishes
543 to call Stop() it must also follow these calling restrictions.
544
545 @param[in] This Protocol instance pointer.
546 @param[in] ControllerHandle Handle of device to stop driver on
547 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number
548 of children is zero stop the entire bus driver.
549 @param[in] ChildHandleBuffer List of Child Handles to Stop.
550
551 @retval EFI_SUCCESS This driver is removed ControllerHandle
552 @retval other This driver was not removed from this device
553
554 **/
555 EFI_STATUS
556 EFIAPI
557 Ip4DriverBindingStop (
558 IN EFI_DRIVER_BINDING_PROTOCOL *This,
559 IN EFI_HANDLE ControllerHandle,
560 IN UINTN NumberOfChildren,
561 IN EFI_HANDLE *ChildHandleBuffer
562 )
563 {
564 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
565 IP4_SERVICE *IpSb;
566 EFI_HANDLE NicHandle;
567 EFI_STATUS Status;
568 INTN State;
569 LIST_ENTRY *List;
570 IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context;
571 IP4_INTERFACE *IpIf;
572 IP4_ROUTE_TABLE *RouteTable;
573
574 //
575 // IP4 driver opens the MNP child, ARP children or the IP4_CONFIG protocol
576 // by driver. So the ControllerHandle may be the MNP child handle, ARP child
577 // handle, or the NIC (UNDI) handle because IP4_CONFIG protocol is installed
578 // in the NIC handle.
579 //
580 //
581 // First, check whether it is the IP4_CONFIG protocol being uninstalled.
582 // IP4_CONFIG protocol is installed on the NIC handle. It isn't necessary
583 // to clean up the default configuration if IP4_CONFIG is being stopped.
584 //
585 Status = gBS->OpenProtocol (
586 ControllerHandle,
587 &gEfiIp4ConfigProtocolGuid,
588 NULL,
589 This->DriverBindingHandle,
590 ControllerHandle,
591 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
592 );
593 if (Status == EFI_SUCCESS) {
594 //
595 // Retrieve the IP4 service binding protocol. If failed, it is
596 // likely that Ip4 ServiceBinding is uninstalled already. In this
597 // case, return immediately.
598 //
599 Status = gBS->OpenProtocol (
600 ControllerHandle,
601 &gEfiIp4ServiceBindingProtocolGuid,
602 (VOID **) &ServiceBinding,
603 This->DriverBindingHandle,
604 ControllerHandle,
605 EFI_OPEN_PROTOCOL_GET_PROTOCOL
606 );
607 if (EFI_ERROR (Status)) {
608 return EFI_DEVICE_ERROR;
609 }
610
611 IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);
612 if (IpSb->Ip4Config != NULL && (IpSb->State != IP4_SERVICE_DESTROY)) {
613
614 IpSb->Ip4Config->Stop (IpSb->Ip4Config);
615
616 Status = gBS->CloseProtocol (
617 ControllerHandle,
618 &gEfiIp4ConfigProtocolGuid,
619 IpSb->Image,
620 ControllerHandle
621 );
622 if (EFI_ERROR (Status)) {
623 return Status;
624 }
625
626 //
627 // If the auto configure hasn't complete, mark it as not started.
628 //
629 if (IpSb->State == IP4_SERVICE_STARTED) {
630 IpSb->State = IP4_SERVICE_UNSTARTED;
631 }
632
633 IpSb->Ip4Config = NULL;
634 gBS->CloseEvent (IpSb->DoneEvent);
635 gBS->CloseEvent (IpSb->ReconfigEvent);
636 }
637
638 return EFI_SUCCESS;
639 }
640
641 //
642 // Either MNP or ARP protocol is being uninstalled. The controller
643 // handle is either the MNP child or ARP child. But, the IP4's
644 // service binding is installed on the NIC handle. So, need to open
645 // the protocol info to find the NIC handle.
646 //
647 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);
648 if (NicHandle == NULL) {
649 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiArpProtocolGuid);
650 if (NicHandle == NULL) {
651 return EFI_SUCCESS;
652 }
653 }
654
655 //
656 // Retrieve the IP4 service binding protocol
657 //
658 Status = gBS->OpenProtocol (
659 NicHandle,
660 &gEfiIp4ServiceBindingProtocolGuid,
661 (VOID **) &ServiceBinding,
662 This->DriverBindingHandle,
663 NicHandle,
664 EFI_OPEN_PROTOCOL_GET_PROTOCOL
665 );
666 if (EFI_ERROR (Status)) {
667 return EFI_DEVICE_ERROR;
668 }
669
670 IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);
671 if (NumberOfChildren != 0) {
672 List = &IpSb->Children;
673 Context.ServiceBinding = ServiceBinding;
674 Context.NumberOfChildren = NumberOfChildren;
675 Context.ChildHandleBuffer = ChildHandleBuffer;
676 Status = NetDestroyLinkList (
677 List,
678 Ip4DestroyChildEntryInHandleBuffer,
679 &Context,
680 NULL
681 );
682 } else if (IpSb->DefaultInterface->ArpHandle == ControllerHandle) {
683 //
684 // The ARP protocol for the default interface is being uninstalled and all
685 // its IP child handles should have been destroyed before. So, release the
686 // default interface and route table, create a new one and mark it as not started.
687 //
688 Ip4CancelReceive (IpSb->DefaultInterface);
689 Ip4FreeInterface (IpSb->DefaultInterface, NULL);
690 Ip4FreeRouteTable (IpSb->DefaultRouteTable);
691
692 IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);
693 if (IpIf == NULL) {
694 goto ON_ERROR;
695 }
696 RouteTable = Ip4CreateRouteTable ();
697 if (RouteTable == NULL) {
698 Ip4FreeInterface (IpIf, NULL);
699 goto ON_ERROR;;
700 }
701
702 IpSb->DefaultInterface = IpIf;
703 InsertHeadList (&IpSb->Interfaces, &IpIf->Link);
704 IpSb->DefaultRouteTable = RouteTable;
705 Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb);
706
707 if (IpSb->Ip4Config != NULL && IpSb->State != IP4_SERVICE_DESTROY) {
708 IpSb->Ip4Config->Stop (IpSb->Ip4Config);
709 }
710 IpSb->State = IP4_SERVICE_UNSTARTED;
711 } else if (IsListEmpty (&IpSb->Children)) {
712 State = IpSb->State;
713 IpSb->State = IP4_SERVICE_DESTROY;
714
715 //
716 // OK, clean other resources then uninstall the service binding protocol.
717 //
718 Status = Ip4CleanService (IpSb);
719 if (EFI_ERROR (Status)) {
720 IpSb->State = State;
721 goto ON_ERROR;
722 }
723
724 gBS->UninstallProtocolInterface (
725 NicHandle,
726 &gEfiIp4ServiceBindingProtocolGuid,
727 ServiceBinding
728 );
729
730 if (gIp4ControllerNameTable != NULL) {
731 FreeUnicodeStringTable (gIp4ControllerNameTable);
732 gIp4ControllerNameTable = NULL;
733 }
734 FreePool (IpSb);
735 }
736
737 ON_ERROR:
738 return Status;
739 }
740
741
742 /**
743 Creates a child handle and installs a protocol.
744
745 The CreateChild() function installs a protocol on ChildHandle.
746 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
747 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
748
749 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
750 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
751 then a new handle is created. If it is a pointer to an existing UEFI handle,
752 then the protocol is added to the existing UEFI handle.
753
754 @retval EFI_SUCCES The protocol was added to ChildHandle.
755 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
756 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
757 the child
758 @retval other The child handle was not created
759
760 **/
761 EFI_STATUS
762 EFIAPI
763 Ip4ServiceBindingCreateChild (
764 IN EFI_SERVICE_BINDING_PROTOCOL *This,
765 IN OUT EFI_HANDLE *ChildHandle
766 )
767 {
768 IP4_SERVICE *IpSb;
769 IP4_PROTOCOL *IpInstance;
770 EFI_TPL OldTpl;
771 EFI_STATUS Status;
772 VOID *Mnp;
773
774 if ((This == NULL) || (ChildHandle == NULL)) {
775 return EFI_INVALID_PARAMETER;
776 }
777
778 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
779 IpInstance = AllocatePool (sizeof (IP4_PROTOCOL));
780
781 if (IpInstance == NULL) {
782 return EFI_OUT_OF_RESOURCES;
783 }
784
785 Ip4InitProtocol (IpSb, IpInstance);
786
787 //
788 // Install Ip4 onto ChildHandle
789 //
790 Status = gBS->InstallMultipleProtocolInterfaces (
791 ChildHandle,
792 &gEfiIp4ProtocolGuid,
793 &IpInstance->Ip4Proto,
794 NULL
795 );
796
797 if (EFI_ERROR (Status)) {
798 goto ON_ERROR;
799 }
800
801 IpInstance->Handle = *ChildHandle;
802
803 //
804 // Open the Managed Network protocol BY_CHILD.
805 //
806 Status = gBS->OpenProtocol (
807 IpSb->MnpChildHandle,
808 &gEfiManagedNetworkProtocolGuid,
809 (VOID **) &Mnp,
810 gIp4DriverBinding.DriverBindingHandle,
811 IpInstance->Handle,
812 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
813 );
814 if (EFI_ERROR (Status)) {
815 gBS->UninstallMultipleProtocolInterfaces (
816 ChildHandle,
817 &gEfiIp4ProtocolGuid,
818 &IpInstance->Ip4Proto,
819 NULL
820 );
821
822 goto ON_ERROR;
823 }
824
825 //
826 // Insert it into the service binding instance.
827 //
828 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
829
830 InsertTailList (&IpSb->Children, &IpInstance->Link);
831 IpSb->NumChildren++;
832
833 gBS->RestoreTPL (OldTpl);
834
835 ON_ERROR:
836
837 if (EFI_ERROR (Status)) {
838
839 Ip4CleanProtocol (IpInstance);
840
841 FreePool (IpInstance);
842 }
843
844 return Status;
845 }
846
847
848 /**
849 Destroys a child handle with a protocol installed on it.
850
851 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
852 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
853 last protocol on ChildHandle, then ChildHandle is destroyed.
854
855 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
856 @param ChildHandle Handle of the child to destroy
857
858 @retval EFI_SUCCES The protocol was removed from ChildHandle.
859 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
860 @retval EFI_INVALID_PARAMETER Child handle is NULL.
861 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
862 because its services are being used.
863 @retval other The child handle was not destroyed
864
865 **/
866 EFI_STATUS
867 EFIAPI
868 Ip4ServiceBindingDestroyChild (
869 IN EFI_SERVICE_BINDING_PROTOCOL *This,
870 IN EFI_HANDLE ChildHandle
871 )
872 {
873 EFI_STATUS Status;
874 IP4_SERVICE *IpSb;
875 IP4_PROTOCOL *IpInstance;
876 EFI_IP4_PROTOCOL *Ip4;
877 EFI_TPL OldTpl;
878 INTN State;
879
880 if ((This == NULL) || (ChildHandle == NULL)) {
881 return EFI_INVALID_PARAMETER;
882 }
883
884 //
885 // Retrieve the private context data structures
886 //
887 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
888
889 Status = gBS->OpenProtocol (
890 ChildHandle,
891 &gEfiIp4ProtocolGuid,
892 (VOID **) &Ip4,
893 gIp4DriverBinding.DriverBindingHandle,
894 ChildHandle,
895 EFI_OPEN_PROTOCOL_GET_PROTOCOL
896 );
897
898 if (EFI_ERROR (Status)) {
899 return EFI_UNSUPPORTED;
900 }
901
902 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (Ip4);
903
904 if (IpInstance->Service != IpSb) {
905 return EFI_INVALID_PARAMETER;
906 }
907
908 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
909
910 //
911 // A child can be destroyed more than once. For example,
912 // Ip4DriverBindingStop will destroy all of its children.
913 // when UDP driver is being stopped, it will destroy all
914 // the IP child it opens.
915 //
916 if (IpInstance->State == IP4_STATE_DESTROY) {
917 gBS->RestoreTPL (OldTpl);
918 return EFI_SUCCESS;
919 }
920
921 State = IpInstance->State;
922 IpInstance->State = IP4_STATE_DESTROY;
923
924 //
925 // Close the Managed Network protocol.
926 //
927 gBS->CloseProtocol (
928 IpSb->MnpChildHandle,
929 &gEfiManagedNetworkProtocolGuid,
930 gIp4DriverBinding.DriverBindingHandle,
931 ChildHandle
932 );
933
934 if (IpInstance->Interface != NULL && IpInstance->Interface->Arp != NULL) {
935 gBS->CloseProtocol (
936 IpInstance->Interface->ArpHandle,
937 &gEfiArpProtocolGuid,
938 gIp4DriverBinding.DriverBindingHandle,
939 ChildHandle
940 );
941 }
942
943 //
944 // Uninstall the IP4 protocol first. Many thing happens during
945 // this:
946 // 1. The consumer of the IP4 protocol will be stopped if it
947 // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is
948 // stopped, IP driver's stop function will be called, and uninstall
949 // EFI_IP4_PROTOCOL will trigger the UDP's stop function. This
950 // makes it possible to create the network stack bottom up, and
951 // stop it top down.
952 // 2. the upper layer will recycle the received packet. The recycle
953 // event's TPL is higher than this function. The recycle events
954 // will be called back before preceeding. If any packets not recycled,
955 // that means there is a resource leak.
956 //
957 gBS->RestoreTPL (OldTpl);
958 Status = gBS->UninstallProtocolInterface (
959 ChildHandle,
960 &gEfiIp4ProtocolGuid,
961 &IpInstance->Ip4Proto
962 );
963 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
964 if (EFI_ERROR (Status)) {
965 goto ON_ERROR;
966 }
967
968 Status = Ip4CleanProtocol (IpInstance);
969 if (EFI_ERROR (Status)) {
970 gBS->InstallMultipleProtocolInterfaces (
971 &ChildHandle,
972 &gEfiIp4ProtocolGuid,
973 Ip4,
974 NULL
975 );
976
977 goto ON_ERROR;
978 }
979
980 RemoveEntryList (&IpInstance->Link);
981 IpSb->NumChildren--;
982
983 gBS->RestoreTPL (OldTpl);
984
985 FreePool (IpInstance);
986 return EFI_SUCCESS;
987
988 ON_ERROR:
989 IpInstance->State = State;
990 gBS->RestoreTPL (OldTpl);
991
992 return Status;
993 }