Skip to content

Commit

Permalink
mptcp: Add key generation and token tree
Browse files Browse the repository at this point in the history
Generate the local keys, IDSN, and token when creating a new socket.
Introduce the token tree to track all tokens in use using a radix tree
with the MPTCP token itself as the index.

Override the rebuild_header callback in inet_connection_sock_af_ops for
creating the local key on a new outgoing connection.

Override the init_req callback of tcp_request_sock_ops for creating the
local key on a new incoming connection.

Will be used to obtain the MPTCP parent socket to handle incoming joins.

Co-developed-by: Davide Caratti <dcaratti@redhat.com>
Signed-off-by: Davide Caratti <dcaratti@redhat.com>
Co-developed-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Florian Westphal <fw@strlen.de>
Co-developed-by: Paolo Abeni <pabeni@redhat.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
Signed-off-by: Peter Krystad <peter.krystad@linux.intel.com>
Signed-off-by: Christoph Paasch <cpaasch@apple.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Peter Krystad authored and davem330 committed Jan 24, 2020
1 parent cf7da0d commit 79c0949
Show file tree
Hide file tree
Showing 6 changed files with 428 additions and 8 deletions.
2 changes: 1 addition & 1 deletion net/mptcp/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_MPTCP) += mptcp.o

mptcp-y := protocol.o subflow.o options.o
mptcp-y := protocol.o subflow.o options.o token.o crypto.o
122 changes: 122 additions & 0 deletions net/mptcp/crypto.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// SPDX-License-Identifier: GPL-2.0
/* Multipath TCP cryptographic functions
* Copyright (c) 2017 - 2019, Intel Corporation.
*
* Note: This code is based on mptcp_ctrl.c, mptcp_ipv4.c, and
* mptcp_ipv6 from multipath-tcp.org, authored by:
*
* Sébastien Barré <sebastien.barre@uclouvain.be>
* Christoph Paasch <christoph.paasch@uclouvain.be>
* Jaakko Korkeaniemi <jaakko.korkeaniemi@aalto.fi>
* Gregory Detal <gregory.detal@uclouvain.be>
* Fabien Duchêne <fabien.duchene@uclouvain.be>
* Andreas Seelinger <Andreas.Seelinger@rwth-aachen.de>
* Lavkesh Lahngir <lavkesh51@gmail.com>
* Andreas Ripke <ripke@neclab.eu>
* Vlad Dogaru <vlad.dogaru@intel.com>
* Octavian Purdila <octavian.purdila@intel.com>
* John Ronan <jronan@tssg.org>
* Catalin Nicutar <catalin.nicutar@gmail.com>
* Brandon Heller <brandonh@stanford.edu>
*/

#include <linux/kernel.h>
#include <linux/cryptohash.h>
#include <asm/unaligned.h>

#include "protocol.h"

struct sha1_state {
u32 workspace[SHA_WORKSPACE_WORDS];
u32 digest[SHA_DIGEST_WORDS];
unsigned int count;
};

static void sha1_init(struct sha1_state *state)
{
sha_init(state->digest);
state->count = 0;
}

static void sha1_update(struct sha1_state *state, u8 *input)
{
sha_transform(state->digest, input, state->workspace);
state->count += SHA_MESSAGE_BYTES;
}

static void sha1_pad_final(struct sha1_state *state, u8 *input,
unsigned int length, __be32 *mptcp_hashed_key)
{
int i;

input[length] = 0x80;
memset(&input[length + 1], 0, SHA_MESSAGE_BYTES - length - 9);
put_unaligned_be64((length + state->count) << 3,
&input[SHA_MESSAGE_BYTES - 8]);

sha_transform(state->digest, input, state->workspace);
for (i = 0; i < SHA_DIGEST_WORDS; ++i)
put_unaligned_be32(state->digest[i], &mptcp_hashed_key[i]);

memzero_explicit(state->workspace, SHA_WORKSPACE_WORDS << 2);
}

void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn)
{
__be32 mptcp_hashed_key[SHA_DIGEST_WORDS];
u8 input[SHA_MESSAGE_BYTES];
struct sha1_state state;

sha1_init(&state);
put_unaligned_be64(key, input);
sha1_pad_final(&state, input, 8, mptcp_hashed_key);

if (token)
*token = be32_to_cpu(mptcp_hashed_key[0]);
if (idsn)
*idsn = be64_to_cpu(*((__be64 *)&mptcp_hashed_key[3]));
}

void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
u32 *hash_out)
{
u8 input[SHA_MESSAGE_BYTES * 2];
struct sha1_state state;
u8 key1be[8];
u8 key2be[8];
int i;

put_unaligned_be64(key1, key1be);
put_unaligned_be64(key2, key2be);

/* Generate key xored with ipad */
memset(input, 0x36, SHA_MESSAGE_BYTES);
for (i = 0; i < 8; i++)
input[i] ^= key1be[i];
for (i = 0; i < 8; i++)
input[i + 8] ^= key2be[i];

put_unaligned_be32(nonce1, &input[SHA_MESSAGE_BYTES]);
put_unaligned_be32(nonce2, &input[SHA_MESSAGE_BYTES + 4]);

sha1_init(&state);
sha1_update(&state, input);

/* emit sha256(K1 || msg) on the second input block, so we can
* reuse 'input' for the last hashing
*/
sha1_pad_final(&state, &input[SHA_MESSAGE_BYTES], 8,
(__force __be32 *)&input[SHA_MESSAGE_BYTES]);

/* Prepare second part of hmac */
memset(input, 0x5C, SHA_MESSAGE_BYTES);
for (i = 0; i < 8; i++)
input[i] ^= key1be[i];
for (i = 0; i < 8; i++)
input[i + 8] ^= key2be[i];

sha1_init(&state);
sha1_update(&state, input);
sha1_pad_final(&state, &input[SHA_MESSAGE_BYTES], SHA_DIGEST_WORDS << 2,
(__be32 *)hash_out);
}
16 changes: 16 additions & 0 deletions net/mptcp/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ static void mptcp_close(struct sock *sk, long timeout)
struct mptcp_subflow_context *subflow, *tmp;
struct mptcp_sock *msk = mptcp_sk(sk);

