2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
2014-06-18 18:18:53 +02:00
|
|
|
* net/tcp/tcp_appsend.c
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
2014-01-14 15:30:35 +01:00
|
|
|
* Copyright (C) 2007-2010, 2014 Gregory Nutt. All rights 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>
|
2011-01-23 18:40:10 +01:00
|
|
|
#include <assert.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2014-06-24 16:53:28 +02:00
|
|
|
#include <nuttx/net/netconfig.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/tcp.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-06-29 02:07:02 +02:00
|
|
|
#include "devif/devif.h"
|
2014-06-25 02:12:49 +02:00
|
|
|
#include "tcp/tcp.h"
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 02:12:49 +02:00
|
|
|
* Name: tcp_appsend
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2010-11-26 06:26:27 +01:00
|
|
|
* Handle application or TCP protocol response. If this function is called
|
|
|
|
* with dev->d_sndlen > 0, then this is an application attempting to send
|
|
|
|
* packet.
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-11-07 19:54:35 +01:00
|
|
|
* dev - The device driver structure to use in the send operation
|
|
|
|
* conn - The TCP connection structure holding connection information
|
|
|
|
* result - App result event sent
|
|
|
|
*
|
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.
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-28 00:48:12 +02:00
|
|
|
void tcp_appsend(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn,
|
2014-06-25 02:12:49 +02:00
|
|
|
uint16_t result)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2015-01-18 01:18:19 +01:00
|
|
|
uint8_t hdrlen;
|
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
ninfo("result: %04x d_sndlen: %d conn->tx_unacked: %d\n",
|
|
|
|
result, dev->d_sndlen, conn->tx_unacked);
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_DELAYED_ACK
|
|
|
|
/* Did the caller request that an ACK be sent? */
|
|
|
|
|
|
|
|
if ((result & TCP_SNDACK) != 0)
|
|
|
|
{
|
|
|
|
/* Yes.. Handle delayed acknowledgments */
|
|
|
|
|
|
|
|
/* Reset the ACK timer in any event. */
|
|
|
|
|
|
|
|
conn->rx_acktimer = 0;
|
|
|
|
|
2019-12-09 13:06:17 +01:00
|
|
|
/* Per RFC 1122: "...in a stream of full-sized segments there
|
|
|
|
* SHOULD be an ACK for at least every second segment."
|
2019-12-08 21:17:55 +01:00
|
|
|
*
|
2019-12-09 13:06:17 +01:00
|
|
|
* NOTES:
|
|
|
|
* 1. If there is a data payload or other flags to be sent with the
|
|
|
|
* outgoing packet, then we may as well include the ACK too.
|
|
|
|
* 2. The RFC refers to full-size segments. It is not clear what
|
|
|
|
* "full-size" means. Does that mean that the payload is the size
|
|
|
|
* of the MSS? Payload size is not considered other there being
|
|
|
|
* a payload or or not. Should there be some special action for
|
|
|
|
* small payloads of size < MSS?
|
|
|
|
* 3. Experimentation shows that Windows and Linux behave somewhat
|
|
|
|
* differently; they delay the ACKs for many more segments (6 or
|
|
|
|
* more). Delaying for more segments would provide less network
|
|
|
|
* traffic and better performance but seems non-compliant.
|
2019-12-08 20:13:51 +01:00
|
|
|
*/
|
|
|
|
|
2019-12-08 21:17:55 +01:00
|
|
|
if (conn->rx_unackseg > 0 || dev->d_sndlen > 0 ||
|
|
|
|
result != TCP_SNDACK)
|
2019-12-08 20:13:51 +01:00
|
|
|
{
|
|
|
|
/* Reset the delayed ACK state and send the ACK with this packet. */
|
|
|
|
|
|
|
|
conn->rx_unackseg = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-08 21:17:55 +01:00
|
|
|
/* This is only an ACK and there is no pending delayed ACK and
|
|
|
|
* no TX data is being sent. Indicate that there is one un-ACKed
|
|
|
|
* segment and don't send anything now.
|
2019-12-08 20:13:51 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
conn->rx_unackseg = 1;
|
2019-12-08 21:17:55 +01:00
|
|
|
return;
|
2019-12-08 20:13:51 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-09 15:45:52 +01:00
|
|
|
|
|
|
|
/* If there are data to be sent in the same direction as the ACK before
|
|
|
|
* the second data packet is received and the delay timer expires, the ACK
|
|
|
|
* is piggybacked with the data segment and sent immediately.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (dev->d_sndlen > 0 && conn->rx_unackseg > 0)
|
|
|
|
{
|
|
|
|
result |= TCP_SNDACK;
|
|
|
|
conn->rx_unackseg = 0;
|
|
|
|
}
|
2019-12-08 20:13:51 +01:00
|
|
|
#endif
|
2007-11-08 17:43:06 +01:00
|
|
|
|
2015-01-18 01:18:19 +01:00
|
|
|
/* Get the IP header length associated with the IP domain configured for
|
|
|
|
* this TCP connection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (conn->domain == PF_INET)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
DEBUGASSERT(IFF_IS_IPv4(dev->d_flags));
|
|
|
|
hdrlen = IPv4TCP_HDRLEN;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
DEBUGASSERT(IFF_IS_IPv6(dev->d_flags));
|
|
|
|
hdrlen = IPv6TCP_HDRLEN;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
2015-05-30 17:12:27 +02:00
|
|
|
/* Check If the device went down */
|
|
|
|
|
|
|
|
if ((result & NETDEV_DOWN) != 0)
|
|
|
|
{
|
|
|
|
/* If so, make sure that the connection is marked closed
|
|
|
|
* and do not try to send anything.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dev->d_sndlen = 0;
|
|
|
|
conn->tcpstateflags = TCP_CLOSED;
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("TCP state: NETDEV_DOWN\n");
|
2015-05-30 17:12:27 +02:00
|
|
|
}
|
|
|
|
|
2007-11-15 23:38:32 +01:00
|
|
|
/* Check for connection aborted */
|
|
|
|
|
2015-05-30 17:12:27 +02:00
|
|
|
else if ((result & TCP_ABORT) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
dev->d_sndlen = 0;
|
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
|
|
|
|
2015-01-18 01:18:19 +01:00
|
|
|
tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2007-11-15 23:38:32 +01:00
|
|
|
/* Check for connection closed */
|
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
else if ((result & TCP_CLOSE) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-07-07 00:10:26 +02:00
|
|
|
conn->tcpstateflags = TCP_FIN_WAIT_1;
|
2019-12-08 20:13:51 +01:00
|
|
|
conn->tx_unacked = 1;
|
2019-08-30 14:43:45 +02:00
|
|
|
conn->nrtx = 0;
|
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
|
|
|
conn->sndseq_max = tcp_getsequence(conn->sndseq) + 1;
|
|
|
|
#endif
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("TCP state: TCP_FIN_WAIT_1\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2019-08-30 14:43:45 +02:00
|
|
|
dev->d_sndlen = 0;
|
2015-01-18 01:18:19 +01:00
|
|
|
tcp_send(dev, conn, TCP_FIN | TCP_ACK, hdrlen);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2007-11-15 23:38:32 +01:00
|
|
|
/* None of the above */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2007-11-15 23:38:32 +01:00
|
|
|
else
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-01-14 15:30:35 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
2015-08-25 23:16:52 +02:00
|
|
|
DEBUGASSERT(dev->d_sndlen <= conn->mss);
|
2014-01-14 15:30:35 +01:00
|
|
|
#else
|
2007-11-15 23:38:32 +01:00
|
|
|
/* If d_sndlen > 0, the application has data to be sent. */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2007-11-15 23:38:32 +01:00
|
|
|
if (dev->d_sndlen > 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2010-11-26 06:26:27 +01:00
|
|
|
/* Remember how much data we send out now so that we know
|
2020-11-25 11:45:02 +01:00
|
|
|
* when everything has been acknowledged. Just increment the
|
|
|
|
* amount of data sent. This will be needed in sequence number
|
|
|
|
* calculations and we know that this is not a re-transmission.
|
|
|
|
* Retransmissions do not go through this path.
|
2007-11-07 19:54:35 +01:00
|
|
|
*/
|
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
conn->tx_unacked += dev->d_sndlen;
|
2007-11-15 23:38:32 +01:00
|
|
|
|
2010-11-26 06:26:27 +01:00
|
|
|
/* The application cannot send more than what is allowed by the
|
2019-12-08 20:13:51 +01:00
|
|
|
* MSS (the minimum of the MSS and the available window).
|
2010-11-26 06:26:27 +01:00
|
|
|
*/
|
2007-11-15 23:38:32 +01:00
|
|
|
|
2010-11-26 06:26:27 +01:00
|
|
|
DEBUGASSERT(dev->d_sndlen <= conn->mss);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2014-01-14 15:30:35 +01:00
|
|
|
conn->nrtx = 0;
|
|
|
|
#endif
|
2019-12-08 20:13:51 +01:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/* Then handle the rest of the operation just as for the rexmit case */
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_rexmit(dev, conn, result);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 02:12:49 +02:00
|
|
|
* Name: tcp_rexmit
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle application retransmission
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-11-07 19:54:35 +01:00
|
|
|
* dev - The device driver structure to use in the send operation
|
|
|
|
* conn - The TCP connection structure holding connection information
|
|
|
|
* result - App result event sent
|
|
|
|
*
|
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.
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-28 00:48:12 +02:00
|
|
|
void tcp_rexmit(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn,
|
2014-06-25 02:12:49 +02:00
|
|
|
uint16_t result)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2015-01-18 01:18:19 +01:00
|
|
|
uint8_t hdrlen;
|
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
ninfo("result: %04x d_sndlen: %d conn->tx_unacked: %d\n",
|
|
|
|
result, dev->d_sndlen, conn->tx_unacked);
|
2007-11-08 17:43:06 +01:00
|
|
|
|
2015-01-18 01:18:19 +01:00
|
|
|
/* Get the IP header length associated with the IP domain configured for
|
|
|
|
* this TCP connection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (conn->domain == PF_INET)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
DEBUGASSERT(IFF_IS_IPv4(dev->d_flags));
|
2015-01-22 19:10:16 +01:00
|
|
|
hdrlen = IPv4TCP_HDRLEN;
|
2015-01-18 01:18:19 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
DEBUGASSERT(IFF_IS_IPv6(dev->d_flags));
|
2015-01-22 19:10:16 +01:00
|
|
|
hdrlen = IPv6TCP_HDRLEN;
|
2015-01-18 01:18:19 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/* If the application has data to be sent, or if the incoming packet had
|
2007-11-07 19:54:35 +01:00
|
|
|
* new data in it, we must send out a packet.
|
|
|
|
*/
|
|
|
|
|
2014-01-14 15:30:35 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
|
|
|
if (dev->d_sndlen > 0)
|
|
|
|
#else
|
2019-12-08 20:13:51 +01:00
|
|
|
if (dev->d_sndlen > 0 && conn->tx_unacked > 0)
|
2014-01-14 15:30:35 +01:00
|
|
|
#endif
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* We always set the ACK flag in response packets adding the length of
|
|
|
|
* the IP and TCP headers.
|
|
|
|
*/
|
|
|
|
|
2015-01-18 01:18:19 +01:00
|
|
|
tcp_send(dev, conn, TCP_ACK | TCP_PSH, dev->d_sndlen + hdrlen);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 11:45:02 +01:00
|
|
|
/* If there is no data to send, just send out a pure ACK if one is
|
|
|
|
* requested.
|
|
|
|
*/
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
else if ((result & TCP_SNDACK) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2015-01-18 01:18:19 +01:00
|
|
|
tcp_send(dev, conn, TCP_ACK, hdrlen);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* There is nothing to do -- drop the packet */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dev->d_len = 0;
|
|
|
|
}
|
|
|
|
}
|
2007-11-22 19:36:46 +01:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_TCP */
|