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