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