arp.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copied from Linux Monitor (LiMon) - Networking.
  3. *
  4. * Copyright 1994 - 2000 Neil Russell.
  5. * (See License)
  6. * Copyright 2000 Roland Borde
  7. * Copyright 2000 Paolo Scaffardi
  8. * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
  9. * SPDX-License-Identifier: GPL-2.0
  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. IPaddr_t NetArpWaitPacketIP;
  25. static IPaddr_t NetArpWaitReplyIP;
  26. /* MAC address of waiting packet's destination */
  27. uchar *NetArpWaitPacketMAC;
  28. int NetArpWaitTxPacketSize;
  29. ulong NetArpWaitTimerStart;
  30. int NetArpWaitTry;
  31. static uchar *NetArpTxPacket; /* THE ARP transmit packet */
  32. static uchar NetArpPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
  33. void ArpInit(void)
  34. {
  35. /* XXX problem with bss workaround */
  36. NetArpWaitPacketMAC = NULL;
  37. NetArpWaitPacketIP = 0;
  38. NetArpWaitReplyIP = 0;
  39. NetArpWaitTxPacketSize = 0;
  40. NetArpTxPacket = &NetArpPacketBuf[0] + (PKTALIGN - 1);
  41. NetArpTxPacket -= (ulong)NetArpTxPacket % PKTALIGN;
  42. }
  43. void arp_raw_request(IPaddr_t sourceIP, const uchar *targetEther,
  44. IPaddr_t targetIP)
  45. {
  46. uchar *pkt;
  47. struct arp_hdr *arp;
  48. int eth_hdr_size;
  49. debug_cond(DEBUG_DEV_PKT, "ARP broadcast %d\n", NetArpWaitTry);
  50. pkt = NetArpTxPacket;
  51. eth_hdr_size = NetSetEther(pkt, NetBcastAddr, 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, NetOurEther, ARP_HLEN); /* source ET addr */
  60. NetWriteIP(&arp->ar_spa, sourceIP); /* source IP addr */
  61. memcpy(&arp->ar_tha, targetEther, ARP_HLEN); /* target ET addr */
  62. NetWriteIP(&arp->ar_tpa, targetIP); /* target IP addr */
  63. NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE);
  64. }
  65. void ArpRequest(void)
  66. {
  67. if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
  68. (NetOurIP & NetOurSubnetMask)) {
  69. if (NetOurGatewayIP == 0) {
  70. puts("## Warning: gatewayip needed but not set\n");
  71. NetArpWaitReplyIP = NetArpWaitPacketIP;
  72. } else {
  73. NetArpWaitReplyIP = NetOurGatewayIP;
  74. }
  75. } else {
  76. NetArpWaitReplyIP = NetArpWaitPacketIP;
  77. }
  78. arp_raw_request(NetOurIP, NetEtherNullAddr, NetArpWaitReplyIP);
  79. }
  80. void ArpTimeoutCheck(void)
  81. {
  82. ulong t;
  83. if (!NetArpWaitPacketIP)
  84. return;
  85. t = get_timer(0);
  86. /* check for arp timeout */
  87. if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
  88. NetArpWaitTry++;
  89. if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
  90. puts("\nARP Retry count exceeded; starting again\n");
  91. NetArpWaitTry = 0;
  92. NetStartAgain();
  93. } else {
  94. NetArpWaitTimerStart = t;
  95. ArpRequest();
  96. }
  97. }
  98. }
  99. void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
  100. {
  101. struct arp_hdr *arp;
  102. IPaddr_t reply_ip_addr;
  103. uchar *pkt;
  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 (NetOurIP == 0)
  129. return;
  130. if (NetReadIP(&arp->ar_tpa) != NetOurIP)
  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. pkt = (uchar *)et;
  137. eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP);
  138. pkt += eth_hdr_size;
  139. arp->ar_op = htons(ARPOP_REPLY);
  140. memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
  141. NetCopyIP(&arp->ar_tpa, &arp->ar_spa);
  142. memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN);
  143. NetCopyIP(&arp->ar_spa, &NetOurIP);
  144. #ifdef CONFIG_CMD_LINK_LOCAL
  145. /*
  146. * Work-around for brain-damaged Cisco equipment with
  147. * arp-proxy enabled.
  148. *
  149. * If the requesting IP is not on our subnet, wait 5ms to
  150. * reply to ARP request so that our reply will overwrite
  151. * the arp-proxy's instead of the other way around.
  152. */
  153. if ((NetReadIP(&arp->ar_tpa) & NetOurSubnetMask) !=
  154. (NetReadIP(&arp->ar_spa) & NetOurSubnetMask))
  155. udelay(5000);
  156. #endif
  157. NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE);
  158. return;
  159. case ARPOP_REPLY: /* arp reply */
  160. /* are we waiting for a reply */
  161. if (!NetArpWaitPacketIP)
  162. break;
  163. #ifdef CONFIG_KEEP_SERVERADDR
  164. if (NetServerIP == NetArpWaitPacketIP) {
  165. char buf[20];
  166. sprintf(buf, "%pM", &arp->ar_sha);
  167. setenv("serveraddr", buf);
  168. }
  169. #endif
  170. reply_ip_addr = NetReadIP(&arp->ar_spa);
  171. /* matched waiting packet's address */
  172. if (reply_ip_addr == NetArpWaitReplyIP) {
  173. debug_cond(DEBUG_DEV_PKT,
  174. "Got ARP REPLY, set eth addr (%pM)\n",
  175. arp->ar_data);
  176. /* save address for later use */
  177. if (NetArpWaitPacketMAC != NULL)
  178. memcpy(NetArpWaitPacketMAC,
  179. &arp->ar_sha, ARP_HLEN);
  180. net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr,
  181. 0, len);
  182. /* set the mac address in the waiting packet's header
  183. and transmit it */
  184. memcpy(((struct ethernet_hdr *)NetTxPacket)->et_dest,
  185. &arp->ar_sha, ARP_HLEN);
  186. NetSendPacket(NetTxPacket, NetArpWaitTxPacketSize);
  187. /* no arp request pending now */
  188. NetArpWaitPacketIP = 0;
  189. NetArpWaitTxPacketSize = 0;
  190. NetArpWaitPacketMAC = NULL;
  191. }
  192. return;
  193. default:
  194. debug("Unexpected ARP opcode 0x%x\n",
  195. ntohs(arp->ar_op));
  196. return;
  197. }
  198. }