xref: /openbmc/linux/include/net/mptcp.h (revision cec37a6e41aae7bf3df9a3da783380a4d9325fd8)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Multipath TCP
4  *
5  * Copyright (c) 2017 - 2019, Intel Corporation.
6  */
7 
8 #ifndef __NET_MPTCP_H
9 #define __NET_MPTCP_H
10 
11 #include <linux/skbuff.h>
12 #include <linux/tcp.h>
13 #include <linux/types.h>
14 
15 /* MPTCP sk_buff extension data */
16 struct mptcp_ext {
17 	u64		data_ack;
18 	u64		data_seq;
19 	u32		subflow_seq;
20 	u16		data_len;
21 	u8		use_map:1,
22 			dsn64:1,
23 			data_fin:1,
24 			use_ack:1,
25 			ack64:1,
26 			__unused:3;
27 	/* one byte hole */
28 };
29 
30 struct mptcp_out_options {
31 #if IS_ENABLED(CONFIG_MPTCP)
32 	u16 suboptions;
33 	u64 sndr_key;
34 	u64 rcvr_key;
35 #endif
36 };
37 
38 #ifdef CONFIG_MPTCP
39 
40 void mptcp_init(void);
41 
42 static inline bool sk_is_mptcp(const struct sock *sk)
43 {
44 	return tcp_sk(sk)->is_mptcp;
45 }
46 
47 static inline bool rsk_is_mptcp(const struct request_sock *req)
48 {
49 	return tcp_rsk(req)->is_mptcp;
50 }
51 
52 void mptcp_parse_option(const unsigned char *ptr, int opsize,
53 			struct tcp_options_received *opt_rx);
54 bool mptcp_syn_options(struct sock *sk, unsigned int *size,
55 		       struct mptcp_out_options *opts);
56 void mptcp_rcv_synsent(struct sock *sk);
57 bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
58 			  struct mptcp_out_options *opts);
59 bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
60 			       unsigned int *size, unsigned int remaining,
61 			       struct mptcp_out_options *opts);
62 
63 void mptcp_write_options(__be32 *ptr, struct mptcp_out_options *opts);
64 
65 /* move the skb extension owership, with the assumption that 'to' is
66  * newly allocated
67  */
68 static inline void mptcp_skb_ext_move(struct sk_buff *to,
69 				      struct sk_buff *from)
70 {
71 	if (!skb_ext_exist(from, SKB_EXT_MPTCP))
72 		return;
73 
74 	if (WARN_ON_ONCE(to->active_extensions))
75 		skb_ext_put(to);
76 
77 	to->active_extensions = from->active_extensions;
78 	to->extensions = from->extensions;
79 	from->active_extensions = 0;
80 }
81 
82 static inline bool mptcp_ext_matches(const struct mptcp_ext *to_ext,
83 				     const struct mptcp_ext *from_ext)
84 {
85 	/* MPTCP always clears the ext when adding it to the skb, so
86 	 * holes do not bother us here
87 	 */
88 	return !from_ext ||
89 	       (to_ext && from_ext &&
90 	        !memcmp(from_ext, to_ext, sizeof(struct mptcp_ext)));
91 }
92 
93 /* check if skbs can be collapsed.
94  * MPTCP collapse is allowed if neither @to or @from carry an mptcp data
95  * mapping, or if the extension of @to is the same as @from.
96  * Collapsing is not possible if @to lacks an extension, but @from carries one.
97  */
98 static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
99 					  const struct sk_buff *from)
100 {
101 	return mptcp_ext_matches(skb_ext_find(to, SKB_EXT_MPTCP),
102 				 skb_ext_find(from, SKB_EXT_MPTCP));
103 }
104 
105 #else
106 
107 static inline void mptcp_init(void)
108 {
109 }
110 
111 static inline bool sk_is_mptcp(const struct sock *sk)
112 {
113 	return false;
114 }
115 
116 static inline bool rsk_is_mptcp(const struct request_sock *req)
117 {
118 	return false;
119 }
120 
121 static inline void mptcp_parse_option(const unsigned char *ptr, int opsize,
122 				      struct tcp_options_received *opt_rx)
123 {
124 }
125 
126 static inline bool mptcp_syn_options(struct sock *sk, unsigned int *size,
127 				     struct mptcp_out_options *opts)
128 {
129 	return false;
130 }
131 
132 static inline void mptcp_rcv_synsent(struct sock *sk)
133 {
134 }
135 
136 static inline bool mptcp_synack_options(const struct request_sock *req,
137 					unsigned int *size,
138 					struct mptcp_out_options *opts)
139 {
140 	return false;
141 }
142 
143 static inline bool mptcp_established_options(struct sock *sk,
144 					     struct sk_buff *skb,
145 					     unsigned int *size,
146 					     unsigned int remaining,
147 					     struct mptcp_out_options *opts)
148 {
149 	return false;
150 }
151 
152 static inline void mptcp_skb_ext_move(struct sk_buff *to,
153 				      const struct sk_buff *from)
154 {
155 }
156 
157 static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
158 					  const struct sk_buff *from)
159 {
160 	return true;
161 }
162 
163 #endif /* CONFIG_MPTCP */
164 
165 void mptcp_handle_ipv6_mapped(struct sock *sk, bool mapped);
166 
167 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
168 int mptcpv6_init(void);
169 #elif IS_ENABLED(CONFIG_IPV6)
170 static inline int mptcpv6_init(void)
171 {
172 	return 0;
173 }
174 #endif
175 
176 #endif /* __NET_MPTCP_H */
177