Branch data Line data Source code
1 : : /* 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 : :
10 : : #include <linux/random.h>
11 : : #include <net/tcp.h>
12 : : #include <net/inet_connection_sock.h>
13 : : #include <uapi/linux/mptcp.h>
14 : : #include <net/genetlink.h>
15 : : #include <net/rstreason.h>
16 : :
17 : : #define MPTCP_SUPPORTED_VERSION 1
18 : :
19 : : /* MPTCP option bits */
20 : : #define OPTION_MPTCP_MPC_SYN BIT(0)
21 : : #define OPTION_MPTCP_MPC_SYNACK BIT(1)
22 : : #define OPTION_MPTCP_MPC_ACK BIT(2)
23 : : #define OPTION_MPTCP_MPJ_SYN BIT(3)
24 : : #define OPTION_MPTCP_MPJ_SYNACK BIT(4)
25 : : #define OPTION_MPTCP_MPJ_ACK BIT(5)
26 : : #define OPTION_MPTCP_ADD_ADDR BIT(6)
27 : : #define OPTION_MPTCP_RM_ADDR BIT(7)
28 : : #define OPTION_MPTCP_FASTCLOSE BIT(8)
29 : : #define OPTION_MPTCP_PRIO BIT(9)
30 : : #define OPTION_MPTCP_RST BIT(10)
31 : : #define OPTION_MPTCP_DSS BIT(11)
32 : : #define OPTION_MPTCP_FAIL BIT(12)
33 : :
34 : : #define OPTION_MPTCP_CSUMREQD BIT(13)
35 : :
36 : : #define OPTIONS_MPTCP_MPC (OPTION_MPTCP_MPC_SYN | OPTION_MPTCP_MPC_SYNACK | \
37 : : OPTION_MPTCP_MPC_ACK)
38 : : #define OPTIONS_MPTCP_MPJ (OPTION_MPTCP_MPJ_SYN | OPTION_MPTCP_MPJ_SYNACK | \
39 : : OPTION_MPTCP_MPJ_ACK)
40 : : #define OPTIONS_MPTCP_DSS (OPTION_MPTCP_DSS | OPTION_MPTCP_CSUMREQD)
41 : :
42 : : /* MPTCP option subtypes */
43 : : #define MPTCPOPT_MP_CAPABLE 0
44 : : #define MPTCPOPT_MP_JOIN 1
45 : : #define MPTCPOPT_DSS 2
46 : : #define MPTCPOPT_ADD_ADDR 3
47 : : #define MPTCPOPT_RM_ADDR 4
48 : : #define MPTCPOPT_MP_PRIO 5
49 : : #define MPTCPOPT_MP_FAIL 6
50 : : #define MPTCPOPT_MP_FASTCLOSE 7
51 : : #define MPTCPOPT_RST 8
52 : :
53 : : /* MPTCP suboption lengths */
54 : : #define TCPOLEN_MPTCP_MPC_SYN 4
55 : : #define TCPOLEN_MPTCP_MPC_SYNACK 12
56 : : #define TCPOLEN_MPTCP_MPC_ACK 20
57 : : #define TCPOLEN_MPTCP_MPC_ACK_DATA 22
58 : : #define TCPOLEN_MPTCP_MPJ_SYN 12
59 : : #define TCPOLEN_MPTCP_MPJ_SYNACK 16
60 : : #define TCPOLEN_MPTCP_MPJ_ACK 24
61 : : #define TCPOLEN_MPTCP_DSS_BASE 4
62 : : #define TCPOLEN_MPTCP_DSS_ACK32 4
63 : : #define TCPOLEN_MPTCP_DSS_ACK64 8
64 : : #define TCPOLEN_MPTCP_DSS_MAP32 10
65 : : #define TCPOLEN_MPTCP_DSS_MAP64 14
66 : : #define TCPOLEN_MPTCP_DSS_CHECKSUM 2
67 : : #define TCPOLEN_MPTCP_ADD_ADDR 16
68 : : #define TCPOLEN_MPTCP_ADD_ADDR_PORT 18
69 : : #define TCPOLEN_MPTCP_ADD_ADDR_BASE 8
70 : : #define TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT 10
71 : : #define TCPOLEN_MPTCP_ADD_ADDR6 28
72 : : #define TCPOLEN_MPTCP_ADD_ADDR6_PORT 30
73 : : #define TCPOLEN_MPTCP_ADD_ADDR6_BASE 20
74 : : #define TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT 22
75 : : #define TCPOLEN_MPTCP_PORT_LEN 2
76 : : #define TCPOLEN_MPTCP_PORT_ALIGN 2
77 : : #define TCPOLEN_MPTCP_RM_ADDR_BASE 3
78 : : #define TCPOLEN_MPTCP_PRIO 3
79 : : #define TCPOLEN_MPTCP_PRIO_ALIGN 4
80 : : #define TCPOLEN_MPTCP_FASTCLOSE 12
81 : : #define TCPOLEN_MPTCP_RST 4
82 : : #define TCPOLEN_MPTCP_FAIL 12
83 : :
84 : : #define TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM (TCPOLEN_MPTCP_DSS_CHECKSUM + TCPOLEN_MPTCP_MPC_ACK_DATA)
85 : :
86 : : /* MPTCP MP_JOIN flags */
87 : : #define MPTCPOPT_BACKUP BIT(0)
88 : : #define MPTCPOPT_THMAC_LEN 8
89 : :
90 : : /* MPTCP MP_CAPABLE flags */
91 : : #define MPTCP_VERSION_MASK (0x0F)
92 : : #define MPTCP_CAP_CHECKSUM_REQD BIT(7)
93 : : #define MPTCP_CAP_EXTENSIBILITY BIT(6)
94 : : #define MPTCP_CAP_DENY_JOIN_ID0 BIT(5)
95 : : #define MPTCP_CAP_HMAC_SHA256 BIT(0)
96 : : #define MPTCP_CAP_FLAG_MASK (0x1F)
97 : :
98 : : /* MPTCP DSS flags */
99 : : #define MPTCP_DSS_DATA_FIN BIT(4)
100 : : #define MPTCP_DSS_DSN64 BIT(3)
101 : : #define MPTCP_DSS_HAS_MAP BIT(2)
102 : : #define MPTCP_DSS_ACK64 BIT(1)
103 : : #define MPTCP_DSS_HAS_ACK BIT(0)
104 : : #define MPTCP_DSS_FLAG_MASK (0x1F)
105 : :
106 : : /* MPTCP ADD_ADDR flags */
107 : : #define MPTCP_ADDR_ECHO BIT(0)
108 : :
109 : : /* MPTCP MP_PRIO flags */
110 : : #define MPTCP_PRIO_BKUP BIT(0)
111 : :
112 : : /* MPTCP TCPRST flags */
113 : : #define MPTCP_RST_TRANSIENT BIT(0)
114 : :
115 : : /* MPTCP socket atomic flags */
116 : : #define MPTCP_WORK_RTX 1
117 : : #define MPTCP_FALLBACK_DONE 2
118 : : #define MPTCP_WORK_CLOSE_SUBFLOW 3
119 : : #define MPTCP_RTX_DISABLED 4
120 : :
121 : : /* MPTCP socket release cb flags */
122 : : #define MPTCP_PUSH_PENDING 1
123 : : #define MPTCP_CLEAN_UNA 2
124 : : #define MPTCP_ERROR_REPORT 3
125 : : #define MPTCP_RETRANSMIT 4
126 : : #define MPTCP_FLUSH_JOIN_LIST 5
127 : : #define MPTCP_SYNC_STATE 6
128 : : #define MPTCP_SYNC_SNDBUF 7
129 : :
130 : : struct mptcp_skb_cb {
131 : : u64 map_seq;
132 : : u64 end_seq;
133 : : u32 offset;
134 : : u8 has_rxtstamp;
135 : : u8 cant_coalesce;
136 : : };
137 : :
138 : : #define MPTCP_SKB_CB(__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0]))
139 : :
140 : 88220 : static inline bool before64(__u64 seq1, __u64 seq2)
141 : : {
142 [ + + - + : 12812641 : return (__s64)(seq1 - seq2) < 0;
+ + + + +
+ + + +
+ ][ + + ]
[ - + + +
+ + - + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + -
- ]
143 : : }
144 : :
145 : : #define after64(seq2, seq1) before64(seq1, seq2)
146 : :
147 : : struct mptcp_options_received {
148 : : u64 sndr_key;
149 : : u64 rcvr_key;
150 : : u64 data_ack;
151 : : u64 data_seq;
152 : : u32 subflow_seq;
153 : : u16 data_len;
154 : : __sum16 csum;
155 : : struct_group(status,
156 : : u16 suboptions;
157 : : u16 use_map:1,
158 : : dsn64:1,
159 : : data_fin:1,
160 : : use_ack:1,
161 : : ack64:1,
162 : : mpc_map:1,
163 : : reset_reason:4,
164 : : reset_transient:1,
165 : : echo:1,
166 : : backup:1,
167 : : deny_join_id0:1,
168 : : __unused:2;
169 : : );
170 : : u8 join_id;
171 : : u32 token;
172 : : u32 nonce;
173 : : u64 thmac;
174 : : u8 hmac[MPTCPOPT_HMAC_LEN];
175 : : struct mptcp_addr_info addr;
176 : : struct mptcp_rm_list rm_list;
177 : : u64 ahmac;
178 : : u64 fail_seq;
179 : : };
180 : :
181 : 0 : static inline __be32 mptcp_option(u8 subopt, u8 len, u8 nib, u8 field)
182 : : {
183 [ + - + + : 1778111 : return htonl((TCPOPT_MPTCP << 24) | (len << 16) | (subopt << 12) |
+ + + - ]
[ + - + +
+ + ]
184 : : ((nib & 0xF) << 8) | field);
185 : : }
186 : :
187 : : enum mptcp_pm_status {
188 : : MPTCP_PM_ADD_ADDR_RECEIVED,
189 : : MPTCP_PM_ADD_ADDR_SEND_ACK,
190 : : MPTCP_PM_RM_ADDR_RECEIVED,
191 : : MPTCP_PM_ESTABLISHED,
192 : : MPTCP_PM_SUBFLOW_ESTABLISHED,
193 : : MPTCP_PM_ALREADY_ESTABLISHED, /* persistent status, set after ESTABLISHED event */
194 : : MPTCP_PM_MPC_ENDPOINT_ACCOUNTED, /* persistent status, set after MPC local address is
195 : : * accounted int id_avail_bitmap
196 : : */
197 : : MPTCP_PM_DESTROYING, /* To fence out PM list allocs */
198 : : };
199 : :
200 : : enum mptcp_pm_type {
201 : : MPTCP_PM_TYPE_KERNEL = 0,
202 : : MPTCP_PM_TYPE_USERSPACE,
203 : :
204 : : __MPTCP_PM_TYPE_NR,
205 : : __MPTCP_PM_TYPE_MAX = __MPTCP_PM_TYPE_NR - 1,
206 : : };
207 : :
208 : : /* Status bits below MPTCP_PM_ALREADY_ESTABLISHED need pm worker actions */
209 : : #define MPTCP_PM_WORK_MASK ((1 << MPTCP_PM_ALREADY_ESTABLISHED) - 1)
210 : :
211 : : enum mptcp_addr_signal_status {
212 : : MPTCP_ADD_ADDR_SIGNAL,
213 : : MPTCP_ADD_ADDR_ECHO,
214 : : MPTCP_RM_ADDR_SIGNAL,
215 : : };
216 : :
217 : : /* max value of mptcp_addr_info.id */
218 : : #define MPTCP_PM_MAX_ADDR_ID U8_MAX
219 : :
220 : : struct mptcp_pm_data {
221 : : struct mptcp_addr_info local;
222 : : struct mptcp_addr_info remote;
223 : : struct list_head anno_list;
224 : : struct list_head userspace_pm_local_addr_list;
225 : : struct mptcp_pm_ops *ops;
226 : :
227 : : spinlock_t lock; /*protects the whole PM data */
228 : :
229 : : struct_group(reset,
230 : :
231 : : u8 addr_signal;
232 : : bool server_side;
233 : : bool work_pending;
234 : : bool accept_addr;
235 : : bool accept_subflow;
236 : : bool remote_deny_join_id0;
237 : : u8 add_addr_signaled;
238 : : u8 add_addr_accepted;
239 : : u8 local_addr_used;
240 : : u8 pm_type;
241 : : u8 extra_subflows;
242 : : u8 status;
243 : :
244 : : );
245 : :
246 : : DECLARE_BITMAP(id_avail_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
247 : : struct mptcp_rm_list rm_list_tx;
248 : : struct mptcp_rm_list rm_list_rx;
249 : : };
250 : :
251 : : struct mptcp_pm_local {
252 : : struct mptcp_addr_info addr;
253 : : u32 flags;
254 : : int ifindex;
255 : : };
256 : :
257 : : struct mptcp_pm_addr_entry {
258 : : struct list_head list;
259 : : struct mptcp_addr_info addr;
260 : : u32 flags;
261 : : int ifindex;
262 : : struct socket *lsk;
263 : : };
264 : :
265 : : struct mptcp_data_frag {
266 : : struct list_head list;
267 : : u64 data_seq;
268 : : u16 data_len;
269 : : u16 offset;
270 : : u8 overhead;
271 : : u8 eor; /* currently using 1 bit */
272 : : u16 already_sent;
273 : : struct page *page;
274 : : };
275 : :
276 : : /* Arbitrary compromise between as low as possible to react timely to subflow
277 : : * close event and as big as possible to avoid being fouled by biased large
278 : : * samples due to peer sending data on a different subflow WRT to the incoming
279 : : * ack.
280 : : */
281 : : #define MPTCP_RTT_SAMPLES 5
282 : :
283 : : /* MPTCP connection sock */
284 : : struct mptcp_sock {
285 : : /* inet_connection_sock must be the first member */
286 : : struct inet_connection_sock sk;
287 : : u64 local_key; /* protected by the first subflow socket lock
288 : : * lockless access read
289 : : */
290 : : u64 remote_key; /* same as above */
291 : : u64 write_seq;
292 : : u64 bytes_sent;
293 : : u64 snd_nxt;
294 : : u64 bytes_received;
295 : : u64 ack_seq;
296 : : atomic64_t rcv_wnd_sent;
297 : : u64 rcv_data_fin_seq;
298 : : u64 bytes_retrans;
299 : : u64 bytes_consumed;
300 : : int snd_burst;
301 : : int old_wspace;
302 : : u64 recovery_snd_nxt; /* in recovery mode accept up to this seq;
303 : : * recovery related fields are under data_lock
304 : : * protection
305 : : */
306 : : u64 bytes_acked;
307 : : u64 snd_una;
308 : : u64 wnd_end;
309 : : u32 last_data_sent;
310 : : u32 last_data_recv;
311 : : u32 last_ack_recv;
312 : : unsigned long timer_ival;
313 : : u32 token;
314 : : unsigned long flags;
315 : : unsigned long cb_flags;
316 : : bool recovery; /* closing subflow write queue reinjected */
317 : : bool can_ack;
318 : : bool fully_established;
319 : : bool rcv_data_fin;
320 : : bool snd_data_fin_enable;
321 : : bool rcv_fastclose;
322 : : bool use_64bit_ack; /* Set when we received a 64-bit DSN */
323 : : bool csum_enabled;
324 : : bool allow_infinite_fallback;
325 : : u8 pending_state; /* A subflow asked to set this sk_state,
326 : : * protected by the msk data lock
327 : : */
328 : : u8 mpc_endpoint_id;
329 : : u8 recvmsg_inq:1,
330 : : cork:1,
331 : : nodelay:1,
332 : : fastopening:1,
333 : : in_accept_queue:1,
334 : : free_first:1,
335 : : rcvspace_init:1,
336 : : fastclosing:1;
337 : : u32 notsent_lowat;
338 : : int keepalive_cnt;
339 : : int keepalive_idle;
340 : : int keepalive_intvl;
341 : : int maxseg;
342 : : struct work_struct work;
343 : : struct sk_buff *ooo_last_skb;
344 : : struct rb_root out_of_order_queue;
345 : : struct list_head conn_list;
346 : : struct list_head rtx_queue;
347 : : struct mptcp_data_frag *first_pending;
348 : : struct list_head join_list;
349 : : struct sock *first; /* The mptcp ops can safely dereference, using suitable
350 : : * ONCE annotation, the subflow outside the socket
351 : : * lock as such sock is freed after close().
352 : : */
353 : : struct mptcp_pm_data pm;
354 : : struct mptcp_sched_ops *sched;
355 : :
356 : : /* Most recent rtt_us observed by in use incoming subflows. */
357 : : struct {
358 : : u32 samples[MPTCP_RTT_SAMPLES];
359 : : u32 next_sample;
360 : : } rcv_rtt_est;
361 : :
362 : : struct {
363 : : int space; /* bytes copied in last measurement window */
364 : : int copied; /* bytes copied in this measurement window */
365 : : u64 time; /* start time of measurement window */
366 : : } rcvq_space;
367 : : u8 scaling_ratio;
368 : : bool allow_subflows;
369 : :
370 : : u32 subflow_id;
371 : : u32 setsockopt_seq;
372 : : char ca_name[TCP_CA_NAME_MAX];
373 : :
374 : : spinlock_t fallback_lock; /* protects fallback,
375 : : * allow_infinite_fallback and
376 : : * allow_join
377 : : */
378 : :
379 : : struct list_head backlog_list; /* protected by the data lock */
380 : : u32 backlog_len;
381 : : u32 backlog_unaccounted;
382 : : };
383 : :
384 : : #define mptcp_data_lock(sk) spin_lock_bh(&(sk)->sk_lock.slock)
385 : : #define mptcp_data_unlock(sk) spin_unlock_bh(&(sk)->sk_lock.slock)
386 : :
387 : : #define mptcp_for_each_subflow(__msk, __subflow) \
388 : : list_for_each_entry(__subflow, &((__msk)->conn_list), node)
389 : : #define mptcp_for_each_subflow_safe(__msk, __subflow, __tmp) \
390 : : list_for_each_entry_safe(__subflow, __tmp, &((__msk)->conn_list), node)
391 : : #define mptcp_next_subflow(__msk, __subflow) \
392 : : list_next_entry_circular(__subflow, &((__msk)->conn_list), node)
393 : :
394 : : extern struct genl_family mptcp_genl_family;
395 : :
396 : 261946 : static inline void msk_owned_by_me(const struct mptcp_sock *msk)
397 : : {
398 : 1589708 : sock_owned_by_me((const struct sock *)msk);
399 : : }
400 : :
401 : : #ifdef CONFIG_DEBUG_NET
402 : : /* MPTCP-specific: we might (indirectly) call this helper with the wrong sk */
403 : : #undef tcp_sk
404 : : #define tcp_sk(ptr) ({ \
405 : : typeof(ptr) _ptr = (ptr); \
406 : : WARN_ON(_ptr->sk_protocol != IPPROTO_TCP); \
407 : : container_of_const(_ptr, struct tcp_sock, inet_conn.icsk_inet.sk); \
408 : : })
409 : : #define mptcp_sk(ptr) ({ \
410 : : typeof(ptr) _ptr = (ptr); \
411 : : WARN_ON(_ptr->sk_protocol != IPPROTO_MPTCP); \
412 : : container_of_const(_ptr, struct mptcp_sock, sk.icsk_inet.sk); \
413 : : })
414 : :
415 : : #else /* !CONFIG_DEBUG_NET */
416 : : #define mptcp_sk(ptr) container_of_const(ptr, struct mptcp_sock, sk.icsk_inet.sk)
417 : : #endif
418 : :
419 : 6794039 : static inline int mptcp_win_from_space(const struct sock *sk, int space)
420 : : {
421 [ - + ]: 6794039 : return __tcp_win_from_space(mptcp_sk(sk)->scaling_ratio, space);
422 : : }
423 : :
424 : 5675 : static inline int mptcp_space_from_win(const struct sock *sk, int win)
425 : : {
426 [ - + ]: 5675 : return __tcp_space_from_win(mptcp_sk(sk)->scaling_ratio, win);
427 : : }
428 : :
429 : 4159539 : static inline int __mptcp_space(const struct sock *sk)
430 : : {
431 : 14572067 : return mptcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf) -
432 [ - + ]: 4159539 : READ_ONCE(mptcp_sk(sk)->backlog_len) -
433 : 4159539 : sk_rmem_alloc_get(sk));
434 : : }
435 : :
436 : 4862463 : static inline struct mptcp_data_frag *mptcp_send_head(const struct sock *sk)
437 : : {
438 [ - + ]: 4862463 : const struct mptcp_sock *msk = mptcp_sk(sk);
439 : :
440 : 4862463 : return msk->first_pending;
441 : : }
442 : :
443 : 0 : static inline void mptcp_init_rtt_est(struct mptcp_sock *msk)
444 : : {
445 : 5116 : int i;
446 : :
447 [ + + + + ]: 31992 : for (i = 0; i < MPTCP_RTT_SAMPLES; ++i)
448 : 25580 : msk->rcv_rtt_est.samples[i] = U32_MAX;
449 : 5116 : msk->rcv_rtt_est.next_sample = 0;
450 : 5116 : msk->scaling_ratio = TCP_DEFAULT_SCALING_RATIO;
451 : : }
452 : :
453 : 0 : static inline u32 mptcp_rtt_us_est(const struct mptcp_sock *msk)
454 : : {
455 : 0 : u32 rtt_us = READ_ONCE(msk->rcv_rtt_est.samples[0]);
456 : 0 : int i;
457 : :
458 : : /* Lockless access of collected samples. */
459 [ + + - - : 6336725 : for (i = 1; i < MPTCP_RTT_SAMPLES; ++i)
- - ]
460 : 0 : rtt_us = min(rtt_us, READ_ONCE(msk->rcv_rtt_est.samples[i]));
461 [ + + ]: 1267345 : return rtt_us;
462 : : }
463 : :
464 : 747267 : static inline struct mptcp_data_frag *mptcp_send_next(struct sock *sk)
465 : : {
466 [ - + ]: 747267 : struct mptcp_sock *msk = mptcp_sk(sk);
467 : 747267 : struct mptcp_data_frag *cur;
468 : :
469 : 747267 : cur = msk->first_pending;
470 [ + + ]: 747267 : return list_is_last(&cur->list, &msk->rtx_queue) ? NULL :
471 : : list_next_entry(cur, list);
472 : : }
473 : :
474 : 1000394 : static inline struct mptcp_data_frag *mptcp_pending_tail(const struct sock *sk)
475 : : {
476 [ - + ]: 1000394 : const struct mptcp_sock *msk = mptcp_sk(sk);
477 : :
478 [ + + ]: 1000394 : if (!msk->first_pending)
479 : : return NULL;
480 : :
481 [ - + + - ]: 452307 : if (WARN_ON_ONCE(list_empty(&msk->rtx_queue)))
482 : : return NULL;
483 : :
484 : 452307 : return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list);
485 : : }
486 : :
487 : 497804 : static inline struct mptcp_data_frag *mptcp_rtx_head(struct sock *sk)
488 : : {
489 [ - + ]: 497804 : struct mptcp_sock *msk = mptcp_sk(sk);
490 : :
491 [ + + ]: 497804 : if (msk->snd_una == msk->snd_nxt)
492 : : return NULL;
493 : :
494 [ + + ]: 390534 : return list_first_entry_or_null(&msk->rtx_queue, struct mptcp_data_frag, list);
495 : : }
496 : :
497 : : struct csum_pseudo_header {
498 : : __be64 data_seq;
499 : : __be32 subflow_seq;
500 : : __be16 data_len;
501 : : __sum16 csum;
502 : : };
503 : :
504 : : struct mptcp_subflow_request_sock {
505 : : struct tcp_request_sock sk;
506 : : u16 mp_capable : 1,
507 : : mp_join : 1,
508 : : backup : 1,
509 : : request_bkup : 1,
510 : : csum_reqd : 1,
511 : : allow_join_id0 : 1;
512 : : u8 local_id;
513 : : u8 remote_id;
514 : : u64 local_key;
515 : : u64 idsn;
516 : : u32 token;
517 : : u32 ssn_offset;
518 : : u64 thmac;
519 : : u32 local_nonce;
520 : : u32 remote_nonce;
521 : : struct mptcp_sock *msk;
522 : : struct hlist_nulls_node token_node;
523 : : };
524 : :
525 : : static inline struct mptcp_subflow_request_sock *
526 : : mptcp_subflow_rsk(const struct request_sock *rsk)
527 : : {
528 : : return (struct mptcp_subflow_request_sock *)rsk;
529 : : }
530 : :
531 : : struct mptcp_delegated_action {
532 : : struct napi_struct napi;
533 : : local_lock_t bh_lock;
534 : : struct list_head head;
535 : : };
536 : :
537 : : DECLARE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
538 : :
539 : : #define MPTCP_DELEGATE_SCHEDULED 0
540 : : #define MPTCP_DELEGATE_SEND 1
541 : : #define MPTCP_DELEGATE_ACK 2
542 : : #define MPTCP_DELEGATE_SNDBUF 3
543 : :
544 : : #define MPTCP_DELEGATE_ACTIONS_MASK (~BIT(MPTCP_DELEGATE_SCHEDULED))
545 : : /* MPTCP subflow context */
546 : : struct mptcp_subflow_context {
547 : : struct list_head node;/* conn_list of subflows */
548 : :
549 : : struct_group(reset,
550 : :
551 : : unsigned long avg_pacing_rate; /* protected by msk socket lock */
552 : : u64 local_key;
553 : : u64 remote_key;
554 : : u64 idsn;
555 : : u64 map_seq;
556 : : u64 rcv_wnd_sent;
557 : : u32 snd_isn;
558 : : u32 token;
559 : : u32 rel_write_seq;
560 : : u32 map_subflow_seq;
561 : : u32 ssn_offset;
562 : : u32 map_data_len;
563 : : __wsum map_data_csum;
564 : : u32 map_csum_len;
565 : : u32 prev_rtt_seq;
566 : : u32 request_mptcp : 1, /* send MP_CAPABLE */
567 : : request_join : 1, /* send MP_JOIN */
568 : : request_bkup : 1,
569 : : mp_capable : 1, /* remote is MPTCP capable */
570 : : mp_join : 1, /* remote is JOINing */
571 : : pm_notified : 1, /* PM hook called for established status */
572 : : conn_finished : 1,
573 : : map_valid : 1,
574 : : map_csum_reqd : 1,
575 : : map_data_fin : 1,
576 : : mpc_map : 1,
577 : : backup : 1,
578 : : send_mp_prio : 1,
579 : : send_mp_fail : 1,
580 : : send_fastclose : 1,
581 : : send_infinite_map : 1,
582 : : remote_key_valid : 1, /* received the peer key from */
583 : : disposable : 1, /* ctx can be free at ulp release time */
584 : : closing : 1, /* must not pass rx data to msk anymore */
585 : : valid_csum_seen : 1, /* at least one csum validated */
586 : : is_mptfo : 1, /* subflow is doing TFO */
587 : : close_event_done : 1, /* has done the post-closed part */
588 : : mpc_drop : 1, /* the MPC option has been dropped in a rtx */
589 : : __unused : 9;
590 : : bool data_avail;
591 : : bool scheduled;
592 : : bool pm_listener; /* a listener managed by the kernel PM? */
593 : : bool fully_established; /* path validated */
594 : : u32 lent_mem_frag;
595 : : u32 remote_nonce;
596 : : u32 local_nonce;
597 : : u32 remote_token;
598 : : union {
599 : : u8 hmac[MPTCPOPT_HMAC_LEN]; /* MPJ subflow only */
600 : : u64 iasn; /* initial ack sequence number, MPC subflows only */
601 : : };
602 : : s16 local_id; /* if negative not initialized yet */
603 : : u8 remote_id;
604 : : u8 reset_seen:1;
605 : : u8 reset_transient:1;
606 : : u8 reset_reason:4;
607 : : u8 stale_count; /* Protected by the msk socket lock */
608 : : u8 stale; /* Protected by the msk socket lock,
609 : : * if set the subflow is unable to snd/rcv
610 : : * data, the schedule should skip it
611 : : */
612 : :
613 : : u32 subflow_id;
614 : :
615 : : long delegated_status;
616 : : unsigned long fail_tout;
617 : :
618 : : );
619 : :
620 : : struct list_head delegated_node; /* link into delegated_action, protected by local BH */
621 : :
622 : : u32 setsockopt_seq;
623 : : u32 stale_rcv_tstamp;
624 : : int cached_sndbuf; /* sndbuf size when last synced with the msk sndbuf,
625 : : * protected by the msk socket lock
626 : : */
627 : :
628 : : struct sock *tcp_sock; /* tcp sk backpointer */
629 : : struct sock *conn; /* parent mptcp_sock */
630 : : const struct inet_connection_sock_af_ops *icsk_af_ops;
631 : : void (*tcp_state_change)(struct sock *sk);
632 : : void (*tcp_error_report)(struct sock *sk);
633 : :
634 : : struct rcu_head rcu;
635 : : };
636 : :
637 : : static inline struct mptcp_subflow_context *
638 : 80 : mptcp_subflow_ctx(const struct sock *sk)
639 : : {
640 [ - + ]: 732283 : const struct inet_connection_sock *icsk = inet_csk(sk);
[ - + + - ]
641 : :
642 : : /* Use RCU on icsk_ulp_data only for sock diag code */
643 [ + + ][ - + : 57968115 : return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;
+ + + - -
+ - + - +
- + + + -
+ + - + +
- + - + -
+ - + +
+ ][ - + -
+ - + ][ -
+ + - - +
- + ][ + +
+ - + - -
+ - + + +
+ + + + +
+ - + + +
+ + - + -
- - + + +
+ - + + -
+ - + - +
+ + + - +
+ - + ][ -
+ + - - +
- + + + +
+ + - + +
- + + + +
+ + + - +
- + ][ - +
+ + - + -
+ - + - +
+ + - + +
- + + - +
- + - + -
+ + + ]
644 : : }
645 : :
646 : : static inline struct sock *
647 : 89266 : mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
648 : : {
649 [ + + ][ - + : 7040854 : return subflow->tcp_sock;
+ + + + +
+ ][ + - +
+ + + - +
- - - - +
+ + + - -
- - ][ + -
+ + + + -
+ - - + +
+ + - - -
- ]
650 : : }
651 : :
652 : : static inline void
653 : 72 : mptcp_subflow_ctx_reset(struct mptcp_subflow_context *subflow)
654 : : {
655 : 1660 : memset(&subflow->reset, 0, sizeof(subflow->reset));
656 : 1660 : subflow->request_mptcp = 1;
657 : 1660 : WRITE_ONCE(subflow->local_id, -1);
658 : 1660 : }
659 : :
660 : : /* Convert reset reasons in MPTCP to enum sk_rst_reason type */
661 : : static inline enum sk_rst_reason
662 : : sk_rst_convert_mptcp_reason(u32 reason)
663 : : {
664 : : switch (reason) {
665 : : case MPTCP_RST_EUNSPEC:
666 : : return SK_RST_REASON_MPTCP_RST_EUNSPEC;
667 : : case MPTCP_RST_EMPTCP:
668 : : return SK_RST_REASON_MPTCP_RST_EMPTCP;
669 : : case MPTCP_RST_ERESOURCE:
670 : : return SK_RST_REASON_MPTCP_RST_ERESOURCE;
671 : : case MPTCP_RST_EPROHIBIT:
672 : : return SK_RST_REASON_MPTCP_RST_EPROHIBIT;
673 : : case MPTCP_RST_EWQ2BIG:
674 : : return SK_RST_REASON_MPTCP_RST_EWQ2BIG;
675 : : case MPTCP_RST_EBADPERF:
676 : : return SK_RST_REASON_MPTCP_RST_EBADPERF;
677 : : case MPTCP_RST_EMIDDLEBOX:
678 : : return SK_RST_REASON_MPTCP_RST_EMIDDLEBOX;
679 : : default:
680 : : /* It should not happen, or else errors may occur
681 : : * in MPTCP layer
682 : : */
683 : : return SK_RST_REASON_ERROR;
684 : : }
685 : : }
686 : :
687 : : static inline void
688 : 48 : mptcp_send_active_reset_reason(struct sock *sk)
689 : : {
690 : 48 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
691 : 48 : enum sk_rst_reason reason;
692 : :
693 [ + - ]: 48 : reason = sk_rst_convert_mptcp_reason(subflow->reset_reason);
694 : 48 : tcp_send_active_reset(sk, GFP_ATOMIC, reason);
695 : 48 : }
696 : :
697 : : /* Made the fwd mem carried by the given skb available to the msk,
698 : : * To be paired with a previous mptcp_subflow_lend_fwdmem() before freeing
699 : : * the skb or setting the skb ownership.
700 : : */
701 : 1106745 : static inline void mptcp_borrow_fwdmem(struct sock *sk, struct sk_buff *skb)
702 : : {
703 : 1106745 : struct sock *ssk = skb->sk;
704 : :
705 : : /* The subflow just lend the skb fwd memory; if the subflow meanwhile
706 : : * closed, mptcp_close_ssk() already released the ssk rcv memory.
707 : : */
708 [ - + ]: 1106745 : DEBUG_NET_WARN_ON_ONCE(skb->destructor);
709 [ + - ]: 1106745 : sk_forward_alloc_add(sk, skb->truesize);
710 [ + - ]: 1106745 : if (!ssk)
711 : : return;
712 : :
713 : 1106745 : atomic_sub(skb->truesize, &ssk->sk_rmem_alloc);
714 : 1106745 : skb->sk = NULL;
715 : : }
716 : :
717 : : static inline void
718 : 80766 : __mptcp_subflow_lend_fwdmem(struct mptcp_subflow_context *subflow, int size)
719 : : {
720 : 1143202 : int frag = (subflow->lent_mem_frag + size) & (PAGE_SIZE - 1);
721 : :
722 : 36507 : subflow->lent_mem_frag = frag;
723 : : }
724 : :
725 : : static inline void
726 : 80688 : mptcp_subflow_lend_fwdmem(struct mptcp_subflow_context *subflow,
727 : : struct sk_buff *skb)
728 : : {
729 : 1106745 : __mptcp_subflow_lend_fwdmem(subflow, skb->truesize);
730 : 1106745 : skb->destructor = NULL;
731 : : }
732 : :
733 : : static inline u64
734 : 5055065 : mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow)
735 : : {
736 [ - + ]: 5055065 : return tcp_sk(mptcp_subflow_tcp_sock(subflow))->copied_seq -
737 : 5055065 : subflow->ssn_offset -
738 : 5055065 : subflow->map_subflow_seq;
739 : : }
740 : :
741 : : static inline u64
742 : 168986 : mptcp_subflow_get_mapped_dsn(const struct mptcp_subflow_context *subflow)
743 : : {
744 [ - + ]: 2221097 : return subflow->map_seq + mptcp_subflow_get_map_offset(subflow);
745 : : }
746 : :
747 : : void mptcp_subflow_process_delegated(struct sock *ssk, long actions);
748 : :
749 : 146489 : static inline void mptcp_subflow_delegate(struct mptcp_subflow_context *subflow, int action)
750 : : {
751 [ - + ]: 146489 : long old, set_bits = BIT(MPTCP_DELEGATE_SCHEDULED) | BIT(action);
752 : 146489 : struct mptcp_delegated_action *delegated;
753 : 146489 : bool schedule;
754 : :
755 : : /* the caller held the subflow bh socket lock */
756 [ + - + - : 146489 : lockdep_assert_in_softirq();
+ - + - -
+ ]
757 : :
758 : : /* The implied barrier pairs with tcp_release_cb_override()
759 : : * mptcp_napi_poll(), and ensures the below list check sees list
760 : : * updates done prior to delegated status bits changes
761 : : */
762 [ - + - + ]: 146489 : old = set_mask_bits(&subflow->delegated_status, 0, set_bits);
763 [ + + ]: 146489 : if (!(old & BIT(MPTCP_DELEGATE_SCHEDULED))) {
764 [ - + + - ]: 80645 : if (WARN_ON_ONCE(!list_empty(&subflow->delegated_node)))
765 : : return;
766 : :
767 [ + - + - : 80645 : local_lock_nested_bh(&mptcp_delegated_actions.bh_lock);
+ - + - -
+ ]
768 : 80645 : delegated = this_cpu_ptr(&mptcp_delegated_actions);
769 : 80645 : schedule = list_empty(&delegated->head);
770 : 80645 : list_add_tail(&subflow->delegated_node, &delegated->head);
771 : 80645 : local_unlock_nested_bh(&mptcp_delegated_actions.bh_lock);
772 : 80645 : sock_hold(mptcp_subflow_tcp_sock(subflow));
773 [ + + ]: 80645 : if (schedule)
774 : 78766 : napi_schedule(&delegated->napi);
775 : : }
776 : : }
777 : :
778 : : static inline struct mptcp_subflow_context *
779 : 158925 : mptcp_subflow_delegated_next(struct mptcp_delegated_action *delegated)
780 : : {
781 : 158925 : struct mptcp_subflow_context *ret;
782 : :
783 [ + - + - : 158925 : local_lock_nested_bh(&mptcp_delegated_actions.bh_lock);
+ - + - -
+ ]
784 [ + + ]: 158925 : if (list_empty(&delegated->head)) {
785 : 57848 : local_unlock_nested_bh(&mptcp_delegated_actions.bh_lock);
786 : 57384 : return NULL;
787 : : }
788 : :
789 : 80645 : ret = list_first_entry(&delegated->head, struct mptcp_subflow_context, delegated_node);
790 : 80645 : list_del_init(&ret->delegated_node);
791 : 80645 : local_unlock_nested_bh(&mptcp_delegated_actions.bh_lock);
792 : 80645 : return ret;
793 : : }
794 : :
795 : : void __mptcp_inherit_memcg(struct sock *sk, struct sock *ssk, gfp_t gfp);
796 : : void __mptcp_inherit_cgrp_data(struct sock *sk, struct sock *ssk);
797 : :
798 : : int mptcp_is_enabled(const struct net *net);
799 : : unsigned int mptcp_get_add_addr_timeout(const struct net *net);
800 : : int mptcp_is_checksum_enabled(const struct net *net);
801 : : int mptcp_allow_join_id0(const struct net *net);
802 : : unsigned int mptcp_stale_loss_cnt(const struct net *net);
803 : : unsigned int mptcp_close_timeout(const struct sock *sk);
804 : : int mptcp_get_pm_type(const struct net *net);
805 : : const char *mptcp_get_path_manager(const struct net *net);
806 : : const char *mptcp_get_scheduler(const struct net *net);
807 : : unsigned int mptcp_add_addr_v6_port_drop_ts(const struct net *net);
808 : :
809 : : void mptcp_active_disable(struct sock *sk);
810 : : bool mptcp_active_should_disable(struct sock *ssk);
811 : : void mptcp_active_enable(struct sock *sk);
812 : :
813 : : void mptcp_get_available_schedulers(char *buf, size_t maxlen);
814 : : void __mptcp_subflow_fully_established(struct mptcp_sock *msk,
815 : : struct mptcp_subflow_context *subflow,
816 : : const struct mptcp_options_received *mp_opt);
817 : : bool __mptcp_retransmit_pending_data(struct sock *sk);
818 : : void mptcp_check_and_set_pending(struct sock *sk);
819 : : void __mptcp_push_pending(struct sock *sk, unsigned int flags);
820 : : bool mptcp_subflow_data_available(struct sock *sk);
821 : : void __init mptcp_subflow_init(void);
822 : : void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how);
823 : : void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
824 : : struct mptcp_subflow_context *subflow);
825 : : void __mptcp_subflow_send_ack(struct sock *ssk);
826 : : void mptcp_subflow_reset(struct sock *ssk);
827 : : void mptcp_subflow_queue_clean(struct sock *sk, struct sock *ssk);
828 : : void mptcp_sock_graft(struct sock *sk, struct socket *parent);
829 : : u64 mptcp_wnd_end(const struct mptcp_sock *msk);
830 : : void mptcp_set_timeout(struct sock *sk);
831 : : struct sock *__mptcp_nmpc_sk(struct mptcp_sock *msk);
832 : : bool __mptcp_close(struct sock *sk, long timeout);
833 : : void mptcp_cancel_work(struct sock *sk);
834 : : void __mptcp_unaccepted_force_close(struct sock *sk);
835 : : void mptcp_set_state(struct sock *sk, int state);
836 : :
837 : : bool mptcp_addresses_equal(const struct mptcp_addr_info *a,
838 : : const struct mptcp_addr_info *b, bool use_port);
839 : : void mptcp_local_address(const struct sock_common *skc,
840 : : struct mptcp_addr_info *addr);
841 : : void mptcp_remote_address(const struct sock_common *skc,
842 : : struct mptcp_addr_info *addr);
843 : :
844 : : /* called with sk socket lock held */
845 : : int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_pm_local *local,
846 : : const struct mptcp_addr_info *remote);
847 : : int mptcp_subflow_create_socket(struct sock *sk, unsigned short family,
848 : : struct socket **new_sock);
849 : : void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
850 : : struct sockaddr_storage *addr,
851 : : unsigned short family);
852 : : struct mptcp_sched_ops *mptcp_sched_find(const char *name);
853 : : int mptcp_validate_scheduler(struct mptcp_sched_ops *sched);
854 : : int mptcp_register_scheduler(struct mptcp_sched_ops *sched);
855 : : void mptcp_unregister_scheduler(struct mptcp_sched_ops *sched);
856 : : void mptcp_sched_init(void);
857 : : int mptcp_init_sched(struct mptcp_sock *msk,
858 : : struct mptcp_sched_ops *sched);
859 : : void mptcp_release_sched(struct mptcp_sock *msk);
860 : : void mptcp_subflow_set_scheduled(struct mptcp_subflow_context *subflow,
861 : : bool scheduled);
862 : : struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk);
863 : : struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk);
864 : : int mptcp_sched_get_send(struct mptcp_sock *msk);
865 : : int mptcp_sched_get_retrans(struct mptcp_sock *msk);
866 : :
867 : 110305 : static inline u64 mptcp_data_avail(const struct mptcp_sock *msk)
868 : : {
869 [ + + ]: 1070438 : return READ_ONCE(msk->bytes_received) - READ_ONCE(msk->bytes_consumed);
870 : : }
871 : :
872 : 1923677 : static inline bool mptcp_epollin_ready(const struct sock *sk)
873 : : {
874 [ - + ]: 1923677 : u64 data_avail = mptcp_data_avail(mptcp_sk(sk));
875 : :
876 [ + + ]: 1923677 : if (!data_avail)
877 : : return false;
878 : :
879 : : /* mptcp doesn't have to deal with small skbs in the receive queue,
880 : : * as it can always coalesce them
881 : : */
882 [ - + - - ]: 2604446 : return (data_avail >= sk->sk_rcvlowat) ||
883 : 0 : tcp_under_memory_pressure(sk);
884 : : }
885 : :
886 : : int mptcp_set_rcvlowat(struct sock *sk, int val);
887 : :
888 : 1812771 : static inline bool __tcp_can_send(const struct sock *ssk)
889 : : {
890 : : /* only send if our side has not closed yet */
891 [ + + ]: 2463060 : return ((1 << inet_sk_state_load(ssk)) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT));
892 : : }
893 : :
894 : 1915166 : static inline bool __mptcp_subflow_active(struct mptcp_subflow_context *subflow)
895 : : {
896 : : /* can't send if JOIN hasn't completed yet (i.e. is usable for mptcp) */
897 [ + + + + ]: 1915166 : if (subflow->request_join && !READ_ONCE(subflow->fully_established))
[ + + - +
+ + ]
898 : : return false;
899 : :
900 : 1914700 : return __tcp_can_send(mptcp_subflow_tcp_sock(subflow));
901 : : }
902 : :
903 : : void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow);
904 : :
905 : : bool mptcp_subflow_active(struct mptcp_subflow_context *subflow);
906 : :
907 : : void mptcp_subflow_drop_ctx(struct sock *ssk);
908 : :
909 : 36 : static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
910 : : struct mptcp_subflow_context *ctx)
911 : : {
912 : 228 : sk->sk_data_ready = sock_def_readable;
913 : 228 : sk->sk_state_change = ctx->tcp_state_change;
914 : 228 : sk->sk_write_space = sk_stream_write_space;
915 : 228 : sk->sk_error_report = ctx->tcp_error_report;
916 : :
917 [ - + ]: 228 : inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops;
918 : : }
919 : :
920 : : void __init mptcp_proto_init(void);
921 : : #if IS_ENABLED(CONFIG_MPTCP_IPV6)
922 : : int __init mptcp_proto_v6_init(void);
923 : : void __init mptcp_subflow_v6_init(void);
924 : : #endif
925 : :
926 : : struct sock *mptcp_sk_clone_init(const struct sock *sk,
927 : : const struct mptcp_options_received *mp_opt,
928 : : struct sock *ssk,
929 : : struct request_sock *req);
930 : : void mptcp_get_options(const struct sk_buff *skb,
931 : : struct mptcp_options_received *mp_opt);
932 : :
933 : : void mptcp_finish_connect(struct sock *sk);
934 : : void __mptcp_sync_state(struct sock *sk, int state);
935 : : void mptcp_reset_tout_timer(struct mptcp_sock *msk, unsigned long fail_tout);
936 : :
937 : 1443 : static inline void mptcp_stop_tout_timer(struct sock *sk)
938 : : {
939 [ + + ]: 1443 : if (!inet_csk(sk)->icsk_mtup.probe_timestamp)
940 : : return;
941 : :
942 : 4 : sk_stop_timer(sk, &inet_csk(sk)->mptcp_tout_timer);
943 : 4 : inet_csk(sk)->icsk_mtup.probe_timestamp = 0;
944 : : }
945 : :
946 : 138 : static inline void mptcp_set_close_tout(struct sock *sk, unsigned long tout)
947 : : {
948 : : /* avoid 0 timestamp, as that means no close timeout */
949 : 42 : inet_csk(sk)->icsk_mtup.probe_timestamp = tout ? : 1;
950 : : }
951 : :
952 : 1466 : static inline void mptcp_start_tout_timer(struct sock *sk)
953 : : {
954 : 1466 : mptcp_set_close_tout(sk, tcp_jiffies32);
955 [ - + ]: 1466 : mptcp_reset_tout_timer(mptcp_sk(sk), 0);
956 : 1466 : }
957 : :
958 : 4532 : static inline bool mptcp_is_fully_established(struct sock *sk)
959 : : {
960 [ + + ]: 4532 : return inet_sk_state_load(sk) == TCP_ESTABLISHED &&
961 [ - + - + ]: 3907 : READ_ONCE(mptcp_sk(sk)->fully_established);
[ - + - +
- + ]
962 : : }
963 : :
964 : 253327 : static inline u64 mptcp_stamp(void)
965 : : {
966 [ + - ]: 1270531 : return div_u64(tcp_clock_ns(), NSEC_PER_USEC);
967 : : }
968 : :
969 : : void mptcp_data_ready(struct sock *sk, struct sock *ssk);
970 : : bool mptcp_finish_join(struct sock *sk);
971 : : bool mptcp_schedule_work(struct sock *sk);
972 : : int mptcp_setsockopt(struct sock *sk, int level, int optname,
973 : : sockptr_t optval, unsigned int optlen);
974 : : int mptcp_getsockopt(struct sock *sk, int level, int optname,
975 : : char __user *optval, int __user *option);
976 : :
977 : : u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq);
978 : 244150 : static inline u64 mptcp_expand_seq(u64 old_seq, u64 cur_seq, bool use_64bit)
979 : : {
980 [ + + + + ]: 3000085 : if (use_64bit)
[ + + ]
981 : : return cur_seq;
982 : :
983 : 135036 : return __mptcp_expand_seq(old_seq, cur_seq);
984 : : }
985 : : void __mptcp_check_push(struct sock *sk, struct sock *ssk);
986 : : void __mptcp_data_acked(struct sock *sk);
987 : : void __mptcp_error_report(struct sock *sk);
988 : : bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit);
989 : 2177887 : static inline bool mptcp_data_fin_enabled(const struct mptcp_sock *msk)
990 : : {
991 [ + + ]: 4145364 : return READ_ONCE(msk->snd_data_fin_enable) &&
[ + + + + ]
992 [ + + ]: 183107 : READ_ONCE(msk->write_seq) == READ_ONCE(msk->snd_nxt);
[ + + - + ]
993 : : }
994 : :
995 : 3816802 : static inline u32 mptcp_notsent_lowat(const struct sock *sk)
996 : : {
997 [ - + ]: 3816802 : struct net *net = sock_net(sk);
998 : 3816802 : u32 val;
999 : :
1000 [ - + ]: 3816802 : val = READ_ONCE(mptcp_sk(sk)->notsent_lowat);
1001 [ + + ]: 3816802 : return val ?: READ_ONCE(net->ipv4.sysctl_tcp_notsent_lowat);
1002 : : }
1003 : :
1004 : 2816408 : static inline bool mptcp_stream_memory_free(const struct sock *sk, int wake)
1005 : : {
1006 [ - + ]: 2816408 : const struct mptcp_sock *msk = mptcp_sk(sk);
1007 : 2816408 : u32 notsent_bytes;
1008 : :
1009 : 2816408 : notsent_bytes = READ_ONCE(msk->write_seq) - READ_ONCE(msk->snd_nxt);
1010 [ - + ]: 2816408 : return (notsent_bytes << wake) < mptcp_notsent_lowat(sk);
1011 : : }
1012 : :
1013 : 969240 : static inline bool __mptcp_stream_is_writeable(const struct sock *sk, int wake)
1014 : : {
1015 [ + - + + ]: 1938480 : return mptcp_stream_memory_free(sk, wake) &&
1016 : 969240 : __sk_stream_is_writeable(sk, wake);
1017 : : }
1018 : :
1019 : 1036624 : static inline void mptcp_write_space(struct sock *sk)
1020 : : {
1021 : : /* pairs with memory barrier in mptcp_poll */
1022 : 1036624 : smp_mb();
1023 [ + + ]: 1036624 : if (mptcp_stream_memory_free(sk, 1))
1024 [ + - ]: 1036564 : INDIRECT_CALL_1(sk->sk_write_space, sk_stream_write_space, sk);
1025 : 1036624 : }
1026 : :
1027 : 32065 : static inline void __mptcp_sync_sndbuf(struct sock *sk)
1028 : : {
1029 : 32065 : struct mptcp_subflow_context *subflow;
1030 : 32065 : int ssk_sndbuf, new_sndbuf;
1031 : :
1032 [ + - ]: 32065 : if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
1033 : : return;
1034 : :
1035 [ - + ]: 32065 : new_sndbuf = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_wmem[0]);
1036 [ - + - + : 101300 : mptcp_for_each_subflow(mptcp_sk(sk), subflow) {
+ + ]
1037 : 69235 : ssk_sndbuf = READ_ONCE(mptcp_subflow_tcp_sock(subflow)->sk_sndbuf);
1038 : :
1039 : 69235 : subflow->cached_sndbuf = ssk_sndbuf;
1040 : 69235 : new_sndbuf += ssk_sndbuf;
1041 : : }
1042 : :
1043 : : /* the msk max wmem limit is <nr_subflows> * tcp wmem[2] */
1044 : 32065 : WRITE_ONCE(sk->sk_sndbuf, new_sndbuf);
1045 : 32065 : mptcp_write_space(sk);
1046 : : }
1047 : :
1048 : : /* The called held both the msk socket and the subflow socket locks,
1049 : : * possibly under BH
1050 : : */
1051 : 156 : static inline void __mptcp_propagate_sndbuf(struct sock *sk, struct sock *ssk)
1052 : : {
1053 : 3966 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1054 : :
1055 [ + + ]: 3966 : if (READ_ONCE(ssk->sk_sndbuf) != subflow->cached_sndbuf)
[ + + + - ]
1056 : 3863 : __mptcp_sync_sndbuf(sk);
1057 : : }
1058 : :
1059 : : /* the caller held only the subflow socket lock, either in process or
1060 : : * BH context. Additionally this can be called under the msk data lock,
1061 : : * so we can't acquire such lock here: let the delegate action acquires
1062 : : * the needed locks in suitable order.
1063 : : */
1064 : 824411 : static inline void mptcp_propagate_sndbuf(struct sock *sk, struct sock *ssk)
1065 : : {
1066 : 824411 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1067 : :
1068 [ + + ]: 824411 : if (likely(READ_ONCE(ssk->sk_sndbuf) == subflow->cached_sndbuf))
1069 : : return;
1070 : :
1071 : 131138 : local_bh_disable();
1072 : 131138 : mptcp_subflow_delegate(subflow, MPTCP_DELEGATE_SNDBUF);
1073 : 131138 : local_bh_enable();
1074 : : }
1075 : :
1076 : : #define MPTCP_TOKEN_MAX_RETRIES 4
1077 : :
1078 : : void __init mptcp_token_init(void);
1079 : 126 : static inline void mptcp_token_init_request(struct request_sock *req)
1080 : : {
1081 : 2600 : mptcp_subflow_rsk(req)->token_node.pprev = NULL;
1082 : : }
1083 : :
1084 : : int mptcp_token_new_request(struct request_sock *req);
1085 : : void mptcp_token_destroy_request(struct request_sock *req);
1086 : : int mptcp_token_new_connect(struct sock *ssk);
1087 : : void mptcp_token_accept(struct mptcp_subflow_request_sock *r,
1088 : : struct mptcp_sock *msk);
1089 : : bool mptcp_token_exists(u32 token);
1090 : : struct mptcp_sock *mptcp_token_get_sock(struct net *net, u32 token);
1091 : : struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
1092 : : long *s_num);
1093 : : void mptcp_token_destroy(struct mptcp_sock *msk);
1094 : :
1095 : : void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn);
1096 : :
1097 : : void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac);
1098 : : __sum16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum);
1099 : :
1100 : : void __init mptcp_pm_init(void);
1101 : : void mptcp_pm_data_init(struct mptcp_sock *msk);
1102 : : void mptcp_pm_data_reset(struct mptcp_sock *msk);
1103 : : void mptcp_pm_destroy(struct mptcp_sock *msk);
1104 : : int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
1105 : : struct mptcp_addr_info *addr);
1106 : : int mptcp_pm_parse_entry(struct nlattr *attr, struct genl_info *info,
1107 : : bool require_family,
1108 : : struct mptcp_pm_addr_entry *entry);
1109 : : bool mptcp_pm_addr_families_match(const struct sock *sk,
1110 : : const struct mptcp_addr_info *loc,
1111 : : const struct mptcp_addr_info *rem);
1112 : : void mptcp_pm_chk_stale(const struct mptcp_sock *msk);
1113 : : void mptcp_pm_new_connection(struct mptcp_sock *msk, const struct sock *ssk, int server_side);
1114 : : void mptcp_pm_fully_established(struct mptcp_sock *msk, const struct sock *ssk);
1115 : : bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk);
1116 : : void mptcp_pm_connection_closed(struct mptcp_sock *msk);
1117 : : void mptcp_pm_subflow_established(struct mptcp_sock *msk);
1118 : : bool mptcp_pm_nl_check_work_pending(struct mptcp_sock *msk);
1119 : : void mptcp_pm_subflow_check_next(struct mptcp_sock *msk,
1120 : : const struct mptcp_subflow_context *subflow);
1121 : : void mptcp_pm_add_addr_received(const struct sock *ssk,
1122 : : const struct mptcp_addr_info *addr);
1123 : : void mptcp_pm_add_addr_echoed(struct mptcp_sock *msk,
1124 : : const struct mptcp_addr_info *addr);
1125 : : void mptcp_pm_send_ack(struct mptcp_sock *msk,
1126 : : struct mptcp_subflow_context *subflow,
1127 : : bool prio, bool backup);
1128 : : void mptcp_pm_addr_send_ack(struct mptcp_sock *msk);
1129 : : void mptcp_pm_nl_rm_addr(struct mptcp_sock *msk, u8 rm_id);
1130 : : void mptcp_pm_rm_subflow(struct mptcp_sock *msk,
1131 : : const struct mptcp_rm_list *rm_list);
1132 : : void mptcp_pm_rm_addr_received(struct mptcp_sock *msk,
1133 : : const struct mptcp_rm_list *rm_list);
1134 : : void mptcp_pm_mp_prio_received(struct sock *sk, u8 bkup);
1135 : : void mptcp_pm_mp_fail_received(struct sock *sk, u64 fail_seq);
1136 : : int mptcp_pm_mp_prio_send_ack(struct mptcp_sock *msk,
1137 : : struct mptcp_addr_info *addr,
1138 : : struct mptcp_addr_info *rem,
1139 : : u8 bkup);
1140 : : bool mptcp_pm_announced_alloc(struct mptcp_sock *msk,
1141 : : const struct mptcp_addr_info *addr);
1142 : : bool mptcp_pm_announced_remove(struct mptcp_sock *msk,
1143 : : const struct mptcp_addr_info *addr);
1144 : : bool mptcp_pm_announced_has_ssk(struct mptcp_sock *msk, const struct sock *ssk);
1145 : : bool mptcp_pm_has_subflow_saddr(const struct mptcp_sock *msk,
1146 : : const struct mptcp_addr_info *saddr);
1147 : : int mptcp_pm_nl_set_flags(struct mptcp_pm_addr_entry *local,
1148 : : struct genl_info *info);
1149 : : int mptcp_userspace_pm_set_flags(struct mptcp_pm_addr_entry *local,
1150 : : struct genl_info *info);
1151 : : int mptcp_pm_announce_addr(struct mptcp_sock *msk,
1152 : : const struct mptcp_addr_info *addr,
1153 : : bool echo);
1154 : : int mptcp_pm_remove_addr(struct mptcp_sock *msk, const struct mptcp_rm_list *rm_list);
1155 : :
1156 : : /* the default path manager, used in mptcp_pm_unregister */
1157 : : extern struct mptcp_pm_ops mptcp_pm_kernel;
1158 : :
1159 : : struct mptcp_pm_ops *mptcp_pm_find(const char *name);
1160 : : int mptcp_pm_register(struct mptcp_pm_ops *pm_ops);
1161 : : void mptcp_pm_unregister(struct mptcp_pm_ops *pm_ops);
1162 : : int mptcp_pm_validate(struct mptcp_pm_ops *pm_ops);
1163 : : void mptcp_pm_get_available(char *buf, size_t maxlen);
1164 : :
1165 : : void mptcp_userspace_pm_free_local_addr_list(struct mptcp_sock *msk);
1166 : :
1167 : : void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
1168 : : const struct sock *ssk, gfp_t gfp);
1169 : : void mptcp_event_addr_announced(const struct sock *ssk, const struct mptcp_addr_info *info);
1170 : : void mptcp_event_addr_removed(const struct mptcp_sock *msk, u8 id);
1171 : : void mptcp_event_pm_listener(const struct sock *ssk,
1172 : : enum mptcp_event_type event);
1173 : : bool mptcp_userspace_pm_active(const struct mptcp_sock *msk);
1174 : :
1175 : : void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subflow,
1176 : : struct request_sock *req);
1177 : : int mptcp_pm_genl_fill_addr(struct sk_buff *msg,
1178 : : struct netlink_callback *cb,
1179 : : struct mptcp_pm_addr_entry *entry);
1180 : :
1181 : 477269 : static inline bool mptcp_pm_should_add_signal(struct mptcp_sock *msk)
1182 : : {
1183 [ + + ][ - + : 4077293 : return READ_ONCE(msk->pm.addr_signal) &
+ - + + -
- ]
1184 : : (BIT(MPTCP_ADD_ADDR_SIGNAL) | BIT(MPTCP_ADD_ADDR_ECHO));
1185 : : }
1186 : :
1187 : 0 : static inline bool mptcp_pm_should_add_signal_addr(struct mptcp_sock *msk)
1188 : : {
1189 [ + + + - ]: 149 : return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_SIGNAL);
1190 : : }
1191 : :
1192 : 6 : static inline bool mptcp_pm_should_add_signal_echo(struct mptcp_sock *msk)
1193 : : {
1194 [ + + ]: 915 : return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_ECHO);
1195 : : }
1196 : :
1197 : 477263 : static inline bool mptcp_pm_should_rm_signal(struct mptcp_sock *msk)
1198 : : {
1199 [ + + ]: 4074539 : return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_RM_ADDR_SIGNAL);
[ - + + + ]
1200 : : }
1201 : :
1202 : 42 : static inline bool mptcp_pm_is_userspace(const struct mptcp_sock *msk)
1203 : : {
1204 [ + + + + : 4447 : return READ_ONCE(msk->pm.pm_type) == MPTCP_PM_TYPE_USERSPACE;
+ + ][ + + ]
[ - + + -
- + + + -
+ ]
1205 : : }
1206 : :
1207 : 21 : static inline bool mptcp_pm_is_kernel(const struct mptcp_sock *msk)
1208 : : {
1209 [ + - - - ]: 1143 : return READ_ONCE(msk->pm.pm_type) == MPTCP_PM_TYPE_KERNEL;
[ + + ]
1210 : : }
1211 : :
1212 : : bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, int *size, int remaining,
1213 : : struct mptcp_addr_info *addr, bool *echo,
1214 : : bool *drop_ts);
1215 : : bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
1216 : : struct mptcp_rm_list *rm_list, int *len);
1217 : : int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
1218 : : bool mptcp_pm_is_backup(struct mptcp_sock *msk, struct sock_common *skc);
1219 : : int mptcp_pm_nl_dump_addr(struct sk_buff *msg,
1220 : : struct netlink_callback *cb);
1221 : : int mptcp_userspace_pm_dump_addr(struct sk_buff *msg,
1222 : : struct netlink_callback *cb);
1223 : : int mptcp_pm_nl_get_addr(u8 id, struct mptcp_pm_addr_entry *addr,
1224 : : struct genl_info *info);
1225 : : int mptcp_userspace_pm_get_addr(u8 id, struct mptcp_pm_addr_entry *addr,
1226 : : struct genl_info *info);
1227 : :
1228 : 72 : static inline u8 subflow_get_local_id(const struct mptcp_subflow_context *subflow)
1229 : : {
1230 : 1216 : int local_id = READ_ONCE(subflow->local_id);
1231 : :
1232 : 1216 : if (local_id < 0)
1233 : : return 0;
1234 : : return local_id;
1235 : : }
1236 : :
1237 : : void __init mptcp_pm_kernel_register(void);
1238 : : void __init mptcp_pm_userspace_register(void);
1239 : : void __init mptcp_pm_nl_init(void);
1240 : : void mptcp_pm_worker(struct mptcp_sock *msk);
1241 : : void __mptcp_pm_kernel_worker(struct mptcp_sock *msk);
1242 : : u8 mptcp_pm_get_endp_signal_max(const struct mptcp_sock *msk);
1243 : : u8 mptcp_pm_get_endp_subflow_max(const struct mptcp_sock *msk);
1244 : : u8 mptcp_pm_get_endp_laminar_max(const struct mptcp_sock *msk);
1245 : : u8 mptcp_pm_get_endp_fullmesh_max(const struct mptcp_sock *msk);
1246 : : u8 mptcp_pm_get_limit_add_addr_accepted(const struct mptcp_sock *msk);
1247 : : u8 mptcp_pm_get_limit_extra_subflows(const struct mptcp_sock *msk);
1248 : :
1249 : : /* called under PM lock */
1250 : 943 : static inline void __mptcp_pm_close_subflow(struct mptcp_sock *msk)
1251 : : {
1252 [ - + + - ]: 1886 : if (!WARN_ON_ONCE(msk->pm.extra_subflows == 0) &&
1253 : 943 : --msk->pm.extra_subflows < mptcp_pm_get_limit_extra_subflows(msk))
1254 : 943 : WRITE_ONCE(msk->pm.accept_subflow, true);
1255 : 943 : }
1256 : :
1257 : 83 : static inline void mptcp_pm_close_subflow(struct mptcp_sock *msk)
1258 : : {
1259 : 83 : spin_lock_bh(&msk->pm.lock);
1260 : 83 : __mptcp_pm_close_subflow(msk);
1261 : 83 : spin_unlock_bh(&msk->pm.lock);
1262 : 83 : }
1263 : :
1264 : 1296 : static inline bool mptcp_pm_add_addr_c_flag_case(struct mptcp_sock *msk)
1265 : : {
1266 [ - + ]: 1086 : return READ_ONCE(msk->pm.remote_deny_join_id0) &&
1267 [ + + + + ]: 38 : msk->pm.local_addr_used == 0 &&
[ # # ]
1268 [ + + - + ]: 1302 : mptcp_pm_get_limit_add_addr_accepted(msk) == 0 &&
1269 [ # # ]: 4 : msk->pm.extra_subflows < mptcp_pm_get_limit_extra_subflows(msk);
1270 : : }
1271 : :
1272 : : void mptcp_sockopt_sync_locked(struct mptcp_sock *msk, struct sock *ssk);
1273 : :
1274 : 0 : static inline struct mptcp_ext *mptcp_get_ext(const struct sk_buff *skb)
1275 : : {
1276 [ + - + + : 4860094 : return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP);
+ + + + ]
[ + + + - ]
[ + - + -
+ - ][ - -
- - + + +
+ + + + -
- - ][ + +
+ - + + -
- - - ]
1277 : : }
1278 : :
1279 : : void mptcp_diag_subflow_init(struct tcp_ulp_ops *ops);
1280 : :
1281 : 12382166 : static inline bool __mptcp_check_fallback(const struct mptcp_sock *msk)
1282 : : {
1283 [ - + - - : 12382166 : return test_bit(MPTCP_FALLBACK_DONE, &msk->flags);
- - ]
1284 : : }
1285 : :
1286 : 1170513 : static inline bool mptcp_check_fallback(const struct sock *sk)
1287 : : {
1288 : 1170513 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1289 [ - + ]: 1170513 : struct mptcp_sock *msk = mptcp_sk(subflow->conn);
1290 : :
1291 : 1170513 : return __mptcp_check_fallback(msk);
1292 : : }
1293 : :
1294 : 1097 : static inline bool __mptcp_has_initial_subflow(const struct mptcp_sock *msk)
1295 : : {
1296 : 1097 : struct sock *ssk = READ_ONCE(msk->first);
1297 : :
1298 [ + - + + ]: 2194 : return ssk && ((1 << inet_sk_state_load(ssk)) &
[ + - - +
+ + ]
1299 : : (TCPF_ESTABLISHED | TCPF_SYN_SENT |
1300 : : TCPF_SYN_RECV | TCPF_LISTEN));
1301 : : }
1302 : :
1303 : : bool __mptcp_try_fallback(struct mptcp_sock *msk, int fb_mib);
1304 : :
1305 : 184 : static inline bool mptcp_try_fallback(struct sock *ssk, int fb_mib)
1306 : : {
1307 : 184 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1308 : 184 : struct sock *sk = subflow->conn;
1309 : 184 : struct mptcp_sock *msk;
1310 : :
1311 [ - + ]: 184 : msk = mptcp_sk(sk);
1312 [ + + ]: 184 : if (!__mptcp_try_fallback(msk, fb_mib))
1313 : : return false;
1314 [ - + - - ]: 172 : if (READ_ONCE(msk->snd_data_fin_enable) && !(ssk->sk_shutdown & SEND_SHUTDOWN)) {
[ - + - +
- - ]
1315 : 0 : gfp_t saved_allocation = ssk->sk_allocation;
1316 : :
1317 : : /* we are in a atomic (BH) scope, override ssk default for data
1318 : : * fin allocation
1319 : : */
1320 : 0 : ssk->sk_allocation = GFP_ATOMIC;
1321 : 0 : ssk->sk_shutdown |= SEND_SHUTDOWN;
1322 : 0 : tcp_shutdown(ssk, SEND_SHUTDOWN);
1323 : 0 : ssk->sk_allocation = saved_allocation;
1324 : : }
1325 : : return true;
1326 : : }
1327 : :
1328 : 30 : static inline void mptcp_early_fallback(struct mptcp_sock *msk,
1329 : : struct mptcp_subflow_context *subflow,
1330 : : int fb_mib)
1331 : : {
1332 : 30 : subflow->request_mptcp = 0;
1333 [ - + ]: 30 : WARN_ON_ONCE(!__mptcp_try_fallback(msk, fb_mib));
1334 : 30 : }
1335 : :
1336 : 113273 : static inline bool mptcp_check_infinite_map(struct sk_buff *skb)
1337 : : {
1338 : 113273 : struct mptcp_ext *mpext;
1339 : :
1340 [ + + ]: 113273 : mpext = skb ? mptcp_get_ext(skb) : NULL;
1341 [ + + + + ]: 23499 : if (mpext && mpext->infinite_map)
1342 : 1 : return true;
1343 : :
1344 : : return false;
1345 : : }
1346 : :
1347 : 0 : static inline bool is_active_ssk(struct mptcp_subflow_context *subflow)
1348 : : {
1349 [ + - - - ]: 6 : return (subflow->request_mptcp || subflow->request_join);
1350 : : }
1351 : :
1352 : 1786 : static inline bool subflow_simultaneous_connect(struct sock *sk)
1353 : : {
1354 : 1786 : struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
1355 : :
1356 : : /* Note that the sk state implies !subflow->conn_finished. */
1357 [ + + ]: 1786 : return sk->sk_state == TCP_SYN_RECV && is_active_ssk(subflow);
1358 : : }
1359 : :
1360 : : #ifdef CONFIG_SYN_COOKIES
1361 : : void subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
1362 : : struct sk_buff *skb);
1363 : : bool mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
1364 : : struct sk_buff *skb);
1365 : : void __init mptcp_join_cookie_init(void);
1366 : : #else
1367 : : static inline void
1368 : : subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
1369 : : struct sk_buff *skb) {}
1370 : : static inline bool
1371 : : mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
1372 : : struct sk_buff *skb)
1373 : : {
1374 : : return false;
1375 : : }
1376 : :
1377 : : static inline void mptcp_join_cookie_init(void) {}
1378 : : #endif
1379 : :
1380 : : #endif /* __MPTCP_PROTOCOL_H */
|