mptcp_token_destroy(msk->token);
inet_sk_state_store(sk, TCP_CLOSE);

lock_sock(sk);
Expand Down Expand Up @@ -281,8 +282,10 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
msk = mptcp_sk(new_mptcp_sock);
msk->remote_key = subflow->remote_key;
msk->local_key = subflow->local_key;
msk->token = subflow->token;
msk->subflow = NULL;

mptcp_token_update_accept(newsk, new_mptcp_sock);
newsk = new_mptcp_sock;
mptcp_copy_inaddrs(newsk, ssk);
list_add(&subflow->node, &msk->conn_list);
Expand All @@ -299,6 +302,10 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
return newsk;
}

static void mptcp_destroy(struct sock *sk)
{
}

static int mptcp_get_port(struct sock *sk, unsigned short snum)
{
struct mptcp_sock *msk = mptcp_sk(sk);
Expand Down Expand Up @@ -331,6 +338,7 @@ void mptcp_finish_connect(struct sock *ssk)
*/
WRITE_ONCE(msk->remote_key, subflow->remote_key);
WRITE_ONCE(msk->local_key, subflow->local_key);
WRITE_ONCE(msk->token, subflow->token);
}

static void mptcp_sock_graft(struct sock *sk, struct socket *parent)
Expand All @@ -349,6 +357,7 @@ static struct proto mptcp_prot = {
.close = mptcp_close,
.accept = mptcp_accept,
.shutdown = tcp_shutdown,
.destroy = mptcp_destroy,
.sendmsg = mptcp_sendmsg,
.recvmsg = mptcp_recvmsg,
.hash = inet_hash,
Expand Down Expand Up @@ -568,6 +577,12 @@ void __init mptcp_init(void)
static struct proto_ops mptcp_v6_stream_ops;
static struct proto mptcp_v6_prot;

static void mptcp_v6_destroy(struct sock *sk)
{
mptcp_destroy(sk);
inet6_destroy_sock(sk);
}

static struct inet_protosw mptcp_v6_protosw = {
.type = SOCK_STREAM,
.protocol = IPPROTO_MPTCP,
Expand All @@ -583,6 +598,7 @@ int mptcpv6_init(void)
mptcp_v6_prot = mptcp_prot;
strcpy(mptcp_v6_prot.name, "MPTCPv6");
mptcp_v6_prot.slab = NULL;
mptcp_v6_prot.destroy = mptcp_v6_destroy;
mptcp_v6_prot.obj_size = sizeof(struct mptcp_sock) +
sizeof(struct ipv6_pinfo);

Expand Down
32 changes: 32 additions & 0 deletions net/mptcp/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#ifndef __MPTCP_PROTOCOL_H
#define __MPTCP_PROTOCOL_H

#include <linux/random.h>
#include <net/tcp.h>
#include <net/inet_connection_sock.h>

#define MPTCP_SUPPORTED_VERSION 0

/* MPTCP option bits */
Expand Down Expand Up @@ -42,6 +46,7 @@ struct mptcp_sock {
struct inet_connection_sock sk;
u64 local_key;
u64 remote_key;
u32 token;
struct list_head conn_list;
struct socket *subflow; /* outgoing connect/listener/!mp_capable */
};
Expand All @@ -61,6 +66,8 @@ struct mptcp_subflow_request_sock {
backup : 1;
u64 local_key;
u64 remote_key;
u64 idsn;
u32 token;
};

static inline struct mptcp_subflow_request_sock *
Expand All @@ -74,6 +81,8 @@ struct mptcp_subflow_context {
struct list_head node;/* conn_list of subflows */
u64 local_key;
u64 remote_key;
u64 idsn;
u32 token;
u32 request_mptcp : 1, /* send MP_CAPABLE */
mp_capable : 1, /* remote is MPTCP capable */
fourth_ack : 1, /* send initial DSS */
Expand Down Expand Up @@ -112,4 +121,27 @@ void mptcp_get_options(const struct sk_buff *skb,

void mptcp_finish_connect(struct sock *sk);

int mptcp_token_new_request(struct request_sock *req);
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);
void mptcp_token_destroy(u32 token);

void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn);
static inline void mptcp_crypto_key_gen_sha(u64 *key, u32 *token, u64 *idsn)
{
/* we might consider a faster version that computes the key as a
* hash of some information available in the MPTCP socket. Use
* random data at the moment, as it's probably the safest option
* in case multiple sockets are opened in different namespaces at
* the same time.
*/
get_random_bytes(key, sizeof(u64));
mptcp_crypto_key_sha(*key, token, idsn);
}

void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
u32 *hash_out);

#endif /* __MPTCP_PROTOCOL_H */
Loading

0 comments on commit 79c0949

Please sign in to comment.