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