1 | /** @file
|
---|
2 | The ICMPv6 handle routines to process the ICMPv6 control messages.
|
---|
3 |
|
---|
4 | Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
|
---|
5 | (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
|
---|
6 |
|
---|
7 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
8 |
|
---|
9 | **/
|
---|
10 |
|
---|
11 | #include "Ip6Impl.h"
|
---|
12 |
|
---|
13 | EFI_IP6_ICMP_TYPE mIp6SupportedIcmp[23] = {
|
---|
14 | {
|
---|
15 | ICMP_V6_DEST_UNREACHABLE,
|
---|
16 | ICMP_V6_NO_ROUTE_TO_DEST
|
---|
17 | },
|
---|
18 | {
|
---|
19 | ICMP_V6_DEST_UNREACHABLE,
|
---|
20 | ICMP_V6_COMM_PROHIBITED
|
---|
21 | },
|
---|
22 | {
|
---|
23 | ICMP_V6_DEST_UNREACHABLE,
|
---|
24 | ICMP_V6_BEYOND_SCOPE
|
---|
25 | },
|
---|
26 | {
|
---|
27 | ICMP_V6_DEST_UNREACHABLE,
|
---|
28 | ICMP_V6_ADDR_UNREACHABLE
|
---|
29 | },
|
---|
30 | {
|
---|
31 | ICMP_V6_DEST_UNREACHABLE,
|
---|
32 | ICMP_V6_PORT_UNREACHABLE
|
---|
33 | },
|
---|
34 | {
|
---|
35 | ICMP_V6_DEST_UNREACHABLE,
|
---|
36 | ICMP_V6_SOURCE_ADDR_FAILED
|
---|
37 | },
|
---|
38 | {
|
---|
39 | ICMP_V6_DEST_UNREACHABLE,
|
---|
40 | ICMP_V6_ROUTE_REJECTED
|
---|
41 | },
|
---|
42 |
|
---|
43 | {
|
---|
44 | ICMP_V6_PACKET_TOO_BIG,
|
---|
45 | ICMP_V6_DEFAULT_CODE
|
---|
46 | },
|
---|
47 |
|
---|
48 | {
|
---|
49 | ICMP_V6_TIME_EXCEEDED,
|
---|
50 | ICMP_V6_TIMEOUT_HOP_LIMIT
|
---|
51 | },
|
---|
52 | {
|
---|
53 | ICMP_V6_TIME_EXCEEDED,
|
---|
54 | ICMP_V6_TIMEOUT_REASSEMBLE
|
---|
55 | },
|
---|
56 |
|
---|
57 | {
|
---|
58 | ICMP_V6_PARAMETER_PROBLEM,
|
---|
59 | ICMP_V6_ERRONEOUS_HEADER
|
---|
60 | },
|
---|
61 | {
|
---|
62 | ICMP_V6_PARAMETER_PROBLEM,
|
---|
63 | ICMP_V6_UNRECOGNIZE_NEXT_HDR
|
---|
64 | },
|
---|
65 | {
|
---|
66 | ICMP_V6_PARAMETER_PROBLEM,
|
---|
67 | ICMP_V6_UNRECOGNIZE_OPTION
|
---|
68 | },
|
---|
69 |
|
---|
70 | {
|
---|
71 | ICMP_V6_ECHO_REQUEST,
|
---|
72 | ICMP_V6_DEFAULT_CODE
|
---|
73 | },
|
---|
74 | {
|
---|
75 | ICMP_V6_ECHO_REPLY,
|
---|
76 | ICMP_V6_DEFAULT_CODE
|
---|
77 | },
|
---|
78 |
|
---|
79 | {
|
---|
80 | ICMP_V6_LISTENER_QUERY,
|
---|
81 | ICMP_V6_DEFAULT_CODE
|
---|
82 | },
|
---|
83 | {
|
---|
84 | ICMP_V6_LISTENER_REPORT,
|
---|
85 | ICMP_V6_DEFAULT_CODE
|
---|
86 | },
|
---|
87 | {
|
---|
88 | ICMP_V6_LISTENER_REPORT_2,
|
---|
89 | ICMP_V6_DEFAULT_CODE
|
---|
90 | },
|
---|
91 | {
|
---|
92 | ICMP_V6_LISTENER_DONE,
|
---|
93 | ICMP_V6_DEFAULT_CODE
|
---|
94 | },
|
---|
95 |
|
---|
96 | {
|
---|
97 | ICMP_V6_ROUTER_SOLICIT,
|
---|
98 | ICMP_V6_DEFAULT_CODE
|
---|
99 | },
|
---|
100 | {
|
---|
101 | ICMP_V6_ROUTER_ADVERTISE,
|
---|
102 | ICMP_V6_DEFAULT_CODE
|
---|
103 | },
|
---|
104 | {
|
---|
105 | ICMP_V6_NEIGHBOR_SOLICIT,
|
---|
106 | ICMP_V6_DEFAULT_CODE
|
---|
107 | },
|
---|
108 | {
|
---|
109 | ICMP_V6_NEIGHBOR_ADVERTISE,
|
---|
110 | ICMP_V6_DEFAULT_CODE
|
---|
111 | },
|
---|
112 | };
|
---|
113 |
|
---|
114 | /**
|
---|
115 | Reply an ICMPv6 echo request.
|
---|
116 |
|
---|
117 | @param[in] IpSb The IP service that received the packet.
|
---|
118 | @param[in] Head The IP head of the ICMPv6 informational message.
|
---|
119 | @param[in] Packet The content of the ICMPv6 message with the IP head
|
---|
120 | removed.
|
---|
121 |
|
---|
122 | @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
|
---|
123 | @retval EFI_SUCCESS Successfully answered the ICMPv6 Echo request.
|
---|
124 | @retval Others Failed to answer the ICMPv6 Echo request.
|
---|
125 |
|
---|
126 | **/
|
---|
127 | EFI_STATUS
|
---|
128 | Ip6IcmpReplyEcho (
|
---|
129 | IN IP6_SERVICE *IpSb,
|
---|
130 | IN EFI_IP6_HEADER *Head,
|
---|
131 | IN NET_BUF *Packet
|
---|
132 | )
|
---|
133 | {
|
---|
134 | IP6_ICMP_INFORMATION_HEAD *Icmp;
|
---|
135 | NET_BUF *Data;
|
---|
136 | EFI_STATUS Status;
|
---|
137 | EFI_IP6_HEADER ReplyHead;
|
---|
138 |
|
---|
139 | Status = EFI_OUT_OF_RESOURCES;
|
---|
140 | //
|
---|
141 | // make a copy the packet, it is really a bad idea to
|
---|
142 | // send the MNP's buffer back to MNP.
|
---|
143 | //
|
---|
144 | Data = NetbufDuplicate (Packet, NULL, IP6_MAX_HEADLEN);
|
---|
145 | if (Data == NULL) {
|
---|
146 | goto Exit;
|
---|
147 | }
|
---|
148 |
|
---|
149 | //
|
---|
150 | // Change the ICMP type to echo reply, exchange the source
|
---|
151 | // and destination, then send it. The source is updated to
|
---|
152 | // use specific destination. See RFC1122. SRR/RR option
|
---|
153 | // update is omitted.
|
---|
154 | //
|
---|
155 | Icmp = (IP6_ICMP_INFORMATION_HEAD *)NetbufGetByte (Data, 0, NULL);
|
---|
156 | if (Icmp == NULL) {
|
---|
157 | NetbufFree (Data);
|
---|
158 | goto Exit;
|
---|
159 | }
|
---|
160 |
|
---|
161 | Icmp->Head.Type = ICMP_V6_ECHO_REPLY;
|
---|
162 | Icmp->Head.Checksum = 0;
|
---|
163 |
|
---|
164 | //
|
---|
165 | // Generate the IPv6 basic header
|
---|
166 | // If the Echo Reply is a response to a Echo Request sent to one of the node's unicast address,
|
---|
167 | // the Source address of the Echo Reply must be the same address.
|
---|
168 | //
|
---|
169 | ZeroMem (&ReplyHead, sizeof (EFI_IP6_HEADER));
|
---|
170 |
|
---|
171 | ReplyHead.PayloadLength = HTONS ((UINT16)(Packet->TotalSize));
|
---|
172 | ReplyHead.NextHeader = IP6_ICMP;
|
---|
173 | ReplyHead.HopLimit = IpSb->CurHopLimit;
|
---|
174 | IP6_COPY_ADDRESS (&ReplyHead.DestinationAddress, &Head->SourceAddress);
|
---|
175 |
|
---|
176 | if (Ip6IsOneOfSetAddress (IpSb, &Head->DestinationAddress, NULL, NULL)) {
|
---|
177 | IP6_COPY_ADDRESS (&ReplyHead.SourceAddress, &Head->DestinationAddress);
|
---|
178 | }
|
---|
179 |
|
---|
180 | //
|
---|
181 | // If source is unspecified, Ip6Output will select a source for us
|
---|
182 | //
|
---|
183 | Status = Ip6Output (
|
---|
184 | IpSb,
|
---|
185 | NULL,
|
---|
186 | NULL,
|
---|
187 | Data,
|
---|
188 | &ReplyHead,
|
---|
189 | NULL,
|
---|
190 | 0,
|
---|
191 | Ip6SysPacketSent,
|
---|
192 | NULL
|
---|
193 | );
|
---|
194 |
|
---|
195 | Exit:
|
---|
196 | NetbufFree (Packet);
|
---|
197 | return Status;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | Process Packet Too Big message sent by a router in response to a packet that
|
---|
202 | it cannot forward because the packet is larger than the MTU of outgoing link.
|
---|
203 | Since this driver already uses IPv6 minimum link MTU as the maximum packet size,
|
---|
204 | if Packet Too Big message is still received, do not reduce the packet size, but
|
---|
205 | rather include a Fragment header in the subsequent packets.
|
---|
206 |
|
---|
207 | @param[in] IpSb The IP service that received the packet.
|
---|
208 | @param[in] Head The IP head of the ICMPv6 error packet.
|
---|
209 | @param[in] Packet The content of the ICMPv6 error with the IP head
|
---|
210 | removed.
|
---|
211 |
|
---|
212 | @retval EFI_SUCCESS The ICMPv6 error processed successfully.
|
---|
213 | @retval EFI_OUT_OF_RESOURCES Failed to finish the operation due to lack of
|
---|
214 | resource.
|
---|
215 | @retval EFI_NOT_FOUND The packet too big message is not sent to us.
|
---|
216 |
|
---|
217 | **/
|
---|
218 | EFI_STATUS
|
---|
219 | Ip6ProcessPacketTooBig (
|
---|
220 | IN IP6_SERVICE *IpSb,
|
---|
221 | IN EFI_IP6_HEADER *Head,
|
---|
222 | IN NET_BUF *Packet
|
---|
223 | )
|
---|
224 | {
|
---|
225 | IP6_ICMP_ERROR_HEAD Icmp;
|
---|
226 | UINT32 Mtu;
|
---|
227 | IP6_ROUTE_ENTRY *RouteEntry;
|
---|
228 | EFI_IPv6_ADDRESS *DestAddress;
|
---|
229 |
|
---|
230 | NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *)&Icmp);
|
---|
231 | Mtu = NTOHL (Icmp.Fourth);
|
---|
232 | DestAddress = &Icmp.IpHead.DestinationAddress;
|
---|
233 |
|
---|
234 | if (Mtu < IP6_MIN_LINK_MTU) {
|
---|
235 | //
|
---|
236 | // Normally the multicast address is considered to be on-link and not recorded
|
---|
237 | // in route table. Here it is added into the table since the MTU information
|
---|
238 | // need be recorded.
|
---|
239 | //
|
---|
240 | if (IP6_IS_MULTICAST (DestAddress)) {
|
---|
241 | RouteEntry = Ip6CreateRouteEntry (DestAddress, 128, NULL);
|
---|
242 | if (RouteEntry == NULL) {
|
---|
243 | NetbufFree (Packet);
|
---|
244 | return EFI_OUT_OF_RESOURCES;
|
---|
245 | }
|
---|
246 |
|
---|
247 | RouteEntry->Flag = IP6_DIRECT_ROUTE | IP6_PACKET_TOO_BIG;
|
---|
248 | InsertHeadList (&IpSb->RouteTable->RouteArea[128], &RouteEntry->Link);
|
---|
249 | IpSb->RouteTable->TotalNum++;
|
---|
250 | } else {
|
---|
251 | RouteEntry = Ip6FindRouteEntry (IpSb->RouteTable, DestAddress, NULL);
|
---|
252 | if (RouteEntry == NULL) {
|
---|
253 | NetbufFree (Packet);
|
---|
254 | return EFI_NOT_FOUND;
|
---|
255 | }
|
---|
256 |
|
---|
257 | RouteEntry->Flag = RouteEntry->Flag | IP6_PACKET_TOO_BIG;
|
---|
258 |
|
---|
259 | Ip6FreeRouteEntry (RouteEntry);
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | NetbufFree (Packet);
|
---|
264 | return EFI_SUCCESS;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /**
|
---|
268 | Process the ICMPv6 error packet, and deliver the packet to upper layer.
|
---|
269 |
|
---|
270 | @param[in] IpSb The IP service that received the packet.
|
---|
271 | @param[in] Head The IP head of the ICMPv6 error packet.
|
---|
272 | @param[in] Packet The content of the ICMPv6 error with the IP head
|
---|
273 | removed.
|
---|
274 |
|
---|
275 | @retval EFI_SUCCESS The ICMPv6 error processed successfully.
|
---|
276 | @retval EFI_INVALID_PARAMETER The packet is invalid.
|
---|
277 | @retval Others Failed to process the packet.
|
---|
278 |
|
---|
279 | **/
|
---|
280 | EFI_STATUS
|
---|
281 | Ip6ProcessIcmpError (
|
---|
282 | IN IP6_SERVICE *IpSb,
|
---|
283 | IN EFI_IP6_HEADER *Head,
|
---|
284 | IN NET_BUF *Packet
|
---|
285 | )
|
---|
286 | {
|
---|
287 | IP6_ICMP_ERROR_HEAD Icmp;
|
---|
288 |
|
---|
289 | //
|
---|
290 | // Check the validity of the packet
|
---|
291 | //
|
---|
292 | if (Packet->TotalSize < sizeof (Icmp)) {
|
---|
293 | goto DROP;
|
---|
294 | }
|
---|
295 |
|
---|
296 | NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *)&Icmp);
|
---|
297 | if (Icmp.Head.Type == ICMP_V6_PACKET_TOO_BIG) {
|
---|
298 | return Ip6ProcessPacketTooBig (IpSb, Head, Packet);
|
---|
299 | }
|
---|
300 |
|
---|
301 | //
|
---|
302 | // Notify the upper-layer process that an ICMPv6 error message is received.
|
---|
303 | //
|
---|
304 | IP6_GET_CLIP_INFO (Packet)->Status = EFI_ICMP_ERROR;
|
---|
305 | return Ip6Demultiplex (IpSb, Head, Packet);
|
---|
306 |
|
---|
307 | DROP:
|
---|
308 | NetbufFree (Packet);
|
---|
309 | Packet = NULL;
|
---|
310 | return EFI_INVALID_PARAMETER;
|
---|
311 | }
|
---|
312 |
|
---|
313 | /**
|
---|
314 | Process the ICMPv6 informational messages. If it is an ICMPv6 echo
|
---|
315 | request, answer it. If it is a MLD message, trigger MLD routines to
|
---|
316 | process it. If it is a ND message, trigger ND routines to process it.
|
---|
317 | Otherwise, deliver it to upper layer.
|
---|
318 |
|
---|
319 | @param[in] IpSb The IP service that receivd the packet.
|
---|
320 | @param[in] Head The IP head of the ICMPv6 informational packet.
|
---|
321 | @param[in] Packet The content of the ICMPv6 informational packet
|
---|
322 | with IP head removed.
|
---|
323 |
|
---|
324 | @retval EFI_INVALID_PARAMETER The packet is invalid.
|
---|
325 | @retval EFI_SUCCESS The ICMPv6 informational message processed.
|
---|
326 | @retval Others Failed to process ICMPv6 informational message.
|
---|
327 |
|
---|
328 | **/
|
---|
329 | EFI_STATUS
|
---|
330 | Ip6ProcessIcmpInformation (
|
---|
331 | IN IP6_SERVICE *IpSb,
|
---|
332 | IN EFI_IP6_HEADER *Head,
|
---|
333 | IN NET_BUF *Packet
|
---|
334 | )
|
---|
335 | {
|
---|
336 | IP6_ICMP_INFORMATION_HEAD Icmp;
|
---|
337 | EFI_STATUS Status;
|
---|
338 |
|
---|
339 | NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
|
---|
340 | NET_CHECK_SIGNATURE (Packet, NET_BUF_SIGNATURE);
|
---|
341 | ASSERT (Head != NULL);
|
---|
342 |
|
---|
343 | NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *)&Icmp);
|
---|
344 | Status = EFI_INVALID_PARAMETER;
|
---|
345 |
|
---|
346 | switch (Icmp.Head.Type) {
|
---|
347 | case ICMP_V6_ECHO_REQUEST:
|
---|
348 | //
|
---|
349 | // If ICMPv6 echo, reply it
|
---|
350 | //
|
---|
351 | if (Icmp.Head.Code == 0) {
|
---|
352 | Status = Ip6IcmpReplyEcho (IpSb, Head, Packet);
|
---|
353 | }
|
---|
354 |
|
---|
355 | break;
|
---|
356 | case ICMP_V6_LISTENER_QUERY:
|
---|
357 | Status = Ip6ProcessMldQuery (IpSb, Head, Packet);
|
---|
358 | break;
|
---|
359 | case ICMP_V6_LISTENER_REPORT:
|
---|
360 | case ICMP_V6_LISTENER_REPORT_2:
|
---|
361 | Status = Ip6ProcessMldReport (IpSb, Head, Packet);
|
---|
362 | break;
|
---|
363 | case ICMP_V6_NEIGHBOR_SOLICIT:
|
---|
364 | Status = Ip6ProcessNeighborSolicit (IpSb, Head, Packet);
|
---|
365 | break;
|
---|
366 | case ICMP_V6_NEIGHBOR_ADVERTISE:
|
---|
367 | Status = Ip6ProcessNeighborAdvertise (IpSb, Head, Packet);
|
---|
368 | break;
|
---|
369 | case ICMP_V6_ROUTER_ADVERTISE:
|
---|
370 | Status = Ip6ProcessRouterAdvertise (IpSb, Head, Packet);
|
---|
371 | break;
|
---|
372 | case ICMP_V6_REDIRECT:
|
---|
373 | Status = Ip6ProcessRedirect (IpSb, Head, Packet);
|
---|
374 | break;
|
---|
375 | case ICMP_V6_ECHO_REPLY:
|
---|
376 | Status = Ip6Demultiplex (IpSb, Head, Packet);
|
---|
377 | break;
|
---|
378 | default:
|
---|
379 | Status = EFI_INVALID_PARAMETER;
|
---|
380 | break;
|
---|
381 | }
|
---|
382 |
|
---|
383 | return Status;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /**
|
---|
387 | Handle the ICMPv6 packet. First validate the message format,
|
---|
388 | then, according to the message types, process it as an informational packet or
|
---|
389 | an error packet.
|
---|
390 |
|
---|
391 | @param[in] IpSb The IP service that received the packet.
|
---|
392 | @param[in] Head The IP head of the ICMPv6 packet.
|
---|
393 | @param[in] Packet The content of the ICMPv6 packet with IP head
|
---|
394 | removed.
|
---|
395 |
|
---|
396 | @retval EFI_INVALID_PARAMETER The packet is malformatted.
|
---|
397 | @retval EFI_SUCCESS The ICMPv6 message successfully processed.
|
---|
398 | @retval Others Failed to handle the ICMPv6 packet.
|
---|
399 |
|
---|
400 | **/
|
---|
401 | EFI_STATUS
|
---|
402 | Ip6IcmpHandle (
|
---|
403 | IN IP6_SERVICE *IpSb,
|
---|
404 | IN EFI_IP6_HEADER *Head,
|
---|
405 | IN NET_BUF *Packet
|
---|
406 | )
|
---|
407 | {
|
---|
408 | IP6_ICMP_HEAD Icmp;
|
---|
409 | UINT16 PseudoCheckSum;
|
---|
410 | UINT16 CheckSum;
|
---|
411 |
|
---|
412 | //
|
---|
413 | // Check the validity of the incoming packet.
|
---|
414 | //
|
---|
415 | if (Packet->TotalSize < sizeof (Icmp)) {
|
---|
416 | goto DROP;
|
---|
417 | }
|
---|
418 |
|
---|
419 | NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *)&Icmp);
|
---|
420 |
|
---|
421 | //
|
---|
422 | // Make sure checksum is valid.
|
---|
423 | //
|
---|
424 | PseudoCheckSum = NetIp6PseudoHeadChecksum (
|
---|
425 | &Head->SourceAddress,
|
---|
426 | &Head->DestinationAddress,
|
---|
427 | IP6_ICMP,
|
---|
428 | Packet->TotalSize
|
---|
429 | );
|
---|
430 | CheckSum = (UINT16) ~NetAddChecksum (PseudoCheckSum, NetbufChecksum (Packet));
|
---|
431 | if (CheckSum != 0) {
|
---|
432 | goto DROP;
|
---|
433 | }
|
---|
434 |
|
---|
435 | //
|
---|
436 | // According to the packet type, call corresponding process
|
---|
437 | //
|
---|
438 | if (Icmp.Type <= ICMP_V6_ERROR_MAX) {
|
---|
439 | return Ip6ProcessIcmpError (IpSb, Head, Packet);
|
---|
440 | } else {
|
---|
441 | return Ip6ProcessIcmpInformation (IpSb, Head, Packet);
|
---|
442 | }
|
---|
443 |
|
---|
444 | DROP:
|
---|
445 | NetbufFree (Packet);
|
---|
446 | return EFI_INVALID_PARAMETER;
|
---|
447 | }
|
---|
448 |
|
---|
449 | /**
|
---|
450 | Retrieve the Prefix address according to the PrefixLength by clear the useless
|
---|
451 | bits.
|
---|
452 |
|
---|
453 | @param[in] PrefixLength The prefix length of the prefix.
|
---|
454 | @param[in, out] Prefix On input, points to the original prefix address
|
---|
455 | with dirty bits; on output, points to the updated
|
---|
456 | address with useless bit clear.
|
---|
457 |
|
---|
458 | **/
|
---|
459 | VOID
|
---|
460 | Ip6GetPrefix (
|
---|
461 | IN UINT8 PrefixLength,
|
---|
462 | IN OUT EFI_IPv6_ADDRESS *Prefix
|
---|
463 | )
|
---|
464 | {
|
---|
465 | UINT8 Byte;
|
---|
466 | UINT8 Bit;
|
---|
467 | UINT8 Mask;
|
---|
468 | UINT8 Value;
|
---|
469 |
|
---|
470 | ASSERT ((Prefix != NULL) && (PrefixLength < IP6_PREFIX_MAX));
|
---|
471 |
|
---|
472 | if (PrefixLength == 0) {
|
---|
473 | ZeroMem (Prefix, sizeof (EFI_IPv6_ADDRESS));
|
---|
474 | return;
|
---|
475 | }
|
---|
476 |
|
---|
477 | if (PrefixLength >= IP6_PREFIX_MAX) {
|
---|
478 | return;
|
---|
479 | }
|
---|
480 |
|
---|
481 | Byte = (UINT8)(PrefixLength / 8);
|
---|
482 | Bit = (UINT8)(PrefixLength % 8);
|
---|
483 | Value = Prefix->Addr[Byte];
|
---|
484 |
|
---|
485 | if (Byte > 0) {
|
---|
486 | ZeroMem (Prefix->Addr + Byte, 16 - Byte);
|
---|
487 | }
|
---|
488 |
|
---|
489 | if (Bit > 0) {
|
---|
490 | Mask = (UINT8)(0xFF << (8 - Bit));
|
---|
491 | Prefix->Addr[Byte] = (UINT8)(Value & Mask);
|
---|
492 | }
|
---|
493 | }
|
---|
494 |
|
---|
495 | /**
|
---|
496 | Check whether the DestinationAddress is an anycast address.
|
---|
497 |
|
---|
498 | @param[in] IpSb The IP service that received the packet.
|
---|
499 | @param[in] DestinationAddress Points to the Destination Address of the packet.
|
---|
500 |
|
---|
501 | @retval TRUE The DestinationAddress is anycast address.
|
---|
502 | @retval FALSE The DestinationAddress is not anycast address.
|
---|
503 |
|
---|
504 | **/
|
---|
505 | BOOLEAN
|
---|
506 | Ip6IsAnycast (
|
---|
507 | IN IP6_SERVICE *IpSb,
|
---|
508 | IN EFI_IPv6_ADDRESS *DestinationAddress
|
---|
509 | )
|
---|
510 | {
|
---|
511 | IP6_PREFIX_LIST_ENTRY *PrefixEntry;
|
---|
512 | EFI_IPv6_ADDRESS Prefix;
|
---|
513 | BOOLEAN Flag;
|
---|
514 |
|
---|
515 | ZeroMem (&Prefix, sizeof (EFI_IPv6_ADDRESS));
|
---|
516 |
|
---|
517 | Flag = FALSE;
|
---|
518 |
|
---|
519 | //
|
---|
520 | // If the address is known as on-link or autonomous prefix, record it as
|
---|
521 | // anycast address.
|
---|
522 | //
|
---|
523 | do {
|
---|
524 | PrefixEntry = Ip6FindPrefixListEntry (IpSb, Flag, 255, DestinationAddress);
|
---|
525 | if (PrefixEntry != NULL) {
|
---|
526 | IP6_COPY_ADDRESS (&Prefix, &PrefixEntry->Prefix);
|
---|
527 | Ip6GetPrefix (PrefixEntry->PrefixLength, &Prefix);
|
---|
528 | if (EFI_IP6_EQUAL (&Prefix, DestinationAddress)) {
|
---|
529 | return TRUE;
|
---|
530 | }
|
---|
531 | }
|
---|
532 |
|
---|
533 | Flag = (BOOLEAN) !Flag;
|
---|
534 | } while (Flag);
|
---|
535 |
|
---|
536 | return FALSE;
|
---|
537 | }
|
---|
538 |
|
---|
539 | /**
|
---|
540 | Generate ICMPv6 error message and send it out to DestinationAddress. Currently
|
---|
541 | Destination Unreachable message, Time Exceeded message and Parameter Problem
|
---|
542 | message are supported.
|
---|
543 |
|
---|
544 | @param[in] IpSb The IP service that received the packet.
|
---|
545 | @param[in] Packet The packet which invoking ICMPv6 error.
|
---|
546 | @param[in] SourceAddress If not NULL, points to the SourceAddress.
|
---|
547 | Otherwise, the IP layer will select a source address
|
---|
548 | according to the DestinationAddress.
|
---|
549 | @param[in] DestinationAddress Points to the Destination Address of the ICMPv6
|
---|
550 | error message.
|
---|
551 | @param[in] Type The type of the ICMPv6 message.
|
---|
552 | @param[in] Code The additional level of the ICMPv6 message.
|
---|
553 | @param[in] Pointer If not NULL, identifies the octet offset within
|
---|
554 | the invoking packet where the error was detected.
|
---|
555 |
|
---|
556 | @retval EFI_INVALID_PARAMETER The packet is malformatted.
|
---|
557 | @retval EFI_OUT_OF_RESOURCES There is no sufficient resource to complete the
|
---|
558 | operation.
|
---|
559 | @retval EFI_SUCCESS The ICMPv6 message was successfully sent out.
|
---|
560 | @retval Others Failed to generate the ICMPv6 packet.
|
---|
561 |
|
---|
562 | **/
|
---|
563 | EFI_STATUS
|
---|
564 | Ip6SendIcmpError (
|
---|
565 | IN IP6_SERVICE *IpSb,
|
---|
566 | IN NET_BUF *Packet,
|
---|
567 | IN EFI_IPv6_ADDRESS *SourceAddress OPTIONAL,
|
---|
568 | IN EFI_IPv6_ADDRESS *DestinationAddress,
|
---|
569 | IN UINT8 Type,
|
---|
570 | IN UINT8 Code,
|
---|
571 | IN UINT32 *Pointer OPTIONAL
|
---|
572 | )
|
---|
573 | {
|
---|
574 | UINT32 PacketLen;
|
---|
575 | NET_BUF *ErrorMsg;
|
---|
576 | UINT16 PayloadLen;
|
---|
577 | EFI_IP6_HEADER Head;
|
---|
578 | IP6_ICMP_INFORMATION_HEAD *IcmpHead;
|
---|
579 | UINT8 *ErrorBody;
|
---|
580 |
|
---|
581 | if (DestinationAddress == NULL) {
|
---|
582 | return EFI_INVALID_PARAMETER;
|
---|
583 | }
|
---|
584 |
|
---|
585 | //
|
---|
586 | // An ICMPv6 error message must not be originated as a result of receiving
|
---|
587 | // a packet whose source address does not uniquely identify a single node --
|
---|
588 | // e.g., the IPv6 Unspecified Address, an IPv6 multicast address, or an address
|
---|
589 | // known by the ICMP message originator to be an IPv6 anycast address.
|
---|
590 | //
|
---|
591 | if (NetIp6IsUnspecifiedAddr (DestinationAddress) ||
|
---|
592 | IP6_IS_MULTICAST (DestinationAddress) ||
|
---|
593 | Ip6IsAnycast (IpSb, DestinationAddress)
|
---|
594 | )
|
---|
595 | {
|
---|
596 | return EFI_INVALID_PARAMETER;
|
---|
597 | }
|
---|
598 |
|
---|
599 | switch (Type) {
|
---|
600 | case ICMP_V6_DEST_UNREACHABLE:
|
---|
601 | case ICMP_V6_TIME_EXCEEDED:
|
---|
602 | break;
|
---|
603 |
|
---|
604 | case ICMP_V6_PARAMETER_PROBLEM:
|
---|
605 | if (Pointer == NULL) {
|
---|
606 | return EFI_INVALID_PARAMETER;
|
---|
607 | }
|
---|
608 |
|
---|
609 | break;
|
---|
610 |
|
---|
611 | default:
|
---|
612 | return EFI_INVALID_PARAMETER;
|
---|
613 | }
|
---|
614 |
|
---|
615 | PacketLen = sizeof (IP6_ICMP_ERROR_HEAD) + Packet->TotalSize;
|
---|
616 |
|
---|
617 | if (PacketLen > IpSb->MaxPacketSize) {
|
---|
618 | PacketLen = IpSb->MaxPacketSize;
|
---|
619 | }
|
---|
620 |
|
---|
621 | ErrorMsg = NetbufAlloc (PacketLen);
|
---|
622 | if (ErrorMsg == NULL) {
|
---|
623 | return EFI_OUT_OF_RESOURCES;
|
---|
624 | }
|
---|
625 |
|
---|
626 | PayloadLen = (UINT16)(PacketLen - sizeof (EFI_IP6_HEADER));
|
---|
627 |
|
---|
628 | //
|
---|
629 | // Create the basic IPv6 header.
|
---|
630 | //
|
---|
631 | ZeroMem (&Head, sizeof (EFI_IP6_HEADER));
|
---|
632 |
|
---|
633 | Head.PayloadLength = HTONS (PayloadLen);
|
---|
634 | Head.NextHeader = IP6_ICMP;
|
---|
635 | Head.HopLimit = IpSb->CurHopLimit;
|
---|
636 |
|
---|
637 | if (SourceAddress != NULL) {
|
---|
638 | IP6_COPY_ADDRESS (&Head.SourceAddress, SourceAddress);
|
---|
639 | } else {
|
---|
640 | ZeroMem (&Head.SourceAddress, sizeof (EFI_IPv6_ADDRESS));
|
---|
641 | }
|
---|
642 |
|
---|
643 | IP6_COPY_ADDRESS (&Head.DestinationAddress, DestinationAddress);
|
---|
644 |
|
---|
645 | NetbufReserve (ErrorMsg, sizeof (EFI_IP6_HEADER));
|
---|
646 |
|
---|
647 | //
|
---|
648 | // Fill in the ICMP error message head
|
---|
649 | //
|
---|
650 | IcmpHead = (IP6_ICMP_INFORMATION_HEAD *)NetbufAllocSpace (ErrorMsg, sizeof (IP6_ICMP_INFORMATION_HEAD), FALSE);
|
---|
651 | if (IcmpHead == NULL) {
|
---|
652 | NetbufFree (ErrorMsg);
|
---|
653 | return EFI_OUT_OF_RESOURCES;
|
---|
654 | }
|
---|
655 |
|
---|
656 | ZeroMem (IcmpHead, sizeof (IP6_ICMP_INFORMATION_HEAD));
|
---|
657 | IcmpHead->Head.Type = Type;
|
---|
658 | IcmpHead->Head.Code = Code;
|
---|
659 |
|
---|
660 | if (Pointer != NULL) {
|
---|
661 | IcmpHead->Fourth = HTONL (*Pointer);
|
---|
662 | }
|
---|
663 |
|
---|
664 | //
|
---|
665 | // Fill in the ICMP error message body
|
---|
666 | //
|
---|
667 | PayloadLen -= sizeof (IP6_ICMP_INFORMATION_HEAD);
|
---|
668 | ErrorBody = NetbufAllocSpace (ErrorMsg, PayloadLen, FALSE);
|
---|
669 | if (ErrorBody != NULL) {
|
---|
670 | ZeroMem (ErrorBody, PayloadLen);
|
---|
671 | NetbufCopy (Packet, 0, PayloadLen, ErrorBody);
|
---|
672 | }
|
---|
673 |
|
---|
674 | //
|
---|
675 | // Transmit the packet
|
---|
676 | //
|
---|
677 | return Ip6Output (IpSb, NULL, NULL, ErrorMsg, &Head, NULL, 0, Ip6SysPacketSent, NULL);
|
---|
678 | }
|
---|