netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Shan Wei <shanwei88@gmail.com>
To: David Miller <davem@davemloft.net>
Cc: NetDev <netdev@vger.kernel.org>
Subject: [PATCH net-next] tcp: avoid tcp loop connection on lo device
Date: Tue, 25 Sep 2012 16:12:34 +0800	[thread overview]
Message-ID: <50616772.8040704@gmail.com> (raw)

Tcp supports simultaneous Connection, but we meat odd phenomenon that tcp client can receive what 
send by itself. tcp client and tcp server communicate through loop device. tcp server selects
port 40000 to listen which is in local port range. But after tcp server program is killed, tcp client
still can connect successfully. 


  Client                    Server
connect  <-----OK--------->   listen:127.0.0.1,port:40000
send(d1) ----------------->   recv
recv(d2) <-----------------   send(d2)  
 

         -----------------    killed(40000 not listened)

connect  <-----OK--------->   
send(d1) ----------------->  
recv(d1) <-----------------  

The simultaneous connection has no meaning for lo device, and for this case,
tcp client don't know whether server is listen on port 40000.
Just fix it sending reset to keep consistent state machine.  


Reproduced step:
1. while true ; do  nc 127.0.0.1 40001 ;done
2.  ss -nt dst 127.0.0.1
    State      Recv-Q Send-Q                                  Local Address:Port                                    Peer Address:Port 
    ESTAB      0      0                                           127.0.0.1:40001                                      127.0.0.1:40001 


Signed-off-by: Shan Wei <davidshan@tencent.com>
---
 net/ipv4/tcp_input.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index e037697..a2f5a10 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -5659,6 +5659,7 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
 	struct tcp_cookie_values *cvp = tp->cookie_values;
 	struct tcp_fastopen_cookie foc = { .len = -1 };
 	int saved_clamp = tp->rx_opt.mss_clamp;
+	struct inet_sock *isk = inet_sk(sk);
 
 	tcp_parse_options(skb, &tp->rx_opt, &hash_location, 0, &foc);
 
@@ -5832,8 +5833,13 @@ discard:
 	if (th->syn) {
 		/* We see SYN without ACK. It is attempt of
 		 * simultaneous connect with crossed SYNs.
-		 * Particularly, it can be connect to self.
+		 * But, avoid tcp loop connection on single socket.
 		 */
+
+		if (isk->inet_dport == isk->inet_sport &&
+		    isk->inet_saddr == isk->inet_daddr)
+			goto reset_and_undo;
+
 		tcp_set_state(sk, TCP_SYN_RECV);
 
 		if (tp->rx_opt.saw_tstamp) {
-- 
1.7.1

             reply	other threads:[~2012-09-25  8:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-25  8:12 Shan Wei [this message]
2012-09-25 17:04 ` [PATCH net-next] tcp: avoid tcp loop connection on lo device David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=50616772.8040704@gmail.com \
    --to=shanwei88@gmail.com \
    --cc=davem@davemloft.net \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).