mptcp.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #ifdef CONFIG_MPTCP
  91. void mptcp_init(void);
  92. static inline bool sk_is_mptcp(const struct sock *sk)
  93. {
  94. return tcp_sk(sk)->is_mptcp;
  95. }
  96. static inline bool rsk_is_mptcp(const struct request_sock *req)
  97. {
  98. return tcp_rsk(req)->is_mptcp;
  99. }
  100. static inline bool rsk_drop_req(const struct request_sock *req)
  101. {
  102. return tcp_rsk(req)->is_mptcp && tcp_rsk(req)->drop_req;
  103. }
  104. void mptcp_space(const struct sock *ssk, int *space, int *full_space);
  105. bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
  106. unsigned int *size, struct mptcp_out_options *opts);
  107. bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
  108. struct mptcp_out_options *opts);
  109. bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
  110. unsigned int *size, unsigned int remaining,
  111. struct mptcp_out_options *opts);
  112. bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb);
  113. void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
  114. struct mptcp_out_options *opts);
  115. void mptcp_diag_fill_info(struct mptcp_sock *msk, struct mptcp_info *info);
  116. /* move the skb extension owership, with the assumption that 'to' is
  117. * newly allocated
  118. */
  119. static inline void mptcp_skb_ext_move(struct sk_buff *to,
  120. struct sk_buff *from)
  121. {
  122. if (!skb_ext_exist(from, SKB_EXT_MPTCP))
  123. return;
  124. if (WARN_ON_ONCE(to->active_extensions))
  125. skb_ext_put(to);
  126. to->active_extensions = from->active_extensions;
  127. to->extensions = from->extensions;
  128. from->active_extensions = 0;
  129. }
  130. static inline void mptcp_skb_ext_copy(struct sk_buff *to,
  131. struct sk_buff *from)
  132. {
  133. struct mptcp_ext *from_ext;
  134. from_ext = skb_ext_find(from, SKB_EXT_MPTCP);
  135. if (!from_ext)
  136. return;
  137. from_ext->frozen = 1;
  138. skb_ext_copy(to, from);
  139. }
  140. static inline bool mptcp_ext_matches(const struct mptcp_ext *to_ext,
  141. const struct mptcp_ext *from_ext)
  142. {
  143. /* MPTCP always clears the ext when adding it to the skb, so
  144. * holes do not bother us here
  145. */
  146. return !from_ext ||
  147. (to_ext && from_ext &&
  148. !memcmp(from_ext, to_ext, sizeof(struct mptcp_ext)));
  149. }
  150. /* check if skbs can be collapsed.
  151. * MPTCP collapse is allowed if neither @to or @from carry an mptcp data
  152. * mapping, or if the extension of @to is the same as @from.
  153. * Collapsing is not possible if @to lacks an extension, but @from carries one.
  154. */
  155. static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
  156. const struct sk_buff *from)
  157. {
  158. return mptcp_ext_matches(skb_ext_find(to, SKB_EXT_MPTCP),
  159. skb_ext_find(from, SKB_EXT_MPTCP));
  160. }
  161. void mptcp_seq_show(struct seq_file *seq);
  162. int mptcp_subflow_init_cookie_req(struct request_sock *req,
  163. const struct sock *sk_listener,
  164. struct sk_buff *skb);
  165. struct request_sock *mptcp_subflow_reqsk_alloc(const struct request_sock_ops *ops,
  166. struct sock *sk_listener,
  167. bool attach_listener);
  168. __be32 mptcp_get_reset_option(const struct sk_buff *skb);
  169. static inline __be32 mptcp_reset_option(const struct sk_buff *skb)
  170. {
  171. if (skb_ext_exist(skb, SKB_EXT_MPTCP))
  172. return mptcp_get_reset_option(skb);
  173. return htonl(0u);
  174. }
  175. #else
  176. static inline void mptcp_init(void)
  177. {
  178. }
  179. static inline bool sk_is_mptcp(const struct sock *sk)
  180. {
  181. return false;
  182. }
  183. static inline bool rsk_is_mptcp(const struct request_sock *req)
  184. {
  185. return false;
  186. }
  187. static inline bool rsk_drop_req(const struct request_sock *req)
  188. {
  189. return false;
  190. }
  191. static inline bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
  192. unsigned int *size,
  193. struct mptcp_out_options *opts)
  194. {
  195. return false;
  196. }
  197. static inline bool mptcp_synack_options(const struct request_sock *req,
  198. unsigned int *size,
  199. struct mptcp_out_options *opts)
  200. {
  201. return false;
  202. }
  203. static inline bool mptcp_established_options(struct sock *sk,
  204. struct sk_buff *skb,
  205. unsigned int *size,
  206. unsigned int remaining,
  207. struct mptcp_out_options *opts)
  208. {
  209. return false;
  210. }
  211. static inline bool mptcp_incoming_options(struct sock *sk,
  212. struct sk_buff *skb)
  213. {
  214. return true;
  215. }
  216. static inline void mptcp_skb_ext_move(struct sk_buff *to,
  217. const struct sk_buff *from)
  218. {
  219. }
  220. static inline void mptcp_skb_ext_copy(struct sk_buff *to,
  221. struct sk_buff *from)
  222. {
  223. }
  224. static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
  225. const struct sk_buff *from)
  226. {
  227. return true;
  228. }
  229. static inline void mptcp_space(const struct sock *ssk, int *s, int *fs) { }
  230. static inline void mptcp_seq_show(struct seq_file *seq) { }
  231. static inline int mptcp_subflow_init_cookie_req(struct request_sock *req,
  232. const struct sock *sk_listener,
  233. struct sk_buff *skb)
  234. {
  235. return 0; /* TCP fallback */
  236. }
  237. static inline struct request_sock *mptcp_subflow_reqsk_alloc(const struct request_sock_ops *ops,
  238. struct sock *sk_listener,
  239. bool attach_listener)
  240. {
  241. return NULL;
  242. }
  243. static inline __be32 mptcp_reset_option(const struct sk_buff *skb) { return htonl(0u); }
  244. #endif /* CONFIG_MPTCP */
  245. #if IS_ENABLED(CONFIG_MPTCP_IPV6)
  246. int mptcpv6_init(void);
  247. void mptcpv6_handle_mapped(struct sock *sk, bool mapped);
  248. #elif IS_ENABLED(CONFIG_IPV6)
  249. static inline int mptcpv6_init(void) { return 0; }
  250. static inline void mptcpv6_handle_mapped(struct sock *sk, bool mapped) { }
  251. #endif
  252. #if defined(CONFIG_MPTCP) && defined(CONFIG_BPF_SYSCALL)
  253. struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk);
  254. #else
  255. static inline struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk) { return NULL; }
  256. #endif
  257. #if !IS_ENABLED(CONFIG_MPTCP)
  258. struct mptcp_sock { };
  259. #endif
  260. #endif /* __NET_MPTCP_H */