1 /* SPDX-License-Identifier: GPL-2.0 */
4 * Copyright (c) 2017 - 2019, Intel Corporation.
7 #ifndef __MPTCP_PROTOCOL_H
8 #define __MPTCP_PROTOCOL_H
10 #include <linux/random.h>
12 #include <net/inet_connection_sock.h>
14 #define MPTCP_SUPPORTED_VERSION 1
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)
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
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
64 /* MPTCP MP_JOIN flags */
65 #define MPTCPOPT_BACKUP BIT(0)
66 #define MPTCPOPT_HMAC_LEN 20
67 #define MPTCPOPT_THMAC_LEN 8
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)
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)
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
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
100 static inline bool before64(__u64 seq1, __u64 seq2)
102 return (__s64)(seq1 - seq2) < 0;
105 #define after64(seq2, seq1) before64(seq1, seq2)
107 struct mptcp_options_received {
126 u8 hmac[MPTCPOPT_HMAC_LEN];
139 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
140 struct in6_addr addr6;
147 static inline __be32 mptcp_option(u8 subopt, u8 len, u8 nib, u8 field)
149 return htonl((TCPOPT_MPTCP << 24) | (len << 16) | (subopt << 12) |
150 ((nib & 0xF) << 8) | field);
153 struct mptcp_addr_info {
161 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
162 struct in6_addr addr6;
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,
176 enum mptcp_addr_signal_status {
177 MPTCP_ADD_ADDR_SIGNAL,
181 MPTCP_RM_ADDR_SIGNAL,
184 struct mptcp_pm_data {
185 struct mptcp_addr_info local;
186 struct mptcp_addr_info remote;
187 struct list_head anno_list;
189 spinlock_t lock; /*protects the whole PM data */
196 u8 add_addr_signaled;
197 u8 add_addr_accepted;
200 u8 add_addr_signal_max;
201 u8 add_addr_accept_max;
208 struct mptcp_data_frag {
209 struct list_head list;
218 /* MPTCP connection sock */
220 /* inet_connection_sock must be the first member */
221 struct inet_connection_sock sk;
228 u64 rcv_data_fin_seq;
230 struct sock *last_snd;
235 unsigned long timer_ival;
240 bool fully_established;
242 bool snd_data_fin_enable;
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 */
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 */
260 struct mptcp_pm_data pm;
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 */
269 #define mptcp_lock_sock(___sk, cb) do { \
270 struct sock *__sk = (___sk); /* silence macro reuse warning */ \
272 spin_lock_bh(&__sk->sk_lock.slock); \
273 if (__sk->sk_lock.owned) \
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_); \
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)
285 #define mptcp_for_each_subflow(__msk, __subflow) \
286 list_for_each_entry(__subflow, &((__msk)->conn_list), node)
288 static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
290 return (struct mptcp_sock *)sk;
293 static inline int __mptcp_space(const struct sock *sk)
295 return tcp_space(sk) + READ_ONCE(mptcp_sk(sk)->rmem_released);
298 static inline struct mptcp_data_frag *mptcp_send_head(const struct sock *sk)
300 const struct mptcp_sock *msk = mptcp_sk(sk);
302 return READ_ONCE(msk->first_pending);
305 static inline struct mptcp_data_frag *mptcp_send_next(struct sock *sk)
307 struct mptcp_sock *msk = mptcp_sk(sk);
308 struct mptcp_data_frag *cur;
310 cur = msk->first_pending;
311 return list_is_last(&cur->list, &msk->rtx_queue) ? NULL :
312 list_next_entry(cur, list);
315 static inline struct mptcp_data_frag *mptcp_pending_tail(const struct sock *sk)
317 struct mptcp_sock *msk = mptcp_sk(sk);
319 if (!msk->first_pending)
322 if (WARN_ON_ONCE(list_empty(&msk->rtx_queue)))
325 return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list);
328 static inline struct mptcp_data_frag *mptcp_rtx_head(const struct sock *sk)
330 struct mptcp_sock *msk = mptcp_sk(sk);
332 if (msk->snd_una == READ_ONCE(msk->snd_nxt))
335 return list_first_entry_or_null(&msk->rtx_queue, struct mptcp_data_frag, list);
338 struct mptcp_subflow_request_sock {
339 struct tcp_request_sock sk;
352 struct mptcp_sock *msk;
353 struct hlist_nulls_node token_node;
356 static inline struct mptcp_subflow_request_sock *
357 mptcp_subflow_rsk(const struct request_sock *rsk)
359 return (struct mptcp_subflow_request_sock *)rsk;
362 enum mptcp_data_avail {
363 MPTCP_SUBFLOW_NODATA,
364 MPTCP_SUBFLOW_DATA_AVAIL,
365 MPTCP_SUBFLOW_OOO_DATA
368 /* MPTCP subflow context */
369 struct mptcp_subflow_context {
370 struct list_head node;/* conn_list of subflows */
381 u32 request_mptcp : 1, /* send MP_CAPABLE */
382 request_join : 1, /* send MP_JOIN */
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 */
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;
400 u8 hmac[MPTCPOPT_HMAC_LEN];
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);
415 static inline struct mptcp_subflow_context *
416 mptcp_subflow_ctx(const struct sock *sk)
418 struct inet_connection_sock *icsk = inet_csk(sk);
420 /* Use RCU on icsk_ulp_data only for sock diag code */
421 return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;
424 static inline struct sock *
425 mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
427 return subflow->tcp_sock;
431 mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow)
433 return tcp_sk(mptcp_subflow_tcp_sock(subflow))->copied_seq -
434 subflow->ssn_offset -
435 subflow->map_subflow_seq;
439 mptcp_subflow_get_mapped_dsn(const struct mptcp_subflow_context *subflow)
441 return subflow->map_seq + mptcp_subflow_get_map_offset(subflow);
444 static inline void mptcp_add_pending_subflow(struct mptcp_sock *msk,
445 struct mptcp_subflow_context *subflow)
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);
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);
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);
469 static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
470 struct mptcp_subflow_context *ctx)
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;
477 inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops;
480 void __init mptcp_proto_init(void);
481 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
482 int __init mptcp_proto_v6_init(void);
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);
491 void mptcp_finish_connect(struct sock *sk);
492 static inline bool mptcp_is_fully_established(struct sock *sk)
494 return inet_sk_state_load(sk) == TCP_ESTABLISHED &&
495 READ_ONCE(mptcp_sk(sk)->fully_established);
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)
509 return READ_ONCE(msk->snd_data_fin_enable) &&
510 READ_ONCE(msk->write_seq) == READ_ONCE(msk->snd_nxt);
513 void mptcp_destroy_common(struct mptcp_sock *msk);
515 void __init mptcp_token_init(void);
516 static inline void mptcp_token_init_request(struct request_sock *req)
518 mptcp_subflow_rsk(req)->token_node.pprev = NULL;
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,
530 void mptcp_token_destroy(struct mptcp_sock *msk);
532 void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn);
534 void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac);
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);
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);
560 static inline bool mptcp_pm_should_add_signal(struct mptcp_sock *msk)
562 return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_SIGNAL);
565 static inline bool mptcp_pm_should_add_signal_echo(struct mptcp_sock *msk)
567 return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_ECHO);
570 static inline bool mptcp_pm_should_add_signal_ipv6(struct mptcp_sock *msk)
572 return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_IPV6);
575 static inline bool mptcp_pm_should_add_signal_port(struct mptcp_sock *msk)
577 return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_ADD_ADDR_PORT);
580 static inline bool mptcp_pm_should_rm_signal(struct mptcp_sock *msk)
582 return READ_ONCE(msk->pm.addr_signal) & BIT(MPTCP_RM_ADDR_SIGNAL);
585 static inline unsigned int mptcp_add_addr_len(int family, bool echo, bool port)
587 u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
589 if (family == AF_INET6)
590 len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
592 len += MPTCPOPT_THMAC_LEN;
594 len += TCPOLEN_MPTCP_PORT_LEN;
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,
603 int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);
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);
615 static inline struct mptcp_ext *mptcp_get_ext(struct sk_buff *skb)
617 return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP);
620 void mptcp_diag_subflow_init(struct tcp_ulp_ops *ops);
622 static inline bool __mptcp_check_fallback(const struct mptcp_sock *msk)
624 return test_bit(MPTCP_FALLBACK_DONE, &msk->flags);
627 static inline bool mptcp_check_fallback(const struct sock *sk)
629 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
630 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
632 return __mptcp_check_fallback(msk);
635 static inline void __mptcp_do_fallback(struct mptcp_sock *msk)
637 if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags)) {
638 pr_debug("TCP fallback already done (msk=%p)", msk);
641 set_bit(MPTCP_FALLBACK_DONE, &msk->flags);
644 static inline void mptcp_do_fallback(struct sock *sk)
646 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
647 struct mptcp_sock *msk = mptcp_sk(subflow->conn);
649 __mptcp_do_fallback(msk);
652 #define pr_fallback(a) pr_debug("%s:fallback to TCP (msk=%p)", __func__, a)
654 static inline bool subflow_simultaneous_connect(struct sock *sk)
656 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
657 struct sock *parent = subflow->conn;
659 return sk->sk_state == TCP_ESTABLISHED &&
660 !mptcp_sk(parent)->pm.server_side &&
661 !subflow->conn_finished;
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);
672 subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
673 struct sk_buff *skb) {}
675 mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_req,
681 static inline void mptcp_join_cookie_init(void) {}
684 #endif /* __MPTCP_PROTOCOL_H */