From 14ced754f5104d7548cfa09ce39a940f2486fcb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9onard=20Bise?= Date: Tue, 16 Jun 2020 15:52:12 +0200 Subject: [PATCH] net: tcp: Do not send FIN when closing listening sockets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A net context in LISTENING mode waits for incoming connections, once a new connection is established a new net context is spawned which is responsible for handling the new connection. Therefore when closing a LISTENING context it is not useful to send FIN as it is never connected. Actually closing the connection would be done by calling close on the spawned net context which is returned by the accept call. Signed-off-by: Léonard Bise --- subsys/net/ip/tcp.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/subsys/net/ip/tcp.c b/subsys/net/ip/tcp.c index f2406f2f82c..4cb16efeeb5 100644 --- a/subsys/net/ip/tcp.c +++ b/subsys/net/ip/tcp.c @@ -1507,8 +1507,7 @@ static void queue_fin(struct net_context *ctx) int net_tcp_put(struct net_context *context) { if (net_context_get_ip_proto(context) == IPPROTO_TCP) { - if ((net_context_get_state(context) == NET_CONTEXT_CONNECTED || - net_context_get_state(context) == NET_CONTEXT_LISTENING) + if (net_context_get_state(context) == NET_CONTEXT_CONNECTED && context->tcp && !context->tcp->fin_rcvd) { NET_DBG("TCP connection in active close, not " @@ -1519,6 +1518,18 @@ int net_tcp_put(struct net_context *context) return 0; } + /* A listening context is only used to establish connections. + * Since once the connection is established it is not handled + * directly by the listening context but rather by the child it + * spawned, it is not needed to send FIN when closing such + * contexts. + */ + if (context->tcp && + net_context_get_state(context) == NET_CONTEXT_LISTENING) { + net_context_unref(context); + return 0; + } + if (context->tcp && net_tcp_get_state(context->tcp) == NET_TCP_SYN_SENT) { net_context_unref(context);