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