arp.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. 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 (!arp_is_waiting())
  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. uchar *tx_packet;
  106. /*
  107. * We have to deal with two types of ARP packets:
  108. * - REQUEST packets will be answered by sending our
  109. * IP address - if we know it.
  110. * - REPLY packates are expected only after we asked
  111. * for the TFTP server's or the gateway's ethernet
  112. * address; so if we receive such a packet, we set
  113. * the server ethernet address
  114. */
  115. debug_cond(DEBUG_NET_PKT, "Got ARP\n");
  116. arp = (struct arp_hdr *)ip;
  117. if (len < ARP_HDR_SIZE) {
  118. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  119. return;
  120. }
  121. if (ntohs(arp->ar_hrd) != ARP_ETHER)
  122. return;
  123. if (ntohs(arp->ar_pro) != PROT_IP)
  124. return;
  125. if (arp->ar_hln != ARP_HLEN)
  126. return;
  127. if (arp->ar_pln != ARP_PLEN)
  128. return;
  129. if (net_ip.s_addr == 0)
  130. return;
  131. if (net_read_ip(&arp->ar_tpa).s_addr != net_ip.s_addr)
  132. return;
  133. switch (ntohs(arp->ar_op)) {
  134. case ARPOP_REQUEST:
  135. /* reply with our IP address */
  136. debug_cond(DEBUG_DEV_PKT, "Got ARP REQUEST, return our IP\n");
  137. eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP);
  138. arp->ar_op = htons(ARPOP_REPLY);
  139. memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
  140. net_copy_ip(&arp->ar_tpa, &arp->ar_spa);
  141. memcpy(&arp->ar_sha, net_ethaddr, ARP_HLEN);
  142. net_copy_ip(&arp->ar_spa, &net_ip);
  143. #ifdef CONFIG_CMD_LINK_LOCAL
  144. /*
  145. * Work-around for brain-damaged Cisco equipment with
  146. * arp-proxy enabled.
  147. *
  148. * If the requesting IP is not on our subnet, wait 5ms to
  149. * reply to ARP request so that our reply will overwrite
  150. * the arp-proxy's instead of the other way around.
  151. */
  152. if ((net_read_ip(&arp->ar_tpa).s_addr & net_netmask.s_addr) !=
  153. (net_read_ip(&arp->ar_spa).s_addr & net_netmask.s_addr))
  154. udelay(5000);
  155. #endif
  156. tx_packet = net_get_async_tx_pkt_buf();
  157. memcpy(tx_packet, et, eth_hdr_size + ARP_HDR_SIZE);
  158. net_send_packet(tx_packet, eth_hdr_size + ARP_HDR_SIZE);
  159. return;
  160. case ARPOP_REPLY: /* arp reply */
  161. /* are we waiting for a reply? */
  162. if (!arp_is_waiting())
  163. break;
  164. #ifdef CONFIG_KEEP_SERVERADDR
  165. if (net_server_ip.s_addr == net_arp_wait_packet_ip.s_addr) {
  166. char buf[20];
  167. sprintf(buf, "%pM", &arp->ar_sha);
  168. env_set("serveraddr", buf);
  169. }
  170. #endif
  171. reply_ip_addr = net_read_ip(&arp->ar_spa);
  172. /* matched waiting packet's address */
  173. if (reply_ip_addr.s_addr == net_arp_wait_reply_ip.s_addr) {
  174. debug_cond(DEBUG_DEV_PKT,
  175. "Got ARP REPLY, set eth addr (%pM)\n",
  176. arp->ar_data);
  177. /* save address for later use */
  178. if (arp_wait_packet_ethaddr != NULL)
  179. memcpy(arp_wait_packet_ethaddr,
  180. &arp->ar_sha, ARP_HLEN);
  181. net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr,
  182. 0, len);
  183. /* set the mac address in the waiting packet's header
  184. and transmit it */
  185. memcpy(((struct ethernet_hdr *)net_tx_packet)->et_dest,
  186. &arp->ar_sha, ARP_HLEN);
  187. net_send_packet(net_tx_packet, arp_wait_tx_packet_size);
  188. /* no arp request pending now */
  189. net_arp_wait_packet_ip.s_addr = 0;
  190. arp_wait_tx_packet_size = 0;
  191. arp_wait_packet_ethaddr = NULL;
  192. }
  193. return;
  194. default:
  195. debug("Unexpected ARP opcode 0x%x\n",
  196. ntohs(arp->ar_op));
  197. return;
  198. }
  199. }
  200. bool arp_is_waiting(void)
  201. {
  202. return !!net_arp_wait_packet_ip.s_addr;
  203. }