Skip to contentSkip to conversation

Add support for v1 of ADD_ADDR option

PATCH Superseded Peter Krystad 1 participant 3 messages Feb 25, 2020

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Peter Krystad

author
#0 lore raw

v2 cover Add support for v1 of ADD_ADDR option

Support parsing and writing the new option format, with hmac generation and validation.

Support for echoing ADD_ADDR still to come.

v2: check msg len in mptcp_crypto_hmac_sha().
remove obsolete casts

Peter Krystad (2):
mptcp: Re-factor mptcp_crypto_hmac_sha()
mptcp: Make v1 changes for ADD_ADDR and RM_ADDR options

 include/linux/tcp.h  |  24 +++---
 include/net/mptcp.h  |   2 +
 net/mptcp/crypto.c   |  17 ++--
 net/mptcp/options.c  | 180 ++++++++++++++++++++++++++++++++++---------
 net/mptcp/protocol.h |  20 +++--
 net/mptcp/subflow.c  |  19 ++++-
 6 files changed, 199 insertions(+), 63 deletions(-)
Signature · 1 line
2.17.2
author
#1 lore raw

PATCH v2 1/2 mptcp: Re-factor mptcp_crypto_hmac_sha()

Allow it to take variable-length messages so that v1 ADD_ADDR option processing may use it.

squashto: Add ADD_ADDR handling

Signed-off-by Peter Krystad <peter.krystad(a)linux.intel.com>
---
 net/mptcp/crypto.c   | 17 +++++++++++------
 net/mptcp/protocol.h |  3 +--
 net/mptcp/subflow.c  | 19 +++++++++++++++----
 3 files changed, 27 insertions(+), 12 deletions(-)
net/mptcp/crypto.c +11 -6
index 40d1bb18fd60..c151628bd416 100644
@@ -44,8 +44,7 @@ void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn)
 		*idsn = be64_to_cpu(*((__be64 *)&mptcp_hashed_key[6]));
 }
 
