mptcp.h 7.9 KB

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