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