2007-11-01 00:27:55 +01:00
|
|
|
/****************************************************************************
|
2014-08-18 19:36:39 +02:00
|
|
|
* net/arp/arp_arpin.c
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2014-05-30 20:13:06 +02:00
|
|
|
* Copyright (C) 2007-2011, 2014 Gregory Nutt. All rights reserved.
|
2011-12-15 23:21:32 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2007-11-01 00:27:55 +01:00
|
|
|
* Based on uIP which also has a BSD style license:
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2007-11-01 00:27:55 +01:00
|
|
|
* Author: Adam Dunkels <adam@dunkels.com>
|
|
|
|
* Copyright (c) 2001-2003, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
2007-11-01 00:27:55 +01:00
|
|
|
*
|
2007-08-27 01:12:17 +02:00
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2007-11-01 00:27:55 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-11-06 00:04:16 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2007-08-27 01:12:17 +02:00
|
|
|
#include <string.h>
|
2007-11-04 23:59:30 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2007-09-17 00:12:04 +02:00
|
|
|
#include <netinet/in.h>
|
2011-03-12 16:36:28 +01:00
|
|
|
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2014-05-30 20:13:06 +02:00
|
|
|
#include <nuttx/net/arp.h>
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2014-07-06 19:05:28 +02:00
|
|
|
#include "arp/arp.h"
|
2014-06-26 17:32:39 +02:00
|
|
|
|
2011-03-12 16:36:28 +01:00
|
|
|
#ifdef CONFIG_NET_ARP
|
|
|
|
|
2007-11-01 00:27:55 +01:00
|
|
|
/****************************************************************************
|
2009-12-15 15:53:45 +01:00
|
|
|
* Pre-processor Definitions
|
2007-11-01 00:27:55 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-18 19:36:39 +02:00
|
|
|
#define ETHBUF ((struct eth_hdr_s *)&dev->d_buf[0])
|
2014-11-15 16:13:50 +01:00
|
|
|
#define ARPBUF ((struct arp_hdr_s *)&dev->d_buf[ETH_HDRLEN])
|
2010-07-13 04:29:09 +02:00
|
|
|
|
2007-11-01 00:27:55 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-07-06 19:05:28 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: arp_arpin
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2014-07-06 19:05:28 +02:00
|
|
|
* Description:
|
2018-01-01 22:08:19 +01:00
|
|
|
* This function should be called by the Ethernet device driver when an
|
|
|
|
* ARP packet has been received. The function will act differently
|
2014-07-06 19:05:28 +02:00
|
|
|
* depending on the ARP packet type: if it is a reply for a request
|
|
|
|
* that we previously sent out, the ARP cache will be filled in with
|
|
|
|
* the values from the ARP reply. If the incoming ARP packet is an ARP
|
|
|
|
* request for our IP address, an ARP reply packet is created and put
|
2016-11-29 23:12:39 +01:00
|
|
|
* into the d_buf buffer.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2014-07-06 19:05:28 +02:00
|
|
|
* On entry, this function expects that an ARP packet with a prepended
|
2016-11-29 23:12:39 +01:00
|
|
|
* Ethernet header is present in the d_buf buffer and that the length of
|
2014-07-06 19:05:28 +02:00
|
|
|
* the packet is set in the d_len field.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2018-01-01 22:08:19 +01:00
|
|
|
* When the function returns, the value of the field d_len indicates
|
|
|
|
* whether the device driver should send out the ARP reply packet or not.
|
|
|
|
* If d_len is zero, no packet should be sent; If d_len is non-zero, it
|
|
|
|
* contains the length of the outbound packet that is present in the
|
|
|
|
* d_buf buffer.
|
2014-07-06 19:05:28 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2014-07-06 19:05:28 +02:00
|
|
|
void arp_arpin(FAR struct net_driver_s *dev)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2015-01-19 21:56:35 +01:00
|
|
|
FAR struct arp_hdr_s *arp = ARPBUF;
|
2007-11-01 00:27:55 +01:00
|
|
|
in_addr_t ipaddr;
|
2007-11-06 00:04:16 +01:00
|
|
|
|
2014-11-15 16:13:50 +01:00
|
|
|
if (dev->d_len < (sizeof(struct arp_hdr_s) + ETH_HDRLEN))
|
2007-09-16 19:46:25 +02:00
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Packet Too small\n");
|
2007-09-16 19:46:25 +02:00
|
|
|
dev->d_len = 0;
|
|
|
|
return;
|
|
|
|
}
|
2014-04-12 20:13:01 +02:00
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
dev->d_len = 0;
|
|
|
|
|
2015-01-19 21:56:35 +01:00
|
|
|
ipaddr = net_ip4addr_conv32(arp->ah_dipaddr);
|
2015-10-08 23:10:04 +02:00
|
|
|
switch (arp->ah_opcode)
|
2007-09-16 19:46:25 +02:00
|
|
|
{
|
|
|
|
case HTONS(ARP_REQUEST):
|
2018-01-01 22:08:19 +01:00
|
|
|
ninfo("ARP request for IP %04lx\n", (unsigned long)ipaddr);
|
2009-03-08 19:57:27 +01:00
|
|
|
|
2007-09-16 19:46:25 +02:00
|
|
|
/* ARP request. If it asked for our address, we send out a reply. */
|
|
|
|
|
2015-01-15 19:19:44 +01:00
|
|
|
if (net_ipv4addr_cmp(ipaddr, dev->d_ipaddr))
|
2007-09-16 19:46:25 +02:00
|
|
|
{
|
2015-01-19 21:56:35 +01:00
|
|
|
FAR struct eth_hdr_s *eth = ETHBUF;
|
2013-08-25 15:33:47 +02:00
|
|
|
|
2007-09-16 19:46:25 +02:00
|
|
|
/* First, we register the one who made the request in our ARP
|
|
|
|
* table, since it is likely that we will do more communication
|
|
|
|
* with this host in the future.
|
|
|
|
*/
|
|
|
|
|
2016-02-08 18:17:22 +01:00
|
|
|
arp_hdr_update(arp->ah_sipaddr, arp->ah_shwaddr);
|
2007-09-16 19:46:25 +02:00
|
|
|
|
2015-01-19 21:56:35 +01:00
|
|
|
arp->ah_opcode = HTONS(ARP_REPLY);
|
|
|
|
memcpy(arp->ah_dhwaddr, arp->ah_shwaddr, ETHER_ADDR_LEN);
|
2019-10-25 19:31:42 +02:00
|
|
|
memcpy(arp->ah_shwaddr, dev->d_mac.ether.ether_addr_octet,
|
|
|
|
ETHER_ADDR_LEN);
|
|
|
|
memcpy(eth->src, dev->d_mac.ether.ether_addr_octet,
|
|
|
|
ETHER_ADDR_LEN);
|
2015-01-19 21:56:35 +01:00
|
|
|
memcpy(eth->dest, arp->ah_dhwaddr, ETHER_ADDR_LEN);
|
2007-09-16 19:46:25 +02:00
|
|
|
|
2015-01-19 21:56:35 +01:00
|
|
|
arp->ah_dipaddr[0] = arp->ah_sipaddr[0];
|
|
|
|
arp->ah_dipaddr[1] = arp->ah_sipaddr[1];
|
|
|
|
net_ipv4addr_hdrcopy(arp->ah_sipaddr, &dev->d_ipaddr);
|
|
|
|
arp_dump(arp);
|
2007-09-16 19:46:25 +02:00
|
|
|
|
2015-01-19 21:56:35 +01:00
|
|
|
eth->type = HTONS(ETHTYPE_ARP);
|
2014-11-15 16:13:50 +01:00
|
|
|
dev->d_len = sizeof(struct arp_hdr_s) + ETH_HDRLEN;
|
2007-09-16 19:46:25 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HTONS(ARP_REPLY):
|
2018-01-01 22:08:19 +01:00
|
|
|
ninfo("ARP reply for IP %04lx\n", (unsigned long)ipaddr);
|
2009-03-08 19:57:27 +01:00
|
|
|
|
2007-09-16 19:46:25 +02:00
|
|
|
/* ARP reply. We insert or update the ARP table if it was meant
|
|
|
|
* for us.
|
|
|
|
*/
|
|
|
|
|
2015-01-15 19:19:44 +01:00
|
|
|
if (net_ipv4addr_cmp(ipaddr, dev->d_ipaddr))
|
2007-09-16 19:46:25 +02:00
|
|
|
{
|
2014-08-19 15:47:32 +02:00
|
|
|
/* Yes... Insert the address mapping in the ARP table */
|
|
|
|
|
2016-02-08 18:17:22 +01:00
|
|
|
arp_hdr_update(arp->ah_sipaddr, arp->ah_shwaddr);
|
2014-08-19 15:47:32 +02:00
|
|
|
|
|
|
|
/* Then notify any logic waiting for the ARP result */
|
|
|
|
|
2015-01-19 21:56:35 +01:00
|
|
|
arp_notify(net_ip4addr_conv32(arp->ah_sipaddr));
|
2007-09-16 19:46:25 +02:00
|
|
|
}
|
|
|
|
break;
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2011-03-12 16:36:28 +01:00
|
|
|
#endif /* CONFIG_NET_ARP */
|