| xj | b04a402 | 2021-11-25 15:01:52 +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(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_attach(struct tls_context *ctx, struct sock *sk, |
| 93 | struct net_device *netdev) |
| 94 | { |
| 95 | if (sk->sk_destruct != tls_device_sk_destruct) { |
| 96 | refcount_set(&ctx->refcount, 1); |
| 97 | dev_hold(netdev); |
| 98 | ctx->netdev = netdev; |
| 99 | spin_lock_irq(&tls_device_lock); |
| 100 | list_add_tail(&ctx->list, &tls_device_list); |
| 101 | spin_unlock_irq(&tls_device_lock); |
| 102 | |
| 103 | ctx->sk_destruct = sk->sk_destruct; |
| 104 | sk->sk_destruct = tls_device_sk_destruct; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static void tls_device_queue_ctx_destruction(struct tls_context *ctx) |
| 109 | { |
| 110 | unsigned long flags; |
| 111 | |
| 112 | spin_lock_irqsave(&tls_device_lock, flags); |
| 113 | list_move_tail(&ctx->list, &tls_device_gc_list); |
| 114 | |
| 115 | /* schedule_work inside the spinlock |
| 116 | * to make sure tls_device_down waits for that work. |
| 117 | */ |
| 118 | schedule_work(&tls_device_gc_work); |
| 119 | |
| 120 | spin_unlock_irqrestore(&tls_device_lock, flags); |
| 121 | } |
| 122 | |
| 123 | /* We assume that the socket is already connected */ |
| 124 | static struct net_device *get_netdev_for_sock(struct sock *sk) |
| 125 | { |
| 126 | struct dst_entry *dst = sk_dst_get(sk); |
| 127 | struct net_device *netdev = NULL; |
| 128 | |
| 129 | if (likely(dst)) { |
| 130 | netdev = dst->dev; |
| 131 | dev_hold(netdev); |
| 132 | } |
| 133 | |
| 134 | dst_release(dst); |
| 135 | |
| 136 | return netdev; |
| 137 | } |
| 138 | |
| 139 | static void destroy_record(struct tls_record_info *record) |
| 140 | { |
| 141 | int nr_frags = record->num_frags; |
| 142 | skb_frag_t *frag; |
| 143 | |
| 144 | while (nr_frags-- > 0) { |
| 145 | frag = &record->frags[nr_frags]; |
| 146 | __skb_frag_unref(frag); |
| 147 | } |
| 148 | kfree(record); |
| 149 | } |
| 150 | |
| 151 | static void delete_all_records(struct tls_offload_context_tx *offload_ctx) |
| 152 | { |
| 153 | struct tls_record_info *info, *temp; |
| 154 | |
| 155 | list_for_each_entry_safe(info, temp, &offload_ctx->records_list, list) { |
| 156 | list_del(&info->list); |
| 157 | destroy_record(info); |
| 158 | } |
| 159 | |
| 160 | offload_ctx->retransmit_hint = NULL; |
| 161 | } |
| 162 | |
| 163 | static void tls_icsk_clean_acked(struct sock *sk, u32 acked_seq) |
| 164 | { |
| 165 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 166 | struct tls_record_info *info, *temp; |
| 167 | struct tls_offload_context_tx *ctx; |
| 168 | u64 deleted_records = 0; |
| 169 | unsigned long flags; |
| 170 | |
| 171 | if (!tls_ctx) |
| 172 | return; |
| 173 | |
| 174 | ctx = tls_offload_ctx_tx(tls_ctx); |
| 175 | |
| 176 | spin_lock_irqsave(&ctx->lock, flags); |
| 177 | info = ctx->retransmit_hint; |
| 178 | if (info && !before(acked_seq, info->end_seq)) { |
| 179 | ctx->retransmit_hint = NULL; |
| 180 | list_del(&info->list); |
| 181 | destroy_record(info); |
| 182 | deleted_records++; |
| 183 | } |
| 184 | |
| 185 | list_for_each_entry_safe(info, temp, &ctx->records_list, list) { |
| 186 | if (before(acked_seq, info->end_seq)) |
| 187 | break; |
| 188 | list_del(&info->list); |
| 189 | |
| 190 | destroy_record(info); |
| 191 | deleted_records++; |
| 192 | } |
| 193 | |
| 194 | ctx->unacked_record_sn += deleted_records; |
| 195 | spin_unlock_irqrestore(&ctx->lock, flags); |
| 196 | } |
| 197 | |
| 198 | /* At this point, there should be no references on this |
| 199 | * socket and no in-flight SKBs associated with this |
| 200 | * socket, so it is safe to free all the resources. |
| 201 | */ |
| 202 | void tls_device_sk_destruct(struct sock *sk) |
| 203 | { |
| 204 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 205 | struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx); |
| 206 | |
| 207 | tls_ctx->sk_destruct(sk); |
| 208 | |
| 209 | if (tls_ctx->tx_conf == TLS_HW) { |
| 210 | if (ctx->open_record) |
| 211 | destroy_record(ctx->open_record); |
| 212 | delete_all_records(ctx); |
| 213 | crypto_free_aead(ctx->aead_send); |
| 214 | clean_acked_data_disable(inet_csk(sk)); |
| 215 | } |
| 216 | |
| 217 | if (refcount_dec_and_test(&tls_ctx->refcount)) |
| 218 | tls_device_queue_ctx_destruction(tls_ctx); |
| 219 | } |
| 220 | EXPORT_SYMBOL(tls_device_sk_destruct); |
| 221 | |
| 222 | static void tls_append_frag(struct tls_record_info *record, |
| 223 | struct page_frag *pfrag, |
| 224 | int size) |
| 225 | { |
| 226 | skb_frag_t *frag; |
| 227 | |
| 228 | frag = &record->frags[record->num_frags - 1]; |
| 229 | if (frag->page.p == pfrag->page && |
| 230 | frag->page_offset + frag->size == pfrag->offset) { |
| 231 | frag->size += size; |
| 232 | } else { |
| 233 | ++frag; |
| 234 | frag->page.p = pfrag->page; |
| 235 | frag->page_offset = pfrag->offset; |
| 236 | frag->size = size; |
| 237 | ++record->num_frags; |
| 238 | get_page(pfrag->page); |
| 239 | } |
| 240 | |
| 241 | pfrag->offset += size; |
| 242 | record->len += size; |
| 243 | } |
| 244 | |
| 245 | static int tls_push_record(struct sock *sk, |
| 246 | struct tls_context *ctx, |
| 247 | struct tls_offload_context_tx *offload_ctx, |
| 248 | struct tls_record_info *record, |
| 249 | struct page_frag *pfrag, |
| 250 | int flags, |
| 251 | unsigned char record_type) |
| 252 | { |
| 253 | struct tcp_sock *tp = tcp_sk(sk); |
| 254 | struct page_frag dummy_tag_frag; |
| 255 | skb_frag_t *frag; |
| 256 | int i; |
| 257 | |
| 258 | /* fill prepend */ |
| 259 | frag = &record->frags[0]; |
| 260 | tls_fill_prepend(ctx, |
| 261 | skb_frag_address(frag), |
| 262 | record->len - ctx->tx.prepend_size, |
| 263 | record_type); |
| 264 | |
| 265 | /* HW doesn't care about the data in the tag, because it fills it. */ |
| 266 | dummy_tag_frag.page = skb_frag_page(frag); |
| 267 | dummy_tag_frag.offset = 0; |
| 268 | |
| 269 | tls_append_frag(record, &dummy_tag_frag, ctx->tx.tag_size); |
| 270 | record->end_seq = tp->write_seq + record->len; |
| 271 | spin_lock_irq(&offload_ctx->lock); |
| 272 | list_add_tail(&record->list, &offload_ctx->records_list); |
| 273 | spin_unlock_irq(&offload_ctx->lock); |
| 274 | offload_ctx->open_record = NULL; |
| 275 | set_bit(TLS_PENDING_CLOSED_RECORD, &ctx->flags); |
| 276 | tls_advance_record_sn(sk, &ctx->tx); |
| 277 | |
| 278 | for (i = 0; i < record->num_frags; i++) { |
| 279 | frag = &record->frags[i]; |
| 280 | sg_unmark_end(&offload_ctx->sg_tx_data[i]); |
| 281 | sg_set_page(&offload_ctx->sg_tx_data[i], skb_frag_page(frag), |
| 282 | frag->size, frag->page_offset); |
| 283 | sk_mem_charge(sk, frag->size); |
| 284 | get_page(skb_frag_page(frag)); |
| 285 | } |
| 286 | sg_mark_end(&offload_ctx->sg_tx_data[record->num_frags - 1]); |
| 287 | |
| 288 | /* all ready, send */ |
| 289 | return tls_push_sg(sk, ctx, offload_ctx->sg_tx_data, 0, flags); |
| 290 | } |
| 291 | |
| 292 | static int tls_create_new_record(struct tls_offload_context_tx *offload_ctx, |
| 293 | struct page_frag *pfrag, |
| 294 | size_t prepend_size) |
| 295 | { |
| 296 | struct tls_record_info *record; |
| 297 | skb_frag_t *frag; |
| 298 | |
| 299 | record = kmalloc(sizeof(*record), GFP_KERNEL); |
| 300 | if (!record) |
| 301 | return -ENOMEM; |
| 302 | |
| 303 | frag = &record->frags[0]; |
| 304 | __skb_frag_set_page(frag, pfrag->page); |
| 305 | frag->page_offset = pfrag->offset; |
| 306 | skb_frag_size_set(frag, prepend_size); |
| 307 | |
| 308 | get_page(pfrag->page); |
| 309 | pfrag->offset += prepend_size; |
| 310 | |
| 311 | record->num_frags = 1; |
| 312 | record->len = prepend_size; |
| 313 | offload_ctx->open_record = record; |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int tls_do_allocation(struct sock *sk, |
| 318 | struct tls_offload_context_tx *offload_ctx, |
| 319 | struct page_frag *pfrag, |
| 320 | size_t prepend_size) |
| 321 | { |
| 322 | int ret; |
| 323 | |
| 324 | if (!offload_ctx->open_record) { |
| 325 | if (unlikely(!skb_page_frag_refill(prepend_size, pfrag, |
| 326 | sk->sk_allocation))) { |
| 327 | sk->sk_prot->enter_memory_pressure(sk); |
| 328 | sk_stream_moderate_sndbuf(sk); |
| 329 | return -ENOMEM; |
| 330 | } |
| 331 | |
| 332 | ret = tls_create_new_record(offload_ctx, pfrag, prepend_size); |
| 333 | if (ret) |
| 334 | return ret; |
| 335 | |
| 336 | if (pfrag->size > pfrag->offset) |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | if (!sk_page_frag_refill(sk, pfrag)) |
| 341 | return -ENOMEM; |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | static int tls_push_data(struct sock *sk, |
| 347 | struct iov_iter *msg_iter, |
| 348 | size_t size, int flags, |
| 349 | unsigned char record_type) |
| 350 | { |
| 351 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 352 | struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx); |
| 353 | int tls_push_record_flags = flags | MSG_SENDPAGE_NOTLAST; |
| 354 | int more = flags & (MSG_SENDPAGE_NOTLAST | MSG_MORE); |
| 355 | struct tls_record_info *record = ctx->open_record; |
| 356 | struct page_frag *pfrag; |
| 357 | size_t orig_size = size; |
| 358 | u32 max_open_record_len; |
| 359 | int copy, rc = 0; |
| 360 | bool done = false; |
| 361 | long timeo; |
| 362 | |
| 363 | if (flags & |
| 364 | ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_SENDPAGE_NOTLAST)) |
| 365 | return -ENOTSUPP; |
| 366 | |
| 367 | if (sk->sk_err) |
| 368 | return -sk->sk_err; |
| 369 | |
| 370 | timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); |
| 371 | rc = tls_complete_pending_work(sk, tls_ctx, flags, &timeo); |
| 372 | if (rc < 0) |
| 373 | return rc; |
| 374 | |
| 375 | pfrag = sk_page_frag(sk); |
| 376 | |
| 377 | /* TLS_HEADER_SIZE is not counted as part of the TLS record, and |
| 378 | * we need to leave room for an authentication tag. |
| 379 | */ |
| 380 | max_open_record_len = TLS_MAX_PAYLOAD_SIZE + |
| 381 | tls_ctx->tx.prepend_size; |
| 382 | do { |
| 383 | rc = tls_do_allocation(sk, ctx, pfrag, |
| 384 | tls_ctx->tx.prepend_size); |
| 385 | if (rc) { |
| 386 | rc = sk_stream_wait_memory(sk, &timeo); |
| 387 | if (!rc) |
| 388 | continue; |
| 389 | |
| 390 | record = ctx->open_record; |
| 391 | if (!record) |
| 392 | break; |
| 393 | handle_error: |
| 394 | if (record_type != TLS_RECORD_TYPE_DATA) { |
| 395 | /* avoid sending partial |
| 396 | * record with type != |
| 397 | * application_data |
| 398 | */ |
| 399 | size = orig_size; |
| 400 | destroy_record(record); |
| 401 | ctx->open_record = NULL; |
| 402 | } else if (record->len > tls_ctx->tx.prepend_size) { |
| 403 | goto last_record; |
| 404 | } |
| 405 | |
| 406 | break; |
| 407 | } |
| 408 | |
| 409 | record = ctx->open_record; |
| 410 | copy = min_t(size_t, size, (pfrag->size - pfrag->offset)); |
| 411 | copy = min_t(size_t, copy, (max_open_record_len - record->len)); |
| 412 | |
| 413 | if (copy_from_iter_nocache(page_address(pfrag->page) + |
| 414 | pfrag->offset, |
| 415 | copy, msg_iter) != copy) { |
| 416 | rc = -EFAULT; |
| 417 | goto handle_error; |
| 418 | } |
| 419 | tls_append_frag(record, pfrag, copy); |
| 420 | |
| 421 | size -= copy; |
| 422 | if (!size) { |
| 423 | last_record: |
| 424 | tls_push_record_flags = flags; |
| 425 | if (more) { |
| 426 | tls_ctx->pending_open_record_frags = |
| 427 | record->num_frags; |
| 428 | break; |
| 429 | } |
| 430 | |
| 431 | done = true; |
| 432 | } |
| 433 | |
| 434 | if (done || record->len >= max_open_record_len || |
| 435 | (record->num_frags >= MAX_SKB_FRAGS - 1)) { |
| 436 | rc = tls_push_record(sk, |
| 437 | tls_ctx, |
| 438 | ctx, |
| 439 | record, |
| 440 | pfrag, |
| 441 | tls_push_record_flags, |
| 442 | record_type); |
| 443 | if (rc < 0) |
| 444 | break; |
| 445 | } |
| 446 | } while (!done); |
| 447 | |
| 448 | if (orig_size - size > 0) |
| 449 | rc = orig_size - size; |
| 450 | |
| 451 | return rc; |
| 452 | } |
| 453 | |
| 454 | int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) |
| 455 | { |
| 456 | unsigned char record_type = TLS_RECORD_TYPE_DATA; |
| 457 | int rc; |
| 458 | |
| 459 | lock_sock(sk); |
| 460 | |
| 461 | if (unlikely(msg->msg_controllen)) { |
| 462 | rc = tls_proccess_cmsg(sk, msg, &record_type); |
| 463 | if (rc) |
| 464 | goto out; |
| 465 | } |
| 466 | |
| 467 | rc = tls_push_data(sk, &msg->msg_iter, size, |
| 468 | msg->msg_flags, record_type); |
| 469 | |
| 470 | out: |
| 471 | release_sock(sk); |
| 472 | return rc; |
| 473 | } |
| 474 | |
| 475 | int tls_device_sendpage(struct sock *sk, struct page *page, |
| 476 | int offset, size_t size, int flags) |
| 477 | { |
| 478 | struct iov_iter msg_iter; |
| 479 | char *kaddr = kmap(page); |
| 480 | struct kvec iov; |
| 481 | int rc; |
| 482 | |
| 483 | if (flags & MSG_SENDPAGE_NOTLAST) |
| 484 | flags |= MSG_MORE; |
| 485 | |
| 486 | lock_sock(sk); |
| 487 | |
| 488 | if (flags & MSG_OOB) { |
| 489 | rc = -ENOTSUPP; |
| 490 | goto out; |
| 491 | } |
| 492 | |
| 493 | iov.iov_base = kaddr + offset; |
| 494 | iov.iov_len = size; |
| 495 | iov_iter_kvec(&msg_iter, WRITE | ITER_KVEC, &iov, 1, size); |
| 496 | rc = tls_push_data(sk, &msg_iter, size, |
| 497 | flags, TLS_RECORD_TYPE_DATA); |
| 498 | kunmap(page); |
| 499 | |
| 500 | out: |
| 501 | release_sock(sk); |
| 502 | return rc; |
| 503 | } |
| 504 | |
| 505 | struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context, |
| 506 | u32 seq, u64 *p_record_sn) |
| 507 | { |
| 508 | u64 record_sn = context->hint_record_sn; |
| 509 | struct tls_record_info *info; |
| 510 | |
| 511 | info = context->retransmit_hint; |
| 512 | if (!info || |
| 513 | before(seq, info->end_seq - info->len)) { |
| 514 | /* if retransmit_hint is irrelevant start |
| 515 | * from the beggining of the list |
| 516 | */ |
| 517 | info = list_first_entry(&context->records_list, |
| 518 | struct tls_record_info, list); |
| 519 | record_sn = context->unacked_record_sn; |
| 520 | } |
| 521 | |
| 522 | list_for_each_entry_from(info, &context->records_list, list) { |
| 523 | if (before(seq, info->end_seq)) { |
| 524 | if (!context->retransmit_hint || |
| 525 | after(info->end_seq, |
| 526 | context->retransmit_hint->end_seq)) { |
| 527 | context->hint_record_sn = record_sn; |
| 528 | context->retransmit_hint = info; |
| 529 | } |
| 530 | *p_record_sn = record_sn; |
| 531 | return info; |
| 532 | } |
| 533 | record_sn++; |
| 534 | } |
| 535 | |
| 536 | return NULL; |
| 537 | } |
| 538 | EXPORT_SYMBOL(tls_get_record); |
| 539 | |
| 540 | static int tls_device_push_pending_record(struct sock *sk, int flags) |
| 541 | { |
| 542 | struct iov_iter msg_iter; |
| 543 | |
| 544 | iov_iter_kvec(&msg_iter, WRITE | ITER_KVEC, NULL, 0, 0); |
| 545 | return tls_push_data(sk, &msg_iter, 0, flags, TLS_RECORD_TYPE_DATA); |
| 546 | } |
| 547 | |
| 548 | static void tls_device_resync_rx(struct tls_context *tls_ctx, |
| 549 | struct sock *sk, u32 seq, u64 rcd_sn) |
| 550 | { |
| 551 | struct net_device *netdev; |
| 552 | |
| 553 | if (WARN_ON(test_and_set_bit(TLS_RX_SYNC_RUNNING, &tls_ctx->flags))) |
| 554 | return; |
| 555 | netdev = READ_ONCE(tls_ctx->netdev); |
| 556 | if (netdev) |
| 557 | netdev->tlsdev_ops->tls_dev_resync_rx(netdev, sk, seq, rcd_sn); |
| 558 | clear_bit_unlock(TLS_RX_SYNC_RUNNING, &tls_ctx->flags); |
| 559 | } |
| 560 | |
| 561 | void handle_device_resync(struct sock *sk, u32 seq, u64 rcd_sn) |
| 562 | { |
| 563 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 564 | struct tls_offload_context_rx *rx_ctx; |
| 565 | u32 is_req_pending; |
| 566 | s64 resync_req; |
| 567 | u32 req_seq; |
| 568 | |
| 569 | if (tls_ctx->rx_conf != TLS_HW) |
| 570 | return; |
| 571 | |
| 572 | rx_ctx = tls_offload_ctx_rx(tls_ctx); |
| 573 | resync_req = atomic64_read(&rx_ctx->resync_req); |
| 574 | req_seq = ntohl(resync_req >> 32) - ((u32)TLS_HEADER_SIZE - 1); |
| 575 | is_req_pending = resync_req; |
| 576 | |
| 577 | if (unlikely(is_req_pending) && req_seq == seq && |
| 578 | atomic64_try_cmpxchg(&rx_ctx->resync_req, &resync_req, 0)) { |
| 579 | seq += TLS_HEADER_SIZE - 1; |
| 580 | tls_device_resync_rx(tls_ctx, sk, seq, rcd_sn); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | static int tls_device_reencrypt(struct sock *sk, struct sk_buff *skb) |
| 585 | { |
| 586 | struct strp_msg *rxm = strp_msg(skb); |
| 587 | int err = 0, offset = rxm->offset, copy, nsg, data_len, pos; |
| 588 | struct sk_buff *skb_iter, *unused; |
| 589 | struct scatterlist sg[1]; |
| 590 | char *orig_buf, *buf; |
| 591 | |
| 592 | orig_buf = kmalloc(rxm->full_len + TLS_HEADER_SIZE + |
| 593 | TLS_CIPHER_AES_GCM_128_IV_SIZE, sk->sk_allocation); |
| 594 | if (!orig_buf) |
| 595 | return -ENOMEM; |
| 596 | buf = orig_buf; |
| 597 | |
| 598 | nsg = skb_cow_data(skb, 0, &unused); |
| 599 | if (unlikely(nsg < 0)) { |
| 600 | err = nsg; |
| 601 | goto free_buf; |
| 602 | } |
| 603 | |
| 604 | sg_init_table(sg, 1); |
| 605 | sg_set_buf(&sg[0], buf, |
| 606 | rxm->full_len + TLS_HEADER_SIZE + |
| 607 | TLS_CIPHER_AES_GCM_128_IV_SIZE); |
| 608 | skb_copy_bits(skb, offset, buf, |
| 609 | TLS_HEADER_SIZE + TLS_CIPHER_AES_GCM_128_IV_SIZE); |
| 610 | |
| 611 | /* We are interested only in the decrypted data not the auth */ |
| 612 | err = decrypt_skb(sk, skb, sg); |
| 613 | if (err != -EBADMSG) |
| 614 | goto free_buf; |
| 615 | else |
| 616 | err = 0; |
| 617 | |
| 618 | data_len = rxm->full_len - TLS_CIPHER_AES_GCM_128_TAG_SIZE; |
| 619 | |
| 620 | if (skb_pagelen(skb) > offset) { |
| 621 | copy = min_t(int, skb_pagelen(skb) - offset, data_len); |
| 622 | |
| 623 | if (skb->decrypted) |
| 624 | skb_store_bits(skb, offset, buf, copy); |
| 625 | |
| 626 | offset += copy; |
| 627 | buf += copy; |
| 628 | } |
| 629 | |
| 630 | pos = skb_pagelen(skb); |
| 631 | skb_walk_frags(skb, skb_iter) { |
| 632 | int frag_pos; |
| 633 | |
| 634 | /* Practically all frags must belong to msg if reencrypt |
| 635 | * is needed with current strparser and coalescing logic, |
| 636 | * but strparser may "get optimized", so let's be safe. |
| 637 | */ |
| 638 | if (pos + skb_iter->len <= offset) |
| 639 | goto done_with_frag; |
| 640 | if (pos >= data_len + rxm->offset) |
| 641 | break; |
| 642 | |
| 643 | frag_pos = offset - pos; |
| 644 | copy = min_t(int, skb_iter->len - frag_pos, |
| 645 | data_len + rxm->offset - offset); |
| 646 | |
| 647 | if (skb_iter->decrypted) |
| 648 | skb_store_bits(skb_iter, frag_pos, buf, copy); |
| 649 | |
| 650 | offset += copy; |
| 651 | buf += copy; |
| 652 | done_with_frag: |
| 653 | pos += skb_iter->len; |
| 654 | } |
| 655 | |
| 656 | free_buf: |
| 657 | kfree(orig_buf); |
| 658 | return err; |
| 659 | } |
| 660 | |
| 661 | int tls_device_decrypted(struct sock *sk, struct sk_buff *skb) |
| 662 | { |
| 663 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 664 | struct tls_offload_context_rx *ctx = tls_offload_ctx_rx(tls_ctx); |
| 665 | int is_decrypted = skb->decrypted; |
| 666 | int is_encrypted = !is_decrypted; |
| 667 | struct sk_buff *skb_iter; |
| 668 | |
| 669 | /* Skip if it is already decrypted */ |
| 670 | if (ctx->sw.decrypted) |
| 671 | return 0; |
| 672 | |
| 673 | /* Check if all the data is decrypted already */ |
| 674 | skb_walk_frags(skb, skb_iter) { |
| 675 | is_decrypted &= skb_iter->decrypted; |
| 676 | is_encrypted &= !skb_iter->decrypted; |
| 677 | } |
| 678 | |
| 679 | ctx->sw.decrypted |= is_decrypted; |
| 680 | |
| 681 | /* Return immedeatly if the record is either entirely plaintext or |
| 682 | * entirely ciphertext. Otherwise handle reencrypt partially decrypted |
| 683 | * record. |
| 684 | */ |
| 685 | return (is_encrypted || is_decrypted) ? 0 : |
| 686 | tls_device_reencrypt(sk, skb); |
| 687 | } |
| 688 | |
| 689 | int tls_set_device_offload(struct sock *sk, struct tls_context *ctx) |
| 690 | { |
| 691 | u16 nonce_size, tag_size, iv_size, rec_seq_size; |
| 692 | struct tls_record_info *start_marker_record; |
| 693 | struct tls_offload_context_tx *offload_ctx; |
| 694 | struct tls_crypto_info *crypto_info; |
| 695 | struct net_device *netdev; |
| 696 | char *iv, *rec_seq; |
| 697 | struct sk_buff *skb; |
| 698 | int rc = -EINVAL; |
| 699 | __be64 rcd_sn; |
| 700 | |
| 701 | if (!ctx) |
| 702 | goto out; |
| 703 | |
| 704 | if (ctx->priv_ctx_tx) { |
| 705 | rc = -EEXIST; |
| 706 | goto out; |
| 707 | } |
| 708 | |
| 709 | start_marker_record = kmalloc(sizeof(*start_marker_record), GFP_KERNEL); |
| 710 | if (!start_marker_record) { |
| 711 | rc = -ENOMEM; |
| 712 | goto out; |
| 713 | } |
| 714 | |
| 715 | offload_ctx = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_TX, GFP_KERNEL); |
| 716 | if (!offload_ctx) { |
| 717 | rc = -ENOMEM; |
| 718 | goto free_marker_record; |
| 719 | } |
| 720 | |
| 721 | crypto_info = &ctx->crypto_send.info; |
| 722 | switch (crypto_info->cipher_type) { |
| 723 | case TLS_CIPHER_AES_GCM_128: |
| 724 | nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE; |
| 725 | tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE; |
| 726 | iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE; |
| 727 | iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv; |
| 728 | rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE; |
| 729 | rec_seq = |
| 730 | ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq; |
| 731 | break; |
| 732 | default: |
| 733 | rc = -EINVAL; |
| 734 | goto free_offload_ctx; |
| 735 | } |
| 736 | |
| 737 | ctx->tx.prepend_size = TLS_HEADER_SIZE + nonce_size; |
| 738 | ctx->tx.tag_size = tag_size; |
| 739 | ctx->tx.overhead_size = ctx->tx.prepend_size + ctx->tx.tag_size; |
| 740 | ctx->tx.iv_size = iv_size; |
| 741 | ctx->tx.iv = kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE, |
| 742 | GFP_KERNEL); |
| 743 | if (!ctx->tx.iv) { |
| 744 | rc = -ENOMEM; |
| 745 | goto free_offload_ctx; |
| 746 | } |
| 747 | |
| 748 | memcpy(ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size); |
| 749 | |
| 750 | ctx->tx.rec_seq_size = rec_seq_size; |
| 751 | ctx->tx.rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL); |
| 752 | if (!ctx->tx.rec_seq) { |
| 753 | rc = -ENOMEM; |
| 754 | goto free_iv; |
| 755 | } |
| 756 | |
| 757 | rc = tls_sw_fallback_init(sk, offload_ctx, crypto_info); |
| 758 | if (rc) |
| 759 | goto free_rec_seq; |
| 760 | |
| 761 | /* start at rec_seq - 1 to account for the start marker record */ |
| 762 | memcpy(&rcd_sn, ctx->tx.rec_seq, sizeof(rcd_sn)); |
| 763 | offload_ctx->unacked_record_sn = be64_to_cpu(rcd_sn) - 1; |
| 764 | |
| 765 | start_marker_record->end_seq = tcp_sk(sk)->write_seq; |
| 766 | start_marker_record->len = 0; |
| 767 | start_marker_record->num_frags = 0; |
| 768 | |
| 769 | INIT_LIST_HEAD(&offload_ctx->records_list); |
| 770 | list_add_tail(&start_marker_record->list, &offload_ctx->records_list); |
| 771 | spin_lock_init(&offload_ctx->lock); |
| 772 | sg_init_table(offload_ctx->sg_tx_data, |
| 773 | ARRAY_SIZE(offload_ctx->sg_tx_data)); |
| 774 | |
| 775 | clean_acked_data_enable(inet_csk(sk), &tls_icsk_clean_acked); |
| 776 | ctx->push_pending_record = tls_device_push_pending_record; |
| 777 | |
| 778 | /* TLS offload is greatly simplified if we don't send |
| 779 | * SKBs where only part of the payload needs to be encrypted. |
| 780 | * So mark the last skb in the write queue as end of record. |
| 781 | */ |
| 782 | skb = tcp_write_queue_tail(sk); |
| 783 | if (skb) |
| 784 | TCP_SKB_CB(skb)->eor = 1; |
| 785 | |
| 786 | /* We support starting offload on multiple sockets |
| 787 | * concurrently, so we only need a read lock here. |
| 788 | * This lock must precede get_netdev_for_sock to prevent races between |
| 789 | * NETDEV_DOWN and setsockopt. |
| 790 | */ |
| 791 | down_read(&device_offload_lock); |
| 792 | netdev = get_netdev_for_sock(sk); |
| 793 | if (!netdev) { |
| 794 | pr_err_ratelimited("%s: netdev not found\n", __func__); |
| 795 | rc = -EINVAL; |
| 796 | goto release_lock; |
| 797 | } |
| 798 | |
| 799 | if (!(netdev->features & NETIF_F_HW_TLS_TX)) { |
| 800 | rc = -ENOTSUPP; |
| 801 | goto release_netdev; |
| 802 | } |
| 803 | |
| 804 | /* Avoid offloading if the device is down |
| 805 | * We don't want to offload new flows after |
| 806 | * the NETDEV_DOWN event |
| 807 | */ |
| 808 | if (!(netdev->flags & IFF_UP)) { |
| 809 | rc = -EINVAL; |
| 810 | goto release_netdev; |
| 811 | } |
| 812 | |
| 813 | ctx->priv_ctx_tx = offload_ctx; |
| 814 | rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_TX, |
| 815 | &ctx->crypto_send.info, |
| 816 | tcp_sk(sk)->write_seq); |
| 817 | if (rc) |
| 818 | goto release_netdev; |
| 819 | |
| 820 | tls_device_attach(ctx, sk, netdev); |
| 821 | |
| 822 | /* following this assignment tls_is_sk_tx_device_offloaded |
| 823 | * will return true and the context might be accessed |
| 824 | * by the netdev's xmit function. |
| 825 | */ |
| 826 | smp_store_release(&sk->sk_validate_xmit_skb, tls_validate_xmit_skb); |
| 827 | dev_put(netdev); |
| 828 | up_read(&device_offload_lock); |
| 829 | goto out; |
| 830 | |
| 831 | release_netdev: |
| 832 | dev_put(netdev); |
| 833 | release_lock: |
| 834 | up_read(&device_offload_lock); |
| 835 | clean_acked_data_disable(inet_csk(sk)); |
| 836 | crypto_free_aead(offload_ctx->aead_send); |
| 837 | free_rec_seq: |
| 838 | kfree(ctx->tx.rec_seq); |
| 839 | free_iv: |
| 840 | kfree(ctx->tx.iv); |
| 841 | free_offload_ctx: |
| 842 | kfree(offload_ctx); |
| 843 | ctx->priv_ctx_tx = NULL; |
| 844 | free_marker_record: |
| 845 | kfree(start_marker_record); |
| 846 | out: |
| 847 | return rc; |
| 848 | } |
| 849 | |
| 850 | int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx) |
| 851 | { |
| 852 | struct tls_offload_context_rx *context; |
| 853 | struct net_device *netdev; |
| 854 | int rc = 0; |
| 855 | |
| 856 | /* We support starting offload on multiple sockets |
| 857 | * concurrently, so we only need a read lock here. |
| 858 | * This lock must precede get_netdev_for_sock to prevent races between |
| 859 | * NETDEV_DOWN and setsockopt. |
| 860 | */ |
| 861 | down_read(&device_offload_lock); |
| 862 | netdev = get_netdev_for_sock(sk); |
| 863 | if (!netdev) { |
| 864 | pr_err_ratelimited("%s: netdev not found\n", __func__); |
| 865 | rc = -EINVAL; |
| 866 | goto release_lock; |
| 867 | } |
| 868 | |
| 869 | if (!(netdev->features & NETIF_F_HW_TLS_RX)) { |
| 870 | pr_err_ratelimited("%s: netdev %s with no TLS offload\n", |
| 871 | __func__, netdev->name); |
| 872 | rc = -ENOTSUPP; |
| 873 | goto release_netdev; |
| 874 | } |
| 875 | |
| 876 | /* Avoid offloading if the device is down |
| 877 | * We don't want to offload new flows after |
| 878 | * the NETDEV_DOWN event |
| 879 | */ |
| 880 | if (!(netdev->flags & IFF_UP)) { |
| 881 | rc = -EINVAL; |
| 882 | goto release_netdev; |
| 883 | } |
| 884 | |
| 885 | context = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_RX, GFP_KERNEL); |
| 886 | if (!context) { |
| 887 | rc = -ENOMEM; |
| 888 | goto release_netdev; |
| 889 | } |
| 890 | |
| 891 | ctx->priv_ctx_rx = context; |
| 892 | rc = tls_set_sw_offload(sk, ctx, 0); |
| 893 | if (rc) |
| 894 | goto release_ctx; |
| 895 | |
| 896 | rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_RX, |
| 897 | &ctx->crypto_recv.info, |
| 898 | tcp_sk(sk)->copied_seq); |
| 899 | if (rc) { |
| 900 | pr_err_ratelimited("%s: The netdev has refused to offload this socket\n", |
| 901 | __func__); |
| 902 | goto free_sw_resources; |
| 903 | } |
| 904 | |
| 905 | tls_device_attach(ctx, sk, netdev); |
| 906 | goto release_netdev; |
| 907 | |
| 908 | free_sw_resources: |
| 909 | up_read(&device_offload_lock); |
| 910 | tls_sw_free_resources_rx(sk); |
| 911 | down_read(&device_offload_lock); |
| 912 | release_ctx: |
| 913 | ctx->priv_ctx_rx = NULL; |
| 914 | release_netdev: |
| 915 | dev_put(netdev); |
| 916 | release_lock: |
| 917 | up_read(&device_offload_lock); |
| 918 | return rc; |
| 919 | } |
| 920 | |
| 921 | void tls_device_offload_cleanup_rx(struct sock *sk) |
| 922 | { |
| 923 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 924 | struct net_device *netdev; |
| 925 | |
| 926 | down_read(&device_offload_lock); |
| 927 | netdev = tls_ctx->netdev; |
| 928 | if (!netdev) |
| 929 | goto out; |
| 930 | |
| 931 | netdev->tlsdev_ops->tls_dev_del(netdev, tls_ctx, |
| 932 | TLS_OFFLOAD_CTX_DIR_RX); |
| 933 | |
| 934 | if (tls_ctx->tx_conf != TLS_HW) { |
| 935 | dev_put(netdev); |
| 936 | tls_ctx->netdev = NULL; |
| 937 | } |
| 938 | out: |
| 939 | up_read(&device_offload_lock); |
| 940 | tls_sw_release_resources_rx(sk); |
| 941 | } |
| 942 | |
| 943 | static int tls_device_down(struct net_device *netdev) |
| 944 | { |
| 945 | struct tls_context *ctx, *tmp; |
| 946 | unsigned long flags; |
| 947 | LIST_HEAD(list); |
| 948 | |
| 949 | /* Request a write lock to block new offload attempts */ |
| 950 | down_write(&device_offload_lock); |
| 951 | |
| 952 | spin_lock_irqsave(&tls_device_lock, flags); |
| 953 | list_for_each_entry_safe(ctx, tmp, &tls_device_list, list) { |
| 954 | if (ctx->netdev != netdev || |
| 955 | !refcount_inc_not_zero(&ctx->refcount)) |
| 956 | continue; |
| 957 | |
| 958 | list_move(&ctx->list, &list); |
| 959 | } |
| 960 | spin_unlock_irqrestore(&tls_device_lock, flags); |
| 961 | |
| 962 | list_for_each_entry_safe(ctx, tmp, &list, list) { |
| 963 | if (ctx->tx_conf == TLS_HW) |
| 964 | netdev->tlsdev_ops->tls_dev_del(netdev, ctx, |
| 965 | TLS_OFFLOAD_CTX_DIR_TX); |
| 966 | if (ctx->rx_conf == TLS_HW) |
| 967 | netdev->tlsdev_ops->tls_dev_del(netdev, ctx, |
| 968 | TLS_OFFLOAD_CTX_DIR_RX); |
| 969 | WRITE_ONCE(ctx->netdev, NULL); |
| 970 | smp_mb__before_atomic(); /* pairs with test_and_set_bit() */ |
| 971 | while (test_bit(TLS_RX_SYNC_RUNNING, &ctx->flags)) |
| 972 | usleep_range(10, 200); |
| 973 | dev_put(netdev); |
| 974 | list_del_init(&ctx->list); |
| 975 | |
| 976 | if (refcount_dec_and_test(&ctx->refcount)) |
| 977 | tls_device_free_ctx(ctx); |
| 978 | } |
| 979 | |
| 980 | up_write(&device_offload_lock); |
| 981 | |
| 982 | flush_work(&tls_device_gc_work); |
| 983 | |
| 984 | return NOTIFY_DONE; |
| 985 | } |
| 986 | |
| 987 | static int tls_dev_event(struct notifier_block *this, unsigned long event, |
| 988 | void *ptr) |
| 989 | { |
| 990 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 991 | |
| 992 | if (!dev->tlsdev_ops && |
| 993 | !(dev->features & (NETIF_F_HW_TLS_RX | NETIF_F_HW_TLS_TX))) |
| 994 | return NOTIFY_DONE; |
| 995 | |
| 996 | switch (event) { |
| 997 | case NETDEV_REGISTER: |
| 998 | case NETDEV_FEAT_CHANGE: |
| 999 | if ((dev->features & NETIF_F_HW_TLS_RX) && |
| 1000 | !dev->tlsdev_ops->tls_dev_resync_rx) |
| 1001 | return NOTIFY_BAD; |
| 1002 | |
| 1003 | if (dev->tlsdev_ops && |
| 1004 | dev->tlsdev_ops->tls_dev_add && |
| 1005 | dev->tlsdev_ops->tls_dev_del) |
| 1006 | return NOTIFY_DONE; |
| 1007 | else |
| 1008 | return NOTIFY_BAD; |
| 1009 | case NETDEV_DOWN: |
| 1010 | return tls_device_down(dev); |
| 1011 | } |
| 1012 | return NOTIFY_DONE; |
| 1013 | } |
| 1014 | |
| 1015 | static struct notifier_block tls_dev_notifier = { |
| 1016 | .notifier_call = tls_dev_event, |
| 1017 | }; |
| 1018 | |
| 1019 | void __init tls_device_init(void) |
| 1020 | { |
| 1021 | register_netdevice_notifier(&tls_dev_notifier); |
| 1022 | } |
| 1023 | |
| 1024 | void __exit tls_device_cleanup(void) |
| 1025 | { |
| 1026 | unregister_netdevice_notifier(&tls_dev_notifier); |
| 1027 | flush_work(&tls_device_gc_work); |
| 1028 | } |