arpin.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2001-2003 by egnite Software GmbH
  3. * Copyright (c) 1993 by Digital Equipment Corporation
  4. * Copyright (c) 1983, 1993 by The Regents of the University of California
  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. /*!
  38. * \file net/arpin.c
  39. * \brief ARP input functions.
  40. *
  41. * \verbatim
  42. * $Id: arpin.c 3686 2011-12-04 14:20:38Z haraldkipp $
  43. * \endverbatim
  44. */
  45. #include <sys/types.h>
  46. #include <net/if_var.h>
  47. #include <netinet/if_ether.h>
  48. #include <netinet/in.h>
  49. /*!
  50. * \addtogroup xgARP
  51. *
  52. * \todo Response may reuse received ARP packet.
  53. */
  54. /*@{*/
  55. /*!
  56. * \brief Handle incoming ARP packets.
  57. *
  58. * Packets not destined to us or packets with unsupported
  59. * address type or item length are silently discarded.
  60. *
  61. * \note This routine is called by the Ethernet layer on
  62. * incoming ARP packets. Applications typically do
  63. * not call this function.
  64. *
  65. * \param dev Identifies the device that received the packet.
  66. * \param nb Pointer to a network buffer structure containing
  67. * the ARP packet.
  68. */
  69. void NutArpInput(NUTDEVICE * dev, NETBUF * nb)
  70. {
  71. ETHERARP *ea;
  72. ARPHDR *ah;
  73. IFNET *nif;
  74. if (nb->nb_nw.sz < sizeof(ETHERARP)) {
  75. NutNetBufFree(nb);
  76. return;
  77. }
  78. ea = (ETHERARP *) nb->nb_nw.vp;
  79. ah = (ARPHDR *) & ea->ea_hdr;
  80. /*
  81. * Silently discard packets with unsupported
  82. * address types and lengths.
  83. */
  84. if (ntohs(ah->ar_hrd) != ARPHRD_ETHER ||
  85. ntohs(ah->ar_pro) != ETHERTYPE_IP ||
  86. ah->ar_hln != 6 || ah->ar_pln != 4) {
  87. NutNetBufFree(nb);
  88. return;
  89. }
  90. /*
  91. * Silently discard packets for other destinations.
  92. */
  93. nif = dev->dev_icb;
  94. if (ea->arp_tpa != nif->if_local_ip) {
  95. NutNetBufFree(nb);
  96. return;
  97. }
  98. /*
  99. * TODO: Silently discard packets with our own
  100. * source address.
  101. */
  102. /*
  103. * TODO: Discard packets with broadcast source
  104. * address.
  105. */
  106. /*
  107. * TODO: Discard packets if source IP address
  108. * equals our address. We should send a reply.
  109. */
  110. /*
  111. * Add the sender to our ARP cache. Note, that we don't do
  112. * this with replies only, but also with requests on our
  113. * address. The assumption is that if someone is requesting
  114. * our address, they are probably intending to talk to us,
  115. * so it saves time if we cache their address.
  116. */
  117. NutArpCacheUpdate(dev, ea->arp_spa, ea->arp_sha);
  118. /*
  119. * Reply to ARP requests.
  120. */
  121. if (htons(ea->ea_hdr.ar_op) == ARPOP_REQUEST) {
  122. NETBUF *nbr =
  123. NutArpAllocNetBuf(ARPOP_REPLY, ea->arp_spa, ea->arp_sha);
  124. if (nbr) {
  125. if (!NutArpOutput(dev, nbr))
  126. NutNetBufFree(nbr);
  127. }
  128. }
  129. NutNetBufFree(nb);
  130. }
  131. /*@}*/