xref: /openbmc/linux/include/net/mptcp.h (revision cefd754d)
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 seq_file;
16 
17 /* MPTCP sk_buff extension data */
18 struct mptcp_ext {
19 	union {
20 		u64	data_ack;
21 		u32	data_ack32;
22 	};
23 	u64		data_seq;
24 	u32		subflow_seq;
25 	u16		data_len;
26 	u8		use_map:1,
27 			dsn64:1,
28 			data_fin:1,
29 			use_ack:1,
30 			ack64:1,
31 			mpc_map:1,
32 			frozen:1,
33 			__unused:1;
34 	/* one byte hole */
35 };
36 
37 struct mptcp_out_options {
38 #if IS_ENABLED(CONFIG_MPTCP)
39 	u16 suboptions;
40 	u64 sndr_key;
41 	u64 rcvr_key;
42 	union {
43 		struct in_addr addr;
44 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
45 		struct in6_addr addr6;
46 #endif
47 	};
48 	u8 addr_id;
49 	u64 ahmac;
50 	u8 rm_id;
51 	u8 join_id;
52 	u8 backup;
53 	u32 nonce;
54 	u64 thmac;
55 	u32 token;
56 	u8 hmac[20];
57 	struct mptcp_ext ext_copy;
58 #endif
59 };
60 
61 #ifdef CONFIG_MPTCP
62 extern struct request_sock_ops mptcp_subflow_request_sock_ops;
63 
64 void mptcp_init(void);
65 
66 static inline bool sk_is_mptcp(const struct sock *sk)
67 {
68 	return tcp_sk(sk)->is_mptcp;
69 }
70 
71 static inline bool rsk_is_mptcp(const struct request_sock *req)
72 {
73 	return tcp_rsk(req)->is_mptcp;
74 }
75 
76 static inline bool rsk_drop_req(const struct request_sock *req)
77 {
78 	return tcp_rsk(req)->is_mptcp && tcp_rsk(req)->drop_req;
79 }
80 
81 void mptcp_space(const struct sock *ssk, int *space, int *full_space);
82 bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
83 		       unsigned int *size, struct mptcp_out_options *opts);
84 bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
85 			  struct mptcp_out_options *opts);
86 bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
87 			       unsigned int *size, unsigned int remaining,
88 			       struct mptcp_out_options *opts);
89 void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb);
90 
91 void mptcp_write_options(__be32 *ptr, struct mptcp_out_options *opts);
92 
93 /* move the skb extension owership, with the assumption that 'to' is
94  * newly allocated
95  */
96 static inline void mptcp_skb_ext_move(struct sk_buff *to,
97 				      struct sk_buff *from)
98 {
99 	if (!skb_ext_exist(from, SKB_EXT_MPTCP))
100 		return;
101 
102 	if (WARN_ON_ONCE(to->active_extensions))
103 		skb_ext_put(to);
104 
105 	to->active_extensions = from->active_extensions;
106 	to->extensions = from->extensions;
107 	from->active_extensions = 0;
108 }
109 
110 static inline void mptcp_skb_ext_copy(struct sk_buff *to,
111 				      struct sk_buff *from)
112 {
113 	struct mptcp_ext *from_ext;
114 
115 	from_ext = skb_ext_find(from, SKB_EXT_MPTCP);
116 	if (!from_ext)
117 		return;
118 
119 	from_ext->frozen = 1;
120 	skb_ext_copy(to, from);
121 }
122 
123 static inline bool mptcp_ext_matches(const struct mptcp_ext *to_ext,
124 				     const struct mptcp_ext *from_ext)
125 {
126 	/* MPTCP always clears the ext when adding it to the skb, so
127 	 * holes do not bother us here
128 	 */
129 	return !from_ext ||
130 	       (to_ext && from_ext &&
131 	        !memcmp(from_ext, to_ext, sizeof(struct mptcp_ext)));
132 }
133 
134 /* check if skbs can be collapsed.
135  * MPTCP collapse is allowed if neither @to or @from carry an mptcp data
136  * mapping, or if the extension of @to is the same as @from.
137  * Collapsing is not possible if @to lacks an extension, but @from carries one.
138  */
139 static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
140 					  const struct sk_buff *from)
141 {
142 	return mptcp_ext_matches(skb_ext_find(to, SKB_EXT_MPTCP),
143 				 skb_ext_find(from, SKB_EXT_MPTCP));
144 }
145 
146 void mptcp_seq_show(struct seq_file *seq);
147 int mptcp_subflow_init_cookie_req(struct request_sock *req,
148 				  const struct sock *sk_listener,
149 				  struct sk_buff *skb);
150 #else
151 
152 static inline void mptcp_init(void)
153 {
154 }
155 
156 static inline bool sk_is_mptcp(const struct sock *sk)
157 {
158 	return false;
159 }
160 
161 static inline bool rsk_is_mptcp(const struct request_sock *req)
162 {
163 	return false;
164 }
165 
166 static inline bool rsk_drop_req(const struct request_sock *req)
167 {
168 	return false;
169 }
170 
171 static inline void mptcp_parse_option(const struct sk_buff *skb,
172 				      const unsigned char *ptr, int opsize,
173 				      struct tcp_options_received *opt_rx)
174 {
175 }
176 
177 static inline bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
178 				     unsigned int *size,
179 				     struct mptcp_out_options *opts)
180 {
181 	return false;
182 }
183 
184 static inline bool mptcp_synack_options(const struct request_sock *req,
185 					unsigned int *size,
186 					struct mptcp_out_options *opts)
187 {
188 	return false;
189 }
190 
191 static inline bool mptcp_established_options(struct sock *sk,
192 					     struct sk_buff *skb,
193 					     unsigned int *size,
194 					     unsigned int remaining,
195 					     struct mptcp_out_options *opts)
196 {
197 	return false;
198 }
199 
200 static inline void mptcp_incoming_options(struct sock *sk,
201 					  struct sk_buff *skb)
202 {
203 }
204 
205 static inline void mptcp_skb_ext_move(struct sk_buff *to,
206 				      const struct sk_buff *from)
207 {
208 }
209 
210 static inline void mptcp_skb_ext_copy(struct sk_buff *to,
211 				      struct sk_buff *from)
212 {
213 }
214 
215 static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
216 					  const struct sk_buff *from)
217 {
218 	return true;
219 }
220 
221 static inline void mptcp_space(const struct sock *ssk, int *s, int *fs) { }
222 static inline void mptcp_seq_show(struct seq_file *seq) { }
223 
224 static inline int mptcp_subflow_init_cookie_req(struct request_sock *req,
225 						const struct sock *sk_listener,
226 						struct sk_buff *skb)
227 {
228 	return 0; /* TCP fallback */
229 }
230 #endif /* CONFIG_MPTCP */
231 
232 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
233 int mptcpv6_init(void);
234 void mptcpv6_handle_mapped(struct sock *sk, bool mapped);
235 #elif IS_ENABLED(CONFIG_IPV6)
236 static inline int mptcpv6_init(void) { return 0; }
237 static inline void mptcpv6_handle_mapped(struct sock *sk, bool mapped) { }
238 #endif
239 
240 #endif /* __NET_MPTCP_H */
241