Merge branch 'mptcp-Miscellaneous-fixes'
[linux-2.6-microblaze.git] / net / mptcp / protocol.h
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
14 #define MPTCP_SUPPORTED_VERSION 1
15
16 /* MPTCP option bits */
17 #define OPTION_MPTCP_MPC_SYN    BIT(0)
18 #define OPTION_MPTCP_MPC_SYNACK BIT(1)
19 #define OPTION_MPTCP_MPC_ACK    BIT(2)
20 #define OPTION_MPTCP_MPJ_SYN    BIT(3)
21 #define OPTION_MPTCP_MPJ_SYNACK BIT(4)
22 #define OPTION_MPTCP_MPJ_ACK    BIT(5)
23 #define OPTION_MPTCP_ADD_ADDR   BIT(6)
24 #define OPTION_MPTCP_ADD_ADDR6  BIT(7)
25 #define OPTION_MPTCP_RM_ADDR    BIT(8)
26 #define OPTION_MPTCP_FASTCLOSE  BIT(9)
27
28 /* MPTCP option subtypes */
29 #define MPTCPOPT_MP_CAPABLE     0
30 #define MPTCPOPT_MP_JOIN        1
31 #define MPTCPOPT_DSS            2
32 #define MPTCPOPT_ADD_ADDR       3
33 #define MPTCPOPT_RM_ADDR        4
34 #define MPTCPOPT_MP_PRIO        5
35 #define MPTCPOPT_MP_FAIL        6
36 #define MPTCPOPT_MP_FASTCLOSE   7
37
38 /* MPTCP suboption lengths */
39 #define TCPOLEN_MPTCP_MPC_SYN           4
40 #define TCPOLEN_MPTCP_MPC_SYNACK        12
41 #define TCPOLEN_MPTCP_MPC_ACK           20
42 #define TCPOLEN_MPTCP_MPC_ACK_DATA      22
43 #define TCPOLEN_MPTCP_MPJ_SYN           12
44 #define TCPOLEN_MPTCP_MPJ_SYNACK        16
45 #define TCPOLEN_MPTCP_MPJ_ACK           24
46 #define TCPOLEN_MPTCP_DSS_BASE          4
47 #define TCPOLEN_MPTCP_DSS_ACK32         4
48 #define TCPOLEN_MPTCP_DSS_ACK64         8
49 #define TCPOLEN_MPTCP_DSS_MAP32         10
50 #define TCPOLEN_MPTCP_DSS_MAP64         14
51 #define TCPOLEN_MPTCP_DSS_CHECKSUM      2
52 #define TCPOLEN_MPTCP_ADD_ADDR          16
53 #define TCPOLEN_MPTCP_ADD_ADDR_PORT     20
54 #define TCPOLEN_MPTCP_ADD_ADDR_BASE     8
55 #define TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT        12
56 #define TCPOLEN_MPTCP_ADD_ADDR6         28
57 #define TCPOLEN_MPTCP_ADD_ADDR6_PORT    32
58 #define TCPOLEN_MPTCP_ADD_ADDR6_BASE    20
59 #define TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT       24
60 #define TCPOLEN_MPTCP_PORT_LEN          4
61 #define TCPOLEN_MPTCP_RM_ADDR_BASE      4
62 #define TCPOLEN_MPTCP_FASTCLOSE         12
63
64 /* MPTCP MP_JOIN flags */
65 #define MPTCPOPT_BACKUP         BIT(0)
66 #define MPTCPOPT_HMAC_LEN       20
67 #define MPTCPOPT_THMAC_LEN      8
68
69 /* MPTCP MP_CAPABLE flags */
70 #define MPTCP_VERSION_MASK      (0x0F)
71 #define MPTCP_CAP_CHECKSUM_REQD BIT(7)
72 #define MPTCP_CAP_EXTENSIBILITY BIT(6)
73 #define MPTCP_CAP_HMAC_SHA256   BIT(0)
74 #define MPTCP_CAP_FLAG_MASK     (0x3F)
75
76 /* MPTCP DSS flags */
77 #define MPTCP_DSS_DATA_FIN      BIT(4)
78 #define MPTCP_DSS_DSN64         BIT(3)
79 #define MPTCP_DSS_HAS_MAP       BIT(2)
80 #define MPTCP_DSS_ACK64         BIT(1)
81 #define MPTCP_DSS_HAS_ACK       BIT(0)
82 #define MPTCP_DSS_FLAG_MASK     (0x1F)
83
84 /* MPTCP ADD_ADDR flags */
85 #define MPTCP_ADDR_ECHO         BIT(0)
86 #define MPTCP_ADDR_IPVERSION_4  4
87 #define MPTCP_ADDR_IPVERSION_6  6
88
89 /* MPTCP socket flags */
90 #define MPTCP_DATA_READY        0
91 #define MPTCP_NOSPACE           1
92 #define MPTCP_WORK_RTX          2
93 #define MPTCP_WORK_EOF          3
94 #define MPTCP_FALLBACK_DONE     4
95 #define MPTCP_WORK_CLOSE_SUBFLOW 5
96 #define MPTCP_PUSH_PENDING      6
97 #define MPTCP_CLEAN_UNA         7
98 #define MPTCP_ERROR_REPORT      8
99
100 static inline bool before64(__u64 seq1, __u64 seq2)
101 {
102         return (__s64)(seq1 - seq2) < 0;
103 }
104
105 #define after64(seq2, seq1)     before64(seq1, seq2)
106
107 struct mptcp_options_received {
108         u64     sndr_key;
109         u64     rcvr_key;
110         u64     data_ack;
111         u64     data_seq;
112         u32     subflow_seq;
113         u16     data_len;
114         u16     mp_capable : 1,
115                 mp_join : 1,
116                 fastclose : 1,
117                 dss : 1,
118                 add_addr : 1,
119                 rm_addr : 1,
120                 family : 4,
121                 echo : 1,
122                 backup : 1;
123         u32     token;
124         u32     nonce;
125         u64     thmac;
126         u8      hmac[MPTCPOPT_HMAC_LEN];
127         u8      join_id;
128         u8      use_map:1,
129                 dsn64:1,
130                 data_fin:1,
131                 use_ack:1,
132                 ack64:1,
133                 mpc_map:1,
134                 __unused:2;
135         u8      addr_id;
136         u8      rm_id;
137         union {
138                 struct in_addr  addr;
139 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
140                 struct in6_addr addr6;
141 #endif
142         };
143         u64     ahmac;
144         u16     port;
145 };
146
147 static inline __be32 mptcp_option(u8 subopt, u8 len, u8 nib, u8 field)
148 {
149         return htonl((TCPOPT_MPTCP << 24) | (len << 16) | (subopt << 12) |
150                      ((nib & 0xF) << 8) | field);
151 }
152
153 struct mptcp_addr_info {
154         sa_family_t             family;
155         __be16                  port;
156         u8                      id;
157         u8                      flags;
158         int                     ifindex;
159         union {
160                 struct in_addr addr;
161 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
162                 struct in6_addr addr6;
163 #endif
164         };
165 };
166
167 enum mptcp_pm_status {
168         MPTCP_PM_ADD_ADDR_RECEIVED,
169         MPTCP_PM_ADD_ADDR_SEND_ACK,
170         MPTCP_PM_RM_ADDR_RECEIVED,
171         MPTCP_PM_ESTABLISHED,
172         MPTCP_PM_ALREADY_ESTABLISHED,   /* persistent status, set after ESTABLISHED event */
173         MPTCP_PM_SUBFLOW_ESTABLISHED,
174 };
175
176 enum mptcp_addr_signal_status {
177         MPTCP_ADD_ADDR_SIGNAL,
178         MPTCP_ADD_ADDR_ECHO,
179         MPTCP_ADD_ADDR_IPV6,
180         MPTCP_ADD_ADDR_PORT,
181         MPTCP_RM_ADDR_SIGNAL,
182 };
183
184 struct mptcp_pm_data {
185         struct mptcp_addr_info local;
186         struct mptcp_addr_info remote;
187         struct list_head anno_list;
188
189         spinlock_t      lock;           /*protects the whole PM data */
190
191         u8              addr_signal;
192         bool            server_side;
193         bool            work_pending;
194         bool            accept_addr;
195         bool            accept_subflow;
196         u8              add_addr_signaled;
197         u8              add_addr_accepted;
198         u8              local_addr_used;
199         u8              subflows;
200         u8              add_addr_signal_max;
201         u8              add_addr_accept_max;
202         u8              local_addr_max;
203         u8              subflows_max;
204         u8              status;
205         u8              rm_id;
206 };
207
208 struct mptcp_data_frag {
209         struct list_head list;
210         u64 data_seq;
211         u16 data_len;
212         u16 offset;
213         u16 overhead;
214         u16 already_sent;
215         struct page *page;
216 };
217
218 /* MPTCP connection sock */
219 struct mptcp_sock {
220         /* inet_connection_sock must be the first member */
221         struct inet_connection_sock sk;
222         u64             local_key;
223         u64             remote_key;
224         u64             write_seq;
225         u64             snd_nxt;
226         u64             ack_seq;
227         u64             rcv_wnd_sent;
228         u64             rcv_data_fin_seq;
229         int             wmem_reserved;
230         struct sock     *last_snd;
231         int             snd_burst;
232         int             old_wspace;
233         u64             snd_una;
234         u64             wnd_end;
235         unsigned long   timer_ival;
236         u32             token;
237         int             rmem_released;
238         unsigned long   flags;
239         bool            can_ack;
240         bool            fully_established;
241         bool            rcv_data_fin;
242         bool            snd_data_fin_enable;
243         bool            rcv_fastclose;
244         bool            use_64bit_ack; /* Set when we received a 64-bit DSN */
245         spinlock_t      join_list_lock;
246         struct sock     *ack_hint;
247         struct work_struct work;
248         struct sk_buff  *ooo_last_skb;
249         struct rb_root  out_of_order_queue;
250         struct sk_buff_head receive_queue;
251         struct sk_buff_head skb_tx_cache;       /* this is wmem accounted */
252         int             tx_pending_data;
253         int             size_goal_cache;
254         struct list_head conn_list;
255         struct list_head rtx_queue;
256         struct mptcp_data_frag *first_pending;
257         struct list_head join_list;
258         struct socket   *subflow; /* outgoing connect/listener/!mp_capable */
259         struct sock     *first;
260         struct mptcp_pm_data    pm;
261         struct {
262                 u32     space;  /* bytes copied in last measurement window */
263                 u32     copied; /* bytes copied in this measurement window */
264                 u64     time;   /* start time of measurement window */
265                 u64     rtt_us; /* last maximum rtt of subflows */
266         } rcvq_space;
267 };
268
269 #define mptcp_lock_sock(___sk, cb) do {                                 \
270         struct sock *__sk = (___sk); /* silence macro reuse warning */  \
271         might_sleep();                                                  \
272         spin_lock_bh(&__sk->sk_lock.slock);                             \
273         if (__sk->sk_lock.owned)                                        \
274                 __lock_sock(__sk);                                      \
275         cb;                                                             \
276         __sk->sk_lock.owned = 1;                                        \
277         spin_unlock(&__sk->sk_lock.slock);                              \
278         mutex_acquire(&__sk->sk_lock.dep_map, 0, 0, _RET_IP_);          \
279         local_bh_enable();                                              \
280 } while (0)
281
282 #define mptcp_data_lock(sk) spin_lock_bh(&(sk)->sk_lock.slock)
283 #define mptcp_data_unlock(sk) spin_unlock_bh(&(sk)->sk_lock.slock)
284
285 #define mptcp_for_each_subflow(__msk, __subflow)                        \
286         list_for_each_entry(__subflow, &((__msk)->conn_list), node)
287
288 static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
289 {
290         return (struct mptcp_sock *)sk;
291 }
292
293 static inline int __mptcp_space(const struct sock *sk)
294 {
295         return tcp_space(sk) + READ_ONCE(mptcp_sk(sk)->rmem_released);
296 }
297
298 static inline struct mptcp_data_frag *mptcp_send_head(const struct sock *sk)
299 {
300         const struct mptcp_sock *msk = mptcp_sk(sk);
301
302         return READ_ONCE(msk->first_pending);
303 }
304
305 static inline struct mptcp_data_frag *mptcp_send_next(struct sock *sk)
306 {
307         struct mptcp_sock *msk = mptcp_sk(sk);
308         struct mptcp_data_frag *cur;
309
310         cur = msk->first_pending;
311         return list_is_last(&cur->list, &msk->rtx_queue) ? NULL :
312                                                      list_next_entry(cur, list);
313 }
314
315 static inline struct mptcp_data_frag *mptcp_pending_tail(const struct sock *sk)
316 {
317         struct mptcp_sock *msk = mptcp_sk(sk);
318
319         if (!msk->first_pending)
320                 return NULL;
321
322         if (WARN_ON_ONCE(list_empty(&msk->rtx_queue)))
323                 return NULL;
324
325         return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list);
326 }
327
328 static inline struct mptcp_data_frag *mptcp_rtx_head(const struct sock *sk)
329 {
330         struct mptcp_sock *msk = mptcp_sk(sk);
331
332         if (msk->snd_una == READ_ONCE(msk->snd_nxt))
333                 return NULL;
334
335         return list_first_entry_or_null(&msk->rtx_queue, struct mptcp_data_frag, list);
336 }
337
338 struct mptcp_subflow_request_sock {
339         struct  tcp_request_sock sk;
340         u16     mp_capable : 1,
341                 mp_join : 1,
342                 backup : 1;
343         u8      local_id;
344         u8      remote_id;
345         u64     local_key;
346         u64     idsn;
347         u32     token;
348         u32     ssn_offset;
349         u64     thmac;
350         u32     local_nonce;
351         u32     remote_nonce;
352         struct mptcp_sock       *msk;
353         struct hlist_nulls_node token_node;
354 };
355
356 static inline struct mptcp_subflow_request_sock *
357 mptcp_subflow_rsk(const struct request_sock *rsk)
358 {
359         return (struct mptcp_subflow_request_sock *)rsk;
360 }
361
362 enum mptcp_data_avail {
363         MPTCP_SUBFLOW_NODATA,
364         MPTCP_SUBFLOW_DATA_AVAIL,
365         MPTCP_SUBFLOW_OOO_DATA
366 };
367
368 /* MPTCP subflow context */
369 struct mptcp_subflow_context {
370         struct  list_head node;/* conn_list of subflows */
371         u64     local_key;
372         u64     remote_key;
373         u64     idsn;
374         u64     map_seq;
375         u32     snd_isn;
376         u32     token;
377         u32     rel_write_seq;
378         u32     map_subflow_seq;
379         u32     ssn_offset;
380         u32     map_data_len;
381         u32     request_mptcp : 1,  /* send MP_CAPABLE */
382                 request_join : 1,   /* send MP_JOIN */
383                 request_bkup : 1,
384                 mp_capable : 1,     /* remote is MPTCP capable */
385                 mp_join : 1,        /* remote is JOINing */
386                 fully_established : 1,      /* path validated */
387                 pm_notified : 1,    /* PM hook called for established status */
388                 conn_finished : 1,
389                 map_valid : 1,
390                 mpc_map : 1,
391                 backup : 1,
392                 rx_eof : 1,
393                 can_ack : 1,        /* only after processing the remote a key */
394                 disposable : 1;     /* ctx can be free at ulp release time */
395         enum mptcp_data_avail data_avail;
396         u32     remote_nonce;
397         u64     thmac;
398         u32     local_nonce;
399         u32     remote_token;
400         u8      hmac[MPTCPOPT_HMAC_LEN];
401         u8      local_id;
402         u8      remote_id;
403
404         struct  sock *tcp_sock;     /* tcp sk backpointer */
405         struct  sock *conn;         /* parent mptcp_sock */
406         const   struct inet_connection_sock_af_ops *icsk_af_ops;
407         void    (*tcp_data_ready)(struct sock *sk);
408         void    (*tcp_state_change)(struct sock *sk);
409         void    (*tcp_write_space)(struct sock *sk);
410         void    (*tcp_error_report)(struct sock *sk);
411
412         struct  rcu_head rcu;
413 };
414
415 static inline struct mptcp_subflow_context *
416 mptcp_subflow_ctx(const struct sock *sk)
417 {
418         struct inet_connection_sock *icsk = inet_csk(sk);
419
420         /* Use RCU on icsk_ulp_data only for sock diag code */
421         return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;
422 }
423
424 static inline struct sock *
425 mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
426 {
427         return subflow->tcp_sock;
428 }
429
430 static inline u64
431 mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow)
432 {
433         return tcp_sk(mptcp_subflow_tcp_sock(subflow))->copied_seq -
434                       subflow->ssn_offset -
435                       subflow->map_subflow_seq;
436 }
437
438 static inline u64
439 mptcp_subflow_get_mapped_dsn(const struct mptcp_subflow_context *subflow)
440 {
441         return subflow->map_seq + mptcp_subflow_get_map_offset(subflow);
442 }
443
444 static inline void mptcp_add_pending_subflow(struct mptcp_sock *msk,
445                                              struct mptcp_subflow_context *subflow)
446 {
447         sock_hold(mptcp_subflow_tcp_sock(subflow));
448         spin_lock_bh(&msk->join_list_lock);
449         list_add_tail(&subflow->node, &msk->join_list);
450         spin_unlock_bh(&msk->join_list_lock);
451 }
452
453 int mptcp_is_enabled(struct net *net);
454 unsigned int mptcp_get_add_addr_timeout(struct net *net);
455 void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
456                                      struct mptcp_options_received *mp_opt);
457 bool mptcp_subflow_data_available(struct sock *sk);
458 void __init mptcp_subflow_init(void);
459 void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how);
460 void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
461                        struct mptcp_subflow_context *subflow);
462 void mptcp_subflow_reset(struct sock *ssk);
463
464 /* called with sk socket lock held */
465 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
466                             const struct mptcp_addr_info *remote);
467 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);
468
469 static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
470                                               struct mptcp_subflow_context *ctx)
471 {
472         sk->sk_data_ready = ctx->tcp_data_ready;
473         sk->sk_state_change = ctx->tcp_state_change;
474         sk->sk_write_space = ctx->tcp_write_space;
475         sk->sk_error_report = ctx->tcp_error_report;
476
477         inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops;
478 }
479
480 void __init mptcp_proto_init(void);
481 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
482 int __init mptcp_proto_v6_init(void);
483 #endif
484
485 struct sock *mptcp_sk_clone(const struct sock *sk,
486                             const struct mptcp_options_received *mp_opt,
487                             struct request_sock *req);
488 void mptcp_get_options(const struct sk_buff *skb,
489                        struct mptcp_options_received *mp_opt);
490
491 void mptcp_finish_connect(struct sock *sk);
492 static inline bool mptcp_is_fully_established(struct sock *sk)
493 {
494         return inet_sk_state_load(sk) == TCP_ESTABLISHED &&
495                READ_ONCE(mptcp_sk(sk)->fully_established);
496 }
497 void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk);
498 void mptcp_data_ready(struct sock *sk, struct sock *ssk);
499 bool mptcp_finish_join(struct sock *sk);
500 bool mptcp_schedule_work(struct sock *sk);
501 void __mptcp_check_push(struct sock *sk, struct sock *ssk);
502 void __mptcp_data_acked(struct sock *sk);
503 void __mptcp_error_report(struct sock *sk);
504 void mptcp_subflow_eof(struct sock *sk);
505 bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit);
506 void __mptcp_flush_join_list(struct mptcp_sock *msk);
507 static inline bool mptcp_data_fin_enabled(const struct mptcp_sock *msk)
508 {
509         return READ_ONCE(msk->snd_data_fin_enable) &&
510                READ_ONCE(msk->write_seq) == READ_ONCE(msk->snd_nxt);
511 }
512
513 void mptcp_destroy_common(struct mptcp_sock *msk);
514
515 void __init mptcp_token_init(void);
516 static inline void mptcp_token_init_request(struct request_sock *req)
517 {
518         mptcp_subflow_rsk(req)->token_node.pprev = NULL;
519 }
520
521 int mptcp_token_new_request(struct request_sock *req);
522 void mptcp_token_destroy_request(struct request_sock *req);
523 int mptcp_token_new_connect(struct sock *sk);
524 void mptcp_token_accept(struct mptcp_subflow_request_sock *r,
525                         struct mptcp_sock *msk);
526 bool mptcp_token_exists(u32 token);
527 struct mptcp_sock *mptcp_token_get_sock(u32 token);
528 struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
529                                          long *s_num);
530 void mptcp_token_destroy(struct mptcp_sock *msk);
531
532 void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn);
533
534 void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac);
535
536 void __init mptcp_pm_init(void);
537 void mptcp_pm_data_init(struct mptcp_sock *msk);
538 void mptcp_pm_new_connection(struct mptcp_sock *msk, int server_side);
539 void mptcp_pm_fully_established(struct mptcp_sock *msk);
540 bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk);
541 void mptcp_pm_connection_closed(struct mptcp_sock *msk);
542 void mptcp_pm_subflow_established(struct mptcp_sock *msk,
543                                   struct mptcp_subflow_context *subflow);
544 void mptcp_pm_subflow_closed(struct mptcp_sock *msk, u8 id);
545 void mptcp_pm_add_addr_received(struct mptcp_sock *msk,
546                                 const struct mptcp_addr_info *addr);
547 void mptcp_pm_add_addr_send_ack(struct mptcp_sock *msk);
548 void mptcp_pm_rm_addr_received(struct mptcp_sock *msk, u8 rm_id);
549 void mptcp_pm_free_anno_list(struct mptcp_sock *msk);
550 struct mptcp_pm_add_entry *
551 mptcp_pm_del_add_timer(struct mptcp_sock *msk,
552                        struct mptcp_addr_info *addr);
553
554 int mptcp_pm_announce_addr(struct mptcp_sock *msk,
555                            const struct mptcp_addr_info *addr,
556                            bool echo, bool port);
557 int mptcp_pm_remove_addr(struct mptcp_sock *msk, u8 local_id);
558 int mptcp_pm_remove_subflow(struct mptcp_sock *msk, u8 local_id);
559
560 static inline bool mptcp_pm_should_add_signal(struct mptcp_sock *msk)
561 {
562         return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_SIGNAL);
563 }
564
565 static inline bool mptcp_pm_should_add_signal_echo(struct mptcp_sock *msk)
566 {
567         return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_ECHO);
568 }
569
570 static inline bool mptcp_pm_should_add_signal_ipv6(struct mptcp_sock *msk)
571 {
572         return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_IPV6);
573 }
574
575 static inline bool mptcp_pm_should_add_signal_port(struct mptcp_sock *msk)
576 {
577         return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_PORT);
578 }
579
580 static inline bool mptcp_pm_should_rm_signal(struct mptcp_sock *msk)
581 {
582         return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_RM_ADDR_SIGNAL);
583 }
584
585 static inline unsigned int mptcp_add_addr_len(int family, bool echo, bool port)
586 {
587         u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
588
589         if (family == AF_INET6)
590                 len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
591         if (!echo)
592                 len += MPTCPOPT_THMAC_LEN;
593         if (port)
594                 len += TCPOLEN_MPTCP_PORT_LEN;
595
596         return len;
597 }
598
599 bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
600                               struct mptcp_addr_info *saddr, bool *echo, bool *port);
601 bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
602                              u8 *rm_id);
603 int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
604
605 void __init mptcp_pm_nl_init(void);
606 void mptcp_pm_nl_data_init(struct mptcp_sock *msk);
607 void mptcp_pm_nl_fully_established(struct mptcp_sock *msk);
608 void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk);
609 void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk);
610 void mptcp_pm_nl_add_addr_send_ack(struct mptcp_sock *msk);
611 void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk);
612 void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk, u8 rm_id);
613 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
614
615 static inline struct mptcp_ext *mptcp_get_ext(struct sk_buff *skb)
616 {
617         return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP);
618 }
619
620 void mptcp_diag_subflow_init(struct tcp_ulp_ops *ops);
621
622 static inline bool __mptcp_check_fallback(const struct mptcp_sock *msk)
623 {
624         return test_bit(MPTCP_FALLBACK_DONE, &msk->flags);
625 }
626
627 static inline bool mptcp_check_fallback(const struct sock *sk)
628 {
629         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
630         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
631
632         return __mptcp_check_fallback(msk);
633 }
634
635 static inline void __mptcp_do_fallback(struct mptcp_sock *msk)
636 {
637         if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags)) {
638                 pr_debug("TCP fallback already done (msk=%p)", msk);
639                 return;
640         }
641         set_bit(MPTCP_FALLBACK_DONE, &msk->flags);
642 }
643
644 static inline void mptcp_do_fallback(struct sock *sk)
645 {
646         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
647         struct mptcp_sock *msk = mptcp_sk(subflow->conn);
648
649         __mptcp_do_fallback(msk);
650 }
651
652 #define pr_fallback(a) pr_debug("%s:fallback to TCP (msk=%p)", __func__, a)
653
654 static inline bool subflow_simultaneous_connect(struct sock *sk)
655 {
656         struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
657         struct sock *parent = subflow->conn;
658
659         return sk->sk_state == TCP_ESTABLISHED &&
660                !mptcp_sk(parent)->pm.server_side &&
661                !subflow->conn_finished;
662 }
663
664 #ifdef CONFIG_SYN_COOKIES
665 void subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
666                                        struct sk_buff *skb);
667 bool mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
668                                         struct sk_buff *skb);
669 void __init mptcp_join_cookie_init(void);
670 #else
671 static inline void
672 subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
673                                   struct sk_buff *skb) {}
674 static inline bool
675 mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
676                                    struct sk_buff *skb)
677 {
678         return false;
679 }
680
681 static inline void mptcp_join_cookie_init(void) {}
682 #endif
683
684 #endif /* __MPTCP_PROTOCOL_H */