arp.c 6.3 KB

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