Skip to contentSkip to conversation

mptcp: cleanup token code

RFC In review Florian Westphal 3 participants 16 messages Aug 29, 2019

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author
#0 lore raw

v2 cover mptcp: cleanup token code

This series again marked RFC so noone bothers to squash all of this into the mptcp tree.

In this second round, I've addressed all comments from Matthieu and Peter. As a result, even more code was pruned from token.c and placed as static helper in subflow.c.

I've added a 'v2' mini-changelog to all affected patches.

 pm.c       |    6 
 protocol.c |    5 
 protocol.h |   23 +--
 subflow.c  |  152 +++++++++++++++++++--
 token.c    |  426 +++++++++++++++++++------------------------------------------
 5 files changed, 277 insertions(+), 333 deletions(-)
author
#1 lore raw

RFC v2 1/10 mptcp: token: remove token_init

Its not needed, just use the static initialisers.
Also add 'MPTCP' prefix for pr_* calls.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c |  1 -
 net/mptcp/protocol.h |  1 -
 net/mptcp/token.c    | 17 ++++++-----------
 3 files changed, 6 insertions(+), 13 deletions(-)
net/mptcp/protocol.c +0 -1
index aaa71b161c1a..b408150e46b9 100644
@@ -1104,7 +1104,6 @@ void mptcp_proto_init(void)
 	mptcp_stream_ops.listen = mptcp_listen;
 	mptcp_stream_ops.shutdown = mptcp_shutdown;
 
-	token_init();
 	subflow_init();
 	pm_init();
 
