MdeModulePkg: Update Ip4Dxe driver to support Ip4Config2 protocol,
[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 - 2015, 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 service 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 = AllocateZeroPool (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
213 //
214 // Create various resources. First create the route table, timer
215 // event and MNP child. IGMP, interface's initialization depend
216 // on the MNP child.
217 //
218 IpSb->DefaultRouteTable = Ip4CreateRouteTable ();
219
220 if (IpSb->DefaultRouteTable == NULL) {
221 Status = EFI_OUT_OF_RESOURCES;
222 goto ON_ERROR;
223 }
224
225 Status = gBS->CreateEvent (
226 EVT_NOTIFY_SIGNAL | EVT_TIMER,
227 TPL_CALLBACK,
228 Ip4TimerTicking,
229 IpSb,
230 &IpSb->Timer
231 );
232
233 if (EFI_ERROR (Status)) {
234 goto ON_ERROR;
235 }
236
237 Status = NetLibCreateServiceChild (
238 Controller,
239 ImageHandle,
240 &gEfiManagedNetworkServiceBindingProtocolGuid,
241 &IpSb->MnpChildHandle
242 );
243
244 if (EFI_ERROR (Status)) {
245 goto ON_ERROR;
246 }
247
248 Status = gBS->OpenProtocol (
249 IpSb->MnpChildHandle,
250 &gEfiManagedNetworkProtocolGuid,
251 (VOID **) &IpSb->Mnp,
252 ImageHandle,
253 Controller,
254 EFI_OPEN_PROTOCOL_BY_DRIVER
255 );
256
257 if (EFI_ERROR (Status)) {
258 goto ON_ERROR;
259 }
260
261 Status = Ip4ServiceConfigMnp (IpSb, TRUE);
262
263 if (EFI_ERROR (Status)) {
264 goto ON_ERROR;
265 }
266
267 Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &IpSb->SnpMode);
268
269 if (EFI_ERROR (Status)) {
270 goto ON_ERROR;
271 }
272
273 Status = Ip4InitIgmp (IpSb);
274
275 if (EFI_ERROR (Status)) {
276 goto ON_ERROR;
277 }
278
279 IpSb->MacString = NULL;
280 Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &IpSb->MacString);
281
282 if (EFI_ERROR (Status)) {
283 goto ON_ERROR;
284 }
285
286 IpSb->DefaultInterface = Ip4CreateInterface (IpSb->Mnp, Controller, ImageHandle);
287
288 if (IpSb->DefaultInterface == NULL) {
289 Status = EFI_OUT_OF_RESOURCES;
290 goto ON_ERROR;
291 }
292
293 InsertHeadList (&IpSb->Interfaces, &IpSb->DefaultInterface->Link);
294
295 ZeroMem (&IpSb->Ip4Config2Instance, sizeof (IP4_CONFIG2_INSTANCE));
296
297 Status = Ip4Config2InitInstance (&IpSb->Ip4Config2Instance);
298
299 if (EFI_ERROR (Status)) {
300 goto ON_ERROR;
301 }
302
303 IpSb->MaxPacketSize = IpSb->SnpMode.MaxPacketSize - sizeof (IP4_HEAD);
304 if (NetLibGetVlanId (IpSb->Controller) != 0) {
305 //
306 // This is a VLAN device, reduce MTU by VLAN tag length
307 //
308 IpSb->MaxPacketSize -= NET_VLAN_TAG_LEN;
309 }
310 IpSb->OldMaxPacketSize = IpSb->MaxPacketSize;
311 *Service = IpSb;
312
313 return EFI_SUCCESS;
314
315 ON_ERROR:
316 Ip4CleanService (IpSb);
317 FreePool (IpSb);
318
319 return Status;
320 }
321
322
323 /**
324 Clean up a IP4 service binding instance. It will release all
325 the resource allocated by the instance. The instance may be
326 partly initialized, or partly destroyed. If a resource is
327 destroyed, it is marked as that in case the destroy failed and
328 being called again later.
329
330 @param[in] IpSb The IP4 service binding instance to clean up
331
332 @retval EFI_SUCCESS The resource used by the instance are cleaned up
333 @retval other Failed to clean up some of the resources.
334
335 **/
336 EFI_STATUS
337 Ip4CleanService (
338 IN IP4_SERVICE *IpSb
339 )
340 {
341 EFI_STATUS Status;
342
343 if (IpSb->DefaultInterface != NULL) {
344 Status = Ip4FreeInterface (IpSb->DefaultInterface, NULL);
345
346 if (EFI_ERROR (Status)) {
347 return Status;
348 }
349
350 IpSb->DefaultInterface = NULL;
351 }
352
353 if (IpSb->DefaultRouteTable != NULL) {
354 Ip4FreeRouteTable (IpSb->DefaultRouteTable);
355 IpSb->DefaultRouteTable = NULL;
356 }
357
358 Ip4CleanAssembleTable (&IpSb->Assemble);
359
360 if (IpSb->MnpChildHandle != NULL) {
361 if (IpSb->Mnp != NULL) {
362 gBS->CloseProtocol (
363 IpSb->MnpChildHandle,
364 &gEfiManagedNetworkProtocolGuid,
365 IpSb->Image,
366 IpSb->Controller
367 );
368
369 IpSb->Mnp = NULL;
370 }
371
372 NetLibDestroyServiceChild (
373 IpSb->Controller,
374 IpSb->Image,
375 &gEfiManagedNetworkServiceBindingProtocolGuid,
376 IpSb->MnpChildHandle
377 );
378
379 IpSb->MnpChildHandle = NULL;
380 }
381
382 if (IpSb->Timer != NULL) {
383 gBS->SetTimer (IpSb->Timer, TimerCancel, 0);
384 gBS->CloseEvent (IpSb->Timer);
385
386 IpSb->Timer = NULL;
387 }
388
389 if (IpSb->MacString != NULL) {
390 FreePool (IpSb->MacString);
391 }
392
393 Ip4Config2CleanInstance (&IpSb->Ip4Config2Instance);
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 EFI_STATUS Status;
463 IP4_SERVICE *IpSb;
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
487 ASSERT (IpSb != NULL);
488
489 //
490 // Install the Ip4ServiceBinding Protocol onto ControlerHandle
491 //
492 Status = gBS->InstallMultipleProtocolInterfaces (
493 &ControllerHandle,
494 &gEfiIp4ServiceBindingProtocolGuid,
495 &IpSb->ServiceBinding,
496 &gEfiIp4Config2ProtocolGuid,
497 &IpSb->Ip4Config2Instance.Ip4Config2,
498 NULL
499 );
500
501 if (EFI_ERROR (Status)) {
502 goto FREE_SERVICE;
503 }
504
505 //
506 // Ready to go: start the receiving and timer.
507 // Ip4Config2SetPolicy maybe call Ip4ReceiveFrame() to set the default interface's RecvRequest first after
508 // Ip4Config2 instance is initialized. So, EFI_ALREADY_STARTED is the allowed return status.
509 //
510 Status = Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);
511
512 if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
513 goto UNINSTALL_PROTOCOL;
514 }
515
516 Status = gBS->SetTimer (IpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);
517
518 if (EFI_ERROR (Status)) {
519 goto UNINSTALL_PROTOCOL;
520 }
521
522 //
523 // Initialize the IP4 ID
524 //
525 mIp4Id = (UINT16)NET_RANDOM (NetRandomInitSeed ());
526
527 return Status;
528
529 UNINSTALL_PROTOCOL:
530 gBS->UninstallProtocolInterface (
531 ControllerHandle,
532 &gEfiIp4ServiceBindingProtocolGuid,
533 &IpSb->ServiceBinding
534 );
535
536 FREE_SERVICE:
537 Ip4CleanService (IpSb);
538 FreePool (IpSb);
539 return Status;
540 }
541
542
543 /**
544 Stop this driver on ControllerHandle. This service is called by the
545 EFI boot service DisconnectController(). In order to
546 make drivers as small as possible, there are a few calling
547 restrictions for this service. DisconnectController()
548 must follow these calling restrictions. If any other agent wishes
549 to call Stop() it must also follow these calling restrictions.
550
551 @param[in] This Protocol instance pointer.
552 @param[in] ControllerHandle Handle of device to stop driver on
553 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number
554 of children is zero stop the entire bus driver.
555 @param[in] ChildHandleBuffer List of Child Handles to Stop.
556
557 @retval EFI_SUCCESS This driver is removed ControllerHandle
558 @retval other This driver was not removed from this device
559
560 **/
561 EFI_STATUS
562 EFIAPI
563 Ip4DriverBindingStop (
564 IN EFI_DRIVER_BINDING_PROTOCOL *This,
565 IN EFI_HANDLE ControllerHandle,
566 IN UINTN NumberOfChildren,
567 IN EFI_HANDLE *ChildHandleBuffer
568 )
569 {
570 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
571 IP4_SERVICE *IpSb;
572 EFI_HANDLE NicHandle;
573 EFI_STATUS Status;
574 INTN State;
575 LIST_ENTRY *List;
576 IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context;
577 IP4_INTERFACE *IpIf;
578 IP4_ROUTE_TABLE *RouteTable;
579
580 BOOLEAN IsDhcp4;
581
582 IsDhcp4 = FALSE;
583
584 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);
585 if (NicHandle == NULL) {
586 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiArpProtocolGuid);
587 if (NicHandle == NULL) {
588 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp4ProtocolGuid);
589 if (NicHandle != NULL) {
590 IsDhcp4 = TRUE;
591 } else {
592 return EFI_SUCCESS;
593 }
594 }
595 }
596
597 Status = gBS->OpenProtocol (
598 NicHandle,
599 &gEfiIp4ServiceBindingProtocolGuid,
600 (VOID **) &ServiceBinding,
601 This->DriverBindingHandle,
602 NicHandle,
603 EFI_OPEN_PROTOCOL_GET_PROTOCOL
604 );
605 if (EFI_ERROR (Status)) {
606 return EFI_DEVICE_ERROR;
607 }
608
609 IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);
610
611 if (IsDhcp4) {
612 Status = Ip4Config2DestroyDhcp4 (&IpSb->Ip4Config2Instance);
613 gBS->CloseEvent (IpSb->Ip4Config2Instance.Dhcp4Event);
614 IpSb->Ip4Config2Instance.Dhcp4Event = NULL;
615 } else if (NumberOfChildren != 0) {
616 List = &IpSb->Children;
617 Context.ServiceBinding = ServiceBinding;
618 Context.NumberOfChildren = NumberOfChildren;
619 Context.ChildHandleBuffer = ChildHandleBuffer;
620 Status = NetDestroyLinkList (
621 List,
622 Ip4DestroyChildEntryInHandleBuffer,
623 &Context,
624 NULL
625 );
626 } else if (IpSb->DefaultInterface->ArpHandle == ControllerHandle) {
627
628 //
629 // The ARP protocol for the default interface is being uninstalled and all
630 // its IP child handles should have been destroyed before. So, release the
631 // default interface and route table, create a new one and mark it as not started.
632 //
633 Ip4CancelReceive (IpSb->DefaultInterface);
634 Ip4FreeInterface (IpSb->DefaultInterface, NULL);
635 Ip4FreeRouteTable (IpSb->DefaultRouteTable);
636
637 IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);
638 if (IpIf == NULL) {
639 goto ON_ERROR;
640 }
641 RouteTable = Ip4CreateRouteTable ();
642 if (RouteTable == NULL) {
643 Ip4FreeInterface (IpIf, NULL);
644 goto ON_ERROR;;
645 }
646
647 IpSb->DefaultInterface = IpIf;
648 InsertHeadList (&IpSb->Interfaces, &IpIf->Link);
649 IpSb->DefaultRouteTable = RouteTable;
650 Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb);
651
652 IpSb->State = IP4_SERVICE_UNSTARTED;
653
654 } else if (IsListEmpty (&IpSb->Children)) {
655 State = IpSb->State;
656 IpSb->State = IP4_SERVICE_DESTROY;
657
658 //
659 // OK, clean other resources then uninstall the service binding protocol.
660 //
661 Status = Ip4CleanService (IpSb);
662 if (EFI_ERROR (Status)) {
663 IpSb->State = State;
664 goto ON_ERROR;
665 }
666
667 gBS->UninstallMultipleProtocolInterfaces (
668 NicHandle,
669 &gEfiIp4ServiceBindingProtocolGuid,
670 ServiceBinding,
671 &gEfiIp4Config2ProtocolGuid,
672 &IpSb->Ip4Config2Instance.Ip4Config2,
673 NULL
674 );
675
676 if (gIp4ControllerNameTable != NULL) {
677 FreeUnicodeStringTable (gIp4ControllerNameTable);
678 gIp4ControllerNameTable = NULL;
679 }
680 FreePool (IpSb);
681 }
682
683 ON_ERROR:
684 return Status;
685 }
686
687
688 /**
689 Creates a child handle and installs a protocol.
690
691 The CreateChild() function installs a protocol on ChildHandle.
692 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
693 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
694
695 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
696 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
697 then a new handle is created. If it is a pointer to an existing UEFI handle,
698 then the protocol is added to the existing UEFI handle.
699
700 @retval EFI_SUCCES The protocol was added to ChildHandle.
701 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
702 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
703 the child
704 @retval other The child handle was not created
705
706 **/
707 EFI_STATUS
708 EFIAPI
709 Ip4ServiceBindingCreateChild (
710 IN EFI_SERVICE_BINDING_PROTOCOL *This,
711 IN OUT EFI_HANDLE *ChildHandle
712 )
713 {
714 IP4_SERVICE *IpSb;
715 IP4_PROTOCOL *IpInstance;
716 EFI_TPL OldTpl;
717 EFI_STATUS Status;
718 VOID *Mnp;
719
720 if ((This == NULL) || (ChildHandle == NULL)) {
721 return EFI_INVALID_PARAMETER;
722 }
723
724 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
725 IpInstance = AllocatePool (sizeof (IP4_PROTOCOL));
726
727 if (IpInstance == NULL) {
728 return EFI_OUT_OF_RESOURCES;
729 }
730
731 Ip4InitProtocol (IpSb, IpInstance);
732
733 //
734 // Install Ip4 onto ChildHandle
735 //
736 Status = gBS->InstallMultipleProtocolInterfaces (
737 ChildHandle,
738 &gEfiIp4ProtocolGuid,
739 &IpInstance->Ip4Proto,
740 NULL
741 );
742
743 if (EFI_ERROR (Status)) {
744 goto ON_ERROR;
745 }
746
747 IpInstance->Handle = *ChildHandle;
748
749 //
750 // Open the Managed Network protocol BY_CHILD.
751 //
752 Status = gBS->OpenProtocol (
753 IpSb->MnpChildHandle,
754 &gEfiManagedNetworkProtocolGuid,
755 (VOID **) &Mnp,
756 gIp4DriverBinding.DriverBindingHandle,
757 IpInstance->Handle,
758 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
759 );
760 if (EFI_ERROR (Status)) {
761 gBS->UninstallMultipleProtocolInterfaces (
762 ChildHandle,
763 &gEfiIp4ProtocolGuid,
764 &IpInstance->Ip4Proto,
765 NULL
766 );
767
768 goto ON_ERROR;
769 }
770
771 //
772 // Insert it into the service binding instance.
773 //
774 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
775
776 InsertTailList (&IpSb->Children, &IpInstance->Link);
777 IpSb->NumChildren++;
778
779 gBS->RestoreTPL (OldTpl);
780
781 ON_ERROR:
782
783 if (EFI_ERROR (Status)) {
784
785 Ip4CleanProtocol (IpInstance);
786
787 FreePool (IpInstance);
788 }
789
790 return Status;
791 }
792
793
794 /**
795 Destroys a child handle with a protocol installed on it.
796
797 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
798 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
799 last protocol on ChildHandle, then ChildHandle is destroyed.
800
801 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
802 @param ChildHandle Handle of the child to destroy
803
804 @retval EFI_SUCCES The protocol was removed from ChildHandle.
805 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
806 @retval EFI_INVALID_PARAMETER Child handle is NULL.
807 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
808 because its services are being used.
809 @retval other The child handle was not destroyed
810
811 **/
812 EFI_STATUS
813 EFIAPI
814 Ip4ServiceBindingDestroyChild (
815 IN EFI_SERVICE_BINDING_PROTOCOL *This,
816 IN EFI_HANDLE ChildHandle
817 )
818 {
819 EFI_STATUS Status;
820 IP4_SERVICE *IpSb;
821 IP4_PROTOCOL *IpInstance;
822 EFI_IP4_PROTOCOL *Ip4;
823 EFI_TPL OldTpl;
824 INTN State;
825
826 if ((This == NULL) || (ChildHandle == NULL)) {
827 return EFI_INVALID_PARAMETER;
828 }
829
830 //
831 // Retrieve the private context data structures
832 //
833 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
834
835 Status = gBS->OpenProtocol (
836 ChildHandle,
837 &gEfiIp4ProtocolGuid,
838 (VOID **) &Ip4,
839 gIp4DriverBinding.DriverBindingHandle,
840 ChildHandle,
841 EFI_OPEN_PROTOCOL_GET_PROTOCOL
842 );
843
844 if (EFI_ERROR (Status)) {
845 return EFI_UNSUPPORTED;
846 }
847
848 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (Ip4);
849
850 if (IpInstance->Service != IpSb) {
851 return EFI_INVALID_PARAMETER;
852 }
853
854 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
855
856 //
857 // A child can be destroyed more than once. For example,
858 // Ip4DriverBindingStop will destroy all of its children.
859 // when UDP driver is being stopped, it will destroy all
860 // the IP child it opens.
861 //
862 if (IpInstance->State == IP4_STATE_DESTROY) {
863 gBS->RestoreTPL (OldTpl);
864 return EFI_SUCCESS;
865 }
866
867 State = IpInstance->State;
868 IpInstance->State = IP4_STATE_DESTROY;
869
870 //
871 // Close the Managed Network protocol.
872 //
873 gBS->CloseProtocol (
874 IpSb->MnpChildHandle,
875 &gEfiManagedNetworkProtocolGuid,
876 gIp4DriverBinding.DriverBindingHandle,
877 ChildHandle
878 );
879
880 if (IpInstance->Interface != NULL && IpInstance->Interface->Arp != NULL) {
881 gBS->CloseProtocol (
882 IpInstance->Interface->ArpHandle,
883 &gEfiArpProtocolGuid,
884 gIp4DriverBinding.DriverBindingHandle,
885 ChildHandle
886 );
887 }
888
889 //
890 // Uninstall the IP4 protocol first. Many thing happens during
891 // this:
892 // 1. The consumer of the IP4 protocol will be stopped if it
893 // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is
894 // stopped, IP driver's stop function will be called, and uninstall
895 // EFI_IP4_PROTOCOL will trigger the UDP's stop function. This
896 // makes it possible to create the network stack bottom up, and
897 // stop it top down.
898 // 2. the upper layer will recycle the received packet. The recycle
899 // event's TPL is higher than this function. The recycle events
900 // will be called back before preceeding. If any packets not recycled,
901 // that means there is a resource leak.
902 //
903 gBS->RestoreTPL (OldTpl);
904 Status = gBS->UninstallProtocolInterface (
905 ChildHandle,
906 &gEfiIp4ProtocolGuid,
907 &IpInstance->Ip4Proto
908 );
909 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
910 if (EFI_ERROR (Status)) {
911 goto ON_ERROR;
912 }
913
914 Status = Ip4CleanProtocol (IpInstance);
915 if (EFI_ERROR (Status)) {
916 gBS->InstallMultipleProtocolInterfaces (
917 &ChildHandle,
918 &gEfiIp4ProtocolGuid,
919 Ip4,
920 NULL
921 );
922
923 goto ON_ERROR;
924 }
925
926 RemoveEntryList (&IpInstance->Link);
927 IpSb->NumChildren--;
928
929 gBS->RestoreTPL (OldTpl);
930
931 FreePool (IpInstance);
932 return EFI_SUCCESS;
933
934 ON_ERROR:
935 IpInstance->State = State;
936 gBS->RestoreTPL (OldTpl);
937
938 return Status;
939 }