rarp.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000-2002
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <log.h>
  9. #include <net.h>
  10. #include <net/tftp.h>
  11. #include "nfs.h"
  12. #include "bootp.h"
  13. #include "rarp.h"
  14. #define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
  15. #ifndef CONFIG_NET_RETRY_COUNT
  16. #define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
  17. #else
  18. #define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
  19. #endif
  20. int rarp_try;
  21. /*
  22. * Handle a RARP received packet.
  23. */
  24. void rarp_receive(struct ip_udp_hdr *ip, unsigned len)
  25. {
  26. struct arp_hdr *arp;
  27. debug_cond(DEBUG_NET_PKT, "Got RARP\n");
  28. arp = (struct arp_hdr *)ip;
  29. if (len < ARP_HDR_SIZE) {
  30. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  31. return;
  32. }
  33. if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
  34. (ntohs(arp->ar_hrd) != ARP_ETHER) ||
  35. (ntohs(arp->ar_pro) != PROT_IP) ||
  36. (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
  37. puts("invalid RARP header\n");
  38. } else {
  39. net_copy_ip(&net_ip, &arp->ar_data[16]);
  40. if (net_server_ip.s_addr == 0)
  41. net_copy_ip(&net_server_ip, &arp->ar_data[6]);
  42. memcpy(net_server_ethaddr, &arp->ar_data[0], 6);
  43. debug_cond(DEBUG_DEV_PKT, "Got good RARP\n");
  44. net_auto_load();
  45. }
  46. }
  47. /*
  48. * Timeout on BOOTP request.
  49. */
  50. static void rarp_timeout_handler(void)
  51. {
  52. if (rarp_try >= TIMEOUT_COUNT) {
  53. puts("\nRetry count exceeded; starting again\n");
  54. net_start_again();
  55. } else {
  56. net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
  57. rarp_request();
  58. }
  59. }
  60. void rarp_request(void)
  61. {
  62. uchar *pkt;
  63. struct arp_hdr *rarp;
  64. int eth_hdr_size;
  65. printf("RARP broadcast %d\n", ++rarp_try);
  66. pkt = net_tx_packet;
  67. eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_RARP);
  68. pkt += eth_hdr_size;
  69. rarp = (struct arp_hdr *)pkt;
  70. rarp->ar_hrd = htons(ARP_ETHER);
  71. rarp->ar_pro = htons(PROT_IP);
  72. rarp->ar_hln = 6;
  73. rarp->ar_pln = 4;
  74. rarp->ar_op = htons(RARPOP_REQUEST);
  75. memcpy(&rarp->ar_data[0], net_ethaddr, 6); /* source ET addr */
  76. memcpy(&rarp->ar_data[6], &net_ip, 4); /* source IP addr */
  77. /* dest ET addr = source ET addr ??*/
  78. memcpy(&rarp->ar_data[10], net_ethaddr, 6);
  79. /* dest IP addr set to broadcast */
  80. memset(&rarp->ar_data[16], 0xff, 4);
  81. net_send_packet(net_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
  82. net_set_timeout_handler(TIMEOUT, rarp_timeout_handler);
  83. }