net/mptcp/protocol.h +0 -1
index 68877e952780..e97ac14115a2 100644
@@ -225,7 +225,6 @@ void mptcp_get_options(const struct sk_buff *skb,
 void mptcp_finish_connect(struct sock *sk, int mp_capable);
 void mptcp_finish_join(struct sock *sk);
 
-void token_init(void);
 void token_new_request(struct request_sock *req, const struct sk_buff *skb);
 int token_join_request(struct request_sock *req, const struct sk_buff *skb);
 int token_join_response(struct sock *sk);
net/mptcp/token.c +6 -11
index 506665da8b2d..19b63bd34315 100644
@@ -20,6 +20,8 @@
  *       Brandon Heller <brandonh(a)stanford.edu>
  */
 
+#define pr_fmt(fmt) "MPTCP: " fmt
+
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/radix-tree.h>
@@ -31,10 +33,10 @@
 #include <net/mptcp.h>
 #include "protocol.h"
 
-static struct radix_tree_root token_tree;
-static struct radix_tree_root token_req_tree;
-static spinlock_t token_tree_lock;
-static int token_used;
+static RADIX_TREE(token_tree, GFP_ATOMIC);
+static RADIX_TREE(token_req_tree, GFP_ATOMIC);
+static DEFINE_SPINLOCK(token_tree_lock);
+static int token_used __read_mostly;
 
 static bool find_req_token(u32 token)
 {
@@ -392,10 +394,3 @@ void token_destroy(u32 token)
 		sock_put(conn);
 	spin_unlock_bh(&token_tree_lock);
 }
-
-void token_init(void)
-{
-	INIT_RADIX_TREE(&token_tree, GFP_ATOMIC);
-	INIT_RADIX_TREE(&token_req_tree, GFP_ATOMIC);
-	spin_lock_init(&token_tree_lock);
-}
Signature · 1 line
2.21.0
author
#2 lore raw

RFC v2 2/10 mptcp: token: remove two unneeded helpers

In reply to Florian Westphal

There were three flavours of almost the same function:

token_lookup_get, the base function that takes a token value and looks up the mptcp socket associated with it.

token_new_subflow, same function, but will also set subflow->local_nonce.

token_new_join, just like lookup_get, but assigns the looked-up socket to subflow->conn.

This renames token_lookup_get to mptcp_token_get_sock to avoid make it clear this belongs to mptcp namespace, and then changes the callers of token_new_subflow to also init the nonce and token_new_join to do the assignment in the caller.

v2: remove a stray WARN_ON (Matthieu)

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/pm.c       |  6 +++---
 net/mptcp/protocol.h |  4 +---
 net/mptcp/subflow.c  | 16 +++++++++++----
 net/mptcp/token.c    | 49 ++++++++++----------------------------------
 4 files changed, 27 insertions(+), 48 deletions(-)
net/mptcp/pm.c +3 -3
index c98b92512adf..a34b76d097f6 100644
@@ -13,7 +13,7 @@
 int pm_announce_addr(u32 token, sa_family_t family, u8 local_id,
 		     struct in_addr *addr)
 {
-	struct mptcp_sock *msk = mptcp_sk(token_lookup_get(token));
+	struct mptcp_sock *msk = mptcp_token_get_sock(token);
 	int err = 0;
 
 	if (!msk)
@@ -38,7 +38,7 @@ int pm_announce_addr(u32 token, sa_family_t family, u8 local_id,
 
 int pm_remove_addr(u32 token, u8 local_id)
 {
-	struct mptcp_sock *msk = mptcp_sk(token_lookup_get(token));
+	struct mptcp_sock *msk = mptcp_token_get_sock(token);
 
 	if (!msk)
 		return -EINVAL;
@@ -52,7 +52,7 @@ int pm_remove_addr(u32 token, u8 local_id)
 
 int pm_create_subflow(u32 token, u8 remote_id)
 {
-	struct mptcp_sock *msk = mptcp_sk(token_lookup_get(token));
+	struct mptcp_sock *msk = mptcp_token_get_sock(token);
 	struct sockaddr_in remote;
 	struct sockaddr_in local;
 	int err;
net/mptcp/protocol.h +1 -3
index e97ac14115a2..1655283dbd6a 100644
@@ -232,11 +232,9 @@ int token_join_valid(struct request_sock *req,
 		     struct tcp_options_received *rx_opt);
 void token_destroy_request(u32 token);
 void token_new_connect(struct sock *sk);
-void token_new_subflow(struct sock *sk);
 void token_new_accept(struct sock *sk);
-int token_new_join(struct sock *sk);
 void token_update_accept(struct sock *sk, struct sock *conn);
-struct sock *token_lookup_get(u32 token);
+struct mptcp_sock *mptcp_token_get_sock(u32 token);
 void token_release(u32 token);
 void token_destroy(u32 token);
 
net/mptcp/subflow.c +12 -4
index 27afc64d377f..fc507e091cf5 100644
@@ -24,7 +24,11 @@ static int subflow_rebuild_header(struct sock *sk)
 		token_new_connect(sk);
 	} else if (subflow->request_join && !subflow->local_nonce) {
 		pr_debug("subflow=%p", sk);
-		token_new_subflow(sk);
+		mptcp_token_get_sock(subflow->token);
+
+		do {
+			get_random_bytes(&subflow->local_nonce, sizeof(u32));
+		} while (!subflow->local_nonce);
 	}
 
 	return inet_sk_rebuild_header(sk);
@@ -179,10 +183,14 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 		if (ctx->mp_capable) {
 			token_new_accept(child);
 		} else if (ctx->mp_join) {
-			if (token_new_join(child))
+			struct mptcp_sock *owner;
+
+			owner = mptcp_token_get_sock(ctx->token);
+			if (!owner)
 				goto close_child;
-			else
-				mptcp_finish_join(child);
+
+			ctx->conn = (struct sock *)owner;
+			mptcp_finish_join(child);
 		}
 	}
 
net/mptcp/token.c +11 -38

67 lines not on this page · raw message

Signature · 1 line
2.21.0
author
#3 lore raw

RFC v2 3/10 mptcp: token: rename token_join_response

In reply to Florian Westphal

This function validates the truncated hmac and computes a hmac for use in the ack packet. Rename it, place it where its used, and do the hmac computation in the caller.

This also adds a pr_fmt specifier, so the included pr_debug will auto-gain 'MPTCP' prefix.

v2: change name and place hmac calculation in the caller (Peter)
add pr_fmt too
squash two pr_debug() into one.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.h |  1 -
 net/mptcp/subflow.c  | 34 ++++++++++++++++++++++++++++++----
 net/mptcp/token.c    | 31 -------------------------------
 3 files changed, 30 insertions(+), 36 deletions(-)
net/mptcp/protocol.h +0 -1
index 1655283dbd6a..89bd68f85856 100644
@@ -227,7 +227,6 @@ void mptcp_finish_join(struct sock *sk);
 
 void token_new_request(struct request_sock *req, const struct sk_buff *skb);
 int token_join_request(struct request_sock *req, const struct sk_buff *skb);
-int token_join_response(struct sock *sk);
 int token_join_valid(struct request_sock *req,
 		     struct tcp_options_received *rx_opt);
 void token_destroy_request(u32 token);
net/mptcp/subflow.c +30 -4
index fc507e091cf5..8a8109466487 100644
@@ -4,6 +4,8 @@
  * Copyright (c) 2017 - 2019, Intel Corporation.
  */
 
+#define pr_fmt(fmt) "MPTCP: " fmt
+
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/netdevice.h>
@@ -96,6 +98,25 @@ static void subflow_v4_init_req(struct request_sock *req,
 	}
 }
 
+/* validate received truncated hmac and create hmac for third ACK */
+static bool subflow_thmac_valid(struct subflow_context *subflow)
+{
+	u8 hmac[MPTCPOPT_HMAC_LEN];
+	u64 thmac;
+
+	crypto_hmac_sha1(subflow->remote_key, subflow->local_key,
+			 subflow->remote_nonce, subflow->local_nonce,
+			 (u32 *)hmac);
+
+	thmac = get_unaligned_be64(hmac);
+	pr_debug("subflow=%p, token=%u, thmac=%llu, subflow->thmac=%llu\n",
+		 subflow, subflow->token,
+		 (unsigned long long)thmac,
+		 (unsigned long long)subflow->thmac);
+
+	return thmac == subflow->thmac;
+}
+
 static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
 {
 	struct subflow_context *subflow = subflow_ctx(sk);
@@ -119,13 +140,18 @@ static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
 		pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u",
 			 subflow_ctx(sk), subflow->thmac,
 			 subflow->remote_nonce);
-		if (token_join_response(sk)) {
+		if (!subflow_thmac_valid(subflow)) {
 			subflow->mp_join = 0;
 			// @@ need to trigger RST
-		} else {
-			mptcp_finish_join(sk);
-			subflow->conn_finished = 1;
+			return;
 		}
+
+		crypto_hmac_sha1(subflow->local_key, subflow->remote_key,
+				 subflow->local_nonce, subflow->remote_nonce,
+				 (u32 *)subflow->hmac);
+
+		mptcp_finish_join(sk);
+		subflow->conn_finished = 1;
 	}
 }
 
net/mptcp/token.c +0 -31
index b63ce35c9ed3..3163832d8ebf 100644
@@ -93,28 +93,6 @@ static void new_req_join(struct request_sock *req, struct sock *sk,
 		 subflow_req->thmac);
 }
 
