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