arp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 1983, 1993 by The Regents of the University of California
  3. * Copyright (C) 2001-2007 by egnite Software GmbH
  4. * Copyright (C) 2010 by egnite GmbH
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. *
  36. * -
  37. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  38. *
  39. * Permission to use, copy, modify, and distribute this software for any
  40. * purpose with or without fee is hereby granted, provided that the above
  41. * copyright notice and this permission notice appear in all copies, and that
  42. * the name of Digital Equipment Corporation not be used in advertising or
  43. * publicity pertaining to distribution of the document or software without
  44. * specific, written prior permission.
  45. *
  46. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  47. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  48. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  49. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  50. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  51. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  52. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  53. * SOFTWARE.
  54. */
  55. /*
  56. * $Id: arp.c 4115 2012-04-12 21:06:13Z olereinhardt $
  57. */
  58. #include <string.h>
  59. #include "dialog.h"
  60. #include "bootmon.h"
  61. #include "ip.h"
  62. #include "arp.h"
  63. /*!
  64. * \addtogroup xgStack
  65. */
  66. /*@{*/
  67. static unsigned long arptab_ip; /*!< \brief ARP entry IP address. */
  68. static unsigned char arptab_ha[6]; /*!< \brief APR entry hardware address. */
  69. ARPENTRY ae;
  70. ETHERHDR arpheader;
  71. ETHERARP arpframe;
  72. /*!
  73. * \brief Request the MAC address of a specified IP address.
  74. *
  75. * \param dip IP address in network byte order.
  76. * \param dmac Points to the buffer, that will receive the MAC address.
  77. *
  78. * \return 0 on success, -1 otherwise.
  79. */
  80. int ArpRequest(unsigned long dip, unsigned char *dmac)
  81. {
  82. ETHERARP *ea;
  83. unsigned char retry;
  84. int rlen;
  85. memset_(dmac, 0xFF, 6);
  86. if (dip == INADDR_BROADCAST) {
  87. return 0;
  88. }
  89. if (dip == arptab_ip) {
  90. memcpy_(dmac, arptab_ha, 6);
  91. return 0;
  92. }
  93. /*
  94. * Set ARP header.
  95. */
  96. arpframe.arp_hrd = ARPHRD_ETHER;
  97. arpframe.arp_pro = ETHERTYPE_IP;
  98. arpframe.arp_hln = 6;
  99. arpframe.arp_pln = 4;
  100. arpframe.arp_op = ARPOP_REQUEST;
  101. memcpy_(arpframe.arp_sha, confnet.cdn_mac, 6);
  102. memset_(arpframe.arp_tha, 0xFF, 6);
  103. arpframe.arp_spa = confnet.cdn_ip_addr;
  104. arpframe.arp_tpa = dip;
  105. ea = &rframe.eth.arp;
  106. for (rlen = retry = 0; retry < 3;) {
  107. /*
  108. * Send a message, if nothing has been received yet.
  109. */
  110. if (rlen == 0) {
  111. /* Transmit failure, must be a NIC problem. Give up. */
  112. if (EtherOutput(dmac, ETHERTYPE_ARP, sizeof(ETHERARP)) < 0)
  113. break;
  114. }
  115. if ((rlen = EtherInput(ETHERTYPE_ARP, 1000)) <= 0) {
  116. retry++;
  117. continue;
  118. }
  119. /*
  120. * Check if the response contains the expected values.
  121. */
  122. if (ea->arp_tpa == confnet.cdn_ip_addr && ea->arp_op == ARPOP_REPLY && ea->arp_spa == dip) {
  123. arptab_ip = dip;
  124. memcpy_(arptab_ha, ea->arp_sha, 6);
  125. memcpy_(dmac, ea->arp_sha, 6);
  126. return 0;
  127. }
  128. }
  129. return -1;
  130. }
  131. /*!
  132. * \brief Process incoming ARP packets.
  133. *
  134. * The incoming IP and hardware address pair is stored. This routine
  135. * does not respond to ARP requests.
  136. */
  137. void ArpRespond(void)
  138. {
  139. ETHERARP *ea = &rframe.eth.arp;
  140. if (ea->arp_op == ARPOP_REPLY) {
  141. if (ea->arp_tpa == confnet.cdn_ip_addr) {
  142. ae.ae_ip = ea->arp_spa;
  143. memcpy_(ae.ae_ha, ea->arp_sha, 6);
  144. }
  145. }
  146. else if (ea->arp_op == ARPOP_REQUEST) {
  147. if (ea->arp_tpa == confnet.cdn_ip_addr) {
  148. /* Use the request as a basis for our reply. */
  149. memcpy_((unsigned char *)&arpframe, (unsigned char *)ea, sizeof(ETHERARP));
  150. /* The request's source becomes the reply's destination. */
  151. memcpy_(arpframe.arp_tha, ea->arp_sha, 6);
  152. arpframe.arp_tpa = ea->arp_spa;
  153. /* Use our configuration for the reply's source. */
  154. memcpy_(arpframe.arp_sha, confnet.cdn_mac, 6);
  155. arpframe.arp_spa = confnet.cdn_ip_addr;
  156. /* Make this a reply and send it . */
  157. arpframe.arp_op = ARPOP_REPLY;
  158. EtherOutput(arpframe.arp_tha, ETHERTYPE_ARP, sizeof(ETHERARP));
  159. }
  160. }
  161. }
  162. /*@}*/