arp_arpin.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /****************************************************************************
  2. * net/arp/arp_arpin.c
  3. *
  4. * Copyright (C) 2007-2011, 2014 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Based on uIP which also has a BSD style license:
  8. *
  9. * Author: Adam Dunkels <adam@dunkels.com>
  10. * Copyright (c) 2001-2003, Adam Dunkels.
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * 3. The name of the author may not be used to endorse or promote
  23. * products derived from this software without specific prior
  24. * written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  27. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  28. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  30. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  32. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  34. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. ****************************************************************************/
  39. /****************************************************************************
  40. * Included Files
  41. ****************************************************************************/
  42. #include <nuttx/config.h>
  43. #include <string.h>
  44. #include <debug.h>
  45. #include <netinet/in.h>
  46. #include <nuttx/net/netdev.h>
  47. #include <nuttx/net/arp.h>
  48. #include "arp/arp.h"
  49. #ifdef CONFIG_NET_ARP
  50. /****************************************************************************
  51. * Pre-processor Definitions
  52. ****************************************************************************/
  53. #define ETHBUF ((struct eth_hdr_s *)&dev->d_buf[0])
  54. #define ARPBUF ((struct arp_hdr_s *)&dev->d_buf[ETH_HDRLEN])
  55. /****************************************************************************
  56. * Public Functions
  57. ****************************************************************************/
  58. /****************************************************************************
  59. * Name: arp_arpin
  60. *
  61. * Description:
  62. * This function should be called by the Ethernet device driver when an
  63. * ARP packet has been received. The function will act differently
  64. * depending on the ARP packet type: if it is a reply for a request
  65. * that we previously sent out, the ARP cache will be filled in with
  66. * the values from the ARP reply. If the incoming ARP packet is an ARP
  67. * request for our IP address, an ARP reply packet is created and put
  68. * into the d_buf buffer.
  69. *
  70. * On entry, this function expects that an ARP packet with a prepended
  71. * Ethernet header is present in the d_buf buffer and that the length of
  72. * the packet is set in the d_len field.
  73. *
  74. * When the function returns, the value of the field d_len indicates
  75. * whether the device driver should send out the ARP reply packet or not.
  76. * If d_len is zero, no packet should be sent; If d_len is non-zero, it
  77. * contains the length of the outbound packet that is present in the
  78. * d_buf buffer.
  79. *
  80. ****************************************************************************/
  81. void arp_arpin(FAR struct net_driver_s *dev)
  82. {
  83. FAR struct arp_hdr_s *arp = ARPBUF;
  84. in_addr_t ipaddr;
  85. if (dev->d_len < (sizeof(struct arp_hdr_s) + ETH_HDRLEN))
  86. {
  87. nerr("ERROR: Packet Too small\n");
  88. dev->d_len = 0;
  89. return;
  90. }
  91. dev->d_len = 0;
  92. ipaddr = net_ip4addr_conv32(arp->ah_dipaddr);
  93. switch (arp->ah_opcode)
  94. {
  95. case HTONS(ARP_REQUEST):
  96. ninfo("ARP request for IP %04lx\n", (unsigned long)ipaddr);
  97. /* ARP request. If it asked for our address, we send out a reply. */
  98. if (net_ipv4addr_cmp(ipaddr, dev->d_ipaddr))
  99. {
  100. FAR struct eth_hdr_s *eth = ETHBUF;
  101. /* First, we register the one who made the request in our ARP
  102. * table, since it is likely that we will do more communication
  103. * with this host in the future.
  104. */
  105. arp_hdr_update(arp->ah_sipaddr, arp->ah_shwaddr);
  106. arp->ah_opcode = HTONS(ARP_REPLY);
  107. memcpy(arp->ah_dhwaddr, arp->ah_shwaddr, ETHER_ADDR_LEN);
  108. memcpy(arp->ah_shwaddr, dev->d_mac.ether.ether_addr_octet,
  109. ETHER_ADDR_LEN);
  110. memcpy(eth->src, dev->d_mac.ether.ether_addr_octet,
  111. ETHER_ADDR_LEN);
  112. memcpy(eth->dest, arp->ah_dhwaddr, ETHER_ADDR_LEN);
  113. arp->ah_dipaddr[0] = arp->ah_sipaddr[0];
  114. arp->ah_dipaddr[1] = arp->ah_sipaddr[1];
  115. net_ipv4addr_hdrcopy(arp->ah_sipaddr, &dev->d_ipaddr);
  116. arp_dump(arp);
  117. eth->type = HTONS(ETHTYPE_ARP);
  118. dev->d_len = sizeof(struct arp_hdr_s) + ETH_HDRLEN;
  119. }
  120. break;
  121. case HTONS(ARP_REPLY):
  122. ninfo("ARP reply for IP %04lx\n", (unsigned long)ipaddr);
  123. /* ARP reply. We insert or update the ARP table if it was meant
  124. * for us.
  125. */
  126. if (net_ipv4addr_cmp(ipaddr, dev->d_ipaddr))
  127. {
  128. /* Yes... Insert the address mapping in the ARP table */
  129. arp_hdr_update(arp->ah_sipaddr, arp->ah_shwaddr);
  130. /* Then notify any logic waiting for the ARP result */
  131. arp_notify(net_ip4addr_conv32(arp->ah_sipaddr));
  132. }
  133. break;
  134. }
  135. }
  136. #endif /* CONFIG_NET_ARP */