arp.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. */
  10. #include <common.h>
  11. #include "arp.h"
  12. #ifndef CONFIG_ARP_TIMEOUT
  13. /* Milliseconds before trying ARP again */
  14. # define ARP_TIMEOUT 5000UL
  15. #else
  16. # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
  17. #endif
  18. #ifndef CONFIG_NET_RETRY_COUNT
  19. # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
  20. #else
  21. # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
  22. #endif
  23. IPaddr_t NetArpWaitPacketIP;
  24. IPaddr_t NetArpWaitReplyIP;
  25. /* MAC address of waiting packet's destination */
  26. uchar *NetArpWaitPacketMAC;
  27. int NetArpWaitTxPacketSize;
  28. ulong NetArpWaitTimerStart;
  29. int NetArpWaitTry;
  30. uchar *NetArpTxPacket; /* THE ARP transmit packet */
  31. uchar NetArpPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
  32. void ArpInit(void)
  33. {
  34. /* XXX problem with bss workaround */
  35. NetArpWaitPacketMAC = NULL;
  36. NetArpWaitPacketIP = 0;
  37. NetArpWaitReplyIP = 0;
  38. NetArpWaitTxPacketSize = 0;
  39. NetArpTxPacket = &NetArpPacketBuf[0] + (PKTALIGN - 1);
  40. NetArpTxPacket -= (ulong)NetArpTxPacket % PKTALIGN;
  41. }
  42. void ArpRequest(void)
  43. {
  44. uchar *pkt;
  45. struct arp_hdr *arp;
  46. int eth_hdr_size;
  47. debug("ARP broadcast %d\n", NetArpWaitTry);
  48. pkt = NetArpTxPacket;
  49. eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP);
  50. pkt += eth_hdr_size;
  51. arp = (struct arp_hdr *) pkt;
  52. arp->ar_hrd = htons(ARP_ETHER);
  53. arp->ar_pro = htons(PROT_IP);
  54. arp->ar_hln = ARP_HLEN;
  55. arp->ar_pln = ARP_PLEN;
  56. arp->ar_op = htons(ARPOP_REQUEST);
  57. /* source ET addr */
  58. memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN);
  59. /* source IP addr */
  60. NetWriteIP(&arp->ar_spa, NetOurIP);
  61. /* dest ET addr = 0 */
  62. memset(&arp->ar_tha, 0, ARP_HLEN);
  63. if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
  64. (NetOurIP & NetOurSubnetMask)) {
  65. if (NetOurGatewayIP == 0) {
  66. puts("## Warning: gatewayip needed but not set\n");
  67. NetArpWaitReplyIP = NetArpWaitPacketIP;
  68. } else {
  69. NetArpWaitReplyIP = NetOurGatewayIP;
  70. }
  71. } else {
  72. NetArpWaitReplyIP = NetArpWaitPacketIP;
  73. }
  74. NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP);
  75. NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE);
  76. }
  77. void ArpTimeoutCheck(void)
  78. {
  79. ulong t;
  80. if (!NetArpWaitPacketIP)
  81. return;
  82. t = get_timer(0);
  83. /* check for arp timeout */
  84. if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
  85. NetArpWaitTry++;
  86. if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
  87. puts("\nARP Retry count exceeded; starting again\n");
  88. NetArpWaitTry = 0;
  89. NetStartAgain();
  90. } else {
  91. NetArpWaitTimerStart = t;
  92. ArpRequest();
  93. }
  94. }
  95. }
  96. void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
  97. {
  98. struct arp_hdr *arp;
  99. IPaddr_t reply_ip_addr;
  100. uchar *pkt;
  101. int eth_hdr_size;
  102. /*
  103. * We have to deal with two types of ARP packets:
  104. * - REQUEST packets will be answered by sending our
  105. * IP address - if we know it.
  106. * - REPLY packates are expected only after we asked
  107. * for the TFTP server's or the gateway's ethernet
  108. * address; so if we receive such a packet, we set
  109. * the server ethernet address
  110. */
  111. debug("Got ARP\n");
  112. arp = (struct arp_hdr *)ip;
  113. if (len < ARP_HDR_SIZE) {
  114. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  115. return;
  116. }
  117. if (ntohs(arp->ar_hrd) != ARP_ETHER)
  118. return;
  119. if (ntohs(arp->ar_pro) != PROT_IP)
  120. return;
  121. if (arp->ar_hln != ARP_HLEN)
  122. return;
  123. if (arp->ar_pln != ARP_PLEN)
  124. return;
  125. if (NetOurIP == 0)
  126. return;
  127. if (NetReadIP(&arp->ar_tpa) != NetOurIP)
  128. return;
  129. switch (ntohs(arp->ar_op)) {
  130. case ARPOP_REQUEST:
  131. /* reply with our IP address */
  132. debug("Got ARP REQUEST, return our IP\n");
  133. pkt = (uchar *)et;
  134. eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP);
  135. pkt += eth_hdr_size;
  136. arp->ar_op = htons(ARPOP_REPLY);
  137. memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
  138. NetCopyIP(&arp->ar_tpa, &arp->ar_spa);
  139. memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN);
  140. NetCopyIP(&arp->ar_spa, &NetOurIP);
  141. NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE);
  142. return;
  143. case ARPOP_REPLY: /* arp reply */
  144. /* are we waiting for a reply */
  145. if (!NetArpWaitPacketIP)
  146. break;
  147. #ifdef CONFIG_KEEP_SERVERADDR
  148. if (NetServerIP == NetArpWaitPacketIP) {
  149. char buf[20];
  150. sprintf(buf, "%pM", arp->ar_sha);
  151. setenv("serveraddr", buf);
  152. }
  153. #endif
  154. reply_ip_addr = NetReadIP(&arp->ar_spa);
  155. /* matched waiting packet's address */
  156. if (reply_ip_addr == NetArpWaitReplyIP) {
  157. debug("Got ARP REPLY, set eth addr (%pM)\n",
  158. arp->ar_data);
  159. /* save address for later use */
  160. if (NetArpWaitPacketMAC != NULL)
  161. memcpy(NetArpWaitPacketMAC,
  162. &arp->ar_sha, ARP_HLEN);
  163. net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr,
  164. 0, len);
  165. /* set the mac address in the waiting packet's header
  166. and transmit it */
  167. memcpy(((struct ethernet_hdr *)NetTxPacket)->et_dest,
  168. &arp->ar_sha, ARP_HLEN);
  169. NetSendPacket(NetTxPacket, NetArpWaitTxPacketSize);
  170. /* no arp request pending now */
  171. NetArpWaitPacketIP = 0;
  172. NetArpWaitTxPacketSize = 0;
  173. NetArpWaitPacketMAC = NULL;
  174. }
  175. return;
  176. default:
  177. debug("Unexpected ARP opcode 0x%x\n",
  178. ntohs(arp->ar_op));
  179. return;
  180. }
  181. }