Add Network1 profile.
[mirror_edk2.git] / ShellPkg / Library / UefiShellNetwork1CommandsLib / Ping.c
CommitLineData
68fb0527 1/** @file\r
2 The implementation for Ping shell command.\r
3\r
4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
5\r
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php.\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "UefiShellNetwork1CommandsLib.h"\r
17\r
18#define PING_IP4_COPY_ADDRESS(Dest, Src) (CopyMem ((Dest), (Src), sizeof (EFI_IPv4_ADDRESS)))\r
19\r
20\r
21//\r
22// Function templates to match the IPv4 and IPv6 commands that we use.\r
23//\r
24typedef \r
25EFI_STATUS\r
26(EFIAPI *PING_IPX_POLL)(\r
27 IN VOID *This\r
28 ); \r
29\r
30typedef \r
31EFI_STATUS\r
32(EFIAPI *PING_IPX_TRANSMIT)(\r
33 IN VOID *This,\r
34 IN VOID *Token\r
35 );\r
36\r
37typedef \r
38EFI_STATUS\r
39(EFIAPI *PING_IPX_RECEIVE)(\r
40 IN VOID *This,\r
41 IN VOID *Token\r
42 ); \r
43\r
44typedef\r
45EFI_STATUS\r
46(EFIAPI *PING_IPX_CANCEL)(\r
47 IN VOID *This,\r
48 IN VOID *Token OPTIONAL\r
49 );\r
50\r
51///\r
52/// A set of pointers to either IPv6 or IPv4 functions. \r
53/// Unknown which one to the ping command.\r
54///\r
55typedef struct {\r
56 PING_IPX_TRANSMIT Transmit;\r
57 PING_IPX_RECEIVE Receive;\r
58 PING_IPX_CANCEL Cancel;\r
59 PING_IPX_POLL Poll;\r
60}PING_IPX_PROTOCOL;\r
61\r
62\r
63typedef union {\r
64 VOID *RxData;\r
65 VOID *TxData;\r
66} PING_PACKET;\r
67\r
68//\r
69// PING_IPX_COMPLETION_TOKEN\r
70// structures are used for both transmit and receive operations. \r
71// This version is IP-unaware.\r
72//\r
73typedef struct {\r
74 EFI_EVENT Event;\r
75 EFI_STATUS Status;\r
76 PING_PACKET Packet;\r
77} PING_IPX_COMPLETION_TOKEN;\r
78\r
79#pragma pack(1)\r
80typedef struct _ICMPX_ECHO_REQUEST_REPLY {\r
81 UINT8 Type;\r
82 UINT8 Code;\r
83 UINT16 Checksum;\r
84 UINT16 Identifier;\r
85 UINT16 SequenceNum;\r
86 UINT64 TimeStamp;\r
87 UINT8 Data[1];\r
88} ICMPX_ECHO_REQUEST_REPLY;\r
89#pragma pack()\r
90\r
91typedef struct _PING_ICMP_TX_INFO {\r
92 LIST_ENTRY Link;\r
93 UINT16 SequenceNum;\r
94 UINT64 TimeStamp;\r
95 PING_IPX_COMPLETION_TOKEN *Token;\r
96} PING_ICMPX_TX_INFO;\r
97\r
98#define DEFAULT_TIMEOUT 5000\r
99#define MAX_SEND_NUMBER 10000\r
100#define MAX_BUFFER_SIZE 32768\r
101#define DEFAULT_TIMER_PERIOD 358049\r
102#define ONE_SECOND 10000000\r
103#define PING_IP_CHOICE_IP4 1\r
104#define PING_IP_CHOICE_IP6 2\r
105#define DEFAULT_SEND_COUNT 10\r
106#define DEFAULT_BUFFER_SIZE 16\r
107#define ICMP_V4_ECHO_REQUEST 0x8\r
108#define ICMP_V4_ECHO_REPLY 0x0\r
109\r
110#define PING_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('P', 'i', 'n', 'g')\r
111typedef struct _PING_PRIVATE_DATA {\r
112 UINT32 Signature;\r
113 EFI_HANDLE NicHandle;\r
114 EFI_HANDLE IpChildHandle;\r
115 EFI_EVENT Timer;\r
116\r
117 EFI_STATUS Status;\r
118 LIST_ENTRY TxList;\r
119 UINT16 RxCount;\r
120 UINT16 TxCount;\r
121 UINT64 RttSum;\r
122 UINT64 RttMin;\r
123 UINT64 RttMax;\r
124 UINT32 SequenceNum;\r
125\r
126 UINT32 SendNum;\r
127 UINT32 BufferSize;\r
128 UINT32 IpChoice;\r
129\r
130 PING_IPX_PROTOCOL ProtocolPointers;\r
131 VOID *IpProtocol;\r
132 UINT8 SrcAddress[MAX(sizeof(EFI_IPv6_ADDRESS) , sizeof(EFI_IPv4_ADDRESS) )];\r
133 UINT8 DstAddress[MAX(sizeof(EFI_IPv6_ADDRESS) , sizeof(EFI_IPv4_ADDRESS) )];\r
134 PING_IPX_COMPLETION_TOKEN RxToken;\r
135} PING_PRIVATE_DATA;\r
136\r
137UINT16\r
138EFIAPI\r
139NetChecksum (\r
140 IN UINT8 *Buffer,\r
141 IN UINT32 Length\r
142 )\r
143/*++\r
144\r
145Routine Description:\r
146\r
147 Calculate the internet checksum (see RFC 1071)\r
148\r
149Arguments:\r
150\r
151 Packet - Buffer which contains the data to be checksummed\r
152 Length - Length to be checksummed\r
153\r
154Returns:\r
155\r
156 Checksum - Returns the 16 bit ones complement of \r
157 ones complement sum of 16 bit words\r
158\r
159--*/\r
160{\r
161 UINT32 Sum;\r
162 UINT8 Odd;\r
163 UINT16 *Packet;\r
164\r
165 Packet = (UINT16 *) Buffer;\r
166\r
167 Sum = 0;\r
168 Odd = (UINT8) (Length & 1);\r
169 Length >>= 1;\r
170 while (Length--) {\r
171 Sum += *Packet++;\r
172 }\r
173\r
174 if (Odd) {\r
175 Sum += *(UINT8 *) Packet;\r
176 }\r
177\r
178 Sum = (Sum & 0xffff) + (Sum >> 16);\r
179\r
180 //\r
181 // in case above carried\r
182 //\r
183 Sum += Sum >> 16;\r
184\r
185 return (UINT16) Sum;\r
186}\r
187\r
188/**\r
189 Reads and returns the current value of register.\r
190 In IA64, the register is the Interval Timer Vector (ITV).\r
191 In X86(IA32/X64), the register is the Time Stamp Counter (TSC)\r
192\r
193 @return The current value of the register.\r
194\r
195**/\r
196UINT64\r
197EFIAPI\r
198ReadTime (\r
199 VOID\r
200 );\r
201\r
202STATIC CONST SHELL_PARAM_ITEM PingParamList[] = {\r
203 {\r
204 L"-l",\r
205 TypeValue\r
206 },\r
207 {\r
208 L"-n",\r
209 TypeValue\r
210 },\r
211 {\r
212 L"-_s",\r
213 TypeValue\r
214 },\r
215 {\r
216 L"-_ip6",\r
217 TypeFlag\r
218 },\r
219 {\r
220 NULL,\r
221 TypeMax\r
222 },\r
223};\r
224\r
225//\r
226// Global Variables in Ping command.\r
227//\r
228STATIC CONST CHAR16 *mDstString;\r
229STATIC CONST CHAR16 *mSrcString;\r
230STATIC UINT64 mFrequency = 0;\r
231\r
232/**\r
233 Get and caculate the frequency in tick/ms.\r
234 The result is saved in the globle variable mFrequency\r
235\r
236 @retval EFI_SUCCESS Caculated the frequency successfully.\r
237 @retval Others Failed to caculate the frequency.\r
238\r
239**/\r
240EFI_STATUS\r
241EFIAPI\r
242GetFrequency (\r
243 VOID\r
244 )\r
245{\r
246 EFI_STATUS Status;\r
247 EFI_CPU_ARCH_PROTOCOL *Cpu;\r
248 UINT64 CurrentTick;\r
249 UINT64 TimerPeriod;\r
250\r
251 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **) &Cpu);\r
252\r
253 if (EFI_ERROR (Status)) {\r
254 return Status;\r
255 }\r
256\r
257 Status = Cpu->GetTimerValue (Cpu, 0, &CurrentTick, &TimerPeriod);\r
258\r
259 if (EFI_ERROR (Status)) {\r
260 TimerPeriod = DEFAULT_TIMER_PERIOD;\r
261 }\r
262\r
263 //\r
264 // The timer period is in femtosecond (1 femtosecond is 1e-15 second).\r
265 // So 1e+12 is divided by timer period to produce the freq in tick/ms.\r
266 //\r
267 mFrequency = DivU64x64Remainder (1000000000000ULL, TimerPeriod, NULL);\r
268\r
269 return EFI_SUCCESS;\r
270}\r
271\r
272/**\r
273 Caculate a duration in ms.\r
274\r
275 @param[in] Begin The start point of time.\r
276 @param[in] End The end point of time.\r
277\r
278 @return The duration in ms.\r
279 @retval 0 The parameters were not valid.\r
280**/\r
281UINT64\r
282EFIAPI\r
283CalculateTick (\r
284 IN UINT64 Begin,\r
285 IN UINT64 End\r
286 )\r
287{\r
288 if (End <= Begin) {\r
289 return (0);\r
290 }\r
291 return DivU64x64Remainder (End - Begin, mFrequency, NULL);\r
292}\r
293\r
294/**\r
295 Destroy PING_ICMPX_TX_INFO, and recollect the memory.\r
296\r
297 @param[in] TxInfo The pointer to PING_ICMPX_TX_INFO.\r
298 @param[in] IpChoice Whether the token is IPv4 or IPv6\r
299**/\r
300VOID\r
301EFIAPI\r
302PingDestroyTxInfo (\r
303 IN PING_ICMPX_TX_INFO *TxInfo,\r
304 IN UINT32 IpChoice\r
305 )\r
306{\r
307 EFI_IP6_TRANSMIT_DATA *Ip6TxData;\r
308 EFI_IP4_TRANSMIT_DATA *Ip4TxData;\r
309 EFI_IP6_FRAGMENT_DATA *FragData;\r
310 UINTN Index;\r
311\r
312 if (TxInfo == NULL) {\r
313 return;\r
314 }\r
315\r
316 if (TxInfo->Token != NULL) {\r
317\r
318 if (TxInfo->Token->Event != NULL) {\r
319 gBS->CloseEvent (TxInfo->Token->Event);\r
320 }\r
321\r
322 if (TxInfo->Token->Packet.TxData != NULL) {\r
323 if (IpChoice == PING_IP_CHOICE_IP6) {\r
324 Ip6TxData = TxInfo->Token->Packet.TxData;\r
325\r
326 if (Ip6TxData->OverrideData != NULL) {\r
327 FreePool (Ip6TxData->OverrideData);\r
328 }\r
329\r
330 if (Ip6TxData->ExtHdrs != NULL) {\r
331 FreePool (Ip6TxData->ExtHdrs);\r
332 }\r
333\r
334 for (Index = 0; Index < Ip6TxData->FragmentCount; Index++) {\r
335 FragData = Ip6TxData->FragmentTable[Index].FragmentBuffer;\r
336 if (FragData != NULL) {\r
337 FreePool (FragData);\r
338 }\r
339 }\r
340 } else {\r
341 Ip4TxData = TxInfo->Token->Packet.TxData;\r
342\r
343 if (Ip4TxData->OverrideData != NULL) {\r
344 FreePool (Ip4TxData->OverrideData);\r
345 }\r
346\r
347 for (Index = 0; Index < Ip4TxData->FragmentCount; Index++) {\r
348 FragData = Ip4TxData->FragmentTable[Index].FragmentBuffer;\r
349 if (FragData != NULL) {\r
350 FreePool (FragData);\r
351 }\r
352 }\r
353 }\r
354 }\r
355\r
356 FreePool (TxInfo->Token);\r
357 }\r
358\r
359 FreePool (TxInfo);\r
360}\r
361\r
362/**\r
363 Match the request, and reply with SequenceNum/TimeStamp.\r
364\r
365 @param[in] Private The pointer to PING_PRIVATE_DATA.\r
366 @param[in] Packet The pointer to ICMPX_ECHO_REQUEST_REPLY.\r
367\r
368 @retval EFI_SUCCESS The match is successful.\r
369 @retval EFI_NOT_FOUND The reply can't be matched with any request.\r
370\r
371**/\r
372EFI_STATUS\r
373EFIAPI\r
374Ping6MatchEchoReply (\r
375 IN PING_PRIVATE_DATA *Private,\r
376 IN ICMPX_ECHO_REQUEST_REPLY *Packet\r
377 )\r
378{\r
379 PING_ICMPX_TX_INFO *TxInfo;\r
380 LIST_ENTRY *Entry;\r
381 LIST_ENTRY *NextEntry;\r
382\r
383 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Private->TxList) {\r
384 TxInfo = BASE_CR (Entry, PING_ICMPX_TX_INFO, Link);\r
385\r
386 if ((TxInfo->SequenceNum == Packet->SequenceNum) && (TxInfo->TimeStamp == Packet->TimeStamp)) {\r
387 Private->RxCount++;\r
388 RemoveEntryList (&TxInfo->Link);\r
389 PingDestroyTxInfo (TxInfo, Private->IpChoice);\r
390 return EFI_SUCCESS;\r
391 }\r
392 }\r
393\r
394 return EFI_NOT_FOUND;\r
395}\r
396\r
397/**\r
398 The original intention is to send a request.\r
399 Currently, the application retransmits an icmp6 echo request packet\r
400 per second in sendnumber times that is specified by the user.\r
401 Because nothing can be done here, all things move to the timer rountine.\r
402\r
403 @param[in] Event A EFI_EVENT type event.\r
404 @param[in] Context The pointer to Context.\r
405\r
406**/\r
407VOID\r
408EFIAPI\r
409Ping6OnEchoRequestSent (\r
410 IN EFI_EVENT Event,\r
411 IN VOID *Context\r
412 )\r
413{\r
414}\r
415\r
416/**\r
417 receive reply, match and print reply infomation.\r
418\r
419 @param[in] Event A EFI_EVENT type event.\r
420 @param[in] Context The pointer to context.\r
421\r
422**/\r
423VOID\r
424EFIAPI\r
425Ping6OnEchoReplyReceived (\r
426 IN EFI_EVENT Event,\r
427 IN VOID *Context\r
428 )\r
429{\r
430 EFI_STATUS Status;\r
431 PING_PRIVATE_DATA *Private;\r
432 ICMPX_ECHO_REQUEST_REPLY *Reply;\r
433 UINT32 PayLoad;\r
434 UINT64 Rtt;\r
435 CHAR8 Near;\r
436\r
437 Private = (PING_PRIVATE_DATA *) Context;\r
438\r
439 if (Private == NULL || Private->Status == EFI_ABORTED || Private->Signature != PING_PRIVATE_DATA_SIGNATURE) {\r
440 return;\r
441 }\r
442\r
443 if (Private->RxToken.Packet.RxData == NULL) {\r
444 return;\r
445 }\r
446\r
447 if (Private->IpChoice == PING_IP_CHOICE_IP6) {\r
448 Reply = ((EFI_IP6_RECEIVE_DATA*)Private->RxToken.Packet.RxData)->FragmentTable[0].FragmentBuffer;\r
449 PayLoad = ((EFI_IP6_RECEIVE_DATA*)Private->RxToken.Packet.RxData)->DataLength;\r
450 if (((EFI_IP6_RECEIVE_DATA*)Private->RxToken.Packet.RxData)->Header->NextHeader != IP6_ICMP) {\r
451 goto ON_EXIT;\r
452 }\r
453 if (!IP6_IS_MULTICAST ((EFI_IPv6_ADDRESS*)&Private->DstAddress) && \r
454 !EFI_IP6_EQUAL (&((EFI_IP6_RECEIVE_DATA*)Private->RxToken.Packet.RxData)->Header->SourceAddress, (EFI_IPv6_ADDRESS*)&Private->DstAddress)) {\r
455 goto ON_EXIT;\r
456 }\r
457\r
458 if ((Reply->Type != ICMP_V6_ECHO_REPLY) || (Reply->Code != 0)) {\r
459 goto ON_EXIT;\r
460 }\r
461 } else {\r
462 Reply = ((EFI_IP4_RECEIVE_DATA*)Private->RxToken.Packet.RxData)->FragmentTable[0].FragmentBuffer;\r
463 PayLoad = ((EFI_IP4_RECEIVE_DATA*)Private->RxToken.Packet.RxData)->DataLength;\r
464 if (!IP4_IS_MULTICAST (EFI_IP4(*(EFI_IPv4_ADDRESS*)Private->DstAddress)) && \r
465 !EFI_IP4_EQUAL (&((EFI_IP4_RECEIVE_DATA*)Private->RxToken.Packet.RxData)->Header->SourceAddress, (EFI_IPv4_ADDRESS*)&Private->DstAddress)) {\r
466 goto ON_EXIT;\r
467 }\r
468\r
469 if ((Reply->Type != ICMP_V4_ECHO_REPLY) || (Reply->Code != 0)) {\r
470 goto ON_EXIT;\r
471 }\r
472 }\r
473 \r
474\r
475 if (PayLoad != Private->BufferSize) {\r
476 goto ON_EXIT;\r
477 }\r
478 //\r
479 // Check whether the reply matches the sent request before.\r
480 //\r
481 Status = Ping6MatchEchoReply (Private, Reply);\r
482 if (EFI_ERROR(Status)) {\r
483 goto ON_EXIT;\r
484 }\r
485 //\r
486 // Display statistics on this icmp6 echo reply packet.\r
487 //\r
488 Rtt = CalculateTick (Reply->TimeStamp, ReadTime ());\r
489 if (Rtt != 0) {\r
490 Near = (CHAR8) '=';\r
491 } else {\r
492 Near = (CHAR8) '<';\r
493 }\r
494\r
495 Private->RttSum += Rtt;\r
496 Private->RttMin = Private->RttMin > Rtt ? Rtt : Private->RttMin;\r
497 Private->RttMax = Private->RttMax < Rtt ? Rtt : Private->RttMax;\r
498\r
499 ShellPrintHiiEx (\r
500 -1,\r
501 -1,\r
502 NULL,\r
503 STRING_TOKEN (STR_PING_REPLY_INFO),\r
504 gShellNetwork1HiiHandle,\r
505 PayLoad,\r
506 mDstString,\r
507 Reply->SequenceNum,\r
508 Private->IpChoice == PING_IP_CHOICE_IP6?((EFI_IP6_RECEIVE_DATA*)Private->RxToken.Packet.RxData)->Header->HopLimit:0,\r
509 Near,\r
510 Rtt\r
511 );\r
512\r
513ON_EXIT:\r
514\r
515 if (Private->RxCount < Private->SendNum) {\r
516 //\r
517 // Continue to receive icmp echo reply packets.\r
518 //\r
519 Private->RxToken.Status = EFI_ABORTED;\r
520\r
521 Status = Private->ProtocolPointers.Receive (Private->IpProtocol, &Private->RxToken);\r
522\r
523 if (EFI_ERROR (Status)) {\r
524 Private->Status = EFI_ABORTED;\r
525 }\r
526 } else {\r
527 //\r
528 // All reply have already been received from the dest host.\r
529 //\r
530 Private->Status = EFI_SUCCESS;\r
531 }\r
532 //\r
533 // Singal to recycle the each rxdata here, not at the end of process.\r
534 //\r
535 gBS->SignalEvent (Private->IpChoice == PING_IP_CHOICE_IP6?((EFI_IP6_RECEIVE_DATA*)Private->RxToken.Packet.RxData)->RecycleSignal:((EFI_IP4_RECEIVE_DATA*)Private->RxToken.Packet.RxData)->RecycleSignal);\r
536}\r
537\r
538/**\r
539 Create a PING_IPX_COMPLETION_TOKEN.\r
540\r
541 @param[in] Private The pointer of PING_PRIVATE_DATA.\r
542 @param[in] TimeStamp The TimeStamp of request.\r
543 @param[in] SequenceNum The SequenceNum of request.\r
544\r
545 @return The pointer of PING_IPX_COMPLETION_TOKEN.\r
546\r
547**/\r
548PING_IPX_COMPLETION_TOKEN *\r
549EFIAPI\r
550PingGenerateToken (\r
551 IN PING_PRIVATE_DATA *Private,\r
552 IN UINT64 TimeStamp,\r
553 IN UINT16 SequenceNum\r
554 )\r
555{\r
556 EFI_STATUS Status;\r
557 PING_IPX_COMPLETION_TOKEN *Token;\r
558 VOID *TxData;\r
559 ICMPX_ECHO_REQUEST_REPLY *Request;\r
560 UINT16 HeadSum;\r
561 UINT16 TempChecksum;\r
562\r
563 Request = AllocateZeroPool (Private->BufferSize);\r
564 if (Request == NULL) {\r
565 return NULL;\r
566 }\r
567 TxData = AllocateZeroPool (Private->IpChoice==PING_IP_CHOICE_IP6?sizeof (EFI_IP6_TRANSMIT_DATA):sizeof (EFI_IP4_TRANSMIT_DATA));\r
568 if (TxData == NULL) {\r
569 FreePool (Request);\r
570 return NULL;\r
571 }\r
572 Token = AllocateZeroPool (sizeof (PING_IPX_COMPLETION_TOKEN));\r
573 if (Token == NULL) {\r
574 FreePool (Request);\r
575 FreePool (TxData);\r
576 return NULL;\r
577 }\r
578\r
579 //\r
580 // Assembly echo request packet.\r
581 //\r
582 Request->Type = (UINT8)(Private->IpChoice==PING_IP_CHOICE_IP6?ICMP_V6_ECHO_REQUEST:ICMP_V4_ECHO_REQUEST);\r
583 Request->Code = 0;\r
584 Request->SequenceNum = SequenceNum;\r
585 Request->Identifier = 0;\r
586 Request->Checksum = 0;\r
587\r
588 //\r
589 // Assembly token for transmit.\r
590 //\r
591 if (Private->IpChoice==PING_IP_CHOICE_IP6) {\r
592 ((EFI_IP6_TRANSMIT_DATA*)TxData)->ExtHdrsLength = 0;\r
593 ((EFI_IP6_TRANSMIT_DATA*)TxData)->ExtHdrs = NULL;\r
594 ((EFI_IP6_TRANSMIT_DATA*)TxData)->OverrideData = 0;\r
595 ((EFI_IP6_TRANSMIT_DATA*)TxData)->DataLength = Private->BufferSize;\r
596 ((EFI_IP6_TRANSMIT_DATA*)TxData)->FragmentCount = 1;\r
597 ((EFI_IP6_TRANSMIT_DATA*)TxData)->FragmentTable[0].FragmentBuffer = (VOID *) Request;\r
598 ((EFI_IP6_TRANSMIT_DATA*)TxData)->FragmentTable[0].FragmentLength = Private->BufferSize;\r
599 } else {\r
600 ((EFI_IP4_TRANSMIT_DATA*)TxData)->OptionsLength = 0;\r
601 ((EFI_IP4_TRANSMIT_DATA*)TxData)->OptionsBuffer = NULL;\r
602 ((EFI_IP4_TRANSMIT_DATA*)TxData)->OverrideData = 0;\r
603 ((EFI_IP4_TRANSMIT_DATA*)TxData)->TotalDataLength = Private->BufferSize;\r
604 ((EFI_IP4_TRANSMIT_DATA*)TxData)->FragmentCount = 1;\r
605 ((EFI_IP4_TRANSMIT_DATA*)TxData)->FragmentTable[0].FragmentBuffer = (VOID *) Request;\r
606 ((EFI_IP4_TRANSMIT_DATA*)TxData)->FragmentTable[0].FragmentLength = Private->BufferSize;\r
607 ((EFI_IP4_TRANSMIT_DATA*)TxData)->DestinationAddress.Addr[0] = Private->DstAddress[0];\r
608 ((EFI_IP4_TRANSMIT_DATA*)TxData)->DestinationAddress.Addr[1] = Private->DstAddress[1];\r
609 ((EFI_IP4_TRANSMIT_DATA*)TxData)->DestinationAddress.Addr[2] = Private->DstAddress[2];\r
610 ((EFI_IP4_TRANSMIT_DATA*)TxData)->DestinationAddress.Addr[3] = Private->DstAddress[3];\r
611\r
612 HeadSum = NetChecksum ((UINT8 *) Request, Private->BufferSize);\r
613 Request->TimeStamp = TimeStamp;\r
614 TempChecksum = NetChecksum ((UINT8 *) &Request->TimeStamp, sizeof (UINT64));\r
615 Request->Checksum = (UINT16)(~NetAddChecksum (HeadSum, TempChecksum));\r
616 }\r
617\r
618\r
619 Token->Status = EFI_ABORTED;\r
620 Token->Packet.TxData = TxData;\r
621\r
622 Status = gBS->CreateEvent (\r
623 EVT_NOTIFY_SIGNAL,\r
624 TPL_CALLBACK,\r
625 Ping6OnEchoRequestSent,\r
626 Private,\r
627 &Token->Event\r
628 );\r
629\r
630 if (EFI_ERROR (Status)) {\r
631 FreePool (Request);\r
632 FreePool (TxData);\r
633 FreePool (Token);\r
634 return NULL;\r
635 }\r
636\r
637 return Token;\r
638}\r
639\r
640/**\r
641 Transmit the PING_IPX_COMPLETION_TOKEN.\r
642\r
643 @param[in] Private The pointer of PING_PRIVATE_DATA.\r
644\r
645 @retval EFI_SUCCESS Transmitted successfully.\r
646 @retval EFI_OUT_OF_RESOURCES No memory is available on the platform.\r
647 @retval others Transmitted unsuccessfully.\r
648\r
649**/\r
650EFI_STATUS\r
651EFIAPI\r
652PingSendEchoRequest (\r
653 IN PING_PRIVATE_DATA *Private\r
654 )\r
655{\r
656 EFI_STATUS Status;\r
657 PING_ICMPX_TX_INFO *TxInfo;\r
658\r
659 TxInfo = AllocateZeroPool (sizeof (PING_ICMPX_TX_INFO));\r
660\r
661 if (TxInfo == NULL) {\r
662 return EFI_OUT_OF_RESOURCES;\r
663 }\r
664\r
665 TxInfo->TimeStamp = ReadTime ();\r
666 TxInfo->SequenceNum = (UINT16) (Private->TxCount + 1);\r
667 TxInfo->Token = PingGenerateToken (\r
668 Private,\r
669 TxInfo->TimeStamp,\r
670 TxInfo->SequenceNum\r
671 );\r
672\r
673 if (TxInfo->Token == NULL) {\r
674 PingDestroyTxInfo (TxInfo, Private->IpChoice);\r
675 return EFI_OUT_OF_RESOURCES;\r
676 }\r
677\r
678 ASSERT(Private->ProtocolPointers.Transmit != NULL);\r
679 Status = Private->ProtocolPointers.Transmit (Private->IpProtocol, TxInfo->Token);\r
680\r
681 if (EFI_ERROR (Status)) {\r
682 PingDestroyTxInfo (TxInfo, Private->IpChoice);\r
683 return Status;\r
684 }\r
685\r
686 InsertTailList (&Private->TxList, &TxInfo->Link);\r
687 Private->TxCount++;\r
688\r
689 return EFI_SUCCESS;\r
690}\r
691\r
692/**\r
693 Place a completion token into the receive packet queue to receive the echo reply.\r
694\r
695 @param[in] Private The pointer of PING_PRIVATE_DATA.\r
696\r
697 @retval EFI_SUCCESS Put the token into the receive packet queue successfully.\r
698 @retval others Put the token into the receive packet queue unsuccessfully.\r
699\r
700**/\r
701EFI_STATUS\r
702EFIAPI\r
703Ping6ReceiveEchoReply (\r
704 IN PING_PRIVATE_DATA *Private\r
705 )\r
706{\r
707 EFI_STATUS Status;\r
708\r
709 ZeroMem (&Private->RxToken, sizeof (PING_IPX_COMPLETION_TOKEN));\r
710\r
711 Status = gBS->CreateEvent (\r
712 EVT_NOTIFY_SIGNAL,\r
713 TPL_CALLBACK,\r
714 Ping6OnEchoReplyReceived,\r
715 Private,\r
716 &Private->RxToken.Event\r
717 );\r
718\r
719 if (EFI_ERROR (Status)) {\r
720 return Status;\r
721 }\r
722\r
723 Private->RxToken.Status = EFI_NOT_READY;\r
724\r
725 return (Private->ProtocolPointers.Receive (Private->IpProtocol, &Private->RxToken));\r
726}\r
727\r
728/**\r
729 Remove the timeout request from the list.\r
730\r
731 @param[in] Event A EFI_EVENT type event.\r
732 @param[in] Context The pointer to Context.\r
733\r
734**/\r
735VOID\r
736EFIAPI\r
737Ping6OnTimerRoutine (\r
738 IN EFI_EVENT Event,\r
739 IN VOID *Context\r
740 )\r
741{\r
742 EFI_STATUS Status;\r
743 PING_PRIVATE_DATA *Private;\r
744 PING_ICMPX_TX_INFO *TxInfo;\r
745 LIST_ENTRY *Entry;\r
746 LIST_ENTRY *NextEntry;\r
747 UINT64 Time;\r
748\r
749 Private = (PING_PRIVATE_DATA *) Context;\r
750 if (Private->Signature != PING_PRIVATE_DATA_SIGNATURE) {\r
751 Private->Status = EFI_NOT_FOUND;\r
752 return;\r
753 }\r
754\r
755 //\r
756 // Retransmit icmp6 echo request packets per second in sendnumber times.\r
757 //\r
758 if (Private->TxCount < Private->SendNum) {\r
759\r
760 Status = PingSendEchoRequest (Private);\r
761 if (Private->TxCount != 0){\r
762 if (EFI_ERROR (Status)) {\r
763 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_PING_SEND_REQUEST), gShellNetwork1HiiHandle, Private->TxCount + 1);\r
764 }\r
765 }\r
766 }\r
767 //\r
768 // Check whether any icmp6 echo request in the list timeout.\r
769 //\r
770 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Private->TxList) {\r
771 TxInfo = BASE_CR (Entry, PING_ICMPX_TX_INFO, Link);\r
772 Time = CalculateTick (TxInfo->TimeStamp, ReadTime ());\r
773\r
774 //\r
775 // Remove the timeout echo request from txlist.\r
776 //\r
777 if (Time > DEFAULT_TIMEOUT) {\r
778\r
779 if (EFI_ERROR (TxInfo->Token->Status)) {\r
780 Private->ProtocolPointers.Cancel (Private->IpProtocol, TxInfo->Token);\r
781 }\r
782 //\r
783 // Remove the timeout icmp6 echo request from list.\r
784 //\r
785 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_PING_TIMEOUT), gShellNetwork1HiiHandle, TxInfo->SequenceNum);\r
786\r
787 RemoveEntryList (&TxInfo->Link);\r
788 PingDestroyTxInfo (TxInfo, Private->IpChoice);\r
789\r
790 //\r
791 // We dont need to wait for this some other time...\r
792 //\r
793 Private->RxCount++;\r
794\r
795 if (IsListEmpty (&Private->TxList) && (Private->TxCount == Private->SendNum)) {\r
796 //\r
797 // All the left icmp6 echo request in the list timeout.\r
798 //\r
799 Private->Status = EFI_TIMEOUT;\r
800 }\r
801 }\r
802 }\r
803}\r
804\r
805/**\r
806 Determine if a IP4 address is Link Local.\r
807\r
808 169.254.1.0 through 169.254.254.255 is link local.\r
809\r
810 @param[in] Address The address to test.\r
811\r
812 @retval TRUE It is.\r
813 @retval FALSE It is not.\r
814**/\r
815BOOLEAN\r
816EFIAPI\r
817PingNetIp4IsLinkLocalAddr (\r
818 IN CONST EFI_IPv4_ADDRESS *Address\r
819 )\r
820{\r
821 return ((BOOLEAN)(Address->Addr[0] == 169 && Address->Addr[1] == 254 && Address->Addr[2] >= 1 && Address->Addr[2] <= 254));\r
822}\r
823\r
824/**\r
825 Determine if a IP4 address is unspecified.\r
826\r
827 @param[in] Address The address to test.\r
828\r
829 @retval TRUE It is.\r
830 @retval FALSE It is not.\r
831**/\r
832BOOLEAN\r
833EFIAPI\r
834PingNetIp4IsUnspecifiedAddr (\r
835 IN CONST EFI_IPv4_ADDRESS *Address\r
836 )\r
837{\r
838 return ((BOOLEAN)((ReadUnaligned32 ((UINT32*)&Address->Addr[0])) == 0x00000000));\r
839}\r
840\r
841/**\r
842 Create a valid IP instance.\r
843\r
844 @param[in] Private The pointer of PING_PRIVATE_DATA.\r
845\r
846 @retval EFI_SUCCESS Create a valid IPx instance successfully.\r
847 @retval EFI_ABORTED Locate handle with ipx service binding protocol unsuccessfully.\r
848 @retval EFI_INVALID_PARAMETER The source address is unspecified when the destination address is a link-local address.\r
849 @retval EFI_OUT_OF_RESOURCES No memory is available on the platform.\r
850 @retval EFI_NOT_FOUND The source address is not found.\r
851**/\r
852EFI_STATUS\r
853EFIAPI\r
854PingCreateIpInstance (\r
855 IN PING_PRIVATE_DATA *Private\r
856 )\r
857{\r
858 EFI_STATUS Status;\r
859 UINTN HandleIndex;\r
860 UINTN HandleNum;\r
861 EFI_HANDLE *HandleBuffer;\r
862 EFI_SERVICE_BINDING_PROTOCOL *EfiSb;\r
863 VOID *IpXCfg;\r
864 EFI_IP6_CONFIG_DATA Ip6Config;\r
865 EFI_IP4_CONFIG_DATA Ip4Config;\r
866 VOID *IpXInterfaceInfo;\r
867 UINTN IfInfoSize;\r
868 EFI_IPv6_ADDRESS *Addr;\r
869 UINTN AddrIndex;\r
870\r
871 HandleBuffer = NULL;\r
872 EfiSb = NULL;\r
873 IpXInterfaceInfo = NULL;\r
874 IfInfoSize = 0;\r
875\r
876 //\r
877 // Locate all the handles with ip6 service binding protocol.\r
878 //\r
879 Status = gBS->LocateHandleBuffer (\r
880 ByProtocol,\r
881 Private->IpChoice == PING_IP_CHOICE_IP6?&gEfiIp6ServiceBindingProtocolGuid:&gEfiIp4ServiceBindingProtocolGuid,\r
882 NULL,\r
883 &HandleNum,\r
884 &HandleBuffer\r
885 );\r
886 if (EFI_ERROR (Status) || (HandleNum == 0)) {\r
887 return EFI_ABORTED;\r
888 }\r
889 //\r
890 // Source address is required when pinging a link-local address on multi-\r
891 // interfaces host.\r
892 //\r
893 if (Private->IpChoice == PING_IP_CHOICE_IP6) {\r
894 if (NetIp6IsLinkLocalAddr ((EFI_IPv6_ADDRESS*)&Private->DstAddress) &&\r
895 NetIp6IsUnspecifiedAddr ((EFI_IPv6_ADDRESS*)&Private->SrcAddress) &&\r
896 (HandleNum > 1)) {\r
897 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellNetwork1HiiHandle, mSrcString);\r
898 Status = EFI_INVALID_PARAMETER;\r
899 goto ON_ERROR;\r
900 }\r
901 } else {\r
902 ASSERT(Private->IpChoice == PING_IP_CHOICE_IP4);\r
903 if (PingNetIp4IsLinkLocalAddr ((EFI_IPv4_ADDRESS*)&Private->DstAddress) &&\r
904 PingNetIp4IsUnspecifiedAddr ((EFI_IPv4_ADDRESS*)&Private->SrcAddress) &&\r
905 (HandleNum > 1)) {\r
906 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellNetwork1HiiHandle, mSrcString);\r
907 Status = EFI_INVALID_PARAMETER;\r
908 goto ON_ERROR;\r
909 }\r
910 }\r
911 //\r
912 // For each ip6 protocol, check interface addresses list.\r
913 //\r
914 for (HandleIndex = 0; HandleIndex < HandleNum; HandleIndex++) {\r
915\r
916 EfiSb = NULL;\r
917 IpXInterfaceInfo = NULL;\r
918 IfInfoSize = 0;\r
919\r
920 Status = gBS->HandleProtocol (\r
921 HandleBuffer[HandleIndex],\r
922 Private->IpChoice == PING_IP_CHOICE_IP6?&gEfiIp6ServiceBindingProtocolGuid:&gEfiIp4ServiceBindingProtocolGuid,\r
923 (VOID **) &EfiSb\r
924 );\r
925 if (EFI_ERROR (Status)) {\r
926 goto ON_ERROR;\r
927 }\r
928\r
929 if (Private->IpChoice == PING_IP_CHOICE_IP6?NetIp6IsUnspecifiedAddr ((EFI_IPv6_ADDRESS*)&Private->SrcAddress):PingNetIp4IsUnspecifiedAddr ((EFI_IPv4_ADDRESS*)&Private->SrcAddress)) {\r
930 //\r
931 // No need to match interface address.\r
932 //\r
933 break;\r
934 } else {\r
935 //\r
936 // Ip6config protocol and ip6 service binding protocol are installed\r
937 // on the same handle.\r
938 //\r
939 Status = gBS->HandleProtocol (\r
940 HandleBuffer[HandleIndex],\r
941 Private->IpChoice == PING_IP_CHOICE_IP6?&gEfiIp6ConfigProtocolGuid:&gEfiIp4ConfigProtocolGuid,\r
942 (VOID **) &IpXCfg\r
943 );\r
944\r
945 if (EFI_ERROR (Status)) {\r
946 goto ON_ERROR;\r
947 }\r
948 //\r
949 // Get the interface information size.\r
950 //\r
951 if (Private->IpChoice == PING_IP_CHOICE_IP6) {\r
952 Status = ((EFI_IP6_CONFIG_PROTOCOL*)IpXCfg)->GetData (\r
953 IpXCfg,\r
954 Ip6ConfigDataTypeInterfaceInfo,\r
955 &IfInfoSize,\r
956 NULL\r
957 );\r
958 } else {\r
959 Status = ((EFI_IP4_CONFIG_PROTOCOL*)IpXCfg)->GetData (\r
960 IpXCfg,\r
961 &IfInfoSize,\r
962 NULL\r
963 );\r
964 }\r
965 \r
966 //\r
967 // Skip the ones not in current use.\r
968 //\r
969 if (Status == EFI_NOT_STARTED) {\r
970 continue;\r
971 }\r
972\r
973 if (Status != EFI_BUFFER_TOO_SMALL) {\r
974 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_PING_GETDATA), gShellNetwork1HiiHandle, Status);\r
975 goto ON_ERROR;\r
976 }\r
977\r
978 IpXInterfaceInfo = AllocateZeroPool (IfInfoSize);\r
979\r
980 if (IpXInterfaceInfo == NULL) {\r
981 Status = EFI_OUT_OF_RESOURCES;\r
982 goto ON_ERROR;\r
983 }\r
984 //\r
985 // Get the interface info.\r
986 //\r
987 if (Private->IpChoice == PING_IP_CHOICE_IP6) {\r
988 Status = ((EFI_IP6_CONFIG_PROTOCOL*)IpXCfg)->GetData (\r
989 IpXCfg,\r
990 Ip6ConfigDataTypeInterfaceInfo,\r
991 &IfInfoSize,\r
992 IpXInterfaceInfo\r
993 );\r
994 } else {\r
995 Status = ((EFI_IP4_CONFIG_PROTOCOL*)IpXCfg)->GetData (\r
996 IpXCfg,\r
997 &IfInfoSize,\r
998 IpXInterfaceInfo\r
999 );\r
1000 }\r
1001\r
1002 if (EFI_ERROR (Status)) {\r
1003 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_PING_GETDATA), gShellNetwork1HiiHandle, Status);\r
1004 goto ON_ERROR;\r
1005 }\r
1006 //\r
1007 // Check whether the source address is one of the interface addresses.\r
1008 //\r
1009 if (Private->IpChoice == PING_IP_CHOICE_IP6) {\r
1010 for (AddrIndex = 0; AddrIndex < ((EFI_IP6_CONFIG_INTERFACE_INFO*)IpXInterfaceInfo)->AddressInfoCount; AddrIndex++) {\r
1011\r
1012 Addr = &(((EFI_IP6_CONFIG_INTERFACE_INFO*)IpXInterfaceInfo)->AddressInfo[AddrIndex].Address);\r
1013 if (EFI_IP6_EQUAL (&Private->SrcAddress, Addr)) {\r
1014 //\r
1015 // Match a certain interface address.\r
1016 //\r
1017 break;\r
1018 }\r
1019 }\r
1020\r
1021 if (AddrIndex < ((EFI_IP6_CONFIG_INTERFACE_INFO*)IpXInterfaceInfo)->AddressInfoCount) {\r
1022 //\r
1023 // Found a nic handle with right interface address.\r
1024 //\r
1025 break;\r
1026 }\r
1027 } else {\r
1028 //\r
1029 // IP4 address check\r
1030 //\r
1031 if (EFI_IP4_EQUAL (&Private->SrcAddress, &((EFI_IP4_IPCONFIG_DATA*)IpXInterfaceInfo)->StationAddress)) {\r
1032 //\r
1033 // Match a certain interface address.\r
1034 //\r
1035 break;\r
1036 }\r
1037 }\r
1038 }\r
1039\r
1040 FreePool (IpXInterfaceInfo);\r
1041 IpXInterfaceInfo = NULL;\r
1042 }\r
1043 //\r
1044 // No exact interface address matched.\r
1045 //\r
1046\r
1047 if (HandleIndex == HandleNum) {\r
1048 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellNetwork1HiiHandle, mSrcString);\r
1049 Status = EFI_NOT_FOUND;\r
1050 goto ON_ERROR;\r
1051 }\r
1052\r
1053 Private->NicHandle = HandleBuffer[HandleIndex];\r
1054\r
1055 ASSERT (EfiSb != NULL);\r
1056 Status = EfiSb->CreateChild (EfiSb, &Private->IpChildHandle);\r
1057\r
1058 if (EFI_ERROR (Status)) {\r
1059 goto ON_ERROR;\r
1060 }\r
1061 if (Private->IpChoice == PING_IP_CHOICE_IP6) {\r
1062 Status = gBS->OpenProtocol (\r
1063 Private->IpChildHandle,\r
1064 &gEfiIp6ProtocolGuid,\r
1065 &Private->IpProtocol,\r
1066 gImageHandle,\r
1067 Private->IpChildHandle,\r
1068 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1069 );\r
1070 if (EFI_ERROR (Status)) {\r
1071 goto ON_ERROR;\r
1072 }\r
1073\r
1074\r
1075 ZeroMem (&Ip6Config, sizeof (EFI_IP6_CONFIG_DATA));\r
1076\r
1077 //\r
1078 // Configure the ip6 instance for icmp6 packet exchange.\r
1079 //\r
1080 Ip6Config.DefaultProtocol = 58;\r
1081 Ip6Config.AcceptAnyProtocol = FALSE;\r
1082 Ip6Config.AcceptIcmpErrors = TRUE;\r
1083 Ip6Config.AcceptPromiscuous = FALSE;\r
1084 Ip6Config.TrafficClass = 0;\r
1085 Ip6Config.HopLimit = 128;\r
1086 Ip6Config.FlowLabel = 0;\r
1087 Ip6Config.ReceiveTimeout = 0;\r
1088 Ip6Config.TransmitTimeout = 0;\r
1089\r
1090 IP6_COPY_ADDRESS (&Ip6Config.StationAddress, &Private->SrcAddress);\r
1091 IP6_COPY_ADDRESS (&Ip6Config.DestinationAddress, &Private->DstAddress);\r
1092\r
1093 Status = ((EFI_IP6_PROTOCOL*)(Private->IpProtocol))->Configure (Private->IpProtocol, &Ip6Config);\r
1094\r
1095 if (EFI_ERROR (Status)) {\r
1096 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_PING_CONFIG), gShellNetwork1HiiHandle, Status);\r
1097 goto ON_ERROR;\r
1098 }\r
1099\r
1100 Private->ProtocolPointers.Transmit = (PING_IPX_TRANSMIT )((EFI_IP6_PROTOCOL*)Private->IpProtocol)->Transmit;\r
1101 Private->ProtocolPointers.Receive = (PING_IPX_RECEIVE )((EFI_IP6_PROTOCOL*)Private->IpProtocol)->Receive;\r
1102 Private->ProtocolPointers.Cancel = (PING_IPX_CANCEL )((EFI_IP6_PROTOCOL*)Private->IpProtocol)->Cancel;\r
1103 Private->ProtocolPointers.Poll = (PING_IPX_POLL )((EFI_IP6_PROTOCOL*)Private->IpProtocol)->Poll;\r
1104 } else {\r
1105 Status = gBS->OpenProtocol (\r
1106 Private->IpChildHandle,\r
1107 &gEfiIp4ProtocolGuid,\r
1108 &Private->IpProtocol,\r
1109 gImageHandle,\r
1110 Private->IpChildHandle,\r
1111 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1112 );\r
1113 if (EFI_ERROR (Status)) {\r
1114 goto ON_ERROR;\r
1115 }\r
1116\r
1117\r
1118 ZeroMem (&Ip4Config, sizeof (EFI_IP4_CONFIG_DATA));\r
1119\r
1120 //\r
1121 // Configure the ip4 instance for icmp4 packet exchange.\r
1122 //\r
1123// PING_IP4_COPY_ADDRESS (&Ip4Config.StationAddress, &Private->SrcAddress);\r
1124// Ip4Config.SubnetMask.Addr[0] = 0xFF;\r
1125// Ip4Config.SubnetMask.Addr[1] = 0xFF;\r
1126// Ip4Config.SubnetMask.Addr[2] = 0xFF;\r
1127// Ip4Config.SubnetMask.Addr[3] = 0x00;\r
1128 Ip4Config.DefaultProtocol = 1;\r
1129 Ip4Config.AcceptAnyProtocol = FALSE;\r
1130 Ip4Config.AcceptBroadcast = FALSE;\r
1131 Ip4Config.AcceptIcmpErrors = TRUE;\r
1132 Ip4Config.AcceptPromiscuous = FALSE;\r
1133 Ip4Config.DoNotFragment = FALSE;\r
1134 Ip4Config.RawData = FALSE;\r
1135 Ip4Config.ReceiveTimeout = 0;\r
1136 Ip4Config.TransmitTimeout = 0;\r
1137 Ip4Config.UseDefaultAddress = TRUE;\r
1138 Ip4Config.TimeToLive = 128;\r
1139 Ip4Config.TypeOfService = 0;\r
1140\r
1141 Status = ((EFI_IP4_PROTOCOL*)(Private->IpProtocol))->Configure (Private->IpProtocol, &Ip4Config);\r
1142\r
1143 if (EFI_ERROR (Status)) {\r
1144 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_PING_CONFIG), gShellNetwork1HiiHandle, Status);\r
1145 goto ON_ERROR;\r
1146 }\r
1147\r
1148 Private->ProtocolPointers.Transmit = (PING_IPX_TRANSMIT )((EFI_IP4_PROTOCOL*)Private->IpProtocol)->Transmit;\r
1149 Private->ProtocolPointers.Receive = (PING_IPX_RECEIVE )((EFI_IP4_PROTOCOL*)Private->IpProtocol)->Receive;\r
1150 Private->ProtocolPointers.Cancel = (PING_IPX_CANCEL )((EFI_IP4_PROTOCOL*)Private->IpProtocol)->Cancel;\r
1151 Private->ProtocolPointers.Poll = (PING_IPX_POLL )((EFI_IP4_PROTOCOL*)Private->IpProtocol)->Poll; \r
1152 }\r
1153\r
1154 if (HandleBuffer != NULL) {\r
1155 FreePool (HandleBuffer);\r
1156 }\r
1157\r
1158 return EFI_SUCCESS;\r
1159\r
1160ON_ERROR:\r
1161 if (HandleBuffer != NULL) {\r
1162 FreePool (HandleBuffer);\r
1163 }\r
1164\r
1165 if (IpXInterfaceInfo != NULL) {\r
1166 FreePool (IpXInterfaceInfo);\r
1167 }\r
1168\r
1169 if ((EfiSb != NULL) && (Private->IpChildHandle != NULL)) {\r
1170 EfiSb->DestroyChild (EfiSb, Private->IpChildHandle);\r
1171 }\r
1172\r
1173 return Status;\r
1174}\r
1175\r
1176/**\r
1177 Destory the IP instance.\r
1178\r
1179 @param[in] Private The pointer of PING_PRIVATE_DATA.\r
1180\r
1181**/\r
1182VOID\r
1183EFIAPI\r
1184Ping6DestoryIp6Instance (\r
1185 IN PING_PRIVATE_DATA *Private\r
1186 )\r
1187{\r
1188 EFI_STATUS Status;\r
1189 EFI_SERVICE_BINDING_PROTOCOL *IpSb;\r
1190\r
1191 gBS->CloseProtocol (\r
1192 Private->IpChildHandle,\r
1193 Private->IpChoice == PING_IP_CHOICE_IP6?&gEfiIp6ProtocolGuid:&gEfiIp4ProtocolGuid,\r
1194 gImageHandle,\r
1195 Private->IpChildHandle\r
1196 );\r
1197\r
1198 Status = gBS->HandleProtocol (\r
1199 Private->NicHandle,\r
1200 Private->IpChoice == PING_IP_CHOICE_IP6?&gEfiIp6ServiceBindingProtocolGuid:&gEfiIp4ServiceBindingProtocolGuid,\r
1201 (VOID **) &IpSb\r
1202 );\r
1203\r
1204 if (!EFI_ERROR(Status)) {\r
1205 IpSb->DestroyChild (IpSb, Private->IpChildHandle);\r
1206 }\r
1207}\r
1208\r
1209/**\r
1210 The Ping Process.\r
1211\r
1212 @param[in] SendNumber The send request count.\r
1213 @param[in] BufferSize The send buffer size.\r
1214 @param[in] SrcAddress The source address.\r
1215 @param[in] DstAddress The destination address.\r
1216 @param[in] IpChoice The choice between IPv4 and IPv6.\r
1217\r
1218 @retval SHELL_SUCCESS The ping processed successfullly.\r
1219 @retval others The ping processed unsuccessfully.\r
1220**/\r
1221SHELL_STATUS\r
1222EFIAPI\r
1223ShellPing (\r
1224 IN UINT32 SendNumber,\r
1225 IN UINT32 BufferSize,\r
1226 IN EFI_IPv6_ADDRESS *SrcAddress,\r
1227 IN EFI_IPv6_ADDRESS *DstAddress,\r
1228 IN UINT32 IpChoice\r
1229 )\r
1230{\r
1231 EFI_STATUS Status;\r
1232 PING_PRIVATE_DATA *Private;\r
1233 PING_ICMPX_TX_INFO *TxInfo;\r
1234 LIST_ENTRY *Entry;\r
1235 LIST_ENTRY *NextEntry;\r
1236 SHELL_STATUS ShellStatus;\r
1237\r
1238 ShellStatus = SHELL_SUCCESS;\r
1239 Private = AllocateZeroPool (sizeof (PING_PRIVATE_DATA));\r
1240\r
1241 if (Private == NULL) {\r
1242 return (SHELL_OUT_OF_RESOURCES);\r
1243 }\r
1244\r
1245 Private->IpChoice = IpChoice;\r
1246 Private->Signature = PING_PRIVATE_DATA_SIGNATURE;\r
1247 Private->SendNum = SendNumber;\r
1248 Private->BufferSize = BufferSize;\r
1249 Private->RttMin = ~((UINT64 )(0x0));\r
1250 Private->Status = EFI_NOT_READY;\r
1251\r
1252 CopyMem(&Private->SrcAddress, SrcAddress, sizeof(Private->SrcAddress));\r
1253 CopyMem(&Private->DstAddress, DstAddress, sizeof(Private->DstAddress));\r
1254\r
1255 InitializeListHead (&Private->TxList);\r
1256\r
1257 //\r
1258 // Open and configure a ip instance for us.\r
1259 //\r
1260 Status = PingCreateIpInstance (Private);\r
1261\r
1262 if (EFI_ERROR (Status)) {\r
1263 ShellStatus = SHELL_ACCESS_DENIED;\r
1264 goto ON_EXIT;\r
1265 }\r
1266 //\r
1267 // Print the command line itself.\r
1268 //\r
1269 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_PING_START), gShellNetwork1HiiHandle, mDstString, Private->BufferSize);\r
1270 //\r
1271 // Create a ipv6 token to receive the first icmp6 echo reply packet.\r
1272 //\r
1273 Status = Ping6ReceiveEchoReply (Private);\r
1274\r
1275 if (EFI_ERROR (Status)) {\r
1276 ShellStatus = SHELL_ACCESS_DENIED;\r
1277 goto ON_EXIT;\r
1278 }\r
1279 //\r
1280 // Create and start timer to send icmp6 echo request packet per second.\r
1281 //\r
1282 Status = gBS->CreateEvent (\r
1283 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
1284 TPL_CALLBACK,\r
1285 Ping6OnTimerRoutine,\r
1286 Private,\r
1287 &Private->Timer\r
1288 );\r
1289\r
1290 if (EFI_ERROR (Status)) {\r
1291 ShellStatus = SHELL_ACCESS_DENIED;\r
1292 goto ON_EXIT;\r
1293 }\r
1294 //\r
1295 // Create a ipv6 token to send the first icmp6 echo request packet.\r
1296 //\r
1297 Status = PingSendEchoRequest (Private);\r
1298 //\r
1299 // EFI_NOT_READY for IPsec is enable and IKE is not established.\r
1300 //\r
1301 if (EFI_ERROR (Status) && (Status != EFI_NOT_READY)) {\r
1302 ShellStatus = SHELL_ACCESS_DENIED;\r
1303 if(Status == EFI_NOT_FOUND) {\r
1304 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_PING_NOSOURCE_INDO), gShellNetwork1HiiHandle, mDstString);\r
1305 } else if (Status == RETURN_NO_MAPPING) {\r
1306 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_PING_NOROUTE_FOUND), gShellNetwork1HiiHandle, mDstString, mSrcString);\r
1307 } else {\r
1308 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_PING_NETWORK_ERROR), gShellNetwork1HiiHandle, Status);\r
1309 }\r
1310\r
1311 goto ON_EXIT;\r
1312 }\r
1313\r
1314 Status = gBS->SetTimer (\r
1315 Private->Timer,\r
1316 TimerPeriodic,\r
1317 ONE_SECOND\r
1318 );\r
1319\r
1320 if (EFI_ERROR (Status)) {\r
1321 ShellStatus = SHELL_ACCESS_DENIED;\r
1322 goto ON_EXIT;\r
1323 }\r
1324 //\r
1325 // Control the ping6 process by two factors:\r
1326 // 1. Hot key\r
1327 // 2. Private->Status\r
1328 // 2.1. success means all icmp6 echo request packets get reply packets.\r
1329 // 2.2. timeout means the last icmp6 echo reply request timeout to get reply.\r
1330 // 2.3. noready means ping6 process is on-the-go.\r
1331 //\r
1332 while (Private->Status == EFI_NOT_READY) {\r
1333 Status = Private->ProtocolPointers.Poll (Private->IpProtocol);\r
1334 if (ShellGetExecutionBreakFlag()) {\r
1335 Private->Status = EFI_ABORTED;\r
1336 goto ON_STAT;\r
1337 }\r
1338 }\r
1339\r
1340ON_STAT:\r
1341 //\r
1342 // Display the statistics in all.\r
1343 //\r
1344 gBS->SetTimer (Private->Timer, TimerCancel, 0);\r
1345\r
1346 if (Private->TxCount != 0) {\r
1347 ShellPrintHiiEx (\r
1348 -1,\r
1349 -1,\r
1350 NULL,\r
1351 STRING_TOKEN (STR_PING_STAT),\r
1352 gShellNetwork1HiiHandle,\r
1353 Private->TxCount,\r
1354 Private->RxCount,\r
1355 (100 * (Private->TxCount - Private->RxCount)) / Private->TxCount,\r
1356 Private->RttSum\r
1357 );\r
1358 }\r
1359\r
1360 if (Private->RxCount != 0) {\r
1361 ShellPrintHiiEx (\r
1362 -1,\r
1363 -1,\r
1364 NULL,\r
1365 STRING_TOKEN (STR_PING_RTT),\r
1366 gShellNetwork1HiiHandle,\r
1367 Private->RttMin,\r
1368 Private->RttMax,\r
1369 DivU64x64Remainder (Private->RttSum, Private->RxCount, NULL)\r
1370 );\r
1371 }\r
1372\r
1373ON_EXIT:\r
1374\r
1375 if (Private != NULL) {\r
1376\r
1377 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Private->TxList) {\r
1378 TxInfo = BASE_CR (Entry, PING_ICMPX_TX_INFO, Link);\r
1379\r
1380 if (Private->IpProtocol != NULL && Private->ProtocolPointers.Cancel != NULL) {\r
1381 Status = Private->ProtocolPointers.Cancel (Private->IpProtocol, TxInfo->Token);\r
1382 }\r
1383\r
1384 RemoveEntryList (&TxInfo->Link);\r
1385 PingDestroyTxInfo (TxInfo, Private->IpChoice);\r
1386 }\r
1387\r
1388 if (Private->Timer != NULL) {\r
1389 gBS->CloseEvent (Private->Timer);\r
1390 }\r
1391\r
1392 if (Private->IpProtocol != NULL && Private->ProtocolPointers.Cancel != NULL) {\r
1393 Status = Private->ProtocolPointers.Cancel (Private->IpProtocol, &Private->RxToken);\r
1394 }\r
1395\r
1396 if (Private->RxToken.Event != NULL) {\r
1397 gBS->CloseEvent (Private->RxToken.Event);\r
1398 }\r
1399\r
1400 if (Private->IpChildHandle != NULL) {\r
1401 Ping6DestoryIp6Instance (Private);\r
1402 }\r
1403\r
1404 FreePool (Private);\r
1405 }\r
1406\r
1407 return ShellStatus;\r
1408}\r
1409\r
1410/**\r
1411 Function for 'ping' command.\r
1412\r
1413 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
1414 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
1415**/\r
1416SHELL_STATUS\r
1417EFIAPI\r
1418ShellCommandRunPing (\r
1419 IN EFI_HANDLE ImageHandle,\r
1420 IN EFI_SYSTEM_TABLE *SystemTable\r
1421 )\r
1422{\r
1423 EFI_STATUS Status;\r
1424 SHELL_STATUS ShellStatus;\r
1425 EFI_IPv6_ADDRESS DstAddress;\r
1426 EFI_IPv6_ADDRESS SrcAddress;\r
1427 UINT64 BufferSize;\r
1428 UINTN SendNumber;\r
1429 LIST_ENTRY *ParamPackage;\r
1430 CONST CHAR16 *ValueStr;\r
1431 UINTN NonOptionCount;\r
1432 UINT32 IpChoice;\r
1433\r
1434 //\r
1435 // we use IPv6 buffers to hold items... \r
1436 // make sure this is enough space!\r
1437 //\r
1438 ASSERT(sizeof(EFI_IPv4_ADDRESS ) <= sizeof(EFI_IPv6_ADDRESS ));\r
1439 ASSERT(sizeof(EFI_IP4_COMPLETION_TOKEN) <= sizeof(EFI_IP6_COMPLETION_TOKEN ));\r
1440\r
1441 IpChoice = PING_IP_CHOICE_IP4;\r
1442\r
1443 ShellStatus = SHELL_SUCCESS;\r
1444\r
1445 Status = ShellCommandLineParseEx (PingParamList, &ParamPackage, NULL, TRUE, FALSE);\r
1446 if (EFI_ERROR(Status)) {\r
1447 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellNetwork1HiiHandle);\r
1448 ShellStatus = SHELL_INVALID_PARAMETER;\r
1449 goto ON_EXIT;\r
1450 }\r
1451\r
1452 if (ShellCommandLineGetFlag (ParamPackage, L"-_ip6")) {\r
1453 IpChoice = PING_IP_CHOICE_IP6;\r
1454 }\r
1455\r
1456 //\r
1457 // Parse the paramter of count number.\r
1458 //\r
1459 ValueStr = ShellCommandLineGetValue (ParamPackage, L"-n");\r
1460 if (ValueStr != NULL) {\r
1461 SendNumber = ShellStrToUintn (ValueStr);\r
1462\r
1463 //\r
1464 // ShellStrToUintn will return 0 when input is 0 or an invalid input string.\r
1465 //\r
1466 if ((SendNumber == 0) || (SendNumber > MAX_SEND_NUMBER)) {\r
1467 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellNetwork1HiiHandle, ValueStr);\r
1468 ShellStatus = SHELL_INVALID_PARAMETER;\r
1469 goto ON_EXIT;\r
1470 }\r
1471 } else {\r
1472 SendNumber = DEFAULT_SEND_COUNT;\r
1473 }\r
1474 //\r
1475 // Parse the paramter of buffer size.\r
1476 //\r
1477 ValueStr = ShellCommandLineGetValue (ParamPackage, L"-l");\r
1478 if (ValueStr != NULL) {\r
1479 BufferSize = ShellStrToUintn (ValueStr);\r
1480\r
1481 //\r
1482 // ShellStrToUintn will return 0 when input is 0 or an invalid input string.\r
1483 //\r
1484 if ((BufferSize < 16) || (BufferSize > MAX_BUFFER_SIZE)) {\r
1485 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellNetwork1HiiHandle, ValueStr);\r
1486 ShellStatus = SHELL_INVALID_PARAMETER;\r
1487 goto ON_EXIT;\r
1488 }\r
1489 } else {\r
1490 BufferSize = DEFAULT_BUFFER_SIZE;\r
1491 }\r
1492\r
1493 ZeroMem (&SrcAddress, sizeof (EFI_IPv6_ADDRESS));\r
1494 ZeroMem (&DstAddress, sizeof (EFI_IPv6_ADDRESS));\r
1495\r
1496 //\r
1497 // Parse the paramter of source ip address.\r
1498 //\r
1499 ValueStr = ShellCommandLineGetValue (ParamPackage, L"-_s");\r
1500 if (ValueStr != NULL) {\r
1501 mSrcString = ValueStr;\r
1502 if (IpChoice == PING_IP_CHOICE_IP6) {\r
1503 Status = NetLibStrToIp6 (ValueStr, &SrcAddress);\r
1504 } else {\r
1505 Status = NetLibStrToIp4 (ValueStr, (EFI_IPv4_ADDRESS*)&SrcAddress);\r
1506 }\r
1507 if (EFI_ERROR (Status)) {\r
1508 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellNetwork1HiiHandle, ValueStr);\r
1509 ShellStatus = SHELL_INVALID_PARAMETER;\r
1510 goto ON_EXIT;\r
1511 }\r
1512 }\r
1513 //\r
1514 // Parse the paramter of destination ip address.\r
1515 //\r
1516 NonOptionCount = ShellCommandLineGetCount(ParamPackage);\r
1517 if (NonOptionCount < 2) {\r
1518 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellNetwork1HiiHandle);\r
1519 ShellStatus = SHELL_INVALID_PARAMETER;\r
1520 goto ON_EXIT;\r
1521 }\r
1522 if (NonOptionCount > 2) {\r
1523 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellNetwork1HiiHandle);\r
1524 ShellStatus = SHELL_INVALID_PARAMETER;\r
1525 goto ON_EXIT;\r
1526 }\r
1527 ValueStr = ShellCommandLineGetRawValue (ParamPackage, 1);\r
1528 if (ValueStr != NULL) {\r
1529 mDstString = ValueStr;\r
1530 if (IpChoice == PING_IP_CHOICE_IP6) {\r
1531 Status = NetLibStrToIp6 (ValueStr, &DstAddress);\r
1532 } else {\r
1533 Status = NetLibStrToIp4 (ValueStr, (EFI_IPv4_ADDRESS*)&DstAddress);\r
1534 }\r
1535 if (EFI_ERROR (Status)) {\r
1536 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellNetwork1HiiHandle, ValueStr);\r
1537 ShellStatus = SHELL_INVALID_PARAMETER;\r
1538 goto ON_EXIT;\r
1539 }\r
1540 }\r
1541 //\r
1542 // Get frequency to calculate the time from ticks.\r
1543 //\r
1544 Status = GetFrequency ();\r
1545\r
1546 if (EFI_ERROR(Status)) {\r
1547 goto ON_EXIT;\r
1548 }\r
1549 //\r
1550 // Enter into ping process.\r
1551 //\r
1552 ShellStatus = ShellPing (\r
1553 (UINT32)SendNumber,\r
1554 (UINT32)BufferSize,\r
1555 &SrcAddress,\r
1556 &DstAddress,\r
1557 IpChoice\r
1558 );\r
1559\r
1560ON_EXIT:\r
1561 ShellCommandLineFreeVarList (ParamPackage);\r
1562 return ShellStatus;\r
1563}\r