2014-08-18 22:29:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/arp/arp_send.c
|
|
|
|
*
|
2020-05-17 16:47:40 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2014-08-18 22:29:02 +02:00
|
|
|
*
|
2020-05-17 16:47:40 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-08-18 22:29:02 +02:00
|
|
|
*
|
2020-05-17 16:47:40 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2014-08-18 22:29:02 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2014-11-15 15:55:50 +01:00
|
|
|
#include <string.h>
|
2014-08-18 22:29:02 +02:00
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
#include <nuttx/net/ip.h>
|
|
|
|
#include <nuttx/net/arp.h>
|
|
|
|
|
|
|
|
#include "netdev/netdev.h"
|
|
|
|
#include "devif/devif.h"
|
2014-08-20 15:09:02 +02:00
|
|
|
#include "route/route.h"
|
2014-08-18 22:29:02 +02:00
|
|
|
#include "arp/arp.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ARP_SEND
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-05-27 19:39:44 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: arp_send_terminate
|
2015-05-27 19:39:44 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void arp_send_terminate(FAR struct arp_send_s *state, int result)
|
|
|
|
{
|
|
|
|
/* Don't allow any further call backs. */
|
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
state->snd_sent = true;
|
|
|
|
state->snd_result = (int16_t)result;
|
|
|
|
state->snd_cb->flags = 0;
|
|
|
|
state->snd_cb->priv = NULL;
|
|
|
|
state->snd_cb->event = NULL;
|
2015-05-27 19:39:44 +02:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
/* Wake up the waiting thread */
|
2015-05-27 19:39:44 +02:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
nxsem_post(&state->snd_sem);
|
2015-05-27 19:39:44 +02:00
|
|
|
}
|
|
|
|
|
2014-08-18 22:29:02 +02:00
|
|
|
/****************************************************************************
|
2017-08-29 22:08:04 +02:00
|
|
|
* Name: arp_send_eventhandler
|
2014-08-18 22:29:02 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-29 22:08:04 +02:00
|
|
|
static uint16_t arp_send_eventhandler(FAR struct net_driver_s *dev,
|
|
|
|
FAR void *pvconn,
|
|
|
|
FAR void *priv, uint16_t flags)
|
2014-08-18 22:29:02 +02:00
|
|
|
{
|
|
|
|
FAR struct arp_send_s *state = (FAR struct arp_send_s *)priv;
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("flags: %04x sent: %d\n", flags, state->snd_sent);
|
2014-08-18 22:29:02 +02:00
|
|
|
|
|
|
|
if (state)
|
|
|
|
{
|
2018-11-09 20:57:16 +01:00
|
|
|
/* Is this the device that we need to route this request? */
|
|
|
|
|
|
|
|
if (strncmp((FAR const char *)dev->d_ifname,
|
|
|
|
(FAR const char *)state->snd_ifname, IFNAMSIZ) != 0)
|
|
|
|
{
|
|
|
|
/* No... pass on this one and wait for the device that we want */
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2015-05-27 19:39:44 +02:00
|
|
|
/* Check if the network is still up */
|
2014-08-18 22:29:02 +02:00
|
|
|
|
2015-05-27 19:39:44 +02:00
|
|
|
if ((flags & NETDEV_DOWN) != 0)
|
2014-08-18 22:29:02 +02:00
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Interface is down\n");
|
2015-05-27 19:39:44 +02:00
|
|
|
arp_send_terminate(state, -ENETUNREACH);
|
2014-08-18 22:29:02 +02:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the outgoing packet is available. It may have been claimed
|
2018-06-23 23:03:01 +02:00
|
|
|
* by a send event handler serving a different thread -OR- if the
|
|
|
|
* output buffer currently contains unprocessed incoming data. In
|
|
|
|
* these cases we will just have to wait for the next polling cycle.
|
2014-08-18 22:29:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->d_sndlen > 0 || (flags & PKT_NEWDATA) != 0)
|
|
|
|
{
|
|
|
|
/* Another thread has beat us sending data or the buffer is busy,
|
|
|
|
* Check for a timeout. If not timed out, wait for the next
|
|
|
|
* polling cycle and check again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* REVISIT: No timeout. Just wait for the next polling cycle */
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2019-03-11 19:48:17 +01:00
|
|
|
/* It looks like we are good to send the data.
|
|
|
|
*
|
|
|
|
* Copy the packet data into the device packet buffer and send it.
|
|
|
|
*/
|
2014-08-18 22:29:02 +02:00
|
|
|
|
|
|
|
arp_format(dev, state->snd_ipaddr);
|
|
|
|
|
|
|
|
/* Make sure no ARP request overwrites this ARP request. This
|
|
|
|
* flag will be cleared in arp_out().
|
|
|
|
*/
|
|
|
|
|
2015-01-20 22:59:52 +01:00
|
|
|
IFF_SET_NOARP(dev->d_flags);
|
2014-08-18 23:24:51 +02:00
|
|
|
|
2014-08-18 22:29:02 +02:00
|
|
|
/* Don't allow any further call backs. */
|
|
|
|
|
2015-05-27 19:39:44 +02:00
|
|
|
arp_send_terminate(state, OK);
|
2014-08-18 22:29:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: arp_send
|
2014-08-18 22:29:02 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The arp_send() call may be to send an ARP request to resolve an IP
|
|
|
|
* address. This function first checks if the IP address is already in
|
|
|
|
* ARP table. If so, then it returns success immediately.
|
|
|
|
*
|
|
|
|
* If the requested IP address in not in the ARP table, then this function
|
|
|
|
* will send an ARP request, delay, then check if the IP address is now in
|
|
|
|
* the ARP table. It will repeat this sequence until either (1) the IP
|
|
|
|
* address mapping is now in the ARP table, or (2) a configurable number
|
|
|
|
* of timeouts occur without receiving the ARP replay.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2014-08-20 15:09:02 +02:00
|
|
|
* ipaddr The IP address to be queried (in network order).
|
2014-08-18 22:29:02 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success and the IP address mapping can now be
|
|
|
|
* found in the ARP table. On error a negated errno value is returned:
|
|
|
|
*
|
|
|
|
* -ETIMEDOUT: The number or retry counts has been exceed.
|
|
|
|
* -EHOSTUNREACH: Could not find a route to the host
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from the normal tasking context.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int arp_send(in_addr_t ipaddr)
|
|
|
|
{
|
|
|
|
FAR struct net_driver_s *dev;
|
2014-08-19 15:47:32 +02:00
|
|
|
struct arp_notify_s notify;
|
2014-08-18 22:29:02 +02:00
|
|
|
struct arp_send_s state;
|
|
|
|
int ret;
|
|
|
|
|
2014-08-20 15:09:02 +02:00
|
|
|
/* First check if destination is a local broadcast. */
|
|
|
|
|
|
|
|
if (ipaddr == INADDR_BROADCAST)
|
|
|
|
{
|
|
|
|
/* We don't need to send the ARP request */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:19:44 +01:00
|
|
|
#ifdef CONFIG_NET_IGMP
|
2014-08-20 15:09:02 +02:00
|
|
|
/* Check if the destination address is a multicast address
|
|
|
|
*
|
2020-05-17 16:47:40 +02:00
|
|
|
* - IPv4: multicast addresses lie in the class D group -- The address
|
|
|
|
* range 224.0.0.0 to 239.255.255.255 (224.0.0.0/4)
|
2014-08-20 15:09:02 +02:00
|
|
|
*
|
|
|
|
* - IPv6 multicast addresses are have the high-order octet of the
|
|
|
|
* addresses=0xff (ff00::/8.)
|
|
|
|
*/
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
if (NTOHL(ipaddr) >= 0xe0000000 && NTOHL(ipaddr) <= 0xefffffff)
|
2014-08-20 15:09:02 +02:00
|
|
|
{
|
|
|
|
/* We don't need to send the ARP request */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-08-18 22:29:02 +02:00
|
|
|
/* Get the device that can route this request */
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
dev = netdev_findby_ripv4addr(INADDR_ANY, ipaddr);
|
2014-08-18 22:29:02 +02:00
|
|
|
if (!dev)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Unreachable: %08lx\n", (unsigned long)ipaddr);
|
2014-08-18 22:29:02 +02:00
|
|
|
ret = -EHOSTUNREACH;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2018-10-20 14:35:39 +02:00
|
|
|
/* ARP support is only built if the Ethernet link layer is supported.
|
2017-08-08 22:24:12 +02:00
|
|
|
* Continue and send the ARP request only if this device uses the
|
2018-10-20 14:35:39 +02:00
|
|
|
* Ethernet link layer protocol.
|
2014-11-15 01:25:33 +01:00
|
|
|
*/
|
|
|
|
|
2020-01-06 12:35:50 +01:00
|
|
|
if (dev->d_lltype != NET_LL_ETHERNET &&
|
|
|
|
dev->d_lltype != NET_LL_IEEE80211)
|
2014-11-15 01:25:33 +01:00
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2014-08-20 15:09:02 +02:00
|
|
|
/* Check if the destination address is on the local network. */
|
|
|
|
|
2015-01-15 19:19:44 +01:00
|
|
|
if (!net_ipv4addr_maskcmp(ipaddr, dev->d_ipaddr, dev->d_netmask))
|
2014-08-20 15:09:02 +02:00
|
|
|
{
|
2015-01-15 19:19:44 +01:00
|
|
|
in_addr_t dripaddr;
|
2014-08-20 15:09:02 +02:00
|
|
|
|
|
|
|
/* Destination address is not on the local network */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ROUTE
|
|
|
|
/* We have a routing table.. find the correct router to use in
|
|
|
|
* this case (or, as a fall-back, use the device's default router
|
|
|
|
* address). We will use the router IP address instead of the
|
|
|
|
* destination address when determining the MAC address.
|
|
|
|
*/
|
|
|
|
|
2015-01-20 20:48:42 +01:00
|
|
|
netdev_ipv4_router(dev, ipaddr, &dripaddr);
|
2014-08-20 15:09:02 +02:00
|
|
|
#else
|
|
|
|
/* Use the device's default router IP address instead of the
|
|
|
|
* destination address when determining the MAC address.
|
|
|
|
*/
|
|
|
|
|
2015-01-15 19:19:44 +01:00
|
|
|
net_ipv4addr_copy(dripaddr, dev->d_draddr);
|
2014-08-20 15:09:02 +02:00
|
|
|
#endif
|
|
|
|
ipaddr = dripaddr;
|
|
|
|
}
|
|
|
|
|
2017-11-22 19:06:36 +01:00
|
|
|
/* The destination address is on the local network. Check if it is
|
|
|
|
* the sub-net broadcast address.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (net_ipv4addr_broadcast(ipaddr, dev->d_netmask))
|
|
|
|
{
|
|
|
|
/* Yes.. We don't need to send the ARP request */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:29:02 +02:00
|
|
|
/* Allocate resources to receive a callback. This and the following
|
|
|
|
* initialization is performed with the network lock because we don't
|
|
|
|
* want anything to happen until we are ready.
|
|
|
|
*/
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2015-05-27 19:39:44 +02:00
|
|
|
state.snd_cb = arp_callback_alloc(dev);
|
2014-08-18 22:29:02 +02:00
|
|
|
if (!state.snd_cb)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Failed to allocate a callback\n");
|
2014-08-18 22:29:02 +02:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
2016-11-03 19:17:02 +01:00
|
|
|
/* This semaphore is used for signaling and, hence, should not have
|
|
|
|
* priority inheritance enabled.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_init(&state.snd_sem, 0, 0); /* Doesn't really fail */
|
2020-05-17 15:56:21 +02:00
|
|
|
nxsem_set_protocol(&state.snd_sem, SEM_PRIO_NONE);
|
2016-11-03 19:17:02 +01:00
|
|
|
|
2014-08-18 22:29:02 +02:00
|
|
|
state.snd_retries = 0; /* No retries yet */
|
|
|
|
state.snd_ipaddr = ipaddr; /* IP address to query */
|
|
|
|
|
|
|
|
/* Remember the routing device name */
|
|
|
|
|
2018-08-25 01:36:08 +02:00
|
|
|
strncpy((FAR char *)state.snd_ifname, (FAR const char *)dev->d_ifname,
|
|
|
|
IFNAMSIZ);
|
2014-08-18 22:29:02 +02:00
|
|
|
|
2018-06-23 23:03:01 +02:00
|
|
|
/* Now loop, testing if the address mapping is in the ARP table and re-
|
|
|
|
* sending the ARP request if it is not.
|
2014-08-18 22:29:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -ETIMEDOUT; /* Assume a timeout failure */
|
2014-08-18 23:24:51 +02:00
|
|
|
|
2014-08-18 22:29:02 +02:00
|
|
|
while (state.snd_retries < CONFIG_ARP_SEND_MAXTRIES)
|
|
|
|
{
|
2014-08-19 15:47:32 +02:00
|
|
|
/* Check if the address mapping is present in the ARP table. This
|
|
|
|
* is only really meaningful on the first time through the loop.
|
|
|
|
*
|
|
|
|
* NOTE: If the ARP table is large than this could be a performance
|
|
|
|
* issue.
|
|
|
|
*/
|
2014-08-18 22:29:02 +02:00
|
|
|
|
2018-08-25 14:47:10 +02:00
|
|
|
if (arp_find(ipaddr, NULL) >= 0)
|
2014-08-18 22:29:02 +02:00
|
|
|
{
|
|
|
|
/* We have it! Break out with success */
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-08-19 15:47:32 +02:00
|
|
|
/* Set up the ARP response wait BEFORE we send the ARP request */
|
|
|
|
|
|
|
|
arp_wait_setup(ipaddr, ¬ify);
|
|
|
|
|
2014-08-18 22:29:02 +02:00
|
|
|
/* Arm/re-arm the callback */
|
|
|
|
|
2014-08-19 15:47:32 +02:00
|
|
|
state.snd_sent = false;
|
2015-05-27 19:39:44 +02:00
|
|
|
state.snd_result = -EBUSY;
|
2015-05-27 20:22:07 +02:00
|
|
|
state.snd_cb->flags = (ARP_POLL | NETDEV_DOWN);
|
2014-08-19 15:47:32 +02:00
|
|
|
state.snd_cb->priv = (FAR void *)&state;
|
2017-08-29 22:08:04 +02:00
|
|
|
state.snd_cb->event = arp_send_eventhandler;
|
2014-08-19 15:47:32 +02:00
|
|
|
|
2018-11-09 18:22:58 +01:00
|
|
|
/* Notify the device driver that new TX data is available. */
|
2014-08-19 15:47:32 +02:00
|
|
|
|
2018-11-09 18:22:58 +01:00
|
|
|
netdev_txnotify_dev(dev);
|
2014-08-19 15:47:32 +02:00
|
|
|
|
2018-06-23 23:03:01 +02:00
|
|
|
/* Wait for the send to complete or an error to occur.
|
|
|
|
* net_lockedwait will also terminate if a signal is received.
|
2014-08-19 15:47:32 +02:00
|
|
|
*/
|
2014-08-18 22:29:02 +02:00
|
|
|
|
2014-08-19 15:47:32 +02:00
|
|
|
do
|
|
|
|
{
|
2021-02-22 03:35:18 +01:00
|
|
|
ret = net_timedwait_uninterruptible(&state.snd_sem,
|
|
|
|
CONFIG_ARP_SEND_DELAYMSEC);
|
|
|
|
if (ret == -ETIMEDOUT)
|
|
|
|
{
|
2021-03-30 23:33:37 +02:00
|
|
|
arp_wait_cancel(¬ify);
|
2021-02-22 03:35:18 +01:00
|
|
|
goto timeout;
|
|
|
|
}
|
2014-08-19 15:47:32 +02:00
|
|
|
}
|
|
|
|
while (!state.snd_sent);
|
2014-08-18 22:29:02 +02:00
|
|
|
|
2015-05-27 19:39:44 +02:00
|
|
|
/* Check the result of the send operation */
|
|
|
|
|
|
|
|
ret = state.snd_result;
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Break out on a send failure */
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Send failed: %d\n", ret);
|
2021-03-30 23:33:37 +02:00
|
|
|
arp_wait_cancel(¬ify);
|
2015-05-27 19:39:44 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-11-09 22:55:38 +01:00
|
|
|
/* Now wait for response to the ARP response to be received. */
|
2014-08-18 22:29:02 +02:00
|
|
|
|
2020-01-04 11:37:46 +01:00
|
|
|
ret = arp_wait(¬ify, CONFIG_ARP_SEND_DELAYMSEC);
|
2014-08-18 22:29:02 +02:00
|
|
|
|
2014-08-19 15:47:32 +02:00
|
|
|
/* arp_wait will return OK if and only if the matching ARP response
|
|
|
|
* is received. Otherwise, it will return -ETIMEDOUT.
|
|
|
|
*/
|
2014-08-18 22:29:02 +02:00
|
|
|
|
2015-05-29 22:32:56 +02:00
|
|
|
if (ret >= OK)
|
2014-08-19 15:47:32 +02:00
|
|
|
{
|
2015-05-27 19:39:44 +02:00
|
|
|
/* Break out if arp_wait() fails */
|
|
|
|
|
2014-08-19 15:47:32 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-08-18 22:29:02 +02:00
|
|
|
|
2021-02-22 03:35:18 +01:00
|
|
|
timeout:
|
|
|
|
|
2020-01-04 11:37:46 +01:00
|
|
|
/* Increment the retry count */
|
2014-08-18 22:29:02 +02:00
|
|
|
|
2014-08-19 15:47:32 +02:00
|
|
|
state.snd_retries++;
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: arp_wait failed: %d\n", ret);
|
2014-08-18 22:29:02 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&state.snd_sem);
|
2015-05-27 19:39:44 +02:00
|
|
|
arp_callback_free(dev, state.snd_cb);
|
2014-08-18 22:29:02 +02:00
|
|
|
errout_with_lock:
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2014-08-18 22:29:02 +02:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_ARP_SEND */
|