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