ping.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 int ping_send(void)
  16. {
  17. static uchar mac[6];
  18. struct ip_udp_hdr *ip;
  19. ushort *s;
  20. uchar *pkt;
  21. /* XXX always send arp request */
  22. memcpy(mac, NetEtherNullAddr, 6);
  23. debug("sending ARP for %pI4\n", &NetPingIP);
  24. NetArpWaitPacketIP = NetPingIP;
  25. NetArpWaitPacketMAC = mac;
  26. pkt = NetArpWaitTxPacket;
  27. pkt += NetSetEther(pkt, mac, PROT_IP);
  28. ip = (struct ip_udp_hdr *)pkt;
  29. /*
  30. * Construct an IP and ICMP header.
  31. * (need to set no fragment bit - XXX)
  32. */
  33. /* IP_HDR_SIZE / 4 (not including UDP) */
  34. ip->ip_hl_v = 0x45;
  35. ip->ip_tos = 0;
  36. ip->ip_len = htons(IP_HDR_SIZE + 8);
  37. ip->ip_id = htons(NetIPID++);
  38. ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
  39. ip->ip_ttl = 255;
  40. ip->ip_p = 0x01; /* ICMP */
  41. ip->ip_sum = 0;
  42. /* already in network byte order */
  43. NetCopyIP((void *)&ip->ip_src, &NetOurIP);
  44. /* - "" - */
  45. NetCopyIP((void *)&ip->ip_dst, &NetPingIP);
  46. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2);
  47. s = &ip->udp_src; /* XXX ICMP starts here */
  48. s[0] = htons(0x0800); /* echo-request, code */
  49. s[1] = 0; /* checksum */
  50. s[2] = 0; /* identifier */
  51. s[3] = htons(PingSeqNo++); /* sequence number */
  52. s[1] = ~NetCksum((uchar *)s, 8/2);
  53. /* size of the waiting packet */
  54. NetArpWaitTxPacketSize =
  55. (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + 8;
  56. /* and do the ARP request */
  57. NetArpWaitTry = 1;
  58. NetArpWaitTimerStart = get_timer(0);
  59. ArpRequest();
  60. return 1; /* waiting */
  61. }
  62. static void ping_timeout(void)
  63. {
  64. eth_halt();
  65. NetState = NETLOOP_FAIL; /* we did not get the reply */
  66. }
  67. static void ping_handler(uchar *pkt, unsigned dest, IPaddr_t sip,
  68. unsigned src, unsigned len)
  69. {
  70. if (sip != NetPingIP)
  71. return;
  72. NetState = NETLOOP_SUCCESS;
  73. }
  74. void ping_start(void)
  75. {
  76. printf("Using %s device\n", eth_get_name());
  77. NetSetTimeout(10000UL, ping_timeout);
  78. NetSetHandler(ping_handler);
  79. ping_send();
  80. }
  81. void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
  82. {
  83. ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
  84. IPaddr_t src_ip;
  85. switch (icmph->type) {
  86. case ICMP_ECHO_REPLY:
  87. /*
  88. * IP header OK. Pass the packet to the
  89. * current handler.
  90. */
  91. /* XXX point to ip packet */
  92. src_ip = NetReadIP((void *)&ip->ip_src);
  93. NetGetHandler()((uchar *)ip, 0, src_ip, 0, 0);
  94. return;
  95. case ICMP_ECHO_REQUEST:
  96. debug("Got ICMP ECHO REQUEST, return "
  97. "%d bytes\n", ETHER_HDR_SIZE + len);
  98. memcpy(&et->et_dest[0], &et->et_src[0], 6);
  99. memcpy(&et->et_src[0], NetOurEther, 6);
  100. ip->ip_sum = 0;
  101. ip->ip_off = 0;
  102. NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
  103. NetCopyIP((void *)&ip->ip_src, &NetOurIP);
  104. ip->ip_sum = ~NetCksum((uchar *)ip,
  105. IP_HDR_SIZE >> 1);
  106. icmph->type = ICMP_ECHO_REPLY;
  107. icmph->checksum = 0;
  108. icmph->checksum = ~NetCksum((uchar *)icmph,
  109. (len - IP_HDR_SIZE) >> 1);
  110. (void) eth_send((uchar *)et,
  111. ETHER_HDR_SIZE + len);
  112. return;
  113. /* default:
  114. return;*/
  115. }
  116. }