]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/Tcp4Dxe/SockInterface.c
MdeModulePkg/Ip4Dxe: Cleanup the resource after error happen during Ip4StartAutoConfig().
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Tcp4Dxe / SockInterface.c
index c0756954674acf868ce13c48243ff237a9866827..bca4b02009df8da0ba7a2d995e3e63881abd7db9 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
+  Interface function of the Socket.\r
 \r
-Copyright (c) 2005 - 2006, Intel Corporation<BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 http://opensource.org/licenses/bsd-license.php<BR>\r
@@ -22,7 +23,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
   @param  Event                The event to be checked.\r
 \r
   @retval  TRUE                The specific Event exists in the List. \r
-  @retval  False               The specific Event is not in the List.\r
+  @retval  FALSE               The specific Event is not in the List.\r
 \r
 **/\r
 BOOLEAN\r
@@ -57,7 +58,8 @@ SockTokenExistedInList (
   @param  Sock                 Pointer to the instance's socket.\r
   @param  Event                The event to be checked.\r
 \r
-  @return The specific Event exists in one of socket's lists or not.\r
+  @retval  TRUE                The Event exists in related socket's lists. \r
+  @retval  FALSE               The Event is not in related socket's lists.\r
 \r
 **/\r
 BOOLEAN\r
@@ -129,7 +131,7 @@ SockBufferToken (
 \r
 \r
 /**\r
-  Destory the socket Sock and its associated protocol control block.\r
+  Destroy the socket Sock and its associated protocol control block.\r
 \r
   @param  Sock                 The socket to be destroyed.\r
 \r
@@ -139,18 +141,69 @@ SockBufferToken (
 **/\r
 EFI_STATUS\r
 SockDestroyChild (\r
-  IN   SOCKET *Sock\r
+  IN SOCKET *Sock\r
   )\r
 {\r
-  EFI_STATUS  Status;\r
+  EFI_STATUS       Status;\r
+  TCP4_PROTO_DATA  *ProtoData;\r
+  TCP_CB           *Tcb;\r
+  VOID             *SockProtocol;\r
 \r
   ASSERT ((Sock != NULL) && (Sock->ProtoHandler != NULL));\r
 \r
-  if (Sock->IsDestroyed) {\r
+  if (Sock->InDestroy) {\r
     return EFI_SUCCESS;\r
   }\r
 \r
-  Sock->IsDestroyed = TRUE;\r
+  Sock->InDestroy = TRUE;\r
+\r
+  ProtoData = (TCP4_PROTO_DATA *) Sock->ProtoReserved;\r
+  Tcb       = ProtoData->TcpPcb;\r
+\r
+  ASSERT (Tcb != NULL);\r
+\r
+  //\r
+  // Close the IP protocol.\r
+  //\r
+  gBS->CloseProtocol (\r
+         Tcb->IpInfo->ChildHandle,\r
+         &gEfiIp4ProtocolGuid,\r
+         ProtoData->TcpService->IpIo->Image,\r
+         Sock->SockHandle\r
+         );\r
+\r
+  if (Sock->DestroyCallback != NULL) {\r
+    Sock->DestroyCallback (Sock, Sock->Context);\r
+  }\r
+\r
+  //\r
+  // Retrieve the protocol installed on this sock\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  Sock->SockHandle,\r
+                  &gEfiTcp4ProtocolGuid,\r
+                  &SockProtocol,\r
+                  Sock->DriverBinding,\r
+                  Sock->SockHandle,\r
+                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                  );\r
+\r
+  if (EFI_ERROR (Status)) {\r
+\r
+    DEBUG ((EFI_D_ERROR, "SockDestroyChild: Open protocol installed "\r
+      "on socket failed with %r\n", Status));\r
+  }\r
+\r
+  //\r
+  // Uninstall the protocol installed on this sock\r
+  // in the light of Sock->SockType\r
+  //\r
+  gBS->UninstallMultipleProtocolInterfaces (\r
+        Sock->SockHandle,\r
+        &gEfiTcp4ProtocolGuid,\r
+        SockProtocol,\r
+        NULL\r
+        );\r
 \r
   Status = EfiAcquireLockOrFail (&(Sock->Lock));\r
   if (EFI_ERROR (Status)) {\r
@@ -171,7 +224,7 @@ SockDestroyChild (
     DEBUG ((EFI_D_ERROR, "SockDestroyChild: Protocol detach socket"\r
       " failed with %r\n", Status));\r
 \r
-    Sock->IsDestroyed = FALSE;\r
+    Sock->InDestroy = FALSE;\r
   } else if (SOCK_IS_CONFIGURED (Sock)) {\r
 \r
     SockConnFlush (Sock);\r
@@ -207,6 +260,7 @@ SockCreateChild (
   )\r
 {\r
   SOCKET      *Sock;\r
+  VOID        *SockProtocol;\r
   EFI_STATUS  Status;\r
 \r
   //\r
@@ -227,25 +281,49 @@ SockCreateChild (
     DEBUG ((EFI_D_ERROR, "SockCreateChild: Get the lock to "\r
       "access socket failed with %r\n", Status));\r
 \r
-    SockDestroy (Sock);\r
-    return NULL;\r
+    goto ERROR;\r
   }\r
   //\r
   // inform the protocol layer to attach the socket\r
   // with a new protocol control block\r
   //\r
   Status = Sock->ProtoHandler (Sock, SOCK_ATTACH, NULL);\r
+  EfiReleaseLock (&(Sock->Lock));\r
   if (EFI_ERROR (Status)) {\r
 \r
     DEBUG ((EFI_D_ERROR, "SockCreateChild: Protocol failed to"\r
       " attach a socket with %r\n", Status));\r
 \r
-    SockDestroy (Sock);\r
-    Sock = NULL;\r
+    goto ERROR;\r
   }\r
 \r
-  EfiReleaseLock (&(Sock->Lock));\r
   return Sock;\r
+\r
+ERROR:\r
+\r
+  if (Sock->DestroyCallback != NULL) {\r
+    Sock->DestroyCallback (Sock, Sock->Context);\r
+  }\r
+\r
+  gBS->OpenProtocol (\r
+         Sock->SockHandle,\r
+         &gEfiTcp4ProtocolGuid,\r
+         &SockProtocol,\r
+         Sock->DriverBinding,\r
+         Sock->SockHandle,\r
+         EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+         );\r
+  //\r
+  // Uninstall the protocol installed on this sock\r
+  //\r
+  gBS->UninstallMultipleProtocolInterfaces (\r
+        Sock->SockHandle,\r
+        &gEfiTcp4ProtocolGuid,\r
+        SockProtocol,\r
+        NULL\r
+        );\r
+   SockDestroy (Sock);\r
+   return NULL;\r
 }\r
 \r
 \r
@@ -395,7 +473,7 @@ SockAccept (
   SOCKET                *Socket;\r
   EFI_EVENT             Event;\r
 \r
-  ASSERT (SOCK_STREAM == Sock->Type);\r
+  ASSERT (SockStream == Sock->Type);\r
 \r
   Status = EfiAcquireLockOrFail (&(Sock->Lock));\r
   if (EFI_ERROR (Status)) {\r
@@ -451,7 +529,7 @@ SockAccept (
       Socket->Parent->ConnCnt--;\r
 \r
       DEBUG (\r
-        (EFI_D_INFO,\r
+        (EFI_D_NET,\r
         "SockAccept: Accept a socket, now conncount is %d",\r
         Socket->Parent->ConnCnt)\r
         );\r
@@ -508,7 +586,7 @@ SockSend (
   SOCK_TOKEN              *SockToken;\r
   UINT32                  DataLen;\r
 \r
-  ASSERT (SOCK_STREAM == Sock->Type);\r
+  ASSERT (SockStream == Sock->Type);\r
 \r
   Status = EfiAcquireLockOrFail (&(Sock->Lock));\r
   if (EFI_ERROR (Status)) {\r
@@ -591,7 +669,7 @@ SockSend (
         "Snd Data\n", Status));\r
 \r
       RemoveEntryList (&(SockToken->TokenList));\r
-      gBS->FreePool (SockToken);\r
+      FreePool (SockToken);\r
     }\r
   }\r
 \r
@@ -631,7 +709,7 @@ SockRcv (
   EFI_STATUS    Status;\r
   EFI_EVENT     Event;\r
 \r
-  ASSERT (SOCK_STREAM == Sock->Type);\r
+  ASSERT (SockStream == Sock->Type);\r
 \r
   Status = EfiAcquireLockOrFail (&(Sock->Lock));\r
   if (EFI_ERROR (Status)) {\r
@@ -695,11 +773,7 @@ SockRcv (
   }\r
 \r
   if (RcvdBytes != 0) {\r
-    Status = SockProcessRcvToken (Sock, RcvToken);\r
-\r
-    if (EFI_ERROR (Status)) {\r
-      goto Exit;\r
-    }\r
+    SockProcessRcvToken (Sock, RcvToken);\r
 \r
     Status = Sock->ProtoHandler (Sock, SOCK_CONSUMED, NULL);\r
   } else {\r
@@ -731,7 +805,7 @@ SockFlush (
 {\r
   EFI_STATUS  Status;\r
 \r
-  ASSERT (SOCK_STREAM == Sock->Type);\r
+  ASSERT (SockStream == Sock->Type);\r
 \r
   Status = EfiAcquireLockOrFail (&(Sock->Lock));\r
   if (EFI_ERROR (Status)) {\r
@@ -796,7 +870,7 @@ SockClose (
   EFI_STATUS  Status;\r
   EFI_EVENT   Event;\r
 \r
-  ASSERT (SOCK_STREAM == Sock->Type);\r
+  ASSERT (SockStream == Sock->Type);\r
 \r
   Status = EfiAcquireLockOrFail (&(Sock->Lock));\r
   if (EFI_ERROR (Status)) {\r