1 /* Copyright (c) 2018, Mellanox Technologies All rights reserved. 2 * 3 * This software is available to you under a choice of one of two 4 * licenses. You may choose to be licensed under the terms of the GNU 5 * General Public License (GPL) Version 2, available from the file 6 * COPYING in the main directory of this source tree, or the 7 * OpenIB.org BSD license below: 8 * 9 * Redistribution and use in source and binary forms, with or 10 * without modification, are permitted provided that the following 11 * conditions are met: 12 * 13 * - Redistributions of source code must retain the above 14 * copyright notice, this list of conditions and the following 15 * disclaimer. 16 * 17 * - Redistributions in binary form must reproduce the above 18 * copyright notice, this list of conditions and the following 19 * disclaimer in the documentation and/or other materials 20 * provided with the distribution. 21 * 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 26 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 27 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 28 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 29 * SOFTWARE. 30 */ 31 32 #include <net/tls.h> 33 #include <crypto/aead.h> 34 #include <crypto/scatterwalk.h> 35 #include <net/ip6_checksum.h> 36 37 #include "tls.h" 38 39 static void chain_to_walk(struct scatterlist *sg, struct scatter_walk *walk) 40 { 41 struct scatterlist *src = walk->sg; 42 int diff = walk->offset - src->offset; 43 44 sg_set_page(sg, sg_page(src), 45 src->length - diff, walk->offset); 46 47 scatterwalk_crypto_chain(sg, sg_next(src), 2); 48 } 49 50 static int tls_enc_record(struct aead_request *aead_req, 51 struct crypto_aead *aead, char *aad, 52 char *iv, __be64 rcd_sn, 53 struct scatter_walk *in, 54 struct scatter_walk *out, int *in_len, 55 struct tls_prot_info *prot) 56 { 57 unsigned char buf[TLS_HEADER_SIZE + MAX_IV_SIZE]; 58 const struct tls_cipher_size_desc *cipher_sz; 59 struct scatterlist sg_in[3]; 60 struct scatterlist sg_out[3]; 61 unsigned int buf_size; 62 u16 len; 63 int rc; 64 65 switch (prot->cipher_type) { 66 case TLS_CIPHER_AES_GCM_128: 67 break; 68 default: 69 return -EINVAL; 70 } 71 cipher_sz = &tls_cipher_size_desc[prot->cipher_type]; 72 73 buf_size = TLS_HEADER_SIZE + cipher_sz->iv; 74 len = min_t(int, *in_len, buf_size); 75 76 scatterwalk_copychunks(buf, in, len, 0); 77 scatterwalk_copychunks(buf, out, len, 1); 78 79 *in_len -= len; 80 if (!*in_len) 81 return 0; 82 83 scatterwalk_pagedone(in, 0, 1); 84 scatterwalk_pagedone(out, 1, 1); 85 86 len = buf[4] | (buf[3] << 8); 87 len -= cipher_sz->iv; 88 89 tls_make_aad(aad, len - cipher_sz->tag, (char *)&rcd_sn, buf[0], prot); 90 91 memcpy(iv + cipher_sz->salt, buf + TLS_HEADER_SIZE, cipher_sz->iv); 92 93 sg_init_table(sg_in, ARRAY_SIZE(sg_in)); 94 sg_init_table(sg_out, ARRAY_SIZE(sg_out)); 95 sg_set_buf(sg_in, aad, TLS_AAD_SPACE_SIZE); 96 sg_set_buf(sg_out, aad, TLS_AAD_SPACE_SIZE); 97 chain_to_walk(sg_in + 1, in); 98 chain_to_walk(sg_out + 1, out); 99 100 *in_len -= len; 101 if (*in_len < 0) { 102 *in_len += cipher_sz->tag; 103 /* the input buffer doesn't contain the entire record. 104 * trim len accordingly. The resulting authentication tag 105 * will contain garbage, but we don't care, so we won't 106 * include any of it in the output skb 107 * Note that we assume the output buffer length 108 * is larger then input buffer length + tag size 109 */ 110 if (*in_len < 0) 111 len += *in_len; 112 113 *in_len = 0; 114 } 115 116 if (*in_len) { 117 scatterwalk_copychunks(NULL, in, len, 2); 118 scatterwalk_pagedone(in, 0, 1); 119 scatterwalk_copychunks(NULL, out, len, 2); 120 scatterwalk_pagedone(out, 1, 1); 121 } 122 123 len -= cipher_sz->tag; 124 aead_request_set_crypt(aead_req, sg_in, sg_out, len, iv); 125 126 rc = crypto_aead_encrypt(aead_req); 127 128 return rc; 129 } 130 131 static void tls_init_aead_request(struct aead_request *aead_req, 132 struct crypto_aead *aead) 133 { 134 aead_request_set_tfm(aead_req, aead); 135 aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE); 136 } 137 138 static struct aead_request *tls_alloc_aead_request(struct crypto_aead *aead, 139 gfp_t flags) 140 { 141 unsigned int req_size = sizeof(struct aead_request) + 142 crypto_aead_reqsize(aead); 143 struct aead_request *aead_req; 144 145 aead_req = kzalloc(req_size, flags); 146 if (aead_req) 147 tls_init_aead_request(aead_req, aead); 148 return aead_req; 149 } 150 151 static int tls_enc_records(struct aead_request *aead_req, 152 struct crypto_aead *aead, struct scatterlist *sg_in, 153 struct scatterlist *sg_out, char *aad, char *iv, 154 u64 rcd_sn, int len, struct tls_prot_info *prot) 155 { 156 struct scatter_walk out, in; 157 int rc; 158 159 scatterwalk_start(&in, sg_in); 160 scatterwalk_start(&out, sg_out); 161 162 do { 163 rc = tls_enc_record(aead_req, aead, aad, iv, 164 cpu_to_be64(rcd_sn), &in, &out, &len, prot); 165 rcd_sn++; 166 167 } while (rc == 0 && len); 168 169 scatterwalk_done(&in, 0, 0); 170 scatterwalk_done(&out, 1, 0); 171 172 return rc; 173 } 174 175 /* Can't use icsk->icsk_af_ops->send_check here because the ip addresses 176 * might have been changed by NAT. 177 */ 178 static void update_chksum(struct sk_buff *skb, int headln) 179 { 180 struct tcphdr *th = tcp_hdr(skb); 181 int datalen = skb->len - headln; 182 const struct ipv6hdr *ipv6h; 183 const struct iphdr *iph; 184 185 /* We only changed the payload so if we are using partial we don't 186 * need to update anything. 187 */ 188 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) 189 return; 190 191 skb->ip_summed = CHECKSUM_PARTIAL; 192 skb->csum_start = skb_transport_header(skb) - skb->head; 193 skb->csum_offset = offsetof(struct tcphdr, check); 194 195 if (skb->sk->sk_family == AF_INET6) { 196 ipv6h = ipv6_hdr(skb); 197 th->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, 198 datalen, IPPROTO_TCP, 0); 199 } else { 200 iph = ip_hdr(skb); 201 th->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, datalen, 202 IPPROTO_TCP, 0); 203 } 204 } 205 206 static void complete_skb(struct sk_buff *nskb, struct sk_buff *skb, int headln) 207 { 208 struct sock *sk = skb->sk; 209 int delta; 210 211 skb_copy_header(nskb, skb); 212 213 skb_put(nskb, skb->len); 214 memcpy(nskb->data, skb->data, headln); 215 216 nskb->destructor = skb->destructor; 217 nskb->sk = sk; 218 skb->destructor = NULL; 219 skb->sk = NULL; 220 221 update_chksum(nskb, headln); 222 223 /* sock_efree means skb must gone through skb_orphan_partial() */ 224 if (nskb->destructor == sock_efree) 225 return; 226 227 delta = nskb->truesize - skb->truesize; 228 if (likely(delta < 0)) 229 WARN_ON_ONCE(refcount_sub_and_test(-delta, &sk->sk_wmem_alloc)); 230 else if (delta) 231 refcount_add(delta, &sk->sk_wmem_alloc); 232 } 233 234 /* This function may be called after the user socket is already 235 * closed so make sure we don't use anything freed during 236 * tls_sk_proto_close here 237 */ 238 239 static int fill_sg_in(struct scatterlist *sg_in, 240 struct sk_buff *skb, 241 struct tls_offload_context_tx *ctx, 242 u64 *rcd_sn, 243 s32 *sync_size, 244 int *resync_sgs) 245 { 246 int tcp_payload_offset = skb_tcp_all_headers(skb); 247 int payload_len = skb->len - tcp_payload_offset; 248 u32 tcp_seq = ntohl(tcp_hdr(skb)->seq); 249 struct tls_record_info *record; 250 unsigned long flags; 251 int remaining; 252 int i; 253 254 spin_lock_irqsave(&ctx->lock, flags); 255 record = tls_get_record(ctx, tcp_seq, rcd_sn); 256 if (!record) { 257 spin_unlock_irqrestore(&ctx->lock, flags); 258 return -EINVAL; 259 } 260 261 *sync_size = tcp_seq - tls_record_start_seq(record); 262 if (*sync_size < 0) { 263 int is_start_marker = tls_record_is_start_marker(record); 264 265 spin_unlock_irqrestore(&ctx->lock, flags); 266 /* This should only occur if the relevant record was 267 * already acked. In that case it should be ok 268 * to drop the packet and avoid retransmission. 269 * 270 * There is a corner case where the packet contains 271 * both an acked and a non-acked record. 272 * We currently don't handle that case and rely 273 * on TCP to retranmit a packet that doesn't contain 274 * already acked payload. 275 */ 276 if (!is_start_marker) 277 *sync_size = 0; 278 return -EINVAL; 279 } 280 281 remaining = *sync_size; 282 for (i = 0; remaining > 0; i++) { 283 skb_frag_t *frag = &record->frags[i]; 284 285 __skb_frag_ref(frag); 286 sg_set_page(sg_in + i, skb_frag_page(frag), 287 skb_frag_size(frag), skb_frag_off(frag)); 288 289 remaining -= skb_frag_size(frag); 290 291 if (remaining < 0) 292 sg_in[i].length += remaining; 293 } 294 *resync_sgs = i; 295 296 spin_unlock_irqrestore(&ctx->lock, flags); 297 if (skb_to_sgvec(skb, &sg_in[i], tcp_payload_offset, payload_len) < 0) 298 return -EINVAL; 299 300 return 0; 301 } 302 303 static void fill_sg_out(struct scatterlist sg_out[3], void *buf, 304 struct tls_context *tls_ctx, 305 struct sk_buff *nskb, 306 int tcp_payload_offset, 307 int payload_len, 308 int sync_size, 309 void *dummy_buf) 310 { 311 const struct tls_cipher_size_desc *cipher_sz = 312 &tls_cipher_size_desc[tls_ctx->crypto_send.info.cipher_type]; 313 314 sg_set_buf(&sg_out[0], dummy_buf, sync_size); 315 sg_set_buf(&sg_out[1], nskb->data + tcp_payload_offset, payload_len); 316 /* Add room for authentication tag produced by crypto */ 317 dummy_buf += sync_size; 318 sg_set_buf(&sg_out[2], dummy_buf, cipher_sz->tag); 319 } 320 321 static struct sk_buff *tls_enc_skb(struct tls_context *tls_ctx, 322 struct scatterlist sg_out[3], 323 struct scatterlist *sg_in, 324 struct sk_buff *skb, 325 s32 sync_size, u64 rcd_sn) 326 { 327 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx); 328 int tcp_payload_offset = skb_tcp_all_headers(skb); 329 int payload_len = skb->len - tcp_payload_offset; 330 const struct tls_cipher_size_desc *cipher_sz; 331 void *buf, *iv, *aad, *dummy_buf, *salt; 332 struct aead_request *aead_req; 333 struct sk_buff *nskb = NULL; 334 int buf_len; 335 336 aead_req = tls_alloc_aead_request(ctx->aead_send, GFP_ATOMIC); 337 if (!aead_req) 338 return NULL; 339 340 switch (tls_ctx->crypto_send.info.cipher_type) { 341 case TLS_CIPHER_AES_GCM_128: 342 salt = tls_ctx->crypto_send.aes_gcm_128.salt; 343 break; 344 default: 345 return NULL; 346 } 347 cipher_sz = &tls_cipher_size_desc[tls_ctx->crypto_send.info.cipher_type]; 348 buf_len = cipher_sz->salt + cipher_sz->iv + TLS_AAD_SPACE_SIZE + 349 sync_size + cipher_sz->tag; 350 buf = kmalloc(buf_len, GFP_ATOMIC); 351 if (!buf) 352 goto free_req; 353 354 iv = buf; 355 memcpy(iv, salt, cipher_sz->salt); 356 aad = buf + cipher_sz->salt + cipher_sz->iv; 357 dummy_buf = aad + TLS_AAD_SPACE_SIZE; 358 359 nskb = alloc_skb(skb_headroom(skb) + skb->len, GFP_ATOMIC); 360 if (!nskb) 361 goto free_buf; 362 363 skb_reserve(nskb, skb_headroom(skb)); 364 365 fill_sg_out(sg_out, buf, tls_ctx, nskb, tcp_payload_offset, 366 payload_len, sync_size, dummy_buf); 367 368 if (tls_enc_records(aead_req, ctx->aead_send, sg_in, sg_out, aad, iv, 369 rcd_sn, sync_size + payload_len, 370 &tls_ctx->prot_info) < 0) 371 goto free_nskb; 372 373 complete_skb(nskb, skb, tcp_payload_offset); 374 375 /* validate_xmit_skb_list assumes that if the skb wasn't segmented 376 * nskb->prev will point to the skb itself 377 */ 378 nskb->prev = nskb; 379 380 free_buf: 381 kfree(buf); 382 free_req: 383 kfree(aead_req); 384 return nskb; 385 free_nskb: 386 kfree_skb(nskb); 387 nskb = NULL; 388 goto free_buf; 389 } 390 391 static struct sk_buff *tls_sw_fallback(struct sock *sk, struct sk_buff *skb) 392 { 393 int tcp_payload_offset = skb_tcp_all_headers(skb); 394 struct tls_context *tls_ctx = tls_get_ctx(sk); 395 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx); 396 int payload_len = skb->len - tcp_payload_offset; 397 struct scatterlist *sg_in, sg_out[3]; 398 struct sk_buff *nskb = NULL; 399 int sg_in_max_elements; 400 int resync_sgs = 0; 401 s32 sync_size = 0; 402 u64 rcd_sn; 403 404 /* worst case is: 405 * MAX_SKB_FRAGS in tls_record_info 406 * MAX_SKB_FRAGS + 1 in SKB head and frags. 407 */ 408 sg_in_max_elements = 2 * MAX_SKB_FRAGS + 1; 409 410 if (!payload_len) 411 return skb; 412 413 sg_in = kmalloc_array(sg_in_max_elements, sizeof(*sg_in), GFP_ATOMIC); 414 if (!sg_in) 415 goto free_orig; 416 417 sg_init_table(sg_in, sg_in_max_elements); 418 sg_init_table(sg_out, ARRAY_SIZE(sg_out)); 419 420 if (fill_sg_in(sg_in, skb, ctx, &rcd_sn, &sync_size, &resync_sgs)) { 421 /* bypass packets before kernel TLS socket option was set */ 422 if (sync_size < 0 && payload_len <= -sync_size) 423 nskb = skb_get(skb); 424 goto put_sg; 425 } 426 427 nskb = tls_enc_skb(tls_ctx, sg_out, sg_in, skb, sync_size, rcd_sn); 428 429 put_sg: 430 while (resync_sgs) 431 put_page(sg_page(&sg_in[--resync_sgs])); 432 kfree(sg_in); 433 free_orig: 434 if (nskb) 435 consume_skb(skb); 436 else 437 kfree_skb(skb); 438 return nskb; 439 } 440 441 struct sk_buff *tls_validate_xmit_skb(struct sock *sk, 442 struct net_device *dev, 443 struct sk_buff *skb) 444 { 445 if (dev == rcu_dereference_bh(tls_get_ctx(sk)->netdev) || 446 netif_is_bond_master(dev)) 447 return skb; 448 449 return tls_sw_fallback(sk, skb); 450 } 451 EXPORT_SYMBOL_GPL(tls_validate_xmit_skb); 452 453 struct sk_buff *tls_validate_xmit_skb_sw(struct sock *sk, 454 struct net_device *dev, 455 struct sk_buff *skb) 456 { 457 return tls_sw_fallback(sk, skb); 458 } 459 460 struct sk_buff *tls_encrypt_skb(struct sk_buff *skb) 461 { 462 return tls_sw_fallback(skb->sk, skb); 463 } 464 EXPORT_SYMBOL_GPL(tls_encrypt_skb); 465 466 int tls_sw_fallback_init(struct sock *sk, 467 struct tls_offload_context_tx *offload_ctx, 468 struct tls_crypto_info *crypto_info) 469 { 470 const struct tls_cipher_size_desc *cipher_sz; 471 const u8 *key; 472 int rc; 473 474 offload_ctx->aead_send = 475 crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC); 476 if (IS_ERR(offload_ctx->aead_send)) { 477 rc = PTR_ERR(offload_ctx->aead_send); 478 pr_err_ratelimited("crypto_alloc_aead failed rc=%d\n", rc); 479 offload_ctx->aead_send = NULL; 480 goto err_out; 481 } 482 483 switch (crypto_info->cipher_type) { 484 case TLS_CIPHER_AES_GCM_128: 485 key = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->key; 486 break; 487 default: 488 return -EINVAL; 489 } 490 cipher_sz = &tls_cipher_size_desc[crypto_info->cipher_type]; 491 492 rc = crypto_aead_setkey(offload_ctx->aead_send, key, cipher_sz->key); 493 if (rc) 494 goto free_aead; 495 496 rc = crypto_aead_setauthsize(offload_ctx->aead_send, cipher_sz->tag); 497 if (rc) 498 goto free_aead; 499 500 return 0; 501 free_aead: 502 crypto_free_aead(offload_ctx->aead_send); 503 err_out: 504 return rc; 505 } 506