-static int new_rsp_join(struct sock *sk)
-{
-	struct subflow_context *subflow = subflow_ctx(sk);
-	u8 hmac[MPTCPOPT_HMAC_LEN];
-	u64 thmac;
-
-	crypto_hmac_sha1(subflow->remote_key, subflow->local_key,
-			 subflow->remote_nonce, subflow->local_nonce,
-			 (u32 *)hmac);
-
-	thmac = get_unaligned_be64(hmac);
-	pr_debug("thmac=%llu", thmac);
-	if (thmac != subflow->thmac)
-		return -1;
-
-	crypto_hmac_sha1(subflow->local_key, subflow->remote_key,
-			 subflow->local_nonce, subflow->remote_nonce,
-			 (u32 *)subflow->hmac);
-
-	return 0;
-}
-
 static int new_join_valid(struct request_sock *req, struct sock *sk,
 			  struct tcp_options_received *rx_opt)
 {
@@ -239,15 +217,6 @@ int token_join_request(struct request_sock *req, const struct sk_buff *skb)
 	return 0;
 }
 
-/* validate received truncated hmac and create hmac for third ACK */
-int token_join_response(struct sock *sk)
-{
-	struct subflow_context *subflow = subflow_ctx(sk);
-
-	pr_debug("subflow=%p, token=%u", subflow, subflow->token);
-	return new_rsp_join(sk);
-}
-
 /* validate hmac received in third ACK */
 int token_join_valid(struct request_sock *req,
 		     struct tcp_options_received *rx_opt)
Signature · 1 line
2.21.0
author
#7 lore raw

RFC v2 4/10 mptcp: token: remove token_join_valid

In reply to Florian Westphal

This helper is only used once, we can just place it in subflow.c and use the existing mptcp_token_get_sock() to fetch the mptcp socket.

Also, prefer the crypto_mneq function rather than memcmp which might reveal timing information when doing hmac compare.

v2: place it in subflow.c, rename to subflow_hmac_valid (Peter)

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.h |  2 --
 net/mptcp/subflow.c  | 29 ++++++++++++++++++++++++++++-
 net/mptcp/token.c    | 36 ------------------------------------
 3 files changed, 28 insertions(+), 39 deletions(-)
net/mptcp/protocol.h +0 -2
index 89bd68f85856..5f33d3729bfd 100644
@@ -227,8 +227,6 @@ void mptcp_finish_join(struct sock *sk);
 
 void token_new_request(struct request_sock *req, const struct sk_buff *skb);
 int token_join_request(struct request_sock *req, const struct sk_buff *skb);
-int token_join_valid(struct request_sock *req,
-		     struct tcp_options_received *rx_opt);
 void token_destroy_request(u32 token);
 void token_new_connect(struct sock *sk);
 void token_new_accept(struct sock *sk);
net/mptcp/subflow.c +28 -1
index 8a8109466487..f604e189126b 100644
@@ -9,6 +9,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/netdevice.h>
+#include <crypto/algapi.h>
 #include <net/sock.h>
 #include <net/inet_common.h>
 #include <net/inet_hashtables.h>
@@ -176,6 +177,31 @@ static int subflow_conn_request(struct sock *sk, struct sk_buff *skb)
 	return 0;
 }
 
+/* validate hmac received in third ACK */
+static bool subflow_hmac_valid(const struct request_sock *req,
+			       const struct tcp_options_received *rx_opt)
+{
+	const struct subflow_request_sock *subflow_req = subflow_rsk(req);
+	u8 hmac[MPTCPOPT_HMAC_LEN];
+	struct mptcp_sock *msk;
+	bool ret;
+
+	msk = mptcp_token_get_sock(subflow_req->token);
+	if (!msk)
+		return false;
+
+	crypto_hmac_sha1(msk->remote_key, msk->local_key,
+			 subflow_req->remote_nonce, subflow_req->local_nonce,
+			 (u32 *)hmac);
+
+	ret = true;
+	if (crypto_memneq(hmac, rx_opt->mptcp.hmac, sizeof(hmac)))
+		ret = false;
+
+	sock_put((struct sock *)msk);
+	return ret;
+}
+
 static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 					  struct sk_buff *skb,
 					  struct request_sock *req,
@@ -194,7 +220,8 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 	if (!subflow_req->mp_capable && subflow_req->mp_join) {
 		opt_rx.mptcp.mp_join = 0;
 		mptcp_get_options(skb, &opt_rx);
-		if (!opt_rx.mptcp.mp_join || token_join_valid(req, &opt_rx))
+		if (!opt_rx.mptcp.mp_join ||
+		    !subflow_hmac_valid(req, &opt_rx))
 			return NULL;
 	}
 
net/mptcp/token.c +0 -36
index 3163832d8ebf..8de7d9d83e96 100644
@@ -93,20 +93,6 @@ static void new_req_join(struct request_sock *req, struct sock *sk,
 		 subflow_req->thmac);
 }
 
