arp.c 6.0 KB

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