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