ping.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /* XXX always send arp request */
  38. memcpy(mac, NetEtherNullAddr, 6);
  39. debug("sending ARP for %pI4\n", &NetPingIP);
  40. NetArpWaitPacketIP = NetPingIP;
  41. NetArpWaitPacketMAC = mac;
  42. pkt = NetArpWaitTxPacket;
  43. pkt += NetSetEther(pkt, mac, PROT_IP);
  44. set_icmp_header(pkt, NetPingIP);
  45. /* size of the waiting packet */
  46. NetArpWaitTxPacketSize =
  47. (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + 8;
  48. /* and do the ARP request */
  49. NetArpWaitTry = 1;
  50. NetArpWaitTimerStart = get_timer(0);
  51. ArpRequest();
  52. return 1; /* waiting */
  53. }
  54. static void ping_timeout(void)
  55. {
  56. eth_halt();
  57. NetState = NETLOOP_FAIL; /* we did not get the reply */
  58. }
  59. static void ping_handler(uchar *pkt, unsigned dest, IPaddr_t sip,
  60. unsigned src, unsigned len)
  61. {
  62. if (sip != NetPingIP)
  63. return;
  64. NetState = NETLOOP_SUCCESS;
  65. }
  66. void ping_start(void)
  67. {
  68. printf("Using %s device\n", eth_get_name());
  69. NetSetTimeout(10000UL, ping_timeout);
  70. NetSetHandler(ping_handler);
  71. ping_send();
  72. }
  73. void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
  74. {
  75. struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
  76. IPaddr_t src_ip;
  77. switch (icmph->type) {
  78. case ICMP_ECHO_REPLY:
  79. /*
  80. * IP header OK. Pass the packet to the
  81. * current handler.
  82. */
  83. /* XXX point to ip packet */
  84. src_ip = NetReadIP((void *)&ip->ip_src);
  85. NetGetHandler()((uchar *)ip, 0, src_ip, 0, 0);
  86. return;
  87. case ICMP_ECHO_REQUEST:
  88. debug("Got ICMP ECHO REQUEST, return "
  89. "%d bytes\n", ETHER_HDR_SIZE + len);
  90. memcpy(&et->et_dest[0], &et->et_src[0], 6);
  91. memcpy(&et->et_src[0], NetOurEther, 6);
  92. ip->ip_sum = 0;
  93. ip->ip_off = 0;
  94. NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
  95. NetCopyIP((void *)&ip->ip_src, &NetOurIP);
  96. ip->ip_sum = ~NetCksum((uchar *)ip,
  97. IP_HDR_SIZE >> 1);
  98. icmph->type = ICMP_ECHO_REPLY;
  99. icmph->checksum = 0;
  100. icmph->checksum = ~NetCksum((uchar *)icmph,
  101. (len - IP_HDR_SIZE) >> 1);
  102. (void) eth_send((uchar *)et,
  103. ETHER_HDR_SIZE + len);
  104. return;
  105. /* default:
  106. return;*/
  107. }
  108. }