mptcp.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Multipath TCP
  4. *
  5. * Copyright (c) 2017 - 2019, Intel Corporation.
  6. */
  7. #ifndef __NET_MPTCP_H
  8. #define __NET_MPTCP_H
  9. #include <linux/skbuff.h>
  10. #include <linux/tcp.h>
  11. #include <linux/types.h>
  12. struct mptcp_info;
  13. struct mptcp_sock;
  14. struct seq_file;
  15. /* MPTCP sk_buff extension data */
  16. struct mptcp_ext {
  17. union {
  18. u64 data_ack;
  19. u32 data_ack32;
  20. };
  21. u64 data_seq;
  22. u32 subflow_seq;
  23. u16 data_len;
  24. __sum16 csum;
  25. u8 use_map:1,
  26. dsn64:1,
  27. data_fin:1,
  28. use_ack:1,
  29. ack64:1,
  30. mpc_map:1,
  31. frozen:1,
  32. reset_transient:1;
  33. u8 reset_reason:4,
  34. csum_reqd:1,
  35. infinite_map:1;
  36. };
  37. #define MPTCPOPT_HMAC_LEN 20
  38. #define MPTCP_RM_IDS_MAX 8
  39. struct mptcp_rm_list {
  40. u8 ids[MPTCP_RM_IDS_MAX];
  41. u8 nr;
  42. };
  43. struct mptcp_addr_info {
  44. u8 id;
  45. sa_family_t family;
  46. __be16 port;
  47. union {
  48. struct in_addr addr;
  49. #if IS_ENABLED(CONFIG_MPTCP_IPV6)
  50. struct in6_addr addr6;
  51. #endif
  52. };
  53. };
  54. struct mptcp_out_options {
  55. #if IS_ENABLED(CONFIG_MPTCP)
  56. u16 suboptions;
  57. struct mptcp_rm_list rm_list;
  58. u8 join_id;
  59. u8 backup;
  60. u8 reset_reason:4,
  61. reset_transient:1,
  62. csum_reqd:1,
  63. allow_join_id0:1;
  64. union {
  65. struct {
  66. u64 sndr_key;
  67. u64 rcvr_key;
  68. u64 data_seq;
  69. u32 subflow_seq;
  70. u16 data_len;
  71. __sum16 csum;
  72. };
  73. struct {
  74. struct mptcp_addr_info addr;
  75. u64 ahmac;
  76. };
  77. struct {
  78. struct mptcp_ext ext_copy;
  79. u64 fail_seq;
  80. };
  81. struct {
  82. u32 nonce;
  83. u32 token;
  84. u64 thmac;
  85. u8 hmac[MPTCPOPT_HMAC_LEN];
  86. };
  87. };
  88. #endif
  89. };
  90. #define MPTCP_SCHED_NAME_MAX 16
  91. #define MPTCP_SCHED_MAX 128
  92. #define MPTCP_SCHED_BUF_MAX (MPTCP_SCHED_NAME_MAX * MPTCP_SCHED_MAX)
  93. #define MPTCP_SUBFLOWS_MAX 8
  94. struct mptcp_sched_data {
  95. bool reinject;
  96. u8 subflows;
  97. struct mptcp_subflow_context *contexts[MPTCP_SUBFLOWS_MAX];
  98. };
  99. struct mptcp_sched_ops {
  100. int (*get_subflow)(struct mptcp_sock *msk,
  101. struct mptcp_sched_data *data);
  102. char name[MPTCP_SCHED_NAME_MAX];
  103. struct module *owner;
  104. struct list_head list;
  105. void (*init)(struct mptcp_sock *msk);
  106. void (*release)(struct mptcp_sock *msk);
  107. } ____cacheline_aligned_in_smp;
  108. #ifdef CONFIG_MPTCP
  109. void mptcp_init(void);
  110. static inline bool sk_is_mptcp(const struct sock *sk)
  111. {
  112. return tcp_sk(sk)->is_mptcp;
  113. }
  114. static inline bool rsk_is_mptcp(const struct request_sock *req)
  115. {
  116. return tcp_rsk(req)->is_mptcp;
  117. }
  118. static inline bool rsk_drop_req(const struct request_sock *req)
  119. {
  120. return tcp_rsk(req)->is_mptcp && tcp_rsk(req)->drop_req;
  121. }
  122. void mptcp_space(const struct sock *ssk, int *space, int *full_space);
  123. bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
  124. unsigned int *size, struct mptcp_out_options *opts);
  125. bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
  126. struct mptcp_out_options *opts);
  127. bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
  128. unsigned int *size, unsigned int remaining,
  129. struct mptcp_out_options *opts);
  130. bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb);
  131. void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
  132. struct mptcp_out_options *opts);
  133. void mptcp_diag_fill_info(struct mptcp_sock *msk, struct mptcp_info *info);
  134. /* move the skb extension owership, with the assumption that 'to' is
  135. * newly allocated
  136. */
  137. static inline void mptcp_skb_ext_move(struct sk_buff *to,
  138. struct sk_buff *from)
  139. {
  140. if (!skb_ext_exist(from, SKB_EXT_MPTCP))
  141. return;
  142. if (WARN_ON_ONCE(to->active_extensions))
  143. skb_ext_put(to);
  144. to->active_extensions = from->active_extensions;
  145. to->extensions = from->extensions;
  146. from->active_extensions = 0;
  147. }
  148. static inline void mptcp_skb_ext_copy(struct sk_buff *to,
  149. struct sk_buff *from)
  150. {
  151. struct mptcp_ext *from_ext;
  152. from_ext = skb_ext_find(from, SKB_EXT_MPTCP);
  153. if (!from_ext)
  154. return;
  155. from_ext->frozen = 1;
  156. skb_ext_copy(to, from);
  157. }
  158. static inline bool mptcp_ext_matches(const struct mptcp_ext *to_ext,
  159. const struct mptcp_ext *from_ext)
  160. {
  161. /* MPTCP always clears the ext when adding it to the skb, so
  162. * holes do not bother us here
  163. */
  164. return !from_ext ||
  165. (to_ext && from_ext &&
  166. !memcmp(from_ext, to_ext, sizeof(struct mptcp_ext)));
  167. }
  168. /* check if skbs can be collapsed.
  169. * MPTCP collapse is allowed if neither @to or @from carry an mptcp data
  170. * mapping, or if the extension of @to is the same as @from.
  171. * Collapsing is not possible if @to lacks an extension, but @from carries one.
  172. */
  173. static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
  174. const struct sk_buff *from)
  175. {
  176. return mptcp_ext_matches(skb_ext_find(to, SKB_EXT_MPTCP),
  177. skb_ext_find(from, SKB_EXT_MPTCP));
  178. }
  179. void mptcp_seq_show(struct seq_file *seq);
  180. int mptcp_subflow_init_cookie_req(struct request_sock *req,
  181. const struct sock *sk_listener,
  182. struct sk_buff *skb);
  183. struct request_sock *mptcp_subflow_reqsk_alloc(const struct request_sock_ops *ops,
  184. struct sock *sk_listener,
  185. bool attach_listener);
  186. __be32 mptcp_get_reset_option(const struct sk_buff *skb);
  187. static inline __be32 mptcp_reset_option(const struct sk_buff *skb)
  188. {
  189. if (skb_ext_exist(skb, SKB_EXT_MPTCP))
  190. return mptcp_get_reset_option(skb);
  191. return htonl(0u);
  192. }
  193. void mptcp_active_detect_blackhole(struct sock *sk, bool expired);
  194. #else
  195. static inline void mptcp_init(void)
  196. {
  197. }
  198. static inline bool sk_is_mptcp(const struct sock *sk)
  199. {
  200. return false;
  201. }
  202. static inline bool rsk_is_mptcp(const struct request_sock *req)
  203. {
  204. return false;
  205. }
  206. static inline bool rsk_drop_req(const struct request_sock *req)
  207. {
  208. return false;
  209. }
  210. static inline bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
  211. unsigned int *size,
  212. struct mptcp_out_options *opts)
  213. {
  214. return false;
  215. }
  216. static inline bool mptcp_synack_options(const struct request_sock *req,
  217. unsigned int *size,
  218. struct mptcp_out_options *opts)
  219. {
  220. return false;
  221. }
  222. static inline bool mptcp_established_options(struct sock *sk,
  223. struct sk_buff *skb,
  224. unsigned int *size,
  225. unsigned int remaining,
  226. struct mptcp_out_options *opts)
  227. {
  228. return false;
  229. }
  230. static inline bool mptcp_incoming_options(struct sock *sk,
  231. struct sk_buff *skb)
  232. {
  233. return true;
  234. }
  235. static inline void mptcp_skb_ext_move(struct sk_buff *to,
  236. const struct sk_buff *from)
  237. {
  238. }
  239. static inline void mptcp_skb_ext_copy(struct sk_buff *to,
  240. struct sk_buff *from)
  241. {
  242. }
  243. static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
  244. const struct sk_buff *from)
  245. {
  246. return true;
  247. }
  248. static inline void mptcp_space(const struct sock *ssk, int *s, int *fs) { }
  249. static inline void mptcp_seq_show(struct seq_file *seq) { }
  250. static inline int mptcp_subflow_init_cookie_req(struct request_sock *req,
  251. const struct sock *sk_listener,
  252. struct sk_buff *skb)
  253. {
  254. return 0; /* TCP fallback */
  255. }
  256. static inline struct request_sock *mptcp_subflow_reqsk_alloc(const struct request_sock_ops *ops,
  257. struct sock *sk_listener,
  258. bool attach_listener)
  259. {
  260. return NULL;
  261. }
  262. static inline __be32 mptcp_reset_option(const struct sk_buff *skb) { return htonl(0u); }
  263. static inline void mptcp_active_detect_blackhole(struct sock *sk, bool expired) { }
  264. #endif /* CONFIG_MPTCP */
  265. #if IS_ENABLED(CONFIG_MPTCP_IPV6)
  266. int mptcpv6_init(void);
  267. void mptcpv6_handle_mapped(struct sock *sk, bool mapped);
  268. #elif IS_ENABLED(CONFIG_IPV6)
  269. static inline int mptcpv6_init(void) { return 0; }
  270. static inline void mptcpv6_handle_mapped(struct sock *sk, bool mapped) { }
  271. #endif
  272. #if defined(CONFIG_MPTCP) && defined(CONFIG_BPF_SYSCALL)
  273. struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk);
  274. #else
  275. static inline struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk) { return NULL; }
  276. #endif
  277. #if !IS_ENABLED(CONFIG_MPTCP)
  278. struct mptcp_sock { };
  279. #endif
  280. #endif /* __NET_MPTCP_H */