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 <crypto/aead.h> 33 #include <linux/highmem.h> 34 #include <linux/module.h> 35 #include <linux/netdevice.h> 36 #include <net/dst.h> 37 #include <net/inet_connection_sock.h> 38 #include <net/tcp.h> 39 #include <net/tls.h> 40 41 #include "tls.h" 42 #include "trace.h" 43 44 /* device_offload_lock is used to synchronize tls_dev_add 45 * against NETDEV_DOWN notifications. 46 */ 47 static DECLARE_RWSEM(device_offload_lock); 48 49 static struct workqueue_struct *destruct_wq __read_mostly; 50 51 static LIST_HEAD(tls_device_list); 52 static LIST_HEAD(tls_device_down_list); 53 static DEFINE_SPINLOCK(tls_device_lock); 54 55 static void tls_device_free_ctx(struct tls_context *ctx) 56 { 57 if (ctx->tx_conf == TLS_HW) { 58 kfree(tls_offload_ctx_tx(ctx)); 59 kfree(ctx->tx.rec_seq); 60 kfree(ctx->tx.iv); 61 } 62 63 if (ctx->rx_conf == TLS_HW) 64 kfree(tls_offload_ctx_rx(ctx)); 65 66 tls_ctx_free(NULL, ctx); 67 } 68 69 static void tls_device_tx_del_task(struct work_struct *work) 70 { 71 struct tls_offload_context_tx *offload_ctx = 72 container_of(work, struct tls_offload_context_tx, destruct_work); 73 struct tls_context *ctx = offload_ctx->ctx; 74 struct net_device *netdev; 75 76 /* Safe, because this is the destroy flow, refcount is 0, so 77 * tls_device_down can't store this field in parallel. 78 */ 79 netdev = rcu_dereference_protected(ctx->netdev, 80 !refcount_read(&ctx->refcount)); 81 82 netdev->tlsdev_ops->tls_dev_del(netdev, ctx, TLS_OFFLOAD_CTX_DIR_TX); 83 dev_put(netdev); 84 ctx->netdev = NULL; 85 tls_device_free_ctx(ctx); 86 } 87 88 static void tls_device_queue_ctx_destruction(struct tls_context *ctx) 89 { 90 struct net_device *netdev; 91 unsigned long flags; 92 bool async_cleanup; 93 94 spin_lock_irqsave(&tls_device_lock, flags); 95 if (unlikely(!refcount_dec_and_test(&ctx->refcount))) { 96 spin_unlock_irqrestore(&tls_device_lock, flags); 97 return; 98 } 99 100 list_del(&ctx->list); /* Remove from tls_device_list / tls_device_down_list */ 101 102 /* Safe, because this is the destroy flow, refcount is 0, so 103 * tls_device_down can't store this field in parallel. 104 */ 105 netdev = rcu_dereference_protected(ctx->netdev, 106 !refcount_read(&ctx->refcount)); 107 108 async_cleanup = netdev && ctx->tx_conf == TLS_HW; 109 if (async_cleanup) { 110 struct tls_offload_context_tx *offload_ctx = tls_offload_ctx_tx(ctx); 111 112 /* queue_work inside the spinlock 113 * to make sure tls_device_down waits for that work. 114 */ 115 queue_work(destruct_wq, &offload_ctx->destruct_work); 116 } 117 spin_unlock_irqrestore(&tls_device_lock, flags); 118 119 if (!async_cleanup) 120 tls_device_free_ctx(ctx); 121 } 122 123 /* We assume that the socket is already connected */ 124 static struct net_device *get_netdev_for_sock(struct sock *sk) 125 { 126 struct dst_entry *dst = sk_dst_get(sk); 127 struct net_device *netdev = NULL; 128 129 if (likely(dst)) { 130 netdev = netdev_sk_get_lowest_dev(dst->dev, sk); 131 dev_hold(netdev); 132 } 133 134 dst_release(dst); 135 136 return netdev; 137 } 138 139 static void destroy_record(struct tls_record_info *record) 140 { 141 int i; 142 143 for (i = 0; i < record->num_frags; i++) 144 __skb_frag_unref(&record->frags[i], false); 145 kfree(record); 146 } 147 148 static void delete_all_records(struct tls_offload_context_tx *offload_ctx) 149 { 150 struct tls_record_info *info, *temp; 151 152 list_for_each_entry_safe(info, temp, &offload_ctx->records_list, list) { 153 list_del(&info->list); 154 destroy_record(info); 155 } 156 157 offload_ctx->retransmit_hint = NULL; 158 } 159 160 static void tls_icsk_clean_acked(struct sock *sk, u32 acked_seq) 161 { 162 struct tls_context *tls_ctx = tls_get_ctx(sk); 163 struct tls_record_info *info, *temp; 164 struct tls_offload_context_tx *ctx; 165 u64 deleted_records = 0; 166 unsigned long flags; 167 168 if (!tls_ctx) 169 return; 170 171 ctx = tls_offload_ctx_tx(tls_ctx); 172 173 spin_lock_irqsave(&ctx->lock, flags); 174 info = ctx->retransmit_hint; 175 if (info && !before(acked_seq, info->end_seq)) 176 ctx->retransmit_hint = NULL; 177 178 list_for_each_entry_safe(info, temp, &ctx->records_list, list) { 179 if (before(acked_seq, info->end_seq)) 180 break; 181 list_del(&info->list); 182 183 destroy_record(info); 184 deleted_records++; 185 } 186 187 ctx->unacked_record_sn += deleted_records; 188 spin_unlock_irqrestore(&ctx->lock, flags); 189 } 190 191 /* At this point, there should be no references on this 192 * socket and no in-flight SKBs associated with this 193 * socket, so it is safe to free all the resources. 194 */ 195 void tls_device_sk_destruct(struct sock *sk) 196 { 197 struct tls_context *tls_ctx = tls_get_ctx(sk); 198 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx); 199 200 tls_ctx->sk_destruct(sk); 201 202 if (tls_ctx->tx_conf == TLS_HW) { 203 if (ctx->open_record) 204 destroy_record(ctx->open_record); 205 delete_all_records(ctx); 206 crypto_free_aead(ctx->aead_send); 207 clean_acked_data_disable(inet_csk(sk)); 208 } 209 210 tls_device_queue_ctx_destruction(tls_ctx); 211 } 212 EXPORT_SYMBOL_GPL(tls_device_sk_destruct); 213 214 void tls_device_free_resources_tx(struct sock *sk) 215 { 216 struct tls_context *tls_ctx = tls_get_ctx(sk); 217 218 tls_free_partial_record(sk, tls_ctx); 219 } 220 221 void tls_offload_tx_resync_request(struct sock *sk, u32 got_seq, u32 exp_seq) 222 { 223 struct tls_context *tls_ctx = tls_get_ctx(sk); 224 225 trace_tls_device_tx_resync_req(sk, got_seq, exp_seq); 226 WARN_ON(test_and_set_bit(TLS_TX_SYNC_SCHED, &tls_ctx->flags)); 227 } 228 EXPORT_SYMBOL_GPL(tls_offload_tx_resync_request); 229 230 static void tls_device_resync_tx(struct sock *sk, struct tls_context *tls_ctx, 231 u32 seq) 232 { 233 struct net_device *netdev; 234 struct sk_buff *skb; 235 int err = 0; 236 u8 *rcd_sn; 237 238 skb = tcp_write_queue_tail(sk); 239 if (skb) 240 TCP_SKB_CB(skb)->eor = 1; 241 242 rcd_sn = tls_ctx->tx.rec_seq; 243 244 trace_tls_device_tx_resync_send(sk, seq, rcd_sn); 245 down_read(&device_offload_lock); 246 netdev = rcu_dereference_protected(tls_ctx->netdev, 247 lockdep_is_held(&device_offload_lock)); 248 if (netdev) 249 err = netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq, 250 rcd_sn, 251 TLS_OFFLOAD_CTX_DIR_TX); 252 up_read(&device_offload_lock); 253 if (err) 254 return; 255 256 clear_bit_unlock(TLS_TX_SYNC_SCHED, &tls_ctx->flags); 257 } 258 259 static void tls_append_frag(struct tls_record_info *record, 260 struct page_frag *pfrag, 261 int size) 262 { 263 skb_frag_t *frag; 264 265 frag = &record->frags[record->num_frags - 1]; 266 if (skb_frag_page(frag) == pfrag->page && 267 skb_frag_off(frag) + skb_frag_size(frag) == pfrag->offset) { 268 skb_frag_size_add(frag, size); 269 } else { 270 ++frag; 271 skb_frag_fill_page_desc(frag, pfrag->page, pfrag->offset, 272 size); 273 ++record->num_frags; 274 get_page(pfrag->page); 275 } 276 277 pfrag->offset += size; 278 record->len += size; 279 } 280 281 static int tls_push_record(struct sock *sk, 282 struct tls_context *ctx, 283 struct tls_offload_context_tx *offload_ctx, 284 struct tls_record_info *record, 285 int flags) 286 { 287 struct tls_prot_info *prot = &ctx->prot_info; 288 struct tcp_sock *tp = tcp_sk(sk); 289 skb_frag_t *frag; 290 int i; 291 292 record->end_seq = tp->write_seq + record->len; 293 list_add_tail_rcu(&record->list, &offload_ctx->records_list); 294 offload_ctx->open_record = NULL; 295 296 if (test_bit(TLS_TX_SYNC_SCHED, &ctx->flags)) 297 tls_device_resync_tx(sk, ctx, tp->write_seq); 298 299 tls_advance_record_sn(sk, prot, &ctx->tx); 300 301 for (i = 0; i < record->num_frags; i++) { 302 frag = &record->frags[i]; 303 sg_unmark_end(&offload_ctx->sg_tx_data[i]); 304 sg_set_page(&offload_ctx->sg_tx_data[i], skb_frag_page(frag), 305 skb_frag_size(frag), skb_frag_off(frag)); 306 sk_mem_charge(sk, skb_frag_size(frag)); 307 get_page(skb_frag_page(frag)); 308 } 309 sg_mark_end(&offload_ctx->sg_tx_data[record->num_frags - 1]); 310 311 /* all ready, send */ 312 return tls_push_sg(sk, ctx, offload_ctx->sg_tx_data, 0, flags); 313 } 314 315 static int tls_device_record_close(struct sock *sk, 316 struct tls_context *ctx, 317 struct tls_record_info *record, 318 struct page_frag *pfrag, 319 unsigned char record_type) 320 { 321 struct tls_prot_info *prot = &ctx->prot_info; 322 int ret; 323 324 /* append tag 325 * device will fill in the tag, we just need to append a placeholder 326 * use socket memory to improve coalescing (re-using a single buffer 327 * increases frag count) 328 * if we can't allocate memory now, steal some back from data 329 */ 330 if (likely(skb_page_frag_refill(prot->tag_size, pfrag, 331 sk->sk_allocation))) { 332 ret = 0; 333 tls_append_frag(record, pfrag, prot->tag_size); 334 } else { 335 ret = prot->tag_size; 336 if (record->len <= prot->overhead_size) 337 return -ENOMEM; 338 } 339 340 /* fill prepend */ 341 tls_fill_prepend(ctx, skb_frag_address(&record->frags[0]), 342 record->len - prot->overhead_size, 343 record_type); 344 return ret; 345 } 346 347 static int tls_create_new_record(struct tls_offload_context_tx *offload_ctx, 348 struct page_frag *pfrag, 349 size_t prepend_size) 350 { 351 struct tls_record_info *record; 352 skb_frag_t *frag; 353 354 record = kmalloc(sizeof(*record), GFP_KERNEL); 355 if (!record) 356 return -ENOMEM; 357 358 frag = &record->frags[0]; 359 skb_frag_fill_page_desc(frag, pfrag->page, pfrag->offset, 360 prepend_size); 361 362 get_page(pfrag->page); 363 pfrag->offset += prepend_size; 364 365 record->num_frags = 1; 366 record->len = prepend_size; 367 offload_ctx->open_record = record; 368 return 0; 369 } 370 371 static int tls_do_allocation(struct sock *sk, 372 struct tls_offload_context_tx *offload_ctx, 373 struct page_frag *pfrag, 374 size_t prepend_size) 375 { 376 int ret; 377 378 if (!offload_ctx->open_record) { 379 if (unlikely(!skb_page_frag_refill(prepend_size, pfrag, 380 sk->sk_allocation))) { 381 READ_ONCE(sk->sk_prot)->enter_memory_pressure(sk); 382 sk_stream_moderate_sndbuf(sk); 383 return -ENOMEM; 384 } 385 386 ret = tls_create_new_record(offload_ctx, pfrag, prepend_size); 387 if (ret) 388 return ret; 389 390 if (pfrag->size > pfrag->offset) 391 return 0; 392 } 393 394 if (!sk_page_frag_refill(sk, pfrag)) 395 return -ENOMEM; 396 397 return 0; 398 } 399 400 static int tls_device_copy_data(void *addr, size_t bytes, struct iov_iter *i) 401 { 402 size_t pre_copy, nocache; 403 404 pre_copy = ~((unsigned long)addr - 1) & (SMP_CACHE_BYTES - 1); 405 if (pre_copy) { 406 pre_copy = min(pre_copy, bytes); 407 if (copy_from_iter(addr, pre_copy, i) != pre_copy) 408 return -EFAULT; 409 bytes -= pre_copy; 410 addr += pre_copy; 411 } 412 413 nocache = round_down(bytes, SMP_CACHE_BYTES); 414 if (copy_from_iter_nocache(addr, nocache, i) != nocache) 415 return -EFAULT; 416 bytes -= nocache; 417 addr += nocache; 418 419 if (bytes && copy_from_iter(addr, bytes, i) != bytes) 420 return -EFAULT; 421 422 return 0; 423 } 424 425 static int tls_push_data(struct sock *sk, 426 struct iov_iter *iter, 427 size_t size, int flags, 428 unsigned char record_type) 429 { 430 struct tls_context *tls_ctx = tls_get_ctx(sk); 431 struct tls_prot_info *prot = &tls_ctx->prot_info; 432 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx); 433 struct tls_record_info *record; 434 int tls_push_record_flags; 435 struct page_frag *pfrag; 436 size_t orig_size = size; 437 u32 max_open_record_len; 438 bool more = false; 439 bool done = false; 440 int copy, rc = 0; 441 long timeo; 442 443 if (flags & 444 ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | 445 MSG_SPLICE_PAGES | MSG_EOR)) 446 return -EOPNOTSUPP; 447 448 if ((flags & (MSG_MORE | MSG_EOR)) == (MSG_MORE | MSG_EOR)) 449 return -EINVAL; 450 451 if (unlikely(sk->sk_err)) 452 return -sk->sk_err; 453 454 flags |= MSG_SENDPAGE_DECRYPTED; 455 tls_push_record_flags = flags | MSG_MORE; 456 457 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); 458 if (tls_is_partially_sent_record(tls_ctx)) { 459 rc = tls_push_partial_record(sk, tls_ctx, flags); 460 if (rc < 0) 461 return rc; 462 } 463 464 pfrag = sk_page_frag(sk); 465 466 /* TLS_HEADER_SIZE is not counted as part of the TLS record, and 467 * we need to leave room for an authentication tag. 468 */ 469 max_open_record_len = TLS_MAX_PAYLOAD_SIZE + 470 prot->prepend_size; 471 do { 472 rc = tls_do_allocation(sk, ctx, pfrag, prot->prepend_size); 473 if (unlikely(rc)) { 474 rc = sk_stream_wait_memory(sk, &timeo); 475 if (!rc) 476 continue; 477 478 record = ctx->open_record; 479 if (!record) 480 break; 481 handle_error: 482 if (record_type != TLS_RECORD_TYPE_DATA) { 483 /* avoid sending partial 484 * record with type != 485 * application_data 486 */ 487 size = orig_size; 488 destroy_record(record); 489 ctx->open_record = NULL; 490 } else if (record->len > prot->prepend_size) { 491 goto last_record; 492 } 493 494 break; 495 } 496 497 record = ctx->open_record; 498 499 copy = min_t(size_t, size, max_open_record_len - record->len); 500 if (copy && (flags & MSG_SPLICE_PAGES)) { 501 struct page_frag zc_pfrag; 502 struct page **pages = &zc_pfrag.page; 503 size_t off; 504 505 rc = iov_iter_extract_pages(iter, &pages, 506 copy, 1, 0, &off); 507 if (rc <= 0) { 508 if (rc == 0) 509 rc = -EIO; 510 goto handle_error; 511 } 512 copy = rc; 513 514 if (WARN_ON_ONCE(!sendpage_ok(zc_pfrag.page))) { 515 iov_iter_revert(iter, copy); 516 rc = -EIO; 517 goto handle_error; 518 } 519 520 zc_pfrag.offset = off; 521 zc_pfrag.size = copy; 522 tls_append_frag(record, &zc_pfrag, copy); 523 } else if (copy) { 524 copy = min_t(size_t, copy, pfrag->size - pfrag->offset); 525 526 rc = tls_device_copy_data(page_address(pfrag->page) + 527 pfrag->offset, copy, 528 iter); 529 if (rc) 530 goto handle_error; 531 tls_append_frag(record, pfrag, copy); 532 } 533 534 size -= copy; 535 if (!size) { 536 last_record: 537 tls_push_record_flags = flags; 538 if (flags & MSG_MORE) { 539 more = true; 540 break; 541 } 542 543 done = true; 544 } 545 546 if (done || record->len >= max_open_record_len || 547 (record->num_frags >= MAX_SKB_FRAGS - 1)) { 548 rc = tls_device_record_close(sk, tls_ctx, record, 549 pfrag, record_type); 550 if (rc) { 551 if (rc > 0) { 552 size += rc; 553 } else { 554 size = orig_size; 555 destroy_record(record); 556 ctx->open_record = NULL; 557 break; 558 } 559 } 560 561 rc = tls_push_record(sk, 562 tls_ctx, 563 ctx, 564 record, 565 tls_push_record_flags); 566 if (rc < 0) 567 break; 568 } 569 } while (!done); 570 571 tls_ctx->pending_open_record_frags = more; 572 573 if (orig_size - size > 0) 574 rc = orig_size - size; 575 576 return rc; 577 } 578 579 int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) 580 { 581 unsigned char record_type = TLS_RECORD_TYPE_DATA; 582 struct tls_context *tls_ctx = tls_get_ctx(sk); 583 int rc; 584 585 if (!tls_ctx->zerocopy_sendfile) 586 msg->msg_flags &= ~MSG_SPLICE_PAGES; 587 588 mutex_lock(&tls_ctx->tx_lock); 589 lock_sock(sk); 590 591 if (unlikely(msg->msg_controllen)) { 592 rc = tls_process_cmsg(sk, msg, &record_type); 593 if (rc) 594 goto out; 595 } 596 597 rc = tls_push_data(sk, &msg->msg_iter, size, msg->msg_flags, 598 record_type); 599 600 out: 601 release_sock(sk); 602 mutex_unlock(&tls_ctx->tx_lock); 603 return rc; 604 } 605 606 void tls_device_splice_eof(struct socket *sock) 607 { 608 struct sock *sk = sock->sk; 609 struct tls_context *tls_ctx = tls_get_ctx(sk); 610 struct iov_iter iter = {}; 611 612 if (!tls_is_partially_sent_record(tls_ctx)) 613 return; 614 615 mutex_lock(&tls_ctx->tx_lock); 616 lock_sock(sk); 617 618 if (tls_is_partially_sent_record(tls_ctx)) { 619 iov_iter_bvec(&iter, ITER_SOURCE, NULL, 0, 0); 620 tls_push_data(sk, &iter, 0, 0, TLS_RECORD_TYPE_DATA); 621 } 622 623 release_sock(sk); 624 mutex_unlock(&tls_ctx->tx_lock); 625 } 626 627 struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context, 628 u32 seq, u64 *p_record_sn) 629 { 630 u64 record_sn = context->hint_record_sn; 631 struct tls_record_info *info, *last; 632 633 info = context->retransmit_hint; 634 if (!info || 635 before(seq, info->end_seq - info->len)) { 636 /* if retransmit_hint is irrelevant start 637 * from the beginning of the list 638 */ 639 info = list_first_entry_or_null(&context->records_list, 640 struct tls_record_info, list); 641 if (!info) 642 return NULL; 643 /* send the start_marker record if seq number is before the 644 * tls offload start marker sequence number. This record is 645 * required to handle TCP packets which are before TLS offload 646 * started. 647 * And if it's not start marker, look if this seq number 648 * belongs to the list. 649 */ 650 if (likely(!tls_record_is_start_marker(info))) { 651 /* we have the first record, get the last record to see 652 * if this seq number belongs to the list. 653 */ 654 last = list_last_entry(&context->records_list, 655 struct tls_record_info, list); 656 657 if (!between(seq, tls_record_start_seq(info), 658 last->end_seq)) 659 return NULL; 660 } 661 record_sn = context->unacked_record_sn; 662 } 663 664 /* We just need the _rcu for the READ_ONCE() */ 665 rcu_read_lock(); 666 list_for_each_entry_from_rcu(info, &context->records_list, list) { 667 if (before(seq, info->end_seq)) { 668 if (!context->retransmit_hint || 669 after(info->end_seq, 670 context->retransmit_hint->end_seq)) { 671 context->hint_record_sn = record_sn; 672 context->retransmit_hint = info; 673 } 674 *p_record_sn = record_sn; 675 goto exit_rcu_unlock; 676 } 677 record_sn++; 678 } 679 info = NULL; 680 681 exit_rcu_unlock: 682 rcu_read_unlock(); 683 return info; 684 } 685 EXPORT_SYMBOL(tls_get_record); 686 687 static int tls_device_push_pending_record(struct sock *sk, int flags) 688 { 689 struct iov_iter iter; 690 691 iov_iter_kvec(&iter, ITER_SOURCE, NULL, 0, 0); 692 return tls_push_data(sk, &iter, 0, flags, TLS_RECORD_TYPE_DATA); 693 } 694 695 void tls_device_write_space(struct sock *sk, struct tls_context *ctx) 696 { 697 if (tls_is_partially_sent_record(ctx)) { 698 gfp_t sk_allocation = sk->sk_allocation; 699 700 WARN_ON_ONCE(sk->sk_write_pending); 701 702 sk->sk_allocation = GFP_ATOMIC; 703 tls_push_partial_record(sk, ctx, 704 MSG_DONTWAIT | MSG_NOSIGNAL | 705 MSG_SENDPAGE_DECRYPTED); 706 sk->sk_allocation = sk_allocation; 707 } 708 } 709 710 static void tls_device_resync_rx(struct tls_context *tls_ctx, 711 struct sock *sk, u32 seq, u8 *rcd_sn) 712 { 713 struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx); 714 struct net_device *netdev; 715 716 trace_tls_device_rx_resync_send(sk, seq, rcd_sn, rx_ctx->resync_type); 717 rcu_read_lock(); 718 netdev = rcu_dereference(tls_ctx->netdev); 719 if (netdev) 720 netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq, rcd_sn, 721 TLS_OFFLOAD_CTX_DIR_RX); 722 rcu_read_unlock(); 723 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXDEVICERESYNC); 724 } 725 726 static bool 727 tls_device_rx_resync_async(struct tls_offload_resync_async *resync_async, 728 s64 resync_req, u32 *seq, u16 *rcd_delta) 729 { 730 u32 is_async = resync_req & RESYNC_REQ_ASYNC; 731 u32 req_seq = resync_req >> 32; 732 u32 req_end = req_seq + ((resync_req >> 16) & 0xffff); 733 u16 i; 734 735 *rcd_delta = 0; 736 737 if (is_async) { 738 /* shouldn't get to wraparound: 739 * too long in async stage, something bad happened 740 */ 741 if (WARN_ON_ONCE(resync_async->rcd_delta == USHRT_MAX)) 742 return false; 743 744 /* asynchronous stage: log all headers seq such that 745 * req_seq <= seq <= end_seq, and wait for real resync request 746 */ 747 if (before(*seq, req_seq)) 748 return false; 749 if (!after(*seq, req_end) && 750 resync_async->loglen < TLS_DEVICE_RESYNC_ASYNC_LOGMAX) 751 resync_async->log[resync_async->loglen++] = *seq; 752 753 resync_async->rcd_delta++; 754 755 return false; 756 } 757 758 /* synchronous stage: check against the logged entries and 759 * proceed to check the next entries if no match was found 760 */ 761 for (i = 0; i < resync_async->loglen; i++) 762 if (req_seq == resync_async->log[i] && 763 atomic64_try_cmpxchg(&resync_async->req, &resync_req, 0)) { 764 *rcd_delta = resync_async->rcd_delta - i; 765 *seq = req_seq; 766 resync_async->loglen = 0; 767 resync_async->rcd_delta = 0; 768 return true; 769 } 770 771 resync_async->loglen = 0; 772 resync_async->rcd_delta = 0; 773 774 if (req_seq == *seq && 775 atomic64_try_cmpxchg(&resync_async->req, 776 &resync_req, 0)) 777 return true; 778 779 return false; 780 } 781 782 void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq) 783 { 784 struct tls_context *tls_ctx = tls_get_ctx(sk); 785 struct tls_offload_context_rx *rx_ctx; 786 u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE]; 787 u32 sock_data, is_req_pending; 788 struct tls_prot_info *prot; 789 s64 resync_req; 790 u16 rcd_delta; 791 u32 req_seq; 792 793 if (tls_ctx->rx_conf != TLS_HW) 794 return; 795 if (unlikely(test_bit(TLS_RX_DEV_DEGRADED, &tls_ctx->flags))) 796 return; 797 798 prot = &tls_ctx->prot_info; 799 rx_ctx = tls_offload_ctx_rx(tls_ctx); 800 memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size); 801 802 switch (rx_ctx->resync_type) { 803 case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ: 804 resync_req = atomic64_read(&rx_ctx->resync_req); 805 req_seq = resync_req >> 32; 806 seq += TLS_HEADER_SIZE - 1; 807 is_req_pending = resync_req; 808 809 if (likely(!is_req_pending) || req_seq != seq || 810 !atomic64_try_cmpxchg(&rx_ctx->resync_req, &resync_req, 0)) 811 return; 812 break; 813 case TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT: 814 if (likely(!rx_ctx->resync_nh_do_now)) 815 return; 816 817 /* head of next rec is already in, note that the sock_inq will 818 * include the currently parsed message when called from parser 819 */ 820 sock_data = tcp_inq(sk); 821 if (sock_data > rcd_len) { 822 trace_tls_device_rx_resync_nh_delay(sk, sock_data, 823 rcd_len); 824 return; 825 } 826 827 rx_ctx->resync_nh_do_now = 0; 828 seq += rcd_len; 829 tls_bigint_increment(rcd_sn, prot->rec_seq_size); 830 break; 831 case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC: 832 resync_req = atomic64_read(&rx_ctx->resync_async->req); 833 is_req_pending = resync_req; 834 if (likely(!is_req_pending)) 835 return; 836 837 if (!tls_device_rx_resync_async(rx_ctx->resync_async, 838 resync_req, &seq, &rcd_delta)) 839 return; 840 tls_bigint_subtract(rcd_sn, rcd_delta); 841 break; 842 } 843 844 tls_device_resync_rx(tls_ctx, sk, seq, rcd_sn); 845 } 846 847 static void tls_device_core_ctrl_rx_resync(struct tls_context *tls_ctx, 848 struct tls_offload_context_rx *ctx, 849 struct sock *sk, struct sk_buff *skb) 850 { 851 struct strp_msg *rxm; 852 853 /* device will request resyncs by itself based on stream scan */ 854 if (ctx->resync_type != TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT) 855 return; 856 /* already scheduled */ 857 if (ctx->resync_nh_do_now) 858 return; 859 /* seen decrypted fragments since last fully-failed record */ 860 if (ctx->resync_nh_reset) { 861 ctx->resync_nh_reset = 0; 862 ctx->resync_nh.decrypted_failed = 1; 863 ctx->resync_nh.decrypted_tgt = TLS_DEVICE_RESYNC_NH_START_IVAL; 864 return; 865 } 866 867 if (++ctx->resync_nh.decrypted_failed <= ctx->resync_nh.decrypted_tgt) 868 return; 869 870 /* doing resync, bump the next target in case it fails */ 871 if (ctx->resync_nh.decrypted_tgt < TLS_DEVICE_RESYNC_NH_MAX_IVAL) 872 ctx->resync_nh.decrypted_tgt *= 2; 873 else 874 ctx->resync_nh.decrypted_tgt += TLS_DEVICE_RESYNC_NH_MAX_IVAL; 875 876 rxm = strp_msg(skb); 877 878 /* head of next rec is already in, parser will sync for us */ 879 if (tcp_inq(sk) > rxm->full_len) { 880 trace_tls_device_rx_resync_nh_schedule(sk); 881 ctx->resync_nh_do_now = 1; 882 } else { 883 struct tls_prot_info *prot = &tls_ctx->prot_info; 884 u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE]; 885 886 memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size); 887 tls_bigint_increment(rcd_sn, prot->rec_seq_size); 888 889 tls_device_resync_rx(tls_ctx, sk, tcp_sk(sk)->copied_seq, 890 rcd_sn); 891 } 892 } 893 894 static int 895 tls_device_reencrypt(struct sock *sk, struct tls_context *tls_ctx) 896 { 897 struct tls_sw_context_rx *sw_ctx = tls_sw_ctx_rx(tls_ctx); 898 const struct tls_cipher_size_desc *cipher_sz; 899 int err, offset, copy, data_len, pos; 900 struct sk_buff *skb, *skb_iter; 901 struct scatterlist sg[1]; 902 struct strp_msg *rxm; 903 char *orig_buf, *buf; 904 905 switch (tls_ctx->crypto_recv.info.cipher_type) { 906 case TLS_CIPHER_AES_GCM_128: 907 case TLS_CIPHER_AES_GCM_256: 908 break; 909 default: 910 return -EINVAL; 911 } 912 cipher_sz = &tls_cipher_size_desc[tls_ctx->crypto_recv.info.cipher_type]; 913 914 rxm = strp_msg(tls_strp_msg(sw_ctx)); 915 orig_buf = kmalloc(rxm->full_len + TLS_HEADER_SIZE + cipher_sz->iv, 916 sk->sk_allocation); 917 if (!orig_buf) 918 return -ENOMEM; 919 buf = orig_buf; 920 921 err = tls_strp_msg_cow(sw_ctx); 922 if (unlikely(err)) 923 goto free_buf; 924 925 skb = tls_strp_msg(sw_ctx); 926 rxm = strp_msg(skb); 927 offset = rxm->offset; 928 929 sg_init_table(sg, 1); 930 sg_set_buf(&sg[0], buf, 931 rxm->full_len + TLS_HEADER_SIZE + cipher_sz->iv); 932 err = skb_copy_bits(skb, offset, buf, TLS_HEADER_SIZE + cipher_sz->iv); 933 if (err) 934 goto free_buf; 935 936 /* We are interested only in the decrypted data not the auth */ 937 err = decrypt_skb(sk, sg); 938 if (err != -EBADMSG) 939 goto free_buf; 940 else 941 err = 0; 942 943 data_len = rxm->full_len - cipher_sz->tag; 944 945 if (skb_pagelen(skb) > offset) { 946 copy = min_t(int, skb_pagelen(skb) - offset, data_len); 947 948 if (skb->decrypted) { 949 err = skb_store_bits(skb, offset, buf, copy); 950 if (err) 951 goto free_buf; 952 } 953 954 offset += copy; 955 buf += copy; 956 } 957 958 pos = skb_pagelen(skb); 959 skb_walk_frags(skb, skb_iter) { 960 int frag_pos; 961 962 /* Practically all frags must belong to msg if reencrypt 963 * is needed with current strparser and coalescing logic, 964 * but strparser may "get optimized", so let's be safe. 965 */ 966 if (pos + skb_iter->len <= offset) 967 goto done_with_frag; 968 if (pos >= data_len + rxm->offset) 969 break; 970 971 frag_pos = offset - pos; 972 copy = min_t(int, skb_iter->len - frag_pos, 973 data_len + rxm->offset - offset); 974 975 if (skb_iter->decrypted) { 976 err = skb_store_bits(skb_iter, frag_pos, buf, copy); 977 if (err) 978 goto free_buf; 979 } 980 981 offset += copy; 982 buf += copy; 983 done_with_frag: 984 pos += skb_iter->len; 985 } 986 987 free_buf: 988 kfree(orig_buf); 989 return err; 990 } 991 992 int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx) 993 { 994 struct tls_offload_context_rx *ctx = tls_offload_ctx_rx(tls_ctx); 995 struct tls_sw_context_rx *sw_ctx = tls_sw_ctx_rx(tls_ctx); 996 struct sk_buff *skb = tls_strp_msg(sw_ctx); 997 struct strp_msg *rxm = strp_msg(skb); 998 int is_decrypted, is_encrypted; 999 1000 if (!tls_strp_msg_mixed_decrypted(sw_ctx)) { 1001 is_decrypted = skb->decrypted; 1002 is_encrypted = !is_decrypted; 1003 } else { 1004 is_decrypted = 0; 1005 is_encrypted = 0; 1006 } 1007 1008 trace_tls_device_decrypted(sk, tcp_sk(sk)->copied_seq - rxm->full_len, 1009 tls_ctx->rx.rec_seq, rxm->full_len, 1010 is_encrypted, is_decrypted); 1011 1012 if (unlikely(test_bit(TLS_RX_DEV_DEGRADED, &tls_ctx->flags))) { 1013 if (likely(is_encrypted || is_decrypted)) 1014 return is_decrypted; 1015 1016 /* After tls_device_down disables the offload, the next SKB will 1017 * likely have initial fragments decrypted, and final ones not 1018 * decrypted. We need to reencrypt that single SKB. 1019 */ 1020 return tls_device_reencrypt(sk, tls_ctx); 1021 } 1022 1023 /* Return immediately if the record is either entirely plaintext or 1024 * entirely ciphertext. Otherwise handle reencrypt partially decrypted 1025 * record. 1026 */ 1027 if (is_decrypted) { 1028 ctx->resync_nh_reset = 1; 1029 return is_decrypted; 1030 } 1031 if (is_encrypted) { 1032 tls_device_core_ctrl_rx_resync(tls_ctx, ctx, sk, skb); 1033 return 0; 1034 } 1035 1036 ctx->resync_nh_reset = 1; 1037 return tls_device_reencrypt(sk, tls_ctx); 1038 } 1039 1040 static void tls_device_attach(struct tls_context *ctx, struct sock *sk, 1041 struct net_device *netdev) 1042 { 1043 if (sk->sk_destruct != tls_device_sk_destruct) { 1044 refcount_set(&ctx->refcount, 1); 1045 dev_hold(netdev); 1046 RCU_INIT_POINTER(ctx->netdev, netdev); 1047 spin_lock_irq(&tls_device_lock); 1048 list_add_tail(&ctx->list, &tls_device_list); 1049 spin_unlock_irq(&tls_device_lock); 1050 1051 ctx->sk_destruct = sk->sk_destruct; 1052 smp_store_release(&sk->sk_destruct, tls_device_sk_destruct); 1053 } 1054 } 1055 1056 int tls_set_device_offload(struct sock *sk, struct tls_context *ctx) 1057 { 1058 struct tls_context *tls_ctx = tls_get_ctx(sk); 1059 struct tls_prot_info *prot = &tls_ctx->prot_info; 1060 const struct tls_cipher_size_desc *cipher_sz; 1061 struct tls_record_info *start_marker_record; 1062 struct tls_offload_context_tx *offload_ctx; 1063 struct tls_crypto_info *crypto_info; 1064 struct net_device *netdev; 1065 char *iv, *rec_seq; 1066 struct sk_buff *skb; 1067 __be64 rcd_sn; 1068 int rc; 1069 1070 if (!ctx) 1071 return -EINVAL; 1072 1073 if (ctx->priv_ctx_tx) 1074 return -EEXIST; 1075 1076 netdev = get_netdev_for_sock(sk); 1077 if (!netdev) { 1078 pr_err_ratelimited("%s: netdev not found\n", __func__); 1079 return -EINVAL; 1080 } 1081 1082 if (!(netdev->features & NETIF_F_HW_TLS_TX)) { 1083 rc = -EOPNOTSUPP; 1084 goto release_netdev; 1085 } 1086 1087 crypto_info = &ctx->crypto_send.info; 1088 if (crypto_info->version != TLS_1_2_VERSION) { 1089 rc = -EOPNOTSUPP; 1090 goto release_netdev; 1091 } 1092 1093 switch (crypto_info->cipher_type) { 1094 case TLS_CIPHER_AES_GCM_128: 1095 iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv; 1096 rec_seq = 1097 ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq; 1098 break; 1099 case TLS_CIPHER_AES_GCM_256: 1100 iv = ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->iv; 1101 rec_seq = 1102 ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->rec_seq; 1103 break; 1104 default: 1105 rc = -EINVAL; 1106 goto release_netdev; 1107 } 1108 cipher_sz = &tls_cipher_size_desc[crypto_info->cipher_type]; 1109 1110 /* Sanity-check the rec_seq_size for stack allocations */ 1111 if (cipher_sz->rec_seq > TLS_MAX_REC_SEQ_SIZE) { 1112 rc = -EINVAL; 1113 goto release_netdev; 1114 } 1115 1116 prot->version = crypto_info->version; 1117 prot->cipher_type = crypto_info->cipher_type; 1118 prot->prepend_size = TLS_HEADER_SIZE + cipher_sz->iv; 1119 prot->tag_size = cipher_sz->tag; 1120 prot->overhead_size = prot->prepend_size + prot->tag_size; 1121 prot->iv_size = cipher_sz->iv; 1122 prot->salt_size = cipher_sz->salt; 1123 ctx->tx.iv = kmalloc(cipher_sz->iv + cipher_sz->salt, GFP_KERNEL); 1124 if (!ctx->tx.iv) { 1125 rc = -ENOMEM; 1126 goto release_netdev; 1127 } 1128 1129 memcpy(ctx->tx.iv + cipher_sz->salt, iv, cipher_sz->iv); 1130 1131 prot->rec_seq_size = cipher_sz->rec_seq; 1132 ctx->tx.rec_seq = kmemdup(rec_seq, cipher_sz->rec_seq, GFP_KERNEL); 1133 if (!ctx->tx.rec_seq) { 1134 rc = -ENOMEM; 1135 goto free_iv; 1136 } 1137 1138 start_marker_record = kmalloc(sizeof(*start_marker_record), GFP_KERNEL); 1139 if (!start_marker_record) { 1140 rc = -ENOMEM; 1141 goto free_rec_seq; 1142 } 1143 1144 offload_ctx = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_TX, GFP_KERNEL); 1145 if (!offload_ctx) { 1146 rc = -ENOMEM; 1147 goto free_marker_record; 1148 } 1149 1150 rc = tls_sw_fallback_init(sk, offload_ctx, crypto_info); 1151 if (rc) 1152 goto free_offload_ctx; 1153 1154 /* start at rec_seq - 1 to account for the start marker record */ 1155 memcpy(&rcd_sn, ctx->tx.rec_seq, sizeof(rcd_sn)); 1156 offload_ctx->unacked_record_sn = be64_to_cpu(rcd_sn) - 1; 1157 1158 start_marker_record->end_seq = tcp_sk(sk)->write_seq; 1159 start_marker_record->len = 0; 1160 start_marker_record->num_frags = 0; 1161 1162 INIT_WORK(&offload_ctx->destruct_work, tls_device_tx_del_task); 1163 offload_ctx->ctx = ctx; 1164 1165 INIT_LIST_HEAD(&offload_ctx->records_list); 1166 list_add_tail(&start_marker_record->list, &offload_ctx->records_list); 1167 spin_lock_init(&offload_ctx->lock); 1168 sg_init_table(offload_ctx->sg_tx_data, 1169 ARRAY_SIZE(offload_ctx->sg_tx_data)); 1170 1171 clean_acked_data_enable(inet_csk(sk), &tls_icsk_clean_acked); 1172 ctx->push_pending_record = tls_device_push_pending_record; 1173 1174 /* TLS offload is greatly simplified if we don't send 1175 * SKBs where only part of the payload needs to be encrypted. 1176 * So mark the last skb in the write queue as end of record. 1177 */ 1178 skb = tcp_write_queue_tail(sk); 1179 if (skb) 1180 TCP_SKB_CB(skb)->eor = 1; 1181 1182 /* Avoid offloading if the device is down 1183 * We don't want to offload new flows after 1184 * the NETDEV_DOWN event 1185 * 1186 * device_offload_lock is taken in tls_devices's NETDEV_DOWN 1187 * handler thus protecting from the device going down before 1188 * ctx was added to tls_device_list. 1189 */ 1190 down_read(&device_offload_lock); 1191 if (!(netdev->flags & IFF_UP)) { 1192 rc = -EINVAL; 1193 goto release_lock; 1194 } 1195 1196 ctx->priv_ctx_tx = offload_ctx; 1197 rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_TX, 1198 &ctx->crypto_send.info, 1199 tcp_sk(sk)->write_seq); 1200 trace_tls_device_offload_set(sk, TLS_OFFLOAD_CTX_DIR_TX, 1201 tcp_sk(sk)->write_seq, rec_seq, rc); 1202 if (rc) 1203 goto release_lock; 1204 1205 tls_device_attach(ctx, sk, netdev); 1206 up_read(&device_offload_lock); 1207 1208 /* following this assignment tls_is_skb_tx_device_offloaded 1209 * will return true and the context might be accessed 1210 * by the netdev's xmit function. 1211 */ 1212 smp_store_release(&sk->sk_validate_xmit_skb, tls_validate_xmit_skb); 1213 dev_put(netdev); 1214 1215 return 0; 1216 1217 release_lock: 1218 up_read(&device_offload_lock); 1219 clean_acked_data_disable(inet_csk(sk)); 1220 crypto_free_aead(offload_ctx->aead_send); 1221 free_offload_ctx: 1222 kfree(offload_ctx); 1223 ctx->priv_ctx_tx = NULL; 1224 free_marker_record: 1225 kfree(start_marker_record); 1226 free_rec_seq: 1227 kfree(ctx->tx.rec_seq); 1228 free_iv: 1229 kfree(ctx->tx.iv); 1230 release_netdev: 1231 dev_put(netdev); 1232 return rc; 1233 } 1234 1235 int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx) 1236 { 1237 struct tls12_crypto_info_aes_gcm_128 *info; 1238 struct tls_offload_context_rx *context; 1239 struct net_device *netdev; 1240 int rc = 0; 1241 1242 if (ctx->crypto_recv.info.version != TLS_1_2_VERSION) 1243 return -EOPNOTSUPP; 1244 1245 netdev = get_netdev_for_sock(sk); 1246 if (!netdev) { 1247 pr_err_ratelimited("%s: netdev not found\n", __func__); 1248 return -EINVAL; 1249 } 1250 1251 if (!(netdev->features & NETIF_F_HW_TLS_RX)) { 1252 rc = -EOPNOTSUPP; 1253 goto release_netdev; 1254 } 1255 1256 /* Avoid offloading if the device is down 1257 * We don't want to offload new flows after 1258 * the NETDEV_DOWN event 1259 * 1260 * device_offload_lock is taken in tls_devices's NETDEV_DOWN 1261 * handler thus protecting from the device going down before 1262 * ctx was added to tls_device_list. 1263 */ 1264 down_read(&device_offload_lock); 1265 if (!(netdev->flags & IFF_UP)) { 1266 rc = -EINVAL; 1267 goto release_lock; 1268 } 1269 1270 context = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_RX, GFP_KERNEL); 1271 if (!context) { 1272 rc = -ENOMEM; 1273 goto release_lock; 1274 } 1275 context->resync_nh_reset = 1; 1276 1277 ctx->priv_ctx_rx = context; 1278 rc = tls_set_sw_offload(sk, ctx, 0); 1279 if (rc) 1280 goto release_ctx; 1281 1282 rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_RX, 1283 &ctx->crypto_recv.info, 1284 tcp_sk(sk)->copied_seq); 1285 info = (void *)&ctx->crypto_recv.info; 1286 trace_tls_device_offload_set(sk, TLS_OFFLOAD_CTX_DIR_RX, 1287 tcp_sk(sk)->copied_seq, info->rec_seq, rc); 1288 if (rc) 1289 goto free_sw_resources; 1290 1291 tls_device_attach(ctx, sk, netdev); 1292 up_read(&device_offload_lock); 1293 1294 dev_put(netdev); 1295 1296 return 0; 1297 1298 free_sw_resources: 1299 up_read(&device_offload_lock); 1300 tls_sw_free_resources_rx(sk); 1301 down_read(&device_offload_lock); 1302 release_ctx: 1303 ctx->priv_ctx_rx = NULL; 1304 release_lock: 1305 up_read(&device_offload_lock); 1306 release_netdev: 1307 dev_put(netdev); 1308 return rc; 1309 } 1310 1311 void tls_device_offload_cleanup_rx(struct sock *sk) 1312 { 1313 struct tls_context *tls_ctx = tls_get_ctx(sk); 1314 struct net_device *netdev; 1315 1316 down_read(&device_offload_lock); 1317 netdev = rcu_dereference_protected(tls_ctx->netdev, 1318 lockdep_is_held(&device_offload_lock)); 1319 if (!netdev) 1320 goto out; 1321 1322 netdev->tlsdev_ops->tls_dev_del(netdev, tls_ctx, 1323 TLS_OFFLOAD_CTX_DIR_RX); 1324 1325 if (tls_ctx->tx_conf != TLS_HW) { 1326 dev_put(netdev); 1327 rcu_assign_pointer(tls_ctx->netdev, NULL); 1328 } else { 1329 set_bit(TLS_RX_DEV_CLOSED, &tls_ctx->flags); 1330 } 1331 out: 1332 up_read(&device_offload_lock); 1333 tls_sw_release_resources_rx(sk); 1334 } 1335 1336 static int tls_device_down(struct net_device *netdev) 1337 { 1338 struct tls_context *ctx, *tmp; 1339 unsigned long flags; 1340 LIST_HEAD(list); 1341 1342 /* Request a write lock to block new offload attempts */ 1343 down_write(&device_offload_lock); 1344 1345 spin_lock_irqsave(&tls_device_lock, flags); 1346 list_for_each_entry_safe(ctx, tmp, &tls_device_list, list) { 1347 struct net_device *ctx_netdev = 1348 rcu_dereference_protected(ctx->netdev, 1349 lockdep_is_held(&device_offload_lock)); 1350 1351 if (ctx_netdev != netdev || 1352 !refcount_inc_not_zero(&ctx->refcount)) 1353 continue; 1354 1355 list_move(&ctx->list, &list); 1356 } 1357 spin_unlock_irqrestore(&tls_device_lock, flags); 1358 1359 list_for_each_entry_safe(ctx, tmp, &list, list) { 1360 /* Stop offloaded TX and switch to the fallback. 1361 * tls_is_skb_tx_device_offloaded will return false. 1362 */ 1363 WRITE_ONCE(ctx->sk->sk_validate_xmit_skb, tls_validate_xmit_skb_sw); 1364 1365 /* Stop the RX and TX resync. 1366 * tls_dev_resync must not be called after tls_dev_del. 1367 */ 1368 rcu_assign_pointer(ctx->netdev, NULL); 1369 1370 /* Start skipping the RX resync logic completely. */ 1371 set_bit(TLS_RX_DEV_DEGRADED, &ctx->flags); 1372 1373 /* Sync with inflight packets. After this point: 1374 * TX: no non-encrypted packets will be passed to the driver. 1375 * RX: resync requests from the driver will be ignored. 1376 */ 1377 synchronize_net(); 1378 1379 /* Release the offload context on the driver side. */ 1380 if (ctx->tx_conf == TLS_HW) 1381 netdev->tlsdev_ops->tls_dev_del(netdev, ctx, 1382 TLS_OFFLOAD_CTX_DIR_TX); 1383 if (ctx->rx_conf == TLS_HW && 1384 !test_bit(TLS_RX_DEV_CLOSED, &ctx->flags)) 1385 netdev->tlsdev_ops->tls_dev_del(netdev, ctx, 1386 TLS_OFFLOAD_CTX_DIR_RX); 1387 1388 dev_put(netdev); 1389 1390 /* Move the context to a separate list for two reasons: 1391 * 1. When the context is deallocated, list_del is called. 1392 * 2. It's no longer an offloaded context, so we don't want to 1393 * run offload-specific code on this context. 1394 */ 1395 spin_lock_irqsave(&tls_device_lock, flags); 1396 list_move_tail(&ctx->list, &tls_device_down_list); 1397 spin_unlock_irqrestore(&tls_device_lock, flags); 1398 1399 /* Device contexts for RX and TX will be freed in on sk_destruct 1400 * by tls_device_free_ctx. rx_conf and tx_conf stay in TLS_HW. 1401 * Now release the ref taken above. 1402 */ 1403 if (refcount_dec_and_test(&ctx->refcount)) { 1404 /* sk_destruct ran after tls_device_down took a ref, and 1405 * it returned early. Complete the destruction here. 1406 */ 1407 list_del(&ctx->list); 1408 tls_device_free_ctx(ctx); 1409 } 1410 } 1411 1412 up_write(&device_offload_lock); 1413 1414 flush_workqueue(destruct_wq); 1415 1416 return NOTIFY_DONE; 1417 } 1418 1419 static int tls_dev_event(struct notifier_block *this, unsigned long event, 1420 void *ptr) 1421 { 1422 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1423 1424 if (!dev->tlsdev_ops && 1425 !(dev->features & (NETIF_F_HW_TLS_RX | NETIF_F_HW_TLS_TX))) 1426 return NOTIFY_DONE; 1427 1428 switch (event) { 1429 case NETDEV_REGISTER: 1430 case NETDEV_FEAT_CHANGE: 1431 if (netif_is_bond_master(dev)) 1432 return NOTIFY_DONE; 1433 if ((dev->features & NETIF_F_HW_TLS_RX) && 1434 !dev->tlsdev_ops->tls_dev_resync) 1435 return NOTIFY_BAD; 1436 1437 if (dev->tlsdev_ops && 1438 dev->tlsdev_ops->tls_dev_add && 1439 dev->tlsdev_ops->tls_dev_del) 1440 return NOTIFY_DONE; 1441 else 1442 return NOTIFY_BAD; 1443 case NETDEV_DOWN: 1444 return tls_device_down(dev); 1445 } 1446 return NOTIFY_DONE; 1447 } 1448 1449 static struct notifier_block tls_dev_notifier = { 1450 .notifier_call = tls_dev_event, 1451 }; 1452 1453 int __init tls_device_init(void) 1454 { 1455 int err; 1456 1457 destruct_wq = alloc_workqueue("ktls_device_destruct", 0, 0); 1458 if (!destruct_wq) 1459 return -ENOMEM; 1460 1461 err = register_netdevice_notifier(&tls_dev_notifier); 1462 if (err) 1463 destroy_workqueue(destruct_wq); 1464 1465 return err; 1466 } 1467 1468 void __exit tls_device_cleanup(void) 1469 { 1470 unregister_netdevice_notifier(&tls_dev_notifier); 1471 destroy_workqueue(destruct_wq); 1472 clean_acked_data_flush(); 1473 } 1474