arp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C) 1983, 1993 by The Regents of the University of California
  3. * Copyright (C) 2001-2004 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 "eboot.h"
  60. #include "config.h"
  61. #include "ip.h"
  62. #include "arp.h"
  63. /*!
  64. * \addtogroup xgStack
  65. */
  66. /*@{*/
  67. ARPENTRY ae;
  68. ARPFRAME arpframe;
  69. /*!
  70. * \brief Request the MAC address of a specified IP address.
  71. *
  72. * \param dip IP address in network byte order.
  73. * \param dmac Points to the buffer, that will receive the MAC address.
  74. *
  75. * \return 0 on success, -1 otherwise.
  76. */
  77. int ArpRequest(u_long dip, u_char * dmac)
  78. {
  79. ETHERARP *ea = &arpframe.eth_arp;
  80. u_char retry;
  81. int rlen;
  82. u_char i;
  83. if (dip == INADDR_BROADCAST) {
  84. for (i = 0; i < 6; i++)
  85. dmac[i] = 0xFF;
  86. return 0;
  87. }
  88. if (ae.ae_ip != dip) {
  89. /*
  90. * Set ARP header.
  91. */
  92. ea->arp_hrd = htons(ARPHRD_ETHER);
  93. ea->arp_pro = ETHERTYPE_IP;
  94. ea->arp_hln = 6;
  95. ea->arp_pln = 4;
  96. ea->arp_op = htons(ARPOP_REQUEST);
  97. for (i = 0; i < 6; i++)
  98. ea->arp_sha[i] = confnet.cdn_mac[i];
  99. ea->arp_spa = confnet.cdn_ip_addr;
  100. for (i = 0; i < 6; i++)
  101. ea->arp_tha[i] = 0xFF;
  102. ea->arp_tpa = dip;
  103. for (rlen = retry = 0; rlen == 0 && retry < 10; retry++) {
  104. if (EtherOutput(0, ETHERTYPE_ARP, sizeof(ETHERARP)) < 0)
  105. break;
  106. if ((rlen = EtherInput(ETHERTYPE_ARP, 1000)) > 0) {
  107. ARPFRAME *af = (ARPFRAME *) & rframe;
  108. ea = &af->eth_arp;
  109. if (ea->arp_tpa == confnet.cdn_ip_addr) {
  110. if (htons(ea->arp_op) == ARPOP_REPLY) {
  111. ae.ae_ip = ea->arp_spa;
  112. for (i = 0; i < 6; i++)
  113. ae.ae_ha[i] = ea->arp_sha[i];
  114. break;
  115. }
  116. }
  117. rlen = 0;
  118. }
  119. }
  120. }
  121. if (ae.ae_ip != dip)
  122. return -1;
  123. for (i = 0; i < 6; i++)
  124. dmac[i] = ae.ae_ha[i];
  125. return 0;
  126. }
  127. /*!
  128. * \brief Process incoming ARP packets.
  129. *
  130. * The incoming IP and hardware address pair is stored. This routine
  131. * does not respond to ARP requests.
  132. */
  133. void ArpRespond(void)
  134. {
  135. u_char i;
  136. ARPFRAME *af = (ARPFRAME *) & rframe;
  137. ETHERARP *ea = &af->eth_arp;
  138. ea = &arpframe.eth_arp;
  139. if (ea->arp_tpa == confnet.cdn_ip_addr) {
  140. if (htons(ea->arp_op) == ARPOP_REPLY) {
  141. ae.ae_ip = ea->arp_spa;
  142. for (i = 0; i < 6; i++)
  143. ae.ae_ha[i] = ea->arp_sha[i];
  144. }
  145. } else
  146. if (ea->arp_spa == confnet.cdn_ip_addr) {
  147. if (htons(ea->arp_op) == ARPOP_REQUEST) {
  148. /*
  149. * Set ARP header.
  150. */
  151. ea->arp_hrd = htons(ARPHRD_ETHER);
  152. ea->arp_pro = ETHERTYPE_IP;
  153. ea->arp_hln = 6;
  154. ea->arp_pln = 4;
  155. ea->arp_op = htons(ARPOP_REPLY);
  156. /*
  157. * Set ARP destination data.
  158. */
  159. for (i = 0; i < 6; i++)
  160. ea->arp_tha[i] = ea->arp_sha[i];
  161. ea->arp_tpa = ea->arp_spa;
  162. for (i = 0; i < 6; i++)
  163. ea->arp_sha[i] = confnet.cdn_mac[i];
  164. ea->arp_spa = confnet.cdn_ip_addr;
  165. EtherOutput(0, ETHERTYPE_ARP, sizeof(ETHERARP));
  166. }
  167. }
  168. }
  169. /*@}*/