ping.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. uchar *pkt;
  36. int eth_hdr_size;
  37. /* XXX always send arp request */
  38. debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &NetPingIP);
  39. NetArpWaitPacketIP = NetPingIP;
  40. eth_hdr_size = NetSetEther(NetTxPacket, NetEtherNullAddr, PROT_IP);
  41. pkt = (uchar *)NetTxPacket + eth_hdr_size;
  42. set_icmp_header(pkt, NetPingIP);
  43. /* size of the waiting packet */
  44. NetArpWaitTxPacketSize = eth_hdr_size + IP_ICMP_HDR_SIZE;
  45. /* and do the ARP request */
  46. NetArpWaitTry = 1;
  47. NetArpWaitTimerStart = get_timer(0);
  48. ArpRequest();
  49. return 1; /* waiting */
  50. }
  51. static void ping_timeout(void)
  52. {
  53. eth_halt();
  54. net_set_state(NETLOOP_FAIL); /* we did not get the reply */
  55. }
  56. void ping_start(void)
  57. {
  58. printf("Using %s device\n", eth_get_name());
  59. NetSetTimeout(10000UL, ping_timeout);
  60. ping_send();
  61. }
  62. void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
  63. {
  64. struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
  65. IPaddr_t src_ip;
  66. int eth_hdr_size;
  67. switch (icmph->type) {
  68. case ICMP_ECHO_REPLY:
  69. src_ip = NetReadIP((void *)&ip->ip_src);
  70. if (src_ip == NetPingIP)
  71. net_set_state(NETLOOP_SUCCESS);
  72. return;
  73. case ICMP_ECHO_REQUEST:
  74. eth_hdr_size = net_update_ether(et, et->et_src, PROT_IP);
  75. debug_cond(DEBUG_DEV_PKT, "Got ICMP ECHO REQUEST, return "
  76. "%d bytes\n", eth_hdr_size + len);
  77. ip->ip_sum = 0;
  78. ip->ip_off = 0;
  79. NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
  80. NetCopyIP((void *)&ip->ip_src, &NetOurIP);
  81. ip->ip_sum = ~NetCksum((uchar *)ip,
  82. IP_HDR_SIZE >> 1);
  83. icmph->type = ICMP_ECHO_REPLY;
  84. icmph->checksum = 0;
  85. icmph->checksum = ~NetCksum((uchar *)icmph,
  86. (len - IP_HDR_SIZE) >> 1);
  87. NetSendPacket((uchar *)et, eth_hdr_size + len);
  88. return;
  89. /* default:
  90. return;*/
  91. }
  92. }