arp_ipin.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /****************************************************************************
  2. * net/arp/arp_ipin.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 <netinet/in.h>
  44. #include <nuttx/net/netdev.h>
  45. #include <nuttx/net/arp.h>
  46. #include "arp/arp.h"
  47. #ifdef CONFIG_NET_ARP_IPIN
  48. /****************************************************************************
  49. * Pre-processor Definitions
  50. ****************************************************************************/
  51. #define ETHBUF ((struct eth_hdr_s *)&dev->d_buf[0])
  52. #define IPBUF ((struct arp_iphdr_s *)&dev->d_buf[ETH_HDRLEN])
  53. /****************************************************************************
  54. * Public Functions
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Name: arp_ipin
  58. *
  59. * Description:
  60. * The arp_ipin() function should be called by Ethernet device drivers
  61. * whenever an IP packet arrives from the network. The function will
  62. * check if the address is in the ARP cache, and if so the ARP cache entry
  63. * will be refreshed. If no ARP cache entry was found, a new one is created.
  64. *
  65. * This function expects that an IP packet with an Ethernet header is
  66. * present in the d_buf buffer and that the length of the packet is in the
  67. * d_len field.
  68. *
  69. ****************************************************************************/
  70. void arp_ipin(FAR struct net_driver_s *dev)
  71. {
  72. in_addr_t srcipaddr;
  73. /* Only insert/update an entry if the source IP address of the incoming IP
  74. * packet comes from a host on the local network.
  75. */
  76. srcipaddr = net_ip4addr_conv32(IPBUF->eh_srcipaddr);
  77. if (net_ipv4addr_maskcmp(srcipaddr, dev->d_ipaddr, dev->d_netmask))
  78. {
  79. arp_hdr_update(IPBUF->eh_srcipaddr, ETHBUF->src);
  80. }
  81. }
  82. #endif /* CONFIG_NET_ARP_IPIN */