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 seq_file;
18 
19 /* MPTCP sk_buff extension data */
20 struct mptcp_ext {
21 	union {
22 		u64	data_ack;
23 		u32	data_ack32;
24 	};
25 	u64		data_seq;
26 	u32		subflow_seq;
27 	u16		data_len;
28 	__sum16		csum;
29 	u8		use_map:1,
30 			dsn64:1,
31 			data_fin:1,
32 			use_ack:1,
33 			ack64:1,
34 			mpc_map:1,
35 			frozen:1,
36 			reset_transient:1;
37 	u8		reset_reason:4,
38 			csum_reqd:1,
39 			infinite_map:1;
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 	union {
73 		struct {
74 			u64 sndr_key;
75 			u64 rcvr_key;
76 			u64 data_seq;
77 			u32 subflow_seq;
78 			u16 data_len;
79 			__sum16 csum;
80 		};
81 		struct {
82 			struct mptcp_addr_info addr;
83 			u64 ahmac;
84 		};
85 		struct {
86 			struct mptcp_ext ext_copy;
87 			u64 fail_seq;
88 		};
89 		struct {
90 			u32 nonce;
91 			u32 token;
92 			u64 thmac;
93 			u8 hmac[MPTCPOPT_HMAC_LEN];
94 		};
95 	};
96 #endif
97 };
98 
99 #ifdef CONFIG_MPTCP
100 void mptcp_init(void);
101 
sk_is_mptcp(const struct sock * sk)102 static inline bool sk_is_mptcp(const struct sock *sk)
103 {
104 	return tcp_sk(sk)->is_mptcp;
105 }
106 
rsk_is_mptcp(const struct request_sock * req)107 static inline bool rsk_is_mptcp(const struct request_sock *req)
108 {
109 	return tcp_rsk(req)->is_mptcp;
110 }
111 
rsk_drop_req(const struct request_sock * req)112 static inline bool rsk_drop_req(const struct request_sock *req)
113 {
114 	return tcp_rsk(req)->is_mptcp && tcp_rsk(req)->drop_req;
115 }
116 
117 void mptcp_space(const struct sock *ssk, int *space, int *full_space);
118 bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
119 		       unsigned int *size, struct mptcp_out_options *opts);
120 bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
121 			  struct mptcp_out_options *opts);
122 bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
123 			       unsigned int *size, unsigned int remaining,
124 			       struct mptcp_out_options *opts);
125 bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb);
126 
127 void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
128 			 struct mptcp_out_options *opts);
129 
130 void mptcp_diag_fill_info(struct mptcp_sock *msk, struct mptcp_info *info);
131 
132 /* move the skb extension owership, with the assumption that 'to' is
133  * newly allocated
134  */
mptcp_skb_ext_move(struct sk_buff * to,struct sk_buff * from)135 static inline void mptcp_skb_ext_move(struct sk_buff *to,
136 				      struct sk_buff *from)
137 {
138 	if (!skb_ext_exist(from, SKB_EXT_MPTCP))
139 		return;
140 
141 	if (WARN_ON_ONCE(to->active_extensions))
142 		skb_ext_put(to);
143 
144 	to->active_extensions = from->active_extensions;
145 	to->extensions = from->extensions;
146 	from->active_extensions = 0;
147 }
148 
mptcp_skb_ext_copy(struct sk_buff * to,struct sk_buff * from)149 static inline void mptcp_skb_ext_copy(struct sk_buff *to,
150 				      struct sk_buff *from)
151 {
152 	struct mptcp_ext *from_ext;
153 
154 	from_ext = skb_ext_find(from, SKB_EXT_MPTCP);
155 	if (!from_ext)
156 		return;
157 
158 	from_ext->frozen = 1;
159 	skb_ext_copy(to, from);
160 }
161 
mptcp_ext_matches(const struct mptcp_ext * to_ext,const struct mptcp_ext * from_ext)162 static inline bool mptcp_ext_matches(const struct mptcp_ext *to_ext,
163 				     const struct mptcp_ext *from_ext)
164 {
165 	/* MPTCP always clears the ext when adding it to the skb, so
166 	 * holes do not bother us here
167 	 */
168 	return !from_ext ||
169 	       (to_ext && from_ext &&
170 	        !memcmp(from_ext, to_ext, sizeof(struct mptcp_ext)));
171 }
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  */
mptcp_skb_can_collapse(const struct sk_buff * to,const struct sk_buff * from)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 
185 void mptcp_seq_show(struct seq_file *seq);
186 int mptcp_subflow_init_cookie_req(struct request_sock *req,
187 				  const struct sock *sk_listener,
188 				  struct sk_buff *skb);
189 struct request_sock *mptcp_subflow_reqsk_alloc(const struct request_sock_ops *ops,
190 					       struct sock *sk_listener,
191 					       bool attach_listener);
192 
193 __be32 mptcp_get_reset_option(const struct sk_buff *skb);
194 
mptcp_reset_option(const struct sk_buff * skb)195 static inline __be32 mptcp_reset_option(const struct sk_buff *skb)
196 {
197 	if (skb_ext_exist(skb, SKB_EXT_MPTCP))
198 		return mptcp_get_reset_option(skb);
199 
200 	return htonl(0u);
201 }
202 #else
203 
mptcp_init(void)204 static inline void mptcp_init(void)
205 {
206 }
207 
sk_is_mptcp(const struct sock * sk)208 static inline bool sk_is_mptcp(const struct sock *sk)
209 {
210 	return false;
211 }
212 
rsk_is_mptcp(const struct request_sock * req)213 static inline bool rsk_is_mptcp(const struct request_sock *req)
214 {
215 	return false;
216 }
217 
rsk_drop_req(const struct request_sock * req)218 static inline bool rsk_drop_req(const struct request_sock *req)
219 {
220 	return false;
221 }
222 
mptcp_syn_options(struct sock * sk,const struct sk_buff * skb,unsigned int * size,struct mptcp_out_options * opts)223 static inline bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
224 				     unsigned int *size,
225 				     struct mptcp_out_options *opts)
226 {
227 	return false;
228 }
229 
mptcp_synack_options(const struct request_sock * req,unsigned int * size,struct mptcp_out_options * opts)230 static inline bool mptcp_synack_options(const struct request_sock *req,
231 					unsigned int *size,
232 					struct mptcp_out_options *opts)
233 {
234 	return false;
235 }
236 
mptcp_established_options(struct sock * sk,struct sk_buff * skb,unsigned int * size,unsigned int remaining,struct mptcp_out_options * opts)237 static inline bool mptcp_established_options(struct sock *sk,
238 					     struct sk_buff *skb,
239 					     unsigned int *size,
240 					     unsigned int remaining,
241 					     struct mptcp_out_options *opts)
242 {
243 	return false;
244 }
245 
mptcp_incoming_options(struct sock * sk,struct sk_buff * skb)246 static inline bool mptcp_incoming_options(struct sock *sk,
247 					  struct sk_buff *skb)
248 {
249 	return true;
250 }
251 
mptcp_skb_ext_move(struct sk_buff * to,const struct sk_buff * from)252 static inline void mptcp_skb_ext_move(struct sk_buff *to,
253 				      const struct sk_buff *from)
254 {
255 }
256 
mptcp_skb_ext_copy(struct sk_buff * to,struct sk_buff * from)257 static inline void mptcp_skb_ext_copy(struct sk_buff *to,
258 				      struct sk_buff *from)
259 {
260 }
261 
mptcp_skb_can_collapse(const struct sk_buff * to,const struct sk_buff * from)262 static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
263 					  const struct sk_buff *from)
264 {
265 	return true;
266 }
267 
mptcp_space(const struct sock * ssk,int * s,int * fs)268 static inline void mptcp_space(const struct sock *ssk, int *s, int *fs) { }
mptcp_seq_show(struct seq_file * seq)269 static inline void mptcp_seq_show(struct seq_file *seq) { }
270 
mptcp_subflow_init_cookie_req(struct request_sock * req,const struct sock * sk_listener,struct sk_buff * skb)271 static inline int mptcp_subflow_init_cookie_req(struct request_sock *req,
272 						const struct sock *sk_listener,
273 						struct sk_buff *skb)
274 {
275 	return 0; /* TCP fallback */
276 }
277 
mptcp_subflow_reqsk_alloc(const struct request_sock_ops * ops,struct sock * sk_listener,bool attach_listener)278 static inline struct request_sock *mptcp_subflow_reqsk_alloc(const struct request_sock_ops *ops,
279 							     struct sock *sk_listener,
280 							     bool attach_listener)
281 {
282 	return NULL;
283 }
284 
mptcp_reset_option(const struct sk_buff * skb)285 static inline __be32 mptcp_reset_option(const struct sk_buff *skb)  { return htonl(0u); }
286 #endif /* CONFIG_MPTCP */
287 
288 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
289 int mptcpv6_init(void);
290 void mptcpv6_handle_mapped(struct sock *sk, bool mapped);
291 #elif IS_ENABLED(CONFIG_IPV6)
mptcpv6_init(void)292 static inline int mptcpv6_init(void) { return 0; }
mptcpv6_handle_mapped(struct sock * sk,bool mapped)293 static inline void mptcpv6_handle_mapped(struct sock *sk, bool mapped) { }
294 #endif
295 
296 #if defined(CONFIG_MPTCP) && defined(CONFIG_BPF_SYSCALL)
297 struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk);
298 #else
bpf_mptcp_sock_from_subflow(struct sock * sk)299 static inline struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk) { return NULL; }
300 #endif
301 
302 #if !IS_ENABLED(CONFIG_MPTCP)
303 struct mptcp_sock { };
304 #endif
305 
306 #endif /* __NET_MPTCP_H */
307