rarp.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * (C) Copyright 2000-2002
  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 "nfs.h"
  27. #include "bootp.h"
  28. #include "rarp.h"
  29. #include "tftp.h"
  30. #define TIMEOUT 5000UL /* Milliseconds before trying BOOTP 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. int RarpTry;
  37. /*
  38. * Handle a RARP received packet.
  39. */
  40. void rarp_receive(struct ip_udp_hdr *ip, unsigned len)
  41. {
  42. struct arp_hdr *arp;
  43. debug_cond(DEBUG_NET_PKT, "Got RARP\n");
  44. arp = (struct arp_hdr *)ip;
  45. if (len < ARP_HDR_SIZE) {
  46. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  47. return;
  48. }
  49. if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
  50. (ntohs(arp->ar_hrd) != ARP_ETHER) ||
  51. (ntohs(arp->ar_pro) != PROT_IP) ||
  52. (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
  53. puts("invalid RARP header\n");
  54. } else {
  55. NetCopyIP(&NetOurIP, &arp->ar_data[16]);
  56. if (NetServerIP == 0)
  57. NetCopyIP(&NetServerIP, &arp->ar_data[6]);
  58. memcpy(NetServerEther, &arp->ar_data[0], 6);
  59. debug_cond(DEBUG_DEV_PKT, "Got good RARP\n");
  60. net_auto_load();
  61. }
  62. }
  63. /*
  64. * Timeout on BOOTP request.
  65. */
  66. static void RarpTimeout(void)
  67. {
  68. if (RarpTry >= TIMEOUT_COUNT) {
  69. puts("\nRetry count exceeded; starting again\n");
  70. NetStartAgain();
  71. } else {
  72. NetSetTimeout(TIMEOUT, RarpTimeout);
  73. RarpRequest();
  74. }
  75. }
  76. void RarpRequest(void)
  77. {
  78. uchar *pkt;
  79. struct arp_hdr *rarp;
  80. int eth_hdr_size;
  81. printf("RARP broadcast %d\n", ++RarpTry);
  82. pkt = NetTxPacket;
  83. eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_RARP);
  84. pkt += eth_hdr_size;
  85. rarp = (struct arp_hdr *)pkt;
  86. rarp->ar_hrd = htons(ARP_ETHER);
  87. rarp->ar_pro = htons(PROT_IP);
  88. rarp->ar_hln = 6;
  89. rarp->ar_pln = 4;
  90. rarp->ar_op = htons(RARPOP_REQUEST);
  91. memcpy(&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */
  92. memcpy(&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */
  93. /* dest ET addr = source ET addr ??*/
  94. memcpy(&rarp->ar_data[10], NetOurEther, 6);
  95. /* dest IP addr set to broadcast */
  96. memset(&rarp->ar_data[16], 0xff, 4);
  97. NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE);
  98. NetSetTimeout(TIMEOUT, RarpTimeout);
  99. }