linux/net/mptcp/protocol.h
<<
>>
Prefs
   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
  15#define MPTCP_SUPPORTED_VERSION 1
  16
  17/* MPTCP option bits */
  18#define OPTION_MPTCP_MPC_SYN    BIT(0)
  19#define OPTION_MPTCP_MPC_SYNACK BIT(1)
  20#define OPTION_MPTCP_MPC_ACK    BIT(2)
  21#define OPTION_MPTCP_MPJ_SYN    BIT(3)
  22#define OPTION_MPTCP_MPJ_SYNACK BIT(4)
  23#define OPTION_MPTCP_MPJ_ACK    BIT(5)
  24#define OPTION_MPTCP_ADD_ADDR   BIT(6)
  25#define OPTION_MPTCP_RM_ADDR    BIT(7)
  26#define OPTION_MPTCP_FASTCLOSE  BIT(8)
  27#define OPTION_MPTCP_PRIO       BIT(9)
  28#define OPTION_MPTCP_RST        BIT(10)
  29
  30/* MPTCP option subtypes */
  31#define MPTCPOPT_MP_CAPABLE     0
  32#define MPTCPOPT_MP_JOIN        1
  33#define MPTCPOPT_DSS            2
  34#define MPTCPOPT_ADD_ADDR       3
  35#define MPTCPOPT_RM_ADDR        4
  36#define MPTCPOPT_MP_PRIO        5
  37#define MPTCPOPT_MP_FAIL        6
  38#define MPTCPOPT_MP_FASTCLOSE   7
  39#define MPTCPOPT_RST            8
  40
  41/* MPTCP suboption lengths */
  42#define TCPOLEN_MPTCP_MPC_SYN           4
  43#define TCPOLEN_MPTCP_MPC_SYNACK        12
  44#define TCPOLEN_MPTCP_MPC_ACK           20
  45#define TCPOLEN_MPTCP_MPC_ACK_DATA      22
  46#define TCPOLEN_MPTCP_MPJ_SYN           12
  47#define TCPOLEN_MPTCP_MPJ_SYNACK        16
  48#define TCPOLEN_MPTCP_MPJ_ACK           24
  49#define TCPOLEN_MPTCP_DSS_BASE          4
  50#define TCPOLEN_MPTCP_DSS_ACK32         4
  51#define TCPOLEN_MPTCP_DSS_ACK64         8
  52#define TCPOLEN_MPTCP_DSS_MAP32         10
  53#define TCPOLEN_MPTCP_DSS_MAP64         14
  54#define TCPOLEN_MPTCP_DSS_CHECKSUM      2
  55#define TCPOLEN_MPTCP_ADD_ADDR          16
  56#define TCPOLEN_MPTCP_ADD_ADDR_PORT     18
  57#define TCPOLEN_MPTCP_ADD_ADDR_BASE     8
  58#define TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT        10
  59#define TCPOLEN_MPTCP_ADD_ADDR6         28
  60#define TCPOLEN_MPTCP_ADD_ADDR6_PORT    30
  61#define TCPOLEN_MPTCP_ADD_ADDR6_BASE    20
  62#define TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT       22
  63#define TCPOLEN_MPTCP_PORT_LEN          2
  64#define TCPOLEN_MPTCP_PORT_ALIGN        2
  65#define TCPOLEN_MPTCP_RM_ADDR_BASE      3
  66#define TCPOLEN_MPTCP_PRIO              3
  67#define TCPOLEN_MPTCP_PRIO_ALIGN        4
  68#define TCPOLEN_MPTCP_FASTCLOSE         12
  69#define TCPOLEN_MPTCP_RST               4
  70
  71/* MPTCP MP_JOIN flags */
  72#define MPTCPOPT_BACKUP         BIT(0)
  73#define MPTCPOPT_HMAC_LEN       20
  74#define MPTCPOPT_THMAC_LEN      8
  75
  76/* MPTCP MP_CAPABLE flags */
  77#define MPTCP_VERSION_MASK      (0x0F)
  78#define MPTCP_CAP_CHECKSUM_REQD BIT(7)
  79#define MPTCP_CAP_EXTENSIBILITY BIT(6)
  80#define MPTCP_CAP_HMAC_SHA256   BIT(0)
  81#define MPTCP_CAP_FLAG_MASK     (0x3F)
  82
  83/* MPTCP DSS flags */
  84#define MPTCP_DSS_DATA_FIN      BIT(4)
  85#define MPTCP_DSS_DSN64         BIT(3)
  86#define MPTCP_DSS_HAS_MAP       BIT(2)
  87#define MPTCP_DSS_ACK64         BIT(1)
  88#define MPTCP_DSS_HAS_ACK       BIT(0)
  89#define MPTCP_DSS_FLAG_MASK     (0x1F)
  90
  91/* MPTCP ADD_ADDR flags */
  92#define MPTCP_ADDR_ECHO         BIT(0)
  93
  94/* MPTCP MP_PRIO flags */
  95#define MPTCP_PRIO_BKUP         BIT(0)
  96
  97/* MPTCP TCPRST flags */
  98#define MPTCP_RST_TRANSIENT     BIT(0)
  99
 100/* MPTCP socket flags */
 101#define MPTCP_DATA_READY        0
 102#define MPTCP_NOSPACE           1
 103#define MPTCP_WORK_RTX          2
 104#define MPTCP_WORK_EOF          3
 105#define MPTCP_FALLBACK_DONE     4
 106#define MPTCP_WORK_CLOSE_SUBFLOW 5
 107#define MPTCP_PUSH_PENDING      6
 108#define MPTCP_CLEAN_UNA         7
 109#define MPTCP_ERROR_REPORT      8
 110#define MPTCP_RETRANSMIT        9
 111#define MPTCP_WORK_SYNC_SETSOCKOPT 10
 112#define MPTCP_CONNECTED         11
 113
 114static inline bool before64(__u64 seq1, __u64 seq2)
 115{
 116        return (__s64)(seq1 - seq2) < 0;
 117}
 118
 119#define after64(seq2, seq1)     before64(seq1, seq2)
 120
 121struct mptcp_options_received {
 122        u64     sndr_key;
 123        u64     rcvr_key;
 124        u64     data_ack;
 125        u64     data_seq;
 126        u32     subflow_seq;
 127        u16     data_len;
 128        u16     mp_capable : 1,
 129                mp_join : 1,
 130                fastclose : 1,
 131                reset : 1,
 132                dss : 1,
 133                add_addr : 1,
 134                rm_addr : 1,
 135                mp_prio : 1,
 136                echo : 1,
 137                backup : 1;
 138        u32     token;
 139        u32     nonce;
 140        u64     thmac;
 141        u8      hmac[MPTCPOPT_HMAC_LEN];
 142        u8      join_id;
 143        u8      use_map:1,
 144                dsn64:1,
 145                data_fin:1,
 146                use_ack:1,
 147                ack64:1,
 148                mpc_map:1,
 149                __unused:2;
 150        struct mptcp_addr_info addr;
 151        struct mptcp_rm_list rm_list;
 152        u64     ahmac;
 153        u8      reset_reason:4;
 154        u8      reset_transient:1;
 155};
 156
 157static inline __be32 mptcp_option(u8 subopt, u8 len, u8 nib, u8 field)
 158{
 159        return htonl((TCPOPT_MPTCP << 24) | (len << 16) | (subopt << 12) |
 160                     ((nib & 0xF) << 8) | field);
 161}
 162
 163enum mptcp_pm_status {
 164        MPTCP_PM_ADD_ADDR_RECEIVED,
 165        MPTCP_PM_ADD_ADDR_SEND_ACK,
 166        MPTCP_PM_RM_ADDR_RECEIVED,
 167        MPTCP_PM_ESTABLISHED,
 168        MPTCP_PM_ALREADY_ESTABLISHED,   /* persistent status, set after ESTABLISHED event */
 169        MPTCP_PM_SUBFLOW_ESTABLISHED,
 170};
 171
 172enum mptcp_addr_signal_status {
 173        MPTCP_ADD_ADDR_SIGNAL,
 174        MPTCP_ADD_ADDR_ECHO,
 175        MPTCP_ADD_ADDR_IPV6,
 176        MPTCP_ADD_ADDR_PORT,
 177        MPTCP_RM_ADDR_SIGNAL,
 178};
 179
 180struct mptcp_pm_data {
 181        struct mptcp_addr_info local;
 182        struct mptcp_addr_info remote;
 183        struct list_head anno_list;
 184
 185        spinlock_t      lock;           /*protects the whole PM data */
 186
 187        u8              addr_signal;
 188        bool            server_side;
 189        bool            work_pending;
 190        bool            accept_addr;
 191        bool            accept_subflow;
 192        u8              add_addr_signaled;
 193        u8              add_addr_accepted;
 194        u8              local_addr_used;
 195        u8              subflows;
 196        u8              status;
 197        struct mptcp_rm_list rm_list_tx;
 198        struct mptcp_rm_list rm_list_rx;
 199};
 200
 201struct mptcp_data_frag {
 202        struct list_head list;
 203        u64 data_seq;
 204        u16 data_len;
 205        u16 offset;
 206        u16 overhead;
 207        u16 already_sent;
 208        struct page *page;
 209};
 210
 211/* MPTCP connection sock */
 212struct mptcp_sock {
 213        /* inet_connection_sock must be the first member */
 214        struct inet_connection_sock sk;
 215        u64             local_key;
 216        u64             remote_key;
 217        u64             write_seq;
 218        u64             snd_nxt;
 219        u64             ack_seq;
 220        u64             rcv_wnd_sent;
 221        u64             rcv_data_fin_seq;
 222        int             wmem_reserved;
 223        struct sock     *last_snd;
 224        int             snd_burst;
 225        int             old_wspace;
 226        u64             recovery_snd_nxt;       /* in recovery mode accept up to this seq;
 227                                                 * recovery related fields are under data_lock
 228                                                 * protection
 229                                                 */
 230        u64             snd_una;
 231        u64             wnd_end;
 232        unsigned long   timer_ival;
 233        u32             token;
 234        int             rmem_released;
 235        unsigned long   flags;
 236        bool            recovery;               /* closing subflow write queue reinjected */
 237        bool            can_ack;
 238        bool            fully_established;
 239        bool            rcv_data_fin;
 240        bool            snd_data_fin_enable;
 241        bool            rcv_fastclose;
 242        bool            use_64bit_ack; /* Set when we received a 64-bit DSN */
 243        spinlock_t      join_list_lock;
 244        struct work_struct work;
 245        struct sk_buff  *ooo_last_skb;
 246        struct rb_root  out_of_order_queue;
 247        struct sk_buff_head receive_queue;
 248        struct list_head conn_list;
 249        struct list_head rtx_queue;
 250        struct mptcp_data_frag *first_pending;
 251        struct list_head join_list;
 252        struct socket   *subflow; /* outgoing connect/listener/!mp_capable */
 253        struct sock     *first;
 254        struct mptcp_pm_data    pm;
 255        struct {
 256                u32     space;  /* bytes copied in last measurement window */
 257                u32     copied; /* bytes copied in this measurement window */
 258                u64     time;   /* start time of measurement window */
 259                u64     rtt_us; /* last maximum rtt of subflows */
 260        } rcvq_space;
 261
 262        u32 setsockopt_seq;
 263        char            ca_name[TCP_CA_NAME_MAX];
 264};
 265
 266#define mptcp_lock_sock(___sk, cb) do {                                 \
 267        struct sock *__sk = (___sk); /* silence macro reuse warning */  \
 268        might_sleep();                                                  \
 269        spin_lock_bh(&__sk->sk_lock.slock);                             \
 270        if (__sk->sk_lock.owned)                                        \
 271                __lock_sock(__sk);                                      \
 272        cb;                                                             \
 273        __sk->sk_lock.owned = 1;                                        \
 274        spin_unlock(&__sk->sk_lock.slock);                              \
 275        mutex_acquire(&__sk->sk_lock.dep_map, 0, 0, _RET_IP_);          \
 276        local_bh_enable();                                              \
 277} while (0)
 278
 279#define mptcp_data_lock(sk) spin_lock_bh(&(sk)->sk_lock.slock)
 280#define mptcp_data_unlock(sk) spin_unlock_bh(&(sk)->sk_lock.slock)
 281
 282#define mptcp_for_each_subflow(__msk, __subflow)                        \
 283        list_for_each_entry(__subflow, &((__msk)->conn_list), node)
 284
 285static inline void msk_owned_by_me(const struct mptcp_sock *msk)
 286{
 287        sock_owned_by_me((const struct sock *)msk);
 288}
 289
 290static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
 291{
 292        return (struct mptcp_sock *)sk;
 293}
 294
 295/* the msk socket don't use the backlog, also account for the bulk
 296 * free memory
 297 */
 298static inline int __mptcp_rmem(const struct sock *sk)
 299{
 300        return atomic_read(&sk->sk_rmem_alloc) - READ_ONCE(mptcp_sk(sk)->rmem_released);
 301}
 302
 303static inline int __mptcp_space(const struct sock *sk)
 304{
 305        return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf) - __mptcp_rmem(sk));
 306}
 307
 308static inline struct mptcp_data_frag *mptcp_send_head(const struct sock *sk)
 309{
 310        const struct mptcp_sock *msk = mptcp_sk(sk);
 311
 312        return READ_ONCE(msk->first_pending);
 313}
 314
 315static inline struct mptcp_data_frag *mptcp_send_next(struct sock *sk)
 316{
 317        struct mptcp_sock *msk = mptcp_sk(sk);
 318        struct mptcp_data_frag *cur;
 319
 320        cur = msk->first_pending;
 321        return list_is_last(&cur->list, &msk->rtx_queue) ? NULL :
 322                                                     list_next_entry(cur, list);
 323}
 324
 325static inline struct mptcp_data_frag *mptcp_pending_tail(const struct sock *sk)
 326{
 327        struct mptcp_sock *msk = mptcp_sk(sk);
 328
 329        if (!msk->first_pending)
 330                return NULL;
 331
 332        if (WARN_ON_ONCE(list_empty(&msk->rtx_queue)))
 333                return NULL;
 334
 335        return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list);
 336}
 337
 338static inline struct mptcp_data_frag *mptcp_rtx_head(const struct sock *sk)
 339{
 340        struct mptcp_sock *msk = mptcp_sk(sk);
 341
 342        if (msk->snd_una == READ_ONCE(msk->snd_nxt))
 343                return NULL;
 344
 345        return list_first_entry_or_null(&msk->rtx_queue, struct mptcp_data_frag, list);
 346}
 347
 348struct mptcp_subflow_request_sock {
 349        struct  tcp_request_sock sk;
 350        u16     mp_capable : 1,
 351                mp_join : 1,
 352                backup : 1;
 353        u8      local_id;
 354        u8      remote_id;
 355        u64     local_key;
 356        u64     idsn;
 357        u32     token;
 358        u32     ssn_offset;
 359        u64     thmac;
 360        u32     local_nonce;
 361        u32     remote_nonce;
 362        struct mptcp_sock       *msk;
 363        struct hlist_nulls_node token_node;
 364};
 365
 366static inline struct mptcp_subflow_request_sock *
 367mptcp_subflow_rsk(const struct request_sock *rsk)
 368{
 369        return (struct mptcp_subflow_request_sock *)rsk;
 370}
 371
 372enum mptcp_data_avail {
 373        MPTCP_SUBFLOW_NODATA,
 374        MPTCP_SUBFLOW_DATA_AVAIL,
 375};
 376
 377struct mptcp_delegated_action {
 378        struct napi_struct napi;
 379        struct list_head head;
 380};
 381
 382DECLARE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
 383
 384#define MPTCP_DELEGATE_SEND             0
 385
 386/* MPTCP subflow context */
 387struct mptcp_subflow_context {
 388        struct  list_head node;/* conn_list of subflows */
 389        u64     local_key;
 390        u64     remote_key;
 391        u64     idsn;
 392        u64     map_seq;
 393        u32     snd_isn;
 394        u32     token;
 395        u32     rel_write_seq;
 396        u32     map_subflow_seq;
 397        u32     ssn_offset;
 398        u32     map_data_len;
 399        u32     request_mptcp : 1,  /* send MP_CAPABLE */
 400                request_join : 1,   /* send MP_JOIN */
 401                request_bkup : 1,
 402                mp_capable : 1,     /* remote is MPTCP capable */
 403                mp_join : 1,        /* remote is JOINing */
 404                fully_established : 1,      /* path validated */
 405                pm_notified : 1,    /* PM hook called for established status */
 406                conn_finished : 1,
 407                map_valid : 1,
 408                mpc_map : 1,
 409                backup : 1,
 410                send_mp_prio : 1,
 411                rx_eof : 1,
 412                can_ack : 1,        /* only after processing the remote a key */
 413                disposable : 1,     /* ctx can be free at ulp release time */
 414                stale : 1;          /* unable to snd/rcv data, do not use for xmit */
 415        enum mptcp_data_avail data_avail;
 416        u32     remote_nonce;
 417        u64     thmac;
 418        u32     local_nonce;
 419        u32     remote_token;
 420        u8      hmac[MPTCPOPT_HMAC_LEN];
 421        u8      local_id;
 422        u8      remote_id;
 423        u8      reset_seen:1;
 424        u8      reset_transient:1;
 425        u8      reset_reason:4;
 426        u8      stale_count;
 427
 428        long    delegated_status;
 429        struct  list_head delegated_node;   /* link into delegated_action, protected by local BH */
 430
 431        u32     setsockopt_seq;
 432        u32     stale_rcv_tstamp;
 433
 434        struct  sock *tcp_sock;     /* tcp sk backpointer */
 435        struct  sock *conn;         /* parent mptcp_sock */
 436        const   struct inet_connection_sock_af_ops *icsk_af_ops;
 437        void    (*tcp_data_ready)(struct sock *sk);
 438        void    (*tcp_state_change)(struct sock *sk);
 439        void    (*tcp_write_space)(struct sock *sk);
 440        void    (*tcp_error_report)(struct sock *sk);
 441
 442        struct  rcu_head rcu;
 443};
 444
 445static inline struct mptcp_subflow_context *
 446mptcp_subflow_ctx(const struct sock *sk)
 447{
 448        struct inet_connection_sock *icsk = inet_csk(sk);
 449
 450        /* Use RCU on icsk_ulp_data only for sock diag code */
 451        return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;
 452}
 453
 454static inline struct sock *
 455mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
 456{
 457        return subflow->tcp_sock;
 458}
 459
 460static inline u64
 461mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow)
 462{
 463        return tcp_sk(mptcp_subflow_tcp_sock(subflow))->copied_seq -
 464                      subflow->ssn_offset -
 465                      subflow->map_subflow_seq;
 466}
 467
 468static inline u64
 469mptcp_subflow_get_mapped_dsn(const struct mptcp_subflow_context *subflow)
 470{
 471        return subflow->map_seq + mptcp_subflow_get_map_offset(subflow);
 472}
 473
 474static inline void mptcp_add_pending_subflow(struct mptcp_sock *msk,
 475                                             struct mptcp_subflow_context *subflow)
 476{
 477        sock_hold(mptcp_subflow_tcp_sock(subflow));
 478        spin_lock_bh(&msk->join_list_lock);
 479        list_add_tail(&subflow->node, &msk->join_list);
 480        spin_unlock_bh(&msk->join_list_lock);
 481}
 482
 483void mptcp_subflow_process_delegated(struct sock *ssk);
 484
 485static inline void mptcp_subflow_delegate(struct mptcp_subflow_context *subflow)
 486{
 487        struct mptcp_delegated_action *delegated;
 488        bool schedule;
 489
 490        /* The implied barrier pairs with mptcp_subflow_delegated_done(), and
 491         * ensures the below list check sees list updates done prior to status
 492         * bit changes
 493         */
 494        if (!test_and_set_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status)) {
 495                /* still on delegated list from previous scheduling */
 496                if (!list_empty(&subflow->delegated_node))
 497                        return;
 498
 499                /* the caller held the subflow bh socket lock */
 500                lockdep_assert_in_softirq();
 501
 502                delegated = this_cpu_ptr(&mptcp_delegated_actions);
 503                schedule = list_empty(&delegated->head);
 504                list_add_tail(&subflow->delegated_node, &delegated->head);
 505                sock_hold(mptcp_subflow_tcp_sock(subflow));
 506                if (schedule)
 507                        napi_schedule(&delegated->napi);
 508        }
 509}
 510
 511static inline struct mptcp_subflow_context *
 512mptcp_subflow_delegated_next(struct mptcp_delegated_action *delegated)
 513{
 514        struct mptcp_subflow_context *ret;
 515
 516        if (list_empty(&delegated->head))
 517                return NULL;
 518
 519        ret = list_first_entry(&delegated->head, struct mptcp_subflow_context, delegated_node);
 520        list_del_init(&ret->delegated_node);
 521        return ret;
 522}
 523
 524static inline bool mptcp_subflow_has_delegated_action(const struct mptcp_subflow_context *subflow)
 525{
 526        return test_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status);
 527}
 528
 529static inline void mptcp_subflow_delegated_done(struct mptcp_subflow_context *subflow)
 530{
 531        /* pairs with mptcp_subflow_delegate, ensures delegate_node is updated before
 532         * touching the status bit
 533         */
 534        smp_wmb();
 535        clear_bit(MPTCP_DELEGATE_SEND, &subflow->delegated_status);
 536}
 537
 538int mptcp_is_enabled(const struct net *net);
 539unsigned int mptcp_get_add_addr_timeout(const struct net *net);
 540unsigned int mptcp_stale_loss_cnt(const struct net *net);
 541void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
 542                                     struct mptcp_options_received *mp_opt);
 543bool __mptcp_retransmit_pending_data(struct sock *sk);
 544void __mptcp_push_pending(struct sock *sk, unsigned int flags);
 545bool mptcp_subflow_data_available(struct sock *sk);
 546void __init mptcp_subflow_init(void);
 547void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how);
 548void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
 549                     struct mptcp_subflow_context *subflow);
 550void mptcp_subflow_send_ack(struct sock *ssk);
 551void mptcp_subflow_reset(struct sock *ssk);
 552void mptcp_sock_graft(struct sock *sk, struct socket *parent);
 553struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk);
 554
 555/* called with sk socket lock held */
 556int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
 557                            const struct mptcp_addr_info *remote,
 558                            u8 flags, int ifindex);
 559int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);
 560void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
 561                         struct sockaddr_storage *addr,
 562                         unsigned short family);
 563
 564static inline bool __mptcp_subflow_active(struct mptcp_subflow_context *subflow)
 565{
 566        struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
 567
 568        /* can't send if JOIN hasn't completed yet (i.e. is usable for mptcp) */
 569        if (subflow->request_join && !subflow->fully_established)
 570                return false;
 571
 572        /* only send if our side has not closed yet */
 573        return ((1 << ssk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT));
 574}
 575
 576void mptcp_subflow_set_active(struct mptcp_subflow_context *subflow);
 577
 578bool mptcp_subflow_active(struct mptcp_subflow_context *subflow);
 579
 580static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
 581                                              struct mptcp_subflow_context *ctx)
 582{
 583        sk->sk_data_ready = ctx->tcp_data_ready;
 584        sk->sk_state_change = ctx->tcp_state_change;
 585        sk->sk_write_space = ctx->tcp_write_space;
 586        sk->sk_error_report = ctx->tcp_error_report;
 587
 588        inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops;
 589}
 590
 591void __init mptcp_proto_init(void);
 592#if IS_ENABLED(CONFIG_MPTCP_IPV6)
 593int __init mptcp_proto_v6_init(void);
 594#endif
 595
 596struct sock *mptcp_sk_clone(const struct sock *sk,
 597                            const struct mptcp_options_received *mp_opt,
 598                            struct request_sock *req);
 599void mptcp_get_options(const struct sock *sk,
 600                       const struct sk_buff *skb,
 601                       struct mptcp_options_received *mp_opt);
 602
 603void mptcp_finish_connect(struct sock *sk);
 604void __mptcp_set_connected(struct sock *sk);
 605static inline bool mptcp_is_fully_established(struct sock *sk)
 606{
 607        return inet_sk_state_load(sk) == TCP_ESTABLISHED &&
 608               READ_ONCE(mptcp_sk(sk)->fully_established);
 609}
 610void mptcp_rcv_space_init(struct mptcp_sock *msk, const struct sock *ssk);
 611void mptcp_data_ready(struct sock *sk, struct sock *ssk);
 612bool mptcp_finish_join(struct sock *sk);
 613bool mptcp_schedule_work(struct sock *sk);
 614int mptcp_setsockopt(struct sock *sk, int level, int optname,
 615                     char __user *optval, unsigned int optlen);
 616int mptcp_getsockopt(struct sock *sk, int level, int optname,
 617                     char __user *optval, int __user *option);
 618
 619u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq);
 620static inline u64 mptcp_expand_seq(u64 old_seq, u64 cur_seq, bool use_64bit)
 621{
 622        if (use_64bit)
 623                return cur_seq;
 624
 625        return __mptcp_expand_seq(old_seq, cur_seq);
 626}
 627void __mptcp_check_push(struct sock *sk, struct sock *ssk);
 628void __mptcp_data_acked(struct sock *sk);
 629void __mptcp_error_report(struct sock *sk);
 630void mptcp_subflow_eof(struct sock *sk);
 631bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit);
 632void __mptcp_flush_join_list(struct mptcp_sock *msk);
 633static inline bool mptcp_data_fin_enabled(const struct mptcp_sock *msk)
 634{
 635        return READ_ONCE(msk->snd_data_fin_enable) &&
 636               READ_ONCE(msk->write_seq) == READ_ONCE(msk->snd_nxt);
 637}
 638
 639static inline bool mptcp_propagate_sndbuf(struct sock *sk, struct sock *ssk)
 640{
 641        if ((sk->sk_userlocks & SOCK_SNDBUF_LOCK) || ssk->sk_sndbuf <= READ_ONCE(sk->sk_sndbuf))
 642                return false;
 643
 644        WRITE_ONCE(sk->sk_sndbuf, ssk->sk_sndbuf);
 645        return true;
 646}
 647
 648static inline void mptcp_write_space(struct sock *sk)
 649{
 650        if (sk_stream_is_writeable(sk)) {
 651                /* pairs with memory barrier in mptcp_poll */
 652                smp_mb();
 653                if (test_and_clear_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags))
 654                        sk_stream_write_space(sk);
 655        }
 656}
 657
 658void mptcp_destroy_common(struct mptcp_sock *msk);
 659
 660#define MPTCP_TOKEN_MAX_RETRIES 4
 661
 662void __init mptcp_token_init(void);
 663static inline void mptcp_token_init_request(struct request_sock *req)
 664{
 665        mptcp_subflow_rsk(req)->token_node.pprev = NULL;
 666}
 667
 668int mptcp_token_new_request(struct request_sock *req);
 669void mptcp_token_destroy_request(struct request_sock *req);
 670int mptcp_token_new_connect(struct sock *sk);
 671void mptcp_token_accept(struct mptcp_subflow_request_sock *r,
 672                        struct mptcp_sock *msk);
 673bool mptcp_token_exists(u32 token);
 674struct mptcp_sock *mptcp_token_get_sock(struct net *net, u32 token);
 675struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
 676                                         long *s_num);
 677void mptcp_token_destroy(struct mptcp_sock *msk);
 678
 679void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn);
 680
 681void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac);
 682
 683void __init mptcp_pm_init(void);
 684void mptcp_pm_data_init(struct mptcp_sock *msk);
 685void mptcp_pm_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk);
 686void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk);
 687void mptcp_pm_new_connection(struct mptcp_sock *msk, const struct sock *ssk, int server_side);
 688void mptcp_pm_fully_established(struct mptcp_sock *msk, const struct sock *ssk, gfp_t gfp);
 689bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk);
 690void mptcp_pm_connection_closed(struct mptcp_sock *msk);
 691void mptcp_pm_subflow_established(struct mptcp_sock *msk);
 692void mptcp_pm_subflow_closed(struct mptcp_sock *msk, u8 id);
 693void mptcp_pm_add_addr_received(struct mptcp_sock *msk,
 694                                const struct mptcp_addr_info *addr);
 695void mptcp_pm_add_addr_echoed(struct mptcp_sock *msk,
 696                              struct mptcp_addr_info *addr);
 697void mptcp_pm_add_addr_send_ack(struct mptcp_sock *msk);
 698void mptcp_pm_nl_addr_send_ack(struct mptcp_sock *msk);
 699void mptcp_pm_rm_addr_received(struct mptcp_sock *msk,
 700                               const struct mptcp_rm_list *rm_list);
 701void mptcp_pm_mp_prio_received(struct sock *sk, u8 bkup);
 702void mptcp_pm_free_anno_list(struct mptcp_sock *msk);
 703bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk);
 704struct mptcp_pm_add_entry *
 705mptcp_pm_del_add_timer(struct mptcp_sock *msk,
 706                       struct mptcp_addr_info *addr, bool check_id);
 707struct mptcp_pm_add_entry *
 708mptcp_lookup_anno_list_by_saddr(struct mptcp_sock *msk,
 709                                struct mptcp_addr_info *addr);
 710
 711int mptcp_pm_announce_addr(struct mptcp_sock *msk,
 712                           const struct mptcp_addr_info *addr,
 713                           bool echo);
 714int mptcp_pm_remove_addr(struct mptcp_sock *msk, const struct mptcp_rm_list *rm_list);
 715int mptcp_pm_remove_subflow(struct mptcp_sock *msk, const struct mptcp_rm_list *rm_list);
 716
 717void mptcp_event(enum mptcp_event_type type, const struct mptcp_sock *msk,
 718                 const struct sock *ssk, gfp_t gfp);
 719void mptcp_event_addr_announced(const struct mptcp_sock *msk, const struct mptcp_addr_info *info);
 720void mptcp_event_addr_removed(const struct mptcp_sock *msk, u8 id);
 721
 722static inline bool mptcp_pm_should_add_signal(struct mptcp_sock *msk)
 723{
 724        return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_SIGNAL);
 725}
 726
 727static inline bool mptcp_pm_should_add_signal_echo(struct mptcp_sock *msk)
 728{
 729        return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_ECHO);
 730}
 731
 732static inline bool mptcp_pm_should_add_signal_ipv6(struct mptcp_sock *msk)
 733{
 734        return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_IPV6);
 735}
 736
 737static inline bool mptcp_pm_should_add_signal_port(struct mptcp_sock *msk)
 738{
 739        return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_PORT);
 740}
 741
 742static inline bool mptcp_pm_should_rm_signal(struct mptcp_sock *msk)
 743{
 744        return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_RM_ADDR_SIGNAL);
 745}
 746
 747static inline unsigned int mptcp_add_addr_len(int family, bool echo, bool port)
 748{
 749        u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
 750
 751        if (family == AF_INET6)
 752                len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
 753        if (!echo)
 754                len += MPTCPOPT_THMAC_LEN;
 755        /* account for 2 trailing 'nop' options */
 756        if (port)
 757                len += TCPOLEN_MPTCP_PORT_LEN + TCPOLEN_MPTCP_PORT_ALIGN;
 758
 759        return len;
 760}
 761
 762static inline int mptcp_rm_addr_len(const struct mptcp_rm_list *rm_list)
 763{
 764        if (rm_list->nr == 0 || rm_list->nr > MPTCP_RM_IDS_MAX)
 765                return -EINVAL;
 766
 767        return TCPOLEN_MPTCP_RM_ADDR_BASE + roundup(rm_list->nr - 1, 4) + 1;
 768}
 769
 770bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
 771                              struct mptcp_addr_info *saddr, bool *echo, bool *port);
 772bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
 773                             struct mptcp_rm_list *rm_list);
 774int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
 775
 776void __init mptcp_pm_nl_init(void);
 777void mptcp_pm_nl_data_init(struct mptcp_sock *msk);
 778void mptcp_pm_nl_work(struct mptcp_sock *msk);
 779void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
 780                                     const struct mptcp_rm_list *rm_list);
 781int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
 782unsigned int mptcp_pm_get_add_addr_signal_max(struct mptcp_sock *msk);
 783unsigned int mptcp_pm_get_add_addr_accept_max(struct mptcp_sock *msk);
 784unsigned int mptcp_pm_get_subflows_max(struct mptcp_sock *msk);
 785unsigned int mptcp_pm_get_local_addr_max(struct mptcp_sock *msk);
 786
 787void mptcp_sockopt_sync(struct mptcp_sock *msk, struct sock *ssk);
 788void mptcp_sockopt_sync_all(struct mptcp_sock *msk);
 789
 790static inline struct mptcp_ext *mptcp_get_ext(const struct sk_buff *skb)
 791{
 792        return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP);
 793}
 794
 795void mptcp_diag_subflow_init(struct tcp_ulp_ops *ops);
 796
 797static inline bool __mptcp_check_fallback(const struct mptcp_sock *msk)
 798{
 799        return test_bit(MPTCP_FALLBACK_DONE, &msk->flags);
 800}
 801
 802static inline bool mptcp_check_fallback(const struct sock *sk)
 803{
 804        struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 805        struct mptcp_sock *msk = mptcp_sk(subflow->conn);
 806
 807        return __mptcp_check_fallback(msk);
 808}
 809
 810static inline void __mptcp_do_fallback(struct mptcp_sock *msk)
 811{
 812        if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags)) {
 813                pr_debug("TCP fallback already done (msk=%p)", msk);
 814                return;
 815        }
 816        set_bit(MPTCP_FALLBACK_DONE, &msk->flags);
 817}
 818
 819static inline void mptcp_do_fallback(struct sock *sk)
 820{
 821        struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 822        struct mptcp_sock *msk = mptcp_sk(subflow->conn);
 823
 824        __mptcp_do_fallback(msk);
 825}
 826
 827#define pr_fallback(a) pr_debug("%s:fallback to TCP (msk=%p)", __func__, a)
 828
 829static inline bool subflow_simultaneous_connect(struct sock *sk)
 830{
 831        struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
 832        struct sock *parent = subflow->conn;
 833
 834        return sk->sk_state == TCP_ESTABLISHED &&
 835               !mptcp_sk(parent)->pm.server_side &&
 836               !subflow->conn_finished;
 837}
 838
 839#ifdef CONFIG_SYN_COOKIES
 840void subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
 841                                       struct sk_buff *skb);
 842bool mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
 843                                        struct sk_buff *skb);
 844void __init mptcp_join_cookie_init(void);
 845#else
 846static inline void
 847subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
 848                                  struct sk_buff *skb) {}
 849static inline bool
 850mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
 851                                   struct sk_buff *skb)
 852{
 853        return false;
 854}
 855
 856static inline void mptcp_join_cookie_init(void) {}
 857#endif
 858
 859#endif /* __MPTCP_PROTOCOL_H */
 860