1 /* Copyright (c) 2018, Mellanox Technologies All rights reserved.
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:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
13 * - Redistributions of source code must retain the above
14 * copyright notice, this list of conditions and the following
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.
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
32 #include <crypto/aead.h>
33 #include <linux/highmem.h>
34 #include <linux/module.h>
35 #include <linux/netdevice.h>
37 #include <net/inet_connection_sock.h>
44 /* device_offload_lock is used to synchronize tls_dev_add
45 * against NETDEV_DOWN notifications.
47 static DECLARE_RWSEM(device_offload_lock);
49 static struct workqueue_struct *destruct_wq __read_mostly;
51 static LIST_HEAD(tls_device_list);
52 static LIST_HEAD(tls_device_down_list);
53 static DEFINE_SPINLOCK(tls_device_lock);
55 static void tls_device_free_ctx(struct tls_context *ctx)
57 if (ctx->tx_conf == TLS_HW) {
58 kfree(tls_offload_ctx_tx(ctx));
59 kfree(ctx->tx.rec_seq);
63 if (ctx->rx_conf == TLS_HW)
64 kfree(tls_offload_ctx_rx(ctx));
66 tls_ctx_free(NULL, ctx);
69 static void tls_device_tx_del_task(struct work_struct *work)
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;
76 /* Safe, because this is the destroy flow, refcount is 0, so
77 * tls_device_down can't store this field in parallel.
79 netdev = rcu_dereference_protected(ctx->netdev,
80 !refcount_read(&ctx->refcount));
82 netdev->tlsdev_ops->tls_dev_del(netdev, ctx, TLS_OFFLOAD_CTX_DIR_TX);
85 tls_device_free_ctx(ctx);
88 static void tls_device_queue_ctx_destruction(struct tls_context *ctx)
90 struct net_device *netdev;
94 spin_lock_irqsave(&tls_device_lock, flags);
95 if (unlikely(!refcount_dec_and_test(&ctx->refcount))) {
96 spin_unlock_irqrestore(&tls_device_lock, flags);
100 list_del(&ctx->list); /* Remove from tls_device_list / tls_device_down_list */
102 /* Safe, because this is the destroy flow, refcount is 0, so
103 * tls_device_down can't store this field in parallel.
105 netdev = rcu_dereference_protected(ctx->netdev,
106 !refcount_read(&ctx->refcount));
108 async_cleanup = netdev && ctx->tx_conf == TLS_HW;
110 struct tls_offload_context_tx *offload_ctx = tls_offload_ctx_tx(ctx);
112 /* queue_work inside the spinlock
113 * to make sure tls_device_down waits for that work.
115 queue_work(destruct_wq, &offload_ctx->destruct_work);
117 spin_unlock_irqrestore(&tls_device_lock, flags);
120 tls_device_free_ctx(ctx);
123 /* We assume that the socket is already connected */
124 static struct net_device *get_netdev_for_sock(struct sock *sk)
126 struct dst_entry *dst = sk_dst_get(sk);
127 struct net_device *netdev = NULL;
130 netdev = netdev_sk_get_lowest_dev(dst->dev, sk);
139 static void destroy_record(struct tls_record_info *record)
143 for (i = 0; i < record->num_frags; i++)
144 __skb_frag_unref(&record->frags[i], false);
148 static void delete_all_records(struct tls_offload_context_tx *offload_ctx)
150 struct tls_record_info *info, *temp;
152 list_for_each_entry_safe(info, temp, &offload_ctx->records_list, list) {
153 list_del(&info->list);
154 destroy_record(info);
157 offload_ctx->retransmit_hint = NULL;
160 static void tls_icsk_clean_acked(struct sock *sk, u32 acked_seq)
162 struct tls_context *tls_ctx = tls_get_ctx(sk);
163 struct tls_record_info *info, *temp;
164 struct tls_offload_context_tx *ctx;
165 u64 deleted_records = 0;
171 ctx = tls_offload_ctx_tx(tls_ctx);
173 spin_lock_irqsave(&ctx->lock, flags);
174 info = ctx->retransmit_hint;
175 if (info && !before(acked_seq, info->end_seq))
176 ctx->retransmit_hint = NULL;
178 list_for_each_entry_safe(info, temp, &ctx->records_list, list) {
179 if (before(acked_seq, info->end_seq))
181 list_del(&info->list);
183 destroy_record(info);
187 ctx->unacked_record_sn += deleted_records;
188 spin_unlock_irqrestore(&ctx->lock, flags);
191 /* At this point, there should be no references on this
192 * socket and no in-flight SKBs associated with this
193 * socket, so it is safe to free all the resources.
195 void tls_device_sk_destruct(struct sock *sk)
197 struct tls_context *tls_ctx = tls_get_ctx(sk);
198 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
200 tls_ctx->sk_destruct(sk);
202 if (tls_ctx->tx_conf == TLS_HW) {
203 if (ctx->open_record)
204 destroy_record(ctx->open_record);
205 delete_all_records(ctx);
206 crypto_free_aead(ctx->aead_send);
207 clean_acked_data_disable(inet_csk(sk));
210 tls_device_queue_ctx_destruction(tls_ctx);
212 EXPORT_SYMBOL_GPL(tls_device_sk_destruct);
214 void tls_device_free_resources_tx(struct sock *sk)
216 struct tls_context *tls_ctx = tls_get_ctx(sk);
218 tls_free_partial_record(sk, tls_ctx);
221 void tls_offload_tx_resync_request(struct sock *sk, u32 got_seq, u32 exp_seq)
223 struct tls_context *tls_ctx = tls_get_ctx(sk);
225 trace_tls_device_tx_resync_req(sk, got_seq, exp_seq);
226 WARN_ON(test_and_set_bit(TLS_TX_SYNC_SCHED, &tls_ctx->flags));
228 EXPORT_SYMBOL_GPL(tls_offload_tx_resync_request);
230 static void tls_device_resync_tx(struct sock *sk, struct tls_context *tls_ctx,
233 struct net_device *netdev;
238 skb = tcp_write_queue_tail(sk);
240 TCP_SKB_CB(skb)->eor = 1;
242 rcd_sn = tls_ctx->tx.rec_seq;
244 trace_tls_device_tx_resync_send(sk, seq, rcd_sn);
245 down_read(&device_offload_lock);
246 netdev = rcu_dereference_protected(tls_ctx->netdev,
247 lockdep_is_held(&device_offload_lock));
249 err = netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq,
251 TLS_OFFLOAD_CTX_DIR_TX);
252 up_read(&device_offload_lock);
256 clear_bit_unlock(TLS_TX_SYNC_SCHED, &tls_ctx->flags);
259 static void tls_append_frag(struct tls_record_info *record,
260 struct page_frag *pfrag,
265 frag = &record->frags[record->num_frags - 1];
266 if (skb_frag_page(frag) == pfrag->page &&
267 skb_frag_off(frag) + skb_frag_size(frag) == pfrag->offset) {
268 skb_frag_size_add(frag, size);
271 __skb_frag_set_page(frag, pfrag->page);
272 skb_frag_off_set(frag, pfrag->offset);
273 skb_frag_size_set(frag, size);
275 get_page(pfrag->page);
278 pfrag->offset += size;
282 static int tls_push_record(struct sock *sk,
283 struct tls_context *ctx,
284 struct tls_offload_context_tx *offload_ctx,
285 struct tls_record_info *record,
288 struct tls_prot_info *prot = &ctx->prot_info;
289 struct tcp_sock *tp = tcp_sk(sk);
293 record->end_seq = tp->write_seq + record->len;
294 list_add_tail_rcu(&record->list, &offload_ctx->records_list);
295 offload_ctx->open_record = NULL;
297 if (test_bit(TLS_TX_SYNC_SCHED, &ctx->flags))
298 tls_device_resync_tx(sk, ctx, tp->write_seq);
300 tls_advance_record_sn(sk, prot, &ctx->tx);
302 for (i = 0; i < record->num_frags; i++) {
303 frag = &record->frags[i];
304 sg_unmark_end(&offload_ctx->sg_tx_data[i]);
305 sg_set_page(&offload_ctx->sg_tx_data[i], skb_frag_page(frag),
306 skb_frag_size(frag), skb_frag_off(frag));
307 sk_mem_charge(sk, skb_frag_size(frag));
308 get_page(skb_frag_page(frag));
310 sg_mark_end(&offload_ctx->sg_tx_data[record->num_frags - 1]);
312 /* all ready, send */
313 return tls_push_sg(sk, ctx, offload_ctx->sg_tx_data, 0, flags);
316 static int tls_device_record_close(struct sock *sk,
317 struct tls_context *ctx,
318 struct tls_record_info *record,
319 struct page_frag *pfrag,
320 unsigned char record_type)
322 struct tls_prot_info *prot = &ctx->prot_info;
326 * device will fill in the tag, we just need to append a placeholder
327 * use socket memory to improve coalescing (re-using a single buffer
328 * increases frag count)
329 * if we can't allocate memory now, steal some back from data
331 if (likely(skb_page_frag_refill(prot->tag_size, pfrag,
332 sk->sk_allocation))) {
334 tls_append_frag(record, pfrag, prot->tag_size);
336 ret = prot->tag_size;
337 if (record->len <= prot->overhead_size)
342 tls_fill_prepend(ctx, skb_frag_address(&record->frags[0]),
343 record->len - prot->overhead_size,
348 static int tls_create_new_record(struct tls_offload_context_tx *offload_ctx,
349 struct page_frag *pfrag,
352 struct tls_record_info *record;
355 record = kmalloc(sizeof(*record), GFP_KERNEL);
359 frag = &record->frags[0];
360 __skb_frag_set_page(frag, pfrag->page);
361 skb_frag_off_set(frag, pfrag->offset);
362 skb_frag_size_set(frag, prepend_size);
364 get_page(pfrag->page);
365 pfrag->offset += prepend_size;
367 record->num_frags = 1;
368 record->len = prepend_size;
369 offload_ctx->open_record = record;
373 static int tls_do_allocation(struct sock *sk,
374 struct tls_offload_context_tx *offload_ctx,
375 struct page_frag *pfrag,
380 if (!offload_ctx->open_record) {
381 if (unlikely(!skb_page_frag_refill(prepend_size, pfrag,
382 sk->sk_allocation))) {
383 READ_ONCE(sk->sk_prot)->enter_memory_pressure(sk);
384 sk_stream_moderate_sndbuf(sk);
388 ret = tls_create_new_record(offload_ctx, pfrag, prepend_size);
392 if (pfrag->size > pfrag->offset)
396 if (!sk_page_frag_refill(sk, pfrag))
402 static int tls_device_copy_data(void *addr, size_t bytes, struct iov_iter *i)
404 size_t pre_copy, nocache;
406 pre_copy = ~((unsigned long)addr - 1) & (SMP_CACHE_BYTES - 1);
408 pre_copy = min(pre_copy, bytes);
409 if (copy_from_iter(addr, pre_copy, i) != pre_copy)
415 nocache = round_down(bytes, SMP_CACHE_BYTES);
416 if (copy_from_iter_nocache(addr, nocache, i) != nocache)
421 if (bytes && copy_from_iter(addr, bytes, i) != bytes)
427 union tls_iter_offset {
428 struct iov_iter *msg_iter;
432 static int tls_push_data(struct sock *sk,
433 union tls_iter_offset iter_offset,
434 size_t size, int flags,
435 unsigned char record_type,
436 struct page *zc_page)
438 struct tls_context *tls_ctx = tls_get_ctx(sk);
439 struct tls_prot_info *prot = &tls_ctx->prot_info;
440 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
441 struct tls_record_info *record;
442 int tls_push_record_flags;
443 struct page_frag *pfrag;
444 size_t orig_size = size;
445 u32 max_open_record_len;
452 ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_SENDPAGE_NOTLAST))
455 if (unlikely(sk->sk_err))
458 flags |= MSG_SENDPAGE_DECRYPTED;
459 tls_push_record_flags = flags | MSG_SENDPAGE_NOTLAST;
461 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
462 if (tls_is_partially_sent_record(tls_ctx)) {
463 rc = tls_push_partial_record(sk, tls_ctx, flags);
468 pfrag = sk_page_frag(sk);
470 /* TLS_HEADER_SIZE is not counted as part of the TLS record, and
471 * we need to leave room for an authentication tag.
473 max_open_record_len = TLS_MAX_PAYLOAD_SIZE +
476 rc = tls_do_allocation(sk, ctx, pfrag, prot->prepend_size);
478 rc = sk_stream_wait_memory(sk, &timeo);
482 record = ctx->open_record;
486 if (record_type != TLS_RECORD_TYPE_DATA) {
487 /* avoid sending partial
488 * record with type !=
492 destroy_record(record);
493 ctx->open_record = NULL;
494 } else if (record->len > prot->prepend_size) {
501 record = ctx->open_record;
503 copy = min_t(size_t, size, max_open_record_len - record->len);
504 if (copy && zc_page) {
505 struct page_frag zc_pfrag;
507 zc_pfrag.page = zc_page;
508 zc_pfrag.offset = iter_offset.offset;
509 zc_pfrag.size = copy;
510 tls_append_frag(record, &zc_pfrag, copy);
512 iter_offset.offset += copy;
514 copy = min_t(size_t, copy, pfrag->size - pfrag->offset);
516 rc = tls_device_copy_data(page_address(pfrag->page) +
518 iter_offset.msg_iter);
521 tls_append_frag(record, pfrag, copy);
527 tls_push_record_flags = flags;
528 if (flags & (MSG_SENDPAGE_NOTLAST | MSG_MORE)) {
536 if (done || record->len >= max_open_record_len ||
537 (record->num_frags >= MAX_SKB_FRAGS - 1)) {
538 rc = tls_device_record_close(sk, tls_ctx, record,
545 destroy_record(record);
546 ctx->open_record = NULL;
551 rc = tls_push_record(sk,
555 tls_push_record_flags);
561 tls_ctx->pending_open_record_frags = more;
563 if (orig_size - size > 0)
564 rc = orig_size - size;
569 int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
571 unsigned char record_type = TLS_RECORD_TYPE_DATA;
572 struct tls_context *tls_ctx = tls_get_ctx(sk);
573 union tls_iter_offset iter;
576 mutex_lock(&tls_ctx->tx_lock);
579 if (unlikely(msg->msg_controllen)) {
580 rc = tls_process_cmsg(sk, msg, &record_type);
585 iter.msg_iter = &msg->msg_iter;
586 rc = tls_push_data(sk, iter, size, msg->msg_flags, record_type, NULL);
590 mutex_unlock(&tls_ctx->tx_lock);
594 int tls_device_sendpage(struct sock *sk, struct page *page,
595 int offset, size_t size, int flags)
597 struct tls_context *tls_ctx = tls_get_ctx(sk);
598 union tls_iter_offset iter_offset;
599 struct iov_iter msg_iter;
604 if (flags & MSG_SENDPAGE_NOTLAST)
607 mutex_lock(&tls_ctx->tx_lock);
610 if (flags & MSG_OOB) {
615 if (tls_ctx->zerocopy_sendfile) {
616 iter_offset.offset = offset;
617 rc = tls_push_data(sk, iter_offset, size,
618 flags, TLS_RECORD_TYPE_DATA, page);
623 iov.iov_base = kaddr + offset;
625 iov_iter_kvec(&msg_iter, ITER_SOURCE, &iov, 1, size);
626 iter_offset.msg_iter = &msg_iter;
627 rc = tls_push_data(sk, iter_offset, size, flags, TLS_RECORD_TYPE_DATA,
633 mutex_unlock(&tls_ctx->tx_lock);
637 struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context,
638 u32 seq, u64 *p_record_sn)
640 u64 record_sn = context->hint_record_sn;
641 struct tls_record_info *info, *last;
643 info = context->retransmit_hint;
645 before(seq, info->end_seq - info->len)) {
646 /* if retransmit_hint is irrelevant start
647 * from the beginning of the list
649 info = list_first_entry_or_null(&context->records_list,
650 struct tls_record_info, list);
653 /* send the start_marker record if seq number is before the
654 * tls offload start marker sequence number. This record is
655 * required to handle TCP packets which are before TLS offload
657 * And if it's not start marker, look if this seq number
658 * belongs to the list.
660 if (likely(!tls_record_is_start_marker(info))) {
661 /* we have the first record, get the last record to see
662 * if this seq number belongs to the list.
664 last = list_last_entry(&context->records_list,
665 struct tls_record_info, list);
667 if (!between(seq, tls_record_start_seq(info),
671 record_sn = context->unacked_record_sn;
674 /* We just need the _rcu for the READ_ONCE() */
676 list_for_each_entry_from_rcu(info, &context->records_list, list) {
677 if (before(seq, info->end_seq)) {
678 if (!context->retransmit_hint ||
680 context->retransmit_hint->end_seq)) {
681 context->hint_record_sn = record_sn;
682 context->retransmit_hint = info;
684 *p_record_sn = record_sn;
685 goto exit_rcu_unlock;
695 EXPORT_SYMBOL(tls_get_record);
697 static int tls_device_push_pending_record(struct sock *sk, int flags)
699 union tls_iter_offset iter;
700 struct iov_iter msg_iter;
702 iov_iter_kvec(&msg_iter, ITER_SOURCE, NULL, 0, 0);
703 iter.msg_iter = &msg_iter;
704 return tls_push_data(sk, iter, 0, flags, TLS_RECORD_TYPE_DATA, NULL);
707 void tls_device_write_space(struct sock *sk, struct tls_context *ctx)
709 if (tls_is_partially_sent_record(ctx)) {
710 gfp_t sk_allocation = sk->sk_allocation;
712 WARN_ON_ONCE(sk->sk_write_pending);
714 sk->sk_allocation = GFP_ATOMIC;
715 tls_push_partial_record(sk, ctx,
716 MSG_DONTWAIT | MSG_NOSIGNAL |
717 MSG_SENDPAGE_DECRYPTED);
718 sk->sk_allocation = sk_allocation;
722 static void tls_device_resync_rx(struct tls_context *tls_ctx,
723 struct sock *sk, u32 seq, u8 *rcd_sn)
725 struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
726 struct net_device *netdev;
728 trace_tls_device_rx_resync_send(sk, seq, rcd_sn, rx_ctx->resync_type);
730 netdev = rcu_dereference(tls_ctx->netdev);
732 netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq, rcd_sn,
733 TLS_OFFLOAD_CTX_DIR_RX);
735 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXDEVICERESYNC);
739 tls_device_rx_resync_async(struct tls_offload_resync_async *resync_async,
740 s64 resync_req, u32 *seq, u16 *rcd_delta)
742 u32 is_async = resync_req & RESYNC_REQ_ASYNC;
743 u32 req_seq = resync_req >> 32;
744 u32 req_end = req_seq + ((resync_req >> 16) & 0xffff);
750 /* shouldn't get to wraparound:
751 * too long in async stage, something bad happened
753 if (WARN_ON_ONCE(resync_async->rcd_delta == USHRT_MAX))
756 /* asynchronous stage: log all headers seq such that
757 * req_seq <= seq <= end_seq, and wait for real resync request
759 if (before(*seq, req_seq))
761 if (!after(*seq, req_end) &&
762 resync_async->loglen < TLS_DEVICE_RESYNC_ASYNC_LOGMAX)
763 resync_async->log[resync_async->loglen++] = *seq;
765 resync_async->rcd_delta++;
770 /* synchronous stage: check against the logged entries and
771 * proceed to check the next entries if no match was found
773 for (i = 0; i < resync_async->loglen; i++)
774 if (req_seq == resync_async->log[i] &&
775 atomic64_try_cmpxchg(&resync_async->req, &resync_req, 0)) {
776 *rcd_delta = resync_async->rcd_delta - i;
778 resync_async->loglen = 0;
779 resync_async->rcd_delta = 0;
783 resync_async->loglen = 0;
784 resync_async->rcd_delta = 0;
786 if (req_seq == *seq &&
787 atomic64_try_cmpxchg(&resync_async->req,
794 void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq)
796 struct tls_context *tls_ctx = tls_get_ctx(sk);
797 struct tls_offload_context_rx *rx_ctx;
798 u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE];
799 u32 sock_data, is_req_pending;
800 struct tls_prot_info *prot;
805 if (tls_ctx->rx_conf != TLS_HW)
807 if (unlikely(test_bit(TLS_RX_DEV_DEGRADED, &tls_ctx->flags)))
810 prot = &tls_ctx->prot_info;
811 rx_ctx = tls_offload_ctx_rx(tls_ctx);
812 memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size);
814 switch (rx_ctx->resync_type) {
815 case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ:
816 resync_req = atomic64_read(&rx_ctx->resync_req);
817 req_seq = resync_req >> 32;
818 seq += TLS_HEADER_SIZE - 1;
819 is_req_pending = resync_req;
821 if (likely(!is_req_pending) || req_seq != seq ||
822 !atomic64_try_cmpxchg(&rx_ctx->resync_req, &resync_req, 0))
825 case TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT:
826 if (likely(!rx_ctx->resync_nh_do_now))
829 /* head of next rec is already in, note that the sock_inq will
830 * include the currently parsed message when called from parser
832 sock_data = tcp_inq(sk);
833 if (sock_data > rcd_len) {
834 trace_tls_device_rx_resync_nh_delay(sk, sock_data,
839 rx_ctx->resync_nh_do_now = 0;
841 tls_bigint_increment(rcd_sn, prot->rec_seq_size);
843 case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC:
844 resync_req = atomic64_read(&rx_ctx->resync_async->req);
845 is_req_pending = resync_req;
846 if (likely(!is_req_pending))
849 if (!tls_device_rx_resync_async(rx_ctx->resync_async,
850 resync_req, &seq, &rcd_delta))
852 tls_bigint_subtract(rcd_sn, rcd_delta);
856 tls_device_resync_rx(tls_ctx, sk, seq, rcd_sn);
859 static void tls_device_core_ctrl_rx_resync(struct tls_context *tls_ctx,
860 struct tls_offload_context_rx *ctx,
861 struct sock *sk, struct sk_buff *skb)
863 struct strp_msg *rxm;
865 /* device will request resyncs by itself based on stream scan */
866 if (ctx->resync_type != TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT)
868 /* already scheduled */
869 if (ctx->resync_nh_do_now)
871 /* seen decrypted fragments since last fully-failed record */
872 if (ctx->resync_nh_reset) {
873 ctx->resync_nh_reset = 0;
874 ctx->resync_nh.decrypted_failed = 1;
875 ctx->resync_nh.decrypted_tgt = TLS_DEVICE_RESYNC_NH_START_IVAL;
879 if (++ctx->resync_nh.decrypted_failed <= ctx->resync_nh.decrypted_tgt)
882 /* doing resync, bump the next target in case it fails */
883 if (ctx->resync_nh.decrypted_tgt < TLS_DEVICE_RESYNC_NH_MAX_IVAL)
884 ctx->resync_nh.decrypted_tgt *= 2;
886 ctx->resync_nh.decrypted_tgt += TLS_DEVICE_RESYNC_NH_MAX_IVAL;
890 /* head of next rec is already in, parser will sync for us */
891 if (tcp_inq(sk) > rxm->full_len) {
892 trace_tls_device_rx_resync_nh_schedule(sk);
893 ctx->resync_nh_do_now = 1;
895 struct tls_prot_info *prot = &tls_ctx->prot_info;
896 u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE];
898 memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size);
899 tls_bigint_increment(rcd_sn, prot->rec_seq_size);
901 tls_device_resync_rx(tls_ctx, sk, tcp_sk(sk)->copied_seq,
907 tls_device_reencrypt(struct sock *sk, struct tls_context *tls_ctx)
909 struct tls_sw_context_rx *sw_ctx = tls_sw_ctx_rx(tls_ctx);
910 const struct tls_cipher_size_desc *cipher_sz;
911 int err, offset, copy, data_len, pos;
912 struct sk_buff *skb, *skb_iter;
913 struct scatterlist sg[1];
914 struct strp_msg *rxm;
915 char *orig_buf, *buf;
917 switch (tls_ctx->crypto_recv.info.cipher_type) {
918 case TLS_CIPHER_AES_GCM_128:
919 case TLS_CIPHER_AES_GCM_256:
924 cipher_sz = &tls_cipher_size_desc[tls_ctx->crypto_recv.info.cipher_type];
926 rxm = strp_msg(tls_strp_msg(sw_ctx));
927 orig_buf = kmalloc(rxm->full_len + TLS_HEADER_SIZE + cipher_sz->iv,
933 err = tls_strp_msg_cow(sw_ctx);
937 skb = tls_strp_msg(sw_ctx);
939 offset = rxm->offset;
941 sg_init_table(sg, 1);
942 sg_set_buf(&sg[0], buf,
943 rxm->full_len + TLS_HEADER_SIZE + cipher_sz->iv);
944 err = skb_copy_bits(skb, offset, buf, TLS_HEADER_SIZE + cipher_sz->iv);
948 /* We are interested only in the decrypted data not the auth */
949 err = decrypt_skb(sk, sg);
955 data_len = rxm->full_len - cipher_sz->tag;
957 if (skb_pagelen(skb) > offset) {
958 copy = min_t(int, skb_pagelen(skb) - offset, data_len);
960 if (skb->decrypted) {
961 err = skb_store_bits(skb, offset, buf, copy);
970 pos = skb_pagelen(skb);
971 skb_walk_frags(skb, skb_iter) {
974 /* Practically all frags must belong to msg if reencrypt
975 * is needed with current strparser and coalescing logic,
976 * but strparser may "get optimized", so let's be safe.
978 if (pos + skb_iter->len <= offset)
980 if (pos >= data_len + rxm->offset)
983 frag_pos = offset - pos;
984 copy = min_t(int, skb_iter->len - frag_pos,
985 data_len + rxm->offset - offset);
987 if (skb_iter->decrypted) {
988 err = skb_store_bits(skb_iter, frag_pos, buf, copy);
996 pos += skb_iter->len;
1004 int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx)
1006 struct tls_offload_context_rx *ctx = tls_offload_ctx_rx(tls_ctx);
1007 struct tls_sw_context_rx *sw_ctx = tls_sw_ctx_rx(tls_ctx);
1008 struct sk_buff *skb = tls_strp_msg(sw_ctx);
1009 struct strp_msg *rxm = strp_msg(skb);
1010 int is_decrypted, is_encrypted;
1012 if (!tls_strp_msg_mixed_decrypted(sw_ctx)) {
1013 is_decrypted = skb->decrypted;
1014 is_encrypted = !is_decrypted;
1020 trace_tls_device_decrypted(sk, tcp_sk(sk)->copied_seq - rxm->full_len,
1021 tls_ctx->rx.rec_seq, rxm->full_len,
1022 is_encrypted, is_decrypted);
1024 if (unlikely(test_bit(TLS_RX_DEV_DEGRADED, &tls_ctx->flags))) {
1025 if (likely(is_encrypted || is_decrypted))
1026 return is_decrypted;
1028 /* After tls_device_down disables the offload, the next SKB will
1029 * likely have initial fragments decrypted, and final ones not
1030 * decrypted. We need to reencrypt that single SKB.
1032 return tls_device_reencrypt(sk, tls_ctx);
1035 /* Return immediately if the record is either entirely plaintext or
1036 * entirely ciphertext. Otherwise handle reencrypt partially decrypted
1040 ctx->resync_nh_reset = 1;
1041 return is_decrypted;
1044 tls_device_core_ctrl_rx_resync(tls_ctx, ctx, sk, skb);
1048 ctx->resync_nh_reset = 1;
1049 return tls_device_reencrypt(sk, tls_ctx);
1052 static void tls_device_attach(struct tls_context *ctx, struct sock *sk,
1053 struct net_device *netdev)
1055 if (sk->sk_destruct != tls_device_sk_destruct) {
1056 refcount_set(&ctx->refcount, 1);
1058 RCU_INIT_POINTER(ctx->netdev, netdev);
1059 spin_lock_irq(&tls_device_lock);
1060 list_add_tail(&ctx->list, &tls_device_list);
1061 spin_unlock_irq(&tls_device_lock);
1063 ctx->sk_destruct = sk->sk_destruct;
1064 smp_store_release(&sk->sk_destruct, tls_device_sk_destruct);
1068 int tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
1070 struct tls_context *tls_ctx = tls_get_ctx(sk);
1071 struct tls_prot_info *prot = &tls_ctx->prot_info;
1072 const struct tls_cipher_size_desc *cipher_sz;
1073 struct tls_record_info *start_marker_record;
1074 struct tls_offload_context_tx *offload_ctx;
1075 struct tls_crypto_info *crypto_info;
1076 struct net_device *netdev;
1078 struct sk_buff *skb;
1085 if (ctx->priv_ctx_tx)
1088 netdev = get_netdev_for_sock(sk);
1090 pr_err_ratelimited("%s: netdev not found\n", __func__);
1094 if (!(netdev->features & NETIF_F_HW_TLS_TX)) {
1096 goto release_netdev;
1099 crypto_info = &ctx->crypto_send.info;
1100 if (crypto_info->version != TLS_1_2_VERSION) {
1102 goto release_netdev;
1105 switch (crypto_info->cipher_type) {
1106 case TLS_CIPHER_AES_GCM_128:
1107 iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv;
1109 ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq;
1111 case TLS_CIPHER_AES_GCM_256:
1112 iv = ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->iv;
1114 ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->rec_seq;
1118 goto release_netdev;
1120 cipher_sz = &tls_cipher_size_desc[crypto_info->cipher_type];
1122 /* Sanity-check the rec_seq_size for stack allocations */
1123 if (cipher_sz->rec_seq > TLS_MAX_REC_SEQ_SIZE) {
1125 goto release_netdev;
1128 prot->version = crypto_info->version;
1129 prot->cipher_type = crypto_info->cipher_type;
1130 prot->prepend_size = TLS_HEADER_SIZE + cipher_sz->iv;
1131 prot->tag_size = cipher_sz->tag;
1132 prot->overhead_size = prot->prepend_size + prot->tag_size;
1133 prot->iv_size = cipher_sz->iv;
1134 prot->salt_size = cipher_sz->salt;
1135 ctx->tx.iv = kmalloc(cipher_sz->iv + cipher_sz->salt, GFP_KERNEL);
1138 goto release_netdev;
1141 memcpy(ctx->tx.iv + cipher_sz->salt, iv, cipher_sz->iv);
1143 prot->rec_seq_size = cipher_sz->rec_seq;
1144 ctx->tx.rec_seq = kmemdup(rec_seq, cipher_sz->rec_seq, GFP_KERNEL);
1145 if (!ctx->tx.rec_seq) {
1150 start_marker_record = kmalloc(sizeof(*start_marker_record), GFP_KERNEL);
1151 if (!start_marker_record) {
1156 offload_ctx = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_TX, GFP_KERNEL);
1159 goto free_marker_record;
1162 rc = tls_sw_fallback_init(sk, offload_ctx, crypto_info);
1164 goto free_offload_ctx;
1166 /* start at rec_seq - 1 to account for the start marker record */
1167 memcpy(&rcd_sn, ctx->tx.rec_seq, sizeof(rcd_sn));
1168 offload_ctx->unacked_record_sn = be64_to_cpu(rcd_sn) - 1;
1170 start_marker_record->end_seq = tcp_sk(sk)->write_seq;
1171 start_marker_record->len = 0;
1172 start_marker_record->num_frags = 0;
1174 INIT_WORK(&offload_ctx->destruct_work, tls_device_tx_del_task);
1175 offload_ctx->ctx = ctx;
1177 INIT_LIST_HEAD(&offload_ctx->records_list);
1178 list_add_tail(&start_marker_record->list, &offload_ctx->records_list);
1179 spin_lock_init(&offload_ctx->lock);
1180 sg_init_table(offload_ctx->sg_tx_data,
1181 ARRAY_SIZE(offload_ctx->sg_tx_data));
1183 clean_acked_data_enable(inet_csk(sk), &tls_icsk_clean_acked);
1184 ctx->push_pending_record = tls_device_push_pending_record;
1186 /* TLS offload is greatly simplified if we don't send
1187 * SKBs where only part of the payload needs to be encrypted.
1188 * So mark the last skb in the write queue as end of record.
1190 skb = tcp_write_queue_tail(sk);
1192 TCP_SKB_CB(skb)->eor = 1;
1194 /* Avoid offloading if the device is down
1195 * We don't want to offload new flows after
1196 * the NETDEV_DOWN event
1198 * device_offload_lock is taken in tls_devices's NETDEV_DOWN
1199 * handler thus protecting from the device going down before
1200 * ctx was added to tls_device_list.
1202 down_read(&device_offload_lock);
1203 if (!(netdev->flags & IFF_UP)) {
1208 ctx->priv_ctx_tx = offload_ctx;
1209 rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_TX,
1210 &ctx->crypto_send.info,
1211 tcp_sk(sk)->write_seq);
1212 trace_tls_device_offload_set(sk, TLS_OFFLOAD_CTX_DIR_TX,
1213 tcp_sk(sk)->write_seq, rec_seq, rc);
1217 tls_device_attach(ctx, sk, netdev);
1218 up_read(&device_offload_lock);
1220 /* following this assignment tls_is_sk_tx_device_offloaded
1221 * will return true and the context might be accessed
1222 * by the netdev's xmit function.
1224 smp_store_release(&sk->sk_validate_xmit_skb, tls_validate_xmit_skb);
1230 up_read(&device_offload_lock);
1231 clean_acked_data_disable(inet_csk(sk));
1232 crypto_free_aead(offload_ctx->aead_send);
1235 ctx->priv_ctx_tx = NULL;
1237 kfree(start_marker_record);
1239 kfree(ctx->tx.rec_seq);
1247 int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
1249 struct tls12_crypto_info_aes_gcm_128 *info;
1250 struct tls_offload_context_rx *context;
1251 struct net_device *netdev;
1254 if (ctx->crypto_recv.info.version != TLS_1_2_VERSION)
1257 netdev = get_netdev_for_sock(sk);
1259 pr_err_ratelimited("%s: netdev not found\n", __func__);
1263 if (!(netdev->features & NETIF_F_HW_TLS_RX)) {
1265 goto release_netdev;
1268 /* Avoid offloading if the device is down
1269 * We don't want to offload new flows after
1270 * the NETDEV_DOWN event
1272 * device_offload_lock is taken in tls_devices's NETDEV_DOWN
1273 * handler thus protecting from the device going down before
1274 * ctx was added to tls_device_list.
1276 down_read(&device_offload_lock);
1277 if (!(netdev->flags & IFF_UP)) {
1282 context = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_RX, GFP_KERNEL);
1287 context->resync_nh_reset = 1;
1289 ctx->priv_ctx_rx = context;
1290 rc = tls_set_sw_offload(sk, ctx, 0);
1294 rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_RX,
1295 &ctx->crypto_recv.info,
1296 tcp_sk(sk)->copied_seq);
1297 info = (void *)&ctx->crypto_recv.info;
1298 trace_tls_device_offload_set(sk, TLS_OFFLOAD_CTX_DIR_RX,
1299 tcp_sk(sk)->copied_seq, info->rec_seq, rc);
1301 goto free_sw_resources;
1303 tls_device_attach(ctx, sk, netdev);
1304 up_read(&device_offload_lock);
1311 up_read(&device_offload_lock);
1312 tls_sw_free_resources_rx(sk);
1313 down_read(&device_offload_lock);
1315 ctx->priv_ctx_rx = NULL;
1317 up_read(&device_offload_lock);
1323 void tls_device_offload_cleanup_rx(struct sock *sk)
1325 struct tls_context *tls_ctx = tls_get_ctx(sk);
1326 struct net_device *netdev;
1328 down_read(&device_offload_lock);
1329 netdev = rcu_dereference_protected(tls_ctx->netdev,
1330 lockdep_is_held(&device_offload_lock));
1334 netdev->tlsdev_ops->tls_dev_del(netdev, tls_ctx,
1335 TLS_OFFLOAD_CTX_DIR_RX);
1337 if (tls_ctx->tx_conf != TLS_HW) {
1339 rcu_assign_pointer(tls_ctx->netdev, NULL);
1341 set_bit(TLS_RX_DEV_CLOSED, &tls_ctx->flags);
1344 up_read(&device_offload_lock);
1345 tls_sw_release_resources_rx(sk);
1348 static int tls_device_down(struct net_device *netdev)
1350 struct tls_context *ctx, *tmp;
1351 unsigned long flags;
1354 /* Request a write lock to block new offload attempts */
1355 down_write(&device_offload_lock);
1357 spin_lock_irqsave(&tls_device_lock, flags);
1358 list_for_each_entry_safe(ctx, tmp, &tls_device_list, list) {
1359 struct net_device *ctx_netdev =
1360 rcu_dereference_protected(ctx->netdev,
1361 lockdep_is_held(&device_offload_lock));
1363 if (ctx_netdev != netdev ||
1364 !refcount_inc_not_zero(&ctx->refcount))
1367 list_move(&ctx->list, &list);
1369 spin_unlock_irqrestore(&tls_device_lock, flags);
1371 list_for_each_entry_safe(ctx, tmp, &list, list) {
1372 /* Stop offloaded TX and switch to the fallback.
1373 * tls_is_sk_tx_device_offloaded will return false.
1375 WRITE_ONCE(ctx->sk->sk_validate_xmit_skb, tls_validate_xmit_skb_sw);
1377 /* Stop the RX and TX resync.
1378 * tls_dev_resync must not be called after tls_dev_del.
1380 rcu_assign_pointer(ctx->netdev, NULL);
1382 /* Start skipping the RX resync logic completely. */
1383 set_bit(TLS_RX_DEV_DEGRADED, &ctx->flags);
1385 /* Sync with inflight packets. After this point:
1386 * TX: no non-encrypted packets will be passed to the driver.
1387 * RX: resync requests from the driver will be ignored.
1391 /* Release the offload context on the driver side. */
1392 if (ctx->tx_conf == TLS_HW)
1393 netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
1394 TLS_OFFLOAD_CTX_DIR_TX);
1395 if (ctx->rx_conf == TLS_HW &&
1396 !test_bit(TLS_RX_DEV_CLOSED, &ctx->flags))
1397 netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
1398 TLS_OFFLOAD_CTX_DIR_RX);
1402 /* Move the context to a separate list for two reasons:
1403 * 1. When the context is deallocated, list_del is called.
1404 * 2. It's no longer an offloaded context, so we don't want to
1405 * run offload-specific code on this context.
1407 spin_lock_irqsave(&tls_device_lock, flags);
1408 list_move_tail(&ctx->list, &tls_device_down_list);
1409 spin_unlock_irqrestore(&tls_device_lock, flags);
1411 /* Device contexts for RX and TX will be freed in on sk_destruct
1412 * by tls_device_free_ctx. rx_conf and tx_conf stay in TLS_HW.
1413 * Now release the ref taken above.
1415 if (refcount_dec_and_test(&ctx->refcount)) {
1416 /* sk_destruct ran after tls_device_down took a ref, and
1417 * it returned early. Complete the destruction here.
1419 list_del(&ctx->list);
1420 tls_device_free_ctx(ctx);
1424 up_write(&device_offload_lock);
1426 flush_workqueue(destruct_wq);
1431 static int tls_dev_event(struct notifier_block *this, unsigned long event,
1434 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1436 if (!dev->tlsdev_ops &&
1437 !(dev->features & (NETIF_F_HW_TLS_RX | NETIF_F_HW_TLS_TX)))
1441 case NETDEV_REGISTER:
1442 case NETDEV_FEAT_CHANGE:
1443 if (netif_is_bond_master(dev))
1445 if ((dev->features & NETIF_F_HW_TLS_RX) &&
1446 !dev->tlsdev_ops->tls_dev_resync)
1449 if (dev->tlsdev_ops &&
1450 dev->tlsdev_ops->tls_dev_add &&
1451 dev->tlsdev_ops->tls_dev_del)
1456 return tls_device_down(dev);
1461 static struct notifier_block tls_dev_notifier = {
1462 .notifier_call = tls_dev_event,
1465 int __init tls_device_init(void)
1469 destruct_wq = alloc_workqueue("ktls_device_destruct", 0, 0);
1473 err = register_netdevice_notifier(&tls_dev_notifier);
1475 destroy_workqueue(destruct_wq);
1480 void __exit tls_device_cleanup(void)
1482 unregister_netdevice_notifier(&tls_dev_notifier);
1483 destroy_workqueue(destruct_wq);
1484 clean_acked_data_flush();