arp.c 5.2 KB

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