arp.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copied from Linux Monitor (LiMon) - Networking.
  4. *
  5. * Copyright 1994 - 2000 Neil Russell.
  6. * (See License)
  7. * Copyright 2000 Roland Borde
  8. * Copyright 2000 Paolo Scaffardi
  9. * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
  10. */
  11. #include <common.h>
  12. #include "arp.h"
  13. #ifndef CONFIG_ARP_TIMEOUT
  14. /* Milliseconds before trying ARP again */
  15. # define ARP_TIMEOUT 5000UL
  16. #else
  17. # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
  18. #endif
  19. #ifndef CONFIG_NET_RETRY_COUNT
  20. # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
  21. #else
  22. # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
  23. #endif
  24. struct in_addr net_arp_wait_packet_ip;
  25. static struct in_addr net_arp_wait_reply_ip;
  26. /* MAC address of waiting packet's destination */
  27. uchar *arp_wait_packet_ethaddr;
  28. int arp_wait_tx_packet_size;
  29. ulong arp_wait_timer_start;
  30. int arp_wait_try;
  31. static uchar *arp_tx_packet; /* THE ARP transmit packet */
  32. static uchar arp_tx_packet_buf[PKTSIZE_ALIGN + PKTALIGN];
  33. void arp_init(void)
  34. {
  35. /* XXX problem with bss workaround */
  36. arp_wait_packet_ethaddr = NULL;
  37. net_arp_wait_packet_ip.s_addr = 0;
  38. net_arp_wait_reply_ip.s_addr = 0;
  39. arp_wait_tx_packet_size = 0;
  40. arp_tx_packet = &arp_tx_packet_buf[0] + (PKTALIGN - 1);
  41. arp_tx_packet -= (ulong)arp_tx_packet % PKTALIGN;
  42. }
  43. void arp_raw_request(struct in_addr source_ip, const uchar *target_ethaddr,
  44. struct in_addr target_ip)
  45. {
  46. uchar *pkt;
  47. struct arp_hdr *arp;
  48. int eth_hdr_size;
  49. debug_cond(DEBUG_DEV_PKT, "ARP broadcast %d\n", arp_wait_try);
  50. pkt = arp_tx_packet;
  51. eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_ARP);
  52. pkt += eth_hdr_size;
  53. arp = (struct arp_hdr *)pkt;
  54. arp->ar_hrd = htons(ARP_ETHER);
  55. arp->ar_pro = htons(PROT_IP);
  56. arp->ar_hln = ARP_HLEN;
  57. arp->ar_pln = ARP_PLEN;
  58. arp->ar_op = htons(ARPOP_REQUEST);
  59. memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN); /* source ET addr */
  60. net_write_ip(&arp->ar_spa, source_ip); /* source IP addr */
  61. memcpy(&arp->ar_tha, target_ethaddr, ARP_HLEN); /* target ET addr */
  62. net_write_ip(&arp->ar_tpa, target_ip); /* target IP addr */
  63. net_send_packet(arp_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
  64. }
  65. void arp_request(void)
  66. {
  67. if ((net_arp_wait_packet_ip.s_addr & net_netmask.s_addr) !=
  68. (net_ip.s_addr & net_netmask.s_addr)) {
  69. if (net_gateway.s_addr == 0) {
  70. puts("## Warning: gatewayip needed but not set\n");
  71. net_arp_wait_reply_ip = net_arp_wait_packet_ip;
  72. } else {
  73. net_arp_wait_reply_ip = net_gateway;
  74. }
  75. } else {
  76. net_arp_wait_reply_ip = net_arp_wait_packet_ip;
  77. }
  78. arp_raw_request(net_ip, net_null_ethaddr, net_arp_wait_reply_ip);
  79. }
  80. int arp_timeout_check(void)
  81. {
  82. ulong t;
  83. if (!net_arp_wait_packet_ip.s_addr)
  84. return 0;
  85. t = get_timer(0);
  86. /* check for arp timeout */
  87. if ((t - arp_wait_timer_start) > ARP_TIMEOUT) {
  88. arp_wait_try++;
  89. if (arp_wait_try >= ARP_TIMEOUT_COUNT) {
  90. puts("\nARP Retry count exceeded; starting again\n");
  91. arp_wait_try = 0;
  92. net_set_state(NETLOOP_FAIL);
  93. } else {
  94. arp_wait_timer_start = t;
  95. arp_request();
  96. }
  97. }
  98. return 1;
  99. }
  100. void arp_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
  101. {
  102. struct arp_hdr *arp;
  103. struct in_addr reply_ip_addr;
  104. int eth_hdr_size;
  105. /*
  106. * We have to deal with two types of ARP packets:
  107. * - REQUEST packets will be answered by sending our
  108. * IP address - if we know it.
  109. * - REPLY packates are expected only after we asked
  110. * for the TFTP server's or the gateway's ethernet
  111. * address; so if we receive such a packet, we set
  112. * the server ethernet address
  113. */
  114. debug_cond(DEBUG_NET_PKT, "Got ARP\n");
  115. arp = (struct arp_hdr *)ip;
  116. if (len < ARP_HDR_SIZE) {
  117. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  118. return;
  119. }
  120. if (ntohs(arp->ar_hrd) != ARP_ETHER)
  121. return;
  122. if (ntohs(arp->ar_pro) != PROT_IP)
  123. return;
  124. if (arp->ar_hln != ARP_HLEN)
  125. return;
  126. if (arp->ar_pln != ARP_PLEN)
  127. return;
  128. if (net_ip.s_addr == 0)
  129. return;
  130. if (net_read_ip(&arp->ar_tpa).s_addr != net_ip.s_addr)
  131. return;
  132. switch (ntohs(arp->ar_op)) {
  133. case ARPOP_REQUEST:
  134. /* reply with our IP address */
  135. debug_cond(DEBUG_DEV_PKT, "Got ARP REQUEST, return our IP\n");
  136. eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP);
  137. arp->ar_op = htons(ARPOP_REPLY);
  138. memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
  139. net_copy_ip(&arp->ar_tpa, &arp->ar_spa);
  140. memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN);
  141. net_copy_ip(&arp->ar_spa, &net_ip);
  142. #ifdef CONFIG_CMD_LINK_LOCAL
  143. /*
  144. * Work-around for brain-damaged Cisco equipment with
  145. * arp-proxy enabled.
  146. *
  147. * If the requesting IP is not on our subnet, wait 5ms to
  148. * reply to ARP request so that our reply will overwrite
  149. * the arp-proxy's instead of the other way around.
  150. */
  151. if ((net_read_ip(&arp->ar_tpa).s_addr & net_netmask.s_addr) !=
  152. (net_read_ip(&arp->ar_spa).s_addr & net_netmask.s_addr))
  153. udelay(5000);
  154. #endif
  155. net_send_packet((uchar *)et, eth_hdr_size + ARP_HDR_SIZE);
  156. return;
  157. case ARPOP_REPLY: /* arp reply */
  158. /* are we waiting for a reply */
  159. if (!net_arp_wait_packet_ip.s_addr)
  160. break;
  161. #ifdef CONFIG_KEEP_SERVERADDR
  162. if (net_server_ip.s_addr == net_arp_wait_packet_ip.s_addr) {
  163. char buf[20];
  164. sprintf(buf, "%pM", &arp->ar_sha);
  165. env_set("serveraddr", buf);
  166. }
  167. #endif
  168. reply_ip_addr = net_read_ip(&arp->ar_spa);
  169. /* matched waiting packet's address */
  170. if (reply_ip_addr.s_addr == net_arp_wait_reply_ip.s_addr) {
  171. debug_cond(DEBUG_DEV_PKT,
  172. "Got ARP REPLY, set eth addr (%pM)\n",
  173. arp->ar_data);
  174. /* save address for later use */
  175. if (arp_wait_packet_ethaddr != NULL)
  176. memcpy(arp_wait_packet_ethaddr,
  177. &arp->ar_sha, ARP_HLEN);
  178. net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr,
  179. 0, len);
  180. /* set the mac address in the waiting packet's header
  181. and transmit it */
  182. memcpy(((struct ethernet_hdr *)net_tx_packet)->et_dest,
  183. &arp->ar_sha, ARP_HLEN);
  184. net_send_packet(net_tx_packet, arp_wait_tx_packet_size);
  185. /* no arp request pending now */
  186. net_arp_wait_packet_ip.s_addr = 0;
  187. arp_wait_tx_packet_size = 0;
  188. arp_wait_packet_ethaddr = NULL;
  189. }
  190. return;
  191. default:
  192. debug("Unexpected ARP opcode 0x%x\n",
  193. ntohs(arp->ar_op));
  194. return;
  195. }
  196. }