blob: 6e6e162d25f1a4a497f3885162ef821942ea008f [file] [log] [blame]
Mat Martineauf870fa02020-01-21 16:56:15 -08001/* SPDX-License-Identifier: GPL-2.0 */
2/* Multipath TCP
3 *
4 * Copyright (c) 2017 - 2019, Intel Corporation.
5 */
6
7#ifndef __MPTCP_PROTOCOL_H
8#define __MPTCP_PROTOCOL_H
9
Peter Krystad79c09492020-01-21 16:56:20 -080010#include <linux/random.h>
11#include <net/tcp.h>
12#include <net/inet_connection_sock.h>
13
Christoph Paaschcc7972e2020-01-21 16:56:31 -080014#define MPTCP_SUPPORTED_VERSION 1
Peter Krystadeda7acd2020-01-21 16:56:16 -080015
16/* MPTCP option bits */
17#define OPTION_MPTCP_MPC_SYN BIT(0)
18#define OPTION_MPTCP_MPC_SYNACK BIT(1)
19#define OPTION_MPTCP_MPC_ACK BIT(2)
20
21/* MPTCP option subtypes */
22#define MPTCPOPT_MP_CAPABLE 0
23#define MPTCPOPT_MP_JOIN 1
24#define MPTCPOPT_DSS 2
25#define MPTCPOPT_ADD_ADDR 3
26#define MPTCPOPT_RM_ADDR 4
27#define MPTCPOPT_MP_PRIO 5
28#define MPTCPOPT_MP_FAIL 6
29#define MPTCPOPT_MP_FASTCLOSE 7
30
31/* MPTCP suboption lengths */
Christoph Paaschcc7972e2020-01-21 16:56:31 -080032#define TCPOLEN_MPTCP_MPC_SYN 4
Peter Krystadeda7acd2020-01-21 16:56:16 -080033#define TCPOLEN_MPTCP_MPC_SYNACK 12
34#define TCPOLEN_MPTCP_MPC_ACK 20
Christoph Paaschcc7972e2020-01-21 16:56:31 -080035#define TCPOLEN_MPTCP_MPC_ACK_DATA 22
Mat Martineau6d0060f2020-01-21 16:56:23 -080036#define TCPOLEN_MPTCP_DSS_BASE 4
Mat Martineau648ef4b2020-01-21 16:56:24 -080037#define TCPOLEN_MPTCP_DSS_ACK32 4
Mat Martineau6d0060f2020-01-21 16:56:23 -080038#define TCPOLEN_MPTCP_DSS_ACK64 8
Mat Martineau648ef4b2020-01-21 16:56:24 -080039#define TCPOLEN_MPTCP_DSS_MAP32 10
Mat Martineau6d0060f2020-01-21 16:56:23 -080040#define TCPOLEN_MPTCP_DSS_MAP64 14
41#define TCPOLEN_MPTCP_DSS_CHECKSUM 2
Peter Krystadeda7acd2020-01-21 16:56:16 -080042
43/* MPTCP MP_CAPABLE flags */
44#define MPTCP_VERSION_MASK (0x0F)
45#define MPTCP_CAP_CHECKSUM_REQD BIT(7)
46#define MPTCP_CAP_EXTENSIBILITY BIT(6)
Paolo Abeni65492c52020-01-21 16:56:30 -080047#define MPTCP_CAP_HMAC_SHA256 BIT(0)
Peter Krystadeda7acd2020-01-21 16:56:16 -080048#define MPTCP_CAP_FLAG_MASK (0x3F)
49
Mat Martineau6d0060f2020-01-21 16:56:23 -080050/* MPTCP DSS flags */
51#define MPTCP_DSS_DATA_FIN BIT(4)
52#define MPTCP_DSS_DSN64 BIT(3)
53#define MPTCP_DSS_HAS_MAP BIT(2)
54#define MPTCP_DSS_ACK64 BIT(1)
55#define MPTCP_DSS_HAS_ACK BIT(0)
Mat Martineau648ef4b2020-01-21 16:56:24 -080056#define MPTCP_DSS_FLAG_MASK (0x1F)
57
58/* MPTCP socket flags */
Florian Westphald99bfed2020-02-17 16:54:38 +010059#define MPTCP_DATA_READY 0
60#define MPTCP_SEND_SPACE 1
Mat Martineau6d0060f2020-01-21 16:56:23 -080061
Mat Martineauf870fa02020-01-21 16:56:15 -080062/* MPTCP connection sock */
63struct mptcp_sock {
64 /* inet_connection_sock must be the first member */
65 struct inet_connection_sock sk;
Peter Krystadcec37a62020-01-21 16:56:18 -080066 u64 local_key;
67 u64 remote_key;
Mat Martineau6d0060f2020-01-21 16:56:23 -080068 u64 write_seq;
69 u64 ack_seq;
Peter Krystad79c09492020-01-21 16:56:20 -080070 u32 token;
Mat Martineau648ef4b2020-01-21 16:56:24 -080071 unsigned long flags;
Christoph Paaschd22f4982020-01-21 16:56:32 -080072 bool can_ack;
Paolo Abeni80992012020-02-26 10:14:47 +010073 struct work_struct work;
Peter Krystadcec37a62020-01-21 16:56:18 -080074 struct list_head conn_list;
Mat Martineau6d0060f2020-01-21 16:56:23 -080075 struct skb_ext *cached_ext; /* for the next sendmsg */
Mat Martineauf870fa02020-01-21 16:56:15 -080076 struct socket *subflow; /* outgoing connect/listener/!mp_capable */
Paolo Abeni8ab183d2020-01-21 16:56:33 -080077 struct sock *first;
Mat Martineauf870fa02020-01-21 16:56:15 -080078};
79
Peter Krystadcec37a62020-01-21 16:56:18 -080080#define mptcp_for_each_subflow(__msk, __subflow) \
81 list_for_each_entry(__subflow, &((__msk)->conn_list), node)
82
Mat Martineauf870fa02020-01-21 16:56:15 -080083static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
84{
85 return (struct mptcp_sock *)sk;
86}
87
Peter Krystadcec37a62020-01-21 16:56:18 -080088struct mptcp_subflow_request_sock {
89 struct tcp_request_sock sk;
Christoph Paaschd22f4982020-01-21 16:56:32 -080090 u16 mp_capable : 1,
Peter Krystadcec37a62020-01-21 16:56:18 -080091 mp_join : 1,
Christoph Paaschd22f4982020-01-21 16:56:32 -080092 backup : 1,
93 remote_key_valid : 1;
Peter Krystadcec37a62020-01-21 16:56:18 -080094 u64 local_key;
95 u64 remote_key;
Peter Krystad79c09492020-01-21 16:56:20 -080096 u64 idsn;
97 u32 token;
Mat Martineau648ef4b2020-01-21 16:56:24 -080098 u32 ssn_offset;
Peter Krystadcec37a62020-01-21 16:56:18 -080099};
100
101static inline struct mptcp_subflow_request_sock *
102mptcp_subflow_rsk(const struct request_sock *rsk)
103{
104 return (struct mptcp_subflow_request_sock *)rsk;
105}
106
Peter Krystad2303f992020-01-21 16:56:17 -0800107/* MPTCP subflow context */
108struct mptcp_subflow_context {
Peter Krystadcec37a62020-01-21 16:56:18 -0800109 struct list_head node;/* conn_list of subflows */
110 u64 local_key;
111 u64 remote_key;
Peter Krystad79c09492020-01-21 16:56:20 -0800112 u64 idsn;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800113 u64 map_seq;
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800114 u32 snd_isn;
Peter Krystad79c09492020-01-21 16:56:20 -0800115 u32 token;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800116 u32 rel_write_seq;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800117 u32 map_subflow_seq;
118 u32 ssn_offset;
119 u32 map_data_len;
Peter Krystadcec37a62020-01-21 16:56:18 -0800120 u32 request_mptcp : 1, /* send MP_CAPABLE */
121 mp_capable : 1, /* remote is MPTCP capable */
122 fourth_ack : 1, /* send initial DSS */
Mat Martineau648ef4b2020-01-21 16:56:24 -0800123 conn_finished : 1,
124 map_valid : 1,
Christoph Paaschd22f4982020-01-21 16:56:32 -0800125 mpc_map : 1,
Mat Martineau648ef4b2020-01-21 16:56:24 -0800126 data_avail : 1,
Christoph Paaschd22f4982020-01-21 16:56:32 -0800127 rx_eof : 1,
128 can_ack : 1; /* only after processing the remote a key */
Mat Martineau648ef4b2020-01-21 16:56:24 -0800129
Peter Krystad2303f992020-01-21 16:56:17 -0800130 struct sock *tcp_sock; /* tcp sk backpointer */
131 struct sock *conn; /* parent mptcp_sock */
Peter Krystadcec37a62020-01-21 16:56:18 -0800132 const struct inet_connection_sock_af_ops *icsk_af_ops;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800133 void (*tcp_data_ready)(struct sock *sk);
134 void (*tcp_state_change)(struct sock *sk);
135 void (*tcp_write_space)(struct sock *sk);
136
Peter Krystad2303f992020-01-21 16:56:17 -0800137 struct rcu_head rcu;
138};
139
140static inline struct mptcp_subflow_context *
141mptcp_subflow_ctx(const struct sock *sk)
142{
143 struct inet_connection_sock *icsk = inet_csk(sk);
144
145 /* Use RCU on icsk_ulp_data only for sock diag code */
146 return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;
147}
148
149static inline struct sock *
150mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
151{
152 return subflow->tcp_sock;
153}
154
Mat Martineau648ef4b2020-01-21 16:56:24 -0800155static inline u64
156mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow)
157{
158 return tcp_sk(mptcp_subflow_tcp_sock(subflow))->copied_seq -
159 subflow->ssn_offset -
160 subflow->map_subflow_seq;
161}
162
163static inline u64
164mptcp_subflow_get_mapped_dsn(const struct mptcp_subflow_context *subflow)
165{
166 return subflow->map_seq + mptcp_subflow_get_map_offset(subflow);
167}
168
169int mptcp_is_enabled(struct net *net);
170bool mptcp_subflow_data_available(struct sock *sk);
Peter Krystad2303f992020-01-21 16:56:17 -0800171void mptcp_subflow_init(void);
172int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);
173
Mat Martineau648ef4b2020-01-21 16:56:24 -0800174static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
175 struct mptcp_subflow_context *ctx)
176{
177 sk->sk_data_ready = ctx->tcp_data_ready;
178 sk->sk_state_change = ctx->tcp_state_change;
179 sk->sk_write_space = ctx->tcp_write_space;
180
181 inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops;
182}
183
Peter Krystadcec37a62020-01-21 16:56:18 -0800184extern const struct inet_connection_sock_af_ops ipv4_specific;
185#if IS_ENABLED(CONFIG_MPTCP_IPV6)
186extern const struct inet_connection_sock_af_ops ipv6_specific;
187#endif
188
Mat Martineau648ef4b2020-01-21 16:56:24 -0800189void mptcp_proto_init(void);
Matthieu Baerts784325e2020-01-21 16:56:28 -0800190#if IS_ENABLED(CONFIG_MPTCP_IPV6)
191int mptcp_proto_v6_init(void);
192#endif
Mat Martineau648ef4b2020-01-21 16:56:24 -0800193
194struct mptcp_read_arg {
195 struct msghdr *msg;
196};
197
198int mptcp_read_actor(read_descriptor_t *desc, struct sk_buff *skb,
199 unsigned int offset, size_t len);
200
Peter Krystadcec37a62020-01-21 16:56:18 -0800201void mptcp_get_options(const struct sk_buff *skb,
202 struct tcp_options_received *opt_rx);
203
204void mptcp_finish_connect(struct sock *sk);
Florian Westphal101f6f82020-02-26 10:14:46 +0100205void mptcp_data_ready(struct sock *sk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800206
Peter Krystad79c09492020-01-21 16:56:20 -0800207int mptcp_token_new_request(struct request_sock *req);
208void mptcp_token_destroy_request(u32 token);
209int mptcp_token_new_connect(struct sock *sk);
210int mptcp_token_new_accept(u32 token);
211void mptcp_token_update_accept(struct sock *sk, struct sock *conn);
212void mptcp_token_destroy(u32 token);
213
214void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn);
215static inline void mptcp_crypto_key_gen_sha(u64 *key, u32 *token, u64 *idsn)
216{
217 /* we might consider a faster version that computes the key as a
218 * hash of some information available in the MPTCP socket. Use
219 * random data at the moment, as it's probably the safest option
220 * in case multiple sockets are opened in different namespaces at
221 * the same time.
222 */
223 get_random_bytes(key, sizeof(u64));
224 mptcp_crypto_key_sha(*key, token, idsn);
225}
226
227void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
Paolo Abeni65492c52020-01-21 16:56:30 -0800228 void *hash_out);
Peter Krystad79c09492020-01-21 16:56:20 -0800229
Mat Martineau6d0060f2020-01-21 16:56:23 -0800230static inline struct mptcp_ext *mptcp_get_ext(struct sk_buff *skb)
231{
232 return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP);
233}
234
Mat Martineau648ef4b2020-01-21 16:56:24 -0800235static inline bool before64(__u64 seq1, __u64 seq2)
236{
237 return (__s64)(seq1 - seq2) < 0;
238}
239
240#define after64(seq2, seq1) before64(seq1, seq2)
241
Mat Martineauf870fa02020-01-21 16:56:15 -0800242#endif /* __MPTCP_PROTOCOL_H */