162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci#include <linux/skbuff.h>
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include "protocol.h"
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci/* Syncookies do not work for JOIN requests.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Unlike MP_CAPABLE, where the ACK cookie contains the needed MPTCP
962306a36Sopenharmony_ci * options to reconstruct the initial syn state, MP_JOIN does not contain
1062306a36Sopenharmony_ci * the token to obtain the mptcp socket nor the server-generated nonce
1162306a36Sopenharmony_ci * that was used in the cookie SYN/ACK response.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * Keep a small best effort state table to store the syn/synack data,
1462306a36Sopenharmony_ci * indexed by skb hash.
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * A MP_JOIN SYN packet handled by syn cookies is only stored if the 32bit
1762306a36Sopenharmony_ci * token matches a known mptcp connection that can still accept more subflows.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * There is no timeout handling -- state is only re-constructed
2062306a36Sopenharmony_ci * when the TCP ACK passed the cookie validation check.
2162306a36Sopenharmony_ci */
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_cistruct join_entry {
2462306a36Sopenharmony_ci	u32 token;
2562306a36Sopenharmony_ci	u32 remote_nonce;
2662306a36Sopenharmony_ci	u32 local_nonce;
2762306a36Sopenharmony_ci	u8 join_id;
2862306a36Sopenharmony_ci	u8 local_id;
2962306a36Sopenharmony_ci	u8 backup;
3062306a36Sopenharmony_ci	u8 valid;
3162306a36Sopenharmony_ci};
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#define COOKIE_JOIN_SLOTS	1024
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_cistatic struct join_entry join_entries[COOKIE_JOIN_SLOTS] __cacheline_aligned_in_smp;
3662306a36Sopenharmony_cistatic spinlock_t join_entry_locks[COOKIE_JOIN_SLOTS] __cacheline_aligned_in_smp;
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistatic u32 mptcp_join_entry_hash(struct sk_buff *skb, struct net *net)
3962306a36Sopenharmony_ci{
4062306a36Sopenharmony_ci	static u32 mptcp_join_hash_secret __read_mostly;
4162306a36Sopenharmony_ci	struct tcphdr *th = tcp_hdr(skb);
4262306a36Sopenharmony_ci	u32 seq, i;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	net_get_random_once(&mptcp_join_hash_secret,
4562306a36Sopenharmony_ci			    sizeof(mptcp_join_hash_secret));
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	if (th->syn)
4862306a36Sopenharmony_ci		seq = TCP_SKB_CB(skb)->seq;
4962306a36Sopenharmony_ci	else
5062306a36Sopenharmony_ci		seq = TCP_SKB_CB(skb)->seq - 1;
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	i = jhash_3words(seq, net_hash_mix(net),
5362306a36Sopenharmony_ci			 (__force __u32)th->source << 16 | (__force __u32)th->dest,
5462306a36Sopenharmony_ci			 mptcp_join_hash_secret);
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	return i % ARRAY_SIZE(join_entries);
5762306a36Sopenharmony_ci}
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_cistatic void mptcp_join_store_state(struct join_entry *entry,
6062306a36Sopenharmony_ci				   const struct mptcp_subflow_request_sock *subflow_req)
6162306a36Sopenharmony_ci{
6262306a36Sopenharmony_ci	entry->token = subflow_req->token;
6362306a36Sopenharmony_ci	entry->remote_nonce = subflow_req->remote_nonce;
6462306a36Sopenharmony_ci	entry->local_nonce = subflow_req->local_nonce;
6562306a36Sopenharmony_ci	entry->backup = subflow_req->backup;
6662306a36Sopenharmony_ci	entry->join_id = subflow_req->remote_id;
6762306a36Sopenharmony_ci	entry->local_id = subflow_req->local_id;
6862306a36Sopenharmony_ci	entry->valid = 1;
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_civoid subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
7262306a36Sopenharmony_ci				       struct sk_buff *skb)
7362306a36Sopenharmony_ci{
7462306a36Sopenharmony_ci	struct net *net = read_pnet(&subflow_req->sk.req.ireq_net);
7562306a36Sopenharmony_ci	u32 i = mptcp_join_entry_hash(skb, net);
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	/* No use in waiting if other cpu is already using this slot --
7862306a36Sopenharmony_ci	 * would overwrite the data that got stored.
7962306a36Sopenharmony_ci	 */
8062306a36Sopenharmony_ci	spin_lock_bh(&join_entry_locks[i]);
8162306a36Sopenharmony_ci	mptcp_join_store_state(&join_entries[i], subflow_req);
8262306a36Sopenharmony_ci	spin_unlock_bh(&join_entry_locks[i]);
8362306a36Sopenharmony_ci}
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/* Called for a cookie-ack with MP_JOIN option present.
8662306a36Sopenharmony_ci * Look up the saved state based on skb hash & check token matches msk
8762306a36Sopenharmony_ci * in same netns.
8862306a36Sopenharmony_ci *
8962306a36Sopenharmony_ci * Caller will check msk can still accept another subflow.  The hmac
9062306a36Sopenharmony_ci * present in the cookie ACK mptcp option space will be checked later.
9162306a36Sopenharmony_ci */
9262306a36Sopenharmony_cibool mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
9362306a36Sopenharmony_ci					struct sk_buff *skb)
9462306a36Sopenharmony_ci{
9562306a36Sopenharmony_ci	struct net *net = read_pnet(&subflow_req->sk.req.ireq_net);
9662306a36Sopenharmony_ci	u32 i = mptcp_join_entry_hash(skb, net);
9762306a36Sopenharmony_ci	struct mptcp_sock *msk;
9862306a36Sopenharmony_ci	struct join_entry *e;
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	e = &join_entries[i];
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	spin_lock_bh(&join_entry_locks[i]);
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	if (e->valid == 0) {
10562306a36Sopenharmony_ci		spin_unlock_bh(&join_entry_locks[i]);
10662306a36Sopenharmony_ci		return false;
10762306a36Sopenharmony_ci	}
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	e->valid = 0;
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	msk = mptcp_token_get_sock(net, e->token);
11262306a36Sopenharmony_ci	if (!msk) {
11362306a36Sopenharmony_ci		spin_unlock_bh(&join_entry_locks[i]);
11462306a36Sopenharmony_ci		return false;
11562306a36Sopenharmony_ci	}
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	subflow_req->remote_nonce = e->remote_nonce;
11862306a36Sopenharmony_ci	subflow_req->local_nonce = e->local_nonce;
11962306a36Sopenharmony_ci	subflow_req->backup = e->backup;
12062306a36Sopenharmony_ci	subflow_req->remote_id = e->join_id;
12162306a36Sopenharmony_ci	subflow_req->token = e->token;
12262306a36Sopenharmony_ci	subflow_req->msk = msk;
12362306a36Sopenharmony_ci	spin_unlock_bh(&join_entry_locks[i]);
12462306a36Sopenharmony_ci	return true;
12562306a36Sopenharmony_ci}
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_civoid __init mptcp_join_cookie_init(void)
12862306a36Sopenharmony_ci{
12962306a36Sopenharmony_ci	int i;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	for (i = 0; i < COOKIE_JOIN_SLOTS; i++)
13262306a36Sopenharmony_ci		spin_lock_init(&join_entry_locks[i]);
13362306a36Sopenharmony_ci}
134