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