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