]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Icmp.c
Fix the comments to follow UEFI Spec regarding how to check an EFI_HANDLE is valid...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Icmp.c
... / ...
CommitLineData
1/** @file\r
2\r
3Copyright (c) 2005 - 2009, Intel Corporation. All rights reserved.<BR>\r
4This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12**/\r
13\r
14#include "Ip4Impl.h"\r
15\r
16IP4_ICMP_CLASS\r
17mIcmpClass[] = {\r
18 {ICMP_ECHO_REPLY, ICMP_QUERY_MESSAGE },\r
19 {1, ICMP_INVALID_MESSAGE},\r
20 {2, ICMP_INVALID_MESSAGE},\r
21 {ICMP_DEST_UNREACHABLE, ICMP_ERROR_MESSAGE },\r
22 {ICMP_SOURCE_QUENCH, ICMP_ERROR_MESSAGE },\r
23 {ICMP_REDIRECT, ICMP_ERROR_MESSAGE },\r
24 {6, ICMP_INVALID_MESSAGE},\r
25 {7, ICMP_INVALID_MESSAGE},\r
26 {ICMP_ECHO_REQUEST, ICMP_QUERY_MESSAGE },\r
27 {9, ICMP_INVALID_MESSAGE},\r
28 {10, ICMP_INVALID_MESSAGE},\r
29 {ICMP_TIME_EXCEEDED, ICMP_ERROR_MESSAGE },\r
30 {ICMP_PARAMETER_PROBLEM, ICMP_ERROR_MESSAGE },\r
31 {ICMP_TIMESTAMP , ICMP_QUERY_MESSAGE },\r
32 {14, ICMP_INVALID_MESSAGE},\r
33 {ICMP_INFO_REQUEST , ICMP_QUERY_MESSAGE },\r
34 {ICMP_INFO_REPLY , ICMP_QUERY_MESSAGE },\r
35};\r
36\r
37EFI_IP4_ICMP_TYPE\r
38mIp4SupportedIcmp[23] = {\r
39 {ICMP_ECHO_REPLY, ICMP_DEFAULT_CODE },\r
40\r
41 {ICMP_DEST_UNREACHABLE, ICMP_NET_UNREACHABLE },\r
42 {ICMP_DEST_UNREACHABLE, ICMP_HOST_UNREACHABLE },\r
43 {ICMP_DEST_UNREACHABLE, ICMP_PROTO_UNREACHABLE },\r
44 {ICMP_DEST_UNREACHABLE, ICMP_PORT_UNREACHABLE },\r
45 {ICMP_DEST_UNREACHABLE, ICMP_FRAGMENT_FAILED },\r
46 {ICMP_DEST_UNREACHABLE, ICMP_SOURCEROUTE_FAILED },\r
47 {ICMP_DEST_UNREACHABLE, ICMP_NET_UNKNOWN },\r
48 {ICMP_DEST_UNREACHABLE, ICMP_HOST_UNKNOWN },\r
49 {ICMP_DEST_UNREACHABLE, ICMP_SOURCE_ISOLATED },\r
50 {ICMP_DEST_UNREACHABLE, ICMP_NET_PROHIBITED },\r
51 {ICMP_DEST_UNREACHABLE, ICMP_HOST_PROHIBITED },\r
52 {ICMP_DEST_UNREACHABLE, ICMP_NET_UNREACHABLE_TOS },\r
53 {ICMP_DEST_UNREACHABLE, ICMP_HOST_UNREACHABLE_TOS},\r
54\r
55 {ICMP_SOURCE_QUENCH, ICMP_DEFAULT_CODE },\r
56\r
57 {ICMP_REDIRECT, ICMP_NET_REDIRECT },\r
58 {ICMP_REDIRECT, ICMP_HOST_REDIRECT },\r
59 {ICMP_REDIRECT, ICMP_NET_TOS_REDIRECT },\r
60 {ICMP_REDIRECT, ICMP_HOST_TOS_REDIRECT },\r
61\r
62 {ICMP_ECHO_REQUEST, ICMP_DEFAULT_CODE },\r
63\r
64 {ICMP_TIME_EXCEEDED, ICMP_TIMEOUT_IN_TRANSIT },\r
65 {ICMP_TIME_EXCEEDED, ICMP_TIMEOUT_REASSEMBLE },\r
66\r
67 {ICMP_PARAMETER_PROBLEM, ICMP_DEFAULT_CODE },\r
68};\r
69\r
70\r
71\r
72/**\r
73 Process the ICMP redirect. Find the instance then update\r
74 its route cache.\r
75 \r
76 All kinds of redirect is treated as host redirect as\r
77 specified by RFC1122 3.3.1.2:\r
78 "Since the subnet mask appropriate to the destination\r
79 address is generally not known, a Network Redirect\r
80 message SHOULD be treated identically to a Host Redirect\r
81 message;"\r
82\r
83 @param[in] IpSb The IP4 service binding instance that received \r
84 the packet.\r
85 @param[in] Head The IP head of the received ICMPpacket.\r
86 @param[in] Packet The content of the ICMP redirect packet with IP\r
87 head removed.\r
88 @param[in] Icmp The buffer to store the ICMP error message if\r
89 something is wrong.\r
90\r
91 @retval EFI_INVALID_PARAMETER The parameter is invalid\r
92 @retval EFI_SUCCESS Successfully updated the route caches\r
93\r
94**/\r
95EFI_STATUS\r
96Ip4ProcessIcmpRedirect (\r
97 IN IP4_SERVICE *IpSb,\r
98 IN IP4_HEAD *Head,\r
99 IN NET_BUF *Packet,\r
100 IN IP4_ICMP_ERROR_HEAD *Icmp\r
101 )\r
102{\r
103 LIST_ENTRY *Entry;\r
104 IP4_PROTOCOL *Ip4Instance;\r
105 IP4_ROUTE_CACHE_ENTRY *CacheEntry;\r
106 IP4_INTERFACE *IpIf;\r
107 IP4_ADDR Gateway;\r
108 IP4_ADDR Src;\r
109 IP4_ADDR Dst;\r
110\r
111 //\r
112 // Find the interface whose IP address is the source of the\r
113 // orgianl IP packet.\r
114 //\r
115 IpIf = Ip4FindInterface (IpSb, NTOHL (Icmp->IpHead.Src));\r
116 Gateway = NTOHL (Icmp->Fourth);\r
117\r
118 //\r
119 // discard the packet if the new gateway address it specifies\r
120 // is not on the same connected net through which the Redirect\r
121 // arrived. (RFC1122 3.2.2.2).\r
122 //\r
123 if ((IpIf == NULL) || !IP4_NET_EQUAL (Gateway, IpIf->Ip, IpIf->SubnetMask)) {\r
124 NetbufFree (Packet);\r
125 return EFI_INVALID_PARAMETER;\r
126 }\r
127\r
128 //\r
129 // Update each IP child's route cache on the interface.\r
130 //\r
131 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {\r
132 Ip4Instance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);\r
133\r
134 if (Ip4Instance->RouteTable == NULL) {\r
135 continue;\r
136 }\r
137\r
138 Dst = NTOHL (Icmp->IpHead.Dst);\r
139 Src = NTOHL (Icmp->IpHead.Src);\r
140 CacheEntry = Ip4FindRouteCache (Ip4Instance->RouteTable, Dst, Src);\r
141\r
142 //\r
143 // Only update the route cache's gateway if the source of the\r
144 // Redirect is the current first-hop gateway\r
145 //\r
146 if ((CacheEntry != NULL) && (NTOHL (Head->Src) == CacheEntry->NextHop)) {\r
147 CacheEntry->NextHop = Gateway;\r
148 }\r
149 }\r
150\r
151 NetbufFree (Packet);\r
152 return EFI_SUCCESS;\r
153}\r
154\r
155\r
156/**\r
157 Process the ICMP error packet. If it is an ICMP redirect packet,\r
158 update call Ip4ProcessIcmpRedirect to update the IP instance's\r
159 route cache, otherwise, deliver the packet to upper layer.\r
160\r
161 @param[in] IpSb The IP service that received the packet.\r
162 @param[in] Head The IP head of the ICMP error packet\r
163 @param[in] Packet The content of the ICMP error with IP head\r
164 removed.\r
165\r
166 @retval EFI_SUCCESS The ICMP error is processed successfully.\r
167 @retval EFI_INVALID_PARAMETER The packet is invalid\r
168 @retval Others Failed to process the packet.\r
169 \r
170**/\r
171EFI_STATUS\r
172Ip4ProcessIcmpError (\r
173 IN IP4_SERVICE *IpSb,\r
174 IN IP4_HEAD *Head,\r
175 IN NET_BUF *Packet\r
176 )\r
177{\r
178 IP4_ICMP_ERROR_HEAD Icmp;\r
179\r
180 if (Packet->TotalSize < sizeof (Icmp)) {\r
181 NetbufFree (Packet);\r
182 return EFI_INVALID_PARAMETER;\r
183 }\r
184\r
185 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);\r
186\r
187 //\r
188 // If it is an ICMP redirect error, update the route cache\r
189 // as RFC1122. Otherwise, demultiplex it to IP instances.\r
190 //\r
191 if (Icmp.Head.Type == ICMP_REDIRECT) {\r
192 return Ip4ProcessIcmpRedirect (IpSb, Head, Packet, &Icmp);\r
193 }\r
194\r
195 IP4_GET_CLIP_INFO (Packet)->Status = EFI_ICMP_ERROR;\r
196 return Ip4Demultiplex (IpSb, Head, Packet);\r
197}\r
198\r
199\r
200/**\r
201 Replay an ICMP echo request.\r
202\r
203 @param[in] IpSb The IP service that receivd the packet\r
204 @param[in] Head The IP head of the ICMP error packet\r
205 @param[in] Packet The content of the ICMP error with IP head\r
206 removed.\r
207\r
208 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.\r
209 @retval EFI_SUCCESS The ICMP Echo request is successfully answered.\r
210 @retval Others Failed to answer the ICMP echo request.\r
211\r
212**/\r
213EFI_STATUS\r
214Ip4IcmpReplyEcho (\r
215 IN IP4_SERVICE *IpSb,\r
216 IN IP4_HEAD *Head,\r
217 IN NET_BUF *Packet\r
218 )\r
219{\r
220 IP4_ICMP_QUERY_HEAD *Icmp;\r
221 NET_BUF *Data;\r
222 EFI_STATUS Status;\r
223 IP4_HEAD ReplyHead;\r
224\r
225 //\r
226 // make a copy the packet, it is really a bad idea to\r
227 // send the MNP's buffer back to MNP.\r
228 //\r
229 Data = NetbufDuplicate (Packet, NULL, IP4_MAX_HEADLEN);\r
230\r
231 if (Data == NULL) {\r
232 Status = EFI_OUT_OF_RESOURCES;\r
233 goto ON_EXIT;\r
234 }\r
235\r
236 //\r
237 // Change the ICMP type to echo reply, exchange the source\r
238 // and destination, then send it. The source is updated to\r
239 // use specific destination. See RFC1122. SRR/RR option\r
240 // update is omitted.\r
241 //\r
242 Icmp = (IP4_ICMP_QUERY_HEAD *) NetbufGetByte (Data, 0, NULL);\r
243 Icmp->Head.Type = ICMP_ECHO_REPLY;\r
244 Icmp->Head.Checksum = 0;\r
245 Icmp->Head.Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Icmp, Data->TotalSize));\r
246\r
247 ReplyHead.Tos = 0;\r
248 ReplyHead.Fragment = 0;\r
249 ReplyHead.Ttl = 64;\r
250 ReplyHead.Protocol = EFI_IP_PROTO_ICMP;\r
251 ReplyHead.Src = 0;\r
252\r
253 //\r
254 // Ip4Output will select a source for us\r
255 //\r
256 ReplyHead.Dst = Head->Src;\r
257\r
258 Status = Ip4Output (\r
259 IpSb,\r
260 NULL,\r
261 Data,\r
262 &ReplyHead,\r
263 NULL,\r
264 0,\r
265 IP4_ALLZERO_ADDRESS,\r
266 Ip4SysPacketSent,\r
267 NULL\r
268 );\r
269\r
270ON_EXIT:\r
271 NetbufFree (Packet);\r
272 return Status;\r
273}\r
274\r
275\r
276/**\r
277 Process the ICMP query message. If it is an ICMP echo\r
278 request, answer it. Otherwise deliver it to upper layer.\r
279\r
280 @param[in] IpSb The IP service that receivd the packet\r
281 @param[in] Head The IP head of the ICMP query packet\r
282 @param[in] Packet The content of the ICMP query with IP head\r
283 removed.\r
284\r
285 @retval EFI_INVALID_PARAMETER The packet is invalid\r
286 @retval EFI_SUCCESS The ICMP query message is processed\r
287 @retval Others Failed to process ICMP query.\r
288\r
289**/\r
290EFI_STATUS\r
291Ip4ProcessIcmpQuery (\r
292 IN IP4_SERVICE *IpSb,\r
293 IN IP4_HEAD *Head,\r
294 IN NET_BUF *Packet\r
295 )\r
296{\r
297 IP4_ICMP_QUERY_HEAD Icmp;\r
298\r
299 if (Packet->TotalSize < sizeof (Icmp)) {\r
300 NetbufFree (Packet);\r
301 return EFI_INVALID_PARAMETER;\r
302 }\r
303\r
304 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);\r
305\r
306 if (Icmp.Head.Type == ICMP_ECHO_REQUEST) {\r
307 return Ip4IcmpReplyEcho (IpSb, Head, Packet);\r
308 }\r
309\r
310 return Ip4Demultiplex (IpSb, Head, Packet);\r
311}\r
312\r
313\r
314/**\r
315 Handle the ICMP packet. First validate the message format,\r
316 then according to the message types, process it as query or\r
317 error packet.\r
318\r
319 @param[in] IpSb The IP service that receivd the packet\r
320 @param[in] Head The IP head of the ICMP query packet\r
321 @param[in] Packet The content of the ICMP query with IP head\r
322 removed.\r
323\r
324 @retval EFI_INVALID_PARAMETER The packet is malformated.\r
325 @retval EFI_SUCCESS The ICMP message is successfully processed.\r
326 @retval Others Failed to handle ICMP packet.\r
327\r
328**/\r
329EFI_STATUS\r
330Ip4IcmpHandle (\r
331 IN IP4_SERVICE *IpSb,\r
332 IN IP4_HEAD *Head,\r
333 IN NET_BUF *Packet\r
334 )\r
335{\r
336 IP4_ICMP_HEAD Icmp;\r
337 UINT16 Checksum;\r
338\r
339 if (Packet->TotalSize < sizeof (Icmp)) {\r
340 goto DROP;\r
341 }\r
342\r
343 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);\r
344\r
345 if (Icmp.Type > ICMP_TYPE_MAX) {\r
346 goto DROP;\r
347 }\r
348\r
349 Checksum = (UINT16) (~NetbufChecksum (Packet));\r
350 if ((Icmp.Checksum != 0) && (Checksum != 0)) {\r
351 goto DROP;\r
352 }\r
353\r
354 if (mIcmpClass[Icmp.Type].IcmpClass == ICMP_ERROR_MESSAGE) {\r
355 return Ip4ProcessIcmpError (IpSb, Head, Packet);\r
356\r
357 } else if (mIcmpClass[Icmp.Type].IcmpClass == ICMP_QUERY_MESSAGE) {\r
358 return Ip4ProcessIcmpQuery (IpSb, Head, Packet);\r
359\r
360 }\r
361\r
362DROP:\r
363 NetbufFree (Packet);\r
364 return EFI_INVALID_PARAMETER;\r
365}\r