ping.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copied from Linux Monitor (LiMon) - Networking.
  3. *
  4. * Copyright 1994 - 2000 Neil Russell.
  5. * (See License)
  6. * Copyright 2000 Roland Borde
  7. * Copyright 2000 Paolo Scaffardi
  8. * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
  9. */
  10. #include "ping.h"
  11. #include "arp.h"
  12. static ushort PingSeqNo;
  13. /* The ip address to ping */
  14. IPaddr_t NetPingIP;
  15. static void set_icmp_header(uchar *pkt, IPaddr_t dest)
  16. {
  17. /*
  18. * Construct an IP and ICMP header.
  19. */
  20. struct ip_hdr *ip = (struct ip_hdr *)pkt;
  21. struct icmp_hdr *icmp = (struct icmp_hdr *)(pkt + IP_HDR_SIZE);
  22. net_set_ip_header(pkt, dest, NetOurIP);
  23. ip->ip_len = htons(IP_ICMP_HDR_SIZE);
  24. ip->ip_p = IPPROTO_ICMP;
  25. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
  26. icmp->type = ICMP_ECHO_REQUEST;
  27. icmp->code = 0;
  28. icmp->checksum = 0;
  29. icmp->un.echo.id = 0;
  30. icmp->un.echo.sequence = htons(PingSeqNo++);
  31. icmp->checksum = ~NetCksum((uchar *)icmp, ICMP_HDR_SIZE >> 1);
  32. }
  33. static int ping_send(void)
  34. {
  35. static uchar mac[6];
  36. uchar *pkt;
  37. int eth_hdr_size;
  38. /* XXX always send arp request */
  39. memcpy(mac, NetEtherNullAddr, 6);
  40. debug("sending ARP for %pI4\n", &NetPingIP);
  41. NetArpWaitPacketIP = NetPingIP;
  42. NetArpWaitPacketMAC = mac;
  43. pkt = NetArpWaitTxPacket;
  44. eth_hdr_size = NetSetEther(pkt, mac, PROT_IP);
  45. pkt += eth_hdr_size;
  46. set_icmp_header(pkt, NetPingIP);
  47. /* size of the waiting packet */
  48. NetArpWaitTxPacketSize = eth_hdr_size + IP_ICMP_HDR_SIZE;
  49. /* and do the ARP request */
  50. NetArpWaitTry = 1;
  51. NetArpWaitTimerStart = get_timer(0);
  52. ArpRequest();
  53. return 1; /* waiting */
  54. }
  55. static void ping_timeout(void)
  56. {
  57. eth_halt();
  58. NetState = NETLOOP_FAIL; /* we did not get the reply */
  59. }
  60. void ping_start(void)
  61. {
  62. printf("Using %s device\n", eth_get_name());
  63. NetSetTimeout(10000UL, ping_timeout);
  64. ping_send();
  65. }
  66. void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
  67. {
  68. struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
  69. IPaddr_t src_ip;
  70. switch (icmph->type) {
  71. case ICMP_ECHO_REPLY:
  72. src_ip = NetReadIP((void *)&ip->ip_src);
  73. if (src_ip == NetPingIP)
  74. NetState = NETLOOP_SUCCESS;
  75. return;
  76. case ICMP_ECHO_REQUEST:
  77. debug("Got ICMP ECHO REQUEST, return "
  78. "%d bytes\n", ETHER_HDR_SIZE + len);
  79. memcpy(&et->et_dest[0], &et->et_src[0], 6);
  80. memcpy(&et->et_src[0], NetOurEther, 6);
  81. ip->ip_sum = 0;
  82. ip->ip_off = 0;
  83. NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
  84. NetCopyIP((void *)&ip->ip_src, &NetOurIP);
  85. ip->ip_sum = ~NetCksum((uchar *)ip,
  86. IP_HDR_SIZE >> 1);
  87. icmph->type = ICMP_ECHO_REPLY;
  88. icmph->checksum = 0;
  89. icmph->checksum = ~NetCksum((uchar *)icmph,
  90. (len - IP_HDR_SIZE) >> 1);
  91. NetSendPacket((uchar *)et, ETHER_HDR_SIZE + len);
  92. return;
  93. /* default:
  94. return;*/
  95. }
  96. }