blob: 837e010575443e04244ea4bc85dd96bdf5c75d18 [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)
Peter Krystadeda7acd2020-01-21 16:56:16 -080026
27/* MPTCP option subtypes */
28#define MPTCPOPT_MP_CAPABLE 0
29#define MPTCPOPT_MP_JOIN 1
30#define MPTCPOPT_DSS 2
31#define MPTCPOPT_ADD_ADDR 3
32#define MPTCPOPT_RM_ADDR 4
33#define MPTCPOPT_MP_PRIO 5
34#define MPTCPOPT_MP_FAIL 6
35#define MPTCPOPT_MP_FASTCLOSE 7
36
37/* MPTCP suboption lengths */
Christoph Paaschcc7972e2020-01-21 16:56:31 -080038#define TCPOLEN_MPTCP_MPC_SYN 4
Peter Krystadeda7acd2020-01-21 16:56:16 -080039#define TCPOLEN_MPTCP_MPC_SYNACK 12
40#define TCPOLEN_MPTCP_MPC_ACK 20
Christoph Paaschcc7972e2020-01-21 16:56:31 -080041#define TCPOLEN_MPTCP_MPC_ACK_DATA 22
Peter Krystadf2962342020-03-27 14:48:39 -070042#define TCPOLEN_MPTCP_MPJ_SYN 12
43#define TCPOLEN_MPTCP_MPJ_SYNACK 16
44#define TCPOLEN_MPTCP_MPJ_ACK 24
Mat Martineau6d0060f2020-01-21 16:56:23 -080045#define TCPOLEN_MPTCP_DSS_BASE 4
Mat Martineau648ef4b2020-01-21 16:56:24 -080046#define TCPOLEN_MPTCP_DSS_ACK32 4
Mat Martineau6d0060f2020-01-21 16:56:23 -080047#define TCPOLEN_MPTCP_DSS_ACK64 8
Mat Martineau648ef4b2020-01-21 16:56:24 -080048#define TCPOLEN_MPTCP_DSS_MAP32 10
Mat Martineau6d0060f2020-01-21 16:56:23 -080049#define TCPOLEN_MPTCP_DSS_MAP64 14
50#define TCPOLEN_MPTCP_DSS_CHECKSUM 2
Peter Krystad3df523a2020-03-27 14:48:37 -070051#define TCPOLEN_MPTCP_ADD_ADDR 16
52#define TCPOLEN_MPTCP_ADD_ADDR_PORT 18
53#define TCPOLEN_MPTCP_ADD_ADDR_BASE 8
54#define TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT 10
55#define TCPOLEN_MPTCP_ADD_ADDR6 28
56#define TCPOLEN_MPTCP_ADD_ADDR6_PORT 30
57#define TCPOLEN_MPTCP_ADD_ADDR6_BASE 20
58#define TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT 22
59#define TCPOLEN_MPTCP_PORT_LEN 2
60#define TCPOLEN_MPTCP_RM_ADDR_BASE 4
Peter Krystadeda7acd2020-01-21 16:56:16 -080061
Peter Krystadec3edaa2020-03-27 14:48:40 -070062/* MPTCP MP_JOIN flags */
Peter Krystadf2962342020-03-27 14:48:39 -070063#define MPTCPOPT_BACKUP BIT(0)
64#define MPTCPOPT_HMAC_LEN 20
Peter Krystadec3edaa2020-03-27 14:48:40 -070065#define MPTCPOPT_THMAC_LEN 8
Peter Krystadf2962342020-03-27 14:48:39 -070066
Peter Krystadeda7acd2020-01-21 16:56:16 -080067/* MPTCP MP_CAPABLE flags */
68#define MPTCP_VERSION_MASK (0x0F)
69#define MPTCP_CAP_CHECKSUM_REQD BIT(7)
70#define MPTCP_CAP_EXTENSIBILITY BIT(6)
Paolo Abeni65492c52020-01-21 16:56:30 -080071#define MPTCP_CAP_HMAC_SHA256 BIT(0)
Peter Krystadeda7acd2020-01-21 16:56:16 -080072#define MPTCP_CAP_FLAG_MASK (0x3F)
73
Mat Martineau6d0060f2020-01-21 16:56:23 -080074/* MPTCP DSS flags */
75#define MPTCP_DSS_DATA_FIN BIT(4)
76#define MPTCP_DSS_DSN64 BIT(3)
77#define MPTCP_DSS_HAS_MAP BIT(2)
78#define MPTCP_DSS_ACK64 BIT(1)
79#define MPTCP_DSS_HAS_ACK BIT(0)
Mat Martineau648ef4b2020-01-21 16:56:24 -080080#define MPTCP_DSS_FLAG_MASK (0x1F)
81
Peter Krystad3df523a2020-03-27 14:48:37 -070082/* MPTCP ADD_ADDR flags */
83#define MPTCP_ADDR_ECHO BIT(0)
Peter Krystad3df523a2020-03-27 14:48:37 -070084#define MPTCP_ADDR_IPVERSION_4 4
85#define MPTCP_ADDR_IPVERSION_6 6
86
Mat Martineau648ef4b2020-01-21 16:56:24 -080087/* MPTCP socket flags */
Florian Westphald99bfed2020-02-17 16:54:38 +010088#define MPTCP_DATA_READY 0
89#define MPTCP_SEND_SPACE 1
Paolo Abeni3b1d6212020-03-27 14:48:48 -070090#define MPTCP_WORK_RTX 2
Florian Westphal59832e22020-04-02 13:44:52 +020091#define MPTCP_WORK_EOF 3
Davide Carattie1ff9e82020-06-29 22:26:20 +020092#define MPTCP_FALLBACK_DONE 4
Mat Martineau6d0060f2020-01-21 16:56:23 -080093
Paolo Abenicfde1412020-04-30 15:01:52 +020094struct mptcp_options_received {
95 u64 sndr_key;
96 u64 rcvr_key;
97 u64 data_ack;
98 u64 data_seq;
99 u32 subflow_seq;
100 u16 data_len;
101 u16 mp_capable : 1,
102 mp_join : 1,
103 dss : 1,
104 add_addr : 1,
105 rm_addr : 1,
106 family : 4,
107 echo : 1,
108 backup : 1;
109 u32 token;
110 u32 nonce;
111 u64 thmac;
112 u8 hmac[20];
113 u8 join_id;
114 u8 use_map:1,
115 dsn64:1,
116 data_fin:1,
117 use_ack:1,
118 ack64:1,
119 mpc_map:1,
120 __unused:2;
121 u8 addr_id;
122 u8 rm_id;
123 union {
124 struct in_addr addr;
125#if IS_ENABLED(CONFIG_MPTCP_IPV6)
126 struct in6_addr addr6;
127#endif
128 };
129 u64 ahmac;
130 u16 port;
131};
132
Peter Krystad3df523a2020-03-27 14:48:37 -0700133static inline __be32 mptcp_option(u8 subopt, u8 len, u8 nib, u8 field)
134{
135 return htonl((TCPOPT_MPTCP << 24) | (len << 16) | (subopt << 12) |
136 ((nib & 0xF) << 8) | field);
137}
138
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700139struct mptcp_addr_info {
140 sa_family_t family;
141 __be16 port;
142 u8 id;
Paolo Abenief0da3b2020-09-14 10:01:15 +0200143 u8 flags;
144 int ifindex;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700145 union {
146 struct in_addr addr;
147#if IS_ENABLED(CONFIG_MPTCP_IPV6)
148 struct in6_addr addr6;
149#endif
150 };
151};
152
153enum mptcp_pm_status {
154 MPTCP_PM_ADD_ADDR_RECEIVED,
Geliang Tangd0876b22020-09-24 08:29:49 +0800155 MPTCP_PM_RM_ADDR_RECEIVED,
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700156 MPTCP_PM_ESTABLISHED,
157 MPTCP_PM_SUBFLOW_ESTABLISHED,
158};
159
160struct mptcp_pm_data {
161 struct mptcp_addr_info local;
162 struct mptcp_addr_info remote;
163
164 spinlock_t lock; /*protects the whole PM data */
165
Geliang Tangf643b802020-09-24 08:29:47 +0800166 bool add_addr_signal;
Geliang Tang5cb104a2020-09-24 08:29:48 +0800167 bool rm_addr_signal;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700168 bool server_side;
169 bool work_pending;
170 bool accept_addr;
171 bool accept_subflow;
172 u8 add_addr_signaled;
173 u8 add_addr_accepted;
174 u8 local_addr_used;
175 u8 subflows;
176 u8 add_addr_signal_max;
177 u8 add_addr_accept_max;
178 u8 local_addr_max;
179 u8 subflows_max;
180 u8 status;
Geliang Tang5cb104a2020-09-24 08:29:48 +0800181 u8 rm_id;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700182};
183
Paolo Abeni18b683b2020-03-27 14:48:43 -0700184struct mptcp_data_frag {
185 struct list_head list;
186 u64 data_seq;
187 int data_len;
188 int offset;
189 int overhead;
190 struct page *page;
191};
192
Mat Martineauf870fa02020-01-21 16:56:15 -0800193/* MPTCP connection sock */
194struct mptcp_sock {
195 /* inet_connection_sock must be the first member */
196 struct inet_connection_sock sk;
Peter Krystadcec37a62020-01-21 16:56:18 -0800197 u64 local_key;
198 u64 remote_key;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800199 u64 write_seq;
200 u64 ack_seq;
Mat Martineau3721b9b2020-07-28 15:12:03 -0700201 u64 rcv_data_fin_seq;
Paolo Abenid5f49192020-09-14 10:01:17 +0200202 struct sock *last_snd;
203 int snd_burst;
Paolo Abenicc9d2562020-03-27 14:48:42 -0700204 atomic64_t snd_una;
Paolo Abenib51f9b82020-03-27 14:48:44 -0700205 unsigned long timer_ival;
Peter Krystad79c09492020-01-21 16:56:20 -0800206 u32 token;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800207 unsigned long flags;
Christoph Paaschd22f4982020-01-21 16:56:32 -0800208 bool can_ack;
Paolo Abenib93df082020-07-23 13:02:32 +0200209 bool fully_established;
Mat Martineau3721b9b2020-07-28 15:12:03 -0700210 bool rcv_data_fin;
Mat Martineau7279da62020-07-28 15:12:02 -0700211 bool snd_data_fin_enable;
Peter Krystadec3edaa2020-03-27 14:48:40 -0700212 spinlock_t join_list_lock;
Paolo Abeni80992012020-02-26 10:14:47 +0100213 struct work_struct work;
Paolo Abeniab174ad2020-09-14 10:01:12 +0200214 struct sk_buff *ooo_last_skb;
215 struct rb_root out_of_order_queue;
Peter Krystadcec37a62020-01-21 16:56:18 -0800216 struct list_head conn_list;
Paolo Abeni18b683b2020-03-27 14:48:43 -0700217 struct list_head rtx_queue;
Peter Krystadec3edaa2020-03-27 14:48:40 -0700218 struct list_head join_list;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800219 struct skb_ext *cached_ext; /* for the next sendmsg */
Mat Martineauf870fa02020-01-21 16:56:15 -0800220 struct socket *subflow; /* outgoing connect/listener/!mp_capable */
Paolo Abeni8ab183d2020-01-21 16:56:33 -0800221 struct sock *first;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700222 struct mptcp_pm_data pm;
Florian Westphala6b118f2020-06-30 21:24:45 +0200223 struct {
224 u32 space; /* bytes copied in last measurement window */
225 u32 copied; /* bytes copied in this measurement window */
226 u64 time; /* start time of measurement window */
227 u64 rtt_us; /* last maximum rtt of subflows */
228 } rcvq_space;
Mat Martineauf870fa02020-01-21 16:56:15 -0800229};
230
Peter Krystadcec37a62020-01-21 16:56:18 -0800231#define mptcp_for_each_subflow(__msk, __subflow) \
232 list_for_each_entry(__subflow, &((__msk)->conn_list), node)
233
Mat Martineauf870fa02020-01-21 16:56:15 -0800234static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
235{
236 return (struct mptcp_sock *)sk;
237}
238
Paolo Abeni18b683b2020-03-27 14:48:43 -0700239static inline struct mptcp_data_frag *mptcp_rtx_tail(const struct sock *sk)
240{
241 struct mptcp_sock *msk = mptcp_sk(sk);
242
243 if (list_empty(&msk->rtx_queue))
244 return NULL;
245
246 return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list);
247}
248
Florian Westphal7948f6c2020-03-27 14:48:46 -0700249static inline struct mptcp_data_frag *mptcp_rtx_head(const struct sock *sk)
250{
251 struct mptcp_sock *msk = mptcp_sk(sk);
252
Geliang Tanga386bc52020-06-15 16:34:28 +0800253 return list_first_entry_or_null(&msk->rtx_queue, struct mptcp_data_frag, list);
Florian Westphal7948f6c2020-03-27 14:48:46 -0700254}
255
Peter Krystadcec37a62020-01-21 16:56:18 -0800256struct mptcp_subflow_request_sock {
257 struct tcp_request_sock sk;
Christoph Paaschd22f4982020-01-21 16:56:32 -0800258 u16 mp_capable : 1,
Peter Krystadcec37a62020-01-21 16:56:18 -0800259 mp_join : 1,
Paolo Abenifca5c822020-04-20 16:25:06 +0200260 backup : 1;
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700261 u8 local_id;
Peter Krystadf2962342020-03-27 14:48:39 -0700262 u8 remote_id;
Peter Krystadcec37a62020-01-21 16:56:18 -0800263 u64 local_key;
Peter Krystad79c09492020-01-21 16:56:20 -0800264 u64 idsn;
265 u32 token;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800266 u32 ssn_offset;
Peter Krystadf2962342020-03-27 14:48:39 -0700267 u64 thmac;
268 u32 local_nonce;
269 u32 remote_nonce;
Paolo Abeni8fd4de12020-06-17 12:08:56 +0200270 struct mptcp_sock *msk;
Paolo Abeni2c5ebd02020-06-26 19:30:00 +0200271 struct hlist_nulls_node token_node;
Peter Krystadcec37a62020-01-21 16:56:18 -0800272};
273
274static inline struct mptcp_subflow_request_sock *
275mptcp_subflow_rsk(const struct request_sock *rsk)
276{
277 return (struct mptcp_subflow_request_sock *)rsk;
278}
279
Paolo Abeni67193312020-09-14 10:01:09 +0200280enum mptcp_data_avail {
281 MPTCP_SUBFLOW_NODATA,
282 MPTCP_SUBFLOW_DATA_AVAIL,
283 MPTCP_SUBFLOW_OOO_DATA
284};
285
Peter Krystad2303f992020-01-21 16:56:17 -0800286/* MPTCP subflow context */
287struct mptcp_subflow_context {
Peter Krystadcec37a62020-01-21 16:56:18 -0800288 struct list_head node;/* conn_list of subflows */
289 u64 local_key;
290 u64 remote_key;
Peter Krystad79c09492020-01-21 16:56:20 -0800291 u64 idsn;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800292 u64 map_seq;
Christoph Paaschcc7972e2020-01-21 16:56:31 -0800293 u32 snd_isn;
Peter Krystad79c09492020-01-21 16:56:20 -0800294 u32 token;
Mat Martineau6d0060f2020-01-21 16:56:23 -0800295 u32 rel_write_seq;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800296 u32 map_subflow_seq;
297 u32 ssn_offset;
298 u32 map_data_len;
Peter Krystadcec37a62020-01-21 16:56:18 -0800299 u32 request_mptcp : 1, /* send MP_CAPABLE */
Peter Krystadec3edaa2020-03-27 14:48:40 -0700300 request_join : 1, /* send MP_JOIN */
301 request_bkup : 1,
Peter Krystadcec37a62020-01-21 16:56:18 -0800302 mp_capable : 1, /* remote is MPTCP capable */
Peter Krystadf2962342020-03-27 14:48:39 -0700303 mp_join : 1, /* remote is JOINing */
Paolo Abeni0be534f2020-03-19 11:06:30 +0100304 fully_established : 1, /* path validated */
Peter Krystadf2962342020-03-27 14:48:39 -0700305 pm_notified : 1, /* PM hook called for established status */
Mat Martineau648ef4b2020-01-21 16:56:24 -0800306 conn_finished : 1,
307 map_valid : 1,
Christoph Paaschd22f4982020-01-21 16:56:32 -0800308 mpc_map : 1,
Peter Krystadf2962342020-03-27 14:48:39 -0700309 backup : 1,
Christoph Paaschd22f4982020-01-21 16:56:32 -0800310 rx_eof : 1,
Christoph Paascha0c1d0e2020-05-14 08:53:03 -0700311 use_64bit_ack : 1, /* Set when we received a 64-bit DSN */
Christoph Paaschd22f4982020-01-21 16:56:32 -0800312 can_ack : 1; /* only after processing the remote a key */
Paolo Abeni67193312020-09-14 10:01:09 +0200313 enum mptcp_data_avail data_avail;
Peter Krystadf2962342020-03-27 14:48:39 -0700314 u32 remote_nonce;
315 u64 thmac;
316 u32 local_nonce;
Peter Krystadec3edaa2020-03-27 14:48:40 -0700317 u32 remote_token;
318 u8 hmac[MPTCPOPT_HMAC_LEN];
Peter Krystadf2962342020-03-27 14:48:39 -0700319 u8 local_id;
320 u8 remote_id;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800321
Peter Krystad2303f992020-01-21 16:56:17 -0800322 struct sock *tcp_sock; /* tcp sk backpointer */
323 struct sock *conn; /* parent mptcp_sock */
Peter Krystadcec37a62020-01-21 16:56:18 -0800324 const struct inet_connection_sock_af_ops *icsk_af_ops;
Mat Martineau648ef4b2020-01-21 16:56:24 -0800325 void (*tcp_data_ready)(struct sock *sk);
326 void (*tcp_state_change)(struct sock *sk);
327 void (*tcp_write_space)(struct sock *sk);
328
Peter Krystad2303f992020-01-21 16:56:17 -0800329 struct rcu_head rcu;
330};
331
332static inline struct mptcp_subflow_context *
333mptcp_subflow_ctx(const struct sock *sk)
334{
335 struct inet_connection_sock *icsk = inet_csk(sk);
336
337 /* Use RCU on icsk_ulp_data only for sock diag code */
338 return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;
339}
340
341static inline struct sock *
342mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
343{
344 return subflow->tcp_sock;
345}
346
Mat Martineau648ef4b2020-01-21 16:56:24 -0800347static inline u64
348mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow)
349{
350 return tcp_sk(mptcp_subflow_tcp_sock(subflow))->copied_seq -
351 subflow->ssn_offset -
352 subflow->map_subflow_seq;
353}
354
355static inline u64
356mptcp_subflow_get_mapped_dsn(const struct mptcp_subflow_context *subflow)
357{
358 return subflow->map_seq + mptcp_subflow_get_map_offset(subflow);
359}
360
361int mptcp_is_enabled(struct net *net);
Paolo Abenib93df082020-07-23 13:02:32 +0200362void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
363 struct mptcp_options_received *mp_opt);
Mat Martineau648ef4b2020-01-21 16:56:24 -0800364bool mptcp_subflow_data_available(struct sock *sk);
Paolo Abenid39dcec2020-06-26 19:29:59 +0200365void __init mptcp_subflow_init(void);
Geliang Tangd0876b22020-09-24 08:29:49 +0800366void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how);
367void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
368 struct mptcp_subflow_context *subflow,
369 long timeout);
Peter Krystadec3edaa2020-03-27 14:48:40 -0700370
371/* called with sk socket lock held */
Paolo Abenief0da3b2020-09-14 10:01:15 +0200372int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
Peter Krystadec3edaa2020-03-27 14:48:40 -0700373 const struct mptcp_addr_info *remote);
Peter Krystad2303f992020-01-21 16:56:17 -0800374int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);
375
Mat Martineau648ef4b2020-01-21 16:56:24 -0800376static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
377 struct mptcp_subflow_context *ctx)
378{
379 sk->sk_data_ready = ctx->tcp_data_ready;
380 sk->sk_state_change = ctx->tcp_state_change;
381 sk->sk_write_space = ctx->tcp_write_space;
382
383 inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops;
384}
385
Paolo Abenid39dcec2020-06-26 19:29:59 +0200386void __init mptcp_proto_init(void);
Matthieu Baerts784325e2020-01-21 16:56:28 -0800387#if IS_ENABLED(CONFIG_MPTCP_IPV6)
Paolo Abenid39dcec2020-06-26 19:29:59 +0200388int __init mptcp_proto_v6_init(void);
Matthieu Baerts784325e2020-01-21 16:56:28 -0800389#endif
Mat Martineau648ef4b2020-01-21 16:56:24 -0800390
Paolo Abenifca5c822020-04-20 16:25:06 +0200391struct sock *mptcp_sk_clone(const struct sock *sk,
Paolo Abenicfde1412020-04-30 15:01:52 +0200392 const struct mptcp_options_received *mp_opt,
Paolo Abenifca5c822020-04-20 16:25:06 +0200393 struct request_sock *req);
Peter Krystadcec37a62020-01-21 16:56:18 -0800394void mptcp_get_options(const struct sk_buff *skb,
Paolo Abenicfde1412020-04-30 15:01:52 +0200395 struct mptcp_options_received *mp_opt);
Peter Krystadcec37a62020-01-21 16:56:18 -0800396
397void mptcp_finish_connect(struct sock *sk);
Paolo Abenib93df082020-07-23 13:02:32 +0200398static inline bool mptcp_is_fully_established(struct sock *sk)
399{
400 return inet_sk_state_load(sk) == TCP_ESTABLISHED &&
401 READ_ONCE(mptcp_sk(sk)->fully_established);
402}
Florian Westphala6b118f2020-06-30 21:24:45 +0200403void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk);
Florian Westphal2e522132020-02-26 10:14:51 +0100404void mptcp_data_ready(struct sock *sk, struct sock *ssk);
Peter Krystadf2962342020-03-27 14:48:39 -0700405bool mptcp_finish_join(struct sock *sk);
Paolo Abenib51f9b82020-03-27 14:48:44 -0700406void mptcp_data_acked(struct sock *sk);
Florian Westphal59832e22020-04-02 13:44:52 +0200407void mptcp_subflow_eof(struct sock *sk);
Mat Martineau3721b9b2020-07-28 15:12:03 -0700408bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq);
Peter Krystadcec37a62020-01-21 16:56:18 -0800409
Paolo Abeni2c5ebd02020-06-26 19:30:00 +0200410void __init mptcp_token_init(void);
411static inline void mptcp_token_init_request(struct request_sock *req)
412{
413 mptcp_subflow_rsk(req)->token_node.pprev = NULL;
414}
415
Peter Krystad79c09492020-01-21 16:56:20 -0800416int mptcp_token_new_request(struct request_sock *req);
Paolo Abeni2c5ebd02020-06-26 19:30:00 +0200417void mptcp_token_destroy_request(struct request_sock *req);
Peter Krystad79c09492020-01-21 16:56:20 -0800418int mptcp_token_new_connect(struct sock *sk);
Paolo Abeni2c5ebd02020-06-26 19:30:00 +0200419void mptcp_token_accept(struct mptcp_subflow_request_sock *r,
420 struct mptcp_sock *msk);
Florian Westphalc83a47e2020-07-30 21:25:54 +0200421bool mptcp_token_exists(u32 token);
Peter Krystadf2962342020-03-27 14:48:39 -0700422struct mptcp_sock *mptcp_token_get_sock(u32 token);
Paolo Abeni96d890d2020-07-09 15:12:40 +0200423struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
424 long *s_num);
Paolo Abeni2c5ebd02020-06-26 19:30:00 +0200425void mptcp_token_destroy(struct mptcp_sock *msk);
Peter Krystad79c09492020-01-21 16:56:20 -0800426
427void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn);
Peter Krystad79c09492020-01-21 16:56:20 -0800428
Peter Krystad3df523a2020-03-27 14:48:37 -0700429void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac);
Peter Krystad79c09492020-01-21 16:56:20 -0800430
Paolo Abenid39dcec2020-06-26 19:29:59 +0200431void __init mptcp_pm_init(void);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700432void mptcp_pm_data_init(struct mptcp_sock *msk);
433void mptcp_pm_new_connection(struct mptcp_sock *msk, int server_side);
434void mptcp_pm_fully_established(struct mptcp_sock *msk);
435bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk);
436void mptcp_pm_connection_closed(struct mptcp_sock *msk);
437void mptcp_pm_subflow_established(struct mptcp_sock *msk,
438 struct mptcp_subflow_context *subflow);
439void mptcp_pm_subflow_closed(struct mptcp_sock *msk, u8 id);
440void mptcp_pm_add_addr_received(struct mptcp_sock *msk,
441 const struct mptcp_addr_info *addr);
Geliang Tangd0876b22020-09-24 08:29:49 +0800442void mptcp_pm_rm_addr_received(struct mptcp_sock *msk, u8 rm_id);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700443
444int mptcp_pm_announce_addr(struct mptcp_sock *msk,
445 const struct mptcp_addr_info *addr);
446int mptcp_pm_remove_addr(struct mptcp_sock *msk, u8 local_id);
447int mptcp_pm_remove_subflow(struct mptcp_sock *msk, u8 remote_id);
448
Geliang Tangf643b802020-09-24 08:29:47 +0800449static inline bool mptcp_pm_should_add_signal(struct mptcp_sock *msk)
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700450{
Geliang Tangf643b802020-09-24 08:29:47 +0800451 return READ_ONCE(msk->pm.add_addr_signal);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700452}
453
Geliang Tang5cb104a2020-09-24 08:29:48 +0800454static inline bool mptcp_pm_should_rm_signal(struct mptcp_sock *msk)
455{
456 return READ_ONCE(msk->pm.rm_addr_signal);
457}
458
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700459static inline unsigned int mptcp_add_addr_len(int family)
460{
461 if (family == AF_INET)
462 return TCPOLEN_MPTCP_ADD_ADDR;
463 return TCPOLEN_MPTCP_ADD_ADDR6;
464}
465
Geliang Tangf643b802020-09-24 08:29:47 +0800466bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
467 struct mptcp_addr_info *saddr);
Geliang Tang5cb104a2020-09-24 08:29:48 +0800468bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
469 u8 *rm_id);
Peter Krystad1b1c7a02020-03-27 14:48:38 -0700470int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
471
Paolo Abenid39dcec2020-06-26 19:29:59 +0200472void __init mptcp_pm_nl_init(void);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700473void mptcp_pm_nl_data_init(struct mptcp_sock *msk);
474void mptcp_pm_nl_fully_established(struct mptcp_sock *msk);
475void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk);
476void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk);
Geliang Tangd0876b22020-09-24 08:29:49 +0800477void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk);
Paolo Abeni01cacb02020-03-27 14:48:51 -0700478int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
479
Mat Martineau6d0060f2020-01-21 16:56:23 -0800480static inline struct mptcp_ext *mptcp_get_ext(struct sk_buff *skb)
481{
482 return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP);
483}
484
Mat Martineau648ef4b2020-01-21 16:56:24 -0800485static inline bool before64(__u64 seq1, __u64 seq2)
486{
487 return (__s64)(seq1 - seq2) < 0;
488}
489
490#define after64(seq2, seq1) before64(seq1, seq2)
491
Davide Caratti5147dfb2020-03-27 14:48:49 -0700492void mptcp_diag_subflow_init(struct tcp_ulp_ops *ops);
493
Paolo Abenid5f49192020-09-14 10:01:17 +0200494static inline bool __mptcp_check_fallback(const struct mptcp_sock *msk)
Davide Carattie1ff9e82020-06-29 22:26:20 +0200495{
496 return test_bit(MPTCP_FALLBACK_DONE, &msk->flags);
497}
498
Paolo Abenid5f49192020-09-14 10:01:17 +0200499static inline bool mptcp_check_fallback(const struct sock *sk)
Davide Carattie1ff9e82020-06-29 22:26:20 +0200500{
501 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
502 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
503
504 return __mptcp_check_fallback(msk);
505}
506
507static inline void __mptcp_do_fallback(struct mptcp_sock *msk)
508{
509 if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags)) {
510 pr_debug("TCP fallback already done (msk=%p)", msk);
511 return;
512 }
513 set_bit(MPTCP_FALLBACK_DONE, &msk->flags);
514}
515
516static inline void mptcp_do_fallback(struct sock *sk)
517{
518 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
519 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
520
521 __mptcp_do_fallback(msk);
522}
523
524#define pr_fallback(a) pr_debug("%s:fallback to TCP (msk=%p)", __func__, a)
525
Davide Caratti8fd73802020-06-29 22:26:21 +0200526static inline bool subflow_simultaneous_connect(struct sock *sk)
527{
528 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
529 struct sock *parent = subflow->conn;
530
531 return sk->sk_state == TCP_ESTABLISHED &&
532 !mptcp_sk(parent)->pm.server_side &&
533 !subflow->conn_finished;
534}
535
Florian Westphal9466a1c2020-07-30 21:25:56 +0200536#ifdef CONFIG_SYN_COOKIES
537void subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
538 struct sk_buff *skb);
539bool mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
540 struct sk_buff *skb);
541void __init mptcp_join_cookie_init(void);
542#else
543static inline void
544subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
545 struct sk_buff *skb) {}
546static inline bool
547mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
548 struct sk_buff *skb)
549{
550 return false;
551}
552
553static inline void mptcp_join_cookie_init(void) {}
554#endif
555
Mat Martineauf870fa02020-01-21 16:56:15 -0800556#endif /* __MPTCP_PROTOCOL_H */