-void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
-			   void *hmac)
+void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)
 {
 	u8 input[SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE];
 	__be32 mptcp_hashed_key[SHA256_DIGEST_WORDS];
@@ -55,6 +54,9 @@ void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
 	u8 key2be[8];
 	int i;
 
+	if (WARN_ON_ONCE(len > SHA256_DIGEST_SIZE))
+		len = SHA256_DIGEST_SIZE;
+
 	put_unaligned_be64(key1, key1be);
 	put_unaligned_be64(key2, key2be);
 
@@ -65,11 +67,10 @@ void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
 	for (i = 0; i < 8; i++)
 		input[i + 8] ^= key2be[i];
 
-	put_unaligned_be32(nonce1, &input[SHA256_BLOCK_SIZE]);
-	put_unaligned_be32(nonce2, &input[SHA256_BLOCK_SIZE + 4]);
+	memcpy(&input[SHA256_BLOCK_SIZE], msg, len);
 
 	sha256_init(&state);
-	sha256_update(&state, input, SHA256_BLOCK_SIZE + 8);
+	sha256_update(&state, input, SHA256_BLOCK_SIZE + len);
 
 	/* emit sha256(K1 || msg) on the second input block, so we can
 	 * reuse 'input' for the last hashing
@@ -125,6 +126,7 @@ static int __init test_mptcp_crypto(void)
 	char hmac[20], hmac_hex[41];
 	u32 nonce1, nonce2;
 	u64 key1, key2;
+	u8 msg[8];
 	int i, j;
 
 	for (i = 0; i < ARRAY_SIZE(tests); ++i) {
@@ -134,7 +136,10 @@ static int __init test_mptcp_crypto(void)
 		nonce1 = be32_to_cpu(*((__be32 *)&tests[i].msg[0]));
 		nonce2 = be32_to_cpu(*((__be32 *)&tests[i].msg[4]));
 
-		mptcp_crypto_hmac_sha(key1, key2, nonce1, nonce2, hmac);
+		put_unaligned_be32(nonce1, &msg[0]);
+		put_unaligned_be32(nonce2, &msg[4]);
+
+		mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
 		for (j = 0; j < 20; ++j)
 			sprintf(&hmac_hex[j << 1], "%02x", hmac[j] & 0xff);
 		hmac_hex[40] = 0;
net/mptcp/protocol.h +1 -2
index 944f14ac3017..e5a50930869d 100644
@@ -339,8 +339,7 @@ static inline void mptcp_crypto_key_gen_sha(u64 *key, u32 *token, u64 *idsn)
 	mptcp_crypto_key_sha(*key, token, idsn);
 }
 
-void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
-			   void *hash_out);
+void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac);
 
 void mptcp_pm_init(void);
 void mptcp_pm_new_connection(struct mptcp_sock *msk, int server_side);
net/mptcp/subflow.c +15 -4
index 547d5ffef070..8a41f6b661c9 100644
@@ -61,6 +61,17 @@ static void subflow_req_destructor(struct request_sock *req)
 	tcp_request_sock_ops.destructor(req);
 }
 
+static void subflow_generate_hmac(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
+				  void *hmac)
+{
+	u8 msg[8];
+
+	put_unaligned_be32(nonce1, &msg[0]);
+	put_unaligned_be32(nonce2, &msg[4]);
+
+	mptcp_crypto_hmac_sha(key1, key2, msg, 8, (u32 *)hmac);
+}
+
 /* 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)
@@ -82,7 +93,7 @@ static bool subflow_token_join_request(struct request_sock *req,
 
 	get_random_bytes(&subflow_req->local_nonce, sizeof(u32));
 
-	mptcp_crypto_hmac_sha(msk->local_key, msk->remote_key,
+	subflow_generate_hmac(msk->local_key, msk->remote_key,
 			      subflow_req->local_nonce,
 			      subflow_req->remote_nonce, (u32 *)hmac);
 
@@ -180,7 +191,7 @@ static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow)
 	u8 hmac[MPTCPOPT_HMAC_LEN];
 	u64 thmac;
 
-	mptcp_crypto_hmac_sha(subflow->remote_key, subflow->local_key,
+	subflow_generate_hmac(subflow->remote_key, subflow->local_key,
 			      subflow->remote_nonce, subflow->local_nonce,
 			      (u32 *)hmac);
 
@@ -225,7 +236,7 @@ static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
 			goto do_reset;
 		}
 
-		mptcp_crypto_hmac_sha(subflow->local_key, subflow->remote_key,
+		subflow_generate_hmac(subflow->local_key, subflow->remote_key,
 				      subflow->local_nonce,
 				      subflow->remote_nonce,
 				      (u32 *)subflow->hmac);
@@ -305,7 +316,7 @@ static bool subflow_hmac_valid(const struct request_sock *req,
 	if (!msk)
 		return false;
 
-	mptcp_crypto_hmac_sha(msk->remote_key, msk->local_key,
+	subflow_generate_hmac(msk->remote_key, msk->local_key,
 			      subflow_req->remote_nonce,
 			      subflow_req->local_nonce, (u32 *)hmac);
 
Signature · 1 line
2.17.2
author
#2 lore raw

PATCH v2 2/2 mptcp: Make v1 changes for ADD_ADDR and RM_ADDR options

In reply to Peter Krystad

Remove family field and add hmac and port fields to struct mptcp_options_received. Parse and validate incoming hmac on ADD_ADDR option, and send hmac with outgoing option.

squashto: Add ADD_ADDR handling

Signed-off-by Peter Krystad <peter.krystad(a)linux.intel.com>
---
 include/linux/tcp.h  |  24 +++---
 include/net/mptcp.h  |   2 +
 net/mptcp/options.c  | 180 ++++++++++++++++++++++++++++++++++---------
 net/mptcp/protocol.h |  17 ++--
 4 files changed, 172 insertions(+), 51 deletions(-)
include/linux/tcp.h +14 -10
index 0c75d7919f9a..3d551f74695c 100644
@@ -86,9 +86,22 @@ struct mptcp_options_received {
 	u64	data_seq;
 	u32	subflow_seq;
 	u16	data_len;
+	union {
+		struct in_addr	addr;
+#if IS_ENABLED(CONFIG_MPTCP_IPV6)
+		struct in6_addr	addr6;
+#endif
+	};
+	u64	ahmac;
+	u8	addr_id;
+	u8	rm_id;
 	u8	mp_capable : 1,
 		mp_join : 1,
 		dss : 1,
+		add_addr : 1,
+		add_addr6 : 1,
+		rm_addr : 1,
+		echo : 1,
 		backup : 1;
 	u8	join_id;
 	u32	token;
@@ -102,16 +115,6 @@ struct mptcp_options_received {
 		ack64:1,
 		mpc_map:1,
 		__unused:2;
-	u8	add_addr : 1,
-		rm_addr : 1,
-		family : 4;
-	u8	addr_id;
-	union {
-		struct	in_addr	addr;
-#if IS_ENABLED(CONFIG_IPV6)
-		struct	in6_addr addr6;
-#endif
-	};
 };
 #endif
 
@@ -148,6 +151,7 @@ static inline void tcp_clear_options(struct tcp_options_received *rx_opt)
 	rx_opt->mptcp.mp_capable = 0;
 	rx_opt->mptcp.mp_join = 0;
 	rx_opt->mptcp.add_addr = 0;
+	rx_opt->mptcp.add_addr6 = 0;
 	rx_opt->mptcp.rm_addr = 0;
 	rx_opt->mptcp.dss = 0;
 #endif
include/net/mptcp.h +2 -0
index 7489f9267640..0e7c5471010b 100644
@@ -42,6 +42,8 @@ struct mptcp_out_options {
 #endif
 	};
 	u8 addr_id;
+	u64 ahmac;
+	u8 rm_id;
 	u8 join_id;
 	u8 backup;
 	u32 nonce;
net/mptcp/options.c +145 -35

254 lines not on this page · raw message

net/mptcp/protocol.h +11 -6
index e5a50930869d..aa5250d49f94 100644
@@ -48,9 +48,16 @@
 #define TCPOLEN_MPTCP_DSS_MAP32		10
 #define TCPOLEN_MPTCP_DSS_MAP64		14
 #define TCPOLEN_MPTCP_DSS_CHECKSUM	2
-#define TCPOLEN_MPTCP_ADD_ADDR		8
-#define TCPOLEN_MPTCP_ADD_ADDR6		20
-#define TCPOLEN_MPTCP_RM_ADDR		4
+#define TCPOLEN_MPTCP_ADD_ADDR		16
+#define TCPOLEN_MPTCP_ADD_ADDR_PORT	18
+#define TCPOLEN_MPTCP_ADD_ADDR_BASE	8
+#define TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT	10
+#define TCPOLEN_MPTCP_ADD_ADDR6		28
+#define TCPOLEN_MPTCP_ADD_ADDR6_PORT	30
+#define TCPOLEN_MPTCP_ADD_ADDR6_BASE	20
+#define TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT	22
+#define TCPOLEN_MPTCP_PORT_LEN		2
+#define TCPOLEN_MPTCP_RM_ADDR_BASE	4
 
 /* MPTCP MP_JOIN flags */
 #define MPTCPOPT_BACKUP		BIT(0)
@@ -73,9 +80,7 @@
 #define MPTCP_DSS_FLAG_MASK	(0x1F)
 
 /* MPTCP ADD_ADDR flags */
-#define MPTCP_ADDR_FAMILY_MASK	(0x0F)
-#define MPTCP_ADDR_IPVERSION_4	4
-#define MPTCP_ADDR_IPVERSION_6	6
+#define MPTCP_ADDR_ECHO		BIT(0)
 
 /* MPTCP socket flags */
 #define MPTCP_DATA_READY	0
Signature · 1 line
2.17.2