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 94 struct mptcp_options_received { 95 u64 sndr_key; 96 u64 rcvr_key; 97 u64 data_ack; 98 u64 data_seq; 99 u32 subflow_seq; 100 u16 data_len; 101 u16 mp_capable : 1, 102 mp_join : 1, 103 dss : 1, 104 add_addr : 1, 105 rm_addr : 1, 106 family : 4, 107 echo : 1, 108 backup : 1; 109 u32 token; 110 u32 nonce; 111 u64 thmac; 112 u8 hmac[20]; 113 u8 join_id; 114 u8 use_map:1, 115 dsn64:1, 116 data_fin:1, 117 use_ack:1, 118 ack64:1, 119 mpc_map:1, 120 __unused:2; 121 u8 addr_id; 122 u8 rm_id; 123 union { 124 struct in_addr addr; 125 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 126 struct in6_addr addr6; 127 #endif 128 }; 129 u64 ahmac; 130 u16 port; 131 }; 132 133 static inline __be32 mptcp_option(u8 subopt, u8 len, u8 nib, u8 field) 134 { 135 return htonl((TCPOPT_MPTCP << 24) | (len << 16) | (subopt << 12) | 136 ((nib & 0xF) << 8) | field); 137 } 138 139 struct mptcp_addr_info { 140 sa_family_t family; 141 __be16 port; 142 u8 id; 143 union { 144 struct in_addr addr; 145 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 146 struct in6_addr addr6; 147 #endif 148 }; 149 }; 150 151 enum mptcp_pm_status { 152 MPTCP_PM_ADD_ADDR_RECEIVED, 153 MPTCP_PM_ESTABLISHED, 154 MPTCP_PM_SUBFLOW_ESTABLISHED, 155 }; 156 157 struct mptcp_pm_data { 158 struct mptcp_addr_info local; 159 struct mptcp_addr_info remote; 160 161 spinlock_t lock; /*protects the whole PM data */ 162 163 bool addr_signal; 164 bool server_side; 165 bool work_pending; 166 bool accept_addr; 167 bool accept_subflow; 168 u8 add_addr_signaled; 169 u8 add_addr_accepted; 170 u8 local_addr_used; 171 u8 subflows; 172 u8 add_addr_signal_max; 173 u8 add_addr_accept_max; 174 u8 local_addr_max; 175 u8 subflows_max; 176 u8 status; 177 178 struct work_struct work; 179 }; 180 181 struct mptcp_data_frag { 182 struct list_head list; 183 u64 data_seq; 184 int data_len; 185 int offset; 186 int overhead; 187 struct page *page; 188 }; 189 190 /* MPTCP connection sock */ 191 struct mptcp_sock { 192 /* inet_connection_sock must be the first member */ 193 struct inet_connection_sock sk; 194 u64 local_key; 195 u64 remote_key; 196 u64 write_seq; 197 u64 ack_seq; 198 atomic64_t snd_una; 199 unsigned long timer_ival; 200 u32 token; 201 unsigned long flags; 202 bool can_ack; 203 spinlock_t join_list_lock; 204 struct work_struct work; 205 struct list_head conn_list; 206 struct list_head rtx_queue; 207 struct list_head join_list; 208 struct skb_ext *cached_ext; /* for the next sendmsg */ 209 struct socket *subflow; /* outgoing connect/listener/!mp_capable */ 210 struct sock *first; 211 struct mptcp_pm_data pm; 212 }; 213 214 #define mptcp_for_each_subflow(__msk, __subflow) \ 215 list_for_each_entry(__subflow, &((__msk)->conn_list), node) 216 217 static inline struct mptcp_sock *mptcp_sk(const struct sock *sk) 218 { 219 return (struct mptcp_sock *)sk; 220 } 221 222 static inline struct mptcp_data_frag *mptcp_rtx_tail(const struct sock *sk) 223 { 224 struct mptcp_sock *msk = mptcp_sk(sk); 225 226 if (list_empty(&msk->rtx_queue)) 227 return NULL; 228 229 return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list); 230 } 231 232 static inline struct mptcp_data_frag *mptcp_rtx_head(const struct sock *sk) 233 { 234 struct mptcp_sock *msk = mptcp_sk(sk); 235 236 return list_first_entry_or_null(&msk->rtx_queue, struct mptcp_data_frag, list); 237 } 238 239 struct mptcp_subflow_request_sock { 240 struct tcp_request_sock sk; 241 u16 mp_capable : 1, 242 mp_join : 1, 243 backup : 1; 244 u8 local_id; 245 u8 remote_id; 246 u64 local_key; 247 u64 idsn; 248 u32 token; 249 u32 ssn_offset; 250 u64 thmac; 251 u32 local_nonce; 252 u32 remote_nonce; 253 struct mptcp_sock *msk; 254 struct hlist_nulls_node token_node; 255 }; 256 257 static inline struct mptcp_subflow_request_sock * 258 mptcp_subflow_rsk(const struct request_sock *rsk) 259 { 260 return (struct mptcp_subflow_request_sock *)rsk; 261 } 262 263 /* MPTCP subflow context */ 264 struct mptcp_subflow_context { 265 struct list_head node;/* conn_list of subflows */ 266 u64 local_key; 267 u64 remote_key; 268 u64 idsn; 269 u64 map_seq; 270 u32 snd_isn; 271 u32 token; 272 u32 rel_write_seq; 273 u32 map_subflow_seq; 274 u32 ssn_offset; 275 u32 map_data_len; 276 u32 request_mptcp : 1, /* send MP_CAPABLE */ 277 request_join : 1, /* send MP_JOIN */ 278 request_bkup : 1, 279 mp_capable : 1, /* remote is MPTCP capable */ 280 mp_join : 1, /* remote is JOINing */ 281 fully_established : 1, /* path validated */ 282 pm_notified : 1, /* PM hook called for established status */ 283 conn_finished : 1, 284 map_valid : 1, 285 mpc_map : 1, 286 backup : 1, 287 data_avail : 1, 288 rx_eof : 1, 289 data_fin_tx_enable : 1, 290 use_64bit_ack : 1, /* Set when we received a 64-bit DSN */ 291 can_ack : 1; /* only after processing the remote a key */ 292 u64 data_fin_tx_seq; 293 u32 remote_nonce; 294 u64 thmac; 295 u32 local_nonce; 296 u32 remote_token; 297 u8 hmac[MPTCPOPT_HMAC_LEN]; 298 u8 local_id; 299 u8 remote_id; 300 301 struct sock *tcp_sock; /* tcp sk backpointer */ 302 struct sock *conn; /* parent mptcp_sock */ 303 const struct inet_connection_sock_af_ops *icsk_af_ops; 304 void (*tcp_data_ready)(struct sock *sk); 305 void (*tcp_state_change)(struct sock *sk); 306 void (*tcp_write_space)(struct sock *sk); 307 308 struct rcu_head rcu; 309 }; 310 311 static inline struct mptcp_subflow_context * 312 mptcp_subflow_ctx(const struct sock *sk) 313 { 314 struct inet_connection_sock *icsk = inet_csk(sk); 315 316 /* Use RCU on icsk_ulp_data only for sock diag code */ 317 return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data; 318 } 319 320 static inline struct sock * 321 mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow) 322 { 323 return subflow->tcp_sock; 324 } 325 326 static inline u64 327 mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow) 328 { 329 return tcp_sk(mptcp_subflow_tcp_sock(subflow))->copied_seq - 330 subflow->ssn_offset - 331 subflow->map_subflow_seq; 332 } 333 334 static inline u64 335 mptcp_subflow_get_mapped_dsn(const struct mptcp_subflow_context *subflow) 336 { 337 return subflow->map_seq + mptcp_subflow_get_map_offset(subflow); 338 } 339 340 int mptcp_is_enabled(struct net *net); 341 bool mptcp_subflow_data_available(struct sock *sk); 342 void __init mptcp_subflow_init(void); 343 344 /* called with sk socket lock held */ 345 int __mptcp_subflow_connect(struct sock *sk, int ifindex, 346 const struct mptcp_addr_info *loc, 347 const struct mptcp_addr_info *remote); 348 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock); 349 350 static inline void mptcp_subflow_tcp_fallback(struct sock *sk, 351 struct mptcp_subflow_context *ctx) 352 { 353 sk->sk_data_ready = ctx->tcp_data_ready; 354 sk->sk_state_change = ctx->tcp_state_change; 355 sk->sk_write_space = ctx->tcp_write_space; 356 357 inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops; 358 } 359 360 void __init mptcp_proto_init(void); 361 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 362 int __init mptcp_proto_v6_init(void); 363 #endif 364 365 struct sock *mptcp_sk_clone(const struct sock *sk, 366 const struct mptcp_options_received *mp_opt, 367 struct request_sock *req); 368 void mptcp_get_options(const struct sk_buff *skb, 369 struct mptcp_options_received *mp_opt); 370 371 void mptcp_finish_connect(struct sock *sk); 372 void mptcp_data_ready(struct sock *sk, struct sock *ssk); 373 bool mptcp_finish_join(struct sock *sk); 374 void mptcp_data_acked(struct sock *sk); 375 void mptcp_subflow_eof(struct sock *sk); 376 377 void __init mptcp_token_init(void); 378 static inline void mptcp_token_init_request(struct request_sock *req) 379 { 380 mptcp_subflow_rsk(req)->token_node.pprev = NULL; 381 } 382 383 int mptcp_token_new_request(struct request_sock *req); 384 void mptcp_token_destroy_request(struct request_sock *req); 385 int mptcp_token_new_connect(struct sock *sk); 386 void mptcp_token_accept(struct mptcp_subflow_request_sock *r, 387 struct mptcp_sock *msk); 388 struct mptcp_sock *mptcp_token_get_sock(u32 token); 389 void mptcp_token_destroy(struct mptcp_sock *msk); 390 391 void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn); 392 static inline void mptcp_crypto_key_gen_sha(u64 *key, u32 *token, u64 *idsn) 393 { 394 /* we might consider a faster version that computes the key as a 395 * hash of some information available in the MPTCP socket. Use 396 * random data at the moment, as it's probably the safest option 397 * in case multiple sockets are opened in different namespaces at 398 * the same time. 399 */ 400 get_random_bytes(key, sizeof(u64)); 401 mptcp_crypto_key_sha(*key, token, idsn); 402 } 403 404 void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac); 405 406 void __init mptcp_pm_init(void); 407 void mptcp_pm_data_init(struct mptcp_sock *msk); 408 void mptcp_pm_close(struct mptcp_sock *msk); 409 void mptcp_pm_new_connection(struct mptcp_sock *msk, int server_side); 410 void mptcp_pm_fully_established(struct mptcp_sock *msk); 411 bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk); 412 void mptcp_pm_connection_closed(struct mptcp_sock *msk); 413 void mptcp_pm_subflow_established(struct mptcp_sock *msk, 414 struct mptcp_subflow_context *subflow); 415 void mptcp_pm_subflow_closed(struct mptcp_sock *msk, u8 id); 416 void mptcp_pm_add_addr_received(struct mptcp_sock *msk, 417 const struct mptcp_addr_info *addr); 418 419 int mptcp_pm_announce_addr(struct mptcp_sock *msk, 420 const struct mptcp_addr_info *addr); 421 int mptcp_pm_remove_addr(struct mptcp_sock *msk, u8 local_id); 422 int mptcp_pm_remove_subflow(struct mptcp_sock *msk, u8 remote_id); 423 424 static inline bool mptcp_pm_should_signal(struct mptcp_sock *msk) 425 { 426 return READ_ONCE(msk->pm.addr_signal); 427 } 428 429 static inline unsigned int mptcp_add_addr_len(int family) 430 { 431 if (family == AF_INET) 432 return TCPOLEN_MPTCP_ADD_ADDR; 433 return TCPOLEN_MPTCP_ADD_ADDR6; 434 } 435 436 bool mptcp_pm_addr_signal(struct mptcp_sock *msk, unsigned int remaining, 437 struct mptcp_addr_info *saddr); 438 int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc); 439 440 void __init mptcp_pm_nl_init(void); 441 void mptcp_pm_nl_data_init(struct mptcp_sock *msk); 442 void mptcp_pm_nl_fully_established(struct mptcp_sock *msk); 443 void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk); 444 void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk); 445 int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc); 446 447 static inline struct mptcp_ext *mptcp_get_ext(struct sk_buff *skb) 448 { 449 return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP); 450 } 451 452 static inline bool before64(__u64 seq1, __u64 seq2) 453 { 454 return (__s64)(seq1 - seq2) < 0; 455 } 456 457 #define after64(seq2, seq1) before64(seq1, seq2) 458 459 void mptcp_diag_subflow_init(struct tcp_ulp_ops *ops); 460 461 static inline bool __mptcp_check_fallback(struct mptcp_sock *msk) 462 { 463 return test_bit(MPTCP_FALLBACK_DONE, &msk->flags); 464 } 465 466 static inline bool mptcp_check_fallback(struct sock *sk) 467 { 468 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 469 struct mptcp_sock *msk = mptcp_sk(subflow->conn); 470 471 return __mptcp_check_fallback(msk); 472 } 473 474 static inline void __mptcp_do_fallback(struct mptcp_sock *msk) 475 { 476 if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags)) { 477 pr_debug("TCP fallback already done (msk=%p)", msk); 478 return; 479 } 480 set_bit(MPTCP_FALLBACK_DONE, &msk->flags); 481 } 482 483 static inline void mptcp_do_fallback(struct sock *sk) 484 { 485 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 486 struct mptcp_sock *msk = mptcp_sk(subflow->conn); 487 488 __mptcp_do_fallback(msk); 489 } 490 491 #define pr_fallback(a) pr_debug("%s:fallback to TCP (msk=%p)", __func__, a) 492 493 static inline bool subflow_simultaneous_connect(struct sock *sk) 494 { 495 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 496 struct sock *parent = subflow->conn; 497 498 return sk->sk_state == TCP_ESTABLISHED && 499 !mptcp_sk(parent)->pm.server_side && 500 !subflow->conn_finished; 501 } 502 503 #endif /* __MPTCP_PROTOCOL_H */ 504