154a3773e785c25b93231733e4d36dae40697014
[platform/kernel/linux-rpi.git] / net / tls / tls.h
1 /*
2  * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
3  * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #ifndef _TLS_INT_H
35 #define _TLS_INT_H
36
37 #include <asm/byteorder.h>
38 #include <linux/types.h>
39 #include <linux/skmsg.h>
40 #include <net/tls.h>
41
42 #define TLS_PAGE_ORDER  (min_t(unsigned int, PAGE_ALLOC_COSTLY_ORDER,   \
43                                TLS_MAX_PAYLOAD_SIZE >> PAGE_SHIFT))
44
45 #define __TLS_INC_STATS(net, field)                             \
46         __SNMP_INC_STATS((net)->mib.tls_statistics, field)
47 #define TLS_INC_STATS(net, field)                               \
48         SNMP_INC_STATS((net)->mib.tls_statistics, field)
49 #define TLS_DEC_STATS(net, field)                               \
50         SNMP_DEC_STATS((net)->mib.tls_statistics, field)
51
52 /* TLS records are maintained in 'struct tls_rec'. It stores the memory pages
53  * allocated or mapped for each TLS record. After encryption, the records are
54  * stores in a linked list.
55  */
56 struct tls_rec {
57         struct list_head list;
58         int tx_ready;
59         int tx_flags;
60
61         struct sk_msg msg_plaintext;
62         struct sk_msg msg_encrypted;
63
64         /* AAD | msg_plaintext.sg.data | sg_tag */
65         struct scatterlist sg_aead_in[2];
66         /* AAD | msg_encrypted.sg.data (data contains overhead for hdr & iv & tag) */
67         struct scatterlist sg_aead_out[2];
68
69         char content_type;
70         struct scatterlist sg_content_type;
71
72         char aad_space[TLS_AAD_SPACE_SIZE];
73         u8 iv_data[MAX_IV_SIZE];
74         struct aead_request aead_req;
75         u8 aead_req_ctx[];
76 };
77
78 int __net_init tls_proc_init(struct net *net);
79 void __net_exit tls_proc_fini(struct net *net);
80
81 struct tls_context *tls_ctx_create(struct sock *sk);
82 void tls_ctx_free(struct sock *sk, struct tls_context *ctx);
83 void update_sk_prot(struct sock *sk, struct tls_context *ctx);
84
85 int wait_on_pending_writer(struct sock *sk, long *timeo);
86 int tls_sk_query(struct sock *sk, int optname, char __user *optval,
87                  int __user *optlen);
88 int tls_sk_attach(struct sock *sk, int optname, char __user *optval,
89                   unsigned int optlen);
90 void tls_err_abort(struct sock *sk, int err);
91
92 int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx);
93 void tls_update_rx_zc_capable(struct tls_context *tls_ctx);
94 void tls_sw_strparser_arm(struct sock *sk, struct tls_context *ctx);
95 void tls_sw_strparser_done(struct tls_context *tls_ctx);
96 int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
97 int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
98                            int offset, size_t size, int flags);
99 int tls_sw_sendpage(struct sock *sk, struct page *page,
100                     int offset, size_t size, int flags);
101 void tls_sw_cancel_work_tx(struct tls_context *tls_ctx);
102 void tls_sw_release_resources_tx(struct sock *sk);
103 void tls_sw_free_ctx_tx(struct tls_context *tls_ctx);
104 void tls_sw_free_resources_rx(struct sock *sk);
105 void tls_sw_release_resources_rx(struct sock *sk);
106 void tls_sw_free_ctx_rx(struct tls_context *tls_ctx);
107 int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
108                    int flags, int *addr_len);
109 bool tls_sw_sock_is_readable(struct sock *sk);
110 ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
111                            struct pipe_inode_info *pipe,
112                            size_t len, unsigned int flags);
113
114 int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
115 int tls_device_sendpage(struct sock *sk, struct page *page,
116                         int offset, size_t size, int flags);
117 int tls_tx_records(struct sock *sk, int flags);
118
119 void tls_sw_write_space(struct sock *sk, struct tls_context *ctx);
120 void tls_device_write_space(struct sock *sk, struct tls_context *ctx);
121
122 int tls_process_cmsg(struct sock *sk, struct msghdr *msg,
123                      unsigned char *record_type);
124 int decrypt_skb(struct sock *sk, struct scatterlist *sgout);
125
126 int tls_sw_fallback_init(struct sock *sk,
127                          struct tls_offload_context_tx *offload_ctx,
128                          struct tls_crypto_info *crypto_info);
129
130 int tls_strp_msg_cow(struct tls_sw_context_rx *ctx);
131 struct sk_buff *tls_strp_msg_detach(struct tls_sw_context_rx *ctx);
132 int tls_strp_msg_hold(struct sock *sk, struct sk_buff *skb,
133                       struct sk_buff_head *dst);
134
135 static inline struct tls_msg *tls_msg(struct sk_buff *skb)
136 {
137         struct sk_skb_cb *scb = (struct sk_skb_cb *)skb->cb;
138
139         return &scb->tls;
140 }
141
142 static inline struct sk_buff *tls_strp_msg(struct tls_sw_context_rx *ctx)
143 {
144         return ctx->recv_pkt;
145 }
146
147 static inline bool tls_strp_msg_ready(struct tls_sw_context_rx *ctx)
148 {
149         return ctx->recv_pkt;
150 }
151
152 #ifdef CONFIG_TLS_DEVICE
153 int tls_device_init(void);
154 void tls_device_cleanup(void);
155 int tls_set_device_offload(struct sock *sk, struct tls_context *ctx);
156 void tls_device_free_resources_tx(struct sock *sk);
157 int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx);
158 void tls_device_offload_cleanup_rx(struct sock *sk);
159 void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq);
160 int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx);
161 #else
162 static inline int tls_device_init(void) { return 0; }
163 static inline void tls_device_cleanup(void) {}
164
165 static inline int
166 tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
167 {
168         return -EOPNOTSUPP;
169 }
170
171 static inline void tls_device_free_resources_tx(struct sock *sk) {}
172
173 static inline int
174 tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
175 {
176         return -EOPNOTSUPP;
177 }
178
179 static inline void tls_device_offload_cleanup_rx(struct sock *sk) {}
180 static inline void
181 tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq) {}
182
183 static inline int
184 tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx)
185 {
186         return 0;
187 }
188 #endif
189
190 int tls_push_sg(struct sock *sk, struct tls_context *ctx,
191                 struct scatterlist *sg, u16 first_offset,
192                 int flags);
193 int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
194                             int flags);
195 void tls_free_partial_record(struct sock *sk, struct tls_context *ctx);
196
197 static inline bool tls_is_partially_sent_record(struct tls_context *ctx)
198 {
199         return !!ctx->partially_sent_record;
200 }
201
202 static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx)
203 {
204         return tls_ctx->pending_open_record_frags;
205 }
206
207 static inline bool tls_bigint_increment(unsigned char *seq, int len)
208 {
209         int i;
210
211         for (i = len - 1; i >= 0; i--) {
212                 ++seq[i];
213                 if (seq[i] != 0)
214                         break;
215         }
216
217         return (i == -1);
218 }
219
220 static inline void tls_bigint_subtract(unsigned char *seq, int  n)
221 {
222         u64 rcd_sn;
223         __be64 *p;
224
225         BUILD_BUG_ON(TLS_MAX_REC_SEQ_SIZE != 8);
226
227         p = (__be64 *)seq;
228         rcd_sn = be64_to_cpu(*p);
229         *p = cpu_to_be64(rcd_sn - n);
230 }
231
232 static inline void
233 tls_advance_record_sn(struct sock *sk, struct tls_prot_info *prot,
234                       struct cipher_context *ctx)
235 {
236         if (tls_bigint_increment(ctx->rec_seq, prot->rec_seq_size))
237                 tls_err_abort(sk, -EBADMSG);
238
239         if (prot->version != TLS_1_3_VERSION &&
240             prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305)
241                 tls_bigint_increment(ctx->iv + prot->salt_size,
242                                      prot->iv_size);
243 }
244
245 static inline void
246 tls_xor_iv_with_seq(struct tls_prot_info *prot, char *iv, char *seq)
247 {
248         int i;
249
250         if (prot->version == TLS_1_3_VERSION ||
251             prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) {
252                 for (i = 0; i < 8; i++)
253                         iv[i + 4] ^= seq[i];
254         }
255 }
256
257 static inline void
258 tls_fill_prepend(struct tls_context *ctx, char *buf, size_t plaintext_len,
259                  unsigned char record_type)
260 {
261         struct tls_prot_info *prot = &ctx->prot_info;
262         size_t pkt_len, iv_size = prot->iv_size;
263
264         pkt_len = plaintext_len + prot->tag_size;
265         if (prot->version != TLS_1_3_VERSION &&
266             prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305) {
267                 pkt_len += iv_size;
268
269                 memcpy(buf + TLS_NONCE_OFFSET,
270                        ctx->tx.iv + prot->salt_size, iv_size);
271         }
272
273         /* we cover nonce explicit here as well, so buf should be of
274          * size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE
275          */
276         buf[0] = prot->version == TLS_1_3_VERSION ?
277                    TLS_RECORD_TYPE_DATA : record_type;
278         /* Note that VERSION must be TLS_1_2 for both TLS1.2 and TLS1.3 */
279         buf[1] = TLS_1_2_VERSION_MINOR;
280         buf[2] = TLS_1_2_VERSION_MAJOR;
281         /* we can use IV for nonce explicit according to spec */
282         buf[3] = pkt_len >> 8;
283         buf[4] = pkt_len & 0xFF;
284 }
285
286 static inline
287 void tls_make_aad(char *buf, size_t size, char *record_sequence,
288                   unsigned char record_type, struct tls_prot_info *prot)
289 {
290         if (prot->version != TLS_1_3_VERSION) {
291                 memcpy(buf, record_sequence, prot->rec_seq_size);
292                 buf += 8;
293         } else {
294                 size += prot->tag_size;
295         }
296
297         buf[0] = prot->version == TLS_1_3_VERSION ?
298                   TLS_RECORD_TYPE_DATA : record_type;
299         buf[1] = TLS_1_2_VERSION_MAJOR;
300         buf[2] = TLS_1_2_VERSION_MINOR;
301         buf[3] = size >> 8;
302         buf[4] = size & 0xFF;
303 }
304
305 #endif