-static int new_join_valid(struct request_sock *req, struct sock *sk,
-			  struct tcp_options_received *rx_opt)
-{
-	struct subflow_request_sock *subflow_req = subflow_rsk(req);
-	struct mptcp_sock *msk = mptcp_sk(sk);
-	u8 hmac[MPTCPOPT_HMAC_LEN];
-
-	crypto_hmac_sha1(msk->remote_key, msk->local_key,
-			 subflow_req->remote_nonce, subflow_req->local_nonce,
-			 (u32 *)hmac);
-
-	return memcmp(hmac, (char *)rx_opt->mptcp.hmac, MPTCPOPT_HMAC_LEN);
-}
-
 static void new_token(const struct sock *sk)
 {
 	struct subflow_context *subflow = subflow_ctx(sk);
@@ -217,28 +203,6 @@ int token_join_request(struct request_sock *req, const struct sk_buff *skb)
 	return 0;
 }
 
-/* validate hmac received in third ACK */
-int token_join_valid(struct request_sock *req,
-		     struct tcp_options_received *rx_opt)
-{
-	struct subflow_request_sock *subflow_req = subflow_rsk(req);
-	struct sock *conn;
-	int err;
-
-	pr_debug("subflow_req=%p, token=%u", subflow_req, subflow_req->token);
-	spin_lock_bh(&token_tree_lock);
-	conn = __token_lookup(subflow_req->token);
-	if (conn)
-		sock_hold(conn);
-	spin_unlock_bh(&token_tree_lock);
-	if (!conn)
-		return -1;
-
-	err = new_join_valid(req, conn, rx_opt);
-	sock_put(conn);
-	return err;
-}
-
 /* create new local key, idsn, and token for subflow */
 void token_new_connect(struct sock *sk)
 {
Signature · 1 line
2.21.0
author
#8 lore raw

RFC v2 5/10 mptcp: token: remove token_join_request

In reply to Florian Westphal

Place the function where needed, we can use existing helper to fetch the mptcp sk.

v2: move the helper entirely (Peter)

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.h |  1 -
 net/mptcp/subflow.c  | 34 +++++++++++++++++++++++++++++++++-
 net/mptcp/token.c    | 44 --------------------------------------------
 3 files changed, 33 insertions(+), 46 deletions(-)
net/mptcp/protocol.h +0 -1
index 5f33d3729bfd..8514c41e3a6b 100644
@@ -226,7 +226,6 @@ void mptcp_finish_connect(struct sock *sk, int mp_capable);
 void mptcp_finish_join(struct sock *sk);
 
 void token_new_request(struct request_sock *req, const struct sk_buff *skb);
-int token_join_request(struct request_sock *req, const struct sk_buff *skb);
 void token_destroy_request(u32 token);
 void token_new_connect(struct sock *sk);
 void token_new_accept(struct sock *sk);
net/mptcp/subflow.c +33 -1
index f604e189126b..33bfee514350 100644
@@ -48,6 +48,38 @@ static void subflow_req_destructor(struct request_sock *req)
 	tcp_request_sock_ops.destructor(req);
 }
 
+/* validate received token and create truncated hmac and nonce for SYN-ACK */
+static bool subflow_token_join_request(struct request_sock *req,
+				       const struct sk_buff *skb)
+{
+	struct subflow_request_sock *subflow_req = subflow_rsk(req);
+	u8 hmac[MPTCPOPT_HMAC_LEN];
+	struct mptcp_sock *msk;
+
+	msk = mptcp_token_get_sock(subflow_req->token);
+	if (!msk) {
+		pr_debug("subflow_req=%p, token=%u - not found\n",
+			 subflow_req, subflow_req->token);
+		return false;
+	}
+
+	if (pm_get_local_id(req, (struct sock *)msk, skb)) {
+		sock_put((struct sock *)msk);
+		return false;
+	}
+
+	get_random_bytes(&subflow_req->local_nonce, sizeof(u32));
+
+	crypto_hmac_sha1(msk->local_key, msk->remote_key,
+			 subflow_req->local_nonce, subflow_req->remote_nonce,
+			 (u32 *)hmac);
+
+	subflow_req->thmac = get_unaligned_be64(hmac);
+
+	sock_put((struct sock *)msk);
+	return true;
+}
+
 static void subflow_v4_init_req(struct request_sock *req,
 				const struct sock *sk_listener,
 				struct sk_buff *skb)
