]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Icmp.c
MdeModulePkg: Update Ip4Dxe driver to support Ip4Config2 protocol,
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Icmp.c
index 7f29c90869c90b915ba042915930f0fc621f1ce9..b4b086496cf048873a1ad3b5f6c6f4e9a6c58cad 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 \r
-Copyright (c) 2005 - 2006, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2005 - 2015, 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\r
@@ -9,14 +9,6 @@ http://opensource.org/licenses/bsd-license.php
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-\r
-Module Name:\r
-\r
-  Ip4Icmp.c\r
-\r
-Abstract:\r
-\r
-\r
 **/\r
 \r
 #include "Ip4Impl.h"\r
@@ -43,36 +35,36 @@ mIcmpClass[] = {
 };\r
 \r
 EFI_IP4_ICMP_TYPE\r
-mIp4SupportedIcmp [23] = {\r
-  {ICMP_ECHO_REPLY,        ICMP_DEFAULT_CODE      },\r
-\r
-  {ICMP_DEST_UNREACHABLE,  ICMP_NET_UNREACHABLE   },\r
-  {ICMP_DEST_UNREACHABLE,  ICMP_HOST_UNREACHABLE  },\r
-  {ICMP_DEST_UNREACHABLE,  ICMP_PROTO_UNREACHABLE },\r
-  {ICMP_DEST_UNREACHABLE,  ICMP_PORT_UNREACHABLE  },\r
-  {ICMP_DEST_UNREACHABLE,  ICMP_FRAGMENT_FAILED   },\r
-  {ICMP_DEST_UNREACHABLE,  ICMP_SOURCEROUTE_FAILED},\r
-  {ICMP_DEST_UNREACHABLE,  ICMP_NET_UNKNOWN       },\r
-  {ICMP_DEST_UNREACHABLE,  ICMP_HOST_UNKNOWN      },\r
-  {ICMP_DEST_UNREACHABLE,  ICMP_SOURCE_ISOLATED   },\r
-  {ICMP_DEST_UNREACHABLE,  ICMP_NET_PROHIBITED    },\r
-  {ICMP_DEST_UNREACHABLE,  ICMP_HOST_PROHIBITED   },\r
+mIp4SupportedIcmp[23] = {\r
+  {ICMP_ECHO_REPLY,        ICMP_DEFAULT_CODE        },\r
+\r
+  {ICMP_DEST_UNREACHABLE,  ICMP_NET_UNREACHABLE     },\r
+  {ICMP_DEST_UNREACHABLE,  ICMP_HOST_UNREACHABLE    },\r
+  {ICMP_DEST_UNREACHABLE,  ICMP_PROTO_UNREACHABLE   },\r
+  {ICMP_DEST_UNREACHABLE,  ICMP_PORT_UNREACHABLE    },\r
+  {ICMP_DEST_UNREACHABLE,  ICMP_FRAGMENT_FAILED     },\r
+  {ICMP_DEST_UNREACHABLE,  ICMP_SOURCEROUTE_FAILED  },\r
+  {ICMP_DEST_UNREACHABLE,  ICMP_NET_UNKNOWN         },\r
+  {ICMP_DEST_UNREACHABLE,  ICMP_HOST_UNKNOWN        },\r
+  {ICMP_DEST_UNREACHABLE,  ICMP_SOURCE_ISOLATED     },\r
+  {ICMP_DEST_UNREACHABLE,  ICMP_NET_PROHIBITED      },\r
+  {ICMP_DEST_UNREACHABLE,  ICMP_HOST_PROHIBITED     },\r
   {ICMP_DEST_UNREACHABLE,  ICMP_NET_UNREACHABLE_TOS },\r
   {ICMP_DEST_UNREACHABLE,  ICMP_HOST_UNREACHABLE_TOS},\r
 \r
-  {ICMP_SOURCE_QUENCH,     ICMP_DEFAULT_CODE      },\r
+  {ICMP_SOURCE_QUENCH,     ICMP_DEFAULT_CODE        },\r
 \r
-  {ICMP_REDIRECT,          ICMP_NET_REDIRECT      },\r
-  {ICMP_REDIRECT,          ICMP_HOST_REDIRECT     },\r
-  {ICMP_REDIRECT,          ICMP_NET_TOS_REDIRECT  },\r
-  {ICMP_REDIRECT,          ICMP_HOST_TOS_REDIRECT },\r
+  {ICMP_REDIRECT,          ICMP_NET_REDIRECT        },\r
+  {ICMP_REDIRECT,          ICMP_HOST_REDIRECT       },\r
+  {ICMP_REDIRECT,          ICMP_NET_TOS_REDIRECT    },\r
+  {ICMP_REDIRECT,          ICMP_HOST_TOS_REDIRECT   },\r
 \r
-  {ICMP_ECHO_REQUEST,      ICMP_DEFAULT_CODE      },\r
+  {ICMP_ECHO_REQUEST,      ICMP_DEFAULT_CODE        },\r
 \r
-  {ICMP_TIME_EXCEEDED,     ICMP_TIMEOUT_IN_TRANSIT},\r
-  {ICMP_TIME_EXCEEDED,     ICMp_TIMEOUT_REASSEMBLE},\r
+  {ICMP_TIME_EXCEEDED,     ICMP_TIMEOUT_IN_TRANSIT  },\r
+  {ICMP_TIME_EXCEEDED,     ICMP_TIMEOUT_REASSEMBLE  },\r
 \r
-  {ICMP_PARAMETER_PROBLEM, ICMP_DEFAULT_CODE      },\r
+  {ICMP_PARAMETER_PROBLEM, ICMP_DEFAULT_CODE        },\r
 };\r
 \r
 \r
@@ -80,6 +72,7 @@ mIp4SupportedIcmp [23] = {
 /**\r
   Process the ICMP redirect. Find the instance then update\r
   its route cache.\r
+  \r
   All kinds of redirect is treated as host redirect as\r
   specified by RFC1122 3.3.1.2:\r
   "Since the subnet mask appropriate to the destination\r
@@ -87,19 +80,18 @@ mIp4SupportedIcmp [23] = {
   message SHOULD be treated identically to a Host Redirect\r
   message;"\r
 \r
-  @param  IpSb                   The IP4 service binding instance that received the\r
-                                 packet\r
-  @param  Head                   The IP head of the received ICMPpacket.\r
-  @param  Packet                 The content of the ICMP redirect packet with IP\r
+  @param[in]  IpSb               The IP4 service binding instance that received \r
+                                 the packet.\r
+  @param[in]  Head               The IP head of the received ICMPpacket.\r
+  @param[in]  Packet             The content of the ICMP redirect packet with IP\r
                                  head removed.\r
-  @param  Icmp                   The buffer to store the ICMP error message if\r
+  @param[in]  Icmp               The buffer to store the ICMP error message if\r
                                  something is wrong.\r
 \r
   @retval EFI_INVALID_PARAMETER  The parameter is invalid\r
   @retval EFI_SUCCESS            Successfully updated the route caches\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 Ip4ProcessIcmpRedirect (\r
   IN IP4_SERVICE            *IpSb,\r
@@ -108,11 +100,13 @@ Ip4ProcessIcmpRedirect (
   IN IP4_ICMP_ERROR_HEAD    *Icmp\r
   )\r
 {\r
-  NET_LIST_ENTRY            *Entry;\r
+  LIST_ENTRY                *Entry;\r
   IP4_PROTOCOL              *Ip4Instance;\r
   IP4_ROUTE_CACHE_ENTRY     *CacheEntry;\r
   IP4_INTERFACE             *IpIf;\r
   IP4_ADDR                  Gateway;\r
+  IP4_ADDR                  Src;\r
+  IP4_ADDR                  Dst;\r
 \r
   //\r
   // Find the interface whose IP address is the source of the\r
@@ -141,11 +135,9 @@ Ip4ProcessIcmpRedirect (
       continue;\r
     }\r
 \r
-    CacheEntry = Ip4FindRouteCache (\r
-                   Ip4Instance->RouteTable,\r
-                   NTOHL (Icmp->IpHead.Dst),\r
-                   NTOHL (Icmp->IpHead.Src)\r
-                   );\r
+    Dst = NTOHL (Icmp->IpHead.Dst);\r
+    Src = NTOHL (Icmp->IpHead.Src);\r
+    CacheEntry = Ip4FindRouteCache (Ip4Instance->RouteTable, Dst, Src);\r
 \r
     //\r
     // Only update the route cache's gateway if the source of the\r
@@ -166,17 +158,16 @@ Ip4ProcessIcmpRedirect (
   update call Ip4ProcessIcmpRedirect to update the IP instance's\r
   route cache, otherwise, deliver the packet to upper layer.\r
 \r
-  @param  IpSb                   The IP service that received the packet.\r
-  @param  Head                   The IP head of the ICMP error packet\r
-  @param  Packet                 The content of the ICMP error with IP head\r
+  @param[in]  IpSb               The IP4 service that received the packet.\r
+  @param[in]  Head               The IP4 head of the ICMP error packet\r
+  @param[in]  Packet             The content of the ICMP error with IP4 head\r
                                  removed.\r
 \r
+  @retval EFI_SUCCESS            The ICMP error is processed successfully.\r
   @retval EFI_INVALID_PARAMETER  The packet is invalid\r
   @retval Others                 Failed to process the packet.\r
-  @retval EFI_SUCCESS            The ICMP error is processed successfully.\r
-\r
+  \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 Ip4ProcessIcmpError (\r
   IN IP4_SERVICE            *IpSb,\r
@@ -202,16 +193,16 @@ Ip4ProcessIcmpError (
   }\r
 \r
   IP4_GET_CLIP_INFO (Packet)->Status = EFI_ICMP_ERROR;\r
-  return Ip4Demultiplex (IpSb, Head, Packet);\r
+  return Ip4Demultiplex (IpSb, Head, Packet, NULL, 0);\r
 }\r
 \r
 \r
 /**\r
   Replay an ICMP echo request.\r
 \r
-  @param  IpSb                   The IP service that receivd the packet\r
-  @param  Head                   The IP head of the ICMP error packet\r
-  @param  Packet                 The content of the ICMP error with IP head\r
+  @param[in]  IpSb               The IP4 service that receivd the packet\r
+  @param[in]  Head               The IP4 head of the ICMP error packet\r
+  @param[in]  Packet             The content of the ICMP error with IP4 head\r
                                  removed.\r
 \r
   @retval EFI_OUT_OF_RESOURCES   Failed to allocate resource.\r
@@ -249,14 +240,15 @@ Ip4IcmpReplyEcho (
   // update is omitted.\r
   //\r
   Icmp                = (IP4_ICMP_QUERY_HEAD *) NetbufGetByte (Data, 0, NULL);\r
+  ASSERT (Icmp != NULL);\r
   Icmp->Head.Type     = ICMP_ECHO_REPLY;\r
   Icmp->Head.Checksum = 0;\r
-  Icmp->Head.Checksum = ~NetblockChecksum ((UINT8 *) Icmp, Data->TotalSize);\r
+  Icmp->Head.Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Icmp, Data->TotalSize));\r
 \r
   ReplyHead.Tos       = 0;\r
   ReplyHead.Fragment  = 0;\r
   ReplyHead.Ttl       = 64;\r
-  ReplyHead.Protocol  = IP4_PROTO_ICMP;\r
+  ReplyHead.Protocol  = EFI_IP_PROTO_ICMP;\r
   ReplyHead.Src       = 0;\r
 \r
   //\r
@@ -286,13 +278,14 @@ ON_EXIT:
   Process the ICMP query message. If it is an ICMP echo\r
   request, answer it. Otherwise deliver it to upper layer.\r
 \r
-  @param  IpSb                   The IP service that receivd the packet\r
-  @param  Head                   The IP head of the ICMP query packet\r
-  @param  Packet                 The content of the ICMP query with IP head\r
+  @param[in]  IpSb               The IP4 service that receivd the packet\r
+  @param[in]  Head               The IP4 head of the ICMP query packet\r
+  @param[in]  Packet             The content of the ICMP query with IP4 head\r
                                  removed.\r
 \r
   @retval EFI_INVALID_PARAMETER  The packet is invalid\r
   @retval EFI_SUCCESS            The ICMP query message is processed\r
+  @retval Others                 Failed to process ICMP query.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -315,7 +308,7 @@ Ip4ProcessIcmpQuery (
     return Ip4IcmpReplyEcho (IpSb, Head, Packet);\r
   }\r
 \r
-  return Ip4Demultiplex (IpSb, Head, Packet);\r
+  return Ip4Demultiplex (IpSb, Head, Packet, NULL, 0);\r
 }\r
 \r
 \r
@@ -324,13 +317,14 @@ Ip4ProcessIcmpQuery (
   then according to the message types, process it as query or\r
   error packet.\r
 \r
-  @param  IpSb                   The IP service that receivd the packet\r
-  @param  Head                   The IP head of the ICMP query packet\r
-  @param  Packet                 The content of the ICMP query with IP head\r
+  @param[in]  IpSb               The IP4 service that receivd the packet.\r
+  @param[in]  Head               The IP4 head of the ICMP query packet.\r
+  @param[in]  Packet             The content of the ICMP query with IP4 head\r
                                  removed.\r
 \r
   @retval EFI_INVALID_PARAMETER  The packet is malformated.\r
   @retval EFI_SUCCESS            The ICMP message is successfully processed.\r
+  @retval Others                 Failed to handle ICMP packet.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -353,7 +347,7 @@ Ip4IcmpHandle (
     goto DROP;\r
   }\r
 \r
-  Checksum = ~NetbufChecksum (Packet);\r
+  Checksum = (UINT16) (~NetbufChecksum (Packet));\r
   if ((Icmp.Checksum != 0) && (Checksum != 0)) {\r
     goto DROP;\r
   }\r