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