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 37e8f69799SIlya Lesokhin static void chain_to_walk(struct scatterlist *sg, struct scatter_walk *walk) 38e8f69799SIlya Lesokhin { 39e8f69799SIlya Lesokhin struct scatterlist *src = walk->sg; 40e8f69799SIlya Lesokhin int diff = walk->offset - src->offset; 41e8f69799SIlya Lesokhin 42e8f69799SIlya Lesokhin sg_set_page(sg, sg_page(src), 43e8f69799SIlya Lesokhin src->length - diff, walk->offset); 44e8f69799SIlya Lesokhin 458c30fbe6SEric Biggers scatterwalk_crypto_chain(sg, sg_next(src), 2); 46e8f69799SIlya Lesokhin } 47e8f69799SIlya Lesokhin 48e8f69799SIlya Lesokhin static int tls_enc_record(struct aead_request *aead_req, 49e8f69799SIlya Lesokhin struct crypto_aead *aead, char *aad, 50e8f69799SIlya Lesokhin char *iv, __be64 rcd_sn, 51e8f69799SIlya Lesokhin struct scatter_walk *in, 52e8f69799SIlya Lesokhin struct scatter_walk *out, int *in_len) 53e8f69799SIlya Lesokhin { 54e8f69799SIlya Lesokhin unsigned char buf[TLS_HEADER_SIZE + TLS_CIPHER_AES_GCM_128_IV_SIZE]; 55e8f69799SIlya Lesokhin struct scatterlist sg_in[3]; 56e8f69799SIlya Lesokhin struct scatterlist sg_out[3]; 57e8f69799SIlya Lesokhin u16 len; 58e8f69799SIlya Lesokhin int rc; 59e8f69799SIlya Lesokhin 60e8f69799SIlya Lesokhin len = min_t(int, *in_len, ARRAY_SIZE(buf)); 61e8f69799SIlya Lesokhin 62e8f69799SIlya Lesokhin scatterwalk_copychunks(buf, in, len, 0); 63e8f69799SIlya Lesokhin scatterwalk_copychunks(buf, out, len, 1); 64e8f69799SIlya Lesokhin 65e8f69799SIlya Lesokhin *in_len -= len; 66e8f69799SIlya Lesokhin if (!*in_len) 67e8f69799SIlya Lesokhin return 0; 68e8f69799SIlya Lesokhin 69e8f69799SIlya Lesokhin scatterwalk_pagedone(in, 0, 1); 70e8f69799SIlya Lesokhin scatterwalk_pagedone(out, 1, 1); 71e8f69799SIlya Lesokhin 72e8f69799SIlya Lesokhin len = buf[4] | (buf[3] << 8); 73e8f69799SIlya Lesokhin len -= TLS_CIPHER_AES_GCM_128_IV_SIZE; 74e8f69799SIlya Lesokhin 75e8f69799SIlya Lesokhin tls_make_aad(aad, len - TLS_CIPHER_AES_GCM_128_TAG_SIZE, 76130b392cSDave Watson (char *)&rcd_sn, sizeof(rcd_sn), buf[0], 77130b392cSDave Watson TLS_1_2_VERSION); 78e8f69799SIlya Lesokhin 79e8f69799SIlya Lesokhin memcpy(iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, buf + TLS_HEADER_SIZE, 80e8f69799SIlya Lesokhin TLS_CIPHER_AES_GCM_128_IV_SIZE); 81e8f69799SIlya Lesokhin 82e8f69799SIlya Lesokhin sg_init_table(sg_in, ARRAY_SIZE(sg_in)); 83e8f69799SIlya Lesokhin sg_init_table(sg_out, ARRAY_SIZE(sg_out)); 84e8f69799SIlya Lesokhin sg_set_buf(sg_in, aad, TLS_AAD_SPACE_SIZE); 85e8f69799SIlya Lesokhin sg_set_buf(sg_out, aad, TLS_AAD_SPACE_SIZE); 86e8f69799SIlya Lesokhin chain_to_walk(sg_in + 1, in); 87e8f69799SIlya Lesokhin chain_to_walk(sg_out + 1, out); 88e8f69799SIlya Lesokhin 89e8f69799SIlya Lesokhin *in_len -= len; 90e8f69799SIlya Lesokhin if (*in_len < 0) { 91e8f69799SIlya Lesokhin *in_len += TLS_CIPHER_AES_GCM_128_TAG_SIZE; 92e8f69799SIlya Lesokhin /* the input buffer doesn't contain the entire record. 93e8f69799SIlya Lesokhin * trim len accordingly. The resulting authentication tag 94e8f69799SIlya Lesokhin * will contain garbage, but we don't care, so we won't 95e8f69799SIlya Lesokhin * include any of it in the output skb 96e8f69799SIlya Lesokhin * Note that we assume the output buffer length 97e8f69799SIlya Lesokhin * is larger then input buffer length + tag size 98e8f69799SIlya Lesokhin */ 99e8f69799SIlya Lesokhin if (*in_len < 0) 100e8f69799SIlya Lesokhin len += *in_len; 101e8f69799SIlya Lesokhin 102e8f69799SIlya Lesokhin *in_len = 0; 103e8f69799SIlya Lesokhin } 104e8f69799SIlya Lesokhin 105e8f69799SIlya Lesokhin if (*in_len) { 106e8f69799SIlya Lesokhin scatterwalk_copychunks(NULL, in, len, 2); 107e8f69799SIlya Lesokhin scatterwalk_pagedone(in, 0, 1); 108e8f69799SIlya Lesokhin scatterwalk_copychunks(NULL, out, len, 2); 109e8f69799SIlya Lesokhin scatterwalk_pagedone(out, 1, 1); 110e8f69799SIlya Lesokhin } 111e8f69799SIlya Lesokhin 112e8f69799SIlya Lesokhin len -= TLS_CIPHER_AES_GCM_128_TAG_SIZE; 113e8f69799SIlya Lesokhin aead_request_set_crypt(aead_req, sg_in, sg_out, len, iv); 114e8f69799SIlya Lesokhin 115e8f69799SIlya Lesokhin rc = crypto_aead_encrypt(aead_req); 116e8f69799SIlya Lesokhin 117e8f69799SIlya Lesokhin return rc; 118e8f69799SIlya Lesokhin } 119e8f69799SIlya Lesokhin 120e8f69799SIlya Lesokhin static void tls_init_aead_request(struct aead_request *aead_req, 121e8f69799SIlya Lesokhin struct crypto_aead *aead) 122e8f69799SIlya Lesokhin { 123e8f69799SIlya Lesokhin aead_request_set_tfm(aead_req, aead); 124e8f69799SIlya Lesokhin aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE); 125e8f69799SIlya Lesokhin } 126e8f69799SIlya Lesokhin 127e8f69799SIlya Lesokhin static struct aead_request *tls_alloc_aead_request(struct crypto_aead *aead, 128e8f69799SIlya Lesokhin gfp_t flags) 129e8f69799SIlya Lesokhin { 130e8f69799SIlya Lesokhin unsigned int req_size = sizeof(struct aead_request) + 131e8f69799SIlya Lesokhin crypto_aead_reqsize(aead); 132e8f69799SIlya Lesokhin struct aead_request *aead_req; 133e8f69799SIlya Lesokhin 134e8f69799SIlya Lesokhin aead_req = kzalloc(req_size, flags); 135e8f69799SIlya Lesokhin if (aead_req) 136e8f69799SIlya Lesokhin tls_init_aead_request(aead_req, aead); 137e8f69799SIlya Lesokhin return aead_req; 138e8f69799SIlya Lesokhin } 139e8f69799SIlya Lesokhin 140e8f69799SIlya Lesokhin static int tls_enc_records(struct aead_request *aead_req, 141e8f69799SIlya Lesokhin struct crypto_aead *aead, struct scatterlist *sg_in, 142e8f69799SIlya Lesokhin struct scatterlist *sg_out, char *aad, char *iv, 143e8f69799SIlya Lesokhin u64 rcd_sn, int len) 144e8f69799SIlya Lesokhin { 145e8f69799SIlya Lesokhin struct scatter_walk out, in; 146e8f69799SIlya Lesokhin int rc; 147e8f69799SIlya Lesokhin 148e8f69799SIlya Lesokhin scatterwalk_start(&in, sg_in); 149e8f69799SIlya Lesokhin scatterwalk_start(&out, sg_out); 150e8f69799SIlya Lesokhin 151e8f69799SIlya Lesokhin do { 152e8f69799SIlya Lesokhin rc = tls_enc_record(aead_req, aead, aad, iv, 153e8f69799SIlya Lesokhin cpu_to_be64(rcd_sn), &in, &out, &len); 154e8f69799SIlya Lesokhin rcd_sn++; 155e8f69799SIlya Lesokhin 156e8f69799SIlya Lesokhin } while (rc == 0 && len); 157e8f69799SIlya Lesokhin 158e8f69799SIlya Lesokhin scatterwalk_done(&in, 0, 0); 159e8f69799SIlya Lesokhin scatterwalk_done(&out, 1, 0); 160e8f69799SIlya Lesokhin 161e8f69799SIlya Lesokhin return rc; 162e8f69799SIlya Lesokhin } 163e8f69799SIlya Lesokhin 164e8f69799SIlya Lesokhin /* Can't use icsk->icsk_af_ops->send_check here because the ip addresses 165e8f69799SIlya Lesokhin * might have been changed by NAT. 166e8f69799SIlya Lesokhin */ 167e8f69799SIlya Lesokhin static void update_chksum(struct sk_buff *skb, int headln) 168e8f69799SIlya Lesokhin { 169e8f69799SIlya Lesokhin struct tcphdr *th = tcp_hdr(skb); 170e8f69799SIlya Lesokhin int datalen = skb->len - headln; 171e8f69799SIlya Lesokhin const struct ipv6hdr *ipv6h; 172e8f69799SIlya Lesokhin const struct iphdr *iph; 173e8f69799SIlya Lesokhin 174e8f69799SIlya Lesokhin /* We only changed the payload so if we are using partial we don't 175e8f69799SIlya Lesokhin * need to update anything. 176e8f69799SIlya Lesokhin */ 177e8f69799SIlya Lesokhin if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) 178e8f69799SIlya Lesokhin return; 179e8f69799SIlya Lesokhin 180e8f69799SIlya Lesokhin skb->ip_summed = CHECKSUM_PARTIAL; 181e8f69799SIlya Lesokhin skb->csum_start = skb_transport_header(skb) - skb->head; 182e8f69799SIlya Lesokhin skb->csum_offset = offsetof(struct tcphdr, check); 183e8f69799SIlya Lesokhin 184e8f69799SIlya Lesokhin if (skb->sk->sk_family == AF_INET6) { 185e8f69799SIlya Lesokhin ipv6h = ipv6_hdr(skb); 186e8f69799SIlya Lesokhin th->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, 187e8f69799SIlya Lesokhin datalen, IPPROTO_TCP, 0); 188e8f69799SIlya Lesokhin } else { 189e8f69799SIlya Lesokhin iph = ip_hdr(skb); 190e8f69799SIlya Lesokhin th->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, datalen, 191e8f69799SIlya Lesokhin IPPROTO_TCP, 0); 192e8f69799SIlya Lesokhin } 193e8f69799SIlya Lesokhin } 194e8f69799SIlya Lesokhin 195e8f69799SIlya Lesokhin static void complete_skb(struct sk_buff *nskb, struct sk_buff *skb, int headln) 196e8f69799SIlya Lesokhin { 1979188d5caSJakub Kicinski struct sock *sk = skb->sk; 1989188d5caSJakub Kicinski int delta; 1999188d5caSJakub Kicinski 200e8f69799SIlya Lesokhin skb_copy_header(nskb, skb); 201e8f69799SIlya Lesokhin 202e8f69799SIlya Lesokhin skb_put(nskb, skb->len); 203e8f69799SIlya Lesokhin memcpy(nskb->data, skb->data, headln); 204e8f69799SIlya Lesokhin 205e8f69799SIlya Lesokhin nskb->destructor = skb->destructor; 2069188d5caSJakub Kicinski nskb->sk = sk; 207e8f69799SIlya Lesokhin skb->destructor = NULL; 208e8f69799SIlya Lesokhin skb->sk = NULL; 2099188d5caSJakub Kicinski 2102dcb0033SJakub Kicinski update_chksum(nskb, headln); 2112dcb0033SJakub Kicinski 2129188d5caSJakub Kicinski delta = nskb->truesize - skb->truesize; 2139188d5caSJakub Kicinski if (likely(delta < 0)) 2149188d5caSJakub Kicinski WARN_ON_ONCE(refcount_sub_and_test(-delta, &sk->sk_wmem_alloc)); 2159188d5caSJakub Kicinski else if (delta) 2169188d5caSJakub Kicinski refcount_add(delta, &sk->sk_wmem_alloc); 217e8f69799SIlya Lesokhin } 218e8f69799SIlya Lesokhin 219e8f69799SIlya Lesokhin /* This function may be called after the user socket is already 220e8f69799SIlya Lesokhin * closed so make sure we don't use anything freed during 221e8f69799SIlya Lesokhin * tls_sk_proto_close here 222e8f69799SIlya Lesokhin */ 223e8f69799SIlya Lesokhin 224e8f69799SIlya Lesokhin static int fill_sg_in(struct scatterlist *sg_in, 225e8f69799SIlya Lesokhin struct sk_buff *skb, 226d80a1b9dSBoris Pismenny struct tls_offload_context_tx *ctx, 227e8f69799SIlya Lesokhin u64 *rcd_sn, 228e8f69799SIlya Lesokhin s32 *sync_size, 229e8f69799SIlya Lesokhin int *resync_sgs) 230e8f69799SIlya Lesokhin { 231e8f69799SIlya Lesokhin int tcp_payload_offset = skb_transport_offset(skb) + tcp_hdrlen(skb); 232e8f69799SIlya Lesokhin int payload_len = skb->len - tcp_payload_offset; 233e8f69799SIlya Lesokhin u32 tcp_seq = ntohl(tcp_hdr(skb)->seq); 234e8f69799SIlya Lesokhin struct tls_record_info *record; 235e8f69799SIlya Lesokhin unsigned long flags; 236e8f69799SIlya Lesokhin int remaining; 237e8f69799SIlya Lesokhin int i; 238e8f69799SIlya Lesokhin 239e8f69799SIlya Lesokhin spin_lock_irqsave(&ctx->lock, flags); 240e8f69799SIlya Lesokhin record = tls_get_record(ctx, tcp_seq, rcd_sn); 241e8f69799SIlya Lesokhin if (!record) { 242e8f69799SIlya Lesokhin spin_unlock_irqrestore(&ctx->lock, flags); 243e8f69799SIlya Lesokhin return -EINVAL; 244e8f69799SIlya Lesokhin } 245e8f69799SIlya Lesokhin 246e8f69799SIlya Lesokhin *sync_size = tcp_seq - tls_record_start_seq(record); 247e8f69799SIlya Lesokhin if (*sync_size < 0) { 248e8f69799SIlya Lesokhin int is_start_marker = tls_record_is_start_marker(record); 249e8f69799SIlya Lesokhin 250e8f69799SIlya Lesokhin spin_unlock_irqrestore(&ctx->lock, flags); 251e8f69799SIlya Lesokhin /* This should only occur if the relevant record was 252e8f69799SIlya Lesokhin * already acked. In that case it should be ok 253e8f69799SIlya Lesokhin * to drop the packet and avoid retransmission. 254e8f69799SIlya Lesokhin * 255e8f69799SIlya Lesokhin * There is a corner case where the packet contains 256e8f69799SIlya Lesokhin * both an acked and a non-acked record. 257e8f69799SIlya Lesokhin * We currently don't handle that case and rely 258e8f69799SIlya Lesokhin * on TCP to retranmit a packet that doesn't contain 259e8f69799SIlya Lesokhin * already acked payload. 260e8f69799SIlya Lesokhin */ 261e8f69799SIlya Lesokhin if (!is_start_marker) 262e8f69799SIlya Lesokhin *sync_size = 0; 263e8f69799SIlya Lesokhin return -EINVAL; 264e8f69799SIlya Lesokhin } 265e8f69799SIlya Lesokhin 266e8f69799SIlya Lesokhin remaining = *sync_size; 267e8f69799SIlya Lesokhin for (i = 0; remaining > 0; i++) { 268e8f69799SIlya Lesokhin skb_frag_t *frag = &record->frags[i]; 269e8f69799SIlya Lesokhin 270e8f69799SIlya Lesokhin __skb_frag_ref(frag); 271e8f69799SIlya Lesokhin sg_set_page(sg_in + i, skb_frag_page(frag), 272e8f69799SIlya Lesokhin skb_frag_size(frag), frag->page_offset); 273e8f69799SIlya Lesokhin 274e8f69799SIlya Lesokhin remaining -= skb_frag_size(frag); 275e8f69799SIlya Lesokhin 276e8f69799SIlya Lesokhin if (remaining < 0) 277e8f69799SIlya Lesokhin sg_in[i].length += remaining; 278e8f69799SIlya Lesokhin } 279e8f69799SIlya Lesokhin *resync_sgs = i; 280e8f69799SIlya Lesokhin 281e8f69799SIlya Lesokhin spin_unlock_irqrestore(&ctx->lock, flags); 282e8f69799SIlya Lesokhin if (skb_to_sgvec(skb, &sg_in[i], tcp_payload_offset, payload_len) < 0) 283e8f69799SIlya Lesokhin return -EINVAL; 284e8f69799SIlya Lesokhin 285e8f69799SIlya Lesokhin return 0; 286e8f69799SIlya Lesokhin } 287e8f69799SIlya Lesokhin 288e8f69799SIlya Lesokhin static void fill_sg_out(struct scatterlist sg_out[3], void *buf, 289e8f69799SIlya Lesokhin struct tls_context *tls_ctx, 290e8f69799SIlya Lesokhin struct sk_buff *nskb, 291e8f69799SIlya Lesokhin int tcp_payload_offset, 292e8f69799SIlya Lesokhin int payload_len, 293e8f69799SIlya Lesokhin int sync_size, 294e8f69799SIlya Lesokhin void *dummy_buf) 295e8f69799SIlya Lesokhin { 296e8f69799SIlya Lesokhin sg_set_buf(&sg_out[0], dummy_buf, sync_size); 297e8f69799SIlya Lesokhin sg_set_buf(&sg_out[1], nskb->data + tcp_payload_offset, payload_len); 298e8f69799SIlya Lesokhin /* Add room for authentication tag produced by crypto */ 299e8f69799SIlya Lesokhin dummy_buf += sync_size; 300e8f69799SIlya Lesokhin sg_set_buf(&sg_out[2], dummy_buf, TLS_CIPHER_AES_GCM_128_TAG_SIZE); 301e8f69799SIlya Lesokhin } 302e8f69799SIlya Lesokhin 303e8f69799SIlya Lesokhin static struct sk_buff *tls_enc_skb(struct tls_context *tls_ctx, 304e8f69799SIlya Lesokhin struct scatterlist sg_out[3], 305e8f69799SIlya Lesokhin struct scatterlist *sg_in, 306e8f69799SIlya Lesokhin struct sk_buff *skb, 307e8f69799SIlya Lesokhin s32 sync_size, u64 rcd_sn) 308e8f69799SIlya Lesokhin { 309e8f69799SIlya Lesokhin int tcp_payload_offset = skb_transport_offset(skb) + tcp_hdrlen(skb); 310d80a1b9dSBoris Pismenny struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx); 311e8f69799SIlya Lesokhin int payload_len = skb->len - tcp_payload_offset; 312e8f69799SIlya Lesokhin void *buf, *iv, *aad, *dummy_buf; 313e8f69799SIlya Lesokhin struct aead_request *aead_req; 314e8f69799SIlya Lesokhin struct sk_buff *nskb = NULL; 315e8f69799SIlya Lesokhin int buf_len; 316e8f69799SIlya Lesokhin 317e8f69799SIlya Lesokhin aead_req = tls_alloc_aead_request(ctx->aead_send, GFP_ATOMIC); 318e8f69799SIlya Lesokhin if (!aead_req) 319e8f69799SIlya Lesokhin return NULL; 320e8f69799SIlya Lesokhin 321e8f69799SIlya Lesokhin buf_len = TLS_CIPHER_AES_GCM_128_SALT_SIZE + 322e8f69799SIlya Lesokhin TLS_CIPHER_AES_GCM_128_IV_SIZE + 323e8f69799SIlya Lesokhin TLS_AAD_SPACE_SIZE + 324e8f69799SIlya Lesokhin sync_size + 325e8f69799SIlya Lesokhin TLS_CIPHER_AES_GCM_128_TAG_SIZE; 326e8f69799SIlya Lesokhin buf = kmalloc(buf_len, GFP_ATOMIC); 327e8f69799SIlya Lesokhin if (!buf) 328e8f69799SIlya Lesokhin goto free_req; 329e8f69799SIlya Lesokhin 330e8f69799SIlya Lesokhin iv = buf; 33186029d10SSabrina Dubroca memcpy(iv, tls_ctx->crypto_send.aes_gcm_128.salt, 332e8f69799SIlya Lesokhin TLS_CIPHER_AES_GCM_128_SALT_SIZE); 333e8f69799SIlya Lesokhin aad = buf + TLS_CIPHER_AES_GCM_128_SALT_SIZE + 334e8f69799SIlya Lesokhin TLS_CIPHER_AES_GCM_128_IV_SIZE; 335e8f69799SIlya Lesokhin dummy_buf = aad + TLS_AAD_SPACE_SIZE; 336e8f69799SIlya Lesokhin 337e8f69799SIlya Lesokhin nskb = alloc_skb(skb_headroom(skb) + skb->len, GFP_ATOMIC); 338e8f69799SIlya Lesokhin if (!nskb) 339e8f69799SIlya Lesokhin goto free_buf; 340e8f69799SIlya Lesokhin 341e8f69799SIlya Lesokhin skb_reserve(nskb, skb_headroom(skb)); 342e8f69799SIlya Lesokhin 343e8f69799SIlya Lesokhin fill_sg_out(sg_out, buf, tls_ctx, nskb, tcp_payload_offset, 344e8f69799SIlya Lesokhin payload_len, sync_size, dummy_buf); 345e8f69799SIlya Lesokhin 346e8f69799SIlya Lesokhin if (tls_enc_records(aead_req, ctx->aead_send, sg_in, sg_out, aad, iv, 347e8f69799SIlya Lesokhin rcd_sn, sync_size + payload_len) < 0) 348e8f69799SIlya Lesokhin goto free_nskb; 349e8f69799SIlya Lesokhin 350e8f69799SIlya Lesokhin complete_skb(nskb, skb, tcp_payload_offset); 351e8f69799SIlya Lesokhin 352e8f69799SIlya Lesokhin /* validate_xmit_skb_list assumes that if the skb wasn't segmented 353e8f69799SIlya Lesokhin * nskb->prev will point to the skb itself 354e8f69799SIlya Lesokhin */ 355e8f69799SIlya Lesokhin nskb->prev = nskb; 356e8f69799SIlya Lesokhin 357e8f69799SIlya Lesokhin free_buf: 358e8f69799SIlya Lesokhin kfree(buf); 359e8f69799SIlya Lesokhin free_req: 360e8f69799SIlya Lesokhin kfree(aead_req); 361e8f69799SIlya Lesokhin return nskb; 362e8f69799SIlya Lesokhin free_nskb: 363e8f69799SIlya Lesokhin kfree_skb(nskb); 364e8f69799SIlya Lesokhin nskb = NULL; 365e8f69799SIlya Lesokhin goto free_buf; 366e8f69799SIlya Lesokhin } 367e8f69799SIlya Lesokhin 368e8f69799SIlya Lesokhin static struct sk_buff *tls_sw_fallback(struct sock *sk, struct sk_buff *skb) 369e8f69799SIlya Lesokhin { 370e8f69799SIlya Lesokhin int tcp_payload_offset = skb_transport_offset(skb) + tcp_hdrlen(skb); 371e8f69799SIlya Lesokhin struct tls_context *tls_ctx = tls_get_ctx(sk); 372d80a1b9dSBoris Pismenny struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx); 373e8f69799SIlya Lesokhin int payload_len = skb->len - tcp_payload_offset; 374e8f69799SIlya Lesokhin struct scatterlist *sg_in, sg_out[3]; 375e8f69799SIlya Lesokhin struct sk_buff *nskb = NULL; 376e8f69799SIlya Lesokhin int sg_in_max_elements; 377e8f69799SIlya Lesokhin int resync_sgs = 0; 378e8f69799SIlya Lesokhin s32 sync_size = 0; 379e8f69799SIlya Lesokhin u64 rcd_sn; 380e8f69799SIlya Lesokhin 381e8f69799SIlya Lesokhin /* worst case is: 382e8f69799SIlya Lesokhin * MAX_SKB_FRAGS in tls_record_info 383e8f69799SIlya Lesokhin * MAX_SKB_FRAGS + 1 in SKB head and frags. 384e8f69799SIlya Lesokhin */ 385e8f69799SIlya Lesokhin sg_in_max_elements = 2 * MAX_SKB_FRAGS + 1; 386e8f69799SIlya Lesokhin 387e8f69799SIlya Lesokhin if (!payload_len) 388e8f69799SIlya Lesokhin return skb; 389e8f69799SIlya Lesokhin 390e8f69799SIlya Lesokhin sg_in = kmalloc_array(sg_in_max_elements, sizeof(*sg_in), GFP_ATOMIC); 391e8f69799SIlya Lesokhin if (!sg_in) 392e8f69799SIlya Lesokhin goto free_orig; 393e8f69799SIlya Lesokhin 394e8f69799SIlya Lesokhin sg_init_table(sg_in, sg_in_max_elements); 395e8f69799SIlya Lesokhin sg_init_table(sg_out, ARRAY_SIZE(sg_out)); 396e8f69799SIlya Lesokhin 397e8f69799SIlya Lesokhin if (fill_sg_in(sg_in, skb, ctx, &rcd_sn, &sync_size, &resync_sgs)) { 398e8f69799SIlya Lesokhin /* bypass packets before kernel TLS socket option was set */ 399e8f69799SIlya Lesokhin if (sync_size < 0 && payload_len <= -sync_size) 400e8f69799SIlya Lesokhin nskb = skb_get(skb); 401e8f69799SIlya Lesokhin goto put_sg; 402e8f69799SIlya Lesokhin } 403e8f69799SIlya Lesokhin 404e8f69799SIlya Lesokhin nskb = tls_enc_skb(tls_ctx, sg_out, sg_in, skb, sync_size, rcd_sn); 405e8f69799SIlya Lesokhin 406e8f69799SIlya Lesokhin put_sg: 407e8f69799SIlya Lesokhin while (resync_sgs) 408e8f69799SIlya Lesokhin put_page(sg_page(&sg_in[--resync_sgs])); 409e8f69799SIlya Lesokhin kfree(sg_in); 410e8f69799SIlya Lesokhin free_orig: 41187b11e06SJakub Kicinski if (nskb) 41287b11e06SJakub Kicinski consume_skb(skb); 41387b11e06SJakub Kicinski else 414e8f69799SIlya Lesokhin kfree_skb(skb); 415e8f69799SIlya Lesokhin return nskb; 416e8f69799SIlya Lesokhin } 417e8f69799SIlya Lesokhin 418e8f69799SIlya Lesokhin struct sk_buff *tls_validate_xmit_skb(struct sock *sk, 419e8f69799SIlya Lesokhin struct net_device *dev, 420e8f69799SIlya Lesokhin struct sk_buff *skb) 421e8f69799SIlya Lesokhin { 422e8f69799SIlya Lesokhin if (dev == tls_get_ctx(sk)->netdev) 423e8f69799SIlya Lesokhin return skb; 424e8f69799SIlya Lesokhin 425e8f69799SIlya Lesokhin return tls_sw_fallback(sk, skb); 426e8f69799SIlya Lesokhin } 4274799ac81SBoris Pismenny EXPORT_SYMBOL_GPL(tls_validate_xmit_skb); 428e8f69799SIlya Lesokhin 429*b9727d7fSDirk van der Merwe struct sk_buff *tls_encrypt_skb(struct sk_buff *skb) 430*b9727d7fSDirk van der Merwe { 431*b9727d7fSDirk van der Merwe return tls_sw_fallback(skb->sk, skb); 432*b9727d7fSDirk van der Merwe } 433*b9727d7fSDirk van der Merwe EXPORT_SYMBOL_GPL(tls_encrypt_skb); 434*b9727d7fSDirk van der Merwe 435e8f69799SIlya Lesokhin int tls_sw_fallback_init(struct sock *sk, 436d80a1b9dSBoris Pismenny struct tls_offload_context_tx *offload_ctx, 437e8f69799SIlya Lesokhin struct tls_crypto_info *crypto_info) 438e8f69799SIlya Lesokhin { 439e8f69799SIlya Lesokhin const u8 *key; 440e8f69799SIlya Lesokhin int rc; 441e8f69799SIlya Lesokhin 442e8f69799SIlya Lesokhin offload_ctx->aead_send = 443e8f69799SIlya Lesokhin crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC); 444e8f69799SIlya Lesokhin if (IS_ERR(offload_ctx->aead_send)) { 445e8f69799SIlya Lesokhin rc = PTR_ERR(offload_ctx->aead_send); 446e8f69799SIlya Lesokhin pr_err_ratelimited("crypto_alloc_aead failed rc=%d\n", rc); 447e8f69799SIlya Lesokhin offload_ctx->aead_send = NULL; 448e8f69799SIlya Lesokhin goto err_out; 449e8f69799SIlya Lesokhin } 450e8f69799SIlya Lesokhin 451e8f69799SIlya Lesokhin key = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->key; 452e8f69799SIlya Lesokhin 453e8f69799SIlya Lesokhin rc = crypto_aead_setkey(offload_ctx->aead_send, key, 454e8f69799SIlya Lesokhin TLS_CIPHER_AES_GCM_128_KEY_SIZE); 455e8f69799SIlya Lesokhin if (rc) 456e8f69799SIlya Lesokhin goto free_aead; 457e8f69799SIlya Lesokhin 458e8f69799SIlya Lesokhin rc = crypto_aead_setauthsize(offload_ctx->aead_send, 459e8f69799SIlya Lesokhin TLS_CIPHER_AES_GCM_128_TAG_SIZE); 460e8f69799SIlya Lesokhin if (rc) 461e8f69799SIlya Lesokhin goto free_aead; 462e8f69799SIlya Lesokhin 463e8f69799SIlya Lesokhin return 0; 464e8f69799SIlya Lesokhin free_aead: 465e8f69799SIlya Lesokhin crypto_free_aead(offload_ctx->aead_send); 466e8f69799SIlya Lesokhin err_out: 467e8f69799SIlya Lesokhin return rc; 468e8f69799SIlya Lesokhin } 469