1 /* Copyright (c) 2018, Mellanox Technologies All rights reserved.
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:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
13 * - Redistributions of source code must retain the above
14 * copyright notice, this list of conditions and the following
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.
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
33 #include <crypto/aead.h>
34 #include <crypto/scatterwalk.h>
35 #include <net/ip6_checksum.h>
39 static void chain_to_walk(struct scatterlist *sg, struct scatter_walk *walk)
41 struct scatterlist *src = walk->sg;
42 int diff = walk->offset - src->offset;
44 sg_set_page(sg, sg_page(src),
45 src->length - diff, walk->offset);
47 scatterwalk_crypto_chain(sg, sg_next(src), 2);
50 static int tls_enc_record(struct aead_request *aead_req,
51 struct crypto_aead *aead, char *aad,
52 char *iv, __be64 rcd_sn,
53 struct scatter_walk *in,
54 struct scatter_walk *out, int *in_len,
55 struct tls_prot_info *prot)
57 unsigned char buf[TLS_HEADER_SIZE + MAX_IV_SIZE];
58 const struct tls_cipher_desc *cipher_desc;
59 struct scatterlist sg_in[3];
60 struct scatterlist sg_out[3];
61 unsigned int buf_size;
65 switch (prot->cipher_type) {
66 case TLS_CIPHER_AES_GCM_128:
67 case TLS_CIPHER_AES_GCM_256:
72 cipher_desc = get_cipher_desc(prot->cipher_type);
74 buf_size = TLS_HEADER_SIZE + cipher_desc->iv;
75 len = min_t(int, *in_len, buf_size);
77 scatterwalk_copychunks(buf, in, len, 0);
78 scatterwalk_copychunks(buf, out, len, 1);
84 scatterwalk_pagedone(in, 0, 1);
85 scatterwalk_pagedone(out, 1, 1);
87 len = buf[4] | (buf[3] << 8);
88 len -= cipher_desc->iv;
90 tls_make_aad(aad, len - cipher_desc->tag, (char *)&rcd_sn, buf[0], prot);
92 memcpy(iv + cipher_desc->salt, buf + TLS_HEADER_SIZE, cipher_desc->iv);
94 sg_init_table(sg_in, ARRAY_SIZE(sg_in));
95 sg_init_table(sg_out, ARRAY_SIZE(sg_out));
96 sg_set_buf(sg_in, aad, TLS_AAD_SPACE_SIZE);
97 sg_set_buf(sg_out, aad, TLS_AAD_SPACE_SIZE);
98 chain_to_walk(sg_in + 1, in);
99 chain_to_walk(sg_out + 1, out);
103 *in_len += cipher_desc->tag;
104 /* the input buffer doesn't contain the entire record.
105 * trim len accordingly. The resulting authentication tag
106 * will contain garbage, but we don't care, so we won't
107 * include any of it in the output skb
108 * Note that we assume the output buffer length
109 * is larger then input buffer length + tag size
118 scatterwalk_copychunks(NULL, in, len, 2);
119 scatterwalk_pagedone(in, 0, 1);
120 scatterwalk_copychunks(NULL, out, len, 2);
121 scatterwalk_pagedone(out, 1, 1);
124 len -= cipher_desc->tag;
125 aead_request_set_crypt(aead_req, sg_in, sg_out, len, iv);
127 rc = crypto_aead_encrypt(aead_req);
132 static void tls_init_aead_request(struct aead_request *aead_req,
133 struct crypto_aead *aead)
135 aead_request_set_tfm(aead_req, aead);
136 aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
139 static struct aead_request *tls_alloc_aead_request(struct crypto_aead *aead,
142 unsigned int req_size = sizeof(struct aead_request) +
143 crypto_aead_reqsize(aead);
144 struct aead_request *aead_req;
146 aead_req = kzalloc(req_size, flags);
148 tls_init_aead_request(aead_req, aead);
152 static int tls_enc_records(struct aead_request *aead_req,
153 struct crypto_aead *aead, struct scatterlist *sg_in,
154 struct scatterlist *sg_out, char *aad, char *iv,
155 u64 rcd_sn, int len, struct tls_prot_info *prot)
157 struct scatter_walk out, in;
160 scatterwalk_start(&in, sg_in);
161 scatterwalk_start(&out, sg_out);
164 rc = tls_enc_record(aead_req, aead, aad, iv,
165 cpu_to_be64(rcd_sn), &in, &out, &len, prot);
168 } while (rc == 0 && len);
170 scatterwalk_done(&in, 0, 0);
171 scatterwalk_done(&out, 1, 0);
176 /* Can't use icsk->icsk_af_ops->send_check here because the ip addresses
177 * might have been changed by NAT.
179 static void update_chksum(struct sk_buff *skb, int headln)
181 struct tcphdr *th = tcp_hdr(skb);
182 int datalen = skb->len - headln;
183 const struct ipv6hdr *ipv6h;
184 const struct iphdr *iph;
186 /* We only changed the payload so if we are using partial we don't
187 * need to update anything.
189 if (likely(skb->ip_summed == CHECKSUM_PARTIAL))
192 skb->ip_summed = CHECKSUM_PARTIAL;
193 skb->csum_start = skb_transport_header(skb) - skb->head;
194 skb->csum_offset = offsetof(struct tcphdr, check);
196 if (skb->sk->sk_family == AF_INET6) {
197 ipv6h = ipv6_hdr(skb);
198 th->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
199 datalen, IPPROTO_TCP, 0);
202 th->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, datalen,
207 static void complete_skb(struct sk_buff *nskb, struct sk_buff *skb, int headln)
209 struct sock *sk = skb->sk;
212 skb_copy_header(nskb, skb);
214 skb_put(nskb, skb->len);
215 memcpy(nskb->data, skb->data, headln);
217 nskb->destructor = skb->destructor;
219 skb->destructor = NULL;
222 update_chksum(nskb, headln);
224 /* sock_efree means skb must gone through skb_orphan_partial() */
225 if (nskb->destructor == sock_efree)
228 delta = nskb->truesize - skb->truesize;
229 if (likely(delta < 0))
230 WARN_ON_ONCE(refcount_sub_and_test(-delta, &sk->sk_wmem_alloc));
232 refcount_add(delta, &sk->sk_wmem_alloc);
235 /* This function may be called after the user socket is already
236 * closed so make sure we don't use anything freed during
237 * tls_sk_proto_close here
240 static int fill_sg_in(struct scatterlist *sg_in,
242 struct tls_offload_context_tx *ctx,
247 int tcp_payload_offset = skb_tcp_all_headers(skb);
248 int payload_len = skb->len - tcp_payload_offset;
249 u32 tcp_seq = ntohl(tcp_hdr(skb)->seq);
250 struct tls_record_info *record;
255 spin_lock_irqsave(&ctx->lock, flags);
256 record = tls_get_record(ctx, tcp_seq, rcd_sn);
258 spin_unlock_irqrestore(&ctx->lock, flags);
262 *sync_size = tcp_seq - tls_record_start_seq(record);
263 if (*sync_size < 0) {
264 int is_start_marker = tls_record_is_start_marker(record);
266 spin_unlock_irqrestore(&ctx->lock, flags);
267 /* This should only occur if the relevant record was
268 * already acked. In that case it should be ok
269 * to drop the packet and avoid retransmission.
271 * There is a corner case where the packet contains
272 * both an acked and a non-acked record.
273 * We currently don't handle that case and rely
274 * on TCP to retransmit a packet that doesn't contain
275 * already acked payload.
277 if (!is_start_marker)
282 remaining = *sync_size;
283 for (i = 0; remaining > 0; i++) {
284 skb_frag_t *frag = &record->frags[i];
286 __skb_frag_ref(frag);
287 sg_set_page(sg_in + i, skb_frag_page(frag),
288 skb_frag_size(frag), skb_frag_off(frag));
290 remaining -= skb_frag_size(frag);
293 sg_in[i].length += remaining;
297 spin_unlock_irqrestore(&ctx->lock, flags);
298 if (skb_to_sgvec(skb, &sg_in[i], tcp_payload_offset, payload_len) < 0)
304 static void fill_sg_out(struct scatterlist sg_out[3], void *buf,
305 struct tls_context *tls_ctx,
306 struct sk_buff *nskb,
307 int tcp_payload_offset,
312 const struct tls_cipher_desc *cipher_desc =
313 get_cipher_desc(tls_ctx->crypto_send.info.cipher_type);
315 sg_set_buf(&sg_out[0], dummy_buf, sync_size);
316 sg_set_buf(&sg_out[1], nskb->data + tcp_payload_offset, payload_len);
317 /* Add room for authentication tag produced by crypto */
318 dummy_buf += sync_size;
319 sg_set_buf(&sg_out[2], dummy_buf, cipher_desc->tag);
322 static struct sk_buff *tls_enc_skb(struct tls_context *tls_ctx,
323 struct scatterlist sg_out[3],
324 struct scatterlist *sg_in,
326 s32 sync_size, u64 rcd_sn)
328 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
329 int tcp_payload_offset = skb_tcp_all_headers(skb);
330 int payload_len = skb->len - tcp_payload_offset;
331 const struct tls_cipher_desc *cipher_desc;
332 void *buf, *iv, *aad, *dummy_buf, *salt;
333 struct aead_request *aead_req;
334 struct sk_buff *nskb = NULL;
337 aead_req = tls_alloc_aead_request(ctx->aead_send, GFP_ATOMIC);
341 switch (tls_ctx->crypto_send.info.cipher_type) {
342 case TLS_CIPHER_AES_GCM_128:
343 salt = tls_ctx->crypto_send.aes_gcm_128.salt;
345 case TLS_CIPHER_AES_GCM_256:
346 salt = tls_ctx->crypto_send.aes_gcm_256.salt;
351 cipher_desc = get_cipher_desc(tls_ctx->crypto_send.info.cipher_type);
352 buf_len = cipher_desc->salt + cipher_desc->iv + TLS_AAD_SPACE_SIZE +
353 sync_size + cipher_desc->tag;
354 buf = kmalloc(buf_len, GFP_ATOMIC);
359 memcpy(iv, salt, cipher_desc->salt);
360 aad = buf + cipher_desc->salt + cipher_desc->iv;
361 dummy_buf = aad + TLS_AAD_SPACE_SIZE;
363 nskb = alloc_skb(skb_headroom(skb) + skb->len, GFP_ATOMIC);
367 skb_reserve(nskb, skb_headroom(skb));
369 fill_sg_out(sg_out, buf, tls_ctx, nskb, tcp_payload_offset,
370 payload_len, sync_size, dummy_buf);
372 if (tls_enc_records(aead_req, ctx->aead_send, sg_in, sg_out, aad, iv,
373 rcd_sn, sync_size + payload_len,
374 &tls_ctx->prot_info) < 0)
377 complete_skb(nskb, skb, tcp_payload_offset);
379 /* validate_xmit_skb_list assumes that if the skb wasn't segmented
380 * nskb->prev will point to the skb itself
395 static struct sk_buff *tls_sw_fallback(struct sock *sk, struct sk_buff *skb)
397 int tcp_payload_offset = skb_tcp_all_headers(skb);
398 struct tls_context *tls_ctx = tls_get_ctx(sk);
399 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
400 int payload_len = skb->len - tcp_payload_offset;
401 struct scatterlist *sg_in, sg_out[3];
402 struct sk_buff *nskb = NULL;
403 int sg_in_max_elements;
409 * MAX_SKB_FRAGS in tls_record_info
410 * MAX_SKB_FRAGS + 1 in SKB head and frags.
412 sg_in_max_elements = 2 * MAX_SKB_FRAGS + 1;
417 sg_in = kmalloc_array(sg_in_max_elements, sizeof(*sg_in), GFP_ATOMIC);
421 sg_init_table(sg_in, sg_in_max_elements);
422 sg_init_table(sg_out, ARRAY_SIZE(sg_out));
424 if (fill_sg_in(sg_in, skb, ctx, &rcd_sn, &sync_size, &resync_sgs)) {
425 /* bypass packets before kernel TLS socket option was set */
426 if (sync_size < 0 && payload_len <= -sync_size)
431 nskb = tls_enc_skb(tls_ctx, sg_out, sg_in, skb, sync_size, rcd_sn);
435 put_page(sg_page(&sg_in[--resync_sgs]));
445 struct sk_buff *tls_validate_xmit_skb(struct sock *sk,
446 struct net_device *dev,
449 if (dev == rcu_dereference_bh(tls_get_ctx(sk)->netdev) ||
450 netif_is_bond_master(dev))
453 return tls_sw_fallback(sk, skb);
455 EXPORT_SYMBOL_GPL(tls_validate_xmit_skb);
457 struct sk_buff *tls_validate_xmit_skb_sw(struct sock *sk,
458 struct net_device *dev,
461 return tls_sw_fallback(sk, skb);
464 struct sk_buff *tls_encrypt_skb(struct sk_buff *skb)
466 return tls_sw_fallback(skb->sk, skb);
468 EXPORT_SYMBOL_GPL(tls_encrypt_skb);
470 int tls_sw_fallback_init(struct sock *sk,
471 struct tls_offload_context_tx *offload_ctx,
472 struct tls_crypto_info *crypto_info)
474 const struct tls_cipher_desc *cipher_desc;
477 cipher_desc = get_cipher_desc(crypto_info->cipher_type);
478 if (!cipher_desc || !cipher_desc->offloadable)
481 offload_ctx->aead_send =
482 crypto_alloc_aead(cipher_desc->cipher_name, 0, CRYPTO_ALG_ASYNC);
483 if (IS_ERR(offload_ctx->aead_send)) {
484 rc = PTR_ERR(offload_ctx->aead_send);
485 pr_err_ratelimited("crypto_alloc_aead failed rc=%d\n", rc);
486 offload_ctx->aead_send = NULL;
490 rc = crypto_aead_setkey(offload_ctx->aead_send,
491 crypto_info_key(crypto_info, cipher_desc),
496 rc = crypto_aead_setauthsize(offload_ctx->aead_send, cipher_desc->tag);
502 crypto_free_aead(offload_ctx->aead_send);