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 389466a1ccSFlorian Westphal static u32 mptcp_join_entry_hash(struct sk_buff *skb, struct net *net) 399466a1ccSFlorian Westphal { 409466a1ccSFlorian Westphal u32 i = skb_get_hash(skb) ^ net_hash_mix(net); 419466a1ccSFlorian Westphal 429466a1ccSFlorian Westphal return i % ARRAY_SIZE(join_entries); 439466a1ccSFlorian Westphal } 449466a1ccSFlorian Westphal 459466a1ccSFlorian Westphal static void mptcp_join_store_state(struct join_entry *entry, 469466a1ccSFlorian Westphal const struct mptcp_subflow_request_sock *subflow_req) 479466a1ccSFlorian Westphal { 489466a1ccSFlorian Westphal entry->token = subflow_req->token; 499466a1ccSFlorian Westphal entry->remote_nonce = subflow_req->remote_nonce; 509466a1ccSFlorian Westphal entry->local_nonce = subflow_req->local_nonce; 519466a1ccSFlorian Westphal entry->backup = subflow_req->backup; 529466a1ccSFlorian Westphal entry->join_id = subflow_req->remote_id; 539466a1ccSFlorian Westphal entry->local_id = subflow_req->local_id; 549466a1ccSFlorian Westphal entry->valid = 1; 559466a1ccSFlorian Westphal } 569466a1ccSFlorian Westphal 579466a1ccSFlorian Westphal void subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req, 589466a1ccSFlorian Westphal struct sk_buff *skb) 599466a1ccSFlorian Westphal { 609466a1ccSFlorian Westphal struct net *net = read_pnet(&subflow_req->sk.req.ireq_net); 619466a1ccSFlorian Westphal u32 i = mptcp_join_entry_hash(skb, net); 629466a1ccSFlorian Westphal 639466a1ccSFlorian Westphal /* No use in waiting if other cpu is already using this slot -- 649466a1ccSFlorian Westphal * would overwrite the data that got stored. 659466a1ccSFlorian Westphal */ 669466a1ccSFlorian Westphal spin_lock_bh(&join_entry_locks[i]); 679466a1ccSFlorian Westphal mptcp_join_store_state(&join_entries[i], subflow_req); 689466a1ccSFlorian Westphal spin_unlock_bh(&join_entry_locks[i]); 699466a1ccSFlorian Westphal } 709466a1ccSFlorian Westphal 719466a1ccSFlorian Westphal /* Called for a cookie-ack with MP_JOIN option present. 729466a1ccSFlorian Westphal * Look up the saved state based on skb hash & check token matches msk 739466a1ccSFlorian Westphal * in same netns. 749466a1ccSFlorian Westphal * 759466a1ccSFlorian Westphal * Caller will check msk can still accept another subflow. The hmac 769466a1ccSFlorian Westphal * present in the cookie ACK mptcp option space will be checked later. 779466a1ccSFlorian Westphal */ 789466a1ccSFlorian Westphal bool mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req, 799466a1ccSFlorian Westphal struct sk_buff *skb) 809466a1ccSFlorian Westphal { 819466a1ccSFlorian Westphal struct net *net = read_pnet(&subflow_req->sk.req.ireq_net); 829466a1ccSFlorian Westphal u32 i = mptcp_join_entry_hash(skb, net); 839466a1ccSFlorian Westphal struct mptcp_sock *msk; 849466a1ccSFlorian Westphal struct join_entry *e; 859466a1ccSFlorian Westphal 869466a1ccSFlorian Westphal e = &join_entries[i]; 879466a1ccSFlorian Westphal 889466a1ccSFlorian Westphal spin_lock_bh(&join_entry_locks[i]); 899466a1ccSFlorian Westphal 909466a1ccSFlorian Westphal if (e->valid == 0) { 919466a1ccSFlorian Westphal spin_unlock_bh(&join_entry_locks[i]); 929466a1ccSFlorian Westphal return false; 939466a1ccSFlorian Westphal } 949466a1ccSFlorian Westphal 959466a1ccSFlorian Westphal e->valid = 0; 969466a1ccSFlorian Westphal 979466a1ccSFlorian Westphal msk = mptcp_token_get_sock(e->token); 989466a1ccSFlorian Westphal if (!msk) { 999466a1ccSFlorian Westphal spin_unlock_bh(&join_entry_locks[i]); 1009466a1ccSFlorian Westphal return false; 1019466a1ccSFlorian Westphal } 1029466a1ccSFlorian Westphal 1039466a1ccSFlorian Westphal /* If this fails, the token got re-used in the mean time by another 1049466a1ccSFlorian Westphal * mptcp socket in a different netns, i.e. entry is outdated. 1059466a1ccSFlorian Westphal */ 1069466a1ccSFlorian Westphal if (!net_eq(sock_net((struct sock *)msk), net)) 1079466a1ccSFlorian Westphal goto err_put; 1089466a1ccSFlorian Westphal 1099466a1ccSFlorian Westphal subflow_req->remote_nonce = e->remote_nonce; 1109466a1ccSFlorian Westphal subflow_req->local_nonce = e->local_nonce; 1119466a1ccSFlorian Westphal subflow_req->backup = e->backup; 1129466a1ccSFlorian Westphal subflow_req->remote_id = e->join_id; 1139466a1ccSFlorian Westphal subflow_req->token = e->token; 1149466a1ccSFlorian Westphal subflow_req->msk = msk; 1159466a1ccSFlorian Westphal spin_unlock_bh(&join_entry_locks[i]); 1169466a1ccSFlorian Westphal return true; 1179466a1ccSFlorian Westphal 1189466a1ccSFlorian Westphal err_put: 1199466a1ccSFlorian Westphal spin_unlock_bh(&join_entry_locks[i]); 1209466a1ccSFlorian Westphal sock_put((struct sock *)msk); 1219466a1ccSFlorian Westphal return false; 1229466a1ccSFlorian Westphal } 1239466a1ccSFlorian Westphal 1249466a1ccSFlorian Westphal void __init mptcp_join_cookie_init(void) 1259466a1ccSFlorian Westphal { 1269466a1ccSFlorian Westphal int i; 1279466a1ccSFlorian Westphal 128*7126bd5cSFlorian Westphal for (i = 0; i < COOKIE_JOIN_SLOTS; i++) 1299466a1ccSFlorian Westphal spin_lock_init(&join_entry_locks[i]); 1309466a1ccSFlorian Westphal } 131