xref: /openbmc/linux/net/tls/tls_device_fallback.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1e8f69799SIlya Lesokhin /* Copyright (c) 2018, Mellanox Technologies All rights reserved.
2e8f69799SIlya Lesokhin  *
3e8f69799SIlya Lesokhin  * This software is available to you under a choice of one of two
4e8f69799SIlya Lesokhin  * licenses.  You may choose to be licensed under the terms of the GNU
5e8f69799SIlya Lesokhin  * General Public License (GPL) Version 2, available from the file
6e8f69799SIlya Lesokhin  * COPYING in the main directory of this source tree, or the
7e8f69799SIlya Lesokhin  * OpenIB.org BSD license below:
8e8f69799SIlya Lesokhin  *
9e8f69799SIlya Lesokhin  *     Redistribution and use in source and binary forms, with or
10e8f69799SIlya Lesokhin  *     without modification, are permitted provided that the following
11e8f69799SIlya Lesokhin  *     conditions are met:
12e8f69799SIlya Lesokhin  *
13e8f69799SIlya Lesokhin  *      - Redistributions of source code must retain the above
14e8f69799SIlya Lesokhin  *        copyright notice, this list of conditions and the following
15e8f69799SIlya Lesokhin  *        disclaimer.
16e8f69799SIlya Lesokhin  *
17e8f69799SIlya Lesokhin  *      - Redistributions in binary form must reproduce the above
18e8f69799SIlya Lesokhin  *        copyright notice, this list of conditions and the following
19e8f69799SIlya Lesokhin  *        disclaimer in the documentation and/or other materials
20e8f69799SIlya Lesokhin  *        provided with the distribution.
21e8f69799SIlya Lesokhin  *
22e8f69799SIlya Lesokhin  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23e8f69799SIlya Lesokhin  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24e8f69799SIlya Lesokhin  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25e8f69799SIlya Lesokhin  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26e8f69799SIlya Lesokhin  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27e8f69799SIlya Lesokhin  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28e8f69799SIlya Lesokhin  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29e8f69799SIlya Lesokhin  * SOFTWARE.
30e8f69799SIlya Lesokhin  */
31e8f69799SIlya Lesokhin 
32e8f69799SIlya Lesokhin #include <net/tls.h>
33e8f69799SIlya Lesokhin #include <crypto/aead.h>
34e8f69799SIlya Lesokhin #include <crypto/scatterwalk.h>
35e8f69799SIlya Lesokhin #include <net/ip6_checksum.h>
36e8f69799SIlya Lesokhin 
3758790314SJakub Kicinski #include "tls.h"
3858790314SJakub Kicinski 
chain_to_walk(struct scatterlist * sg,struct scatter_walk * walk)39e8f69799SIlya Lesokhin static void chain_to_walk(struct scatterlist *sg, struct scatter_walk *walk)
40e8f69799SIlya Lesokhin {
41e8f69799SIlya Lesokhin 	struct scatterlist *src = walk->sg;
42e8f69799SIlya Lesokhin 	int diff = walk->offset - src->offset;
43e8f69799SIlya Lesokhin 
44e8f69799SIlya Lesokhin 	sg_set_page(sg, sg_page(src),
45e8f69799SIlya Lesokhin 		    src->length - diff, walk->offset);
46e8f69799SIlya Lesokhin 
478c30fbe6SEric Biggers 	scatterwalk_crypto_chain(sg, sg_next(src), 2);
48e8f69799SIlya Lesokhin }
49e8f69799SIlya Lesokhin 
tls_enc_record(struct aead_request * aead_req,struct crypto_aead * aead,char * aad,char * iv,__be64 rcd_sn,struct scatter_walk * in,struct scatter_walk * out,int * in_len,struct tls_prot_info * prot)50e8f69799SIlya Lesokhin static int tls_enc_record(struct aead_request *aead_req,
51e8f69799SIlya Lesokhin 			  struct crypto_aead *aead, char *aad,
52e8f69799SIlya Lesokhin 			  char *iv, __be64 rcd_sn,
53e8f69799SIlya Lesokhin 			  struct scatter_walk *in,
546942a284SVadim Fedorenko 			  struct scatter_walk *out, int *in_len,
556942a284SVadim Fedorenko 			  struct tls_prot_info *prot)
56e8f69799SIlya Lesokhin {
57ea7a9d88SGal Pressman 	unsigned char buf[TLS_HEADER_SIZE + MAX_IV_SIZE];
588db44ab2SSabrina Dubroca 	const struct tls_cipher_desc *cipher_desc;
59e8f69799SIlya Lesokhin 	struct scatterlist sg_in[3];
60e8f69799SIlya Lesokhin 	struct scatterlist sg_out[3];
61ea7a9d88SGal Pressman 	unsigned int buf_size;
62e8f69799SIlya Lesokhin 	u16 len;
63e8f69799SIlya Lesokhin 	int rc;
64e8f69799SIlya Lesokhin 
65ea7a9d88SGal Pressman 	switch (prot->cipher_type) {
66ea7a9d88SGal Pressman 	case TLS_CIPHER_AES_GCM_128:
6756e5a6d3SGal Pressman 	case TLS_CIPHER_AES_GCM_256:
68ea7a9d88SGal Pressman 		break;
69ea7a9d88SGal Pressman 	default:
70ea7a9d88SGal Pressman 		return -EINVAL;
71ea7a9d88SGal Pressman 	}
728db44ab2SSabrina Dubroca 	cipher_desc = get_cipher_desc(prot->cipher_type);
73ea7a9d88SGal Pressman 
748db44ab2SSabrina Dubroca 	buf_size = TLS_HEADER_SIZE + cipher_desc->iv;
75ea7a9d88SGal Pressman 	len = min_t(int, *in_len, buf_size);
76e8f69799SIlya Lesokhin 
77e8f69799SIlya Lesokhin 	scatterwalk_copychunks(buf, in, len, 0);
78e8f69799SIlya Lesokhin 	scatterwalk_copychunks(buf, out, len, 1);
79e8f69799SIlya Lesokhin 
80e8f69799SIlya Lesokhin 	*in_len -= len;
81e8f69799SIlya Lesokhin 	if (!*in_len)
82e8f69799SIlya Lesokhin 		return 0;
83e8f69799SIlya Lesokhin 
84e8f69799SIlya Lesokhin 	scatterwalk_pagedone(in, 0, 1);
85e8f69799SIlya Lesokhin 	scatterwalk_pagedone(out, 1, 1);
86e8f69799SIlya Lesokhin 
87e8f69799SIlya Lesokhin 	len = buf[4] | (buf[3] << 8);
888db44ab2SSabrina Dubroca 	len -= cipher_desc->iv;
89e8f69799SIlya Lesokhin 
908db44ab2SSabrina Dubroca 	tls_make_aad(aad, len - cipher_desc->tag, (char *)&rcd_sn, buf[0], prot);
91e8f69799SIlya Lesokhin 
928db44ab2SSabrina Dubroca 	memcpy(iv + cipher_desc->salt, buf + TLS_HEADER_SIZE, cipher_desc->iv);
93e8f69799SIlya Lesokhin 
94e8f69799SIlya Lesokhin 	sg_init_table(sg_in, ARRAY_SIZE(sg_in));
95e8f69799SIlya Lesokhin 	sg_init_table(sg_out, ARRAY_SIZE(sg_out));
96e8f69799SIlya Lesokhin 	sg_set_buf(sg_in, aad, TLS_AAD_SPACE_SIZE);
97e8f69799SIlya Lesokhin 	sg_set_buf(sg_out, aad, TLS_AAD_SPACE_SIZE);
98e8f69799SIlya Lesokhin 	chain_to_walk(sg_in + 1, in);
99e8f69799SIlya Lesokhin 	chain_to_walk(sg_out + 1, out);
100e8f69799SIlya Lesokhin 
101e8f69799SIlya Lesokhin 	*in_len -= len;
102e8f69799SIlya Lesokhin 	if (*in_len < 0) {
1038db44ab2SSabrina Dubroca 		*in_len += cipher_desc->tag;
104e8f69799SIlya Lesokhin 		/* the input buffer doesn't contain the entire record.
105e8f69799SIlya Lesokhin 		 * trim len accordingly. The resulting authentication tag
106e8f69799SIlya Lesokhin 		 * will contain garbage, but we don't care, so we won't
107e8f69799SIlya Lesokhin 		 * include any of it in the output skb
108e8f69799SIlya Lesokhin 		 * Note that we assume the output buffer length
109e8f69799SIlya Lesokhin 		 * is larger then input buffer length + tag size
110e8f69799SIlya Lesokhin 		 */
111e8f69799SIlya Lesokhin 		if (*in_len < 0)
112e8f69799SIlya Lesokhin 			len += *in_len;
113e8f69799SIlya Lesokhin 
114e8f69799SIlya Lesokhin 		*in_len = 0;
115e8f69799SIlya Lesokhin 	}
116e8f69799SIlya Lesokhin 
117e8f69799SIlya Lesokhin 	if (*in_len) {
118e8f69799SIlya Lesokhin 		scatterwalk_copychunks(NULL, in, len, 2);
119e8f69799SIlya Lesokhin 		scatterwalk_pagedone(in, 0, 1);
120e8f69799SIlya Lesokhin 		scatterwalk_copychunks(NULL, out, len, 2);
121e8f69799SIlya Lesokhin 		scatterwalk_pagedone(out, 1, 1);
122e8f69799SIlya Lesokhin 	}
123e8f69799SIlya Lesokhin 
1248db44ab2SSabrina Dubroca 	len -= cipher_desc->tag;
125e8f69799SIlya Lesokhin 	aead_request_set_crypt(aead_req, sg_in, sg_out, len, iv);
126e8f69799SIlya Lesokhin 
127e8f69799SIlya Lesokhin 	rc = crypto_aead_encrypt(aead_req);
128e8f69799SIlya Lesokhin 
129e8f69799SIlya Lesokhin 	return rc;
130e8f69799SIlya Lesokhin }
131e8f69799SIlya Lesokhin 
tls_init_aead_request(struct aead_request * aead_req,struct crypto_aead * aead)132e8f69799SIlya Lesokhin static void tls_init_aead_request(struct aead_request *aead_req,
133e8f69799SIlya Lesokhin 				  struct crypto_aead *aead)
134e8f69799SIlya Lesokhin {
135e8f69799SIlya Lesokhin 	aead_request_set_tfm(aead_req, aead);
136e8f69799SIlya Lesokhin 	aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
137e8f69799SIlya Lesokhin }
138e8f69799SIlya Lesokhin 
tls_alloc_aead_request(struct crypto_aead * aead,gfp_t flags)139e8f69799SIlya Lesokhin static struct aead_request *tls_alloc_aead_request(struct crypto_aead *aead,
140e8f69799SIlya Lesokhin 						   gfp_t flags)
141e8f69799SIlya Lesokhin {
142e8f69799SIlya Lesokhin 	unsigned int req_size = sizeof(struct aead_request) +
143e8f69799SIlya Lesokhin 		crypto_aead_reqsize(aead);
144e8f69799SIlya Lesokhin 	struct aead_request *aead_req;
145e8f69799SIlya Lesokhin 
146e8f69799SIlya Lesokhin 	aead_req = kzalloc(req_size, flags);
147e8f69799SIlya Lesokhin 	if (aead_req)
148e8f69799SIlya Lesokhin 		tls_init_aead_request(aead_req, aead);
149e8f69799SIlya Lesokhin 	return aead_req;
150e8f69799SIlya Lesokhin }
151e8f69799SIlya Lesokhin 
tls_enc_records(struct aead_request * aead_req,struct crypto_aead * aead,struct scatterlist * sg_in,struct scatterlist * sg_out,char * aad,char * iv,u64 rcd_sn,int len,struct tls_prot_info * prot)152e8f69799SIlya Lesokhin static int tls_enc_records(struct aead_request *aead_req,
153e8f69799SIlya Lesokhin 			   struct crypto_aead *aead, struct scatterlist *sg_in,
154e8f69799SIlya Lesokhin 			   struct scatterlist *sg_out, char *aad, char *iv,
1556942a284SVadim Fedorenko 			   u64 rcd_sn, int len, struct tls_prot_info *prot)
156e8f69799SIlya Lesokhin {
157e8f69799SIlya Lesokhin 	struct scatter_walk out, in;
158e8f69799SIlya Lesokhin 	int rc;
159e8f69799SIlya Lesokhin 
160e8f69799SIlya Lesokhin 	scatterwalk_start(&in, sg_in);
161e8f69799SIlya Lesokhin 	scatterwalk_start(&out, sg_out);
162e8f69799SIlya Lesokhin 
163e8f69799SIlya Lesokhin 	do {
164e8f69799SIlya Lesokhin 		rc = tls_enc_record(aead_req, aead, aad, iv,
1656942a284SVadim Fedorenko 				    cpu_to_be64(rcd_sn), &in, &out, &len, prot);
166e8f69799SIlya Lesokhin 		rcd_sn++;
167e8f69799SIlya Lesokhin 
168e8f69799SIlya Lesokhin 	} while (rc == 0 && len);
169e8f69799SIlya Lesokhin 
170e8f69799SIlya Lesokhin 	scatterwalk_done(&in, 0, 0);
171e8f69799SIlya Lesokhin 	scatterwalk_done(&out, 1, 0);
172e8f69799SIlya Lesokhin 
173e8f69799SIlya Lesokhin 	return rc;
174e8f69799SIlya Lesokhin }
175e8f69799SIlya Lesokhin 
176e8f69799SIlya Lesokhin /* Can't use icsk->icsk_af_ops->send_check here because the ip addresses
177e8f69799SIlya Lesokhin  * might have been changed by NAT.
178e8f69799SIlya Lesokhin  */
update_chksum(struct sk_buff * skb,int headln)179e8f69799SIlya Lesokhin static void update_chksum(struct sk_buff *skb, int headln)
180e8f69799SIlya Lesokhin {
181e8f69799SIlya Lesokhin 	struct tcphdr *th = tcp_hdr(skb);
182e8f69799SIlya Lesokhin 	int datalen = skb->len - headln;
183e8f69799SIlya Lesokhin 	const struct ipv6hdr *ipv6h;
184e8f69799SIlya Lesokhin 	const struct iphdr *iph;
185e8f69799SIlya Lesokhin 
186e8f69799SIlya Lesokhin 	/* We only changed the payload so if we are using partial we don't
187e8f69799SIlya Lesokhin 	 * need to update anything.
188e8f69799SIlya Lesokhin 	 */
189e8f69799SIlya Lesokhin 	if (likely(skb->ip_summed == CHECKSUM_PARTIAL))
190e8f69799SIlya Lesokhin 		return;
191e8f69799SIlya Lesokhin 
192e8f69799SIlya Lesokhin 	skb->ip_summed = CHECKSUM_PARTIAL;
193e8f69799SIlya Lesokhin 	skb->csum_start = skb_transport_header(skb) - skb->head;
194e8f69799SIlya Lesokhin 	skb->csum_offset = offsetof(struct tcphdr, check);
195e8f69799SIlya Lesokhin 
196e8f69799SIlya Lesokhin 	if (skb->sk->sk_family == AF_INET6) {
197e8f69799SIlya Lesokhin 		ipv6h = ipv6_hdr(skb);
198e8f69799SIlya Lesokhin 		th->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
199e8f69799SIlya Lesokhin 					     datalen, IPPROTO_TCP, 0);
200e8f69799SIlya Lesokhin 	} else {
201e8f69799SIlya Lesokhin 		iph = ip_hdr(skb);
202e8f69799SIlya Lesokhin 		th->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, datalen,
203e8f69799SIlya Lesokhin 					       IPPROTO_TCP, 0);
204e8f69799SIlya Lesokhin 	}
205e8f69799SIlya Lesokhin }
206e8f69799SIlya Lesokhin 
complete_skb(struct sk_buff * nskb,struct sk_buff * skb,int headln)207e8f69799SIlya Lesokhin static void complete_skb(struct sk_buff *nskb, struct sk_buff *skb, int headln)
208e8f69799SIlya Lesokhin {
2099188d5caSJakub Kicinski 	struct sock *sk = skb->sk;
2109188d5caSJakub Kicinski 	int delta;
2119188d5caSJakub Kicinski 
212e8f69799SIlya Lesokhin 	skb_copy_header(nskb, skb);
213e8f69799SIlya Lesokhin 
214e8f69799SIlya Lesokhin 	skb_put(nskb, skb->len);
215e8f69799SIlya Lesokhin 	memcpy(nskb->data, skb->data, headln);
216e8f69799SIlya Lesokhin 
217e8f69799SIlya Lesokhin 	nskb->destructor = skb->destructor;
2189188d5caSJakub Kicinski 	nskb->sk = sk;
219e8f69799SIlya Lesokhin 	skb->destructor = NULL;
220e8f69799SIlya Lesokhin 	skb->sk = NULL;
2219188d5caSJakub Kicinski 
2222dcb0033SJakub Kicinski 	update_chksum(nskb, headln);
2232dcb0033SJakub Kicinski 
2245c4b4608SJakub Kicinski 	/* sock_efree means skb must gone through skb_orphan_partial() */
2255c4b4608SJakub Kicinski 	if (nskb->destructor == sock_efree)
2265c4b4608SJakub Kicinski 		return;
2275c4b4608SJakub Kicinski 
2289188d5caSJakub Kicinski 	delta = nskb->truesize - skb->truesize;
2299188d5caSJakub Kicinski 	if (likely(delta < 0))
2309188d5caSJakub Kicinski 		WARN_ON_ONCE(refcount_sub_and_test(-delta, &sk->sk_wmem_alloc));
2319188d5caSJakub Kicinski 	else if (delta)
2329188d5caSJakub Kicinski 		refcount_add(delta, &sk->sk_wmem_alloc);
233e8f69799SIlya Lesokhin }
234e8f69799SIlya Lesokhin 
235e8f69799SIlya Lesokhin /* This function may be called after the user socket is already
236e8f69799SIlya Lesokhin  * closed so make sure we don't use anything freed during
237e8f69799SIlya Lesokhin  * tls_sk_proto_close here
238e8f69799SIlya Lesokhin  */
239e8f69799SIlya Lesokhin 
fill_sg_in(struct scatterlist * sg_in,struct sk_buff * skb,struct tls_offload_context_tx * ctx,u64 * rcd_sn,s32 * sync_size,int * resync_sgs)240e8f69799SIlya Lesokhin static int fill_sg_in(struct scatterlist *sg_in,
241e8f69799SIlya Lesokhin 		      struct sk_buff *skb,
242d80a1b9dSBoris Pismenny 		      struct tls_offload_context_tx *ctx,
243e8f69799SIlya Lesokhin 		      u64 *rcd_sn,
244e8f69799SIlya Lesokhin 		      s32 *sync_size,
245e8f69799SIlya Lesokhin 		      int *resync_sgs)
246e8f69799SIlya Lesokhin {
247504148feSEric Dumazet 	int tcp_payload_offset = skb_tcp_all_headers(skb);
248e8f69799SIlya Lesokhin 	int payload_len = skb->len - tcp_payload_offset;
249e8f69799SIlya Lesokhin 	u32 tcp_seq = ntohl(tcp_hdr(skb)->seq);
250e8f69799SIlya Lesokhin 	struct tls_record_info *record;
251e8f69799SIlya Lesokhin 	unsigned long flags;
252e8f69799SIlya Lesokhin 	int remaining;
253e8f69799SIlya Lesokhin 	int i;
254e8f69799SIlya Lesokhin 
255e8f69799SIlya Lesokhin 	spin_lock_irqsave(&ctx->lock, flags);
256e8f69799SIlya Lesokhin 	record = tls_get_record(ctx, tcp_seq, rcd_sn);
257e8f69799SIlya Lesokhin 	if (!record) {
258e8f69799SIlya Lesokhin 		spin_unlock_irqrestore(&ctx->lock, flags);
259e8f69799SIlya Lesokhin 		return -EINVAL;
260e8f69799SIlya Lesokhin 	}
261e8f69799SIlya Lesokhin 
262e8f69799SIlya Lesokhin 	*sync_size = tcp_seq - tls_record_start_seq(record);
263e8f69799SIlya Lesokhin 	if (*sync_size < 0) {
264e8f69799SIlya Lesokhin 		int is_start_marker = tls_record_is_start_marker(record);
265e8f69799SIlya Lesokhin 
266e8f69799SIlya Lesokhin 		spin_unlock_irqrestore(&ctx->lock, flags);
267e8f69799SIlya Lesokhin 		/* This should only occur if the relevant record was
268e8f69799SIlya Lesokhin 		 * already acked. In that case it should be ok
269e8f69799SIlya Lesokhin 		 * to drop the packet and avoid retransmission.
270e8f69799SIlya Lesokhin 		 *
271e8f69799SIlya Lesokhin 		 * There is a corner case where the packet contains
272e8f69799SIlya Lesokhin 		 * both an acked and a non-acked record.
273e8f69799SIlya Lesokhin 		 * We currently don't handle that case and rely
274a0e128efSYueh-Shun Li 		 * on TCP to retransmit a packet that doesn't contain
275e8f69799SIlya Lesokhin 		 * already acked payload.
276e8f69799SIlya Lesokhin 		 */
277e8f69799SIlya Lesokhin 		if (!is_start_marker)
278e8f69799SIlya Lesokhin 			*sync_size = 0;
279e8f69799SIlya Lesokhin 		return -EINVAL;
280e8f69799SIlya Lesokhin 	}
281e8f69799SIlya Lesokhin 
282e8f69799SIlya Lesokhin 	remaining = *sync_size;
283e8f69799SIlya Lesokhin 	for (i = 0; remaining > 0; i++) {
284e8f69799SIlya Lesokhin 		skb_frag_t *frag = &record->frags[i];
285e8f69799SIlya Lesokhin 
286e8f69799SIlya Lesokhin 		__skb_frag_ref(frag);
287e8f69799SIlya Lesokhin 		sg_set_page(sg_in + i, skb_frag_page(frag),
288b54c9d5bSJonathan Lemon 			    skb_frag_size(frag), skb_frag_off(frag));
289e8f69799SIlya Lesokhin 
290e8f69799SIlya Lesokhin 		remaining -= skb_frag_size(frag);
291e8f69799SIlya Lesokhin 
292e8f69799SIlya Lesokhin 		if (remaining < 0)
293e8f69799SIlya Lesokhin 			sg_in[i].length += remaining;
294e8f69799SIlya Lesokhin 	}
295e8f69799SIlya Lesokhin 	*resync_sgs = i;
296e8f69799SIlya Lesokhin 
297e8f69799SIlya Lesokhin 	spin_unlock_irqrestore(&ctx->lock, flags);
298e8f69799SIlya Lesokhin 	if (skb_to_sgvec(skb, &sg_in[i], tcp_payload_offset, payload_len) < 0)
299e8f69799SIlya Lesokhin 		return -EINVAL;
300e8f69799SIlya Lesokhin 
301e8f69799SIlya Lesokhin 	return 0;
302e8f69799SIlya Lesokhin }
303e8f69799SIlya Lesokhin 
fill_sg_out(struct scatterlist sg_out[3],void * buf,struct tls_context * tls_ctx,struct sk_buff * nskb,int tcp_payload_offset,int payload_len,int sync_size,void * dummy_buf)304e8f69799SIlya Lesokhin static void fill_sg_out(struct scatterlist sg_out[3], void *buf,
305e8f69799SIlya Lesokhin 			struct tls_context *tls_ctx,
306e8f69799SIlya Lesokhin 			struct sk_buff *nskb,
307e8f69799SIlya Lesokhin 			int tcp_payload_offset,
308e8f69799SIlya Lesokhin 			int payload_len,
309e8f69799SIlya Lesokhin 			int sync_size,
310e8f69799SIlya Lesokhin 			void *dummy_buf)
311e8f69799SIlya Lesokhin {
3128db44ab2SSabrina Dubroca 	const struct tls_cipher_desc *cipher_desc =
3138db44ab2SSabrina Dubroca 		get_cipher_desc(tls_ctx->crypto_send.info.cipher_type);
314ea7a9d88SGal Pressman 
315e8f69799SIlya Lesokhin 	sg_set_buf(&sg_out[0], dummy_buf, sync_size);
316e8f69799SIlya Lesokhin 	sg_set_buf(&sg_out[1], nskb->data + tcp_payload_offset, payload_len);
317e8f69799SIlya Lesokhin 	/* Add room for authentication tag produced by crypto */
318e8f69799SIlya Lesokhin 	dummy_buf += sync_size;
3198db44ab2SSabrina Dubroca 	sg_set_buf(&sg_out[2], dummy_buf, cipher_desc->tag);
320e8f69799SIlya Lesokhin }
321e8f69799SIlya Lesokhin 
tls_enc_skb(struct tls_context * tls_ctx,struct scatterlist sg_out[3],struct scatterlist * sg_in,struct sk_buff * skb,s32 sync_size,u64 rcd_sn)322e8f69799SIlya Lesokhin static struct sk_buff *tls_enc_skb(struct tls_context *tls_ctx,
323e8f69799SIlya Lesokhin 				   struct scatterlist sg_out[3],
324e8f69799SIlya Lesokhin 				   struct scatterlist *sg_in,
325e8f69799SIlya Lesokhin 				   struct sk_buff *skb,
326e8f69799SIlya Lesokhin 				   s32 sync_size, u64 rcd_sn)
327e8f69799SIlya Lesokhin {
328d80a1b9dSBoris Pismenny 	struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
329504148feSEric Dumazet 	int tcp_payload_offset = skb_tcp_all_headers(skb);
330e8f69799SIlya Lesokhin 	int payload_len = skb->len - tcp_payload_offset;
3318db44ab2SSabrina Dubroca 	const struct tls_cipher_desc *cipher_desc;
332ea7a9d88SGal Pressman 	void *buf, *iv, *aad, *dummy_buf, *salt;
333e8f69799SIlya Lesokhin 	struct aead_request *aead_req;
334e8f69799SIlya Lesokhin 	struct sk_buff *nskb = NULL;
335e8f69799SIlya Lesokhin 	int buf_len;
336e8f69799SIlya Lesokhin 
337e8f69799SIlya Lesokhin 	aead_req = tls_alloc_aead_request(ctx->aead_send, GFP_ATOMIC);
338e8f69799SIlya Lesokhin 	if (!aead_req)
339e8f69799SIlya Lesokhin 		return NULL;
340e8f69799SIlya Lesokhin 
341ea7a9d88SGal Pressman 	switch (tls_ctx->crypto_send.info.cipher_type) {
342ea7a9d88SGal Pressman 	case TLS_CIPHER_AES_GCM_128:
343ea7a9d88SGal Pressman 		salt = tls_ctx->crypto_send.aes_gcm_128.salt;
344ea7a9d88SGal Pressman 		break;
34556e5a6d3SGal Pressman 	case TLS_CIPHER_AES_GCM_256:
34656e5a6d3SGal Pressman 		salt = tls_ctx->crypto_send.aes_gcm_256.salt;
34756e5a6d3SGal Pressman 		break;
348ea7a9d88SGal Pressman 	default:
3490834ced6SYu Liao 		goto free_req;
350ea7a9d88SGal Pressman 	}
3518db44ab2SSabrina Dubroca 	cipher_desc = get_cipher_desc(tls_ctx->crypto_send.info.cipher_type);
3528db44ab2SSabrina Dubroca 	buf_len = cipher_desc->salt + cipher_desc->iv + TLS_AAD_SPACE_SIZE +
3538db44ab2SSabrina Dubroca 		  sync_size + cipher_desc->tag;
354e8f69799SIlya Lesokhin 	buf = kmalloc(buf_len, GFP_ATOMIC);
355e8f69799SIlya Lesokhin 	if (!buf)
356e8f69799SIlya Lesokhin 		goto free_req;
357e8f69799SIlya Lesokhin 
358e8f69799SIlya Lesokhin 	iv = buf;
3598db44ab2SSabrina Dubroca 	memcpy(iv, salt, cipher_desc->salt);
3608db44ab2SSabrina Dubroca 	aad = buf + cipher_desc->salt + cipher_desc->iv;
361e8f69799SIlya Lesokhin 	dummy_buf = aad + TLS_AAD_SPACE_SIZE;
362e8f69799SIlya Lesokhin 
363e8f69799SIlya Lesokhin 	nskb = alloc_skb(skb_headroom(skb) + skb->len, GFP_ATOMIC);
364e8f69799SIlya Lesokhin 	if (!nskb)
365e8f69799SIlya Lesokhin 		goto free_buf;
366e8f69799SIlya Lesokhin 
367e8f69799SIlya Lesokhin 	skb_reserve(nskb, skb_headroom(skb));
368e8f69799SIlya Lesokhin 
369e8f69799SIlya Lesokhin 	fill_sg_out(sg_out, buf, tls_ctx, nskb, tcp_payload_offset,
370e8f69799SIlya Lesokhin 		    payload_len, sync_size, dummy_buf);
371e8f69799SIlya Lesokhin 
372e8f69799SIlya Lesokhin 	if (tls_enc_records(aead_req, ctx->aead_send, sg_in, sg_out, aad, iv,
3736942a284SVadim Fedorenko 			    rcd_sn, sync_size + payload_len,
3746942a284SVadim Fedorenko 			    &tls_ctx->prot_info) < 0)
375e8f69799SIlya Lesokhin 		goto free_nskb;
376e8f69799SIlya Lesokhin 
377e8f69799SIlya Lesokhin 	complete_skb(nskb, skb, tcp_payload_offset);
378e8f69799SIlya Lesokhin 
379e8f69799SIlya Lesokhin 	/* validate_xmit_skb_list assumes that if the skb wasn't segmented
380e8f69799SIlya Lesokhin 	 * nskb->prev will point to the skb itself
381e8f69799SIlya Lesokhin 	 */
382e8f69799SIlya Lesokhin 	nskb->prev = nskb;
383e8f69799SIlya Lesokhin 
384e8f69799SIlya Lesokhin free_buf:
385e8f69799SIlya Lesokhin 	kfree(buf);
386e8f69799SIlya Lesokhin free_req:
387e8f69799SIlya Lesokhin 	kfree(aead_req);
388e8f69799SIlya Lesokhin 	return nskb;
389e8f69799SIlya Lesokhin free_nskb:
390e8f69799SIlya Lesokhin 	kfree_skb(nskb);
391e8f69799SIlya Lesokhin 	nskb = NULL;
392e8f69799SIlya Lesokhin 	goto free_buf;
393e8f69799SIlya Lesokhin }
394e8f69799SIlya Lesokhin 
tls_sw_fallback(struct sock * sk,struct sk_buff * skb)395e8f69799SIlya Lesokhin static struct sk_buff *tls_sw_fallback(struct sock *sk, struct sk_buff *skb)
396e8f69799SIlya Lesokhin {
397504148feSEric Dumazet 	int tcp_payload_offset = skb_tcp_all_headers(skb);
398e8f69799SIlya Lesokhin 	struct tls_context *tls_ctx = tls_get_ctx(sk);
399d80a1b9dSBoris Pismenny 	struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
400e8f69799SIlya Lesokhin 	int payload_len = skb->len - tcp_payload_offset;
401e8f69799SIlya Lesokhin 	struct scatterlist *sg_in, sg_out[3];
402e8f69799SIlya Lesokhin 	struct sk_buff *nskb = NULL;
403e8f69799SIlya Lesokhin 	int sg_in_max_elements;
404e8f69799SIlya Lesokhin 	int resync_sgs = 0;
405e8f69799SIlya Lesokhin 	s32 sync_size = 0;
406e8f69799SIlya Lesokhin 	u64 rcd_sn;
407e8f69799SIlya Lesokhin 
408e8f69799SIlya Lesokhin 	/* worst case is:
409e8f69799SIlya Lesokhin 	 * MAX_SKB_FRAGS in tls_record_info
410e8f69799SIlya Lesokhin 	 * MAX_SKB_FRAGS + 1 in SKB head and frags.
411e8f69799SIlya Lesokhin 	 */
412e8f69799SIlya Lesokhin 	sg_in_max_elements = 2 * MAX_SKB_FRAGS + 1;
413e8f69799SIlya Lesokhin 
414e8f69799SIlya Lesokhin 	if (!payload_len)
415e8f69799SIlya Lesokhin 		return skb;
416e8f69799SIlya Lesokhin 
417e8f69799SIlya Lesokhin 	sg_in = kmalloc_array(sg_in_max_elements, sizeof(*sg_in), GFP_ATOMIC);
418e8f69799SIlya Lesokhin 	if (!sg_in)
419e8f69799SIlya Lesokhin 		goto free_orig;
420e8f69799SIlya Lesokhin 
421e8f69799SIlya Lesokhin 	sg_init_table(sg_in, sg_in_max_elements);
422e8f69799SIlya Lesokhin 	sg_init_table(sg_out, ARRAY_SIZE(sg_out));
423e8f69799SIlya Lesokhin 
424e8f69799SIlya Lesokhin 	if (fill_sg_in(sg_in, skb, ctx, &rcd_sn, &sync_size, &resync_sgs)) {
425e8f69799SIlya Lesokhin 		/* bypass packets before kernel TLS socket option was set */
426e8f69799SIlya Lesokhin 		if (sync_size < 0 && payload_len <= -sync_size)
427e8f69799SIlya Lesokhin 			nskb = skb_get(skb);
428e8f69799SIlya Lesokhin 		goto put_sg;
429e8f69799SIlya Lesokhin 	}
430e8f69799SIlya Lesokhin 
431e8f69799SIlya Lesokhin 	nskb = tls_enc_skb(tls_ctx, sg_out, sg_in, skb, sync_size, rcd_sn);
432e8f69799SIlya Lesokhin 
433e8f69799SIlya Lesokhin put_sg:
434e8f69799SIlya Lesokhin 	while (resync_sgs)
435e8f69799SIlya Lesokhin 		put_page(sg_page(&sg_in[--resync_sgs]));
436e8f69799SIlya Lesokhin 	kfree(sg_in);
437e8f69799SIlya Lesokhin free_orig:
43887b11e06SJakub Kicinski 	if (nskb)
43987b11e06SJakub Kicinski 		consume_skb(skb);
44087b11e06SJakub Kicinski 	else
441e8f69799SIlya Lesokhin 		kfree_skb(skb);
442e8f69799SIlya Lesokhin 	return nskb;
443e8f69799SIlya Lesokhin }
444e8f69799SIlya Lesokhin 
tls_validate_xmit_skb(struct sock * sk,struct net_device * dev,struct sk_buff * skb)445e8f69799SIlya Lesokhin struct sk_buff *tls_validate_xmit_skb(struct sock *sk,
446e8f69799SIlya Lesokhin 				      struct net_device *dev,
447e8f69799SIlya Lesokhin 				      struct sk_buff *skb)
448e8f69799SIlya Lesokhin {
44994ce3b64SMaxim Mikityanskiy 	if (dev == rcu_dereference_bh(tls_get_ctx(sk)->netdev) ||
45094ce3b64SMaxim Mikityanskiy 	    netif_is_bond_master(dev))
451e8f69799SIlya Lesokhin 		return skb;
452e8f69799SIlya Lesokhin 
453e8f69799SIlya Lesokhin 	return tls_sw_fallback(sk, skb);
454e8f69799SIlya Lesokhin }
4554799ac81SBoris Pismenny EXPORT_SYMBOL_GPL(tls_validate_xmit_skb);
456e8f69799SIlya Lesokhin 
tls_validate_xmit_skb_sw(struct sock * sk,struct net_device * dev,struct sk_buff * skb)457c55dcdd4SMaxim Mikityanskiy struct sk_buff *tls_validate_xmit_skb_sw(struct sock *sk,
458c55dcdd4SMaxim Mikityanskiy 					 struct net_device *dev,
459c55dcdd4SMaxim Mikityanskiy 					 struct sk_buff *skb)
460c55dcdd4SMaxim Mikityanskiy {
461c55dcdd4SMaxim Mikityanskiy 	return tls_sw_fallback(sk, skb);
462c55dcdd4SMaxim Mikityanskiy }
463c55dcdd4SMaxim Mikityanskiy 
tls_encrypt_skb(struct sk_buff * skb)464b9727d7fSDirk van der Merwe struct sk_buff *tls_encrypt_skb(struct sk_buff *skb)
465b9727d7fSDirk van der Merwe {
466b9727d7fSDirk van der Merwe 	return tls_sw_fallback(skb->sk, skb);
467b9727d7fSDirk van der Merwe }
468b9727d7fSDirk van der Merwe EXPORT_SYMBOL_GPL(tls_encrypt_skb);
469b9727d7fSDirk van der Merwe 
tls_sw_fallback_init(struct sock * sk,struct tls_offload_context_tx * offload_ctx,struct tls_crypto_info * crypto_info)470e8f69799SIlya Lesokhin int tls_sw_fallback_init(struct sock *sk,
471d80a1b9dSBoris Pismenny 			 struct tls_offload_context_tx *offload_ctx,
472e8f69799SIlya Lesokhin 			 struct tls_crypto_info *crypto_info)
473e8f69799SIlya Lesokhin {
4748db44ab2SSabrina Dubroca 	const struct tls_cipher_desc *cipher_desc;
475e8f69799SIlya Lesokhin 	int rc;
476e8f69799SIlya Lesokhin 
4778db44ab2SSabrina Dubroca 	cipher_desc = get_cipher_desc(crypto_info->cipher_type);
478*e907277aSSabrina Dubroca 	if (!cipher_desc || !cipher_desc->offloadable)
479*e907277aSSabrina Dubroca 		return -EINVAL;
480e8f69799SIlya Lesokhin 
481d2322cf5SSabrina Dubroca 	offload_ctx->aead_send =
482*e907277aSSabrina Dubroca 	    crypto_alloc_aead(cipher_desc->cipher_name, 0, CRYPTO_ALG_ASYNC);
483d2322cf5SSabrina Dubroca 	if (IS_ERR(offload_ctx->aead_send)) {
484d2322cf5SSabrina Dubroca 		rc = PTR_ERR(offload_ctx->aead_send);
485d2322cf5SSabrina Dubroca 		pr_err_ratelimited("crypto_alloc_aead failed rc=%d\n", rc);
486d2322cf5SSabrina Dubroca 		offload_ctx->aead_send = NULL;
487d2322cf5SSabrina Dubroca 		goto err_out;
488d2322cf5SSabrina Dubroca 	}
489d2322cf5SSabrina Dubroca 
490*e907277aSSabrina Dubroca 	rc = crypto_aead_setkey(offload_ctx->aead_send,
491*e907277aSSabrina Dubroca 				crypto_info_key(crypto_info, cipher_desc),
492*e907277aSSabrina Dubroca 				cipher_desc->key);
493e8f69799SIlya Lesokhin 	if (rc)
494e8f69799SIlya Lesokhin 		goto free_aead;
495e8f69799SIlya Lesokhin 
4968db44ab2SSabrina Dubroca 	rc = crypto_aead_setauthsize(offload_ctx->aead_send, cipher_desc->tag);
497e8f69799SIlya Lesokhin 	if (rc)
498e8f69799SIlya Lesokhin 		goto free_aead;
499e8f69799SIlya Lesokhin 
500e8f69799SIlya Lesokhin 	return 0;
501e8f69799SIlya Lesokhin free_aead:
502e8f69799SIlya Lesokhin 	crypto_free_aead(offload_ctx->aead_send);
503e8f69799SIlya Lesokhin err_out:
504e8f69799SIlya Lesokhin 	return rc;
505e8f69799SIlya Lesokhin }
506