19466a1ccSFlorian Westphal // SPDX-License-Identifier: GPL-2.0
29466a1ccSFlorian Westphal #include <linux/skbuff.h>
39466a1ccSFlorian Westphal
49466a1ccSFlorian Westphal #include "protocol.h"
59466a1ccSFlorian Westphal
69466a1ccSFlorian Westphal /* Syncookies do not work for JOIN requests.
79466a1ccSFlorian Westphal *
89466a1ccSFlorian Westphal * Unlike MP_CAPABLE, where the ACK cookie contains the needed MPTCP
99466a1ccSFlorian Westphal * options to reconstruct the initial syn state, MP_JOIN does not contain
109466a1ccSFlorian Westphal * the token to obtain the mptcp socket nor the server-generated nonce
119466a1ccSFlorian Westphal * that was used in the cookie SYN/ACK response.
129466a1ccSFlorian Westphal *
139466a1ccSFlorian Westphal * Keep a small best effort state table to store the syn/synack data,
149466a1ccSFlorian Westphal * indexed by skb hash.
159466a1ccSFlorian Westphal *
169466a1ccSFlorian Westphal * A MP_JOIN SYN packet handled by syn cookies is only stored if the 32bit
179466a1ccSFlorian Westphal * token matches a known mptcp connection that can still accept more subflows.
189466a1ccSFlorian Westphal *
199466a1ccSFlorian Westphal * There is no timeout handling -- state is only re-constructed
209466a1ccSFlorian Westphal * when the TCP ACK passed the cookie validation check.
219466a1ccSFlorian Westphal */
229466a1ccSFlorian Westphal
239466a1ccSFlorian Westphal struct join_entry {
249466a1ccSFlorian Westphal u32 token;
259466a1ccSFlorian Westphal u32 remote_nonce;
269466a1ccSFlorian Westphal u32 local_nonce;
279466a1ccSFlorian Westphal u8 join_id;
289466a1ccSFlorian Westphal u8 local_id;
299466a1ccSFlorian Westphal u8 backup;
309466a1ccSFlorian Westphal u8 valid;
319466a1ccSFlorian Westphal };
329466a1ccSFlorian Westphal
339466a1ccSFlorian Westphal #define COOKIE_JOIN_SLOTS 1024
349466a1ccSFlorian Westphal
359466a1ccSFlorian Westphal static struct join_entry join_entries[COOKIE_JOIN_SLOTS] __cacheline_aligned_in_smp;
369466a1ccSFlorian Westphal static spinlock_t join_entry_locks[COOKIE_JOIN_SLOTS] __cacheline_aligned_in_smp;
379466a1ccSFlorian Westphal
mptcp_join_entry_hash(struct sk_buff * skb,struct net * net)389466a1ccSFlorian Westphal static u32 mptcp_join_entry_hash(struct sk_buff *skb, struct net *net)
399466a1ccSFlorian Westphal {
400c71929bSJianguo Wu static u32 mptcp_join_hash_secret __read_mostly;
410c71929bSJianguo Wu struct tcphdr *th = tcp_hdr(skb);
420c71929bSJianguo Wu u32 seq, i;
430c71929bSJianguo Wu
440c71929bSJianguo Wu net_get_random_once(&mptcp_join_hash_secret,
450c71929bSJianguo Wu sizeof(mptcp_join_hash_secret));
460c71929bSJianguo Wu
470c71929bSJianguo Wu if (th->syn)
480c71929bSJianguo Wu seq = TCP_SKB_CB(skb)->seq;
490c71929bSJianguo Wu else
500c71929bSJianguo Wu seq = TCP_SKB_CB(skb)->seq - 1;
510c71929bSJianguo Wu
520c71929bSJianguo Wu i = jhash_3words(seq, net_hash_mix(net),
530c71929bSJianguo Wu (__force __u32)th->source << 16 | (__force __u32)th->dest,
540c71929bSJianguo Wu mptcp_join_hash_secret);
559466a1ccSFlorian Westphal
569466a1ccSFlorian Westphal return i % ARRAY_SIZE(join_entries);
579466a1ccSFlorian Westphal }
589466a1ccSFlorian Westphal
mptcp_join_store_state(struct join_entry * entry,const struct mptcp_subflow_request_sock * subflow_req)599466a1ccSFlorian Westphal static void mptcp_join_store_state(struct join_entry *entry,
609466a1ccSFlorian Westphal const struct mptcp_subflow_request_sock *subflow_req)
619466a1ccSFlorian Westphal {
629466a1ccSFlorian Westphal entry->token = subflow_req->token;
639466a1ccSFlorian Westphal entry->remote_nonce = subflow_req->remote_nonce;
649466a1ccSFlorian Westphal entry->local_nonce = subflow_req->local_nonce;
659466a1ccSFlorian Westphal entry->backup = subflow_req->backup;
669466a1ccSFlorian Westphal entry->join_id = subflow_req->remote_id;
679466a1ccSFlorian Westphal entry->local_id = subflow_req->local_id;
689466a1ccSFlorian Westphal entry->valid = 1;
699466a1ccSFlorian Westphal }
709466a1ccSFlorian Westphal
subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock * subflow_req,struct sk_buff * skb)719466a1ccSFlorian Westphal void subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
729466a1ccSFlorian Westphal struct sk_buff *skb)
739466a1ccSFlorian Westphal {
749466a1ccSFlorian Westphal struct net *net = read_pnet(&subflow_req->sk.req.ireq_net);
759466a1ccSFlorian Westphal u32 i = mptcp_join_entry_hash(skb, net);
769466a1ccSFlorian Westphal
779466a1ccSFlorian Westphal /* No use in waiting if other cpu is already using this slot --
789466a1ccSFlorian Westphal * would overwrite the data that got stored.
799466a1ccSFlorian Westphal */
809466a1ccSFlorian Westphal spin_lock_bh(&join_entry_locks[i]);
819466a1ccSFlorian Westphal mptcp_join_store_state(&join_entries[i], subflow_req);
829466a1ccSFlorian Westphal spin_unlock_bh(&join_entry_locks[i]);
839466a1ccSFlorian Westphal }
849466a1ccSFlorian Westphal
859466a1ccSFlorian Westphal /* Called for a cookie-ack with MP_JOIN option present.
869466a1ccSFlorian Westphal * Look up the saved state based on skb hash & check token matches msk
879466a1ccSFlorian Westphal * in same netns.
889466a1ccSFlorian Westphal *
899466a1ccSFlorian Westphal * Caller will check msk can still accept another subflow. The hmac
909466a1ccSFlorian Westphal * present in the cookie ACK mptcp option space will be checked later.
919466a1ccSFlorian Westphal */
mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock * subflow_req,struct sk_buff * skb)929466a1ccSFlorian Westphal bool mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
939466a1ccSFlorian Westphal struct sk_buff *skb)
949466a1ccSFlorian Westphal {
959466a1ccSFlorian Westphal struct net *net = read_pnet(&subflow_req->sk.req.ireq_net);
969466a1ccSFlorian Westphal u32 i = mptcp_join_entry_hash(skb, net);
979466a1ccSFlorian Westphal struct mptcp_sock *msk;
989466a1ccSFlorian Westphal struct join_entry *e;
999466a1ccSFlorian Westphal
1009466a1ccSFlorian Westphal e = &join_entries[i];
1019466a1ccSFlorian Westphal
1029466a1ccSFlorian Westphal spin_lock_bh(&join_entry_locks[i]);
1039466a1ccSFlorian Westphal
1049466a1ccSFlorian Westphal if (e->valid == 0) {
1059466a1ccSFlorian Westphal spin_unlock_bh(&join_entry_locks[i]);
1069466a1ccSFlorian Westphal return false;
1079466a1ccSFlorian Westphal }
1089466a1ccSFlorian Westphal
1099466a1ccSFlorian Westphal e->valid = 0;
1109466a1ccSFlorian Westphal
111*ea1300b9SFlorian Westphal msk = mptcp_token_get_sock(net, e->token);
1129466a1ccSFlorian Westphal if (!msk) {
1139466a1ccSFlorian Westphal spin_unlock_bh(&join_entry_locks[i]);
1149466a1ccSFlorian Westphal return false;
1159466a1ccSFlorian Westphal }
1169466a1ccSFlorian Westphal
1179466a1ccSFlorian Westphal subflow_req->remote_nonce = e->remote_nonce;
1189466a1ccSFlorian Westphal subflow_req->local_nonce = e->local_nonce;
1199466a1ccSFlorian Westphal subflow_req->backup = e->backup;
1209466a1ccSFlorian Westphal subflow_req->remote_id = e->join_id;
1219466a1ccSFlorian Westphal subflow_req->token = e->token;
1229466a1ccSFlorian Westphal subflow_req->msk = msk;
1239466a1ccSFlorian Westphal spin_unlock_bh(&join_entry_locks[i]);
1249466a1ccSFlorian Westphal return true;
1259466a1ccSFlorian Westphal }
1269466a1ccSFlorian Westphal
mptcp_join_cookie_init(void)1279466a1ccSFlorian Westphal void __init mptcp_join_cookie_init(void)
1289466a1ccSFlorian Westphal {
1299466a1ccSFlorian Westphal int i;
1309466a1ccSFlorian Westphal
1317126bd5cSFlorian Westphal for (i = 0; i < COOKIE_JOIN_SLOTS; i++)
1329466a1ccSFlorian Westphal spin_lock_init(&join_entry_locks[i]);
1339466a1ccSFlorian Westphal }
134