3 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
12 { ICMP_ECHO_REPLY
, ICMP_QUERY_MESSAGE
},
13 { 1, ICMP_INVALID_MESSAGE
},
14 { 2, ICMP_INVALID_MESSAGE
},
15 { ICMP_DEST_UNREACHABLE
, ICMP_ERROR_MESSAGE
},
16 { ICMP_SOURCE_QUENCH
, ICMP_ERROR_MESSAGE
},
17 { ICMP_REDIRECT
, ICMP_ERROR_MESSAGE
},
18 { 6, ICMP_INVALID_MESSAGE
},
19 { 7, ICMP_INVALID_MESSAGE
},
20 { ICMP_ECHO_REQUEST
, ICMP_QUERY_MESSAGE
},
21 { 9, ICMP_INVALID_MESSAGE
},
22 { 10, ICMP_INVALID_MESSAGE
},
23 { ICMP_TIME_EXCEEDED
, ICMP_ERROR_MESSAGE
},
24 { ICMP_PARAMETER_PROBLEM
, ICMP_ERROR_MESSAGE
},
25 { ICMP_TIMESTAMP
, ICMP_QUERY_MESSAGE
},
26 { 14, ICMP_INVALID_MESSAGE
},
27 { ICMP_INFO_REQUEST
, ICMP_QUERY_MESSAGE
},
28 { ICMP_INFO_REPLY
, ICMP_QUERY_MESSAGE
},
32 mIp4SupportedIcmp
[23] = {
33 { ICMP_ECHO_REPLY
, ICMP_DEFAULT_CODE
},
35 { ICMP_DEST_UNREACHABLE
, ICMP_NET_UNREACHABLE
},
36 { ICMP_DEST_UNREACHABLE
, ICMP_HOST_UNREACHABLE
},
37 { ICMP_DEST_UNREACHABLE
, ICMP_PROTO_UNREACHABLE
},
38 { ICMP_DEST_UNREACHABLE
, ICMP_PORT_UNREACHABLE
},
39 { ICMP_DEST_UNREACHABLE
, ICMP_FRAGMENT_FAILED
},
40 { ICMP_DEST_UNREACHABLE
, ICMP_SOURCEROUTE_FAILED
},
41 { ICMP_DEST_UNREACHABLE
, ICMP_NET_UNKNOWN
},
42 { ICMP_DEST_UNREACHABLE
, ICMP_HOST_UNKNOWN
},
43 { ICMP_DEST_UNREACHABLE
, ICMP_SOURCE_ISOLATED
},
44 { ICMP_DEST_UNREACHABLE
, ICMP_NET_PROHIBITED
},
45 { ICMP_DEST_UNREACHABLE
, ICMP_HOST_PROHIBITED
},
46 { ICMP_DEST_UNREACHABLE
, ICMP_NET_UNREACHABLE_TOS
},
47 { ICMP_DEST_UNREACHABLE
, ICMP_HOST_UNREACHABLE_TOS
},
49 { ICMP_SOURCE_QUENCH
, ICMP_DEFAULT_CODE
},
51 { ICMP_REDIRECT
, ICMP_NET_REDIRECT
},
52 { ICMP_REDIRECT
, ICMP_HOST_REDIRECT
},
53 { ICMP_REDIRECT
, ICMP_NET_TOS_REDIRECT
},
54 { ICMP_REDIRECT
, ICMP_HOST_TOS_REDIRECT
},
56 { ICMP_ECHO_REQUEST
, ICMP_DEFAULT_CODE
},
58 { ICMP_TIME_EXCEEDED
, ICMP_TIMEOUT_IN_TRANSIT
},
59 { ICMP_TIME_EXCEEDED
, ICMP_TIMEOUT_REASSEMBLE
},
61 { ICMP_PARAMETER_PROBLEM
, ICMP_DEFAULT_CODE
},
65 Process the ICMP redirect. Find the instance then update
68 All kinds of redirect is treated as host redirect as
69 specified by RFC1122 3.3.1.2:
70 "Since the subnet mask appropriate to the destination
71 address is generally not known, a Network Redirect
72 message SHOULD be treated identically to a Host Redirect
75 @param[in] IpSb The IP4 service binding instance that received
77 @param[in] Head The IP head of the received ICMPpacket.
78 @param[in] Packet The content of the ICMP redirect packet with IP
80 @param[in] Icmp The buffer to store the ICMP error message if
83 @retval EFI_INVALID_PARAMETER The parameter is invalid
84 @retval EFI_SUCCESS Successfully updated the route caches
88 Ip4ProcessIcmpRedirect (
92 IN IP4_ICMP_ERROR_HEAD
*Icmp
96 IP4_PROTOCOL
*Ip4Instance
;
97 IP4_ROUTE_CACHE_ENTRY
*CacheEntry
;
104 // Find the interface whose IP address is the source of the
105 // orgianl IP packet.
107 IpIf
= Ip4FindInterface (IpSb
, NTOHL (Icmp
->IpHead
.Src
));
108 Gateway
= NTOHL (Icmp
->Fourth
);
111 // discard the packet if the new gateway address it specifies
112 // is not on the same connected net through which the Redirect
113 // arrived. (RFC1122 3.2.2.2).
115 if ((IpIf
== NULL
) || !IP4_NET_EQUAL (Gateway
, IpIf
->Ip
, IpIf
->SubnetMask
)) {
117 return EFI_INVALID_PARAMETER
;
121 // Update each IP child's route cache on the interface.
123 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
124 Ip4Instance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
126 if (Ip4Instance
->RouteTable
== NULL
) {
130 Dst
= NTOHL (Icmp
->IpHead
.Dst
);
131 Src
= NTOHL (Icmp
->IpHead
.Src
);
132 CacheEntry
= Ip4FindRouteCache (Ip4Instance
->RouteTable
, Dst
, Src
);
135 // Only update the route cache's gateway if the source of the
136 // Redirect is the current first-hop gateway
138 if ((CacheEntry
!= NULL
) && (NTOHL (Head
->Src
) == CacheEntry
->NextHop
)) {
139 CacheEntry
->NextHop
= Gateway
;
148 Process the ICMP error packet. If it is an ICMP redirect packet,
149 update call Ip4ProcessIcmpRedirect to update the IP instance's
150 route cache, otherwise, deliver the packet to upper layer.
152 @param[in] IpSb The IP4 service that received the packet.
153 @param[in] Head The IP4 head of the ICMP error packet
154 @param[in] Packet The content of the ICMP error with IP4 head
157 @retval EFI_SUCCESS The ICMP error is processed successfully.
158 @retval EFI_INVALID_PARAMETER The packet is invalid
159 @retval Others Failed to process the packet.
163 Ip4ProcessIcmpError (
164 IN IP4_SERVICE
*IpSb
,
169 IP4_ICMP_ERROR_HEAD Icmp
;
171 if (Packet
->TotalSize
< sizeof (Icmp
)) {
173 return EFI_INVALID_PARAMETER
;
176 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*)&Icmp
);
179 // If it is an ICMP redirect error, update the route cache
180 // as RFC1122. Otherwise, demultiplex it to IP instances.
182 if (Icmp
.Head
.Type
== ICMP_REDIRECT
) {
183 return Ip4ProcessIcmpRedirect (IpSb
, Head
, Packet
, &Icmp
);
186 IP4_GET_CLIP_INFO (Packet
)->Status
= EFI_ICMP_ERROR
;
187 return Ip4Demultiplex (IpSb
, Head
, Packet
, NULL
, 0);
191 Replay an ICMP echo request.
193 @param[in] IpSb The IP4 service that receivd the packet
194 @param[in] Head The IP4 head of the ICMP error packet
195 @param[in] Packet The content of the ICMP error with IP4 head
198 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
199 @retval EFI_SUCCESS The ICMP Echo request is successfully answered.
200 @retval Others Failed to answer the ICMP echo request.
205 IN IP4_SERVICE
*IpSb
,
210 IP4_ICMP_QUERY_HEAD
*Icmp
;
216 // make a copy the packet, it is really a bad idea to
217 // send the MNP's buffer back to MNP.
219 Data
= NetbufDuplicate (Packet
, NULL
, IP4_MAX_HEADLEN
);
222 Status
= EFI_OUT_OF_RESOURCES
;
227 // Change the ICMP type to echo reply, exchange the source
228 // and destination, then send it. The source is updated to
229 // use specific destination. See RFC1122. SRR/RR option
230 // update is omitted.
232 Icmp
= (IP4_ICMP_QUERY_HEAD
*)NetbufGetByte (Data
, 0, NULL
);
233 ASSERT (Icmp
!= NULL
);
234 Icmp
->Head
.Type
= ICMP_ECHO_REPLY
;
235 Icmp
->Head
.Checksum
= 0;
236 Icmp
->Head
.Checksum
= (UINT16
)(~NetblockChecksum ((UINT8
*)Icmp
, Data
->TotalSize
));
239 ReplyHead
.Fragment
= 0;
241 ReplyHead
.Protocol
= EFI_IP_PROTO_ICMP
;
245 // Ip4Output will select a source for us
247 ReplyHead
.Dst
= Head
->Src
;
260 if (EFI_ERROR (Status
)) {
270 Process the ICMP query message. If it is an ICMP echo
271 request, answer it. Otherwise deliver it to upper layer.
273 @param[in] IpSb The IP4 service that receivd the packet
274 @param[in] Head The IP4 head of the ICMP query packet
275 @param[in] Packet The content of the ICMP query with IP4 head
278 @retval EFI_INVALID_PARAMETER The packet is invalid
279 @retval EFI_SUCCESS The ICMP query message is processed
280 @retval Others Failed to process ICMP query.
284 Ip4ProcessIcmpQuery (
285 IN IP4_SERVICE
*IpSb
,
290 IP4_ICMP_QUERY_HEAD Icmp
;
292 if (Packet
->TotalSize
< sizeof (Icmp
)) {
294 return EFI_INVALID_PARAMETER
;
297 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*)&Icmp
);
299 if (Icmp
.Head
.Type
== ICMP_ECHO_REQUEST
) {
300 return Ip4IcmpReplyEcho (IpSb
, Head
, Packet
);
303 return Ip4Demultiplex (IpSb
, Head
, Packet
, NULL
, 0);
307 Handle the ICMP packet. First validate the message format,
308 then according to the message types, process it as query or
311 @param[in] IpSb The IP4 service that receivd the packet.
312 @param[in] Head The IP4 head of the ICMP query packet.
313 @param[in] Packet The content of the ICMP query with IP4 head
316 @retval EFI_INVALID_PARAMETER The packet is malformatted.
317 @retval EFI_SUCCESS The ICMP message is successfully processed.
318 @retval Others Failed to handle ICMP packet.
323 IN IP4_SERVICE
*IpSb
,
331 if (Packet
->TotalSize
< sizeof (Icmp
)) {
335 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*)&Icmp
);
337 if (Icmp
.Type
> ICMP_TYPE_MAX
) {
341 Checksum
= (UINT16
)(~NetbufChecksum (Packet
));
342 if ((Icmp
.Checksum
!= 0) && (Checksum
!= 0)) {
346 if (mIcmpClass
[Icmp
.Type
].IcmpClass
== ICMP_ERROR_MESSAGE
) {
347 return Ip4ProcessIcmpError (IpSb
, Head
, Packet
);
348 } else if (mIcmpClass
[Icmp
.Type
].IcmpClass
== ICMP_QUERY_MESSAGE
) {
349 return Ip4ProcessIcmpQuery (IpSb
, Head
, Packet
);
354 return EFI_INVALID_PARAMETER
;