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