2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
2014-06-18 18:18:53 +02:00
|
|
|
* net/tcp/tcp_timer.c
|
2007-11-07 19:54:35 +01:00
|
|
|
* Poll for the availability of TCP TX data
|
|
|
|
*
|
2020-01-11 16:40:32 +01:00
|
|
|
* Copyright (C) 2007-2010, 2015-2016, 2018, 2020 Gregory Nutt. All rights
|
2018-03-12 17:59:46 +01:00
|
|
|
* reserved.
|
2012-09-13 20:32:24 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* Adapted for NuttX from logic in uIP which also has a BSD-like license:
|
|
|
|
*
|
|
|
|
* Original author Adam Dunkels <adam@dunkels.com>
|
|
|
|
* Copyright () 2001-2003, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2007-11-22 19:36:46 +01:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2009-12-15 15:53:45 +01:00
|
|
|
#include <stdint.h>
|
2017-04-03 01:46:22 +02:00
|
|
|
#include <assert.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
#include <debug.h>
|
2022-04-25 17:08:24 +02:00
|
|
|
#include <time.h>
|
|
|
|
#include <stdlib.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-06-24 16:53:28 +02:00
|
|
|
#include <nuttx/net/netconfig.h>
|
2018-03-12 17:59:46 +01:00
|
|
|
#include <nuttx/net/net.h>
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2014-06-26 17:32:39 +02:00
|
|
|
#include <nuttx/net/netstats.h>
|
2014-07-06 20:34:27 +02:00
|
|
|
#include <nuttx/net/tcp.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2022-06-05 13:30:49 +02:00
|
|
|
#include "netdev/netdev.h"
|
2014-06-29 02:07:02 +02:00
|
|
|
#include "devif/devif.h"
|
2018-03-12 17:59:46 +01:00
|
|
|
#include "socket/socket.h"
|
2014-06-25 02:12:49 +02:00
|
|
|
#include "tcp/tcp.h"
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Per RFC 1122: "... an ACK should not be excessively delayed; in
|
|
|
|
* particular, the delay MUST be less than 0.5 seconds ..."
|
|
|
|
*
|
|
|
|
* NOTE: We only have 0.5 timing resolution here so the delay will be
|
|
|
|
* between 0.5 and 1.0 seconds, and may be delayed further, depending on the
|
|
|
|
* polling rate of the the driver (often 1 second).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ACK_DELAY (1)
|
|
|
|
|
2022-04-25 17:08:24 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_get_timeout
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Gets the time of the next timeout
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - The TCP "connection" to poll for TX data
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* int - The time required for the next expiry (units: half-seconds)
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* conn is not NULL.
|
|
|
|
* The connection (conn) is bound to the polling device (dev).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tcp_get_timeout(FAR struct tcp_conn_s *conn)
|
|
|
|
{
|
|
|
|
int timeout = conn->timer;
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCP_KEEPALIVE
|
|
|
|
if (timeout == 0)
|
|
|
|
{
|
|
|
|
timeout = conn->keeptimer;
|
|
|
|
}
|
|
|
|
else if (conn->keeptimer > 0 && timeout > conn->keeptimer)
|
|
|
|
{
|
|
|
|
timeout = conn->keeptimer;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_timer_expiry
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle a TCP timer expiration for the provided TCP connection
|
|
|
|
* Restart a TCP timer if need to
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - The TCP "connection" to poll for TX data
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* arg is not NULL.
|
|
|
|
* The connection (arg) is bound to the polling device (dev).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void tcp_timer_expiry(FAR void *arg)
|
|
|
|
{
|
2022-06-05 17:43:17 +02:00
|
|
|
FAR struct tcp_conn_s *conn = NULL;
|
2022-04-25 17:08:24 +02:00
|
|
|
|
2022-06-05 13:30:49 +02:00
|
|
|
net_lock();
|
2022-06-05 17:43:17 +02:00
|
|
|
|
|
|
|
while ((conn = tcp_nextconn(conn)) != NULL)
|
|
|
|
{
|
|
|
|
if (conn == arg)
|
|
|
|
{
|
|
|
|
conn->timeout = true;
|
|
|
|
netdev_txnotify_dev(conn->dev);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 13:30:49 +02:00
|
|
|
net_unlock();
|
2022-04-25 17:08:24 +02:00
|
|
|
}
|
|
|
|
|
2023-01-16 04:57:03 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-25 17:08:24 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_update_timer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Update the TCP timer for the provided TCP connection,
|
|
|
|
* The timeout is accurate
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - The TCP "connection" to poll for TX data
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* conn is not NULL.
|
|
|
|
* The connection (conn) is bound to the polling device (dev).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-01-16 04:57:03 +01:00
|
|
|
void tcp_update_timer(FAR struct tcp_conn_s *conn)
|
2022-04-25 17:08:24 +02:00
|
|
|
{
|
|
|
|
int timeout = tcp_get_timeout(conn);
|
|
|
|
|
|
|
|
if (timeout > 0)
|
|
|
|
{
|
2023-01-16 04:57:03 +01:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
|
|
|
/* Re-update tcp timeout */
|
|
|
|
|
|
|
|
if (conn->ltimeout != 0)
|
|
|
|
{
|
|
|
|
sclock_t ticks = conn->ltimeout - clock_systime_ticks();
|
|
|
|
|
|
|
|
if (ticks <= 0)
|
|
|
|
{
|
|
|
|
timeout = 0;
|
|
|
|
}
|
|
|
|
else if (timeout > TICK2HSEC(ticks))
|
|
|
|
{
|
|
|
|
timeout = TICK2HSEC(ticks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (work_available(&conn->work) ||
|
|
|
|
TICK2HSEC(work_timeleft(&conn->work)) != timeout)
|
2022-04-25 17:08:24 +02:00
|
|
|
{
|
|
|
|
work_queue(LPWORK, &conn->work, tcp_timer_expiry,
|
|
|
|
conn, HSEC2TICK(timeout));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
work_cancel(LPWORK, &conn->work);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_update_retrantimer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Update the retransmit TCP timer for the provided TCP connection,
|
|
|
|
* The timeout is accurate
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - The TCP "connection" to poll for TX data
|
|
|
|
* timeout - Time for the next timeout
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* conn is not NULL.
|
|
|
|
* The connection (conn) is bound to the polling device (dev).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void tcp_update_retrantimer(FAR struct tcp_conn_s *conn, int timeout)
|
|
|
|
{
|
|
|
|
conn->timer = timeout;
|
|
|
|
tcp_update_timer(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_update_keeptimer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Update the keeplive TCP timer for the provided TCP connection,
|
|
|
|
* The timeout is accurate
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - The TCP "connection" to poll for TX data
|
|
|
|
* timeout - Time for the next timeout
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* conn is not NULL.
|
|
|
|
* The connection (conn) is bound to the polling device (dev).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCP_KEEPALIVE
|
|
|
|
void tcp_update_keeptimer(FAR struct tcp_conn_s *conn, int timeout)
|
|
|
|
{
|
|
|
|
conn->keeptimer = timeout;
|
|
|
|
tcp_update_timer(conn);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_stop_timer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Stop TCP timer for the provided TCP connection
|
|
|
|
* When the connection is closed
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - The TCP "connection" to poll for TX data
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* conn is not NULL.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void tcp_stop_timer(FAR struct tcp_conn_s *conn)
|
|
|
|
{
|
|
|
|
work_cancel(LPWORK, &conn->work);
|
|
|
|
}
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
2014-06-25 02:12:49 +02:00
|
|
|
* Name: tcp_timer
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle a TCP timer expiration for the provided TCP connection
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2014-04-12 20:13:01 +02:00
|
|
|
* dev - The device driver structure to use in the send operation
|
|
|
|
* conn - The TCP "connection" to poll for TX data
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-11-07 19:54:35 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2017-08-29 23:08:38 +02:00
|
|
|
* The network is locked.
|
2021-10-02 02:47:08 +02:00
|
|
|
* dev is not NULL.
|
|
|
|
* conn is not NULL.
|
|
|
|
* The connection (conn) is bound to the polling device (dev).
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-25 17:08:24 +02:00
|
|
|
void tcp_timer(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2022-04-25 17:08:24 +02:00
|
|
|
int hsec = tcp_get_timeout(conn);
|
2015-05-28 01:17:42 +02:00
|
|
|
uint16_t result;
|
2015-01-18 00:48:15 +01:00
|
|
|
uint8_t hdrlen;
|
2007-11-08 17:01:18 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
/* NOTE: It is important to decrease conn->timer at "hsec" pace,
|
|
|
|
* not faster. Excessive (false) decrements of conn->timer are not allowed
|
|
|
|
* here. Otherwise, it breaks TCP timings and leads to TCP spurious
|
|
|
|
* retransmissions and other issues due to premature timeouts.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(dev != NULL && conn != NULL && dev == conn->dev);
|
|
|
|
|
2015-01-18 00:48:15 +01:00
|
|
|
/* Set up for the callback. We can't know in advance if the application
|
|
|
|
* is going to send a IPv4 or an IPv6 packet, so this setup may not
|
|
|
|
* actually be used. Furthermore, the TCP logic is required to call
|
|
|
|
* tcp_ipv4_select() or tcp_ipv6_select() prior to sending any packets.
|
|
|
|
* We will try to set the correct value here basic on the binding of
|
|
|
|
* the connection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (conn->domain == PF_INET)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
hdrlen = IPv4TCP_HDRLEN;
|
|
|
|
tcp_ipv4_select(dev);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
hdrlen = IPv6TCP_HDRLEN;
|
|
|
|
tcp_ipv6_select(dev);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/* Increase the TCP sequence number */
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_nextsequence();
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/* Reset the length variables. */
|
|
|
|
|
2007-11-07 23:54:51 +01:00
|
|
|
dev->d_len = 0;
|
2007-11-07 19:54:35 +01:00
|
|
|
dev->d_sndlen = 0;
|
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
if (conn->tcpstateflags == TCP_CLOSED)
|
|
|
|
{
|
|
|
|
/* Nothing to be done */
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-16 04:57:03 +01:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
|
|
|
/* Send reset immediately if linger timeout */
|
|
|
|
|
|
|
|
if (conn->ltimeout != 0 &&
|
|
|
|
((sclock_t)(conn->ltimeout - clock_systime_ticks()) <= 0))
|
|
|
|
{
|
|
|
|
conn->tcpstateflags = TCP_CLOSED;
|
|
|
|
ninfo("TCP state: TCP_CLOSED\n");
|
|
|
|
|
|
|
|
/* We call tcp_callback() with TCP_TIMEDOUT to
|
|
|
|
* inform the application that the connection has
|
|
|
|
* timed out.
|
|
|
|
*/
|
|
|
|
|
|
|
|
tcp_callback(dev, conn, TCP_TIMEDOUT);
|
|
|
|
|
|
|
|
/* We also send a reset packet to the remote host. */
|
|
|
|
|
|
|
|
tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen);
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/* Check if the connection is in a state in which we simply wait
|
|
|
|
* for the connection to time out. If so, we increase the
|
|
|
|
* connection's timer and remove the connection if it times
|
|
|
|
* out.
|
|
|
|
*/
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
if (conn->tcpstateflags == TCP_TIME_WAIT ||
|
|
|
|
conn->tcpstateflags == TCP_FIN_WAIT_2)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2016-03-20 15:19:00 +01:00
|
|
|
/* Check if the timer exceeds the timeout value */
|
|
|
|
|
2022-05-17 12:23:00 +02:00
|
|
|
if (conn->timer <= hsec)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2022-05-17 12:23:00 +02:00
|
|
|
/* Set the timer to zero value */
|
2016-03-20 15:19:00 +01:00
|
|
|
|
2022-05-17 12:23:00 +02:00
|
|
|
conn->timer = 0;
|
2021-10-02 02:47:08 +02:00
|
|
|
conn->tcpstateflags = TCP_CLOSED;
|
2016-03-20 15:19:00 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
/* Notify upper layers about the timeout */
|
2014-06-02 15:39:29 +02:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
tcp_callback(dev, conn, TCP_TIMEDOUT);
|
2014-06-02 15:39:29 +02:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
ninfo("TCP state: TCP_CLOSED\n");
|
2016-03-20 15:19:00 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-05-17 12:23:00 +02:00
|
|
|
/* No timeout. Just update the decremented timer */
|
2014-06-02 15:39:29 +02:00
|
|
|
|
2022-05-17 12:23:00 +02:00
|
|
|
conn->timer -= hsec;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
2014-07-07 00:10:26 +02:00
|
|
|
else if (conn->tcpstateflags != TCP_CLOSED)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2007-11-07 23:54:51 +01:00
|
|
|
/* If the connection has outstanding data, we increase the connection's
|
|
|
|
* timer and see if it has reached the RTO value in which case we
|
|
|
|
* retransmit.
|
2007-11-07 19:54:35 +01:00
|
|
|
*/
|
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
if (conn->tx_unacked > 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2007-11-07 23:54:51 +01:00
|
|
|
/* The connection has outstanding data */
|
|
|
|
|
|
|
|
if (conn->timer > hsec)
|
|
|
|
{
|
|
|
|
/* Will not yet decrement to zero */
|
|
|
|
|
|
|
|
conn->timer -= hsec;
|
|
|
|
}
|
|
|
|
else
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2007-11-07 23:54:51 +01:00
|
|
|
/* Will decrement to zero */
|
|
|
|
|
|
|
|
conn->timer = 0;
|
|
|
|
|
2017-05-14 18:56:25 +02:00
|
|
|
/* Check for a timeout on connection in the TCP_SYN_RCVD state.
|
|
|
|
* On such timeouts, we would normally resend the SYNACK until
|
2017-05-14 20:14:31 +02:00
|
|
|
* the ACK is received, completing the 3-way handshake. But if
|
2017-05-14 18:56:25 +02:00
|
|
|
* the retry count elapsed, then we must assume that no ACK is
|
|
|
|
* forthcoming and terminate the attempted connection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (conn->tcpstateflags == TCP_SYN_RCVD &&
|
|
|
|
conn->nrtx >= TCP_MAXSYNRTX)
|
|
|
|
{
|
|
|
|
FAR struct tcp_conn_s *listener;
|
|
|
|
|
|
|
|
conn->tcpstateflags = TCP_CLOSED;
|
2017-05-15 16:10:43 +02:00
|
|
|
ninfo("TCP state: TCP_SYN_RCVD->TCP_CLOSED\n");
|
2017-05-14 18:56:25 +02:00
|
|
|
|
2017-05-14 20:14:31 +02:00
|
|
|
/* Find the listener for this connection. */
|
2017-05-14 18:56:25 +02:00
|
|
|
|
2017-05-25 09:48:55 +02:00
|
|
|
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
|
2021-09-23 08:55:19 +02:00
|
|
|
listener = tcp_findlistener(&conn->u, conn->lport,
|
|
|
|
conn->domain);
|
2017-05-25 09:48:55 +02:00
|
|
|
#else
|
2021-09-23 08:55:19 +02:00
|
|
|
listener = tcp_findlistener(&conn->u, conn->lport);
|
2017-05-25 09:48:55 +02:00
|
|
|
#endif
|
2017-05-14 18:56:25 +02:00
|
|
|
if (listener != NULL)
|
|
|
|
{
|
|
|
|
/* We call tcp_callback() for the connection with
|
|
|
|
* TCP_TIMEDOUT to inform the listener that the
|
|
|
|
* connection has timed out.
|
|
|
|
*/
|
|
|
|
|
2020-08-04 03:09:33 +02:00
|
|
|
tcp_callback(dev, listener, TCP_TIMEDOUT);
|
2017-05-14 18:56:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We also send a reset packet to the remote host. */
|
|
|
|
|
|
|
|
tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen);
|
|
|
|
|
|
|
|
/* Finally, we must free this TCP connection structure */
|
|
|
|
|
2021-01-28 12:47:14 +01:00
|
|
|
conn->crefs = 0;
|
2017-05-14 18:56:25 +02:00
|
|
|
tcp_free(conn);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, check for a timeout on an established connection.
|
|
|
|
* If the retry count is exceeded in this case, we should
|
|
|
|
* close the connection.
|
|
|
|
*/
|
2007-11-07 23:54:51 +01:00
|
|
|
|
2017-05-14 18:56:25 +02:00
|
|
|
else if (
|
2014-01-14 15:30:35 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
2022-01-16 05:40:08 +01:00
|
|
|
# ifdef CONFIG_NET_SENDFILE
|
|
|
|
(!conn->sendfile && conn->expired > 0) ||
|
|
|
|
(conn->sendfile && conn->nrtx >= TCP_MAXRTX) ||
|
|
|
|
# else
|
2014-01-14 15:30:35 +01:00
|
|
|
conn->expired > 0 ||
|
2022-01-16 05:40:08 +01:00
|
|
|
# endif
|
2014-01-14 15:30:35 +01:00
|
|
|
#else
|
2017-05-14 18:56:25 +02:00
|
|
|
conn->nrtx >= TCP_MAXRTX ||
|
2014-01-14 15:30:35 +01:00
|
|
|
#endif
|
2017-05-14 18:56:25 +02:00
|
|
|
(conn->tcpstateflags == TCP_SYN_SENT &&
|
|
|
|
conn->nrtx >= TCP_MAXSYNRTX)
|
2014-01-14 15:30:35 +01:00
|
|
|
)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-07-07 00:10:26 +02:00
|
|
|
conn->tcpstateflags = TCP_CLOSED;
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("TCP state: TCP_CLOSED\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
/* We call tcp_callback() with TCP_TIMEDOUT to
|
2007-11-08 17:01:18 +01:00
|
|
|
* inform the application that the connection has
|
|
|
|
* timed out.
|
2007-11-07 19:54:35 +01:00
|
|
|
*/
|
|
|
|
|
2020-08-04 03:09:33 +02:00
|
|
|
tcp_callback(dev, conn, TCP_TIMEDOUT);
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/* We also send a reset packet to the remote host. */
|
|
|
|
|
2015-01-18 00:48:15 +01:00
|
|
|
tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen);
|
2007-11-07 19:54:35 +01:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
/* Exponential backoff. */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-07-07 00:19:26 +02:00
|
|
|
conn->timer = TCP_RTO << (conn->nrtx > 4 ? 4: conn->nrtx);
|
2022-05-17 12:23:00 +02:00
|
|
|
conn->nrtx++;
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/* Ok, so we need to retransmit. We do this differently
|
|
|
|
* depending on which state we are in. In ESTABLISHED, we
|
|
|
|
* call upon the application so that it may prepare the
|
|
|
|
* data for the retransmit. In SYN_RCVD, we resend the
|
|
|
|
* SYNACK that we sent earlier and in LAST_ACK we have to
|
|
|
|
* retransmit our FINACK.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.tcp.rexmit++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
2014-07-07 00:10:26 +02:00
|
|
|
switch (conn->tcpstateflags & TCP_STATE_MASK)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-07-07 00:10:26 +02:00
|
|
|
case TCP_SYN_RCVD:
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/* In the SYN_RCVD state, we should retransmit our
|
|
|
|
* SYNACK.
|
|
|
|
*/
|
|
|
|
|
2022-01-02 16:53:43 +01:00
|
|
|
#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS)
|
|
|
|
tcp_setsequence(conn->sndseq, conn->rexmit_seq);
|
|
|
|
#else
|
|
|
|
/* REVISIT for the buffered mode */
|
|
|
|
#endif
|
2019-12-08 20:13:51 +01:00
|
|
|
tcp_synack(dev, conn, TCP_ACK | TCP_SYN);
|
2007-11-07 19:54:35 +01:00
|
|
|
goto done;
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
case TCP_SYN_SENT:
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/* In the SYN_SENT state, we retransmit out SYN. */
|
|
|
|
|
2022-01-02 16:53:43 +01:00
|
|
|
#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS)
|
|
|
|
tcp_setsequence(conn->sndseq, conn->rexmit_seq);
|
|
|
|
#else
|
|
|
|
/* REVISIT for the buffered mode */
|
|
|
|
#endif
|
2019-12-08 20:13:51 +01:00
|
|
|
tcp_synack(dev, conn, TCP_SYN);
|
2007-11-07 19:54:35 +01:00
|
|
|
goto done;
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
case TCP_ESTABLISHED:
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2020-11-16 14:57:56 +01:00
|
|
|
/* In the ESTABLISHED state, we call upon the application
|
|
|
|
* to do the actual retransmit after which we jump into
|
|
|
|
* the code for sending out the packet.
|
2007-11-07 19:54:35 +01:00
|
|
|
*/
|
|
|
|
|
2020-11-16 14:57:56 +01:00
|
|
|
result = tcp_callback(dev, conn, TCP_REXMIT);
|
|
|
|
tcp_rexmit(dev, conn, result);
|
2007-11-07 19:54:35 +01:00
|
|
|
goto done;
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
case TCP_FIN_WAIT_1:
|
|
|
|
case TCP_CLOSING:
|
|
|
|
case TCP_LAST_ACK:
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/* In all these states we should retransmit a FINACK. */
|
|
|
|
|
2022-01-02 16:53:43 +01:00
|
|
|
#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS)
|
|
|
|
tcp_setsequence(conn->sndseq, conn->rexmit_seq);
|
|
|
|
#else
|
|
|
|
/* REVISIT for the buffered mode */
|
|
|
|
#endif
|
2015-01-18 00:48:15 +01:00
|
|
|
tcp_send(dev, conn, TCP_FIN | TCP_ACK, hdrlen);
|
2007-11-07 19:54:35 +01:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-07 23:54:51 +01:00
|
|
|
|
2016-03-20 15:19:00 +01:00
|
|
|
/* The connection does not have outstanding data. Check if the TCP
|
|
|
|
* connection has been established.
|
|
|
|
*/
|
2007-11-07 23:54:51 +01:00
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
else if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_ESTABLISHED)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2021-10-02 02:47:08 +02:00
|
|
|
#ifdef CONFIG_NET_TCP_KEEPALIVE
|
|
|
|
/* Is this an established connected with KeepAlive enabled? */
|
2016-03-20 15:19:00 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
if (conn->keepalive)
|
2016-03-20 15:19:00 +01:00
|
|
|
{
|
2021-10-02 02:47:08 +02:00
|
|
|
uint32_t saveseq;
|
2018-03-12 17:59:46 +01:00
|
|
|
|
2022-05-18 07:32:56 +02:00
|
|
|
/* Yes... has the idle period elapsed with no data or ACK
|
|
|
|
* received from the remote peer?
|
2021-10-02 02:47:08 +02:00
|
|
|
*/
|
|
|
|
|
2022-05-18 07:32:56 +02:00
|
|
|
if (conn->keeptimer > hsec)
|
2018-03-12 17:59:46 +01:00
|
|
|
{
|
2022-05-18 07:32:56 +02:00
|
|
|
/* Will not yet decrement to zero */
|
|
|
|
|
|
|
|
conn->keeptimer -= hsec;
|
2021-10-02 02:47:08 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Yes.. Has the retry count expired? */
|
2018-03-12 17:59:46 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
if (conn->keepretries >= conn->keepcnt)
|
2018-03-12 17:59:46 +01:00
|
|
|
{
|
2021-10-02 02:47:08 +02:00
|
|
|
/* Yes... stop the network monitor, closing the
|
|
|
|
* connection and all sockets associated with the
|
|
|
|
* connection.
|
|
|
|
*/
|
2018-03-12 17:59:46 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
tcp_stop_monitor(conn, TCP_ABORT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned int tcpiplen;
|
2018-03-12 17:59:46 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
/* No.. we need to send another probe.
|
|
|
|
* Get the size of the IP and TCP header.
|
|
|
|
*/
|
2020-01-11 16:40:32 +01:00
|
|
|
|
2018-03-12 17:59:46 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2021-10-02 02:47:08 +02:00
|
|
|
if (conn->domain == PF_INET)
|
2018-03-12 17:59:46 +01:00
|
|
|
#endif
|
2021-10-02 02:47:08 +02:00
|
|
|
{
|
|
|
|
tcpiplen = IPv4_HDRLEN + TCP_HDRLEN;
|
|
|
|
}
|
2018-03-12 17:59:46 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2021-10-02 02:47:08 +02:00
|
|
|
else
|
2018-03-12 17:59:46 +01:00
|
|
|
#endif
|
2021-10-02 02:47:08 +02:00
|
|
|
{
|
|
|
|
tcpiplen = IPv6_HDRLEN + TCP_HDRLEN;
|
|
|
|
}
|
2018-03-12 17:59:46 +01:00
|
|
|
#endif
|
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
/* And send the probe.
|
|
|
|
* The packet we send must have these properties:
|
|
|
|
*
|
|
|
|
* - TCP_ACK flag (only) is set.
|
|
|
|
* - Sequence number is the sequence number of
|
|
|
|
* previously ACKed data, i.e., the expected
|
|
|
|
* sequence number minus one.
|
|
|
|
*
|
|
|
|
* tcp_send() will send the TCP sequence number as
|
|
|
|
* conn->sndseq. Rather than creating a new
|
|
|
|
* interface, we spoof tcp_end() here:
|
|
|
|
*/
|
2018-03-12 22:54:21 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
saveseq = tcp_getsequence(conn->sndseq);
|
|
|
|
tcp_setsequence(conn->sndseq, saveseq - 1);
|
2018-03-12 22:54:21 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
tcp_send(dev, conn, TCP_ACK, tcpiplen);
|
2018-03-12 22:54:21 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
tcp_setsequence(conn->sndseq, saveseq);
|
2018-03-12 22:54:21 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
2021-10-02 02:47:08 +02:00
|
|
|
/* Increment the un-ACKed sequence number */
|
2018-03-12 22:54:21 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
conn->sndseq_max++;
|
2018-03-12 22:54:21 +01:00
|
|
|
#endif
|
2021-10-02 02:47:08 +02:00
|
|
|
/* Update for the next probe */
|
2018-03-12 17:59:46 +01:00
|
|
|
|
2022-05-18 07:32:56 +02:00
|
|
|
conn->keeptimer = conn->keepintvl;
|
2021-10-02 02:47:08 +02:00
|
|
|
conn->keepretries++;
|
2018-03-12 17:59:46 +01:00
|
|
|
}
|
2021-10-02 02:47:08 +02:00
|
|
|
|
|
|
|
goto done;
|
2018-03-12 17:59:46 +01:00
|
|
|
}
|
2021-10-02 02:47:08 +02:00
|
|
|
}
|
2018-03-12 17:59:46 +01:00
|
|
|
#endif
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_DELAYED_ACK
|
2021-10-02 02:47:08 +02:00
|
|
|
/* Handle delayed acknowledgments. Is there a segment with a
|
|
|
|
* delayed acknowledgment?
|
|
|
|
*/
|
2019-12-08 20:13:51 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
if (conn->rx_unackseg > 0)
|
|
|
|
{
|
|
|
|
/* Increment the ACK delay. */
|
2019-12-08 20:13:51 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
conn->rx_acktimer += hsec;
|
2019-12-08 20:13:51 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
/* Per RFC 1122: "...an ACK should not be excessively
|
|
|
|
* delayed; in particular, the delay must be less than
|
|
|
|
* 0.5 seconds..."
|
|
|
|
*/
|
2019-12-08 20:13:51 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
if (conn->rx_acktimer >= ACK_DELAY)
|
|
|
|
{
|
|
|
|
/* Reset the delayed ACK state and send the ACK
|
|
|
|
* packet.
|
|
|
|
*/
|
2019-12-08 20:13:51 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
conn->rx_unackseg = 0;
|
|
|
|
conn->rx_acktimer = 0;
|
|
|
|
tcp_synack(dev, conn, TCP_ACK);
|
|
|
|
goto done;
|
2019-12-08 20:13:51 +01:00
|
|
|
}
|
2021-10-02 02:47:08 +02:00
|
|
|
}
|
2019-12-08 20:13:51 +01:00
|
|
|
#endif
|
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
/* There was no need for a retransmission and there was no
|
|
|
|
* need to probe the remote peer and there was no need to
|
|
|
|
* send a delayed ACK. We poll the application for new
|
|
|
|
* outgoing data.
|
|
|
|
*/
|
2018-03-12 17:59:46 +01:00
|
|
|
|
2021-10-02 02:47:08 +02:00
|
|
|
result = tcp_callback(dev, conn, TCP_POLL);
|
|
|
|
tcp_appsend(dev, conn, result);
|
|
|
|
goto done;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Nothing to be done */
|
|
|
|
|
|
|
|
dev->d_len = 0;
|
|
|
|
|
|
|
|
done:
|
2022-04-25 17:08:24 +02:00
|
|
|
tcp_update_timer(conn);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2007-11-22 19:36:46 +01:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_TCP */
|