arp.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <net.h>
  26. #include "bootp.h"
  27. #include "tftp.h"
  28. #include "arp.h"
  29. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  30. #define TIMEOUT 5 /* Seconds before trying ARP again */
  31. #ifndef CONFIG_NET_RETRY_COUNT
  32. # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
  33. #else
  34. # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
  35. #endif
  36. static void ArpHandler(uchar *pkt, unsigned dest, unsigned src, unsigned len);
  37. static void ArpTimeout(void);
  38. int ArpTry = 0;
  39. /*
  40. * Handle a ARP received packet.
  41. */
  42. static void
  43. ArpHandler(uchar *pkt, unsigned dest, unsigned src, unsigned len)
  44. {
  45. /* Check if the frame is really an ARP reply */
  46. if (memcmp (NetServerEther, NetBcastAddr, 6) != 0) {
  47. #ifdef DEBUG
  48. printf("Got good ARP - start TFTP\n");
  49. #endif
  50. TftpStart ();
  51. }
  52. }
  53. /*
  54. * Timeout on ARP request.
  55. */
  56. static void
  57. ArpTimeout(void)
  58. {
  59. if (ArpTry >= TIMEOUT_COUNT) {
  60. puts ("\nRetry count exceeded; starting again\n");
  61. NetStartAgain ();
  62. } else {
  63. NetSetTimeout (TIMEOUT * CFG_HZ, ArpTimeout);
  64. ArpRequest ();
  65. }
  66. }
  67. void
  68. ArpRequest (void)
  69. {
  70. int i;
  71. volatile uchar *pkt;
  72. ARP_t * arp;
  73. printf("ARP broadcast %d\n", ++ArpTry);
  74. pkt = NetTxPacket;
  75. NetSetEther(pkt, NetBcastAddr, PROT_ARP);
  76. pkt += ETHER_HDR_SIZE;
  77. arp = (ARP_t *)pkt;
  78. arp->ar_hrd = htons(ARP_ETHER);
  79. arp->ar_pro = htons(PROT_IP);
  80. arp->ar_hln = 6;
  81. arp->ar_pln = 4;
  82. arp->ar_op = htons(ARPOP_REQUEST);
  83. memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
  84. NetWriteIP((uchar*)&arp->ar_data[6], NetOurIP); /* source IP addr */
  85. for (i=10; i<16; ++i) {
  86. arp->ar_data[i] = 0; /* dest ET addr = 0 */
  87. }
  88. if((NetServerIP & NetOurSubnetMask) != (NetOurIP & NetOurSubnetMask)) {
  89. if (NetOurGatewayIP == 0) {
  90. puts ("## Warning: gatewayip needed but not set\n");
  91. }
  92. NetWriteIP((uchar*)&arp->ar_data[16], NetOurGatewayIP);
  93. } else {
  94. NetWriteIP((uchar*)&arp->ar_data[16], NetServerIP);
  95. }
  96. NetSendPacket(NetTxPacket, ETHER_HDR_SIZE + ARP_HDR_SIZE);
  97. NetSetTimeout(TIMEOUT * CFG_HZ, ArpTimeout);
  98. NetSetHandler(ArpHandler);
  99. }
  100. #endif /* CFG_CMD_NET */