netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Miller <davem@davemloft.net>
To: ole@ans.pl
Cc: eric.dumazet@gmail.com, netdev@vger.kernel.org
Subject: Re: [PATCH] inet: dont set inet_rcv_saddr in connect()
Date: Tue, 07 Sep 2010 19:16:06 -0700 (PDT)	[thread overview]
Message-ID: <20100907.191606.102548977.davem@davemloft.net> (raw)
In-Reply-To: <4C86B402.5000205@ans.pl>

From: Krzysztof Olędzki <ole@ans.pl>
Date: Tue, 07 Sep 2010 23:52:02 +0200

>> [PATCH] inet: dont set inet_rcv_saddr in connect()
 ...
>> Reported-by: Krzysztof Olędzki <ole@ans.pl>
>> Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
> 
> Tested-by: Krzysztof Piotr Oledzki <ole@ans.pl>

Eric, ipv6 has the same problem so I fixed it there too.

Here is the final patch I put into net-2.6, thanks!

--------------------
inet: dont set inet_rcv_saddr in connect()

The following sequence :

socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)

connect(fd, {sa_family=AF_INET, sin_port=htons(xx),
sin_addr=inet_addr("1.2.3.4")}, 28)

1) Does an implicit inet_autobind()
  (using an INADDR_ANY address, and selecting a random port).

2) Does an ip4_datagram_connect() to specify the address/port of
remote end point.

Problem is ip4_datagram_connect() also sets inet->inet_rcv_saddr (from
INADDR_ANY to IP source address, given the current route to remote end
point). Only the first connect() on the socket does this. Following ones
dont change the (possibly wrong) source address.

This breaks the secondary UDP hash, based on (ADDRESS, port), that was
computed by inet_autobind(). If more than 10 sockets are linked in
primary hash chain, we can drop incoming packets because searches are
done in wrong secondary hash chain.

This also potentially breaks multiple connect() to change remote
endpoints, because old source address might be non usable for packets to
new destination.

If route happens to change, then we should automatically change our
source address too, at next sendmsg() call, and UDP code deals with this
just fine.

If an application needs to specify a precise source address, it must use
bind() system call. connect() man page only refers to remote address,
not local one.

[ IPV6 has the same problem so fix it there too. -DaveM ]

Reported-by: Krzysztof Olędzki <ole@ans.pl>
Tested-by: Krzysztof Piotr Oledzki <ole@ans.pl>
Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
---
 net/ipv4/datagram.c |   11 +++++++----
 net/ipv6/datagram.c |   31 +++++++++++++++----------------
 2 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/net/ipv4/datagram.c b/net/ipv4/datagram.c
index f055094..a59492c 100644
--- a/net/ipv4/datagram.c
+++ b/net/ipv4/datagram.c
@@ -60,10 +60,13 @@ int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
 		ip_rt_put(rt);
 		return -EACCES;
 	}
-	if (!inet->inet_saddr)
-		inet->inet_saddr = rt->rt_src;	/* Update source address */
-	if (!inet->inet_rcv_saddr)
-		inet->inet_rcv_saddr = rt->rt_src;
+	/*
+	 * Should connect() change inet_rcv_saddr / inet_saddr ?
+	 * It should not, because we want to specify the peer to which
+	 * datagrams are to be sent, regardless of our source address that
+	 * might change in the future, after a route change.
+	 * To specify our source address, bind() is the right API.
+	 */
 	inet->inet_daddr = rt->rt_dst;
 	inet->inet_dport = usin->sin_port;
 	sk->sk_state = TCP_ESTABLISHED;
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index 7d929a2..9c26556 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -102,13 +102,14 @@ ipv4_connected:
 
 		ipv6_addr_set_v4mapped(inet->inet_daddr, &np->daddr);
 
