OSDN Git Service

mptcp: move from sha1 (v0) to sha256 (v1)
[tomoyo/tomoyo-test1.git] / net / mptcp / protocol.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Multipath TCP
3  *
4  * Copyright (c) 2017 - 2019, Intel Corporation.
5  */
6
7 #ifndef __MPTCP_PROTOCOL_H
8 #define __MPTCP_PROTOCOL_H
9
10 #include <linux/random.h>
11 #include <net/tcp.h>
12 #include <net/inet_connection_sock.h>
13
14 #define MPTCP_SUPPORTED_VERSION 0
15
16 /* MPTCP option bits */
17 #define OPTION_MPTCP_MPC_SYN    BIT(0)
18 #define OPTION_MPTCP_MPC_SYNACK BIT(1)
19 #define OPTION_MPTCP_MPC_ACK    BIT(2)
20
21 /* MPTCP option subtypes */
22 #define MPTCPOPT_MP_CAPABLE     0
23 #define MPTCPOPT_MP_JOIN        1
24 #define MPTCPOPT_DSS            2
25 #define MPTCPOPT_ADD_ADDR       3
26 #define MPTCPOPT_RM_ADDR        4
27 #define MPTCPOPT_MP_PRIO        5
28 #define MPTCPOPT_MP_FAIL        6
29 #define MPTCPOPT_MP_FASTCLOSE   7
30
31 /* MPTCP suboption lengths */
32 #define TCPOLEN_MPTCP_MPC_SYN           12
33 #define TCPOLEN_MPTCP_MPC_SYNACK        12
34 #define TCPOLEN_MPTCP_MPC_ACK           20
35 #define TCPOLEN_MPTCP_DSS_BASE          4
36 #define TCPOLEN_MPTCP_DSS_ACK32         4
37 #define TCPOLEN_MPTCP_DSS_ACK64         8
38 #define TCPOLEN_MPTCP_DSS_MAP32         10
39 #define TCPOLEN_MPTCP_DSS_MAP64         14
40 #define TCPOLEN_MPTCP_DSS_CHECKSUM      2
41
42 /* MPTCP MP_CAPABLE flags */
43 #define MPTCP_VERSION_MASK      (0x0F)
44 #define MPTCP_CAP_CHECKSUM_REQD BIT(7)
45 #define MPTCP_CAP_EXTENSIBILITY BIT(6)
46 #define MPTCP_CAP_HMAC_SHA256   BIT(0)
47 #define MPTCP_CAP_FLAG_MASK     (0x3F)
48
49 /* MPTCP DSS flags */
50 #define MPTCP_DSS_DATA_FIN      BIT(4)
51 #define MPTCP_DSS_DSN64         BIT(3)
52 #define MPTCP_DSS_HAS_MAP       BIT(2)
53 #define MPTCP_DSS_ACK64         BIT(1)
54 #define MPTCP_DSS_HAS_ACK       BIT(0)
55 #define MPTCP_DSS_FLAG_MASK     (0x1F)
56
57 /* MPTCP socket flags */
58 #define MPTCP_DATA_READY        BIT(0)
59 #define MPTCP_SEND_SPACE        BIT(1)
60
61 /* MPTCP connection sock */
62 struct mptcp_sock {
63         /* inet_connection_sock must be the first member */
64         struct inet_connection_sock sk;
65         u64             local_key;
66         u64             remote_key;
67         u64             write_seq;
68         u64             ack_seq;
69         u32             token;
70         unsigned long   flags;
71         struct list_head conn_list;
72         struct skb_ext  *cached_ext;    /* for the next sendmsg */
73         struct socket   *subflow; /* outgoing connect/listener/!mp_capable */
74 };
75
76 #define mptcp_for_each_subflow(__msk, __subflow)                        \
77         list_for_each_entry(__subflow, &((__msk)->conn_list), node)
78
79 static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
80 {
81         return (struct mptcp_sock *)sk;
82 }
83
84 struct mptcp_subflow_request_sock {
85         struct  tcp_request_sock sk;
86         u8      mp_capable : 1,
87                 mp_join : 1,
88                 backup : 1;
89         u64     local_key;
90         u64     remote_key;
91         u64     idsn;
92         u32     token;
93         u32     ssn_offset;
94 };
95
96 static inline struct mptcp_subflow_request_sock *
97 mptcp_subflow_rsk(const struct request_sock *rsk)
98 {
99         return (struct mptcp_subflow_request_sock *)rsk;
100 }
101
102 /* MPTCP subflow context */
103 struct mptcp_subflow_context {
104         struct  list_head node;/* conn_list of subflows */
105         u64     local_key;
106         u64     remote_key;
107         u64     idsn;
108         u64     map_seq;
109         u32     token;
110         u32     rel_write_seq;
111         u32     map_subflow_seq;
112         u32     ssn_offset;
113         u32     map_data_len;
114         u32     request_mptcp : 1,  /* send MP_CAPABLE */
115                 mp_capable : 1,     /* remote is MPTCP capable */
116                 fourth_ack : 1,     /* send initial DSS */
117                 conn_finished : 1,
118                 map_valid : 1,
119                 data_avail : 1,
120                 rx_eof : 1;
121
122         struct  sock *tcp_sock;     /* tcp sk backpointer */
123         struct  sock *conn;         /* parent mptcp_sock */
124         const   struct inet_connection_sock_af_ops *icsk_af_ops;
125         void    (*tcp_data_ready)(struct sock *sk);
126         void    (*tcp_state_change)(struct sock *sk);
127         void    (*tcp_write_space)(struct sock *sk);
128
129         struct  rcu_head rcu;
130 };
131
132 static inline struct mptcp_subflow_context *
133 mptcp_subflow_ctx(const struct sock *sk)
134 {
135         struct inet_connection_sock *icsk = inet_csk(sk);
136
137         /* Use RCU on icsk_ulp_data only for sock diag code */
138         return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;
139 }
140
141 static inline struct sock *
142 mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
143 {
144         return subflow->tcp_sock;
145 }
146
147 static inline u64
148 mptcp_subflow_get_map_offset(const struct mptcp_subflow_context *subflow)
149 {
150         return tcp_sk(mptcp_subflow_tcp_sock(subflow))->copied_seq -
151                       subflow->ssn_offset -
152                       subflow->map_subflow_seq;
153 }
154
155 static inline u64
156 mptcp_subflow_get_mapped_dsn(const struct mptcp_subflow_context *subflow)
157 {
158         return subflow->map_seq + mptcp_subflow_get_map_offset(subflow);
159 }
160
161 int mptcp_is_enabled(struct net *net);
162 bool mptcp_subflow_data_available(struct sock *sk);
163 void mptcp_subflow_init(void);
164 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);
165
166 static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
167                                               struct mptcp_subflow_context *ctx)
168 {
169         sk->sk_data_ready = ctx->tcp_data_ready;
170         sk->sk_state_change = ctx->tcp_state_change;
171         sk->sk_write_space = ctx->tcp_write_space;
172
173         inet_csk(sk)->icsk_af_ops = ctx->icsk_af_ops;
174 }
175
176 extern const struct inet_connection_sock_af_ops ipv4_specific;
177 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
178 extern const struct inet_connection_sock_af_ops ipv6_specific;
179 #endif
180
181 void mptcp_proto_init(void);
182 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
183 int mptcp_proto_v6_init(void);
184 #endif
185
186 struct mptcp_read_arg {
187         struct msghdr *msg;
188 };
189
190 int mptcp_read_actor(read_descriptor_t *desc, struct sk_buff *skb,
191                      unsigned int offset, size_t len);
192
193 void mptcp_get_options(const struct sk_buff *skb,
194                        struct tcp_options_received *opt_rx);
195
196 void mptcp_finish_connect(struct sock *sk);
197
198 int mptcp_token_new_request(struct request_sock *req);
199 void mptcp_token_destroy_request(u32 token);
200 int mptcp_token_new_connect(struct sock *sk);
201 int mptcp_token_new_accept(u32 token);
202 void mptcp_token_update_accept(struct sock *sk, struct sock *conn);
203 void mptcp_token_destroy(u32 token);
204
205 void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn);
206 static inline void mptcp_crypto_key_gen_sha(u64 *key, u32 *token, u64 *idsn)
207 {
208         /* we might consider a faster version that computes the key as a
209          * hash of some information available in the MPTCP socket. Use
210          * random data at the moment, as it's probably the safest option
211          * in case multiple sockets are opened in different namespaces at
212          * the same time.
213          */
214         get_random_bytes(key, sizeof(u64));
215         mptcp_crypto_key_sha(*key, token, idsn);
216 }
217
218 void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
219                            void *hash_out);
220
221 static inline struct mptcp_ext *mptcp_get_ext(struct sk_buff *skb)
222 {
223         return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP);
224 }
225
226 static inline bool before64(__u64 seq1, __u64 seq2)
227 {
228         return (__s64)(seq1 - seq2) < 0;
229 }
230
231 #define after64(seq2, seq1)     before64(seq1, seq2)
232
233 #endif /* __MPTCP_PROTOCOL_H */