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