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