diff mbox series

net: split sk_reuse into sk_reuse and sk_force_reuse

Message ID 20180614005606.1057-1-avagin@openvz.org
State Changes Requested, archived
Delegated to: David Miller
Headers show
Series net: split sk_reuse into sk_reuse and sk_force_reuse | expand

Commit Message

Andrei Vagin June 14, 2018, 12:56 a.m. UTC
The commit f396922d862a added a check to not allow changing
SO_REUSEADDR/SO_REUSEPORT on bound sockets. First, it doesn't
take into account that TCP_REPAIR changes SO_REUSEADDR. Second, now it
is impossible to restore a socket state and set SO_REUSEADDR,
because the kernel always sets SO_REUSEADDR into zero after disabling
the repair mode.

Currently, sk_reuse can have three values: SK_NO_REUSE, SK_CAN_REUSE,
SK_FORCE_REUSE. SK_CAN_REUSE is set by SOL_REUSEADDR.  SK_FORCE_REUSE is
used to ignore bind conflicts for sockets in the repair mode.

This patch makes sk->sk_reuse back into a boolean and adds
sk->sk_force_reuse to track SK_FORCE_REUSE separatly.

In this case, the tcp_repair mode doesn't affect sk_reuse and
it's value can be set before switching a socketn into the repair mode.

Fixes: f396922d862a ("net: do not allow changing SO_REUSEADDR/SO_REUSEPORT on bound sockets")
Cc: Maciej Żenczykowski <maze@google.com>
Cc: Eric Dumazet <edumazet@google.com>
Signed-off-by: Andrei Vagin <avagin@openvz.org>
---
 include/net/sock.h              | 13 ++++---------
 net/ipv4/inet_connection_sock.c |  2 +-
 net/ipv4/tcp.c                  |  4 ++--
 3 files changed, 7 insertions(+), 12 deletions(-)

Comments

Maciej Żenczykowski June 14, 2018, 1:10 a.m. UTC | #1
>  #define SK_NO_REUSE    0
>  #define SK_CAN_REUSE   1

since it's a boolean now these should go away too I believe.

should there simply/also be a separate privileged socket option to
set/get force reuse?
Maciej Żenczykowski June 14, 2018, 1:34 a.m. UTC | #2
> Hi Andrey
>
> This commit was reverted, do we still need  this patch ?

I think it still makes things easier to understand...
Andrei Vagin June 14, 2018, 8:02 p.m. UTC | #3
On Wed, Jun 13, 2018 at 06:17:41PM -0700, Eric Dumazet wrote:
> On Wed, Jun 13, 2018 at 5:56 PM Andrei Vagin <avagin@openvz.org> wrote:
> 
> > The commit f396922d862a added a check to not allow changing
> > SO_REUSEADDR/SO_REUSEPORT on bound sockets. First, it doesn't
> > take into account that TCP_REPAIR changes SO_REUSEADDR. Second, now it
> > is impossible to restore a socket state and set SO_REUSEADDR,
> > because the kernel always sets SO_REUSEADDR into zero after disabling
> > the repair mode.
> >
> >
> Hi Andrey
> 
> This commit was reverted, do we still need  this patch ?

I have seen that this patch was reverted. Probably I had to check
net-next before sending it.

I'm agree with Maciej Żenczykowski that it makes this code better. I
have never understood why TCP_REPAIR drops SO_REUSEADDR. Now each time
when we use TCP_REPAIR, we have to save a value of SO_REUSEADDR and
restore it back after disabling TCP_REPAIR. With this patch, we will
able to enable/disable TCP_REPAIR and don't care about sk_reuse.

I will update the commit message and send the patch again.

Thanks,
Andrei
diff mbox series

Patch

diff --git a/include/net/sock.h b/include/net/sock.h
index b3b75419eafe..8ad19286ab9e 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -130,6 +130,7 @@  typedef __u64 __bitwise __addrpair;
  *	@skc_family: network address family
  *	@skc_state: Connection state
  *	@skc_reuse: %SO_REUSEADDR setting
+ *	@skc_force_reuse: ignore bind conflicts
  *	@skc_reuseport: %SO_REUSEPORT setting
  *	@skc_bound_dev_if: bound device index if != 0
  *	@skc_bind_node: bind hash linkage for various protocol lookup tables
@@ -174,7 +175,8 @@  struct sock_common {
 
 	unsigned short		skc_family;
 	volatile unsigned char	skc_state;
-	unsigned char		skc_reuse:4;
+	unsigned char		skc_reuse:1;
+	unsigned char		skc_force_reuse:1;
 	unsigned char		skc_reuseport:1;
 	unsigned char		skc_ipv6only:1;
 	unsigned char		skc_net_refcnt:1;
@@ -339,6 +341,7 @@  struct sock {
 #define sk_family		__sk_common.skc_family
 #define sk_state		__sk_common.skc_state
 #define sk_reuse		__sk_common.skc_reuse
+#define sk_force_reuse		__sk_common.skc_force_reuse
 #define sk_reuseport		__sk_common.skc_reuseport
 #define sk_ipv6only		__sk_common.skc_ipv6only
 #define sk_net_refcnt		__sk_common.skc_net_refcnt
@@ -502,16 +505,8 @@  enum sk_pacing {
 #define rcu_dereference_sk_user_data(sk)	rcu_dereference(__sk_user_data((sk)))
 #define rcu_assign_sk_user_data(sk, ptr)	rcu_assign_pointer(__sk_user_data((sk)), ptr)
 
-/*
- * SK_CAN_REUSE and SK_NO_REUSE on a socket mean that the socket is OK
- * or not whether his port will be reused by someone else. SK_FORCE_REUSE
- * on a socket means that the socket will reuse everybody else's port
- * without looking at the other's sk_reuse value.
- */
-
 #define SK_NO_REUSE	0
 #define SK_CAN_REUSE	1
-#define SK_FORCE_REUSE	2
 
 int sk_set_peek_off(struct sock *sk, int val);
 
diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
index 33a88e045efd..2ac1c591b60c 100644
--- a/net/ipv4/inet_connection_sock.c
+++ b/net/ipv4/inet_connection_sock.c
@@ -306,7 +306,7 @@  int inet_csk_get_port(struct sock *sk, unsigned short snum)
 		goto fail_unlock;
 tb_found:
 	if (!hlist_empty(&tb->owners)) {
-		if (sk->sk_reuse == SK_FORCE_REUSE)
+		if (sk->sk_force_reuse)
 			goto success;
 
 		if ((tb->fastreuse > 0 && reuse) ||
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 2741953adaba..70bfdd5a2fc4 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -2810,11 +2810,11 @@  static int do_tcp_setsockopt(struct sock *sk, int level,
 			err = -EPERM;
 		else if (val == 1) {
 			tp->repair = 1;
-			sk->sk_reuse = SK_FORCE_REUSE;
+			sk->sk_force_reuse = 1;
 			tp->repair_queue = TCP_NO_QUEUE;
 		} else if (val == 0) {
 			tp->repair = 0;
-			sk->sk_reuse = SK_NO_REUSE;
+			sk->sk_force_reuse = 0;
 			tcp_send_window_probe(sk);
 		} else
 			err = -EINVAL;