2007-09-02 23:58:35 +02:00
|
|
|
/****************************************************************************
|
2014-06-18 18:18:53 +02:00
|
|
|
* net/tcp/tcp_conn.c
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
2014-01-13 20:55:51 +01:00
|
|
|
* Copyright (C) 2007-2011, 2013-2014 Gregory Nutt. All rights reserved.
|
2012-09-13 20:32:24 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
2007-09-03 01:11:10 +02:00
|
|
|
* Large parts of this file were leveraged from uIP logic:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2001-2003, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2007-09-02 23:58:35 +02:00
|
|
|
* 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
|
2007-09-03 01:11:10 +02:00
|
|
|
* 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.
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
2007-09-03 01:11:10 +02:00
|
|
|
* 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.
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2007-11-22 19:36:46 +01:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2009-12-15 15:53:45 +01:00
|
|
|
#include <stdint.h>
|
2007-09-02 23:58:35 +02:00
|
|
|
#include <string.h>
|
2009-12-17 16:01:22 +01:00
|
|
|
#include <assert.h>
|
2007-09-03 01:11:10 +02:00
|
|
|
#include <errno.h>
|
2007-11-06 00:04:16 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2007-09-02 23:58:35 +02:00
|
|
|
#include <arch/irq.h>
|
|
|
|
|
2014-06-24 16:53:28 +02:00
|
|
|
#include <nuttx/net/netconfig.h>
|
2014-07-05 00:38:51 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <nuttx/net/ip.h>
|
2014-06-26 17:32:39 +02:00
|
|
|
#include <nuttx/net/tcp.h>
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2014-06-29 02:07:02 +02:00
|
|
|
#include "devif/devif.h"
|
2014-07-05 00:38:51 +02:00
|
|
|
#include "tcp/tcp.h"
|
2007-09-02 23:58:35 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-09-03 01:11:10 +02:00
|
|
|
/* The array containing all uIP TCP connections. */
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
static struct tcp_conn_s g_tcp_connections[CONFIG_NET_TCP_CONNS];
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/* A list of all free TCP connections */
|
|
|
|
|
|
|
|
static dq_queue_t g_free_tcp_connections;
|
|
|
|
|
|
|
|
/* A list of all connected TCP connections */
|
|
|
|
|
|
|
|
static dq_queue_t g_active_tcp_connections;
|
|
|
|
|
2007-09-02 23:58:35 +02:00
|
|
|
/* Last port used by a TCP connection connection. */
|
|
|
|
|
2009-12-15 15:53:45 +01:00
|
|
|
static uint16_t g_last_tcp_port;
|
2007-09-02 23:58:35 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
/****************************************************************************
|
2014-06-29 03:26:16 +02:00
|
|
|
* Name: tcp_selectport()
|
2007-09-09 19:20:56 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2014-04-12 20:13:01 +02:00
|
|
|
* If the port number is zero; select an unused port for the connection.
|
|
|
|
* If the port number is non-zero, verify that no other connection has
|
2007-09-09 19:20:56 +02:00
|
|
|
* been created with this port number.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* portno -- the selected port number in host order. Zero means no port
|
|
|
|
* selected.
|
|
|
|
*
|
|
|
|
* Return:
|
2007-11-16 19:48:39 +01:00
|
|
|
* 0 on success, negated errno on failure:
|
|
|
|
*
|
|
|
|
* EADDRINUSE
|
|
|
|
* The given address is already in use.
|
|
|
|
* EADDRNOTAVAIL
|
|
|
|
* Cannot assign requested address (unlikely)
|
2007-09-09 19:20:56 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Interrupts are disabled
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
static int tcp_selectport(uint16_t portno)
|
2007-09-09 19:20:56 +02:00
|
|
|
{
|
|
|
|
if (portno == 0)
|
|
|
|
{
|
|
|
|
/* No local port assigned. Loop until we find a valid listen port number
|
2007-11-16 19:48:39 +01:00
|
|
|
* that is not being used by any other connection. NOTE the following loop
|
|
|
|
* is assumed to terminate but could not if all 32000-4096+1 ports are
|
|
|
|
* in used (unlikely).
|
2007-09-09 19:20:56 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Guess that the next available port number will be the one after
|
|
|
|
* the last port number assigned.
|
|
|
|
*/
|
|
|
|
portno = ++g_last_tcp_port;
|
|
|
|
|
|
|
|
/* Make sure that the port number is within range */
|
|
|
|
|
|
|
|
if (g_last_tcp_port >= 32000)
|
|
|
|
{
|
|
|
|
g_last_tcp_port = 4096;
|
|
|
|
}
|
|
|
|
}
|
2014-06-25 02:12:49 +02:00
|
|
|
while (tcp_listener(htons(g_last_tcp_port)));
|
2007-09-09 19:20:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* A port number has been supplied. Verify that no other TCP/IP
|
|
|
|
* connection is using this local port.
|
|
|
|
*/
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
if (tcp_listener(portno))
|
2007-09-09 19:20:56 +02:00
|
|
|
{
|
|
|
|
/* It is in use... return EADDRINUSE */
|
|
|
|
|
|
|
|
return -EADDRINUSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-16 19:48:39 +01:00
|
|
|
/* Return the selected or verified port number */
|
2007-09-09 19:20:56 +02:00
|
|
|
|
|
|
|
return portno;
|
|
|
|
}
|
|
|
|
|
2007-09-02 23:58:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 02:12:49 +02:00
|
|
|
* Name: tcp_initialize()
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2007-09-03 01:11:10 +02:00
|
|
|
* Initialize the TCP/IP connection structures. Called only once and only
|
2014-04-12 20:13:01 +02:00
|
|
|
* from the UIP layer at start-up in normal user mode.
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
void tcp_initialize(void)
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
|
|
|
int i;
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/* Initialize the queues */
|
|
|
|
|
|
|
|
dq_init(&g_free_tcp_connections);
|
|
|
|
dq_init(&g_active_tcp_connections);
|
|
|
|
|
|
|
|
/* Now initialize each connection structure */
|
|
|
|
|
2007-11-20 00:09:39 +01:00
|
|
|
for (i = 0; i < CONFIG_NET_TCP_CONNS; i++)
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
2007-09-03 22:34:44 +02:00
|
|
|
/* Mark the connection closed and move it to the free list */
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
g_tcp_connections[i].tcpstateflags = TCP_CLOSED;
|
2007-09-03 22:34:44 +02:00
|
|
|
dq_addlast(&g_tcp_connections[i].node, &g_free_tcp_connections);
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
g_last_tcp_port = 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 02:12:49 +02:00
|
|
|
* Name: tcp_alloc()
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a free TCP/IP connection structure and allocate it
|
|
|
|
* for use. This is normally something done by the implementation of the
|
|
|
|
* socket() API but is also called from the interrupt level when a TCP
|
|
|
|
* packet is received while "listening"
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
FAR struct tcp_conn_s *tcp_alloc(void)
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
2014-06-25 02:12:49 +02:00
|
|
|
FAR struct tcp_conn_s *conn;
|
2014-06-26 22:23:21 +02:00
|
|
|
net_lock_t flags;
|
2007-09-02 23:58:35 +02:00
|
|
|
|
|
|
|
/* Because this routine is called from both interrupt level and
|
|
|
|
* and from user level, we have not option but to disable interrupts
|
2007-09-03 22:34:44 +02:00
|
|
|
* while accessing g_free_tcp_connections[];
|
2007-09-02 23:58:35 +02:00
|
|
|
*/
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
flags = net_lock();
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/* Return the entry from the head of the free list */
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
conn = (FAR struct tcp_conn_s *)dq_remfirst(&g_free_tcp_connections);
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2014-06-02 15:48:05 +02:00
|
|
|
#ifndef CONFIG_NET_SOLINGER
|
2007-09-03 22:34:44 +02:00
|
|
|
/* Is the free list empty? */
|
|
|
|
|
|
|
|
if (!conn)
|
|
|
|
{
|
2014-04-12 20:13:01 +02:00
|
|
|
/* As a fall-back, check for connection structures which can be stalled.
|
2013-10-06 17:48:54 +02:00
|
|
|
*
|
|
|
|
* Search the active connection list for the oldest connection
|
2014-06-02 15:48:05 +02:00
|
|
|
* that is about to be closed anyway.
|
2007-09-02 23:58:35 +02:00
|
|
|
*/
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
FAR struct tcp_conn_s *tmp =
|
|
|
|
(FAR struct tcp_conn_s *)g_active_tcp_connections.head;
|
2013-11-29 00:47:03 +01:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
while (tmp)
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
2013-10-06 17:48:54 +02:00
|
|
|
nllvdbg("conn: %p state: %02x\n", tmp, tmp->tcpstateflags);
|
|
|
|
|
2014-06-02 15:48:05 +02:00
|
|
|
/* Is this connection in a state we can sacrifice. */
|
|
|
|
|
|
|
|
/* REVISIT: maybe we could check for SO_LINGER but it's buried
|
|
|
|
* in the socket layer.
|
2013-10-06 17:48:54 +02:00
|
|
|
*/
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
if (tmp->tcpstateflags == TCP_CLOSING ||
|
|
|
|
tmp->tcpstateflags == TCP_FIN_WAIT_1 ||
|
|
|
|
tmp->tcpstateflags == TCP_FIN_WAIT_2 ||
|
|
|
|
tmp->tcpstateflags == TCP_TIME_WAIT ||
|
|
|
|
tmp->tcpstateflags == TCP_LAST_ACK)
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
2013-10-06 17:48:54 +02:00
|
|
|
/* Yes.. Is it the oldest one we have seen so far? */
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
if (!conn || tmp->timer > conn->timer)
|
|
|
|
{
|
|
|
|
/* Yes.. remember it */
|
|
|
|
|
|
|
|
conn = tmp;
|
|
|
|
}
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/* Look at the next active connection */
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
tmp = (FAR struct tcp_conn_s *)tmp->node.flink;
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2013-10-06 17:48:54 +02:00
|
|
|
/* Did we find a connection that we can re-use? */
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2013-10-06 17:48:54 +02:00
|
|
|
if (conn != NULL)
|
|
|
|
{
|
|
|
|
nlldbg("Closing unestablished connection: %p\n", conn);
|
|
|
|
|
|
|
|
/* Yes... free it. This will remove the connection from the list
|
|
|
|
* of active connections and release all resources held by the
|
|
|
|
* connection.
|
|
|
|
*
|
|
|
|
* REVISIT: Could there be any higher level, socket interface
|
|
|
|
* that needs to be informed that we did this to them?
|
2014-06-02 15:48:05 +02:00
|
|
|
*
|
|
|
|
* Actually yes. When CONFIG_NET_SOLINGER is enabled there is a
|
|
|
|
* pending callback in netclose_disconnect waiting for getting
|
|
|
|
* woken up. Otherwise there's the callback too, but no one is
|
|
|
|
* waiting for it.
|
2013-10-06 17:48:54 +02:00
|
|
|
*/
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_free(conn);
|
2013-10-06 17:48:54 +02:00
|
|
|
|
|
|
|
/* Now there is guaranteed to be one free connection. Get it! */
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
conn = (FAR struct tcp_conn_s *)dq_remfirst(&g_free_tcp_connections);
|
2013-10-06 17:48:54 +02:00
|
|
|
}
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
2014-06-02 15:48:05 +02:00
|
|
|
#endif
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
net_unlock(flags);
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/* Mark the connection allocated */
|
|
|
|
|
|
|
|
if (conn)
|
|
|
|
{
|
2014-06-25 02:12:49 +02:00
|
|
|
memset(conn, 0, sizeof(struct tcp_conn_s));
|
2014-07-07 00:10:26 +02:00
|
|
|
conn->tcpstateflags = TCP_ALLOCATED;
|
2007-09-03 22:34:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return conn;
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 02:12:49 +02:00
|
|
|
* Name: tcp_free()
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a connection structure that is no longer in use. This should be
|
|
|
|
* done by the implementation of close()
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
void tcp_free(FAR struct tcp_conn_s *conn)
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
2014-06-29 20:59:34 +02:00
|
|
|
FAR struct devif_callback_s *cb;
|
|
|
|
FAR struct devif_callback_s *next;
|
2014-01-14 00:26:11 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
2014-06-21 23:23:39 +02:00
|
|
|
FAR struct tcp_wrbuffer_s *wrbuffer;
|
2007-11-20 00:35:42 +01:00
|
|
|
#endif
|
2014-06-26 22:23:21 +02:00
|
|
|
net_lock_t flags;
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/* Because g_free_tcp_connections is accessed from user level and interrupt
|
|
|
|
* level, code, it is necessary to keep interrupts disabled during this
|
|
|
|
* operation.
|
|
|
|
*/
|
|
|
|
|
2009-06-15 20:58:22 +02:00
|
|
|
DEBUGASSERT(conn->crefs == 0);
|
2014-06-26 22:23:21 +02:00
|
|
|
flags = net_lock();
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2013-10-08 17:14:27 +02:00
|
|
|
/* Free remaining callbacks, actually there should be only the close callback
|
|
|
|
* left.
|
|
|
|
*/
|
2013-10-06 17:48:54 +02:00
|
|
|
|
2013-10-08 17:14:27 +02:00
|
|
|
for (cb = conn->list; cb; cb = next)
|
2013-10-06 17:48:54 +02:00
|
|
|
{
|
2013-10-08 17:14:27 +02:00
|
|
|
next = cb->flink;
|
2014-06-29 02:36:09 +02:00
|
|
|
tcp_callback_free(conn, cb);
|
2013-10-06 17:48:54 +02:00
|
|
|
}
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
/* TCP_ALLOCATED means that that the connection is not in the active list
|
2007-09-03 22:34:44 +02:00
|
|
|
* yet.
|
|
|
|
*/
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
if (conn->tcpstateflags != TCP_ALLOCATED)
|
2007-09-03 22:34:44 +02:00
|
|
|
{
|
|
|
|
/* Remove the connection from the active list */
|
|
|
|
|
2007-11-20 22:55:06 +01:00
|
|
|
dq_rem(&conn->node, &g_active_tcp_connections);
|
2007-09-03 22:34:44 +02:00
|
|
|
}
|
|
|
|
|
2014-01-14 00:26:11 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_READAHEAD
|
2007-11-20 00:35:42 +01:00
|
|
|
/* Release any read-ahead buffers attached to the connection */
|
|
|
|
|
2014-06-24 23:38:00 +02:00
|
|
|
iob_free_queue(&conn->readahead);
|
2007-11-20 00:35:42 +01:00
|
|
|
#endif
|
|
|
|
|
2014-01-14 00:26:11 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
|
|
|
/* Release any write buffers attached to the connection */
|
|
|
|
|
2014-06-21 23:23:39 +02:00
|
|
|
while ((wrbuffer = (struct tcp_wrbuffer_s *)sq_remfirst(&conn->write_q)) != NULL)
|
2014-01-14 00:26:11 +01:00
|
|
|
{
|
2014-06-21 23:23:39 +02:00
|
|
|
tcp_wrbuffer_release(wrbuffer);
|
2014-01-14 00:26:11 +01:00
|
|
|
}
|
|
|
|
|
2014-06-21 23:23:39 +02:00
|
|
|
while ((wrbuffer = (struct tcp_wrbuffer_s *)sq_remfirst(&conn->unacked_q)) != NULL)
|
2014-01-14 00:26:11 +01:00
|
|
|
{
|
2014-06-21 23:23:39 +02:00
|
|
|
tcp_wrbuffer_release(wrbuffer);
|
2014-01-14 00:26:11 +01:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-20 20:24:06 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCPBACKLOG
|
2014-01-14 00:26:11 +01:00
|
|
|
/* Remove any backlog attached to this connection */
|
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
if (conn->backlog)
|
|
|
|
{
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_backlogdestroy(conn);
|
2008-11-20 20:24:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If this connection is, itself, backlogged, then remove it from the
|
|
|
|
* parent connection's backlog list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (conn->blparent)
|
|
|
|
{
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_backlogdelete(conn->blparent, conn);
|
2008-11-20 20:24:06 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/* Mark the connection available and put it into the free list */
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
conn->tcpstateflags = TCP_CLOSED;
|
2007-09-03 22:34:44 +02:00
|
|
|
dq_addlast(&conn->node, &g_free_tcp_connections);
|
2014-06-26 22:23:21 +02:00
|
|
|
net_unlock(flags);
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 02:12:49 +02:00
|
|
|
* Name: tcp_active()
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a connection structure that is the appropriate
|
2009-09-11 20:21:57 +02:00
|
|
|
* connection to be used with the provided TCP/IP header
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from UIP logic at interrupt level
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
FAR struct tcp_conn_s *tcp_active(struct tcp_iphdr_s *buf)
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
2014-06-25 02:12:49 +02:00
|
|
|
FAR struct tcp_conn_s *conn = (struct tcp_conn_s *)g_active_tcp_connections.head;
|
2014-06-29 21:20:00 +02:00
|
|
|
in_addr_t srcipaddr = net_ip4addr_conv32(buf->srcipaddr);
|
2007-11-06 00:04:16 +01:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
while (conn)
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
|
|
|
/* Find an open connection matching the tcp input */
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
if (conn->tcpstateflags != TCP_CLOSED &&
|
2007-11-06 00:04:16 +01:00
|
|
|
buf->destport == conn->lport && buf->srcport == conn->rport &&
|
2014-06-29 21:11:08 +02:00
|
|
|
net_ipaddr_cmp(srcipaddr, conn->ripaddr))
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
2007-09-03 22:34:44 +02:00
|
|
|
/* Matching connection found.. break out of the loop and return a
|
|
|
|
* reference to it.
|
|
|
|
*/
|
2007-09-03 01:11:10 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
break;
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/* Look at the next active connection */
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
conn = (FAR struct tcp_conn_s *)conn->node.flink;
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
2007-09-03 01:11:10 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
return conn;
|
|
|
|
}
|
2007-09-03 01:11:10 +02:00
|
|
|
|
2007-11-06 00:04:16 +01:00
|
|
|
/****************************************************************************
|
2014-07-01 02:03:58 +02:00
|
|
|
* Name: tcp_nextconn()
|
2007-11-06 00:04:16 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Traverse the list of active TCP connections
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from UIP logic at interrupt level (or with
|
|
|
|
* interrupts disabled).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-07-01 02:03:58 +02:00
|
|
|
FAR struct tcp_conn_s *tcp_nextconn(FAR struct tcp_conn_s *conn)
|
2007-11-06 00:04:16 +01:00
|
|
|
{
|
|
|
|
if (!conn)
|
|
|
|
{
|
2014-06-25 02:12:49 +02:00
|
|
|
return (FAR struct tcp_conn_s *)g_active_tcp_connections.head;
|
2007-11-06 00:04:16 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-25 02:12:49 +02:00
|
|
|
return (FAR struct tcp_conn_s *)conn->node.flink;
|
2007-11-06 00:04:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/****************************************************************************
|
2014-06-25 02:12:49 +02:00
|
|
|
* Name: tcp_listener()
|
2007-09-23 22:45:30 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2007-11-16 19:48:39 +01:00
|
|
|
* Given a local port number (in network byte order), find the TCP
|
|
|
|
* connection that listens on this this port.
|
2007-09-23 22:45:30 +02:00
|
|
|
*
|
|
|
|
* Primary uses: (1) to determine if a port number is available, (2) to
|
2014-04-12 20:13:01 +02:00
|
|
|
* To identify the socket that will accept new connections on a local port.
|
2007-09-23 22:45:30 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
FAR struct tcp_conn_s *tcp_listener(uint16_t portno)
|
2007-09-23 22:45:30 +02:00
|
|
|
{
|
2014-06-25 02:12:49 +02:00
|
|
|
FAR struct tcp_conn_s *conn;
|
2007-09-23 22:45:30 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Check if this port number is in use by any active UIP TCP connection */
|
2013-10-06 17:48:54 +02:00
|
|
|
|
2007-11-20 00:09:39 +01:00
|
|
|
for (i = 0; i < CONFIG_NET_TCP_CONNS; i++)
|
2007-09-23 22:45:30 +02:00
|
|
|
{
|
|
|
|
conn = &g_tcp_connections[i];
|
2014-07-07 00:10:26 +02:00
|
|
|
if (conn->tcpstateflags != TCP_CLOSED && conn->lport == portno)
|
2007-09-23 22:45:30 +02:00
|
|
|
{
|
2014-04-12 20:13:01 +02:00
|
|
|
/* The port number is in use, return the connection */
|
2007-09-23 22:45:30 +02:00
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
}
|
2013-10-06 17:48:54 +02:00
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 02:12:49 +02:00
|
|
|
* Name: tcp_alloc_accept()
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2014-07-03 01:23:25 +02:00
|
|
|
* Called when driver interrupt processing matches the incoming packet
|
|
|
|
* with a connection in LISTEN. In that case, this function will create
|
|
|
|
* a new connection and initialize it to send a SYNACK in return.
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from UIP logic at interrupt level
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
FAR struct tcp_conn_s *tcp_alloc_accept(FAR struct tcp_iphdr_s *buf)
|
2007-09-03 22:34:44 +02:00
|
|
|
{
|
2014-06-25 02:12:49 +02:00
|
|
|
FAR struct tcp_conn_s *conn = tcp_alloc();
|
2007-09-03 22:34:44 +02:00
|
|
|
if (conn)
|
|
|
|
{
|
|
|
|
/* Fill in the necessary fields for the new connection. */
|
|
|
|
|
2014-07-07 00:19:26 +02:00
|
|
|
conn->rto = TCP_RTO;
|
|
|
|
conn->timer = TCP_RTO;
|
2007-11-16 19:48:39 +01:00
|
|
|
conn->sa = 0;
|
|
|
|
conn->sv = 4;
|
|
|
|
conn->nrtx = 0;
|
|
|
|
conn->lport = buf->destport;
|
|
|
|
conn->rport = buf->srcport;
|
2014-07-05 21:04:48 +02:00
|
|
|
conn->mss = TCP_INITIAL_MSS;
|
2014-06-29 21:20:00 +02:00
|
|
|
net_ipaddr_copy(conn->ripaddr, net_ip4addr_conv32(buf->srcipaddr));
|
2014-07-07 00:10:26 +02:00
|
|
|
conn->tcpstateflags = TCP_SYN_RCVD;
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_initsequence(conn->sndseq);
|
2010-11-27 02:01:26 +01:00
|
|
|
conn->unacked = 1;
|
2014-01-14 00:26:11 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
|
|
|
conn->expired = 0;
|
|
|
|
conn->isn = 0;
|
|
|
|
conn->sent = 0;
|
|
|
|
#endif
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2010-11-27 02:01:26 +01:00
|
|
|
/* rcvseq should be the seqno from the incoming packet + 1. */
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2010-11-27 02:01:26 +01:00
|
|
|
memcpy(conn->rcvseq, buf->seqno, 4);
|
2007-11-16 19:48:39 +01:00
|
|
|
|
2014-01-14 00:26:11 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_READAHEAD
|
2007-11-20 00:09:39 +01:00
|
|
|
/* Initialize the list of TCP read-ahead buffers */
|
|
|
|
|
2014-06-24 23:38:00 +02:00
|
|
|
IOB_QINIT(&conn->readahead);
|
2007-11-20 00:09:39 +01:00
|
|
|
#endif
|
|
|
|
|
2014-01-14 00:26:11 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
|
|
|
/* Initialize the write buffer lists */
|
|
|
|
|
|
|
|
sq_init(&conn->write_q);
|
|
|
|
sq_init(&conn->unacked_q);
|
|
|
|
#endif
|
|
|
|
|
2007-11-16 19:48:39 +01:00
|
|
|
/* And, finally, put the connection structure into the active list.
|
|
|
|
* Interrupts should already be disabled in this context.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dq_addlast(&conn->node, &g_active_tcp_connections);
|
2007-11-20 00:09:39 +01:00
|
|
|
}
|
2013-10-06 17:48:54 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
return conn;
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 02:12:49 +02:00
|
|
|
* Name: tcp_bind()
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2014-07-06 20:34:27 +02:00
|
|
|
* This function implements the lower level parts of the standard TCP
|
2007-09-02 23:58:35 +02:00
|
|
|
* bind() operation.
|
|
|
|
*
|
2007-11-16 19:48:39 +01:00
|
|
|
* Return:
|
|
|
|
* 0 on success or -EADDRINUSE on failure
|
|
|
|
*
|
2007-09-02 23:58:35 +02:00
|
|
|
* Assumptions:
|
|
|
|
* This function is called from normal user level code.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2014-06-25 02:12:49 +02:00
|
|
|
int tcp_bind(FAR struct tcp_conn_s *conn,
|
|
|
|
FAR const struct sockaddr_in6 *addr)
|
2007-09-02 23:58:35 +02:00
|
|
|
#else
|
2014-06-25 02:12:49 +02:00
|
|
|
int tcp_bind(FAR struct tcp_conn_s *conn,
|
|
|
|
FAR const struct sockaddr_in *addr)
|
2007-09-02 23:58:35 +02:00
|
|
|
#endif
|
|
|
|
{
|
2014-06-26 22:23:21 +02:00
|
|
|
net_lock_t flags;
|
2007-09-09 19:20:56 +02:00
|
|
|
int port;
|
|
|
|
|
|
|
|
/* Verify or select a local port */
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
flags = net_lock();
|
2014-06-29 03:26:16 +02:00
|
|
|
port = tcp_selectport(ntohs(addr->sin_port));
|
2014-06-26 22:23:21 +02:00
|
|
|
net_unlock(flags);
|
2007-09-09 19:20:56 +02:00
|
|
|
|
|
|
|
if (port < 0)
|
|
|
|
{
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
2007-11-16 19:48:39 +01:00
|
|
|
/* Save the local address in the connection structure. Note that the requested
|
|
|
|
* local IP address is saved but not used. At present, only a single network
|
|
|
|
* interface is supported, the IP address is not of importance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
conn->lport = addr->sin_port;
|
2007-11-22 15:42:52 +01:00
|
|
|
|
|
|
|
#if 0 /* Not used */
|
2007-11-16 19:48:39 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2014-06-29 21:11:08 +02:00
|
|
|
net_ipaddr_copy(conn->lipaddr, addr->sin6_addr.in6_u.u6_addr16);
|
2007-11-16 19:48:39 +01:00
|
|
|
#else
|
2014-06-29 21:11:08 +02:00
|
|
|
net_ipaddr_copy(conn->lipaddr, addr->sin_addr.s_addr);
|
2007-11-22 15:42:52 +01:00
|
|
|
#endif
|
2007-11-16 19:48:39 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return OK;
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 02:12:49 +02:00
|
|
|
* Name: tcp_connect
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2014-07-06 20:34:27 +02:00
|
|
|
* This function implements the lower level parts of the standard
|
2007-09-02 23:58:35 +02:00
|
|
|
* TCP connect() operation: It connects to a remote host using TCP.
|
|
|
|
*
|
|
|
|
* This function is used to start a new connection to the specified
|
2014-04-12 20:13:01 +02:00
|
|
|
* port on the specified host. It uses the connection structure that was
|
2007-09-02 23:58:35 +02:00
|
|
|
* allocated by a preceding socket() call. It sets the connection to
|
|
|
|
* the SYN_SENT state and sets the retransmission timer to 0. This will
|
|
|
|
* cause a TCP SYN segment to be sent out the next time this connection
|
|
|
|
* is periodically processed, which usually is done within 0.5 seconds
|
2014-06-25 02:12:49 +02:00
|
|
|
* after the call to tcp_connect().
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from normal user level code.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2014-06-25 02:12:49 +02:00
|
|
|
int tcp_connect(FAR struct tcp_conn_s *conn,
|
|
|
|
FAR const struct sockaddr_in6 *addr)
|
2007-09-02 23:58:35 +02:00
|
|
|
#else
|
2014-06-25 02:12:49 +02:00
|
|
|
int tcp_connect(FAR struct tcp_conn_s *conn,
|
|
|
|
FAR const struct sockaddr_in *addr)
|
2007-09-02 23:58:35 +02:00
|
|
|
#endif
|
|
|
|
{
|
2014-06-26 22:23:21 +02:00
|
|
|
net_lock_t flags;
|
2007-09-09 19:20:56 +02:00
|
|
|
int port;
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
/* The connection is expected to be in the TCP_ALLOCATED state.. i.e.,
|
2007-09-03 22:34:44 +02:00
|
|
|
* allocated via up_tcpalloc(), but not yet put into the active connections
|
|
|
|
* list.
|
|
|
|
*/
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
if (!conn || conn->tcpstateflags != TCP_ALLOCATED)
|
2007-09-03 22:34:44 +02:00
|
|
|
{
|
|
|
|
return -EISCONN;
|
|
|
|
}
|
|
|
|
|
2014-04-12 20:13:01 +02:00
|
|
|
/* If the TCP port has not already been bound to a local port, then select
|
2007-09-02 23:58:35 +02:00
|
|
|
* one now.
|
|
|
|
*/
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
flags = net_lock();
|
2014-06-29 03:26:16 +02:00
|
|
|
port = tcp_selectport(ntohs(conn->lport));
|
2014-06-26 22:23:21 +02:00
|
|
|
net_unlock(flags);
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
if (port < 0)
|
|
|
|
{
|
|
|
|
return port;
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize and return the connection structure, bind it to the port number */
|
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
conn->tcpstateflags = TCP_SYN_SENT;
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_initsequence(conn->sndseq);
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2014-07-05 21:04:48 +02:00
|
|
|
conn->mss = TCP_INITIAL_MSS;
|
2010-11-27 02:01:26 +01:00
|
|
|
conn->unacked = 1; /* TCP length of the SYN is one. */
|
2007-09-02 23:58:35 +02:00
|
|
|
conn->nrtx = 0;
|
|
|
|
conn->timer = 1; /* Send the SYN next time around. */
|
2014-07-07 00:19:26 +02:00
|
|
|
conn->rto = TCP_RTO;
|
2007-09-02 23:58:35 +02:00
|
|
|
conn->sa = 0;
|
|
|
|
conn->sv = 16; /* Initial value of the RTT variance. */
|
2009-12-15 15:53:45 +01:00
|
|
|
conn->lport = htons((uint16_t)port);
|
2014-01-14 00:26:11 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
|
|
|
conn->expired = 0;
|
|
|
|
conn->isn = 0;
|
|
|
|
conn->sent = 0;
|
|
|
|
#endif
|
2007-09-02 23:58:35 +02:00
|
|
|
|
|
|
|
/* The sockaddr port is 16 bits and already in network order */
|
|
|
|
|
|
|
|
conn->rport = addr->sin_port;
|
|
|
|
|
|
|
|
/* The sockaddr address is 32-bits in network order. */
|
|
|
|
|
2014-06-29 21:11:08 +02:00
|
|
|
net_ipaddr_copy(conn->ripaddr, addr->sin_addr.s_addr);
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2014-01-14 00:26:11 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_READAHEAD
|
2007-11-20 00:09:39 +01:00
|
|
|
/* Initialize the list of TCP read-ahead buffers */
|
|
|
|
|
2014-06-24 23:38:00 +02:00
|
|
|
IOB_QINIT(&conn->readahead);
|
2007-11-20 00:09:39 +01:00
|
|
|
#endif
|
|
|
|
|
2014-01-14 00:26:11 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
|
|
|
/* Initialize the TCP write buffer lists */
|
|
|
|
|
|
|
|
sq_init(&conn->write_q);
|
|
|
|
sq_init(&conn->unacked_q);
|
|
|
|
#endif
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/* And, finally, put the connection structure into the active
|
|
|
|
* list. Because g_active_tcp_connections is accessed from user level and
|
|
|
|
* interrupt level, code, it is necessary to keep interrupts disabled during
|
|
|
|
* this operation.
|
|
|
|
*/
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
flags = net_lock();
|
2007-09-03 22:34:44 +02:00
|
|
|
dq_addlast(&conn->node, &g_active_tcp_connections);
|
2014-06-26 22:23:21 +02:00
|
|
|
net_unlock(flags);
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2007-09-02 23:58:35 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2007-11-22 19:36:46 +01:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_TCP */
|