rarp.c 2.3 KB

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