blob: d67de793d363f6e041fd0aabffb7d35bf4930952 [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)
Peter Krystadf2962342020-03-27 14:48:39 -070020#define OPTION_MPTCP_MPJ_SYN BIT(3)
21#define OPTION_MPTCP_MPJ_SYNACK BIT(4)
22#define OPTION_MPTCP_MPJ_ACK BIT(5)
Peter Krystad3df523a2020-03-27 14:48:37 -070023#define OPTION_MPTCP_ADD_ADDR BIT(6)
24#define OPTION_MPTCP_ADD_ADDR6 BIT(7)
25#define OPTION_MPTCP_RM_ADDR BIT(8)
Florian Westphal50c504a2020-12-10 14:25:04 -080026#define OPTION_MPTCP_FASTCLOSE BIT(9)
Peter Krystadeda7acd2020-01-21 16:56:16 -080027
28/* MPTCP option subtypes */
29#define MPTCPOPT_MP_CAPABLE 0
30#define MPTCPOPT_MP_JOIN 1
31#define MPTCPOPT_DSS 2
32#define MPTCPOPT_ADD_ADDR 3
33#define MPTCPOPT_RM_ADDR 4
34#define MPTCPOPT_MP_PRIO 5
35#define MPTCPOPT_MP_FAIL 6
36#define MPTCPOPT_MP_FASTCLOSE 7
37
38/* MPTCP suboption lengths */
Christoph Paaschcc7972e2020-01-21 16:56:31 -080039#define TCPOLEN_MPTCP_MPC_SYN 4
Peter Krystadeda7acd2020-01-21 16:56:16 -080040#define TCPOLEN_MPTCP_MPC_SYNACK 12
41#define TCPOLEN_MPTCP_MPC_ACK 20
Christoph Paaschcc7972e2020-01-21 16:56:31 -080042#define TCPOLEN_MPTCP_MPC_ACK_DATA 22
Peter Krystadf2962342020-03-27 14:48:39 -070043#define TCPOLEN_MPTCP_MPJ_SYN 12
44#define TCPOLEN_MPTCP_MPJ_SYNACK 16
45#define TCPOLEN_MPTCP_MPJ_ACK 24
Mat Martineau6d0060f2020-01-21 16:56:23 -080046#define TCPOLEN_MPTCP_DSS_BASE 4
Mat Martineau648ef4b2020-01-21 16:56:24 -080047#define TCPOLEN_MPTCP_DSS_ACK32 4
Mat Martineau6d0060f2020-01-21 16:56:23 -080048#define TCPOLEN_MPTCP_DSS_ACK64 8
Mat Martineau648ef4b2020-01-21 16:56:24 -080049#define TCPOLEN_MPTCP_DSS_MAP32 10
Mat Martineau6d0060f2020-01-21 16:56:23 -080050#define TCPOLEN_MPTCP_DSS_MAP64 14
51#define TCPOLEN_MPTCP_DSS_CHECKSUM 2
Peter Krystad3df523a2020-03-27 14:48:37 -070052#define TCPOLEN_MPTCP_ADD_ADDR 16
Geliang Tang22fb85f2020-12-09 15:51:20 -080053#define TCPOLEN_MPTCP_ADD_ADDR_PORT 20
Peter Krystad3df523a2020-03-27 14:48:37 -070054#define TCPOLEN_MPTCP_ADD_ADDR_BASE 8
Geliang Tang22fb85f2020-12-09 15:51:20 -080055#define TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT 12
Peter Krystad3df523a2020-03-27 14:48:37 -070056#define TCPOLEN_MPTCP_ADD_ADDR6 28
Geliang Tang22fb85f2020-12-09 15:51:20 -080057#define TCPOLEN_MPTCP_ADD_ADDR6_PORT 32
Peter Krystad3df523a2020-03-27 14:48:37 -070058#define TCPOLEN_MPTCP_ADD_ADDR6_BASE 20
Geliang Tang22fb85f2020-12-09 15:51:20 -080059#define TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT 24
60#define TCPOLEN_MPTCP_PORT_LEN 4
Peter Krystad3df523a2020-03-27 14:48:37 -070061#define TCPOLEN_MPTCP_RM_ADDR_BASE 4
Florian Westphal50c504a2020-12-10 14:25:04 -080062#define TCPOLEN_MPTCP_FASTCLOSE 12
Peter Krystadeda7acd2020-01-21 16:56:16 -080063
Peter Krystadec3edaa2020-03-27 14:48:40 -070064/* MPTCP MP_JOIN flags */
Peter Krystadf2962342020-03-27 14:48:39 -070065#define MPTCPOPT_BACKUP BIT(0)
66#define MPTCPOPT_HMAC_LEN 20
Peter Krystadec3edaa2020-03-27 14:48:40 -070067#define MPTCPOPT_THMAC_LEN 8
Peter Krystadf2962342020-03-27 14:48:39 -070068
Peter Krystadeda7acd2020-01-21 16:56:16 -080069/* MPTCP MP_CAPABLE flags */
70#define MPTCP_VERSION_MASK (0x0F)
71#define MPTCP_CAP_CHECKSUM_REQD BIT(7)
72#define MPTCP_CAP_EXTENSIBILITY BIT(6)
Paolo Abeni65492c52020-01-21 16:56:30 -080073#define MPTCP_CAP_HMAC_SHA256 BIT(0)
Peter Krystadeda7acd2020-01-21 16:56:16 -080074#define MPTCP_CAP_FLAG_MASK (0x3F)
75
Mat Martineau6d0060f2020-01-21 16:56:23 -080076/* MPTCP DSS flags */
77#define MPTCP_DSS_DATA_FIN BIT(4)
78#define MPTCP_DSS_DSN64 BIT(3)
79#define MPTCP_DSS_HAS_MAP BIT(2)
80#define MPTCP_DSS_ACK64 BIT(1)
81#define MPTCP_DSS_HAS_ACK BIT(0)
Mat Martineau648ef4b2020-01-21 16:56:24 -080082#define MPTCP_DSS_FLAG_MASK (0x1F)
83
Peter Krystad3df523a2020-03-27 14:48:37 -070084/* MPTCP ADD_ADDR flags */
85#define MPTCP_ADDR_ECHO BIT(0)
Peter Krystad3df523a2020-03-27 14:48:37 -070086#define MPTCP_ADDR_IPVERSION_4 4
87#define MPTCP_ADDR_IPVERSION_6 6
88
Mat Martineau648ef4b2020-01-21 16:56:24 -080089/* MPTCP socket flags */
Florian Westphald99bfed2020-02-17 16:54:38 +010090#define MPTCP_DATA_READY 0
Florian Westphal8edf0862020-11-16 10:48:12 +010091#define MPTCP_NOSPACE 1
Paolo Abeni3b1d6212020-03-27 14:48:48 -070092#define MPTCP_WORK_RTX 2
Florian Westphal59832e22020-04-02 13:44:52 +020093#define MPTCP_WORK_EOF 3
Davide Carattie1ff9e82020-06-29 22:26:20 +020094#define MPTCP_FALLBACK_DONE 4
Paolo Abeni0e4f35d2020-10-09 19:00:01 +020095#define MPTCP_WORK_CLOSE_SUBFLOW 5
Paolo Abeni6e628cd2020-11-27 11:10:27 +010096#define MPTCP_PUSH_PENDING 6
97#define MPTCP_CLEAN_UNA 7
Mat Martineau6d0060f2020-01-21 16:56:23 -080098
Paolo Abenieaa2ffa2020-11-16 10:48:08 +010099static inline bool before64(__u64 seq1, __u64 seq2)
100{
101 return (__s64)(seq1 - seq2) < 0;
102}
103
104#define after64(seq2, seq1) before64(seq1, seq2)
105
Paolo Abenicfde1412020-04-30 15:01:52 +0200106struct mptcp_options_received {
107 u64 sndr_key;
108 u64 rcvr_key;
109 u64 data_ack;
110 u64 data_seq;
111 u32 subflow_seq;
112 u16 data_len;
113 u16 mp_capable : 1,
114 mp_join : 1,
Florian Westphal50c504a2020-12-10 14:25:04 -0800115 fastclose : 1,
Paolo Abenicfde1412020-04-30 15:01:52 +0200116 dss : 1,
117 add_addr : 1,
118 rm_addr : 1,
119 family : 4,
120 echo : 1,
121 backup : 1;
122 u32 token;
123 u32 nonce;
124 u64 thmac;
Geliang Tangba34c3d2020-12-10 14:25:01 -0800125 u8 hmac[MPTCPOPT_HMAC_LEN];
Paolo Abenicfde1412020-04-30 15:01:52 +0200126 u8 join_id;
127 u8 use_map:1,
128 dsn64:1,
129 data_fin:1,
130 use_ack:1,
131 ack64:1,
132 mpc_map:1,
133 __unused:2;
134 u8 addr_id;
135 u8 rm_id;
136 union {
137 struct in_addr addr;
138#if IS_ENABLED(CONFIG_MPTCP_IPV6)
139 struct in6_addr addr6;
140#endif
141 };
142 u64 ahmac;
143 u16 port;
144};
145
Peter Krystad3df523a2020-03-27 14:48:37 -0700146static inline __be32 mptcp_option(u8 subopt, u8 len, u8 nib, u8 field)
147{
148 return htonl((TCPOPT_MPTCP << 24) | (len << 16) | (subopt << 12) |
149 ((nib & 0xF) << 8) | field);
150}
151
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700152struct mptcp_addr_info {
153 sa_family_t family;
154 __be16 port;
155 u8 id;
Paolo Abenief0da3b2020-09-14 10:01:15 +0200156 u8 flags;
157 int ifindex;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700158 union {
159 struct in_addr addr;
160#if IS_ENABLED(CONFIG_MPTCP_IPV6)
161 struct in6_addr addr6;
162#endif
163 };
164};
165
166enum mptcp_pm_status {
167 MPTCP_PM_ADD_ADDR_RECEIVED,
Geliang Tang84dfe362020-11-19 11:46:00 -0800168 MPTCP_PM_ADD_ADDR_SEND_ACK,
Geliang Tangd0876b22020-09-24 08:29:49 +0800169 MPTCP_PM_RM_ADDR_RECEIVED,
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700170 MPTCP_PM_ESTABLISHED,
Paolo Abeni5b950ff2020-12-09 12:03:29 +0100171 MPTCP_PM_ALREADY_ESTABLISHED, /* persistent status, set after ESTABLISHED event */
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700172 MPTCP_PM_SUBFLOW_ESTABLISHED,
173};
174
Geliang Tang13ad9f02020-12-09 15:51:27 -0800175enum mptcp_addr_signal_status {
Geliang Tangd91d3222020-11-19 11:45:59 -0800176 MPTCP_ADD_ADDR_SIGNAL,
177 MPTCP_ADD_ADDR_ECHO,
Geliang Tang84dfe362020-11-19 11:46:00 -0800178 MPTCP_ADD_ADDR_IPV6,
Geliang Tang4a2777a82020-12-09 15:51:22 -0800179 MPTCP_ADD_ADDR_PORT,
Geliang Tang42842a42020-12-09 15:51:26 -0800180 MPTCP_RM_ADDR_SIGNAL,
Geliang Tangd91d3222020-11-19 11:45:59 -0800181};
182
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700183struct mptcp_pm_data {
184 struct mptcp_addr_info local;
185 struct mptcp_addr_info remote;
Geliang Tangb6c08382020-09-24 08:29:54 +0800186 struct list_head anno_list;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700187
188 spinlock_t lock; /*protects the whole PM data */
189
Geliang Tang13ad9f02020-12-09 15:51:27 -0800190 u8 addr_signal;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700191 bool server_side;
192 bool work_pending;
193 bool accept_addr;
194 bool accept_subflow;
195 u8 add_addr_signaled;
196 u8 add_addr_accepted;
197 u8 local_addr_used;
198 u8 subflows;
199 u8 add_addr_signal_max;
200 u8 add_addr_accept_max;
201 u8 local_addr_max;
202 u8 subflows_max;
203 u8 status;
Geliang Tang5cb104a2020-09-24 08:29:48 +0800204 u8 rm_id;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700205};
206
Paolo Abeni18b683b2020-03-27 14:48:43 -0700207struct mptcp_data_frag {
208 struct list_head list;
209 u64 data_seq;
Paolo Abenif0e6a4c2020-11-16 10:48:07 +0100210 u16 data_len;
211 u16 offset;
212 u16 overhead;
213 u16 already_sent;
Paolo Abeni18b683b2020-03-27 14:48:43 -0700214 struct page *page;
215};
216
Mat Martineauf870fa02020-01-21 16:56:15 -0800217/* MPTCP connection sock */
218struct mptcp_sock {
219 /* inet_connection_sock must be the first member */
220 struct inet_connection_sock sk;
Peter Krystadcec37a62020-01-21 16:56:18 -0800221 u64 local_key;
222 u64 remote_key;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800223 u64 write_seq;
Paolo Abenieaa2ffa2020-11-16 10:48:08 +0100224 u64 snd_nxt;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800225 u64 ack_seq;
Florian Westphalfa3fe2b2020-11-19 11:46:02 -0800226 u64 rcv_wnd_sent;
Mat Martineau3721b9b2020-07-28 15:12:03 -0700227 u64 rcv_data_fin_seq;
Paolo Abenie93da922020-11-27 11:10:23 +0100228 int wmem_reserved;
Paolo Abenid5f49192020-09-14 10:01:17 +0200229 struct sock *last_snd;
230 int snd_burst;
Paolo Abeniea4ca582020-11-19 11:46:03 -0800231 int old_wspace;
Paolo Abeni7439d682020-11-27 11:10:26 +0100232 u64 snd_una;
233 u64 wnd_end;
Paolo Abenib51f9b82020-03-27 14:48:44 -0700234 unsigned long timer_ival;
Peter Krystad79c09492020-01-21 16:56:20 -0800235 u32 token;
Paolo Abeniea4ca582020-11-19 11:46:03 -0800236 int rmem_pending;
Paolo Abeni87952602020-11-27 11:10:24 +0100237 int rmem_released;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800238 unsigned long flags;
Christoph Paaschd22f4982020-01-21 16:56:32 -0800239 bool can_ack;
Paolo Abenib93df082020-07-23 13:02:32 +0200240 bool fully_established;
Mat Martineau3721b9b2020-07-28 15:12:03 -0700241 bool rcv_data_fin;
Mat Martineau7279da62020-07-28 15:12:02 -0700242 bool snd_data_fin_enable;
Florian Westphal50c504a2020-12-10 14:25:04 -0800243 bool rcv_fastclose;
Davide Caratti37198e92020-10-06 18:26:17 +0200244 bool use_64bit_ack; /* Set when we received a 64-bit DSN */
Peter Krystadec3edaa2020-03-27 14:48:40 -0700245 spinlock_t join_list_lock;
Paolo Abeniea4ca582020-11-19 11:46:03 -0800246 struct sock *ack_hint;
Paolo Abeni80992012020-02-26 10:14:47 +0100247 struct work_struct work;
Paolo Abeniab174ad2020-09-14 10:01:12 +0200248 struct sk_buff *ooo_last_skb;
249 struct rb_root out_of_order_queue;
Paolo Abeni87952602020-11-27 11:10:24 +0100250 struct sk_buff_head receive_queue;
Paolo Abeni724cfd22020-11-27 11:10:25 +0100251 struct sk_buff_head skb_tx_cache; /* this is wmem accounted */
252 int tx_pending_data;
253 int size_goal_cache;
Peter Krystadcec37a62020-01-21 16:56:18 -0800254 struct list_head conn_list;
Paolo Abeni18b683b2020-03-27 14:48:43 -0700255 struct list_head rtx_queue;
Paolo Abenif0e6a4c2020-11-16 10:48:07 +0100256 struct mptcp_data_frag *first_pending;
Peter Krystadec3edaa2020-03-27 14:48:40 -0700257 struct list_head join_list;
Mat Martineauf870fa02020-01-21 16:56:15 -0800258 struct socket *subflow; /* outgoing connect/listener/!mp_capable */
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800259 struct sock *first;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700260 struct mptcp_pm_data pm;
Florian Westphala6b118f2020-06-30 21:24:45 +0200261 struct {
262 u32 space; /* bytes copied in last measurement window */
263 u32 copied; /* bytes copied in this measurement window */
264 u64 time; /* start time of measurement window */
265 u64 rtt_us; /* last maximum rtt of subflows */
266 } rcvq_space;
Mat Martineauf870fa02020-01-21 16:56:15 -0800267};
268
Paolo Abeniad80b0f2020-11-27 11:10:22 +0100269#define mptcp_lock_sock(___sk, cb) do { \
270 struct sock *__sk = (___sk); /* silence macro reuse warning */ \
271 might_sleep(); \
272 spin_lock_bh(&__sk->sk_lock.slock); \
273 if (__sk->sk_lock.owned) \
274 __lock_sock(__sk); \
275 cb; \
276 __sk->sk_lock.owned = 1; \
277 spin_unlock(&__sk->sk_lock.slock); \
278 mutex_acquire(&__sk->sk_lock.dep_map, 0, 0, _RET_IP_); \
279 local_bh_enable(); \
280} while (0)
281
Paolo Abeni87952602020-11-27 11:10:24 +0100282#define mptcp_data_lock(sk) spin_lock_bh(&(sk)->sk_lock.slock)
283#define mptcp_data_unlock(sk) spin_unlock_bh(&(sk)->sk_lock.slock)
284
Peter Krystadcec37a62020-01-21 16:56:18 -0800285#define mptcp_for_each_subflow(__msk, __subflow) \
286 list_for_each_entry(__subflow, &((__msk)->conn_list), node)
287
Mat Martineauf870fa02020-01-21 16:56:15 -0800288static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
289{
290 return (struct mptcp_sock *)sk;
291}
292
Paolo Abeniea4ca582020-11-19 11:46:03 -0800293static inline int __mptcp_space(const struct sock *sk)
294{
295 return tcp_space(sk) + READ_ONCE(mptcp_sk(sk)->rmem_pending);
296}
297
Paolo Abenif0e6a4c2020-11-16 10:48:07 +0100298static inline struct mptcp_data_frag *mptcp_send_head(const struct sock *sk)
299{
300 const struct mptcp_sock *msk = mptcp_sk(sk);
301
302 return READ_ONCE(msk->first_pending);
303}
304
305static inline struct mptcp_data_frag *mptcp_send_next(struct sock *sk)
306{
307 struct mptcp_sock *msk = mptcp_sk(sk);
308 struct mptcp_data_frag *cur;
309
310 cur = msk->first_pending;
311 return list_is_last(&cur->list, &msk->rtx_queue) ? NULL :
312 list_next_entry(cur, list);
313}
314
315static inline struct mptcp_data_frag *mptcp_pending_tail(const struct sock *sk)
316{
317 struct mptcp_sock *msk = mptcp_sk(sk);
318
319 if (!msk->first_pending)
320 return NULL;
321
322 if (WARN_ON_ONCE(list_empty(&msk->rtx_queue)))
323 return NULL;
324
325 return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list);
326}
327
Paolo Abeni18b683b2020-03-27 14:48:43 -0700328static inline struct mptcp_data_frag *mptcp_rtx_tail(const struct sock *sk)
329{
330 struct mptcp_sock *msk = mptcp_sk(sk);
331
Paolo Abeni7439d682020-11-27 11:10:26 +0100332 if (!before64(msk->snd_nxt, READ_ONCE(msk->snd_una)))
Paolo Abeni18b683b2020-03-27 14:48:43 -0700333 return NULL;
334
335 return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list);
336}
337
Florian Westphal7948f6c2020-03-27 14:48:46 -0700338static inline struct mptcp_data_frag *mptcp_rtx_head(const struct sock *sk)
339{
340 struct mptcp_sock *msk = mptcp_sk(sk);
341
Geliang Tanga386bc52020-06-15 16:34:28 +0800342 return list_first_entry_or_null(&msk->rtx_queue, struct mptcp_data_frag, list);
Florian Westphal7948f6c2020-03-27 14:48:46 -0700343}
344
Peter Krystadcec37a62020-01-21 16:56:18 -0800345struct mptcp_subflow_request_sock {
346 struct tcp_request_sock sk;
Christoph Paaschd22f4982020-01-21 16:56:32 -0800347 u16 mp_capable : 1,
Peter Krystadcec37a62020-01-21 16:56:18 -0800348 mp_join : 1,
Paolo Abenifca5c822020-04-20 16:25:06 +0200349 backup : 1;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700350 u8 local_id;
Peter Krystadf2962342020-03-27 14:48:39 -0700351 u8 remote_id;
Peter Krystadcec37a62020-01-21 16:56:18 -0800352 u64 local_key;
Peter Krystad79c09492020-01-21 16:56:20 -0800353 u64 idsn;
354 u32 token;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800355 u32 ssn_offset;
Peter Krystadf2962342020-03-27 14:48:39 -0700356 u64 thmac;
357 u32 local_nonce;
358 u32 remote_nonce;
Paolo Abeni8fd4de12020-06-17 12:08:56 +0200359 struct mptcp_sock *msk;
Paolo Abeni2c5ebd02020-06-26 19:30:00 +0200360 struct hlist_nulls_node token_node;
Peter Krystadcec37a62020-01-21 16:56:18 -0800361};
362
363static inline struct mptcp_subflow_request_sock *
364mptcp_subflow_rsk(const struct request_sock *rsk)
365{
366 return (struct mptcp_subflow_request_sock *)rsk;
367}
368
Paolo Abeni67193312020-09-14 10:01:09 +0200369enum mptcp_data_avail {
370 MPTCP_SUBFLOW_NODATA,
371 MPTCP_SUBFLOW_DATA_AVAIL,
372 MPTCP_SUBFLOW_OOO_DATA
373};
374
Peter Krystad2303f992020-01-21 16:56:17 -0800375/* MPTCP subflow context */
376struct mptcp_subflow_context {
Peter Krystadcec37a62020-01-21 16:56:18 -0800377 struct list_head node;/* conn_list of subflows */
378 u64 local_key;
379 u64 remote_key;
Peter Krystad79c09492020-01-21 16:56:20 -0800380 u64 idsn;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800381 u64 map_seq;
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800382 u32 snd_isn;
Peter Krystad79c09492020-01-21 16:56:20 -0800383 u32 token;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800384 u32 rel_write_seq;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800385 u32 map_subflow_seq;
386 u32 ssn_offset;
387 u32 map_data_len;
Peter Krystadcec37a62020-01-21 16:56:18 -0800388 u32 request_mptcp : 1, /* send MP_CAPABLE */
Peter Krystadec3edaa2020-03-27 14:48:40 -0700389 request_join : 1, /* send MP_JOIN */
390 request_bkup : 1,
Peter Krystadcec37a62020-01-21 16:56:18 -0800391 mp_capable : 1, /* remote is MPTCP capable */
Peter Krystadf2962342020-03-27 14:48:39 -0700392 mp_join : 1, /* remote is JOINing */
Paolo Abeni0be534f2020-03-19 11:06:30 +0100393 fully_established : 1, /* path validated */
Peter Krystadf2962342020-03-27 14:48:39 -0700394 pm_notified : 1, /* PM hook called for established status */
Mat Martineau648ef4b2020-01-21 16:56:24 -0800395 conn_finished : 1,
396 map_valid : 1,
Christoph Paaschd22f4982020-01-21 16:56:32 -0800397 mpc_map : 1,
Peter Krystadf2962342020-03-27 14:48:39 -0700398 backup : 1,
Christoph Paaschd22f4982020-01-21 16:56:32 -0800399 rx_eof : 1,
Paolo Abenie16163b2020-11-16 10:48:09 +0100400 can_ack : 1, /* only after processing the remote a key */
401 disposable : 1; /* ctx can be free at ulp release time */
Paolo Abeni67193312020-09-14 10:01:09 +0200402 enum mptcp_data_avail data_avail;
Peter Krystadf2962342020-03-27 14:48:39 -0700403 u32 remote_nonce;
404 u64 thmac;
405 u32 local_nonce;
Peter Krystadec3edaa2020-03-27 14:48:40 -0700406 u32 remote_token;
407 u8 hmac[MPTCPOPT_HMAC_LEN];
Peter Krystadf2962342020-03-27 14:48:39 -0700408 u8 local_id;
409 u8 remote_id;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800410
Peter Krystad2303f992020-01-21 16:56:17 -0800411 struct sock *tcp_sock; /* tcp sk backpointer */
412 struct sock *conn; /* parent mptcp_sock */
Peter Krystadcec37a62020-01-21 16:56:18 -0800413 const struct inet_connection_sock_af_ops *icsk_af_ops;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800414 void (*tcp_data_ready)(struct sock *sk);
415 void (*tcp_state_change)(struct sock *sk);
416 void (*tcp_write_space)(struct sock *sk);
417
Peter Krystad2303f992020-01-21 16:56:17 -0800418 struct rcu_head rcu;
419};
420
421static inline struct mptcp_subflow_context *
422mptcp_subflow_ctx(const struct sock *sk)
423{
424 struct inet_connection_sock *icsk = inet_csk(sk);
425
426 /* Use RCU on icsk_ulp_data only for sock diag code */
427 return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;
428}
429
430static inline struct sock *
431mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
432{
433 return subflow->tcp_sock;
434}
435
Mat Martineau648ef4b2020-01-21 16:56:24 -0800436static inline u64
437mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow)
438{
439 return tcp_sk(mptcp_subflow_tcp_sock(subflow))->copied_seq -
440 subflow->ssn_offset -
441 subflow->map_subflow_seq;
442}
443
444static inline u64
445mptcp_subflow_get_mapped_dsn(const struct mptcp_subflow_context *subflow)
446{
447 return subflow->map_seq + mptcp_subflow_get_map_offset(subflow);
448}
449
Paolo Abeni0397c6d2020-11-19 11:45:58 -0800450static inline void mptcp_add_pending_subflow(struct mptcp_sock *msk,
451 struct mptcp_subflow_context *subflow)
452{
453 sock_hold(mptcp_subflow_tcp_sock(subflow));
454 spin_lock_bh(&msk->join_list_lock);
455 list_add_tail(&subflow->node, &msk->join_list);
456 spin_unlock_bh(&msk->join_list_lock);
457}
458
Mat Martineau648ef4b2020-01-21 16:56:24 -0800459int mptcp_is_enabled(struct net *net);
Geliang Tang93f323b2020-11-03 11:05:07 -0800460unsigned int mptcp_get_add_addr_timeout(struct net *net);
Paolo Abenib93df082020-07-23 13:02:32 +0200461void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
462 struct mptcp_options_received *mp_opt);
Mat Martineau648ef4b2020-01-21 16:56:24 -0800463bool mptcp_subflow_data_available(struct sock *sk);
Paolo Abenid39dcec2020-06-26 19:29:59 +0200464void __init mptcp_subflow_init(void);
Geliang Tangd0876b22020-09-24 08:29:49 +0800465void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how);
466void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
Paolo Abenie16163b2020-11-16 10:48:09 +0100467 struct mptcp_subflow_context *subflow);
Paolo Abenid5824842020-10-09 19:00:00 +0200468void mptcp_subflow_reset(struct sock *ssk);
Peter Krystadec3edaa2020-03-27 14:48:40 -0700469
470/* called with sk socket lock held */
Paolo Abenief0da3b2020-09-14 10:01:15 +0200471int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
Peter Krystadec3edaa2020-03-27 14:48:40 -0700472 const struct mptcp_addr_info *remote);
Peter Krystad2303f992020-01-21 16:56:17 -0800473int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);
474
Mat Martineau648ef4b2020-01-21 16:56:24 -0800475static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
476 struct mptcp_subflow_context *ctx)
477{
478 sk->sk_data_ready = ctx->tcp_data_ready;
479 sk->sk_state_change = ctx->tcp_state_change;
480 sk->sk_write_space = ctx->tcp_write_space;
481
482 inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops;
483}
484
Paolo Abenid39dcec2020-06-26 19:29:59 +0200485void __init mptcp_proto_init(void);
Matthieu Baerts784325e2020-01-21 16:56:28 -0800486#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Paolo Abenid39dcec2020-06-26 19:29:59 +0200487int __init mptcp_proto_v6_init(void);
Matthieu Baerts784325e2020-01-21 16:56:28 -0800488#endif
Mat Martineau648ef4b2020-01-21 16:56:24 -0800489
Paolo Abenifca5c822020-04-20 16:25:06 +0200490struct sock *mptcp_sk_clone(const struct sock *sk,
Paolo Abenicfde1412020-04-30 15:01:52 +0200491 const struct mptcp_options_received *mp_opt,
Paolo Abenifca5c822020-04-20 16:25:06 +0200492 struct request_sock *req);
Peter Krystadcec37a62020-01-21 16:56:18 -0800493void mptcp_get_options(const struct sk_buff *skb,
Paolo Abenicfde1412020-04-30 15:01:52 +0200494 struct mptcp_options_received *mp_opt);
Peter Krystadcec37a62020-01-21 16:56:18 -0800495
496void mptcp_finish_connect(struct sock *sk);
Paolo Abenib93df082020-07-23 13:02:32 +0200497static inline bool mptcp_is_fully_established(struct sock *sk)
498{
499 return inet_sk_state_load(sk) == TCP_ESTABLISHED &&
500 READ_ONCE(mptcp_sk(sk)->fully_established);
501}
Florian Westphala6b118f2020-06-30 21:24:45 +0200502void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk);
Florian Westphal2e522132020-02-26 10:14:51 +0100503void mptcp_data_ready(struct sock *sk, struct sock *ssk);
Peter Krystadf2962342020-03-27 14:48:39 -0700504bool mptcp_finish_join(struct sock *sk);
Paolo Abeniba8f48f2020-11-16 10:48:05 +0100505bool mptcp_schedule_work(struct sock *sk);
Paolo Abeni219d0492020-12-16 12:48:34 +0100506void __mptcp_check_push(struct sock *sk, struct sock *ssk);
Paolo Abeni7439d682020-11-27 11:10:26 +0100507void __mptcp_data_acked(struct sock *sk);
Florian Westphal59832e22020-04-02 13:44:52 +0200508void mptcp_subflow_eof(struct sock *sk);
Mat Martineau1a49b2c2020-09-29 15:08:20 -0700509bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit);
Geliang Tang84dfe362020-11-19 11:46:00 -0800510void __mptcp_flush_join_list(struct mptcp_sock *msk);
Paolo Abenie16163b2020-11-16 10:48:09 +0100511static inline bool mptcp_data_fin_enabled(const struct mptcp_sock *msk)
512{
513 return READ_ONCE(msk->snd_data_fin_enable) &&
514 READ_ONCE(msk->write_seq) == READ_ONCE(msk->snd_nxt);
515}
516
Geliang Tang5c8c1642020-09-24 08:29:57 +0800517void mptcp_destroy_common(struct mptcp_sock *msk);
Peter Krystadcec37a62020-01-21 16:56:18 -0800518
Paolo Abeni2c5ebd02020-06-26 19:30:00 +0200519void __init mptcp_token_init(void);
520static inline void mptcp_token_init_request(struct request_sock *req)
521{
522 mptcp_subflow_rsk(req)->token_node.pprev = NULL;
523}
524
Peter Krystad79c09492020-01-21 16:56:20 -0800525int mptcp_token_new_request(struct request_sock *req);
Paolo Abeni2c5ebd02020-06-26 19:30:00 +0200526void mptcp_token_destroy_request(struct request_sock *req);
Peter Krystad79c09492020-01-21 16:56:20 -0800527int mptcp_token_new_connect(struct sock *sk);
Paolo Abeni2c5ebd02020-06-26 19:30:00 +0200528void mptcp_token_accept(struct mptcp_subflow_request_sock *r,
529 struct mptcp_sock *msk);
Florian Westphalc83a47e2020-07-30 21:25:54 +0200530bool mptcp_token_exists(u32 token);
Peter Krystadf2962342020-03-27 14:48:39 -0700531struct mptcp_sock *mptcp_token_get_sock(u32 token);
Paolo Abeni96d890d2020-07-09 15:12:40 +0200532struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
533 long *s_num);
Paolo Abeni2c5ebd02020-06-26 19:30:00 +0200534void mptcp_token_destroy(struct mptcp_sock *msk);
Peter Krystad79c09492020-01-21 16:56:20 -0800535
536void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn);
Peter Krystad79c09492020-01-21 16:56:20 -0800537
Peter Krystad3df523a2020-03-27 14:48:37 -0700538void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac);
Peter Krystad79c09492020-01-21 16:56:20 -0800539
Paolo Abenid39dcec2020-06-26 19:29:59 +0200540void __init mptcp_pm_init(void);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700541void mptcp_pm_data_init(struct mptcp_sock *msk);
542void mptcp_pm_new_connection(struct mptcp_sock *msk, int server_side);
543void mptcp_pm_fully_established(struct mptcp_sock *msk);
544bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk);
545void mptcp_pm_connection_closed(struct mptcp_sock *msk);
546void mptcp_pm_subflow_established(struct mptcp_sock *msk,
547 struct mptcp_subflow_context *subflow);
548void mptcp_pm_subflow_closed(struct mptcp_sock *msk, u8 id);
549void mptcp_pm_add_addr_received(struct mptcp_sock *msk,
550 const struct mptcp_addr_info *addr);
Geliang Tang84dfe362020-11-19 11:46:00 -0800551void mptcp_pm_add_addr_send_ack(struct mptcp_sock *msk);
Geliang Tangd0876b22020-09-24 08:29:49 +0800552void mptcp_pm_rm_addr_received(struct mptcp_sock *msk, u8 rm_id);
Geliang Tangb6c08382020-09-24 08:29:54 +0800553void mptcp_pm_free_anno_list(struct mptcp_sock *msk);
Geliang Tang00cfd772020-09-24 08:30:02 +0800554struct mptcp_pm_add_entry *
555mptcp_pm_del_add_timer(struct mptcp_sock *msk,
556 struct mptcp_addr_info *addr);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700557
558int mptcp_pm_announce_addr(struct mptcp_sock *msk,
Geliang Tang6a6c05a2020-09-24 08:29:50 +0800559 const struct mptcp_addr_info *addr,
Geliang Tang0f5c9e32020-12-09 15:51:24 -0800560 bool echo, bool port);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700561int mptcp_pm_remove_addr(struct mptcp_sock *msk, u8 local_id);
Geliang Tang0ee42612020-09-24 08:29:55 +0800562int mptcp_pm_remove_subflow(struct mptcp_sock *msk, u8 local_id);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700563
Geliang Tangf643b802020-09-24 08:29:47 +0800564static inline bool mptcp_pm_should_add_signal(struct mptcp_sock *msk)
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700565{
Geliang Tang13ad9f02020-12-09 15:51:27 -0800566 return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_SIGNAL);
Geliang Tangd91d3222020-11-19 11:45:59 -0800567}
568
569static inline bool mptcp_pm_should_add_signal_echo(struct mptcp_sock *msk)
570{
Geliang Tang13ad9f02020-12-09 15:51:27 -0800571 return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_ECHO);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700572}
573
Geliang Tang84dfe362020-11-19 11:46:00 -0800574static inline bool mptcp_pm_should_add_signal_ipv6(struct mptcp_sock *msk)
575{
Geliang Tang13ad9f02020-12-09 15:51:27 -0800576 return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_IPV6);
Geliang Tang84dfe362020-11-19 11:46:00 -0800577}
578
Geliang Tang4a2777a82020-12-09 15:51:22 -0800579static inline bool mptcp_pm_should_add_signal_port(struct mptcp_sock *msk)
580{
Geliang Tang13ad9f02020-12-09 15:51:27 -0800581 return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_PORT);
Geliang Tang4a2777a82020-12-09 15:51:22 -0800582}
583
Geliang Tang5cb104a2020-09-24 08:29:48 +0800584static inline bool mptcp_pm_should_rm_signal(struct mptcp_sock *msk)
585{
Geliang Tang13ad9f02020-12-09 15:51:27 -0800586 return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_RM_ADDR_SIGNAL);
Geliang Tang5cb104a2020-09-24 08:29:48 +0800587}
588
Geliang Tang4a2777a82020-12-09 15:51:22 -0800589static inline unsigned int mptcp_add_addr_len(int family, bool echo, bool port)
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700590{
Geliang Tang2ec72fa2020-12-09 15:51:21 -0800591 u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
592
593 if (family == AF_INET6)
594 len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
595 if (!echo)
596 len += MPTCPOPT_THMAC_LEN;
Geliang Tang4a2777a82020-12-09 15:51:22 -0800597 if (port)
598 len += TCPOLEN_MPTCP_PORT_LEN;
Geliang Tang2ec72fa2020-12-09 15:51:21 -0800599
600 return len;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700601}
602
Geliang Tangf643b802020-09-24 08:29:47 +0800603bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
Geliang Tang4a2777a82020-12-09 15:51:22 -0800604 struct mptcp_addr_info *saddr, bool *echo, bool *port);
Geliang Tang5cb104a2020-09-24 08:29:48 +0800605bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
606 u8 *rm_id);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700607int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
608
Paolo Abenid39dcec2020-06-26 19:29:59 +0200609void __init mptcp_pm_nl_init(void);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700610void mptcp_pm_nl_data_init(struct mptcp_sock *msk);
611void mptcp_pm_nl_fully_established(struct mptcp_sock *msk);
612void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk);
613void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk);
Geliang Tang84dfe362020-11-19 11:46:00 -0800614void mptcp_pm_nl_add_addr_send_ack(struct mptcp_sock *msk);
Geliang Tangd0876b22020-09-24 08:29:49 +0800615void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk);
Geliang Tang0ee42612020-09-24 08:29:55 +0800616void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk, u8 rm_id);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700617int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
618
Mat Martineau6d0060f2020-01-21 16:56:23 -0800619static inline struct mptcp_ext *mptcp_get_ext(struct sk_buff *skb)
620{
621 return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP);
622}
623
Davide Caratti5147dfb2020-03-27 14:48:49 -0700624void mptcp_diag_subflow_init(struct tcp_ulp_ops *ops);
625
Paolo Abenid5f49192020-09-14 10:01:17 +0200626static inline bool __mptcp_check_fallback(const struct mptcp_sock *msk)
Davide Carattie1ff9e82020-06-29 22:26:20 +0200627{
628 return test_bit(MPTCP_FALLBACK_DONE, &msk->flags);
629}
630
Paolo Abenid5f49192020-09-14 10:01:17 +0200631static inline bool mptcp_check_fallback(const struct sock *sk)
Davide Carattie1ff9e82020-06-29 22:26:20 +0200632{
633 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
634 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
635
636 return __mptcp_check_fallback(msk);
637}
638
639static inline void __mptcp_do_fallback(struct mptcp_sock *msk)
640{
641 if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags)) {
642 pr_debug("TCP fallback already done (msk=%p)", msk);
643 return;
644 }
645 set_bit(MPTCP_FALLBACK_DONE, &msk->flags);
646}
647
648static inline void mptcp_do_fallback(struct sock *sk)
649{
650 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
651 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
652
653 __mptcp_do_fallback(msk);
654}
655
656#define pr_fallback(a) pr_debug("%s:fallback to TCP (msk=%p)", __func__, a)
657
Davide Caratti8fd73802020-06-29 22:26:21 +0200658static inline bool subflow_simultaneous_connect(struct sock *sk)
659{
660 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
661 struct sock *parent = subflow->conn;
662
663 return sk->sk_state == TCP_ESTABLISHED &&
664 !mptcp_sk(parent)->pm.server_side &&
665 !subflow->conn_finished;
666}
667
Florian Westphal9466a1c2020-07-30 21:25:56 +0200668#ifdef CONFIG_SYN_COOKIES
669void subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
670 struct sk_buff *skb);
671bool mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
672 struct sk_buff *skb);
673void __init mptcp_join_cookie_init(void);
674#else
675static inline void
676subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
677 struct sk_buff *skb) {}
678static inline bool
679mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
680 struct sk_buff *skb)
681{
682 return false;
683}
684
685static inline void mptcp_join_cookie_init(void) {}
686#endif
687
Mat Martineauf870fa02020-01-21 16:56:15 -0800688#endif /* __MPTCP_PROTOCOL_H */