2017-10-24 19:23:08 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* net/icmpv6/icmpv6_conn.c
|
2017-10-24 19:23:08 +02:00
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-19 12:45:37 +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
|
2017-10-24 19:23:08 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-10-24 19:23:08 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +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.
|
2017-10-24 19:23:08 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arch/irq.h>
|
|
|
|
|
2022-01-04 04:53:15 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2017-10-24 19:23:08 +02:00
|
|
|
#include <nuttx/net/netconfig.h>
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
|
|
|
|
#include "devif/devif.h"
|
|
|
|
#include "icmpv6/icmpv6.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ICMPv6_SOCKET
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* The array containing all IPPROTO_ICMP socket connections */
|
|
|
|
|
2023-02-11 14:09:01 +01:00
|
|
|
#if CONFIG_NET_ICMPv6_PREALLOC_CONNS > 0
|
|
|
|
static struct icmpv6_conn_s
|
|
|
|
g_icmpv6_connections[CONFIG_NET_ICMPv6_PREALLOC_CONNS];
|
2022-01-04 04:53:15 +01:00
|
|
|
#endif
|
2017-10-24 19:23:08 +02:00
|
|
|
|
|
|
|
/* A list of all free IPPROTO_ICMP socket connections */
|
|
|
|
|
|
|
|
static dq_queue_t g_free_icmpv6_connections;
|
2022-09-06 08:18:45 +02:00
|
|
|
static mutex_t g_free_lock = NXMUTEX_INITIALIZER;
|
2017-10-24 19:23:08 +02:00
|
|
|
|
|
|
|
/* A list of all allocated IPPROTO_ICMP socket connections */
|
|
|
|
|
|
|
|
static dq_queue_t g_active_icmpv6_connections;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: icmpv6_sock_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the IPPROTO_ICMP socket connection structures. Called once
|
|
|
|
* and only from the network initialization layer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void icmpv6_sock_initialize(void)
|
|
|
|
{
|
2023-02-11 14:09:01 +01:00
|
|
|
#if CONFIG_NET_ICMPv6_PREALLOC_CONNS > 0
|
2017-10-24 19:23:08 +02:00
|
|
|
int i;
|
|
|
|
|
2023-02-11 14:09:01 +01:00
|
|
|
for (i = 0; i < CONFIG_NET_ICMPv6_PREALLOC_CONNS; i++)
|
2017-10-24 19:23:08 +02:00
|
|
|
{
|
|
|
|
/* Move the connection structure to the free list */
|
|
|
|
|
2022-02-07 04:37:39 +01:00
|
|
|
dq_addlast(&g_icmpv6_connections[i].sconn.node,
|
|
|
|
&g_free_icmpv6_connections);
|
2017-10-24 19:23:08 +02:00
|
|
|
}
|
2022-01-04 04:53:15 +01:00
|
|
|
#endif
|
2017-10-24 19:23:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: icmpv6_alloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a new, uninitialized IPPROTO_ICMP socket connection structure.
|
|
|
|
* This is normally something done by the implementation of the socket()
|
|
|
|
* interface.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct icmpv6_conn_s *icmpv6_alloc(void)
|
|
|
|
{
|
|
|
|
FAR struct icmpv6_conn_s *conn = NULL;
|
|
|
|
int ret;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* The free list is protected by a mutex. */
|
2017-10-24 19:23:08 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_free_lock);
|
2017-10-24 19:23:08 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2023-02-11 14:09:01 +01:00
|
|
|
#if CONFIG_NET_ICMPv6_ALLOC_CONNS > 0
|
2022-01-04 04:53:15 +01:00
|
|
|
if (dq_peek(&g_active_icmpv6_connections) == NULL)
|
|
|
|
{
|
2023-02-11 14:09:01 +01:00
|
|
|
#if CONFIG_NET_ICMPv6_MAX_CONNS > 0
|
|
|
|
if (dq_count(&g_active_icmpv6_connections) +
|
2024-04-06 15:07:06 +02:00
|
|
|
CONFIG_NET_ICMPv6_ALLOC_CONNS > CONFIG_NET_ICMPv6_MAX_CONNS)
|
2023-02-11 14:09:01 +01:00
|
|
|
{
|
|
|
|
nxmutex_unlock(&g_free_lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
conn = kmm_zalloc(sizeof(*conn) * CONFIG_NET_ICMPv6_ALLOC_CONNS);
|
2022-01-04 04:53:15 +01:00
|
|
|
if (conn != NULL)
|
|
|
|
{
|
2023-02-11 14:09:01 +01:00
|
|
|
for (ret = 0; ret < CONFIG_NET_ICMPv6_ALLOC_CONNS; ret++)
|
2022-01-04 04:53:15 +01:00
|
|
|
{
|
2022-02-07 04:37:39 +01:00
|
|
|
dq_addlast(&conn[ret].sconn.node,
|
|
|
|
&g_free_icmpv6_connections);
|
2022-01-04 04:53:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-02-19 15:01:46 +01:00
|
|
|
conn = (FAR struct icmpv6_conn_s *)
|
|
|
|
dq_remfirst(&g_free_icmpv6_connections);
|
2017-10-24 19:23:08 +02:00
|
|
|
if (conn != NULL)
|
|
|
|
{
|
|
|
|
/* Enqueue the connection into the active list */
|
|
|
|
|
2022-02-07 04:37:39 +01:00
|
|
|
dq_addlast(&conn->sconn.node, &g_active_icmpv6_connections);
|
2017-10-24 19:23:08 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_free_lock);
|
2017-10-24 19:23:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: icmpv6_free
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a IPPROTO_ICMP socket connection structure that is no longer in
|
|
|
|
* use. This should be done by the implementation of close().
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void icmpv6_free(FAR struct icmpv6_conn_s *conn)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
/* The free list is protected by a mutex. */
|
2017-10-24 19:23:08 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(conn->crefs == 0);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Take the mutex (perhaps waiting) */
|
2017-10-24 19:23:08 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&g_free_lock);
|
2017-10-24 19:23:08 +02:00
|
|
|
|
2018-11-09 22:22:25 +01:00
|
|
|
/* Remove the connection from the active list */
|
2017-10-24 19:23:08 +02:00
|
|
|
|
2022-02-07 04:37:39 +01:00
|
|
|
dq_rem(&conn->sconn.node, &g_active_icmpv6_connections);
|
2017-10-24 19:23:08 +02:00
|
|
|
|
2023-02-11 14:09:01 +01:00
|
|
|
/* If this is a preallocated or a batch allocated connection store it in
|
|
|
|
* the free connections list. Else free it.
|
|
|
|
*/
|
2023-02-28 11:31:21 +01:00
|
|
|
|
2023-02-11 14:09:01 +01:00
|
|
|
#if CONFIG_NET_ICMPv6_ALLOC_CONNS == 1
|
|
|
|
if (conn < g_icmpv6_connections || conn >= (g_icmpv6_connections +
|
|
|
|
CONFIG_NET_ICMPv6_PREALLOC_CONNS))
|
|
|
|
{
|
|
|
|
kmm_free(conn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2023-02-28 11:31:21 +01:00
|
|
|
memset(conn, 0, sizeof(*conn));
|
2023-02-11 14:09:01 +01:00
|
|
|
dq_addlast(&conn->sconn.node, &g_free_icmpv6_connections);
|
|
|
|
}
|
2017-10-24 19:23:08 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_free_lock);
|
2017-10-24 19:23:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: icmpv6_active()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a connection structure that is the appropriate connection to be
|
|
|
|
* used with the provided ECHO request ID.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from network logic at with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct icmpv6_conn_s *icmpv6_active(uint16_t id)
|
|
|
|
{
|
|
|
|
FAR struct icmpv6_conn_s *conn =
|
|
|
|
(FAR struct icmpv6_conn_s *)g_active_icmpv6_connections.head;
|
|
|
|
|
|
|
|
while (conn != NULL)
|
|
|
|
{
|
|
|
|
/* FIXME lmac in conn should have been set by icmpv6_bind() */
|
|
|
|
|
|
|
|
if (id == conn->id)
|
|
|
|
{
|
|
|
|
/* Matching connection found.. return a reference to it */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look at the next active connection */
|
|
|
|
|
2022-02-07 04:37:39 +01:00
|
|
|
conn = (FAR struct icmpv6_conn_s *)conn->sconn.node.flink;
|
2017-10-24 19:23:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: icmpv6_nextconn
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Traverse the list of allocated packet connections
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from network logic at with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct icmpv6_conn_s *icmpv6_nextconn(FAR struct icmpv6_conn_s *conn)
|
|
|
|
{
|
|
|
|
if (conn == NULL)
|
|
|
|
{
|
|
|
|
return (FAR struct icmpv6_conn_s *)g_active_icmpv6_connections.head;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-07 04:37:39 +01:00
|
|
|
return (FAR struct icmpv6_conn_s *)conn->sconn.node.flink;
|
2017-10-24 19:23:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-04-23 05:25:28 +02:00
|
|
|
* Name: icmpv6_foreach
|
2017-10-24 19:23:08 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2023-04-23 05:25:28 +02:00
|
|
|
* Enumerate each ICMPv6 connection structure. This function will terminate
|
|
|
|
* when either (1) all connection have been enumerated or (2) when a
|
|
|
|
* callback returns any non-zero value.
|
2017-10-24 19:23:08 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from network logic at with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-23 05:25:28 +02:00
|
|
|
int icmpv6_foreach(icmpv6_callback_t callback, FAR void *arg)
|
2017-10-24 19:23:08 +02:00
|
|
|
{
|
|
|
|
FAR struct icmpv6_conn_s *conn;
|
2023-04-23 05:25:28 +02:00
|
|
|
int ret = 0;
|
2017-10-24 19:23:08 +02:00
|
|
|
|
2023-04-23 05:25:28 +02:00
|
|
|
if (callback != NULL)
|
2017-10-24 19:23:08 +02:00
|
|
|
{
|
2023-04-23 05:25:28 +02:00
|
|
|
for (conn = icmpv6_nextconn(NULL); conn != NULL;
|
|
|
|
conn = icmpv6_nextconn(conn))
|
2017-10-24 19:23:08 +02:00
|
|
|
{
|
2023-04-23 05:25:28 +02:00
|
|
|
ret = callback(conn, arg);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-10-24 19:23:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 05:25:28 +02:00
|
|
|
return ret;
|
2017-10-24 19:23:08 +02:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_ICMP */
|