Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Multipath TCP token management |
| 3 | * Copyright (c) 2017 - 2019, Intel Corporation. |
| 4 | * |
| 5 | * Note: This code is based on mptcp_ctrl.c from multipath-tcp.org, |
| 6 | * authored by: |
| 7 | * |
| 8 | * Sébastien Barré <sebastien.barre@uclouvain.be> |
| 9 | * Christoph Paasch <christoph.paasch@uclouvain.be> |
| 10 | * Jaakko Korkeaniemi <jaakko.korkeaniemi@aalto.fi> |
| 11 | * Gregory Detal <gregory.detal@uclouvain.be> |
| 12 | * Fabien Duchêne <fabien.duchene@uclouvain.be> |
| 13 | * Andreas Seelinger <Andreas.Seelinger@rwth-aachen.de> |
| 14 | * Lavkesh Lahngir <lavkesh51@gmail.com> |
| 15 | * Andreas Ripke <ripke@neclab.eu> |
| 16 | * Vlad Dogaru <vlad.dogaru@intel.com> |
| 17 | * Octavian Purdila <octavian.purdila@intel.com> |
| 18 | * John Ronan <jronan@tssg.org> |
| 19 | * Catalin Nicutar <catalin.nicutar@gmail.com> |
| 20 | * Brandon Heller <brandonh@stanford.edu> |
| 21 | */ |
| 22 | |
| 23 | #define pr_fmt(fmt) "MPTCP: " fmt |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/radix-tree.h> |
| 28 | #include <linux/ip.h> |
| 29 | #include <linux/tcp.h> |
| 30 | #include <net/sock.h> |
| 31 | #include <net/inet_common.h> |
| 32 | #include <net/protocol.h> |
| 33 | #include <net/mptcp.h> |
| 34 | #include "protocol.h" |
| 35 | |
| 36 | static RADIX_TREE(token_tree, GFP_ATOMIC); |
| 37 | static RADIX_TREE(token_req_tree, GFP_ATOMIC); |
| 38 | static DEFINE_SPINLOCK(token_tree_lock); |
| 39 | static int token_used __read_mostly; |
| 40 | |
| 41 | /** |
| 42 | * mptcp_token_new_request - create new key/idsn/token for subflow_request |
| 43 | * @req - the request socket |
| 44 | * |
| 45 | * This function is called when a new mptcp connection is coming in. |
| 46 | * |
| 47 | * It creates a unique token to identify the new mptcp connection, |
| 48 | * a secret local key and the initial data sequence number (idsn). |
| 49 | * |
| 50 | * Returns 0 on success. |
| 51 | */ |
| 52 | int mptcp_token_new_request(struct request_sock *req) |
| 53 | { |
| 54 | struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req); |
| 55 | int err; |
| 56 | |
| 57 | while (1) { |
| 58 | u32 token; |
| 59 | |
| 60 | mptcp_crypto_key_gen_sha(&subflow_req->local_key, |
| 61 | &subflow_req->token, |
| 62 | &subflow_req->idsn); |
| 63 | pr_debug("req=%p local_key=%llu, token=%u, idsn=%llu\n", |
| 64 | req, subflow_req->local_key, subflow_req->token, |
| 65 | subflow_req->idsn); |
| 66 | |
| 67 | token = subflow_req->token; |
| 68 | spin_lock_bh(&token_tree_lock); |
| 69 | if (!radix_tree_lookup(&token_req_tree, token) && |
| 70 | !radix_tree_lookup(&token_tree, token)) |
| 71 | break; |
| 72 | spin_unlock_bh(&token_tree_lock); |
| 73 | } |
| 74 | |
| 75 | err = radix_tree_insert(&token_req_tree, |
| 76 | subflow_req->token, &token_used); |
| 77 | spin_unlock_bh(&token_tree_lock); |
| 78 | return err; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * mptcp_token_new_connect - create new key/idsn/token for subflow |
| 83 | * @sk - the socket that will initiate a connection |
| 84 | * |
| 85 | * This function is called when a new outgoing mptcp connection is |
| 86 | * initiated. |
| 87 | * |
| 88 | * It creates a unique token to identify the new mptcp connection, |
| 89 | * a secret local key and the initial data sequence number (idsn). |
| 90 | * |
| 91 | * On success, the mptcp connection can be found again using |
| 92 | * the computed token at a later time, this is needed to process |
| 93 | * join requests. |
| 94 | * |
| 95 | * returns 0 on success. |
| 96 | */ |
| 97 | int mptcp_token_new_connect(struct sock *sk) |
| 98 | { |
| 99 | struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); |
| 100 | struct sock *mptcp_sock = subflow->conn; |
| 101 | int err; |
| 102 | |
| 103 | while (1) { |
| 104 | u32 token; |
| 105 | |
| 106 | mptcp_crypto_key_gen_sha(&subflow->local_key, &subflow->token, |
| 107 | &subflow->idsn); |
| 108 | |
| 109 | pr_debug("ssk=%p, local_key=%llu, token=%u, idsn=%llu\n", |
| 110 | sk, subflow->local_key, subflow->token, subflow->idsn); |
| 111 | |
| 112 | token = subflow->token; |
| 113 | spin_lock_bh(&token_tree_lock); |
| 114 | if (!radix_tree_lookup(&token_req_tree, token) && |
| 115 | !radix_tree_lookup(&token_tree, token)) |
| 116 | break; |
| 117 | spin_unlock_bh(&token_tree_lock); |
| 118 | } |
| 119 | err = radix_tree_insert(&token_tree, subflow->token, mptcp_sock); |
| 120 | spin_unlock_bh(&token_tree_lock); |
| 121 | |
| 122 | return err; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * mptcp_token_new_accept - insert token for later processing |
| 127 | * @token: the token to insert to the tree |
| 128 | * |
| 129 | * Called when a SYN packet creates a new logical connection, i.e. |
| 130 | * is not a join request. |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 131 | */ |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 132 | int mptcp_token_new_accept(u32 token, struct sock *conn) |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 133 | { |
| 134 | int err; |
| 135 | |
| 136 | spin_lock_bh(&token_tree_lock); |
Paolo Abeni | 58b0991 | 2020-03-13 16:52:41 +0100 | [diff] [blame] | 137 | err = radix_tree_insert(&token_tree, token, conn); |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 138 | spin_unlock_bh(&token_tree_lock); |
| 139 | |
| 140 | return err; |
| 141 | } |
| 142 | |
| 143 | /** |
Peter Krystad | f296234 | 2020-03-27 14:48:39 -0700 | [diff] [blame^] | 144 | * mptcp_token_get_sock - retrieve mptcp connection sock using its token |
| 145 | * @token: token of the mptcp connection to retrieve |
| 146 | * |
| 147 | * This function returns the mptcp connection structure with the given token. |
| 148 | * A reference count on the mptcp socket returned is taken. |
| 149 | * |
| 150 | * returns NULL if no connection with the given token value exists. |
| 151 | */ |
| 152 | struct mptcp_sock *mptcp_token_get_sock(u32 token) |
| 153 | { |
| 154 | struct sock *conn; |
| 155 | |
| 156 | spin_lock_bh(&token_tree_lock); |
| 157 | conn = radix_tree_lookup(&token_tree, token); |
| 158 | if (conn) { |
| 159 | /* token still reserved? */ |
| 160 | if (conn == (struct sock *)&token_used) |
| 161 | conn = NULL; |
| 162 | else |
| 163 | sock_hold(conn); |
| 164 | } |
| 165 | spin_unlock_bh(&token_tree_lock); |
| 166 | |
| 167 | return mptcp_sk(conn); |
| 168 | } |
| 169 | |
| 170 | /** |
Peter Krystad | 79c0949 | 2020-01-21 16:56:20 -0800 | [diff] [blame] | 171 | * mptcp_token_destroy_request - remove mptcp connection/token |
| 172 | * @token - token of mptcp connection to remove |
| 173 | * |
| 174 | * Remove not-yet-fully-established incoming connection identified |
| 175 | * by @token. |
| 176 | */ |
| 177 | void mptcp_token_destroy_request(u32 token) |
| 178 | { |
| 179 | spin_lock_bh(&token_tree_lock); |
| 180 | radix_tree_delete(&token_req_tree, token); |
| 181 | spin_unlock_bh(&token_tree_lock); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * mptcp_token_destroy - remove mptcp connection/token |
| 186 | * @token - token of mptcp connection to remove |
| 187 | * |
| 188 | * Remove the connection identified by @token. |
| 189 | */ |
| 190 | void mptcp_token_destroy(u32 token) |
| 191 | { |
| 192 | spin_lock_bh(&token_tree_lock); |
| 193 | radix_tree_delete(&token_tree, token); |
| 194 | spin_unlock_bh(&token_tree_lock); |
| 195 | } |