2015-05-30 17:17:46 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/tcp/tcp_finddev.c
|
|
|
|
*
|
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
|
2015-05-30 17:17:46 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-05-30 17:17:46 +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.
|
2015-05-30 17:17:46 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
#include <nuttx/net/ip.h>
|
|
|
|
|
|
|
|
#include "netdev/netdev.h"
|
2017-08-07 19:50:50 +02:00
|
|
|
#include "inet/inet.h"
|
2015-05-30 17:17:46 +02:00
|
|
|
#include "tcp/tcp.h"
|
2022-11-10 07:44:17 +01:00
|
|
|
#include "utils/utils.h"
|
2015-05-30 17:17:46 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tcp_find_ipv4_device
|
2015-05-30 17:17:46 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2015-09-03 03:48:31 +02:00
|
|
|
* Select the network driver to use with the IPv4 TCP connection.
|
2015-05-30 17:17:46 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2015-09-03 03:48:31 +02:00
|
|
|
* conn - TCP connection structure.
|
|
|
|
* addr - The IPv4 address to use
|
2015-05-30 17:17:46 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success. A negated errno value is returned
|
2015-09-03 04:03:28 +02:00
|
|
|
* on failure. -ENETUNREACH is the only expected error value.
|
2015-05-30 17:17:46 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2019-12-24 11:00:54 +01:00
|
|
|
static int tcp_find_ipv4_device(FAR struct tcp_conn_s *conn,
|
|
|
|
in_addr_t addr, bool local)
|
2015-05-30 17:17:46 +02:00
|
|
|
{
|
2015-09-03 03:48:31 +02:00
|
|
|
/* Do nothing if a device is already bound to the connection */
|
|
|
|
|
|
|
|
if (conn->dev != NULL)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-05-30 17:17:46 +02:00
|
|
|
/* Return success without using device notification if the locally bound
|
|
|
|
* address is INADDR_ANY. In this case, there may be multiple devices
|
|
|
|
* that can provide data so the exceptional events from any particular
|
|
|
|
* device are not important.
|
|
|
|
*/
|
|
|
|
|
2015-09-03 03:48:31 +02:00
|
|
|
if (net_ipv4addr_cmp(addr, INADDR_ANY))
|
2015-05-30 17:17:46 +02:00
|
|
|
{
|
2022-11-10 07:44:17 +01:00
|
|
|
if (local)
|
|
|
|
{
|
2023-02-09 08:35:00 +01:00
|
|
|
#ifdef CONFIG_NET_BINDTODEVICE
|
|
|
|
if (conn->sconn.s_boundto != 0)
|
|
|
|
{
|
|
|
|
conn->dev = netdev_findbyindex(conn->sconn.s_boundto);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-11-10 07:44:17 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-11-15 07:51:02 +01:00
|
|
|
return -ECONNREFUSED;
|
2015-05-30 17:17:46 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 22:24:12 +02:00
|
|
|
/* We need to select the device that is going to route the TCP packet
|
|
|
|
* based on the provided IP address.
|
2015-05-30 17:17:46 +02:00
|
|
|
*/
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
conn->dev = netdev_findby_ripv4addr(addr, addr);
|
2015-05-30 17:17:46 +02:00
|
|
|
|
|
|
|
/* Return success if we found the device */
|
|
|
|
|
|
|
|
return conn->dev != NULL ? OK : -ENETUNREACH;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tcp_find_ipv6_device
|
2015-05-30 17:17:46 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Select the network driver to use with the IPv6 TCP transaction.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2015-09-03 03:48:31 +02:00
|
|
|
* conn - TCP connection structure.
|
|
|
|
* addr - The IPv6 address to use
|
2015-05-30 17:17:46 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success. A negated errno value is returned
|
|
|
|
* on failure. -ENETUNREACH is the only expected error value.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2015-09-03 15:33:50 +02:00
|
|
|
static int tcp_find_ipv6_device(FAR struct tcp_conn_s *conn,
|
2019-12-24 11:00:54 +01:00
|
|
|
const net_ipv6addr_t addr, bool local)
|
2015-05-30 17:17:46 +02:00
|
|
|
{
|
2015-09-03 03:48:31 +02:00
|
|
|
/* Do nothing if a device is already bound to the connection */
|
|
|
|
|
|
|
|
if (conn->dev != NULL)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-05-30 17:17:46 +02:00
|
|
|
/* Return success without using device notification if the locally bound
|
2018-06-23 20:53:27 +02:00
|
|
|
* address is the IPv6 unspecified address. In this case, there may be
|
|
|
|
* multiple devices* that can provide data so the exceptional events from
|
|
|
|
* any particular device are not important.
|
2015-05-30 17:17:46 +02:00
|
|
|
*/
|
|
|
|
|
2018-06-23 20:53:27 +02:00
|
|
|
if (net_ipv6addr_cmp(addr, g_ipv6_unspecaddr))
|
2015-05-30 17:17:46 +02:00
|
|
|
{
|
2022-11-10 07:44:17 +01:00
|
|
|
if (local)
|
|
|
|
{
|
2023-02-09 08:35:00 +01:00
|
|
|
#ifdef CONFIG_NET_BINDTODEVICE
|
|
|
|
if (conn->sconn.s_boundto != 0)
|
|
|
|
{
|
|
|
|
conn->dev = netdev_findbyindex(conn->sconn.s_boundto);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-11-10 07:44:17 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-11-15 07:51:02 +01:00
|
|
|
return -ECONNREFUSED;
|
2015-05-30 17:17:46 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 22:24:12 +02:00
|
|
|
/* We need to select the device that is going to route the TCP packet
|
|
|
|
* based on the provided IP address.
|
2015-05-30 17:17:46 +02:00
|
|
|
*/
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
conn->dev = netdev_findby_ripv6addr(addr, addr);
|
2015-05-30 17:17:46 +02:00
|
|
|
|
|
|
|
/* Return success if we found the device */
|
|
|
|
|
|
|
|
return conn->dev != NULL ? OK : -ENETUNREACH;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
2015-09-03 03:48:31 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tcp_local_ipv4_device
|
2015-09-03 03:48:31 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Select the network driver to use with the IPv4 TCP transaction based
|
2020-02-14 15:50:45 +01:00
|
|
|
* on the locally bound IPv4 address.
|
2015-09-03 03:48:31 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - TCP connection structure. The locally bound address, laddr,
|
|
|
|
* should be set to a non-zero value in this structure.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success. A negated errno value is returned
|
2015-09-03 04:03:28 +02:00
|
|
|
* on failure. -ENETUNREACH is the only expected error value.
|
2015-09-03 03:48:31 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
int tcp_local_ipv4_device(FAR struct tcp_conn_s *conn)
|
|
|
|
{
|
2019-12-24 11:00:54 +01:00
|
|
|
return tcp_find_ipv4_device(conn, conn->u.ipv4.laddr, true);
|
2015-09-03 03:48:31 +02:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tcp_remote_ipv4_device
|
2015-09-03 03:48:31 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Select the network driver to use with the IPv4 TCP transaction based
|
2020-02-14 15:50:45 +01:00
|
|
|
* on the remotely connected IPv4 address.
|
2015-09-03 03:48:31 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2020-02-14 15:50:45 +01:00
|
|
|
* conn - TCP connection structure. The remotely connected address, raddr,
|
2015-09-03 03:48:31 +02:00
|
|
|
* should be set to a non-zero value in this structure.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success. A negated errno value is returned
|
2015-09-03 04:03:28 +02:00
|
|
|
* on failure. -ENETUNREACH is the only expected error value.
|
2015-09-03 03:48:31 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
int tcp_remote_ipv4_device(FAR struct tcp_conn_s *conn)
|
|
|
|
{
|
2019-12-24 11:00:54 +01:00
|
|
|
return tcp_find_ipv4_device(conn, conn->u.ipv4.raddr, false);
|
2015-09-03 03:48:31 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tcp_local_ipv6_device
|
2015-09-03 03:48:31 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Select the network driver to use with the IPv6 TCP transaction.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - TCP connection structure. The locally bound address, laddr,
|
|
|
|
* should be set to a non-zero value in this structure.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success. A negated errno value is returned
|
|
|
|
* on failure. -ENETUNREACH is the only expected error value.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
int tcp_local_ipv6_device(FAR struct tcp_conn_s *conn)
|
|
|
|
{
|
2019-12-24 11:00:54 +01:00
|
|
|
return tcp_find_ipv6_device(conn, conn->u.ipv6.laddr, true);
|
2015-09-03 03:48:31 +02:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tcp_remote_ipv6_device
|
2015-09-03 03:48:31 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Select the network driver to use with the IPv6 TCP transaction based
|
2020-02-14 15:50:45 +01:00
|
|
|
* on the remotely connected IPv6 address.
|
2015-09-03 03:48:31 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - TCP connection structure. The remotely connected address, raddr,
|
|
|
|
* should be set to a non-zero value in this structure.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success. A negated errno value is returned
|
|
|
|
* on failure. -EHOSTUNREACH is the only expected error value.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
int tcp_remote_ipv6_device(FAR struct tcp_conn_s *conn)
|
|
|
|
{
|
2019-12-24 11:00:54 +01:00
|
|
|
return tcp_find_ipv6_device(conn, conn->u.ipv6.raddr, false);
|
2015-09-03 03:48:31 +02:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
2015-05-30 17:17:46 +02:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_TCP */
|