xref: /openbmc/linux/include/net/mptcp.h (revision fa3fe2b1)
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, const struct tcp_sock *tp,
92 			 struct mptcp_out_options *opts);
93 
94 /* move the skb extension owership, with the assumption that 'to' is
95  * newly allocated
96  */
97 static inline void mptcp_skb_ext_move(struct sk_buff *to,
98 				      struct sk_buff *from)
99 {
100 	if (!skb_ext_exist(from, SKB_EXT_MPTCP))
101 		return;
102 
103 	if (WARN_ON_ONCE(to->active_extensions))
104 		skb_ext_put(to);
105 
106 	to->active_extensions = from->active_extensions;
107 	to->extensions = from->extensions;
108 	from->active_extensions = 0;
109 }
110 
111 static inline void mptcp_skb_ext_copy(struct sk_buff *to,
112 				      struct sk_buff *from)
113 {
114 	struct mptcp_ext *from_ext;
115 
116 	from_ext = skb_ext_find(from, SKB_EXT_MPTCP);
117 	if (!from_ext)
118 		return;
119 
120 	from_ext->frozen = 1;
121 	skb_ext_copy(to, from);
122 }
123 
124 static inline bool mptcp_ext_matches(const struct mptcp_ext *to_ext,
125 				     const struct mptcp_ext *from_ext)
126 {
127 	/* MPTCP always clears the ext when adding it to the skb, so
128 	 * holes do not bother us here
129 	 */
130 	return !from_ext ||
131 	       (to_ext && from_ext &&
132 	        !memcmp(from_ext, to_ext, sizeof(struct mptcp_ext)));
133 }
134 
135 /* check if skbs can be collapsed.
136  * MPTCP collapse is allowed if neither @to or @from carry an mptcp data
137  * mapping, or if the extension of @to is the same as @from.
138  * Collapsing is not possible if @to lacks an extension, but @from carries one.
139  */
140 static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
141 					  const struct sk_buff *from)
142 {
143 	return mptcp_ext_matches(skb_ext_find(to, SKB_EXT_MPTCP),
144 				 skb_ext_find(from, SKB_EXT_MPTCP));
145 }
146 
147 void mptcp_seq_show(struct seq_file *seq);
148 int mptcp_subflow_init_cookie_req(struct request_sock *req,
149 				  const struct sock *sk_listener,
150 				  struct sk_buff *skb);
151 #else
152 
153 static inline void mptcp_init(void)
154 {
155 }
156 
157 static inline bool sk_is_mptcp(const struct sock *sk)
158 {
159 	return false;
160 }
161 
162 static inline bool rsk_is_mptcp(const struct request_sock *req)
163 {
164 	return false;
165 }
166 
167 static inline bool rsk_drop_req(const struct request_sock *req)
168 {
169 	return false;
170 }
171 
172 static inline void mptcp_parse_option(const struct sk_buff *skb,
173 				      const unsigned char *ptr, int opsize,
174 				      struct tcp_options_received *opt_rx)
175 {
176 }
177 
178 static inline bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
179 				     unsigned int *size,
180 				     struct mptcp_out_options *opts)
181 {
182 	return false;
183 }
184 
185 static inline bool mptcp_synack_options(const struct request_sock *req,
186 					unsigned int *size,
187 					struct mptcp_out_options *opts)
188 {
189 	return false;
190 }
191 
192 static inline bool mptcp_established_options(struct sock *sk,
193 					     struct sk_buff *skb,
194 					     unsigned int *size,
195 					     unsigned int remaining,
196 					     struct mptcp_out_options *opts)
197 {
198 	return false;
199 }
200 
201 static inline void mptcp_incoming_options(struct sock *sk,
202 					  struct sk_buff *skb)
203 {
204 }
205 
206 static inline void mptcp_skb_ext_move(struct sk_buff *to,
207 				      const struct sk_buff *from)
208 {
209 }
210 
211 static inline void mptcp_skb_ext_copy(struct sk_buff *to,
212 				      struct sk_buff *from)
213 {
214 }
215 
216 static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
217 					  const struct sk_buff *from)
218 {
219 	return true;
220 }
221 
222 static inline void mptcp_space(const struct sock *ssk, int *s, int *fs) { }
223 static inline void mptcp_seq_show(struct seq_file *seq) { }
224 
225 static inline int mptcp_subflow_init_cookie_req(struct request_sock *req,
226 						const struct sock *sk_listener,
227 						struct sk_buff *skb)
228 {
229 	return 0; /* TCP fallback */
230 }
231 #endif /* CONFIG_MPTCP */
232 
233 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
234 int mptcpv6_init(void);
235 void mptcpv6_handle_mapped(struct sock *sk, bool mapped);
236 #elif IS_ENABLED(CONFIG_IPV6)
237 static inline int mptcpv6_init(void) { return 0; }
238 static inline void mptcpv6_handle_mapped(struct sock *sk, bool mapped) { }
239 #endif
240 
241 #endif /* __NET_MPTCP_H */
242