File Index
Files and directories
Sign in
FILE

tls_device_fallback.c

File preview with syntax highlighting.

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