-		if (ipv6_addr_any(&np->saddr))
-			ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
-
-		if (ipv6_addr_any(&np->rcv_saddr))
-			ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
-					       &np->rcv_saddr);
-
+		/*
+		 * Should connect() change np->rcv_saddr / np->saddr ?
+		 * It should not, because we want to specify the
+		 * peer to which datagrams are to be sent, regardless
+		 * of our source address that might change in the
+		 * future, after a route change.  To specify our
+		 * source address, bind() is the right API.
+		 */
 		goto out;
 	}
 
@@ -173,15 +174,13 @@ ipv4_connected:
 			goto out;
 	}
 
-	/* source address lookup done in ip6_dst_lookup */
-
-	if (ipv6_addr_any(&np->saddr))
-		ipv6_addr_copy(&np->saddr, &fl.fl6_src);
-
-	if (ipv6_addr_any(&np->rcv_saddr)) {
-		ipv6_addr_copy(&np->rcv_saddr, &fl.fl6_src);
-		inet->inet_rcv_saddr = LOOPBACK4_IPV6;
-	}
+	/*
+	 * Should connect() change np->rcv_saddr / np->saddr ?
+	 * It should not, because we want to specify the peer to which
+	 * datagrams are to be sent, regardless of our source address that
+	 * might change in the future, after a route change.
+	 * To specify our source address, bind() is the right API.
+	 */
 
 	ip6_dst_store(sk, dst,
 		      ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
-- 
1.7.2.2


  reply	other threads:[~2010-09-08  2:15 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-06 17:11 2.6.34: Problem with UDP traffic on lo + poll(?) Krzysztof Oledzki
2010-09-06 19:42 ` Eric Dumazet
2010-09-06 19:55   ` Krzysztof Olędzki
2010-09-06 20:29     ` Eric Dumazet
2010-09-06 20:44       ` Krzysztof Olędzki
2010-09-06 20:48         ` Krzysztof Olędzki
2010-09-07 15:37           ` Krzysztof Olędzki
2010-09-07 16:36             ` Eric Dumazet
2010-09-07 19:20               ` Krzysztof Olędzki
2010-09-07 19:26               ` Eric Dumazet
2010-09-07 19:59                 ` David Miller
2010-09-07 21:35                   ` [PATCH] inet: dont set inet_rcv_saddr in connect() Eric Dumazet
2010-09-07 21:52                     ` Krzysztof Olędzki
2010-09-08  2:16                       ` David Miller [this message]
2010-09-08  4:13                         ` Eric Dumazet
2010-09-08  2:34                     ` Brian Haley
2010-09-08  3:34                       ` David Miller
2010-09-08  4:42                         ` Eric Dumazet
2010-09-08  5:51                           ` David Miller
2010-09-08  4:57                       ` Eric Dumazet
2010-09-08  5:36                         ` David Miller
2010-09-08  5:52                           ` Eric Dumazet
2010-09-08 10:10                             ` [PATCH] udp: add rehash on connect() Eric Dumazet
2010-09-08 15:06                               ` Krzysztof Olędzki
2010-09-08 15:17                                 ` Eric Dumazet
2010-09-08 15:29                                   ` Krzysztof Olędzki
2010-09-08 15:08                               ` [PATCH v2] " Eric Dumazet
2010-09-08 16:52                                 ` Krzysztof Olędzki
2010-09-09  4:39                                   ` David Miller
2010-09-08 14:27                             ` [PATCH] inet: dont set inet_rcv_saddr in connect() Eric Dumazet
2010-09-07 21:28                 ` 2.6.34: Problem with UDP traffic on lo + poll(?) Krzysztof Olędzki
2010-09-07 21:39                   ` Eric Dumazet
2010-09-07 21:51                     ` Krzysztof Olędzki
2010-09-08  4:12                       ` Eric Dumazet

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=20100907.191606.102548977.davem@davemloft.net \
    --to=davem@davemloft.net \
    --cc=eric.dumazet@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=ole@ans.pl \
    /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).