@@ -92,7 +124,7 @@ static void subflow_v4_init_req(struct request_sock *req,
 		subflow_req->remote_nonce = rx_opt.mptcp.nonce;
 		pr_debug("token=%u, remote_nonce=%u", subflow_req->token,
 			 subflow_req->remote_nonce);
-		if (token_join_request(req, skb)) {
+		if (!subflow_token_join_request(req, skb)) {
 			subflow_req->mp_join = 0;
 			// @@ need to trigger RST
 		}
net/mptcp/token.c +0 -44

56 lines not on this page · raw message

Signature · 1 line
2.21.0
author
#9 lore raw

RFC v2 6/10 mptcp: token: fold helpers into token connect/request functions

In reply to Florian Westphal

This also adds error handling -- adding an object to the radix tree might require dynamic memory allocation, so insertions can fail.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.h |   4 +-
 net/mptcp/subflow.c  |  15 ++++--
 net/mptcp/token.c    | 126 ++++++++++++++++++++++---------------------
 3 files changed, 78 insertions(+), 67 deletions(-)
net/mptcp/protocol.h +2 -2
index 8514c41e3a6b..e35dca76fdb3 100644
@@ -225,9 +225,9 @@ void mptcp_get_options(const struct sk_buff *skb,
 void mptcp_finish_connect(struct sock *sk, int mp_capable);
 void mptcp_finish_join(struct sock *sk);
 
-void token_new_request(struct request_sock *req, const struct sk_buff *skb);
+int mptcp_token_new_request(struct request_sock *req);
 void token_destroy_request(u32 token);
-void token_new_connect(struct sock *sk);
+int mptcp_token_new_connect(struct sock *sk);
 void token_new_accept(struct sock *sk);
 void token_update_accept(struct sock *sk, struct sock *conn);
 struct mptcp_sock *mptcp_token_get_sock(u32 token);
net/mptcp/subflow.c +11 -4
index 33bfee514350..16d5df54c533 100644
@@ -21,10 +21,11 @@
 static int subflow_rebuild_header(struct sock *sk)
 {
 	struct subflow_context *subflow = subflow_ctx(sk);
+	int err = 0;
 
 	if (subflow->request_mptcp && !subflow->token) {
 		pr_debug("subflow=%p", sk);
-		token_new_connect(sk);
+		err = mptcp_token_new_connect(sk);
 	} else if (subflow->request_join && !subflow->local_nonce) {
 		pr_debug("subflow=%p", sk);
 		mptcp_token_get_sock(subflow->token);
@@ -34,6 +35,9 @@ static int subflow_rebuild_header(struct sock *sk)
 		} while (!subflow->local_nonce);
 	}
 
+	if (err)
+		return err;
+
 	return inet_sk_rebuild_header(sk);
 }
 
@@ -103,7 +107,12 @@ static void subflow_v4_init_req(struct request_sock *req,
 		return;
 
 	if (rx_opt.mptcp.mp_capable && listener->request_mptcp) {
-		subflow_req->mp_capable = 1;
+		int err;
+
+		err = mptcp_token_new_request(req);
+		if (err == 0)
+			subflow_req->mp_capable = 1;
+
 		if (rx_opt.mptcp.version >= listener->request_version)
 			subflow_req->version = listener->request_version;
 		else
@@ -112,8 +121,6 @@ static void subflow_v4_init_req(struct request_sock *req,
 		    listener->request_cksum)
 			subflow_req->checksum = 1;
 		subflow_req->remote_key = rx_opt.mptcp.sndr_key;
-		pr_debug("remote_key=%llu", subflow_req->remote_key);
-		token_new_request(req, skb);
 		pr_debug("syn seq=%u", TCP_SKB_CB(skb)->seq);
 		subflow_req->ssn_offset = TCP_SKB_CB(skb)->seq;
 	} else if (rx_opt.mptcp.mp_join && listener->request_mptcp) {
net/mptcp/token.c +65 -61

167 lines not on this page · raw message

Signature · 1 line
2.21.0
author
#10 lore raw

RFC v2 7/10 mptcp: token: fold update_token into its caller

In reply to Florian Westphal

The calling function is very small, so just fold this.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c |  2 +-
 net/mptcp/protocol.h |  2 +-
 net/mptcp/token.c    | 38 ++++++++++++++++++--------------------
 3 files changed, 20 insertions(+), 22 deletions(-)
net/mptcp/protocol.c +1 -1
index b408150e46b9..20b04e3cf0a8 100644
@@ -668,7 +668,7 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		msk->remote_key = subflow->remote_key;
 		msk->local_key = subflow->local_key;
 		msk->token = subflow->token;
-		token_update_accept(new_sock->sk, new_mptcp_sock);
+		mptcp_token_update_accept(new_sock->sk, new_mptcp_sock);
 		msk->subflow = NULL;
 
 		pm_new_connection(msk, 1);
net/mptcp/protocol.h +1 -1
index e35dca76fdb3..0e3945e10a79 100644
@@ -229,7 +229,7 @@ int mptcp_token_new_request(struct request_sock *req);
 void token_destroy_request(u32 token);
 int mptcp_token_new_connect(struct sock *sk);
 void token_new_accept(struct sock *sk);
-void token_update_accept(struct sock *sk, struct sock *conn);
+void mptcp_token_update_accept(struct sock *sk, struct sock *conn);
 struct mptcp_sock *mptcp_token_get_sock(u32 token);
 void token_release(u32 token);
 void token_destroy(u32 token);
net/mptcp/token.c +18 -20
index 7d6a5c4e8176..555da89b3a4b 100644
@@ -58,21 +58,6 @@ static int insert_token(u32 token, void *conn)
 	return radix_tree_insert(&token_tree, token, used);
 }
 
-static void update_token(u32 token, void *conn)
-{
-	void **slot;
-
-	pr_debug("token=%u, conn=%p", token, conn);
-	slot = radix_tree_lookup_slot(&token_tree, token);
-	if (slot) {
-		if (*slot != &token_used)
-			pr_err("slot ALREADY updated!");
-		*slot = conn;
-	} else {
-		pr_warn("token NOT FOUND!");
-	}
-}
-
 static void destroy_req_token(u32 token)
 {
 	void *cur;
@@ -194,15 +179,28 @@ void token_new_accept(struct sock *sk)
 	spin_unlock_bh(&token_tree_lock);
 }
 
-void token_update_accept(struct sock *sk, struct sock *conn)
+/**
+ * mptcp_token_update_accept - update token to map to mptcp socket
+ * @conn: the new struct mptcp_sock
+ * @sk: the initial subflow for this mptcp socket
+ *
+ * Called when the first mptcp socket is created on accept to
+ * refresh the dummy mapping (done to reserve the token) with
+ * the mptcp_socket structure that wasn't allocated before.
+ */
+void mptcp_token_update_accept(struct sock *sk, struct sock *conn)
 {
 	struct subflow_context *subflow = subflow_ctx(sk);
-
-	pr_debug("subflow=%p, conn=%p", sk, conn);
+	void __rcu **slot;
 
 	spin_lock_bh(&token_tree_lock);
-	update_token(subflow->token, conn);
-	sock_hold(conn);
+	slot = radix_tree_lookup_slot(&token_tree, subflow->token);
+	WARN_ON_ONCE(!slot);
+	if (slot) {
+		WARN_ON_ONCE(*slot != &token_used);
+		*slot = conn;
+		sock_hold(conn);
+	}
 	spin_unlock_bh(&token_tree_lock);
 }
 
Signature · 1 line
2.21.0
author
#11 lore raw

RFC v2 8/10 mptcp: token: add error handling for token_new_accept

In reply to Florian Westphal

Insertion into radix tree might fail, so add error handling for this.

v2: elide a temporary 'ret' variable (Matthieu)

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.h |  2 +-
 net/mptcp/subflow.c  |  3 ++-
 net/mptcp/token.c    | 31 +++++++++++++++----------------
 3 files changed, 18 insertions(+), 18 deletions(-)
net/mptcp/protocol.h +1 -1
index 0e3945e10a79..f9267730c114 100644
@@ -228,7 +228,7 @@ void mptcp_finish_join(struct sock *sk);
 int mptcp_token_new_request(struct request_sock *req);
 void token_destroy_request(u32 token);
 int mptcp_token_new_connect(struct sock *sk);
-void token_new_accept(struct sock *sk);
+int mptcp_token_new_accept(u32 token);
 void mptcp_token_update_accept(struct sock *sk, struct sock *conn);
 struct mptcp_sock *mptcp_token_get_sock(u32 token);
 void token_release(u32 token);
net/mptcp/subflow.c +2 -1
index 16d5df54c533..9d906709c216 100644
@@ -273,7 +273,8 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 			goto close_child;
 
 		if (ctx->mp_capable) {
-			token_new_accept(child);
+			if (mptcp_token_new_accept(ctx->token))
+				goto close_child;
 		} else if (ctx->mp_join) {
 			struct mptcp_sock *owner;
 
net/mptcp/token.c +15 -16
index 555da89b3a4b..c9645176fb88 100644
@@ -47,17 +47,6 @@ static struct sock *__token_lookup(u32 token)
 	return (struct sock *)conn;
 }
 
-static int insert_token(u32 token, void *conn)
-{
-	void *used = &token_used;
-
-	if (conn)
-		used = conn;
-
-	pr_debug("token=%u, conn=%p", token, used);
-	return radix_tree_insert(&token_tree, token, used);
-}
-
 static void destroy_req_token(u32 token)
 {
 	void *cur;
@@ -168,15 +157,25 @@ int mptcp_token_new_connect(struct sock *sk)
 	return err;
 }
 
-void token_new_accept(struct sock *sk)
+/**
+ * mptcp_token_new_accept - insert token for later processing
+ * @token: the token to insert to the tree
+ *
+ * Called when a SYN packet creates a new logical connection, i.e.
+ * is not a join request.
+ *
+ * We don't have an mptcp socket yet at that point.
+ * This is paired with mptcp_token_update_accept, called on accept().
+ */
+int mptcp_token_new_accept(u32 token)
 {
-	struct subflow_context *subflow = subflow_ctx(sk);
-
-	pr_debug("subflow=%p", sk);
+	int err;
 
 	spin_lock_bh(&token_tree_lock);
-	insert_token(subflow->token, NULL);
+	err = radix_tree_insert(&token_tree, token, &token_used);
 	spin_unlock_bh(&token_tree_lock);
+
+	return err;
 }
 
 /**
Signature · 1 line
2.21.0
author
#12 lore raw

RFC v2 9/10 mptcp: token: rename the destroy_token helpers

In reply to Florian Westphal

add mptcp prefix.

v2: fix typo and remove unneeded temporary variable (Matthieu)

    be sure to keep "conn != &token_used" check (also Matthieu)
Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c |  2 +-
 net/mptcp/protocol.h |  4 ++--
 net/mptcp/subflow.c  |  2 +-
 net/mptcp/token.c    | 56 +++++++++++++++++---------------------------
 4 files changed, 26 insertions(+), 38 deletions(-)
net/mptcp/protocol.c +1 -1
index 20b04e3cf0a8..2a73fa4b0f01 100644
@@ -704,7 +704,7 @@ static void mptcp_destroy(struct sock *sk)
 
 	pr_debug("msk=%p, subflow=%p", sk, msk->subflow->sk);
 
-	token_destroy(msk->token);
+	mptcp_token_destroy(msk->token);
 }
 
 static int mptcp_setsockopt(struct sock *sk, int level, int optname,
net/mptcp/protocol.h +2 -2
index f9267730c114..e8a060e23c8d 100644
@@ -226,13 +226,13 @@ void mptcp_finish_connect(struct sock *sk, int mp_capable);
 void mptcp_finish_join(struct sock *sk);
 
 int mptcp_token_new_request(struct request_sock *req);
-void token_destroy_request(u32 token);
+void mptcp_token_destroy_request(u32 token);
 int mptcp_token_new_connect(struct sock *sk);
 int mptcp_token_new_accept(u32 token);
 void mptcp_token_update_accept(struct sock *sk, struct sock *conn);
 struct mptcp_sock *mptcp_token_get_sock(u32 token);
 void token_release(u32 token);
-void token_destroy(u32 token);
+void mptcp_token_destroy(u32 token);
 
 void crypto_key_sha1(u64 key, u32 *token, u64 *idsn);
 static inline void crypto_key_gen_sha1(u64 *key, u32 *token, u64 *idsn)
net/mptcp/subflow.c +1 -1
index 9d906709c216..b6059a4bb15b 100644
@@ -48,7 +48,7 @@ static void subflow_req_destructor(struct request_sock *req)
 	pr_debug("subflow_req=%p", subflow_req);
 
 	if (subflow_req->mp_capable)
-		token_destroy_request(subflow_req->token);
+		mptcp_token_destroy_request(subflow_req->token);
 	tcp_request_sock_ops.destructor(req);
 }
 
net/mptcp/token.c +22 -34

79 lines not on this page · raw message

Signature · 1 line
2.21.0
author
#13 lore raw

RFC v2 10/10 mptcp: token: rename token_release

In reply to Florian Westphal

avoid namespace pollution -- add an mptcp prefix.
Also, we can place the sock_put after we've released the token tree lock.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.h |  2 +-
 net/mptcp/subflow.c  |  3 +--
 net/mptcp/token.c    | 12 +++++++++---
 3 files changed, 11 insertions(+), 6 deletions(-)
net/mptcp/protocol.h +1 -1
index e8a060e23c8d..bbb0c92274d3 100644
@@ -231,7 +231,7 @@ int mptcp_token_new_connect(struct sock *sk);
 int mptcp_token_new_accept(u32 token);
 void mptcp_token_update_accept(struct sock *sk, struct sock *conn);
 struct mptcp_sock *mptcp_token_get_sock(u32 token);
-void token_release(u32 token);
+void mptcp_token_release(u32 token);
 void mptcp_token_destroy(u32 token);
 
 void crypto_key_sha1(u64 key, u32 *token, u64 *idsn);
net/mptcp/subflow.c +1 -2
index b6059a4bb15b..a9a3bb9aef2d 100644
@@ -440,8 +440,7 @@ static void subflow_ulp_release(struct sock *sk)
 	struct subflow_context *ctx = subflow_ctx(sk);
 
 	pr_debug("subflow=%p", ctx);
-
-	token_release(ctx->token);
+	mptcp_token_release(ctx->token);
 
 	kfree(ctx);
 }
net/mptcp/token.c +9 -3
index 5bd3d0c78d04..2b1b571d49cd 100644
@@ -215,16 +215,22 @@ void mptcp_token_destroy_request(u32 token)
 	spin_unlock_bh(&token_tree_lock);
 }
 
-void token_release(u32 token)
+/**
+ * mptcp_token_release - put reference on token without withdrawing it
+ * @token - token of mptcp connection that the subflow is "leaving"
+ *
+ * This is used to close/remove a subflow without removing the
+ * mptcp connection from the token <-> mptcp_sk map.
+ */
+void mptcp_token_release(u32 token)
 {
 	struct sock *conn;
 
-	pr_debug("token=%u", token);
 	spin_lock_bh(&token_tree_lock);
 	conn = __token_lookup(token);
+	spin_unlock_bh(&token_tree_lock);
 	if (conn)
 		sock_put(conn);
-	spin_unlock_bh(&token_tree_lock);
 }
 
 /**
Signature · 1 line
2.21.0
#4 lore raw

Re: [MPTCP] [RFC v2 03/10] mptcp: token: rename token_join_response

In reply to Florian Westphal

Hi Florian,

Quoted 35 lines · Florian Westphal

This function validates the truncated hmac and computes a hmac for use in the ack packet. Rename it, place it where its used, and do the hmac computation in the caller.

This also adds a pr_fmt specifier, so the included pr_debug will auto-gain 'MPTCP' prefix.

v2: change name and place hmac calculation in the caller (Peter)
add pr_fmt too
squash two pr_debug() into one.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.h |  1 -
 net/mptcp/subflow.c  | 34 ++++++++++++++++++++++++++++++----
 net/mptcp/token.c    | 31 -------------------------------
 3 files changed, 30 insertions(+), 36 deletions(-)
net/mptcp/protocol.h +0 -1
index 1655283dbd6a..89bd68f85856 100644
@@ -227,7 +227,6 @@ void mptcp_finish_join(struct sock *sk);
 
 void token_new_request(struct request_sock *req, const struct sk_buff *skb);
 int token_join_request(struct request_sock *req, const struct sk_buff *skb);
-int token_join_response(struct sock *sk);
 int token_join_valid(struct request_sock *req,
 		     struct tcp_options_received *rx_opt);
 void token_destroy_request(u32 token);
net/mptcp/subflow.c +0 -0
index fc507e091cf5..8a8109466487 100644

net/mptcp/subflow.c line 7

Quoted 4 lines
net/mptcp/subflow.c +1 -0
@@ -4,6 +4,8 @@
  * Copyright (c) 2017 - 2019, Intel Corporation.
  */
 
+#define pr_fmt(fmt) "MPTCP: " fmt

Good idea to add this!

A small detail: should we also add "subflow:" (or "sf:")?

    "MPTCP:sf: "

Cheers,
Matt

Signature · 5 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium
#14 lore raw
In reply to Florian Westphal

Hi Florian,

Quoted 8 lines · Florian Westphal

This series again marked RFC so noone bothers to squash all of this into the mptcp tree.

In this second round, I've addressed all comments from Matthieu and Peter. As a result, even more code was pruned from token.c and placed as static helper in subflow.c.

I've added a 'v2' mini-changelog to all affected patches.

Thank you for this new v2!

After a quick review, it looks good to me. Just a small detail, maybe not relevant but no need to send a new version just for that.

I guess Peter will do another pass on this patch set.

Cheers,
Matt

Signature · 5 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium
author
#5 lore raw

Re: [MPTCP] [RFC v2 03/10] mptcp: token: rename token_join_response

In reply to Matthieu Baerts
Quoted 3 lines · Matthieu Baerts

A small detail: should we also add "subflow:" (or "sf:")?

    "MPTCP:sf: "

I modeled it after what is done for builtin code in net/, e.g.:

net/ipv4/ip_fragment.c:#define pr_fmt(fmt) "IPv4: " fmt net/ipv4/ip_input.c:#define pr_fmt(fmt) "IPv4: " fmt net/ipv4/ip_options.c:#define pr_fmt(fmt) "IPv4: " fmt net/ipv4/route.c:#define pr_fmt(fmt) "IPv4: " fmt net/ipv4/tcp.c:#define pr_fmt(fmt) "TCP: " fmt net/ipv4/tcp_cong.c:#define pr_fmt(fmt) "TCP: " fmt net/ipv4/tcp_input.c:#define pr_fmt(fmt) "TCP: " fmt net/ipv4/tcp_ipv4.c:#define pr_fmt(fmt) "TCP: " fmt net/ipv4/tcp_output.c:#define pr_fmt(fmt) "TCP: " fmt net/ipv4/udp.c:#define pr_fmt(fmt) "UDP: " fmt net/ipv4/udplite.c:#define pr_fmt(fmt) "UDPLite: " fmt net/ipv4/xfrm4_tunnel.c:#define pr_fmt(fmt) "IPsec: " fmt net/ipv6/addrconf.c:#define pr_fmt(fmt) "IPv6: " fmt net/ipv6/af_inet6.c:#define pr_fmt(fmt) "IPv6: " fmt net/ipv6/ah6.c:#define pr_fmt(fmt) "IPv6: " fmt net/ipv6/esp6.c:#define pr_fmt(fmt) "IPv6: " fmt
...

So single "MPTCP: " seems more consistent with rest of kernel. I have no strong opinion though, if others want more detailed prefix too I can change it of course.

#6 lore raw

Re: [MPTCP] [RFC v2 03/10] mptcp: token: rename token_join_response

Hi Florian,

Quoted 6 lines · Florian Westphal
Quoted 3 lines · Matthieu Baerts

A small detail: should we also add "subflow:" (or "sf:")?

    "MPTCP:sf: "

I modeled it after what is done for builtin code in net/, e.g.:

Thank you for having looking at that!

Quoted 18 lines

net/ipv4/ip_fragment.c:#define pr_fmt(fmt) "IPv4: " fmt net/ipv4/ip_input.c:#define pr_fmt(fmt) "IPv4: " fmt net/ipv4/ip_options.c:#define pr_fmt(fmt) "IPv4: " fmt net/ipv4/route.c:#define pr_fmt(fmt) "IPv4: " fmt net/ipv4/tcp.c:#define pr_fmt(fmt) "TCP: " fmt net/ipv4/tcp_cong.c:#define pr_fmt(fmt) "TCP: " fmt net/ipv4/tcp_input.c:#define pr_fmt(fmt) "TCP: " fmt net/ipv4/tcp_ipv4.c:#define pr_fmt(fmt) "TCP: " fmt net/ipv4/tcp_output.c:#define pr_fmt(fmt) "TCP: " fmt net/ipv4/udp.c:#define pr_fmt(fmt) "UDP: " fmt net/ipv4/udplite.c:#define pr_fmt(fmt) "UDPLite: " fmt net/ipv4/xfrm4_tunnel.c:#define pr_fmt(fmt) "IPsec: " fmt net/ipv6/addrconf.c:#define pr_fmt(fmt) "IPv6: " fmt net/ipv6/af_inet6.c:#define pr_fmt(fmt) "IPv6: " fmt net/ipv6/ah6.c:#define pr_fmt(fmt) "IPv6: " fmt net/ipv6/esp6.c:#define pr_fmt(fmt) "IPv6: " fmt
...

Quoted 3 lines

So single "MPTCP: " seems more consistent with rest of kernel. I have no strong opinion though, if others want more detailed prefix too I can change it of course.

I also don't have a strong opinion on that, I am fine to follow what seems to be the standard :-)

Cheers,
Matt

Signature · 5 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium
#15 lore raw
In reply to Florian Westphal

LGTM. I like the debug formatting too.

Peter.

Quoted 21 lines · Florian Westphal

This series again marked RFC so noone bothers to squash all of this into the mptcp tree.

In this second round, I've addressed all comments from Matthieu and Peter. As a result, even more code was pruned from token.c and placed as static helper in subflow.c.

I've added a 'v2' mini-changelog to all affected patches.

 pm.c       |    6 
 protocol.c |    5 
 protocol.h |   23 +--
 subflow.c  |  152 +++++++++++++++++++--
 token.c    |  426 +++++++++++++++++++------------------------------------------
 5 files changed, 277 insertions(+), 333 deletions(-)

_______________________________________________
mptcp mailing list
mptcp(a)lists.01.org
https://lists.01.org/mailman/listinfo/mptcp