2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
2014-06-18 18:18:53 +02:00
|
|
|
* net/tcp/tcp_input.c
|
2024-09-11 14:39:39 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
2020-01-11 16:40:32 +01:00
|
|
|
* Copyright (C) 2007-2014, 2017-2019, 2020 Gregory Nutt. All rights
|
|
|
|
* reserved.
|
2012-02-23 16:53:27 +01: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>
|
2012-02-23 16:53:27 +01:00
|
|
|
|
2007-11-22 19:36:46 +01:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2020-11-23 12:43:52 +01:00
|
|
|
#include <inttypes.h>
|
2023-02-01 13:07:47 +01:00
|
|
|
#include <stdbool.h>
|
2009-12-15 15:53:45 +01:00
|
|
|
#include <stdint.h>
|
2007-12-11 15:27:11 +01:00
|
|
|
#include <string.h>
|
2017-08-26 18:00:47 +02: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/netstats.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <nuttx/net/ip.h>
|
|
|
|
#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-28 01:51:32 +02:00
|
|
|
#include "utils/utils.h"
|
2014-06-25 02:12:49 +02:00
|
|
|
#include "tcp/tcp.h"
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2023-01-06 04:46:36 +01:00
|
|
|
#define IPDATA(hl) (*(FAR uint8_t *)IPBUF(hl))
|
2019-08-31 20:25:30 +02:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-18 04:27:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_trim_head
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Trim the head of the TCP segment.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device driver structure containing the received TCP
|
|
|
|
* packet.
|
|
|
|
* tcp - The TCP header.
|
|
|
|
* trimlen - The length to trim in bytes.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* True if nothing was left.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool tcp_trim_head(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct tcp_hdr_s *tcp,
|
|
|
|
uint32_t trimlen)
|
|
|
|
{
|
|
|
|
uint32_t seq = tcp_getsequence(tcp->seqno);
|
|
|
|
uint16_t urg_ptr = (tcp->urgp[0] << 8) | tcp->urgp[1];
|
|
|
|
uint32_t urg_trimlen = 0;
|
|
|
|
uint8_t th_flags = tcp->flags;
|
|
|
|
|
|
|
|
DEBUGASSERT(trimlen > 0);
|
|
|
|
ninfo("Dropping %" PRIu32 " bytes: "
|
|
|
|
"seq=%" PRIu32 ", "
|
|
|
|
"tcp flags=%" PRIx8 ", "
|
|
|
|
"d_len=%" PRIu16 ", "
|
|
|
|
"urg_ptr=%" PRIu16 "\n",
|
|
|
|
trimlen,
|
|
|
|
seq,
|
|
|
|
th_flags,
|
|
|
|
dev->d_len,
|
|
|
|
urg_ptr);
|
|
|
|
|
|
|
|
if ((th_flags & TCP_SYN) != 0)
|
|
|
|
{
|
|
|
|
ninfo("Dropping SYN\n");
|
|
|
|
seq = TCP_SEQ_ADD(seq, 1);
|
|
|
|
urg_trimlen++;
|
|
|
|
trimlen--;
|
|
|
|
th_flags &= ~TCP_SYN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trimlen > 0)
|
|
|
|
{
|
|
|
|
uint32_t len = trimlen;
|
|
|
|
|
|
|
|
if (len > dev->d_len)
|
|
|
|
{
|
|
|
|
len = dev->d_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
ninfo("Dropping %" PRIu32 " bytes app data\n", len);
|
|
|
|
seq = TCP_SEQ_ADD(seq, len);
|
|
|
|
urg_trimlen += len;
|
|
|
|
dev->d_appdata += len;
|
|
|
|
dev->d_len -= len;
|
|
|
|
trimlen -= len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trimlen > 0)
|
|
|
|
{
|
|
|
|
if ((th_flags & TCP_FIN) != 0)
|
|
|
|
{
|
|
|
|
ninfo("Dropping FIN\n");
|
|
|
|
seq = TCP_SEQ_ADD(seq, 1);
|
|
|
|
urg_trimlen++;
|
|
|
|
trimlen--;
|
|
|
|
th_flags &= ~TCP_FIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the header */
|
|
|
|
|
|
|
|
if ((th_flags & TCP_URG) != 0)
|
|
|
|
{
|
|
|
|
/* Adjust URG pointer */
|
|
|
|
|
|
|
|
if (urg_trimlen >= urg_ptr)
|
|
|
|
{
|
|
|
|
th_flags &= ~TCP_URG;
|
|
|
|
urg_ptr = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
urg_ptr -= urg_trimlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
ninfo("Adjusting URG pointer by %" PRIu32 ", "
|
|
|
|
"new urg_ptr=%" PRIu16 "\n",
|
|
|
|
urg_trimlen, urg_ptr);
|
|
|
|
|
|
|
|
tcp->urgp[0] = (uint8_t)(urg_ptr >> 8);
|
|
|
|
tcp->urgp[1] = (uint8_t)urg_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcp->flags = th_flags;
|
|
|
|
tcp_setsequence(tcp->seqno, seq);
|
|
|
|
|
|
|
|
if ((th_flags & (TCP_SYN | TCP_FIN)) == 0 && dev->d_len == 0)
|
|
|
|
{
|
|
|
|
ninfo("Dropped the entire segment\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(trimlen == 0);
|
|
|
|
ninfo("Dropped the segment partially\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-16 12:47:08 +02:00
|
|
|
static void tcp_snd_wnd_init(FAR struct tcp_conn_s *conn,
|
|
|
|
FAR struct tcp_hdr_s *tcp)
|
|
|
|
{
|
|
|
|
/* Just ensure that the next tcp_update_snd_wnd will be accepted. */
|
|
|
|
|
|
|
|
DEBUGASSERT((tcp->flags & TCP_ACK) != 0);
|
|
|
|
conn->snd_wl1 = TCP_SEQ_SUB(tcp_getsequence(tcp->seqno), 1);
|
|
|
|
conn->snd_wl2 = tcp_getsequence(tcp->ackno);
|
|
|
|
conn->snd_wnd = 0;
|
|
|
|
ninfo("snd_wnd init: wl1 %" PRIu32 "\n", conn->snd_wl1);
|
|
|
|
}
|
|
|
|
|
2023-02-01 13:07:47 +01:00
|
|
|
static bool tcp_snd_wnd_update(FAR struct tcp_conn_s *conn,
|
2021-07-16 12:47:08 +02:00
|
|
|
FAR struct tcp_hdr_s *tcp)
|
|
|
|
{
|
|
|
|
uint32_t ackseq = tcp_getsequence(tcp->ackno);
|
|
|
|
uint32_t seq = tcp_getsequence(tcp->seqno);
|
|
|
|
uint16_t unscaled_wnd = ((uint16_t)tcp->wnd[0] << 8) + tcp->wnd[1];
|
|
|
|
#ifdef CONFIG_NET_TCP_WINDOW_SCALE
|
|
|
|
uint32_t wnd = (uint32_t)unscaled_wnd << conn->snd_scale;
|
|
|
|
#else
|
|
|
|
uint16_t wnd = unscaled_wnd;
|
|
|
|
#endif
|
|
|
|
uint32_t wl2 = conn->snd_wl2;
|
|
|
|
|
|
|
|
DEBUGASSERT((tcp->flags & TCP_ACK) != 0);
|
|
|
|
|
|
|
|
if (TCP_SEQ_LT(wl2, ackseq))
|
|
|
|
{
|
|
|
|
uint32_t nacked = TCP_SEQ_SUB(ackseq, wl2);
|
|
|
|
|
|
|
|
ninfo("snd_wnd acked: "
|
|
|
|
"wl2 %" PRIu32 " -> %" PRIu32 " subtracting wnd %" PRIu32
|
|
|
|
" by %" PRIu32 "\n",
|
|
|
|
wl2,
|
|
|
|
ackseq,
|
|
|
|
(uint32_t)conn->snd_wnd,
|
|
|
|
nacked);
|
|
|
|
|
|
|
|
if (nacked > conn->snd_wnd)
|
|
|
|
{
|
|
|
|
conn->snd_wnd = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
conn->snd_wnd -= nacked;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->snd_wl2 = ackseq;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TCP_SEQ_LT(conn->snd_wl1, seq) ||
|
|
|
|
(conn->snd_wl1 == seq && TCP_SEQ_LT(wl2, ackseq)) ||
|
|
|
|
(wl2 == ackseq && conn->snd_wnd < wnd))
|
|
|
|
{
|
|
|
|
ninfo("snd_wnd update: "
|
|
|
|
"wl1 %" PRIu32 " wl2 %" PRIu32 " wnd %" PRIu32 " -> "
|
|
|
|
"wl1 %" PRIu32 " wl2 %" PRIu32 " wnd %" PRIu32 "\n",
|
|
|
|
conn->snd_wl1,
|
|
|
|
wl2,
|
|
|
|
(uint32_t)conn->snd_wnd,
|
|
|
|
seq,
|
|
|
|
ackseq,
|
|
|
|
(uint32_t)wnd);
|
|
|
|
|
|
|
|
conn->snd_wl1 = seq;
|
|
|
|
conn->snd_wl2 = ackseq;
|
2023-06-19 11:15:29 +02:00
|
|
|
if (conn->snd_wnd != wnd)
|
|
|
|
{
|
|
|
|
conn->snd_wnd = wnd;
|
|
|
|
return true;
|
|
|
|
}
|
2021-07-16 12:47:08 +02:00
|
|
|
}
|
2023-02-01 13:07:47 +01:00
|
|
|
|
|
|
|
return false;
|
2021-07-16 12:47:08 +02:00
|
|
|
}
|
|
|
|
|
2023-01-06 08:30:58 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_OUT_OF_ORDER
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_rebuild_ofosegs
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Re-build out-of-order pool from incoming segment
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - The TCP connection of interest
|
|
|
|
* ofoseg - Pointer to incoming out-of-order segment
|
|
|
|
* start - Index of start postion of segment pool
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* True if incoming data has been consumed
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool tcp_rebuild_ofosegs(FAR struct tcp_conn_s *conn,
|
|
|
|
FAR struct tcp_ofoseg_s *ofoseg,
|
|
|
|
int start)
|
|
|
|
{
|
|
|
|
struct tcp_ofoseg_s *seg;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = start; i < conn->nofosegs && ofoseg->data != NULL; i++)
|
|
|
|
{
|
|
|
|
seg = &conn->ofosegs[i];
|
|
|
|
|
|
|
|
/* ofoseg |~~~
|
|
|
|
* segpool |---|
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (TCP_SEQ_GTE(ofoseg->left, seg->left))
|
|
|
|
{
|
|
|
|
/* ofoseg |---|
|
|
|
|
* segpool |---|
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (TCP_SEQ_GT(ofoseg->left, seg->right))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ofoseg |---|
|
|
|
|
* segpool |---|
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (ofoseg->left == seg->right)
|
|
|
|
{
|
2023-07-04 08:24:16 +02:00
|
|
|
net_iob_concat(&seg->data, &ofoseg->data);
|
2023-01-06 08:30:58 +01:00
|
|
|
seg->right = ofoseg->right;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ofoseg |--|
|
|
|
|
* segpool |---|
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (TCP_SEQ_LTE(ofoseg->right, seg->right))
|
|
|
|
{
|
|
|
|
iob_free_chain(ofoseg->data);
|
|
|
|
ofoseg->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ofoseg |---|
|
|
|
|
* segpool |---|
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (TCP_SEQ_GT(ofoseg->right, seg->right))
|
|
|
|
{
|
|
|
|
ofoseg->data =
|
|
|
|
iob_trimhead(ofoseg->data,
|
|
|
|
TCP_SEQ_SUB(seg->right, ofoseg->left));
|
2023-07-04 08:24:16 +02:00
|
|
|
net_iob_concat(&seg->data, &ofoseg->data);
|
2023-01-06 08:30:58 +01:00
|
|
|
seg->right = ofoseg->right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ofoseg |~~~
|
|
|
|
* segpool |---|
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* ofoseg |---|
|
|
|
|
* segpool |---|
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ofoseg->right == seg->left)
|
|
|
|
{
|
2023-07-04 08:24:16 +02:00
|
|
|
net_iob_concat(&ofoseg->data, &seg->data);
|
2023-01-06 08:30:58 +01:00
|
|
|
seg->data = ofoseg->data;
|
|
|
|
seg->left = ofoseg->left;
|
|
|
|
ofoseg->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ofoseg |---|
|
|
|
|
* segpool |---|
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (TCP_SEQ_LT(ofoseg->right, seg->left))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ofoseg |---|~|
|
|
|
|
* segpool |--|
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (TCP_SEQ_GTE(ofoseg->right, seg->right))
|
|
|
|
{
|
|
|
|
iob_free_chain(seg->data);
|
|
|
|
*seg = *ofoseg;
|
|
|
|
ofoseg->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ofoseg |---|
|
|
|
|
* segpool |---|
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (TCP_SEQ_GT(ofoseg->right, seg->left))
|
|
|
|
{
|
|
|
|
ofoseg->data =
|
|
|
|
iob_trimtail(ofoseg->data,
|
|
|
|
ofoseg->right - seg->left);
|
2023-07-04 08:24:16 +02:00
|
|
|
net_iob_concat(&ofoseg->data, &seg->data);
|
2023-01-06 08:30:58 +01:00
|
|
|
seg->data = ofoseg->data;
|
|
|
|
seg->left = ofoseg->left;
|
|
|
|
ofoseg->data = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ofoseg->data == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_input_ofosegs
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle incoming TCP data to out-of-order pool
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device driver structure containing the received TCP packet.
|
|
|
|
* conn - The TCP connection of interest
|
|
|
|
* iplen - Length of the IP header (IPv4_HDRLEN or IPv6_HDRLEN).
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void tcp_input_ofosegs(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct tcp_conn_s *conn,
|
|
|
|
unsigned int iplen)
|
|
|
|
{
|
|
|
|
struct tcp_ofoseg_s ofoseg;
|
|
|
|
bool rebuild;
|
|
|
|
int i = 0;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
ofoseg.left =
|
|
|
|
tcp_getsequence(((FAR struct tcp_hdr_s *)IPBUF(iplen))->seqno);
|
|
|
|
|
|
|
|
/* Calculate the pending size of out-of-order cache, if the input edge can
|
|
|
|
* not fill the adjacent segments, drop it
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (tcp_ofoseg_bufsize(conn) > CONFIG_NET_TCP_OUT_OF_ORDER_BUFSIZE &&
|
|
|
|
ofoseg.left >= conn->ofosegs[0].left)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get left/right edge from incoming data */
|
|
|
|
|
|
|
|
len = (dev->d_appdata - dev->d_iob->io_data) - dev->d_iob->io_offset;
|
|
|
|
ofoseg.right = TCP_SEQ_ADD(ofoseg.left, dev->d_iob->io_pktlen - len);
|
|
|
|
|
|
|
|
ninfo("TCP OFOSEG out-of-order "
|
|
|
|
"[%" PRIu32 " : %" PRIu32 " : %" PRIu32 "]\n",
|
|
|
|
ofoseg.left, ofoseg.right, TCP_SEQ_SUB(ofoseg.right, ofoseg.left));
|
|
|
|
|
|
|
|
/* Trim l3/l4 header to reserve appdata */
|
|
|
|
|
|
|
|
dev->d_iob = iob_trimhead(dev->d_iob, len);
|
2023-04-18 11:46:56 +02:00
|
|
|
if (dev->d_iob == NULL || dev->d_iob->io_pktlen == 0)
|
2023-01-06 08:30:58 +01:00
|
|
|
{
|
2023-09-19 04:24:06 +02:00
|
|
|
/* No available data, prepare device iob */
|
2023-01-06 08:30:58 +01:00
|
|
|
|
2023-09-19 04:24:06 +02:00
|
|
|
goto prepare;
|
2023-01-06 08:30:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ofoseg.data = dev->d_iob;
|
|
|
|
|
|
|
|
/* Build out-of-order pool */
|
|
|
|
|
|
|
|
rebuild = tcp_rebuild_ofosegs(conn, &ofoseg, 0);
|
|
|
|
|
|
|
|
/* Incoming segment out of order from existing pool, add to new segment */
|
|
|
|
|
|
|
|
if (!rebuild && conn->nofosegs != TCP_SACK_RANGES_MAX)
|
|
|
|
{
|
|
|
|
conn->ofosegs[conn->nofosegs] = ofoseg;
|
|
|
|
conn->nofosegs++;
|
|
|
|
rebuild = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try Re-order ofosegs */
|
|
|
|
|
|
|
|
if (rebuild &&
|
|
|
|
tcp_reorder_ofosegs(conn->nofosegs, (FAR void *)conn->ofosegs))
|
|
|
|
{
|
|
|
|
/* Re-build out-of-order pool after re-order */
|
|
|
|
|
|
|
|
while (i < conn->nofosegs - 1)
|
|
|
|
{
|
|
|
|
if (tcp_rebuild_ofosegs(conn, &conn->ofosegs[i], i + 1))
|
|
|
|
{
|
|
|
|
for (; i < conn->nofosegs - 1; i++)
|
|
|
|
{
|
|
|
|
conn->ofosegs[i] = conn->ofosegs[i + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->nofosegs--;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < conn->nofosegs; i++)
|
|
|
|
{
|
|
|
|
ninfo("TCP OFOSEG [%d][%" PRIu32 " : %" PRIu32 " : %" PRIu32 "]\n", i,
|
|
|
|
conn->ofosegs[i].left, conn->ofosegs[i].right,
|
|
|
|
TCP_SEQ_SUB(conn->ofosegs[i].right, conn->ofosegs[i].left));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Incoming data has been consumed, re-prepare device buffer to send
|
|
|
|
* response.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (rebuild)
|
|
|
|
{
|
|
|
|
netdev_iob_clear(dev);
|
|
|
|
}
|
2023-09-19 04:24:06 +02:00
|
|
|
|
|
|
|
prepare:
|
|
|
|
netdev_iob_prepare(dev, false, 0);
|
2023-01-06 08:30:58 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_TCP_OUT_OF_ORDER */
|
|
|
|
|
2023-01-09 12:44:23 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_parse_option
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Parse incoming TCP options
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device driver structure containing the received TCP packet.
|
|
|
|
* conn - The TCP connection of interest
|
|
|
|
* iplen - Length of the IP header (IPv4_HDRLEN or IPv6_HDRLEN).
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void tcp_parse_option(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct tcp_conn_s *conn,
|
|
|
|
unsigned int iplen)
|
|
|
|
{
|
|
|
|
FAR struct tcp_hdr_s *tcp;
|
|
|
|
unsigned int tcpiplen;
|
|
|
|
uint16_t tmp16;
|
|
|
|
uint8_t opt;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
tcp = IPBUF(iplen);
|
|
|
|
|
|
|
|
if ((tcp->tcpoffset & 0xf0) <= 0x50)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcpiplen = iplen + TCP_HDRLEN;
|
|
|
|
|
|
|
|
for (i = 0; i < ((tcp->tcpoffset >> 4) - 5) << 2 ; )
|
|
|
|
{
|
|
|
|
opt = IPDATA(tcpiplen + i);
|
|
|
|
if (opt == TCP_OPT_END)
|
|
|
|
{
|
|
|
|
/* End of options. */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (opt == TCP_OPT_NOOP)
|
|
|
|
{
|
|
|
|
/* NOP option. */
|
|
|
|
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (opt == TCP_OPT_MSS &&
|
|
|
|
IPDATA(tcpiplen + 1 + i) == TCP_OPT_MSS_LEN)
|
|
|
|
{
|
|
|
|
uint16_t tcp_mss = TCP_MSS(dev, iplen);
|
|
|
|
|
|
|
|
/* An MSS option with the right option length. */
|
|
|
|
|
|
|
|
tmp16 = ((uint16_t)IPDATA(tcpiplen + 2 + i) << 8) |
|
|
|
|
(uint16_t)IPDATA(tcpiplen + 3 + i);
|
2023-03-08 16:43:38 +01:00
|
|
|
#ifdef CONFIG_NET_TCPPROTO_OPTIONS
|
|
|
|
if (conn->user_mss > 0 && conn->user_mss < tcp_mss)
|
|
|
|
{
|
|
|
|
tcp_mss = conn->user_mss;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-01-09 12:44:23 +01:00
|
|
|
conn->mss = tmp16 > tcp_mss ? tcp_mss : tmp16;
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_NET_TCP_WINDOW_SCALE
|
|
|
|
else if (opt == TCP_OPT_WS &&
|
|
|
|
IPDATA(tcpiplen + 1 + i) == TCP_OPT_WS_LEN)
|
|
|
|
{
|
|
|
|
conn->snd_scale = IPDATA(tcpiplen + 2 + i);
|
|
|
|
conn->rcv_scale = CONFIG_NET_TCP_WINDOW_SCALE_FACTOR;
|
|
|
|
conn->flags |= TCP_WSCALE;
|
|
|
|
}
|
2023-01-10 06:41:02 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_TCP_SELECTIVE_ACK
|
|
|
|
else if (opt == TCP_OPT_SACK_PERM &&
|
|
|
|
IPDATA(tcpiplen + 1 + i) ==
|
|
|
|
TCP_OPT_SACK_PERM_LEN)
|
|
|
|
{
|
|
|
|
conn->flags |= TCP_SACK;
|
|
|
|
}
|
2023-01-09 12:44:23 +01:00
|
|
|
#endif
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* All other options have a length field, so that we
|
|
|
|
* easily can skip past them.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (IPDATA(tcpiplen + 1 + i) == 0)
|
|
|
|
{
|
|
|
|
/* If the length field is zero, the options are
|
|
|
|
* malformed and we don't process them further.
|
|
|
|
*/
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i += IPDATA(tcpiplen + 1 + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 13:43:37 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_clear_zero_probe
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* clear the TCP zero window probe
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - The TCP connection of interest
|
|
|
|
* tcp - Header of TCP structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void tcp_clear_zero_probe(FAR struct tcp_conn_s *conn,
|
|
|
|
FAR struct tcp_hdr_s *tcp)
|
|
|
|
{
|
|
|
|
/* If the receive window is not 0,
|
|
|
|
* the zero window probe timer needs to be cleared
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((tcp->wnd[0] || tcp->wnd[1]) && conn->zero_probe &&
|
|
|
|
(tcp->flags & TCP_ACK) != 0)
|
|
|
|
{
|
|
|
|
conn->zero_probe = false;
|
|
|
|
conn->nrtx = 0;
|
|
|
|
conn->timer = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
2014-06-25 02:12:49 +02:00
|
|
|
* Name: tcp_input
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle incoming TCP input
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2018-11-03 00:50:01 +01:00
|
|
|
* dev - The device driver structure containing the received TCP packet.
|
2017-05-25 16:02:08 +02:00
|
|
|
* domain - IP domain (PF_INET or PF_INET6)
|
2018-11-03 00:50:01 +01:00
|
|
|
* iplen - Length of the IP header (IPv4_HDRLEN or IPv6_HDRLEN).
|
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.
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-25 16:02:08 +02:00
|
|
|
static void tcp_input(FAR struct net_driver_s *dev, uint8_t domain,
|
|
|
|
unsigned int iplen)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-07-06 20:34:27 +02:00
|
|
|
FAR struct tcp_conn_s *conn = NULL;
|
2021-09-23 08:55:19 +02:00
|
|
|
FAR struct tcp_hdr_s *tcp;
|
|
|
|
union ip_binding_u uaddr;
|
2015-01-16 22:03:10 +01:00
|
|
|
unsigned int tcpiplen;
|
2009-12-15 15:53:45 +01:00
|
|
|
uint16_t tmp16;
|
|
|
|
uint16_t flags;
|
2015-05-28 01:17:42 +02:00
|
|
|
uint16_t result;
|
2009-12-15 15:53:45 +01:00
|
|
|
int len;
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2015-01-15 22:06:46 +01:00
|
|
|
/* Bump up the count of TCP packets received */
|
|
|
|
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.tcp.recv++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
|
|
|
|
2015-01-16 22:03:10 +01:00
|
|
|
/* Get a pointer to the TCP header. The TCP header lies just after the
|
|
|
|
* the link layer header and the IP header.
|
|
|
|
*/
|
|
|
|
|
2022-10-26 06:35:08 +02:00
|
|
|
tcp = IPBUF(iplen);
|
2015-01-16 22:03:10 +01:00
|
|
|
|
2018-06-24 20:06:12 +02:00
|
|
|
/* Get the size of the IP header and the TCP header.
|
|
|
|
*
|
|
|
|
* REVISIT: TCP header is *not* a constant! It can be larger if the
|
2018-06-24 23:01:49 +02:00
|
|
|
* TCP header includes options. The constant TCP_HDRLEN should be
|
2018-06-24 20:06:12 +02:00
|
|
|
* replaced with the macro TCP_OPT_HDRLEN(n) which will calculate the
|
|
|
|
* correct header length in all cases.
|
|
|
|
*/
|
2015-01-16 22:03:10 +01:00
|
|
|
|
|
|
|
tcpiplen = iplen + TCP_HDRLEN;
|
|
|
|
|
2024-04-24 05:06:52 +02:00
|
|
|
#ifdef CONFIG_NET_TCP_CHECKSUMS
|
2007-11-07 19:54:35 +01:00
|
|
|
/* Start of TCP input header processing code. */
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
if (tcp_chksum(dev) != 0xffff)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* Compute and check the TCP checksum. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.tcp.drop++;
|
|
|
|
g_netstats.tcp.chkerr++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
2016-06-20 17:37:08 +02:00
|
|
|
nwarn("WARNING: Bad TCP checksum\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
}
|
2024-04-24 05:06:52 +02:00
|
|
|
#endif
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/* Demultiplex this segment. First check any active connections. */
|
|
|
|
|
2015-01-15 22:06:46 +01:00
|
|
|
conn = tcp_active(dev, tcp);
|
2007-11-08 17:01:18 +01:00
|
|
|
if (conn)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2011-01-23 18:40:10 +01:00
|
|
|
/* We found an active connection.. Check for the subsequent SYN
|
2014-07-07 00:10:26 +02:00
|
|
|
* arriving in TCP_SYN_RCVD state after the SYNACK packet was
|
2011-01-23 18:40:10 +01:00
|
|
|
* lost. To avoid other issues, reset any active connection
|
2014-07-07 00:10:26 +02:00
|
|
|
* where a SYN arrives in a state != TCP_SYN_RCVD.
|
2011-01-23 18:40:10 +01:00
|
|
|
*/
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
if ((conn->tcpstateflags & TCP_STATE_MASK) != TCP_SYN_RCVD &&
|
2015-01-15 22:06:46 +01:00
|
|
|
(tcp->flags & TCP_CTL) == TCP_SYN)
|
2011-01-23 18:40:10 +01:00
|
|
|
{
|
2017-06-24 19:57:59 +02:00
|
|
|
nwarn("WARNING: SYN in TCP_SYN_RCVD\n");
|
2011-01-23 18:40:10 +01:00
|
|
|
goto reset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto found;
|
|
|
|
}
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2016-03-20 15:19:00 +01:00
|
|
|
/* If we didn't find an active connection that expected the packet,
|
2007-11-07 19:54:35 +01:00
|
|
|
* either (1) this packet is an old duplicate, or (2) this is a SYN packet
|
2017-06-24 19:57:59 +02:00
|
|
|
* destined for a connection in LISTEN. If the SYN flag isn't set,
|
2007-11-07 19:54:35 +01:00
|
|
|
* it is an old packet and we send a RST.
|
|
|
|
*/
|
|
|
|
|
2015-01-15 22:06:46 +01:00
|
|
|
if ((tcp->flags & TCP_CTL) == TCP_SYN)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2007-11-08 17:01:18 +01:00
|
|
|
/* This is a SYN packet for a connection. Find the connection
|
|
|
|
* listening on this port.
|
|
|
|
*/
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2015-01-15 22:06:46 +01:00
|
|
|
tmp16 = tcp->destport;
|
2021-09-23 08:55:19 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
# ifdef CONFIG_NET_IPv4
|
|
|
|
if (domain == PF_INET6)
|
|
|
|
# endif
|
|
|
|
{
|
|
|
|
net_ipv6addr_copy(&uaddr.ipv6.laddr, IPv6BUF->destipaddr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
# ifdef CONFIG_NET_IPv6
|
|
|
|
if (domain == PF_INET)
|
|
|
|
# endif
|
|
|
|
{
|
|
|
|
net_ipv4addr_copy(uaddr.ipv4.laddr,
|
|
|
|
net_ip4addr_conv32(IPv4BUF->destipaddr));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-25 09:48:55 +02:00
|
|
|
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
|
2023-03-09 05:37:43 +01:00
|
|
|
if ((conn = tcp_findlistener(&uaddr, tmp16, domain)) != NULL)
|
2017-05-25 09:48:55 +02:00
|
|
|
#else
|
2023-03-09 05:37:43 +01:00
|
|
|
if ((conn = tcp_findlistener(&uaddr, tmp16)) != NULL)
|
2017-05-25 09:48:55 +02:00
|
|
|
#endif
|
2007-11-08 17:01:18 +01:00
|
|
|
{
|
2023-04-17 16:07:31 +02:00
|
|
|
if (!tcp_backlogavailable(conn))
|
|
|
|
{
|
|
|
|
nerr("ERROR: no free containers for TCP BACKLOG!\n");
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
2007-11-08 17:01:18 +01:00
|
|
|
/* We matched the incoming packet with a connection in LISTEN.
|
|
|
|
* We now need to create a new connection and send a SYNACK in
|
|
|
|
* response.
|
|
|
|
*/
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2020-05-04 19:34:04 +02:00
|
|
|
/* First allocate a new connection structure and see if there is
|
|
|
|
* any user application to accept it.
|
2007-11-07 19:54:35 +01:00
|
|
|
*/
|
|
|
|
|
2023-03-09 05:37:43 +01:00
|
|
|
conn = tcp_alloc_accept(dev, tcp, conn);
|
2007-11-08 17:01:18 +01:00
|
|
|
if (conn)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2017-05-14 18:56:25 +02:00
|
|
|
/* The connection structure was successfully allocated and has
|
|
|
|
* been initialized in the TCP_SYN_RECVD state. The expected
|
|
|
|
* sequence of events is then the rest of the 3-way handshake:
|
|
|
|
*
|
|
|
|
* 1. We just received a TCP SYN packet from a remote host.
|
|
|
|
* 2. We will send the SYN-ACK response below (perhaps
|
|
|
|
* repeatedly in the event of a timeout)
|
|
|
|
* 3. Then we expect to receive an ACK from the remote host
|
|
|
|
* indicated the TCP socket connection is ESTABLISHED.
|
|
|
|
*
|
|
|
|
* Possible failure:
|
|
|
|
*
|
|
|
|
* 1. The ACK is never received. This will be handled by
|
|
|
|
* a timeout managed by tcp_timer().
|
|
|
|
* 2. The listener "unlistens()". This will be handled by
|
2017-05-14 20:14:31 +02:00
|
|
|
* the failure of tcp_accept_connection() when the ACK is
|
|
|
|
* received.
|
2007-11-07 19:54:35 +01:00
|
|
|
*/
|
|
|
|
|
2012-02-27 18:22:10 +01:00
|
|
|
conn->crefs = 1;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2007-11-08 17:01:18 +01:00
|
|
|
if (!conn)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2017-05-14 20:14:31 +02:00
|
|
|
/* Either (1) all available connections are in use, or (2)
|
|
|
|
* there is no application in place to accept the connection.
|
|
|
|
* We drop packet and hope that the remote end will retransmit
|
|
|
|
* the packet at a time when we have more spare connections
|
|
|
|
* or someone waiting to accept the connection.
|
2007-11-07 19:54:35 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.tcp.syndrop++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
2016-06-20 17:37:08 +02:00
|
|
|
nerr("ERROR: No free TCP connections\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
2021-06-17 06:27:18 +02:00
|
|
|
net_incr32(conn->rcvseq, 1); /* ack SYN */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/* Parse the TCP MSS option, if present. */
|
|
|
|
|
2023-01-09 12:44:23 +01:00
|
|
|
tcp_parse_option(dev, conn, iplen);
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/* Our response will be a SYNACK. */
|
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
tcp_synack(dev, conn, TCP_ACK | TCP_SYN);
|
2007-11-08 17:01:18 +01:00
|
|
|
return;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-07 16:50:01 +02:00
|
|
|
nwarn("WARNING: SYN with no listener (or old packet) .. reset\n");
|
2017-06-24 19:57:59 +02:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/* This is (1) an old duplicate packet or (2) a SYN packet but with
|
|
|
|
* no matching listener found. Send RST packet in either case.
|
|
|
|
*/
|
|
|
|
|
2011-01-23 18:40:10 +01:00
|
|
|
reset:
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/* We do not send resets in response to resets. */
|
|
|
|
|
2015-01-15 22:06:46 +01:00
|
|
|
if ((tcp->flags & TCP_RST) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.tcp.synrst++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
2022-12-21 05:40:42 +01:00
|
|
|
tcp_reset(dev, conn);
|
2007-11-08 17:01:18 +01:00
|
|
|
return;
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
found:
|
2007-11-20 00:09:39 +01:00
|
|
|
flags = 0;
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/* We do a very naive form of TCP reset processing; we just accept
|
|
|
|
* any RST and kill our connection. We should in fact check if the
|
2011-01-23 18:40:10 +01:00
|
|
|
* sequence number of this reset is within our advertised window
|
2007-11-07 19:54:35 +01:00
|
|
|
* before we accept the reset.
|
|
|
|
*/
|
|
|
|
|
2015-01-15 22:06:46 +01:00
|
|
|
if ((tcp->flags & TCP_RST) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2017-05-15 16:10:43 +02:00
|
|
|
FAR struct tcp_conn_s *listener = NULL;
|
|
|
|
|
|
|
|
/* An RST received during the 3-way connection handshake requires
|
|
|
|
* little more clean-up.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_SYN_RCVD)
|
|
|
|
{
|
|
|
|
conn->tcpstateflags = TCP_CLOSED;
|
|
|
|
nwarn("WARNING: RESET in TCP_SYN_RCVD\n");
|
|
|
|
|
2017-05-16 15:38:57 +02:00
|
|
|
/* Notify the listener for the connection of the reset event */
|
2017-05-15 16:10:43 +02:00
|
|
|
|
2021-09-23 08:55:19 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
# ifdef CONFIG_NET_IPv4
|
|
|
|
if (domain == PF_INET6)
|
|
|
|
# endif
|
|
|
|
{
|
|
|
|
net_ipv6addr_copy(&uaddr.ipv6.laddr, IPv6BUF->destipaddr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
# ifdef CONFIG_NET_IPv6
|
|
|
|
if (domain == PF_INET)
|
|
|
|
# endif
|
|
|
|
{
|
|
|
|
net_ipv4addr_copy(uaddr.ipv4.laddr,
|
|
|
|
net_ip4addr_conv32(IPv4BUF->destipaddr));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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(&uaddr, conn->lport, domain);
|
2017-05-25 09:48:55 +02:00
|
|
|
#else
|
2021-09-23 08:55:19 +02:00
|
|
|
listener = tcp_findlistener(&uaddr, conn->lport);
|
2017-05-25 09:48:55 +02:00
|
|
|
#endif
|
2017-05-15 16:10:43 +02:00
|
|
|
|
2017-05-16 15:38:57 +02:00
|
|
|
/* We must free this TCP connection structure; this connection
|
2017-08-26 18:00:47 +02:00
|
|
|
* will never be established. There should only be one reference
|
|
|
|
* on this connection when we allocated for the connection.
|
2017-05-15 16:10:43 +02:00
|
|
|
*/
|
|
|
|
|
2017-08-26 18:00:47 +02:00
|
|
|
DEBUGASSERT(conn->crefs == 1);
|
|
|
|
conn->crefs = 0;
|
2017-05-15 16:10:43 +02:00
|
|
|
tcp_free(conn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
conn->tcpstateflags = TCP_CLOSED;
|
|
|
|
nwarn("WARNING: RESET TCP state: TCP_CLOSED\n");
|
|
|
|
|
|
|
|
/* Notify this connection of the reset event */
|
|
|
|
|
|
|
|
listener = conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the TCP_ABORT callback and drop the packet */
|
|
|
|
|
|
|
|
if (listener != NULL)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
tcp_callback(dev, listener, TCP_ABORT);
|
2017-05-15 16:10:43 +02:00
|
|
|
}
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculated the length of the data, if the application has sent
|
|
|
|
* any data to us.
|
|
|
|
*/
|
|
|
|
|
2015-01-15 22:06:46 +01:00
|
|
|
len = (tcp->tcpoffset >> 4) << 2;
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2021-07-22 11:01:46 +02:00
|
|
|
/* d_appdata should remove the tcp specific option field. */
|
|
|
|
|
|
|
|
if ((tcp->tcpoffset & 0xf0) > 0x50)
|
|
|
|
{
|
|
|
|
if (dev->d_len >= len)
|
|
|
|
{
|
2022-07-15 11:10:37 +02:00
|
|
|
dev->d_appdata += len - TCP_HDRLEN;
|
2021-07-22 11:01:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-15 11:10:37 +02:00
|
|
|
/* d_len will contain the length of the actual TCP data. This is
|
|
|
|
* calculated by subtracting the length of the TCP header (in
|
|
|
|
* len) and the length of the IP header.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dev->d_len -= (len + iplen);
|
|
|
|
|
2023-01-13 06:40:08 +01:00
|
|
|
#if defined(CONFIG_NET_STATISTICS) && \
|
|
|
|
defined(CONFIG_NET_TCP_DEBUG_DROP_RECV)
|
|
|
|
|
|
|
|
#pragma message \
|
|
|
|
"CONFIG_NET_TCP_DEBUG_DROP_RECV is selected, this is debug " \
|
|
|
|
"feature to drop the tcp received packet on the floor, " \
|
|
|
|
"please confirm the configuration again if you do not want " \
|
|
|
|
"debug the TCP stack."
|
|
|
|
|
|
|
|
/* Debug feature to drop the tcp received packet on the floor */
|
|
|
|
|
|
|
|
if (dev->d_len > 0)
|
|
|
|
{
|
|
|
|
if ((g_netstats.tcp.recv %
|
|
|
|
CONFIG_NET_TCP_DEBUG_DROP_RECV_PROBABILITY) == 0)
|
|
|
|
{
|
|
|
|
uint32_t seq = tcp_getsequence(tcp->seqno);
|
|
|
|
|
|
|
|
g_netstats.tcp.drop++;
|
|
|
|
|
|
|
|
ninfo("TCP DROP RCVPKT: "
|
|
|
|
"[%d][%" PRIu32 " : %" PRIu32 " : %d]\n",
|
|
|
|
g_netstats.tcp.drop, seq, TCP_SEQ_ADD(seq, dev->d_len),
|
|
|
|
dev->d_len);
|
|
|
|
|
|
|
|
dev->d_len = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-12 22:54:21 +01:00
|
|
|
/* Check if the incoming segment acknowledges any outstanding data. If so,
|
|
|
|
* we update the sequence number, reset the length of the outstanding
|
|
|
|
* data, calculate RTT estimations, and reset the retransmission timer.
|
2007-11-07 19:54:35 +01:00
|
|
|
*/
|
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
if ((tcp->flags & TCP_ACK) != 0 && conn->tx_unacked > 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2010-11-27 19:01:54 +01:00
|
|
|
uint32_t unackseq;
|
|
|
|
uint32_t ackseq;
|
2024-08-22 15:35:16 +02:00
|
|
|
int timeout;
|
2022-01-20 22:44:30 +01:00
|
|
|
|
2009-12-19 23:05:12 +01:00
|
|
|
/* The next sequence number is equal to the current sequence
|
2014-04-12 20:13:01 +02:00
|
|
|
* number (sndseq) plus the size of the outstanding, unacknowledged
|
2019-12-08 20:13:51 +01:00
|
|
|
* data (tx_unacked).
|
2009-12-19 23:05:12 +01:00
|
|
|
*/
|
|
|
|
|
2022-01-20 17:21:46 +01:00
|
|
|
#if defined(CONFIG_NET_TCP_WRITE_BUFFERS) && !defined(CONFIG_NET_SENDFILE)
|
While working with version 7.10 I discovered a problem in TCP stack that could be observed on high network load. Generally speaking, the problem is that RST flag is set in unnecessary case, in which between loss of some TCP packet and its proper retransmission, another packets had been successfully sent. The scenario is as follows: NuttX did not receive ACK for some sent packet, so it has been probably lost somewhere. But before its retransmission starts, NuttX is correctly issuing next TCP packets, with sequence numbers increasing properly. When the retransmission of previously lost packet finally succeeds, tcp_input receives the accumulated ACK value, which acknowledges also the packets sent in the meantime (i.e. between unsuccessful sending of lost packet and its proper retransmission). However, variable unackseq is still set to conn->isn + conn->sent, which is truth only if no further packets transmission occurred in the meantime. Because of incorrect (in such specific case) unackseq value, few lines further condition if (ackseq <= unackseq)is not met, and, as a result, we are going to reset label.
2016-06-20 14:55:29 +02:00
|
|
|
unackseq = conn->sndseq_max;
|
2022-01-20 17:21:46 +01:00
|
|
|
#elif defined(CONFIG_NET_TCP_WRITE_BUFFERS) && defined(CONFIG_NET_SENDFILE)
|
|
|
|
if (!conn->sendfile)
|
|
|
|
{
|
|
|
|
unackseq = conn->sndseq_max;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unackseq = tcp_getsequence(conn->sndseq);
|
|
|
|
}
|
2014-01-14 15:30:35 +01:00
|
|
|
#else
|
2021-10-10 04:56:51 +02:00
|
|
|
unackseq = tcp_getsequence(conn->sndseq);
|
2014-01-14 15:30:35 +01:00
|
|
|
#endif
|
2009-12-19 23:05:12 +01:00
|
|
|
|
2010-11-27 19:01:54 +01:00
|
|
|
/* Get the sequence number of that has just been acknowledged by this
|
|
|
|
* incoming packet.
|
|
|
|
*/
|
|
|
|
|
2015-01-15 22:06:46 +01:00
|
|
|
ackseq = tcp_getsequence(tcp->ackno);
|
2010-11-27 19:01:54 +01:00
|
|
|
|
|
|
|
/* Check how many of the outstanding bytes have been acknowledged. For
|
2016-05-30 17:31:44 +02:00
|
|
|
* most send operations, this should always be true. However,
|
2009-12-19 23:05:12 +01:00
|
|
|
* the send() API sends data ahead when it can without waiting for
|
2010-11-27 19:01:54 +01:00
|
|
|
* the ACK. In this case, the 'ackseq' could be less than then the
|
2009-12-19 23:05:12 +01:00
|
|
|
* new sequence number.
|
|
|
|
*/
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2021-06-03 09:42:02 +02:00
|
|
|
if (TCP_SEQ_LTE(ackseq, unackseq))
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-04-12 20:13:01 +02:00
|
|
|
/* Calculate the new number of outstanding, unacknowledged bytes */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
conn->tx_unacked = unackseq - ackseq;
|
2010-11-27 19:01:54 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* What would it mean if ackseq > unackseq? The peer has ACKed
|
|
|
|
* more bytes than we think we have sent? Someone has lost it.
|
2014-01-14 15:30:35 +01:00
|
|
|
* Complain and reset the number of outstanding, unacknowledged
|
2010-11-27 19:01:54 +01:00
|
|
|
* bytes
|
|
|
|
*/
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_ESTABLISHED)
|
2014-01-14 15:30:35 +01:00
|
|
|
{
|
2017-06-25 21:00:42 +02:00
|
|
|
nwarn("WARNING: ackseq > unackseq\n");
|
2020-11-25 11:43:29 +01:00
|
|
|
nwarn("sndseq=%" PRIu32 " tx_unacked=%" PRIu32
|
|
|
|
" unackseq=%" PRIu32 " ackseq=%" PRIu32 "\n",
|
2020-11-25 22:47:31 +01:00
|
|
|
tcp_getsequence(conn->sndseq),
|
|
|
|
(uint32_t)conn->tx_unacked,
|
2020-05-04 19:34:04 +02:00
|
|
|
unackseq, ackseq);
|
2018-10-24 17:10:56 +02:00
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
conn->tx_unacked = 0;
|
2014-01-14 15:30:35 +01:00
|
|
|
}
|
2010-11-27 19:01:54 +01:00
|
|
|
}
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2021-12-28 21:18:55 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
2022-01-20 22:44:30 +01:00
|
|
|
#ifdef CONFIG_NET_SENDFILE
|
|
|
|
if (!conn->sendfile)
|
|
|
|
#endif
|
2021-07-27 06:55:04 +02:00
|
|
|
{
|
2022-01-20 22:44:30 +01:00
|
|
|
/* Update sequence number to the unacknowledge sequence number. If
|
|
|
|
* there is still outstanding, unacknowledged data, then this will
|
|
|
|
* be beyond ackseq.
|
|
|
|
*/
|
|
|
|
|
|
|
|
uint32_t sndseq = tcp_getsequence(conn->sndseq);
|
|
|
|
if (TCP_SEQ_LT(sndseq, ackseq))
|
|
|
|
{
|
|
|
|
ninfo("sndseq: %08" PRIx32 "->%08" PRIx32
|
|
|
|
" unackseq: %08" PRIx32 " new tx_unacked: %" PRIu32 "\n",
|
|
|
|
tcp_getsequence(conn->sndseq), ackseq, unackseq,
|
|
|
|
(uint32_t)conn->tx_unacked);
|
|
|
|
tcp_setsequence(conn->sndseq, ackseq);
|
2022-07-26 08:03:48 +02:00
|
|
|
conn->nrtx = 0;
|
2022-01-20 22:44:30 +01:00
|
|
|
}
|
2021-07-27 06:55:04 +02:00
|
|
|
}
|
2021-12-28 21:18:55 +01:00
|
|
|
#endif
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2010-11-27 19:01:54 +01:00
|
|
|
/* Do RTT estimation, unless we have done retransmissions. */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2010-11-27 19:01:54 +01:00
|
|
|
if (conn->nrtx == 0)
|
|
|
|
{
|
|
|
|
signed char m;
|
|
|
|
m = conn->rto - conn->timer;
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2010-11-27 19:01:54 +01:00
|
|
|
/* This is taken directly from VJs original code in his paper */
|
|
|
|
|
|
|
|
m = m - (conn->sa >> 3);
|
|
|
|
conn->sa += m;
|
|
|
|
if (m < 0)
|
|
|
|
{
|
|
|
|
m = -m;
|
|
|
|
}
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2010-11-27 19:01:54 +01:00
|
|
|
m = m - (conn->sv >> 2);
|
|
|
|
conn->sv += m;
|
|
|
|
conn->rto = (conn->sa >> 3) + conn->sv;
|
|
|
|
}
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
/* Set the acknowledged flag. */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
flags |= TCP_ACKDATA;
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2024-08-22 15:35:16 +02:00
|
|
|
/* Check if no packet need to retransmission, clear timer. */
|
|
|
|
|
|
|
|
if (conn->tx_unacked == 0 && conn->tcpstateflags == TCP_ESTABLISHED)
|
|
|
|
{
|
|
|
|
timeout = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
timeout = conn->rto;
|
|
|
|
}
|
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
/* Reset the retransmission timer. */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2024-08-22 15:35:16 +02:00
|
|
|
tcp_update_retrantimer(conn, timeout);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 14:45:55 +02:00
|
|
|
/* Check if the sequence number of the incoming packet is what we are
|
|
|
|
* expecting next. If not, we send out an ACK with the correct numbers
|
|
|
|
* in, unless we are in the SYN_RCVD state and receive a SYN, in which
|
|
|
|
* case we should retransmit our SYNACK (which is done further down).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!((((conn->tcpstateflags & TCP_STATE_MASK) == TCP_SYN_SENT) &&
|
|
|
|
((tcp->flags & TCP_CTL) == (TCP_SYN | TCP_ACK))) ||
|
|
|
|
(((conn->tcpstateflags & TCP_STATE_MASK) == TCP_SYN_RCVD) &&
|
|
|
|
((tcp->flags & TCP_CTL) == TCP_SYN))))
|
|
|
|
{
|
|
|
|
uint32_t seq;
|
|
|
|
uint32_t rcvseq;
|
|
|
|
|
|
|
|
seq = tcp_getsequence(tcp->seqno);
|
|
|
|
rcvseq = tcp_getsequence(conn->rcvseq);
|
|
|
|
|
|
|
|
if (seq != rcvseq)
|
|
|
|
{
|
|
|
|
/* Trim the head of the segment */
|
|
|
|
|
|
|
|
if (TCP_SEQ_LT(seq, rcvseq))
|
|
|
|
{
|
|
|
|
uint32_t trimlen = TCP_SEQ_SUB(rcvseq, seq);
|
|
|
|
|
|
|
|
if (tcp_trim_head(dev, tcp, trimlen))
|
|
|
|
{
|
|
|
|
/* The segment was completely out of the window.
|
|
|
|
* E.g. a retransmit which was not necessary.
|
|
|
|
* E.g. a keep-alive segment.
|
|
|
|
*/
|
|
|
|
|
|
|
|
tcp_send(dev, conn, TCP_ACK, tcpiplen);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_TCP_OUT_OF_ORDER
|
|
|
|
/* Queue out-of-order segments. */
|
|
|
|
|
|
|
|
tcp_input_ofosegs(dev, conn, iplen);
|
|
|
|
#endif
|
2023-08-31 13:31:31 +02:00
|
|
|
if ((conn->tcpstateflags & TCP_STATE_MASK) <= TCP_ESTABLISHED)
|
|
|
|
{
|
|
|
|
tcp_send(dev, conn, TCP_ACK, tcpiplen);
|
|
|
|
return;
|
|
|
|
}
|
2023-08-30 14:45:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 13:43:37 +02:00
|
|
|
tcp_clear_zero_probe(conn, tcp);
|
|
|
|
|
2021-07-16 12:47:08 +02:00
|
|
|
/* Update the connection's window size */
|
|
|
|
|
|
|
|
if ((tcp->flags & TCP_ACK) != 0 &&
|
|
|
|
(conn->tcpstateflags & TCP_STATE_MASK) != TCP_SYN_RCVD)
|
|
|
|
{
|
2023-04-20 05:08:02 +02:00
|
|
|
#ifdef CONFIG_NET_TCP_CC_NEWRENO
|
|
|
|
/* If the packet is ack, update the cc var. */
|
|
|
|
|
|
|
|
tcp_cc_recv_ack(conn, tcp);
|
|
|
|
#endif
|
2023-02-01 13:07:47 +01:00
|
|
|
if (tcp_snd_wnd_update(conn, tcp))
|
|
|
|
{
|
|
|
|
/* Window updated, set the acknowledged flag. */
|
|
|
|
|
|
|
|
flags |= TCP_ACKDATA;
|
|
|
|
}
|
2021-07-16 12:47:08 +02:00
|
|
|
}
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/* Do different things depending on in what state the connection is. */
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
switch (conn->tcpstateflags & TCP_STATE_MASK)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* CLOSED and LISTEN are not handled here. CLOSE_WAIT is not
|
|
|
|
* implemented, since we force the application to close when the
|
|
|
|
* peer sends a FIN (hence the application goes directly from
|
|
|
|
* ESTABLISHED to LAST_ACK).
|
|
|
|
*/
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
case TCP_SYN_RCVD:
|
2007-11-07 19:54:35 +01:00
|
|
|
/* In SYN_RCVD we have sent out a SYNACK in response to a SYN, and
|
|
|
|
* we are waiting for an ACK that acknowledges the data we sent
|
2014-07-07 01:22:02 +02:00
|
|
|
* out the last time. Therefore, we want to have the TCP_ACKDATA
|
2007-11-07 19:54:35 +01:00
|
|
|
* flag set. If so, we enter the ESTABLISHED state.
|
|
|
|
*/
|
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
if ((flags & TCP_ACKDATA) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2017-05-14 18:56:25 +02:00
|
|
|
/* The three way handshake is complete and the TCP connection
|
|
|
|
* is now in the ESTABLISHED state.
|
|
|
|
*/
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
conn->tcpstateflags = TCP_ESTABLISHED;
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2017-05-14 18:56:25 +02:00
|
|
|
/* Wake up any listener waiting for a connection on this port */
|
|
|
|
|
2017-05-14 17:08:32 +02:00
|
|
|
if (tcp_accept_connection(dev, conn, tcp->destport) != OK)
|
|
|
|
{
|
|
|
|
/* No more listener for current port. We can free conn here
|
|
|
|
* because it has not been shared with upper layers yet as
|
|
|
|
* handshake is not complete
|
|
|
|
*/
|
|
|
|
|
2019-03-11 19:48:17 +01:00
|
|
|
nwarn("WARNING: Listen canceled while waiting for ACK on "
|
2022-01-18 08:38:00 +01:00
|
|
|
"port %d\n", NTOHS(tcp->destport));
|
2017-05-14 21:30:59 +02:00
|
|
|
|
|
|
|
/* Free the connection structure */
|
|
|
|
|
2017-05-14 17:08:32 +02:00
|
|
|
conn->crefs = 0;
|
|
|
|
tcp_free(conn);
|
|
|
|
conn = NULL;
|
2017-05-14 21:30:59 +02:00
|
|
|
|
|
|
|
/* And send a reset packet to the remote host. */
|
|
|
|
|
|
|
|
goto reset;
|
2017-05-14 17:08:32 +02:00
|
|
|
}
|
|
|
|
|
2014-01-14 15:30:35 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
2015-01-15 22:06:46 +01:00
|
|
|
conn->isn = tcp_getsequence(tcp->ackno);
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_setsequence(conn->sndseq, conn->isn);
|
2014-01-14 15:30:35 +01:00
|
|
|
conn->sent = 0;
|
While working with version 7.10 I discovered a problem in TCP stack that could be observed on high network load. Generally speaking, the problem is that RST flag is set in unnecessary case, in which between loss of some TCP packet and its proper retransmission, another packets had been successfully sent. The scenario is as follows: NuttX did not receive ACK for some sent packet, so it has been probably lost somewhere. But before its retransmission starts, NuttX is correctly issuing next TCP packets, with sequence numbers increasing properly. When the retransmission of previously lost packet finally succeeds, tcp_input receives the accumulated ACK value, which acknowledges also the packets sent in the meantime (i.e. between unsuccessful sending of lost packet and its proper retransmission). However, variable unackseq is still set to conn->isn + conn->sent, which is truth only if no further packets transmission occurred in the meantime. Because of incorrect (in such specific case) unackseq value, few lines further condition if (ackseq <= unackseq)is not met, and, as a result, we are going to reset label.
2016-06-20 14:55:29 +02:00
|
|
|
conn->sndseq_max = 0;
|
2014-01-14 15:30:35 +01:00
|
|
|
#endif
|
2019-12-08 20:13:51 +01:00
|
|
|
conn->tx_unacked = 0;
|
2021-07-16 12:47:08 +02:00
|
|
|
tcp_snd_wnd_init(conn, tcp);
|
|
|
|
tcp_snd_wnd_update(conn, tcp);
|
|
|
|
|
2023-04-20 05:08:02 +02:00
|
|
|
#ifdef CONFIG_NET_TCP_CC_NEWRENO
|
|
|
|
tcp_cc_update(conn, tcp);
|
|
|
|
#endif
|
2014-07-07 01:22:02 +02:00
|
|
|
flags = TCP_CONNECTED;
|
2016-06-20 17:37:08 +02:00
|
|
|
ninfo("TCP state: TCP_ESTABLISHED\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
if (dev->d_len > 0)
|
|
|
|
{
|
2014-07-07 01:22:02 +02:00
|
|
|
flags |= TCP_NEWDATA;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2007-11-08 17:01:18 +01:00
|
|
|
dev->d_sndlen = 0;
|
2014-06-25 02:12:49 +02:00
|
|
|
result = tcp_callback(dev, conn, flags);
|
|
|
|
tcp_appsend(dev, conn, result);
|
2007-11-08 17:01:18 +01:00
|
|
|
return;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
2011-01-23 18:40:10 +01:00
|
|
|
|
|
|
|
/* We need to retransmit the SYNACK */
|
|
|
|
|
2015-01-15 22:06:46 +01:00
|
|
|
if ((tcp->flags & TCP_CTL) == TCP_SYN)
|
2011-01-23 18:40:10 +01:00
|
|
|
{
|
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);
|
2011-01-23 18:40:10 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-04-12 20:13:01 +02:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
case TCP_SYN_SENT:
|
2007-11-07 19:54:35 +01:00
|
|
|
/* In SYN_SENT, we wait for a SYNACK that is sent in response to
|
2010-11-27 02:01:26 +01:00
|
|
|
* our SYN. The rcvseq is set to sequence number in the SYNACK
|
2007-11-07 19:54:35 +01:00
|
|
|
* plus one, and we send an ACK. We move into the ESTABLISHED
|
|
|
|
* state.
|
|
|
|
*/
|
|
|
|
|
2019-03-11 19:48:17 +01:00
|
|
|
if ((flags & TCP_ACKDATA) != 0 &&
|
|
|
|
(tcp->flags & TCP_CTL) == (TCP_SYN | TCP_ACK))
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* Parse the TCP MSS option, if present. */
|
|
|
|
|
2023-01-09 12:44:23 +01:00
|
|
|
tcp_parse_option(dev, conn, iplen);
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
conn->tcpstateflags = TCP_ESTABLISHED;
|
2015-01-15 22:06:46 +01:00
|
|
|
memcpy(conn->rcvseq, tcp->seqno, 4);
|
2021-06-01 09:57:00 +02:00
|
|
|
conn->rcv_adv = tcp_getsequence(conn->rcvseq);
|
2021-07-16 12:47:08 +02:00
|
|
|
tcp_snd_wnd_init(conn, tcp);
|
|
|
|
tcp_snd_wnd_update(conn, tcp);
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2023-04-20 05:08:02 +02:00
|
|
|
#ifdef CONFIG_NET_TCP_CC_NEWRENO
|
|
|
|
tcp_cc_update(conn, tcp);
|
|
|
|
#endif
|
2021-06-17 06:27:18 +02:00
|
|
|
net_incr32(conn->rcvseq, 1); /* ack SYN */
|
2019-12-08 20:13:51 +01:00
|
|
|
conn->tx_unacked = 0;
|
2014-01-14 15:30:35 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
2015-01-15 22:06:46 +01:00
|
|
|
conn->isn = tcp_getsequence(tcp->ackno);
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_setsequence(conn->sndseq, conn->isn);
|
2014-01-14 15:30:35 +01:00
|
|
|
#endif
|
2007-11-08 17:01:18 +01:00
|
|
|
dev->d_len = 0;
|
|
|
|
dev->d_sndlen = 0;
|
2014-01-14 15:30:35 +01:00
|
|
|
|
2016-06-20 17:37:08 +02:00
|
|
|
ninfo("TCP state: TCP_ESTABLISHED\n");
|
2014-07-07 01:22:02 +02:00
|
|
|
result = tcp_callback(dev, conn, TCP_CONNECTED | TCP_NEWDATA);
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_appsend(dev, conn, result);
|
2007-11-08 17:01:18 +01:00
|
|
|
return;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Inform the application that the connection failed */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
tcp_callback(dev, conn, TCP_ABORT);
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/* The connection is closed after we send the RST */
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
conn->tcpstateflags = TCP_CLOSED;
|
2016-06-20 17:37:08 +02:00
|
|
|
ninfo("Connection failed - TCP state: TCP_CLOSED\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/* We do not send resets in response to resets. */
|
|
|
|
|
2015-01-15 22:06:46 +01:00
|
|
|
if ((tcp->flags & TCP_RST) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
goto drop;
|
|
|
|
}
|
2014-04-12 20:13:01 +02:00
|
|
|
|
2022-12-21 05:40:42 +01:00
|
|
|
tcp_reset(dev, conn);
|
2007-11-08 17:01:18 +01:00
|
|
|
return;
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
case TCP_ESTABLISHED:
|
2007-11-07 19:54:35 +01:00
|
|
|
/* In the ESTABLISHED state, we call upon the application to feed
|
2018-03-12 17:59:46 +01:00
|
|
|
* data into the d_buf. If the TCP_ACKDATA flag is set, the
|
2007-11-07 19:54:35 +01:00
|
|
|
* application should put new data into the buffer, otherwise we are
|
|
|
|
* retransmitting an old segment, and the application should put that
|
|
|
|
* data into the buffer.
|
|
|
|
*
|
|
|
|
* If the incoming packet is a FIN, we should close the connection on
|
|
|
|
* this side as well, and we send out a FIN and enter the LAST_ACK
|
2020-05-04 19:34:04 +02:00
|
|
|
* state. We require that there is no outstanding data; otherwise
|
|
|
|
* the sequence numbers will be screwed up.
|
2007-11-07 19:54:35 +01:00
|
|
|
*/
|
|
|
|
|
2019-03-11 19:48:17 +01:00
|
|
|
if ((tcp->flags & TCP_FIN) != 0 &&
|
|
|
|
(conn->tcpstateflags & TCP_STOPPED) == 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2013-10-17 17:45:38 +02:00
|
|
|
/* Needs to be investigated further.
|
|
|
|
* Windows often sends FIN packets together with the last ACK for
|
2020-05-04 19:34:04 +02:00
|
|
|
* the received data. So the socket layer has to get this ACK
|
|
|
|
* even if the connection is going to be closed.
|
2013-10-17 17:45:38 +02:00
|
|
|
*/
|
2020-01-11 16:40:32 +01:00
|
|
|
|
2013-10-17 17:45:38 +02:00
|
|
|
#if 0
|
2019-12-08 20:13:51 +01:00
|
|
|
if (conn->tx_unacked > 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
goto drop;
|
|
|
|
}
|
2013-10-17 17:45:38 +02:00
|
|
|
#endif
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2020-05-04 19:34:04 +02:00
|
|
|
/* Update the sequence number and indicate that the connection
|
|
|
|
* has been closed.
|
2012-02-23 16:53:27 +01:00
|
|
|
*/
|
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
flags |= TCP_CLOSE;
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
if (dev->d_len > 0)
|
|
|
|
{
|
2014-07-07 01:22:02 +02:00
|
|
|
flags |= TCP_NEWDATA;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 06:27:18 +02:00
|
|
|
result = tcp_callback(dev, conn, flags);
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2021-06-17 06:27:18 +02:00
|
|
|
if ((result & TCP_CLOSE) != 0)
|
|
|
|
{
|
|
|
|
conn->tcpstateflags = TCP_LAST_ACK;
|
|
|
|
conn->tx_unacked = 1;
|
|
|
|
conn->nrtx = 0;
|
|
|
|
net_incr32(conn->rcvseq, 1); /* ack FIN */
|
2019-08-30 14:43:45 +02:00
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
2021-06-17 06:27:18 +02:00
|
|
|
conn->sndseq_max = tcp_getsequence(conn->sndseq) + 1;
|
2019-08-30 14:43:45 +02:00
|
|
|
#endif
|
2021-06-17 06:27:18 +02:00
|
|
|
ninfo("TCP state: TCP_LAST_ACK\n");
|
|
|
|
tcp_send(dev, conn, TCP_FIN | TCP_ACK, tcpiplen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ninfo("TCP: Dropped a FIN\n");
|
|
|
|
tcp_appsend(dev, conn, result);
|
|
|
|
}
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2007-11-08 17:01:18 +01:00
|
|
|
return;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2020-11-30 08:16:12 +01:00
|
|
|
#ifdef CONFIG_NET_TCPURGDATA
|
|
|
|
/* Check the URG flag. If this is set, the segment carries urgent
|
2007-11-22 15:42:52 +01:00
|
|
|
* data that we must pass to the application.
|
|
|
|
*/
|
|
|
|
|
2015-01-15 22:06:46 +01:00
|
|
|
if ((tcp->flags & TCP_URG) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2015-01-15 22:06:46 +01:00
|
|
|
dev->d_urglen = (tcp->urgp[0] << 8) | tcp->urgp[1];
|
2007-11-22 15:42:52 +01:00
|
|
|
if (dev->d_urglen > dev->d_len)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* There is more urgent data in the next segment to come. */
|
|
|
|
|
2007-11-22 15:42:52 +01:00
|
|
|
dev->d_urglen = dev->d_len;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2020-11-30 08:16:12 +01:00
|
|
|
/* The d_len field contains the length of the incoming data.
|
|
|
|
* d_urgdata points to the "urgent" data at the beginning of
|
|
|
|
* the payload; d_appdata field points to the any "normal" data
|
|
|
|
* that may follow the urgent data.
|
|
|
|
*
|
|
|
|
* NOTE: If the urgent data continues in the next packet, then
|
|
|
|
* d_len will be zero and d_appdata will point past the end of
|
|
|
|
* the payload (which is OK).
|
|
|
|
*/
|
|
|
|
|
2014-06-28 01:51:32 +02:00
|
|
|
net_incr32(conn->rcvseq, dev->d_urglen);
|
2007-11-22 15:42:52 +01:00
|
|
|
dev->d_len -= dev->d_urglen;
|
|
|
|
dev->d_urgdata = dev->d_appdata;
|
|
|
|
dev->d_appdata += dev->d_urglen;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-30 08:16:12 +01:00
|
|
|
/* No urgent data */
|
|
|
|
|
2007-11-22 15:42:52 +01:00
|
|
|
dev->d_urglen = 0;
|
2020-11-30 08:16:12 +01:00
|
|
|
}
|
|
|
|
|
2007-11-22 15:42:52 +01:00
|
|
|
#else /* CONFIG_NET_TCPURGDATA */
|
2020-11-30 08:16:12 +01:00
|
|
|
/* Check the URG flag. If this is set, We must gracefully ignore
|
|
|
|
* and discard the urgent data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((tcp->flags & TCP_URG) != 0)
|
|
|
|
{
|
|
|
|
uint16_t urglen = (tcp->urgp[0] << 8) | tcp->urgp[1];
|
|
|
|
if (urglen > dev->d_len)
|
|
|
|
{
|
|
|
|
/* There is more urgent data in the next segment to come. */
|
|
|
|
|
|
|
|
urglen = dev->d_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The d_len field contains the length of the incoming data;
|
|
|
|
* The d_appdata field points to the any "normal" data that
|
|
|
|
* may follow the urgent data.
|
|
|
|
*
|
|
|
|
* NOTE: If the urgent data continues in the next packet, then
|
|
|
|
* d_len will be zero and d_appdata will point past the end of
|
|
|
|
* the payload (which is OK).
|
|
|
|
*/
|
|
|
|
|
|
|
|
net_incr32(conn->rcvseq, urglen);
|
|
|
|
dev->d_len -= urglen;
|
|
|
|
dev->d_appdata += urglen;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
2020-11-30 08:16:12 +01:00
|
|
|
#endif /* CONFIG_NET_TCPURGDATA */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2018-03-12 22:54:21 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_KEEPALIVE
|
2018-03-12 17:59:46 +01:00
|
|
|
/* If the established socket receives an ACK or any kind of data
|
|
|
|
* from the remote peer (whether we accept it or not), then reset
|
|
|
|
* the keep alive timer.
|
|
|
|
*/
|
|
|
|
|
2019-03-11 19:48:17 +01:00
|
|
|
if (conn->keepalive &&
|
|
|
|
(dev->d_len > 0 || (tcp->flags & TCP_ACK) != 0))
|
2018-03-12 17:59:46 +01:00
|
|
|
{
|
2022-05-18 07:32:56 +02:00
|
|
|
/* Reset the "alive" timer. */
|
2018-03-12 17:59:46 +01:00
|
|
|
|
2022-04-25 17:08:24 +02:00
|
|
|
tcp_update_keeptimer(conn, conn->keepidle);
|
2018-03-12 17:59:46 +01:00
|
|
|
conn->keepretries = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/* If d_len > 0 we have TCP data in the packet, and we flag this
|
2014-07-07 01:22:02 +02:00
|
|
|
* by setting the TCP_NEWDATA flag. If the application has stopped
|
2014-07-07 00:10:26 +02:00
|
|
|
* the data flow using TCP_STOPPED, we must not accept any data
|
2012-02-23 16:53:27 +01:00
|
|
|
* packets from the remote host.
|
2007-11-07 19:54:35 +01:00
|
|
|
*/
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
if (dev->d_len > 0 && (conn->tcpstateflags & TCP_STOPPED) == 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-07-07 01:22:02 +02:00
|
|
|
flags |= TCP_NEWDATA;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If this packet constitutes an ACK for outstanding data (flagged
|
2014-07-07 01:22:02 +02:00
|
|
|
* by the TCP_ACKDATA flag), we should call the application since it
|
2018-03-12 17:59:46 +01:00
|
|
|
* might want to send more data. If the incoming packet had data
|
2014-07-07 01:22:02 +02:00
|
|
|
* from the peer (as flagged by the TCP_NEWDATA flag), the
|
2007-11-07 19:54:35 +01:00
|
|
|
* application must also be notified.
|
|
|
|
*
|
|
|
|
* When the application is called, the d_len field
|
2018-03-12 17:59:46 +01:00
|
|
|
* contains the length of the incoming data. The application can
|
2007-11-07 19:54:35 +01:00
|
|
|
* access the incoming data through the global pointer
|
2015-01-16 22:03:10 +01:00
|
|
|
* d_appdata, which usually points hdrlen bytes into the d_buf
|
|
|
|
* array.
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* If the application wishes to send any data, this data should be
|
|
|
|
* put into the d_appdata and the length of the data should be
|
2018-03-12 17:59:46 +01:00
|
|
|
* put into d_len. If the application don't have any data to
|
2007-11-07 19:54:35 +01:00
|
|
|
* send, d_len must be set to 0.
|
|
|
|
*/
|
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
if ((flags & (TCP_NEWDATA | TCP_ACKDATA)) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
dev->d_sndlen = 0;
|
2012-02-23 16:53:27 +01:00
|
|
|
|
|
|
|
/* Provide the packet to the application */
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
result = tcp_callback(dev, conn, flags);
|
2012-02-23 16:53:27 +01:00
|
|
|
|
|
|
|
/* Send the response, ACKing the data or not, as appropriate */
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_appsend(dev, conn, result);
|
2007-11-08 17:01:18 +01:00
|
|
|
return;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
2014-04-12 20:13:01 +02:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
case TCP_LAST_ACK:
|
2007-11-07 19:54:35 +01:00
|
|
|
/* We can close this connection if the peer has acknowledged our
|
2014-07-07 01:22:02 +02:00
|
|
|
* FIN. This is indicated by the TCP_ACKDATA flag.
|
2007-11-07 19:54:35 +01:00
|
|
|
*/
|
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
if ((flags & TCP_ACKDATA) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-07-07 00:10:26 +02:00
|
|
|
conn->tcpstateflags = TCP_CLOSED;
|
2016-06-20 17:37:08 +02:00
|
|
|
ninfo("TCP_LAST_ACK TCP state: TCP_CLOSED\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
tcp_callback(dev, conn, TCP_CLOSE);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
case TCP_FIN_WAIT_1:
|
2007-11-07 19:54:35 +01:00
|
|
|
/* The application has closed the connection, but the remote host
|
2014-01-14 15:30:35 +01:00
|
|
|
* hasn't closed its end yet. Thus we stay in the FIN_WAIT_1 state
|
|
|
|
* until we receive a FIN from the remote.
|
2007-11-07 19:54:35 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->d_len > 0)
|
|
|
|
{
|
2014-06-28 01:51:32 +02:00
|
|
|
net_incr32(conn->rcvseq, dev->d_len);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
2010-11-27 19:01:54 +01:00
|
|
|
|
2015-01-15 22:06:46 +01:00
|
|
|
if ((tcp->flags & TCP_FIN) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2019-12-08 20:13:51 +01:00
|
|
|
if ((flags & TCP_ACKDATA) != 0 && conn->tx_unacked == 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-07-07 00:10:26 +02:00
|
|
|
conn->tcpstateflags = TCP_TIME_WAIT;
|
2022-04-25 17:08:24 +02:00
|
|
|
tcp_update_retrantimer(conn,
|
|
|
|
TCP_TIME_WAIT_TIMEOUT * HSEC_PER_SEC);
|
2016-06-20 17:37:08 +02:00
|
|
|
ninfo("TCP state: TCP_TIME_WAIT\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-07 00:10:26 +02:00
|
|
|
conn->tcpstateflags = TCP_CLOSING;
|
2016-06-20 17:37:08 +02:00
|
|
|
ninfo("TCP state: TCP_CLOSING\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 06:27:18 +02:00
|
|
|
net_incr32(conn->rcvseq, 1); /* ack FIN */
|
2020-01-02 17:49:34 +01:00
|
|
|
tcp_callback(dev, conn, TCP_CLOSE);
|
2015-01-15 22:06:46 +01:00
|
|
|
tcp_send(dev, conn, TCP_ACK, tcpiplen);
|
2007-11-08 17:01:18 +01:00
|
|
|
return;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
2019-12-08 20:13:51 +01:00
|
|
|
else if ((flags & TCP_ACKDATA) != 0 && conn->tx_unacked == 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-07-07 00:10:26 +02:00
|
|
|
conn->tcpstateflags = TCP_FIN_WAIT_2;
|
2016-06-20 17:37:08 +02:00
|
|
|
ninfo("TCP state: TCP_FIN_WAIT_2\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->d_len > 0)
|
|
|
|
{
|
2023-03-27 11:52:19 +02:00
|
|
|
/* Due to RFC 2525, Section 2.17, we SHOULD send RST if we can no
|
|
|
|
* longer read any received data. Also set state into TCP_CLOSED
|
|
|
|
* because the peer will not send FIN after RST received.
|
|
|
|
*
|
|
|
|
* TODO: Modify shutdown behavior to allow read in FIN_WAIT.
|
|
|
|
*/
|
|
|
|
|
|
|
|
conn->tcpstateflags = TCP_CLOSED;
|
2024-03-18 08:35:25 +01:00
|
|
|
|
|
|
|
/* In the TCP_FIN_WAIT_1, we need call tcp_close_eventhandler to
|
|
|
|
* release nofosegs, that we received in this state.
|
|
|
|
*/
|
|
|
|
|
|
|
|
tcp_callback(dev, conn, TCP_CLOSE);
|
2023-03-27 11:52:19 +02:00
|
|
|
tcp_reset(dev, conn);
|
2007-11-08 17:01:18 +01:00
|
|
|
return;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
2014-01-14 15:30:35 +01:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
case TCP_FIN_WAIT_2:
|
2007-11-07 19:54:35 +01:00
|
|
|
if (dev->d_len > 0)
|
|
|
|
{
|
2014-06-28 01:51:32 +02:00
|
|
|
net_incr32(conn->rcvseq, dev->d_len);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 22:06:46 +01:00
|
|
|
if ((tcp->flags & TCP_FIN) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-07-07 00:10:26 +02:00
|
|
|
conn->tcpstateflags = TCP_TIME_WAIT;
|
2022-04-25 17:08:24 +02:00
|
|
|
tcp_update_retrantimer(conn,
|
|
|
|
TCP_TIME_WAIT_TIMEOUT * HSEC_PER_SEC);
|
2016-06-20 17:37:08 +02:00
|
|
|
ninfo("TCP state: TCP_TIME_WAIT\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2021-06-17 06:27:18 +02:00
|
|
|
net_incr32(conn->rcvseq, 1); /* ack FIN */
|
2020-01-02 17:49:34 +01:00
|
|
|
tcp_callback(dev, conn, TCP_CLOSE);
|
2015-01-15 22:06:46 +01:00
|
|
|
tcp_send(dev, conn, TCP_ACK, tcpiplen);
|
2007-11-08 17:01:18 +01:00
|
|
|
return;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->d_len > 0)
|
|
|
|
{
|
2023-03-27 11:52:19 +02:00
|
|
|
/* Due to RFC 2525, Section 2.17, we SHOULD send RST if we can no
|
|
|
|
* longer read any received data. Also set state into TCP_CLOSED
|
|
|
|
* because the peer will not send FIN after RST received.
|
|
|
|
*/
|
|
|
|
|
|
|
|
conn->tcpstateflags = TCP_CLOSED;
|
2024-03-18 08:35:25 +01:00
|
|
|
|
|
|
|
/* In the TCP_FIN_WAIT_2, we need call tcp_close_eventhandler to
|
|
|
|
* release nofosegs, that we received in this state.
|
|
|
|
*/
|
|
|
|
|
|
|
|
tcp_callback(dev, conn, TCP_CLOSE);
|
2023-03-27 11:52:19 +02:00
|
|
|
tcp_reset(dev, conn);
|
2007-11-08 17:01:18 +01:00
|
|
|
return;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
2014-01-14 15:30:35 +01:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
case TCP_TIME_WAIT:
|
2015-01-15 22:06:46 +01:00
|
|
|
tcp_send(dev, conn, TCP_ACK, tcpiplen);
|
2007-11-08 17:01:18 +01:00
|
|
|
return;
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
case TCP_CLOSING:
|
2014-07-07 01:22:02 +02:00
|
|
|
if ((flags & TCP_ACKDATA) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-07-07 00:10:26 +02:00
|
|
|
conn->tcpstateflags = TCP_TIME_WAIT;
|
2022-04-25 17:08:24 +02:00
|
|
|
tcp_update_retrantimer(conn,
|
|
|
|
TCP_TIME_WAIT_TIMEOUT * HSEC_PER_SEC);
|
2016-06-20 17:37:08 +02:00
|
|
|
ninfo("TCP state: TCP_TIME_WAIT\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2007-11-08 17:01:18 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
drop:
|
|
|
|
dev->d_len = 0;
|
|
|
|
}
|
|
|
|
|
2015-01-15 22:06:46 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-01-10 06:41:02 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_reorder_ofosegs
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Sort out-of-order segments by left edge
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* nofosegs - Number of out-of-order semgnets
|
|
|
|
* ofosegs - Pointer to out-of-order segments
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* True if re-order occurs
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
bool tcp_reorder_ofosegs(int nofosegs, FAR struct tcp_ofoseg_s *ofosegs)
|
|
|
|
{
|
|
|
|
struct tcp_ofoseg_s segs;
|
|
|
|
bool reordered = false;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
/* Sort out-of-order segments by left edge */
|
|
|
|
|
|
|
|
for (i = 0; i < nofosegs - 1; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < nofosegs - 1 - i; j++)
|
|
|
|
{
|
|
|
|
if (TCP_SEQ_GT(ofosegs[j].left,
|
|
|
|
ofosegs[j + 1].left))
|
|
|
|
{
|
|
|
|
segs = ofosegs[j];
|
|
|
|
ofosegs[j] = ofosegs[j + 1];
|
|
|
|
ofosegs[j + 1] = segs;
|
|
|
|
reordered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return reordered;
|
|
|
|
}
|
|
|
|
|
2015-01-15 22:06:46 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_ipv4_input
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle incoming TCP input with IPv4 header
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-01-15 22:06:46 +01:00
|
|
|
* dev - The device driver structure containing the received TCP packet.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2015-01-15 22:06:46 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called from the Ethernet driver with the network stack locked
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
void tcp_ipv4_input(FAR struct net_driver_s *dev)
|
|
|
|
{
|
2019-08-31 20:25:30 +02:00
|
|
|
FAR struct ipv4_hdr_s *ipv4 = IPv4BUF;
|
|
|
|
uint16_t iphdrlen;
|
|
|
|
|
2015-01-17 14:42:09 +01:00
|
|
|
/* Configure to receive an TCP IPv4 packet */
|
|
|
|
|
|
|
|
tcp_ipv4_select(dev);
|
|
|
|
|
2019-08-31 20:25:30 +02:00
|
|
|
/* Get the IP header length (accounting for possible options). */
|
|
|
|
|
|
|
|
iphdrlen = (ipv4->vhl & IPv4_HLMASK) << 2;
|
|
|
|
|
2015-01-17 14:42:09 +01:00
|
|
|
/* Then process in the TCP IPv4 input */
|
|
|
|
|
2019-08-31 20:25:30 +02:00
|
|
|
tcp_input(dev, PF_INET, iphdrlen);
|
2015-01-15 22:06:46 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_ipv6_input
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle incoming TCP input with IPv4 header
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2018-11-03 00:50:01 +01:00
|
|
|
* dev - The device driver structure containing the received TCP packet.
|
|
|
|
* iplen - The size of the IPv6 header. This may be larger than
|
|
|
|
* IPv6_HDRLEN the IPv6 header if IPv6 extension headers are
|
|
|
|
* present.
|
2015-01-15 22:06:46 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2015-01-15 22:06:46 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called from the Ethernet driver with the network stack locked
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2018-11-03 00:50:01 +01:00
|
|
|
void tcp_ipv6_input(FAR struct net_driver_s *dev, unsigned int iplen)
|
2015-01-15 22:06:46 +01:00
|
|
|
{
|
2015-01-17 14:42:09 +01:00
|
|
|
/* Configure to receive an TCP IPv6 packet */
|
|
|
|
|
|
|
|
tcp_ipv6_select(dev);
|
|
|
|
|
|
|
|
/* Then process in the TCP IPv6 input */
|
|
|
|
|
2018-11-03 00:50:01 +01:00
|
|
|
tcp_input(dev, PF_INET6, iplen);
|
2015-01-15 22:06:46 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-11-22 19:36:46 +01:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_TCP */
|