2015-02-02 20:44:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/icmpv6/icmpv6_neighbor.c
|
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
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
|
2015-02-02 20:44:31 +01:00
|
|
|
*
|
2020-05-17 16:47:40 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-02-02 20:44:31 +01: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.
|
2015-02-02 20:44:31 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
|
2016-11-03 19:17:02 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2015-02-02 20:44:31 +01:00
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
#include <nuttx/net/ip.h>
|
|
|
|
#include <nuttx/net/icmpv6.h>
|
|
|
|
|
|
|
|
#include "netdev/netdev.h"
|
|
|
|
#include "devif/devif.h"
|
2017-08-07 19:50:50 +02:00
|
|
|
#include "inet/inet.h"
|
2015-02-02 20:44:31 +01:00
|
|
|
#include "neighbor/neighbor.h"
|
2017-08-10 15:27:40 +02:00
|
|
|
#include "route/route.h"
|
2015-02-02 20:44:31 +01:00
|
|
|
#include "icmpv6/icmpv6.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ICMPv6_NEIGHBOR
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure holds the state of the send operation until it can be
|
2018-06-23 22:09:06 +02:00
|
|
|
* operated upon from the event handler.
|
2015-02-02 20:44:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct icmpv6_neighbor_s
|
|
|
|
{
|
|
|
|
FAR struct devif_callback_s *snd_cb; /* Reference to callback instance */
|
|
|
|
sem_t snd_sem; /* Used to wake up the waiting thread */
|
|
|
|
uint8_t snd_retries; /* Retry count */
|
|
|
|
volatile bool snd_sent; /* True: if request sent */
|
|
|
|
uint8_t snd_ifname[IFNAMSIZ]; /* Interface name */
|
|
|
|
net_ipv6addr_t snd_ipaddr; /* The IPv6 address to be queried */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-08-29 22:08:04 +02:00
|
|
|
* Name: icmpv6_neighbor_eventhandler
|
2015-02-02 20:44:31 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-29 22:08:04 +02:00
|
|
|
static uint16_t icmpv6_neighbor_eventhandler(FAR struct net_driver_s *dev,
|
|
|
|
FAR void *priv, uint16_t flags)
|
2015-02-02 20:44:31 +01:00
|
|
|
{
|
|
|
|
FAR struct icmpv6_neighbor_s *state = (FAR struct icmpv6_neighbor_s *)priv;
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("flags: %04x sent: %d\n", flags, state->snd_sent);
|
2015-02-02 20:44:31 +01:00
|
|
|
|
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the outgoing packet is available. It may have been claimed
|
2020-05-17 16:47:40 +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.
|
2015-02-02 20:44:31 +01:00
|
|
|
*/
|
|
|
|
|
2015-02-03 18:19:33 +01:00
|
|
|
if (dev->d_sndlen > 0 || (flags & ICMPv6_NEWDATA) != 0)
|
2015-02-02 20:44:31 +01:00
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2023-01-11 09:09:20 +01:00
|
|
|
/* Prepare device buffer */
|
|
|
|
|
|
|
|
if (netdev_iob_prepare(dev, false, 0) != OK)
|
|
|
|
{
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2018-11-09 22:41:23 +01:00
|
|
|
/* It looks like we are good to send the data.
|
|
|
|
*
|
|
|
|
* Copy the packet data into the device packet buffer and send it.
|
|
|
|
*/
|
2015-02-02 20:44:31 +01:00
|
|
|
|
|
|
|
icmpv6_solicit(dev, state->snd_ipaddr);
|
|
|
|
|
2018-11-09 22:41:23 +01:00
|
|
|
IFF_SET_IPv6(dev->d_flags);
|
2015-02-02 20:44:31 +01:00
|
|
|
|
|
|
|
/* Don't allow any further call backs. */
|
|
|
|
|
|
|
|
state->snd_sent = true;
|
|
|
|
state->snd_cb->flags = 0;
|
|
|
|
state->snd_cb->priv = NULL;
|
|
|
|
state->snd_cb->event = NULL;
|
|
|
|
|
|
|
|
/* Wake up the waiting thread */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&state->snd_sem);
|
2015-02-02 20:44:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: icmpv6_neighbor
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The icmpv6_solicit() call may be to send an ICMPv6 Neighbor
|
|
|
|
* Solicitation to resolve an IPv6 address. This function first checks if
|
|
|
|
* the IPv6 address is already in the Neighbor Table. If so, then it
|
|
|
|
* returns success immediately.
|
|
|
|
*
|
|
|
|
* If the requested IPv6 address in not in the Neighbor Table, then this
|
|
|
|
* function will send the Neighbor Solicitation, delay, then check if the
|
|
|
|
* IP address is now in the Neighbor able. It will repeat this sequence
|
|
|
|
* until either (1) the IPv6 address mapping is now in the Neighbor table,
|
|
|
|
* or (2) a configurable number of timeouts occur without receiving the
|
|
|
|
* ICMPv6 Neighbor Advertisement.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2023-08-10 05:43:27 +02:00
|
|
|
* dev The suggested device driver structure to do the solicitation,
|
|
|
|
* can be NULL for auto decision, must set for link-local ipaddr.
|
2015-02-02 20:44:31 +01:00
|
|
|
* ipaddr The IPv6 address to be queried.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success and the IP address mapping can now be
|
2020-05-17 16:47:40 +02:00
|
|
|
* found in the Neighbor Table. On error a negated errno value is
|
|
|
|
* returned:
|
2015-02-02 20:44:31 +01:00
|
|
|
*
|
|
|
|
* -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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-08-10 05:43:27 +02:00
|
|
|
int icmpv6_neighbor(FAR struct net_driver_s *dev,
|
|
|
|
const net_ipv6addr_t ipaddr)
|
2015-02-02 20:44:31 +01:00
|
|
|
{
|
|
|
|
struct icmpv6_notify_s notify;
|
|
|
|
struct icmpv6_neighbor_s state;
|
2018-11-09 22:18:17 +01:00
|
|
|
net_ipv6addr_t lookup;
|
2015-02-02 20:44:31 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* First check if destination is a local broadcast or a multicast address.
|
|
|
|
*
|
|
|
|
* - IPv6 multicast addresses are have the high-order octet of the
|
|
|
|
* addresses=0xff (ff00::/8.)
|
|
|
|
*/
|
|
|
|
|
2018-06-23 20:53:27 +02:00
|
|
|
if (net_ipv6addr_cmp(ipaddr, g_ipv6_unspecaddr) ||
|
|
|
|
net_is_addr_mcast(ipaddr))
|
2015-02-02 20:44:31 +01:00
|
|
|
{
|
2018-11-08 01:21:21 +01:00
|
|
|
/* We don't need to send the Neighbor Solicitation. But for the case
|
|
|
|
* of the Multicast address, a routing able entry will be required.
|
|
|
|
*/
|
2015-02-02 20:44:31 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the device that can route this request */
|
|
|
|
|
2023-08-10 05:43:27 +02:00
|
|
|
if (!dev)
|
|
|
|
{
|
|
|
|
dev = netdev_findby_ripv6addr(g_ipv6_unspecaddr, ipaddr);
|
|
|
|
}
|
|
|
|
|
2015-02-02 20:44:31 +01:00
|
|
|
if (!dev)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Unreachable: %08lx\n", (unsigned long)ipaddr);
|
2015-02-02 20:44:31 +01:00
|
|
|
ret = -EHOSTUNREACH;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2024-01-24 08:00:38 +01:00
|
|
|
/* Neighbor support is only built if the Ethernet link layer is supported.
|
|
|
|
* Continue and send the Solicitation only if this device uses the
|
|
|
|
* Ethernet link layer protocol.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->d_lltype != NET_LL_ETHERNET &&
|
|
|
|
dev->d_lltype != NET_LL_IEEE80211)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-02-02 20:44:31 +01:00
|
|
|
/* Check if the destination address is on the local network. */
|
|
|
|
|
2023-08-25 11:37:11 +02:00
|
|
|
if (NETDEV_V6ADDR_ONLINK(dev, ipaddr) || net_is_addr_linklocal(ipaddr))
|
2015-02-02 20:44:31 +01:00
|
|
|
{
|
|
|
|
/* Yes.. use the input address for the lookup */
|
|
|
|
|
2018-11-09 22:18:17 +01:00
|
|
|
net_ipv6addr_copy(lookup, ipaddr);
|
2015-02-02 20:44:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2018-11-09 22:18:17 +01:00
|
|
|
netdev_ipv6_router(dev, ipaddr, lookup);
|
2015-02-02 20:44:31 +01:00
|
|
|
#else
|
|
|
|
/* Use the device's default router IP address instead of the
|
|
|
|
* destination address when determining the MAC address.
|
|
|
|
*/
|
|
|
|
|
2018-11-09 22:18:17 +01:00
|
|
|
net_ipv6addr_copy(lookup, dev->d_ipv6draddr);
|
2015-02-02 20:44:31 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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();
|
net/devif/devif_callback.c: corrected the connection event list to work as FIFO instead of LIFO.
In case of enabled packet forwarding mode, packets were forwarded in a reverse order
because of LIFO behavior of the connection event list.
The issue exposed only during high network traffic. Thus the event list started to grow
that resulted in changing the order of packets inside of groups of several packets
like the following: 3, 2, 1, 6, 5, 4, 8, 7 etc.
Remarks concerning the connection event list implementation:
* Now the queue (list) is FIFO as it should be.
* The list is singly linked.
* The list has a head pointer (inside of outer net_driver_s structure),
and a tail pointer is added into outer net_driver_s structure.
* The list item is devif_callback_s structure.
It still has two pointers to two different list chains (*nxtconn and *nxtdev).
* As before the first argument (*dev) of the list functions can be NULL,
while the other argument (*list) is effective (not NULL).
* An extra (*tail) argument is added to devif_callback_alloc()
and devif_conn_callback_free() functions.
* devif_callback_alloc() time complexity is O(1) (i.e. O(n) to fill the whole list).
* devif_callback_free() time complexity is O(n) (i.e. O(n^2) to empty the whole list).
* devif_conn_event() time complexity is O(n).
2021-08-29 22:57:26 +02:00
|
|
|
state.snd_cb = devif_callback_alloc((dev),
|
|
|
|
&(dev)->d_conncb,
|
|
|
|
&(dev)->d_conncb_tail);
|
2015-02-02 20:44:31 +01:00
|
|
|
if (!state.snd_cb)
|
|
|
|
{
|
2016-06-12 15:08:40 +02:00
|
|
|
nerr("ERROR: Failed to allocate a callback\n");
|
2015-02-02 20:44:31 +01:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_init(&state.snd_sem, 0, 0); /* Doesn't really fail */
|
2016-11-03 19:17:02 +01:00
|
|
|
|
2017-07-05 19:01:16 +02:00
|
|
|
state.snd_retries = 0; /* No retries yet */
|
2015-02-02 20:44:31 +01:00
|
|
|
net_ipv6addr_copy(state.snd_ipaddr, lookup); /* IP address to query */
|
|
|
|
|
|
|
|
/* Remember the routing device name */
|
|
|
|
|
2022-08-24 04:23:20 +02:00
|
|
|
strlcpy((FAR char *)state.snd_ifname, (FAR const char *)dev->d_ifname,
|
2015-02-03 18:19:33 +01:00
|
|
|
IFNAMSIZ);
|
2015-02-02 20:44:31 +01:00
|
|
|
|
2015-02-03 18:19:33 +01:00
|
|
|
/* Now loop, testing if the address mapping is in the Neighbor Table and
|
|
|
|
* re-sending the Neighbor Solicitation if it is not.
|
2015-02-02 20:44:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -ETIMEDOUT; /* Assume a timeout failure */
|
|
|
|
|
|
|
|
while (state.snd_retries < CONFIG_ICMPv6_NEIGHBOR_MAXTRIES)
|
|
|
|
{
|
|
|
|
/* Check if the address mapping is present in the Neighbor Table. This
|
|
|
|
* is only really meaningful on the first time through the loop.
|
|
|
|
*
|
2020-05-17 16:47:40 +02:00
|
|
|
* NOTE: If the Neighbor Table is large than this could be a
|
|
|
|
* performance issue.
|
2015-02-02 20:44:31 +01:00
|
|
|
*/
|
|
|
|
|
2018-11-09 21:14:15 +01:00
|
|
|
if (neighbor_lookup(lookup, NULL) >= 0)
|
2015-02-02 20:44:31 +01:00
|
|
|
{
|
|
|
|
/* We have it! Break out with success */
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-02-03 18:19:33 +01:00
|
|
|
/* Set up the Neighbor Advertisement wait BEFORE we send the Neighbor
|
|
|
|
* Solicitation.
|
|
|
|
*/
|
2015-02-02 20:44:31 +01:00
|
|
|
|
|
|
|
icmpv6_wait_setup(lookup, ¬ify);
|
|
|
|
|
|
|
|
/* Arm/re-arm the callback */
|
|
|
|
|
|
|
|
state.snd_sent = false;
|
|
|
|
state.snd_cb->flags = ICMPv6_POLL;
|
|
|
|
state.snd_cb->priv = (FAR void *)&state;
|
2017-08-29 22:08:04 +02:00
|
|
|
state.snd_cb->event = icmpv6_neighbor_eventhandler;
|
2015-02-02 20:44:31 +01:00
|
|
|
|
2015-02-03 20:25:28 +01:00
|
|
|
/* Notify the device driver that new TX data is available. */
|
2015-02-02 20:44:31 +01:00
|
|
|
|
2018-11-09 18:22:58 +01:00
|
|
|
netdev_txnotify_dev(dev);
|
2015-02-02 20:44:31 +01:00
|
|
|
|
2018-06-23 23:03:01 +02:00
|
|
|
/* Wait for the send to complete or an error to occur.
|
2023-01-13 07:51:38 +01:00
|
|
|
* net_sem_wait will also terminate if a signal is received.
|
2015-02-02 20:44:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2023-01-13 07:51:38 +01:00
|
|
|
net_sem_wait(&state.snd_sem);
|
2015-02-02 20:44:31 +01:00
|
|
|
}
|
|
|
|
while (!state.snd_sent);
|
|
|
|
|
2020-05-17 16:47:40 +02:00
|
|
|
/* Now wait for response to the Neighbor Advertisement to be
|
|
|
|
* received.
|
|
|
|
*/
|
2015-02-02 20:44:31 +01:00
|
|
|
|
2020-01-04 11:37:46 +01:00
|
|
|
ret = icmpv6_wait(¬ify, CONFIG_ICMPv6_NEIGHBOR_DELAYMSEC);
|
2015-02-02 20:44:31 +01:00
|
|
|
|
2015-02-03 18:19:33 +01:00
|
|
|
/* icmpv6_wait will return OK if and only if the matching Neighbor
|
|
|
|
* Advertisement is received. Otherwise, it will return -ETIMEDOUT.
|
2015-02-02 20:44:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-04 11:37:46 +01:00
|
|
|
/* Increment the retry count */
|
2015-02-02 20:44:31 +01:00
|
|
|
|
|
|
|
state.snd_retries++;
|
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&state.snd_sem);
|
2019-03-19 16:43:32 +01:00
|
|
|
devif_dev_callback_free(dev, state.snd_cb);
|
2018-11-10 14:13:54 +01:00
|
|
|
|
2015-02-02 20:44:31 +01:00
|
|
|
errout_with_lock:
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2018-11-10 14:13:54 +01:00
|
|
|
|
2015-02-02 20:44:31 +01:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_ICMPv6_NEIGHBOR */
|