2015-08-24 19:03:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/net/loopback.c
|
|
|
|
*
|
2021-03-04 07:10:42 +01: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-08-24 19:03:36 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01: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-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
2015-08-26 04:09:12 +02:00
|
|
|
#include <net/if.h>
|
2015-08-24 19:03:36 +02:00
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
#include <nuttx/wqueue.h>
|
2020-02-11 01:35:02 +01:00
|
|
|
#include <nuttx/net/netconfig.h>
|
2015-08-24 19:03:36 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
#include <nuttx/net/ip.h>
|
|
|
|
#include <nuttx/net/loopback.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_PKT
|
|
|
|
# include <nuttx/net/pkt.h>
|
|
|
|
#endif
|
|
|
|
|
2020-02-02 07:15:24 +01:00
|
|
|
#ifdef CONFIG_NET_LOOPBACK
|
2017-11-05 19:25:58 +01:00
|
|
|
|
2015-08-24 19:03:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-02-02 07:15:24 +01:00
|
|
|
/* We need to have the work queue to handle interrupts */
|
2016-11-18 16:22:49 +01:00
|
|
|
|
|
|
|
#if !defined(CONFIG_SCHED_WORKQUEUE)
|
|
|
|
# error Worker thread support is required (CONFIG_SCHED_WORKQUEUE)
|
2015-08-24 19:03:36 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* The lo_driver_s encapsulates all state information for a single hardware
|
|
|
|
* interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct lo_driver_s
|
|
|
|
{
|
|
|
|
bool lo_bifup; /* true:ifup false:ifdown */
|
2017-03-03 21:45:09 +01:00
|
|
|
struct work_s lo_work; /* For deferring poll work to the work queue */
|
2015-08-24 19:03:36 +02:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
/* This holds the information visible to the NuttX network */
|
2015-08-24 19:03:36 +02:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
struct net_driver_s lo_dev; /* Interface understood by the network */
|
2015-08-24 19:03:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static struct lo_driver_s g_loopback;
|
2015-08-26 18:33:44 +02:00
|
|
|
|
2015-08-24 19:03:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* NuttX callback functions */
|
|
|
|
|
|
|
|
static int lo_ifup(FAR struct net_driver_s *dev);
|
|
|
|
static int lo_ifdown(FAR struct net_driver_s *dev);
|
|
|
|
static void lo_txavail_work(FAR void *arg);
|
|
|
|
static int lo_txavail(FAR struct net_driver_s *dev);
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2017-09-19 23:00:46 +02:00
|
|
|
static int lo_addmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac);
|
2015-08-24 19:03:36 +02:00
|
|
|
static int lo_rmmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: lo_ifup
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX Callback: Bring up the Ethernet interface when an IP address is
|
|
|
|
* provided
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-08-24 19:03:36 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lo_ifup(FAR struct net_driver_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct lo_driver_s *priv = (FAR struct lo_driver_s *)dev->d_private;
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2016-06-12 16:31:22 +02:00
|
|
|
ninfo("Bringing up: %d.%d.%d.%d\n",
|
2020-11-23 12:18:04 +01:00
|
|
|
(int)(dev->d_ipaddr & 0xff), (int)((dev->d_ipaddr >> 8) & 0xff),
|
|
|
|
(int)((dev->d_ipaddr >> 16) & 0xff), (int)(dev->d_ipaddr >> 24));
|
2015-08-24 19:03:36 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2016-06-12 16:31:22 +02:00
|
|
|
ninfo("Bringing up: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
|
2022-01-18 08:38:00 +01:00
|
|
|
NTOHS(dev->d_ipv6addr[0]), NTOHS(dev->d_ipv6addr[1]),
|
|
|
|
NTOHS(dev->d_ipv6addr[2]), NTOHS(dev->d_ipv6addr[3]),
|
|
|
|
NTOHS(dev->d_ipv6addr[4]), NTOHS(dev->d_ipv6addr[5]),
|
|
|
|
NTOHS(dev->d_ipv6addr[6]), NTOHS(dev->d_ipv6addr[7]));
|
2015-08-24 19:03:36 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
priv->lo_bifup = true;
|
2021-12-21 09:29:22 +01:00
|
|
|
netdev_carrier_on(dev);
|
2015-08-24 19:03:36 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: lo_ifdown
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX Callback: Stop the interface.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-08-24 19:03:36 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lo_ifdown(FAR struct net_driver_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct lo_driver_s *priv = (FAR struct lo_driver_s *)dev->d_private;
|
|
|
|
|
2021-12-21 09:29:22 +01:00
|
|
|
netdev_carrier_off(dev);
|
|
|
|
|
2015-08-24 19:03:36 +02:00
|
|
|
/* Mark the device "down" */
|
|
|
|
|
|
|
|
priv->lo_bifup = false;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: lo_txavail_work
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform an out-of-cycle poll on the worker thread.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-08-24 19:03:36 +02:00
|
|
|
* arg - Reference to the NuttX driver state structure (cast to void*)
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called on the higher priority worker thread.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void lo_txavail_work(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct lo_driver_s *priv = (FAR struct lo_driver_s *)arg;
|
|
|
|
|
2015-08-25 00:08:17 +02:00
|
|
|
/* Ignore the notification if the interface is not yet up */
|
2015-08-24 19:03:36 +02:00
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2015-08-25 00:08:17 +02:00
|
|
|
if (priv->lo_bifup)
|
|
|
|
{
|
2022-11-29 19:51:05 +01:00
|
|
|
/* Reuse the devif_loopback() logic, Polling all pending events until
|
|
|
|
* return stop
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (devif_poll(&priv->lo_dev, NULL));
|
2015-08-25 00:08:17 +02:00
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-08-24 19:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: lo_txavail
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Driver callback invoked when new TX data is available. This is a
|
|
|
|
* stimulus perform an out-of-cycle poll and, thereby, reduce the TX
|
|
|
|
* latency.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-08-24 19:03:36 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called in normal user mode
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lo_txavail(FAR struct net_driver_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct lo_driver_s *priv = (FAR struct lo_driver_s *)dev->d_private;
|
|
|
|
|
|
|
|
/* Is our single work structure available? It may not be if there are
|
|
|
|
* pending interrupt actions and we will have to ignore the Tx
|
|
|
|
* availability action.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (work_available(&priv->lo_work))
|
|
|
|
{
|
|
|
|
/* Schedule to serialize the poll on the worker thread. */
|
|
|
|
|
2017-11-05 19:25:58 +01:00
|
|
|
work_queue(LPWORK, &priv->lo_work, lo_txavail_work, priv, 0);
|
2015-08-24 19:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: lo_addmac
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX Callback: Add the specified MAC address to the hardware multicast
|
|
|
|
* address filtering
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-08-24 19:03:36 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
* mac - The MAC address to be added
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2015-08-24 19:03:36 +02:00
|
|
|
static int lo_addmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac)
|
|
|
|
{
|
|
|
|
/* There is no multicast support in the loopback driver */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: lo_rmmac
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2019-02-24 18:51:25 +01:00
|
|
|
* NuttX Callback: Remove the specified MAC address from the hardware
|
|
|
|
* multicast address filtering
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-08-24 19:03:36 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
* mac - The MAC address to be removed
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2015-08-24 19:03:36 +02:00
|
|
|
static int lo_rmmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac)
|
|
|
|
{
|
|
|
|
/* There is no multicast support in the loopback driver */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: localhost_initialize
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-08-09 14:59:55 +02:00
|
|
|
* Initialize the localhost, loopback network driver
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-08-09 14:59:55 +02:00
|
|
|
* None
|
2015-08-24 19:03:36 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; Negated errno on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int localhost_initialize(void)
|
|
|
|
{
|
|
|
|
FAR struct lo_driver_s *priv;
|
|
|
|
|
|
|
|
/* Get the interface structure associated with this interface number. */
|
|
|
|
|
|
|
|
priv = &g_loopback;
|
|
|
|
|
|
|
|
/* Initialize the driver structure */
|
|
|
|
|
|
|
|
memset(priv, 0, sizeof(struct lo_driver_s));
|
2015-08-26 18:33:44 +02:00
|
|
|
priv->lo_dev.d_ifup = lo_ifup; /* I/F up (new IP address) callback */
|
|
|
|
priv->lo_dev.d_ifdown = lo_ifdown; /* I/F down callback */
|
|
|
|
priv->lo_dev.d_txavail = lo_txavail; /* New TX data callback */
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2015-08-26 18:33:44 +02:00
|
|
|
priv->lo_dev.d_addmac = lo_addmac; /* Add multicast MAC address */
|
|
|
|
priv->lo_dev.d_rmmac = lo_rmmac; /* Remove multicast MAC address */
|
|
|
|
#endif
|
2020-08-06 19:41:45 +02:00
|
|
|
priv->lo_dev.d_private = priv; /* Used to recover private state from dev */
|
2015-08-24 19:03:36 +02:00
|
|
|
|
|
|
|
/* Register the loopabck device with the OS so that socket IOCTLs can b
|
|
|
|
* performed.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
netdev_register(&priv->lo_dev, NET_LL_LOOPBACK);
|
2015-08-24 19:03:36 +02:00
|
|
|
|
|
|
|
/* Set the local loopback IP address */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2015-08-24 21:49:12 +02:00
|
|
|
net_ipv4addr_copy(priv->lo_dev.d_ipaddr, g_lo_ipv4addr);
|
|
|
|
net_ipv4addr_copy(priv->lo_dev.d_draddr, g_lo_ipv4addr);
|
|
|
|
net_ipv4addr_copy(priv->lo_dev.d_netmask, g_lo_ipv4mask);
|
2015-08-24 19:03:36 +02:00
|
|
|
#endif
|
2015-08-26 04:09:12 +02:00
|
|
|
|
2015-08-24 19:03:36 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2015-10-04 23:04:00 +02:00
|
|
|
net_ipv6addr_copy(priv->lo_dev.d_ipv6addr, g_lo_ipv6addr);
|
|
|
|
net_ipv6addr_copy(priv->lo_dev.d_ipv6draddr, g_lo_ipv6addr);
|
2017-08-09 14:59:55 +02:00
|
|
|
net_ipv6addr_copy(priv->lo_dev.d_ipv6netmask, g_lo_ipv6mask);
|
2015-08-24 19:03:36 +02:00
|
|
|
#endif
|
2015-08-26 04:09:12 +02:00
|
|
|
|
2015-08-24 19:03:36 +02:00
|
|
|
/* Put the network in the UP state */
|
|
|
|
|
2015-08-26 04:09:12 +02:00
|
|
|
priv->lo_dev.d_flags = IFF_UP;
|
2015-08-24 19:03:36 +02:00
|
|
|
return lo_ifup(&priv->lo_dev);
|
|
|
|
}
|
|
|
|
|
2020-02-02 07:15:24 +01:00
|
|
|
#endif /* CONFIG_NET_LOOPBACK */
|