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