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>
43 /* device_offload_lock is used to synchronize tls_dev_add
44 * against NETDEV_DOWN notifications.
46 static DECLARE_RWSEM(device_offload_lock);
48 static void tls_device_gc_task(struct work_struct *work);
50 static DECLARE_WORK(tls_device_gc_work, tls_device_gc_task);
51 static LIST_HEAD(tls_device_gc_list);
52 static LIST_HEAD(tls_device_list);
53 static LIST_HEAD(tls_device_down_list);
54 static DEFINE_SPINLOCK(tls_device_lock);
56 static void tls_device_free_ctx(struct tls_context *ctx)
58 if (ctx->tx_conf == TLS_HW) {
59 kfree(tls_offload_ctx_tx(ctx));
60 kfree(ctx->tx.rec_seq);
64 if (ctx->rx_conf == TLS_HW)
65 kfree(tls_offload_ctx_rx(ctx));
67 tls_ctx_free(NULL, ctx);
70 static void tls_device_gc_task(struct work_struct *work)
72 struct tls_context *ctx, *tmp;
76 spin_lock_irqsave(&tls_device_lock, flags);
77 list_splice_init(&tls_device_gc_list, &gc_list);
78 spin_unlock_irqrestore(&tls_device_lock, flags);
80 list_for_each_entry_safe(ctx, tmp, &gc_list, list) {
81 struct net_device *netdev = ctx->netdev;
83 if (netdev && ctx->tx_conf == TLS_HW) {
84 netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
85 TLS_OFFLOAD_CTX_DIR_TX);
91 tls_device_free_ctx(ctx);
95 static void tls_device_queue_ctx_destruction(struct tls_context *ctx)
99 spin_lock_irqsave(&tls_device_lock, flags);
100 if (unlikely(!refcount_dec_and_test(&ctx->refcount)))
103 list_move_tail(&ctx->list, &tls_device_gc_list);
105 /* schedule_work inside the spinlock
106 * to make sure tls_device_down waits for that work.
108 schedule_work(&tls_device_gc_work);
110 spin_unlock_irqrestore(&tls_device_lock, flags);
113 /* We assume that the socket is already connected */
114 static struct net_device *get_netdev_for_sock(struct sock *sk)
116 struct dst_entry *dst = sk_dst_get(sk);
117 struct net_device *netdev = NULL;
120 netdev = netdev_sk_get_lowest_dev(dst->dev, sk);
129 static void destroy_record(struct tls_record_info *record)
133 for (i = 0; i < record->num_frags; i++)
134 __skb_frag_unref(&record->frags[i], false);
138 static void delete_all_records(struct tls_offload_context_tx *offload_ctx)
140 struct tls_record_info *info, *temp;
142 list_for_each_entry_safe(info, temp, &offload_ctx->records_list, list) {
143 list_del(&info->list);
144 destroy_record(info);
147 offload_ctx->retransmit_hint = NULL;
150 static void tls_icsk_clean_acked(struct sock *sk, u32 acked_seq)
152 struct tls_context *tls_ctx = tls_get_ctx(sk);
153 struct tls_record_info *info, *temp;
154 struct tls_offload_context_tx *ctx;
155 u64 deleted_records = 0;
161 ctx = tls_offload_ctx_tx(tls_ctx);
163 spin_lock_irqsave(&ctx->lock, flags);
164 info = ctx->retransmit_hint;
165 if (info && !before(acked_seq, info->end_seq))
166 ctx->retransmit_hint = NULL;
168 list_for_each_entry_safe(info, temp, &ctx->records_list, list) {
169 if (before(acked_seq, info->end_seq))
171 list_del(&info->list);
173 destroy_record(info);
177 ctx->unacked_record_sn += deleted_records;
178 spin_unlock_irqrestore(&ctx->lock, flags);
181 /* At this point, there should be no references on this
182 * socket and no in-flight SKBs associated with this
183 * socket, so it is safe to free all the resources.
185 void tls_device_sk_destruct(struct sock *sk)
187 struct tls_context *tls_ctx = tls_get_ctx(sk);
188 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
190 tls_ctx->sk_destruct(sk);
192 if (tls_ctx->tx_conf == TLS_HW) {
193 if (ctx->open_record)
194 destroy_record(ctx->open_record);
195 delete_all_records(ctx);
196 crypto_free_aead(ctx->aead_send);
197 clean_acked_data_disable(inet_csk(sk));
200 tls_device_queue_ctx_destruction(tls_ctx);
202 EXPORT_SYMBOL_GPL(tls_device_sk_destruct);
204 void tls_device_free_resources_tx(struct sock *sk)
206 struct tls_context *tls_ctx = tls_get_ctx(sk);
208 tls_free_partial_record(sk, tls_ctx);
211 void tls_offload_tx_resync_request(struct sock *sk, u32 got_seq, u32 exp_seq)
213 struct tls_context *tls_ctx = tls_get_ctx(sk);
215 trace_tls_device_tx_resync_req(sk, got_seq, exp_seq);
216 WARN_ON(test_and_set_bit(TLS_TX_SYNC_SCHED, &tls_ctx->flags));
218 EXPORT_SYMBOL_GPL(tls_offload_tx_resync_request);
220 static void tls_device_resync_tx(struct sock *sk, struct tls_context *tls_ctx,
223 struct net_device *netdev;
228 skb = tcp_write_queue_tail(sk);
230 TCP_SKB_CB(skb)->eor = 1;
232 rcd_sn = tls_ctx->tx.rec_seq;
234 trace_tls_device_tx_resync_send(sk, seq, rcd_sn);
235 down_read(&device_offload_lock);
236 netdev = tls_ctx->netdev;
238 err = netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq,
240 TLS_OFFLOAD_CTX_DIR_TX);
241 up_read(&device_offload_lock);
245 clear_bit_unlock(TLS_TX_SYNC_SCHED, &tls_ctx->flags);
248 static void tls_append_frag(struct tls_record_info *record,
249 struct page_frag *pfrag,
254 frag = &record->frags[record->num_frags - 1];
255 if (skb_frag_page(frag) == pfrag->page &&
256 skb_frag_off(frag) + skb_frag_size(frag) == pfrag->offset) {
257 skb_frag_size_add(frag, size);
260 __skb_frag_set_page(frag, pfrag->page);
261 skb_frag_off_set(frag, pfrag->offset);
262 skb_frag_size_set(frag, size);
264 get_page(pfrag->page);
267 pfrag->offset += size;
271 static int tls_push_record(struct sock *sk,
272 struct tls_context *ctx,
273 struct tls_offload_context_tx *offload_ctx,
274 struct tls_record_info *record,
277 struct tls_prot_info *prot = &ctx->prot_info;
278 struct tcp_sock *tp = tcp_sk(sk);
282 record->end_seq = tp->write_seq + record->len;
283 list_add_tail_rcu(&record->list, &offload_ctx->records_list);
284 offload_ctx->open_record = NULL;
286 if (test_bit(TLS_TX_SYNC_SCHED, &ctx->flags))
287 tls_device_resync_tx(sk, ctx, tp->write_seq);
289 tls_advance_record_sn(sk, prot, &ctx->tx);
291 for (i = 0; i < record->num_frags; i++) {
292 frag = &record->frags[i];
293 sg_unmark_end(&offload_ctx->sg_tx_data[i]);
294 sg_set_page(&offload_ctx->sg_tx_data[i], skb_frag_page(frag),
295 skb_frag_size(frag), skb_frag_off(frag));
296 sk_mem_charge(sk, skb_frag_size(frag));
297 get_page(skb_frag_page(frag));
299 sg_mark_end(&offload_ctx->sg_tx_data[record->num_frags - 1]);
301 /* all ready, send */
302 return tls_push_sg(sk, ctx, offload_ctx->sg_tx_data, 0, flags);
305 static int tls_device_record_close(struct sock *sk,
306 struct tls_context *ctx,
307 struct tls_record_info *record,
308 struct page_frag *pfrag,
309 unsigned char record_type)
311 struct tls_prot_info *prot = &ctx->prot_info;
315 * device will fill in the tag, we just need to append a placeholder
316 * use socket memory to improve coalescing (re-using a single buffer
317 * increases frag count)
318 * if we can't allocate memory now, steal some back from data
320 if (likely(skb_page_frag_refill(prot->tag_size, pfrag,
321 sk->sk_allocation))) {
323 tls_append_frag(record, pfrag, prot->tag_size);
325 ret = prot->tag_size;
326 if (record->len <= prot->overhead_size)
331 tls_fill_prepend(ctx, skb_frag_address(&record->frags[0]),
332 record->len - prot->overhead_size,
337 static int tls_create_new_record(struct tls_offload_context_tx *offload_ctx,
338 struct page_frag *pfrag,
341 struct tls_record_info *record;
344 record = kmalloc(sizeof(*record), GFP_KERNEL);
348 frag = &record->frags[0];
349 __skb_frag_set_page(frag, pfrag->page);
350 skb_frag_off_set(frag, pfrag->offset);
351 skb_frag_size_set(frag, prepend_size);
353 get_page(pfrag->page);
354 pfrag->offset += prepend_size;
356 record->num_frags = 1;
357 record->len = prepend_size;
358 offload_ctx->open_record = record;
362 static int tls_do_allocation(struct sock *sk,
363 struct tls_offload_context_tx *offload_ctx,
364 struct page_frag *pfrag,
369 if (!offload_ctx->open_record) {
370 if (unlikely(!skb_page_frag_refill(prepend_size, pfrag,
371 sk->sk_allocation))) {
372 READ_ONCE(sk->sk_prot)->enter_memory_pressure(sk);
373 sk_stream_moderate_sndbuf(sk);
377 ret = tls_create_new_record(offload_ctx, pfrag, prepend_size);
381 if (pfrag->size > pfrag->offset)
385 if (!sk_page_frag_refill(sk, pfrag))
391 static int tls_device_copy_data(void *addr, size_t bytes, struct iov_iter *i)
393 size_t pre_copy, nocache;
395 pre_copy = ~((unsigned long)addr - 1) & (SMP_CACHE_BYTES - 1);
397 pre_copy = min(pre_copy, bytes);
398 if (copy_from_iter(addr, pre_copy, i) != pre_copy)
404 nocache = round_down(bytes, SMP_CACHE_BYTES);
405 if (copy_from_iter_nocache(addr, nocache, i) != nocache)
410 if (bytes && copy_from_iter(addr, bytes, i) != bytes)
416 static int tls_push_data(struct sock *sk,
417 struct iov_iter *msg_iter,
418 size_t size, int flags,
419 unsigned char record_type)
421 struct tls_context *tls_ctx = tls_get_ctx(sk);
422 struct tls_prot_info *prot = &tls_ctx->prot_info;
423 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
424 struct tls_record_info *record;
425 int tls_push_record_flags;
426 struct page_frag *pfrag;
427 size_t orig_size = size;
428 u32 max_open_record_len;
435 ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_SENDPAGE_NOTLAST))
438 if (unlikely(sk->sk_err))
441 flags |= MSG_SENDPAGE_DECRYPTED;
442 tls_push_record_flags = flags | MSG_SENDPAGE_NOTLAST;
444 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
445 if (tls_is_partially_sent_record(tls_ctx)) {
446 rc = tls_push_partial_record(sk, tls_ctx, flags);
451 pfrag = sk_page_frag(sk);
453 /* TLS_HEADER_SIZE is not counted as part of the TLS record, and
454 * we need to leave room for an authentication tag.
456 max_open_record_len = TLS_MAX_PAYLOAD_SIZE +
459 rc = tls_do_allocation(sk, ctx, pfrag, prot->prepend_size);
461 rc = sk_stream_wait_memory(sk, &timeo);
465 record = ctx->open_record;
469 if (record_type != TLS_RECORD_TYPE_DATA) {
470 /* avoid sending partial
471 * record with type !=
475 destroy_record(record);
476 ctx->open_record = NULL;
477 } else if (record->len > prot->prepend_size) {
484 record = ctx->open_record;
485 copy = min_t(size_t, size, (pfrag->size - pfrag->offset));
486 copy = min_t(size_t, copy, (max_open_record_len - record->len));
489 rc = tls_device_copy_data(page_address(pfrag->page) +
490 pfrag->offset, copy, msg_iter);
493 tls_append_frag(record, pfrag, copy);
499 tls_push_record_flags = flags;
500 if (flags & (MSG_SENDPAGE_NOTLAST | MSG_MORE)) {
508 if (done || record->len >= max_open_record_len ||
509 (record->num_frags >= MAX_SKB_FRAGS - 1)) {
510 rc = tls_device_record_close(sk, tls_ctx, record,
517 destroy_record(record);
518 ctx->open_record = NULL;
523 rc = tls_push_record(sk,
527 tls_push_record_flags);
533 tls_ctx->pending_open_record_frags = more;
535 if (orig_size - size > 0)
536 rc = orig_size - size;
541 int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
543 unsigned char record_type = TLS_RECORD_TYPE_DATA;
544 struct tls_context *tls_ctx = tls_get_ctx(sk);
547 mutex_lock(&tls_ctx->tx_lock);
550 if (unlikely(msg->msg_controllen)) {
551 rc = tls_proccess_cmsg(sk, msg, &record_type);
556 rc = tls_push_data(sk, &msg->msg_iter, size,
557 msg->msg_flags, record_type);
561 mutex_unlock(&tls_ctx->tx_lock);
565 int tls_device_sendpage(struct sock *sk, struct page *page,
566 int offset, size_t size, int flags)
568 struct tls_context *tls_ctx = tls_get_ctx(sk);
569 struct iov_iter msg_iter;
574 if (flags & MSG_SENDPAGE_NOTLAST)
577 mutex_lock(&tls_ctx->tx_lock);
580 if (flags & MSG_OOB) {
586 iov.iov_base = kaddr + offset;
588 iov_iter_kvec(&msg_iter, WRITE, &iov, 1, size);
589 rc = tls_push_data(sk, &msg_iter, size,
590 flags, TLS_RECORD_TYPE_DATA);
595 mutex_unlock(&tls_ctx->tx_lock);
599 struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context,
600 u32 seq, u64 *p_record_sn)
602 u64 record_sn = context->hint_record_sn;
603 struct tls_record_info *info, *last;
605 info = context->retransmit_hint;
607 before(seq, info->end_seq - info->len)) {
608 /* if retransmit_hint is irrelevant start
609 * from the beginning of the list
611 info = list_first_entry_or_null(&context->records_list,
612 struct tls_record_info, list);
615 /* send the start_marker record if seq number is before the
616 * tls offload start marker sequence number. This record is
617 * required to handle TCP packets which are before TLS offload
619 * And if it's not start marker, look if this seq number
620 * belongs to the list.
622 if (likely(!tls_record_is_start_marker(info))) {
623 /* we have the first record, get the last record to see
624 * if this seq number belongs to the list.
626 last = list_last_entry(&context->records_list,
627 struct tls_record_info, list);
629 if (!between(seq, tls_record_start_seq(info),
633 record_sn = context->unacked_record_sn;
636 /* We just need the _rcu for the READ_ONCE() */
638 list_for_each_entry_from_rcu(info, &context->records_list, list) {
639 if (before(seq, info->end_seq)) {
640 if (!context->retransmit_hint ||
642 context->retransmit_hint->end_seq)) {
643 context->hint_record_sn = record_sn;
644 context->retransmit_hint = info;
646 *p_record_sn = record_sn;
647 goto exit_rcu_unlock;
657 EXPORT_SYMBOL(tls_get_record);
659 static int tls_device_push_pending_record(struct sock *sk, int flags)
661 struct iov_iter msg_iter;
663 iov_iter_kvec(&msg_iter, WRITE, NULL, 0, 0);
664 return tls_push_data(sk, &msg_iter, 0, flags, TLS_RECORD_TYPE_DATA);
667 void tls_device_write_space(struct sock *sk, struct tls_context *ctx)
669 if (tls_is_partially_sent_record(ctx)) {
670 gfp_t sk_allocation = sk->sk_allocation;
672 WARN_ON_ONCE(sk->sk_write_pending);
674 sk->sk_allocation = GFP_ATOMIC;
675 tls_push_partial_record(sk, ctx,
676 MSG_DONTWAIT | MSG_NOSIGNAL |
677 MSG_SENDPAGE_DECRYPTED);
678 sk->sk_allocation = sk_allocation;
682 static void tls_device_resync_rx(struct tls_context *tls_ctx,
683 struct sock *sk, u32 seq, u8 *rcd_sn)
685 struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
686 struct net_device *netdev;
688 trace_tls_device_rx_resync_send(sk, seq, rcd_sn, rx_ctx->resync_type);
690 netdev = READ_ONCE(tls_ctx->netdev);
692 netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq, rcd_sn,
693 TLS_OFFLOAD_CTX_DIR_RX);
695 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXDEVICERESYNC);
699 tls_device_rx_resync_async(struct tls_offload_resync_async *resync_async,
700 s64 resync_req, u32 *seq, u16 *rcd_delta)
702 u32 is_async = resync_req & RESYNC_REQ_ASYNC;
703 u32 req_seq = resync_req >> 32;
704 u32 req_end = req_seq + ((resync_req >> 16) & 0xffff);
710 /* shouldn't get to wraparound:
711 * too long in async stage, something bad happened
713 if (WARN_ON_ONCE(resync_async->rcd_delta == USHRT_MAX))
716 /* asynchronous stage: log all headers seq such that
717 * req_seq <= seq <= end_seq, and wait for real resync request
719 if (before(*seq, req_seq))
721 if (!after(*seq, req_end) &&
722 resync_async->loglen < TLS_DEVICE_RESYNC_ASYNC_LOGMAX)
723 resync_async->log[resync_async->loglen++] = *seq;
725 resync_async->rcd_delta++;
730 /* synchronous stage: check against the logged entries and
731 * proceed to check the next entries if no match was found
733 for (i = 0; i < resync_async->loglen; i++)
734 if (req_seq == resync_async->log[i] &&
735 atomic64_try_cmpxchg(&resync_async->req, &resync_req, 0)) {
736 *rcd_delta = resync_async->rcd_delta - i;
738 resync_async->loglen = 0;
739 resync_async->rcd_delta = 0;
743 resync_async->loglen = 0;
744 resync_async->rcd_delta = 0;
746 if (req_seq == *seq &&
747 atomic64_try_cmpxchg(&resync_async->req,
754 void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq)
756 struct tls_context *tls_ctx = tls_get_ctx(sk);
757 struct tls_offload_context_rx *rx_ctx;
758 u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE];
759 u32 sock_data, is_req_pending;
760 struct tls_prot_info *prot;
765 if (tls_ctx->rx_conf != TLS_HW)
767 if (unlikely(test_bit(TLS_RX_DEV_DEGRADED, &tls_ctx->flags)))
770 prot = &tls_ctx->prot_info;
771 rx_ctx = tls_offload_ctx_rx(tls_ctx);
772 memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size);
774 switch (rx_ctx->resync_type) {
775 case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ:
776 resync_req = atomic64_read(&rx_ctx->resync_req);
777 req_seq = resync_req >> 32;
778 seq += TLS_HEADER_SIZE - 1;
779 is_req_pending = resync_req;
781 if (likely(!is_req_pending) || req_seq != seq ||
782 !atomic64_try_cmpxchg(&rx_ctx->resync_req, &resync_req, 0))
785 case TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT:
786 if (likely(!rx_ctx->resync_nh_do_now))
789 /* head of next rec is already in, note that the sock_inq will
790 * include the currently parsed message when called from parser
792 sock_data = tcp_inq(sk);
793 if (sock_data > rcd_len) {
794 trace_tls_device_rx_resync_nh_delay(sk, sock_data,
799 rx_ctx->resync_nh_do_now = 0;
801 tls_bigint_increment(rcd_sn, prot->rec_seq_size);
803 case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC:
804 resync_req = atomic64_read(&rx_ctx->resync_async->req);
805 is_req_pending = resync_req;
806 if (likely(!is_req_pending))
809 if (!tls_device_rx_resync_async(rx_ctx->resync_async,
810 resync_req, &seq, &rcd_delta))
812 tls_bigint_subtract(rcd_sn, rcd_delta);
816 tls_device_resync_rx(tls_ctx, sk, seq, rcd_sn);
819 static void tls_device_core_ctrl_rx_resync(struct tls_context *tls_ctx,
820 struct tls_offload_context_rx *ctx,
821 struct sock *sk, struct sk_buff *skb)
823 struct strp_msg *rxm;
825 /* device will request resyncs by itself based on stream scan */
826 if (ctx->resync_type != TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT)
828 /* already scheduled */
829 if (ctx->resync_nh_do_now)
831 /* seen decrypted fragments since last fully-failed record */
832 if (ctx->resync_nh_reset) {
833 ctx->resync_nh_reset = 0;
834 ctx->resync_nh.decrypted_failed = 1;
835 ctx->resync_nh.decrypted_tgt = TLS_DEVICE_RESYNC_NH_START_IVAL;
839 if (++ctx->resync_nh.decrypted_failed <= ctx->resync_nh.decrypted_tgt)
842 /* doing resync, bump the next target in case it fails */
843 if (ctx->resync_nh.decrypted_tgt < TLS_DEVICE_RESYNC_NH_MAX_IVAL)
844 ctx->resync_nh.decrypted_tgt *= 2;
846 ctx->resync_nh.decrypted_tgt += TLS_DEVICE_RESYNC_NH_MAX_IVAL;
850 /* head of next rec is already in, parser will sync for us */
851 if (tcp_inq(sk) > rxm->full_len) {
852 trace_tls_device_rx_resync_nh_schedule(sk);
853 ctx->resync_nh_do_now = 1;
855 struct tls_prot_info *prot = &tls_ctx->prot_info;
856 u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE];
858 memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size);
859 tls_bigint_increment(rcd_sn, prot->rec_seq_size);
861 tls_device_resync_rx(tls_ctx, sk, tcp_sk(sk)->copied_seq,
866 static int tls_device_reencrypt(struct sock *sk, struct sk_buff *skb)
868 struct strp_msg *rxm = strp_msg(skb);
869 int err = 0, offset = rxm->offset, copy, nsg, data_len, pos;
870 struct sk_buff *skb_iter, *unused;
871 struct scatterlist sg[1];
872 char *orig_buf, *buf;
874 orig_buf = kmalloc(rxm->full_len + TLS_HEADER_SIZE +
875 TLS_CIPHER_AES_GCM_128_IV_SIZE, sk->sk_allocation);
880 nsg = skb_cow_data(skb, 0, &unused);
881 if (unlikely(nsg < 0)) {
886 sg_init_table(sg, 1);
887 sg_set_buf(&sg[0], buf,
888 rxm->full_len + TLS_HEADER_SIZE +
889 TLS_CIPHER_AES_GCM_128_IV_SIZE);
890 err = skb_copy_bits(skb, offset, buf,
891 TLS_HEADER_SIZE + TLS_CIPHER_AES_GCM_128_IV_SIZE);
895 /* We are interested only in the decrypted data not the auth */
896 err = decrypt_skb(sk, skb, sg);
902 data_len = rxm->full_len - TLS_CIPHER_AES_GCM_128_TAG_SIZE;
904 if (skb_pagelen(skb) > offset) {
905 copy = min_t(int, skb_pagelen(skb) - offset, data_len);
907 if (skb->decrypted) {
908 err = skb_store_bits(skb, offset, buf, copy);
917 pos = skb_pagelen(skb);
918 skb_walk_frags(skb, skb_iter) {
921 /* Practically all frags must belong to msg if reencrypt
922 * is needed with current strparser and coalescing logic,
923 * but strparser may "get optimized", so let's be safe.
925 if (pos + skb_iter->len <= offset)
927 if (pos >= data_len + rxm->offset)
930 frag_pos = offset - pos;
931 copy = min_t(int, skb_iter->len - frag_pos,
932 data_len + rxm->offset - offset);
934 if (skb_iter->decrypted) {
935 err = skb_store_bits(skb_iter, frag_pos, buf, copy);
943 pos += skb_iter->len;
951 int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx,
952 struct sk_buff *skb, struct strp_msg *rxm)
954 struct tls_offload_context_rx *ctx = tls_offload_ctx_rx(tls_ctx);
955 int is_decrypted = skb->decrypted;
956 int is_encrypted = !is_decrypted;
957 struct sk_buff *skb_iter;
959 /* Check if all the data is decrypted already */
960 skb_walk_frags(skb, skb_iter) {
961 is_decrypted &= skb_iter->decrypted;
962 is_encrypted &= !skb_iter->decrypted;
965 trace_tls_device_decrypted(sk, tcp_sk(sk)->copied_seq - rxm->full_len,
966 tls_ctx->rx.rec_seq, rxm->full_len,
967 is_encrypted, is_decrypted);
969 ctx->sw.decrypted |= is_decrypted;
971 if (unlikely(test_bit(TLS_RX_DEV_DEGRADED, &tls_ctx->flags))) {
972 if (likely(is_encrypted || is_decrypted))
975 /* After tls_device_down disables the offload, the next SKB will
976 * likely have initial fragments decrypted, and final ones not
977 * decrypted. We need to reencrypt that single SKB.
979 return tls_device_reencrypt(sk, skb);
982 /* Return immediately if the record is either entirely plaintext or
983 * entirely ciphertext. Otherwise handle reencrypt partially decrypted
987 ctx->resync_nh_reset = 1;
991 tls_device_core_ctrl_rx_resync(tls_ctx, ctx, sk, skb);
995 ctx->resync_nh_reset = 1;
996 return tls_device_reencrypt(sk, skb);
999 static void tls_device_attach(struct tls_context *ctx, struct sock *sk,
1000 struct net_device *netdev)
1002 if (sk->sk_destruct != tls_device_sk_destruct) {
1003 refcount_set(&ctx->refcount, 1);
1005 ctx->netdev = netdev;
1006 spin_lock_irq(&tls_device_lock);
1007 list_add_tail(&ctx->list, &tls_device_list);
1008 spin_unlock_irq(&tls_device_lock);
1010 ctx->sk_destruct = sk->sk_destruct;
1011 smp_store_release(&sk->sk_destruct, tls_device_sk_destruct);
1015 int tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
1017 u16 nonce_size, tag_size, iv_size, rec_seq_size, salt_size;
1018 struct tls_context *tls_ctx = tls_get_ctx(sk);
1019 struct tls_prot_info *prot = &tls_ctx->prot_info;
1020 struct tls_record_info *start_marker_record;
1021 struct tls_offload_context_tx *offload_ctx;
1022 struct tls_crypto_info *crypto_info;
1023 struct net_device *netdev;
1025 struct sk_buff *skb;
1032 if (ctx->priv_ctx_tx)
1035 start_marker_record = kmalloc(sizeof(*start_marker_record), GFP_KERNEL);
1036 if (!start_marker_record)
1039 offload_ctx = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_TX, GFP_KERNEL);
1042 goto free_marker_record;
1045 crypto_info = &ctx->crypto_send.info;
1046 if (crypto_info->version != TLS_1_2_VERSION) {
1048 goto free_offload_ctx;
1051 switch (crypto_info->cipher_type) {
1052 case TLS_CIPHER_AES_GCM_128:
1053 nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
1054 tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
1055 iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
1056 iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv;
1057 rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
1058 salt_size = TLS_CIPHER_AES_GCM_128_SALT_SIZE;
1060 ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq;
1064 goto free_offload_ctx;
1067 /* Sanity-check the rec_seq_size for stack allocations */
1068 if (rec_seq_size > TLS_MAX_REC_SEQ_SIZE) {
1070 goto free_offload_ctx;
1073 prot->version = crypto_info->version;
1074 prot->cipher_type = crypto_info->cipher_type;
1075 prot->prepend_size = TLS_HEADER_SIZE + nonce_size;
1076 prot->tag_size = tag_size;
1077 prot->overhead_size = prot->prepend_size + prot->tag_size;
1078 prot->iv_size = iv_size;
1079 prot->salt_size = salt_size;
1080 ctx->tx.iv = kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
1084 goto free_offload_ctx;
1087 memcpy(ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size);
1089 prot->rec_seq_size = rec_seq_size;
1090 ctx->tx.rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL);
1091 if (!ctx->tx.rec_seq) {
1096 rc = tls_sw_fallback_init(sk, offload_ctx, crypto_info);
1100 /* start at rec_seq - 1 to account for the start marker record */
1101 memcpy(&rcd_sn, ctx->tx.rec_seq, sizeof(rcd_sn));
1102 offload_ctx->unacked_record_sn = be64_to_cpu(rcd_sn) - 1;
1104 start_marker_record->end_seq = tcp_sk(sk)->write_seq;
1105 start_marker_record->len = 0;
1106 start_marker_record->num_frags = 0;
1108 INIT_LIST_HEAD(&offload_ctx->records_list);
1109 list_add_tail(&start_marker_record->list, &offload_ctx->records_list);
1110 spin_lock_init(&offload_ctx->lock);
1111 sg_init_table(offload_ctx->sg_tx_data,
1112 ARRAY_SIZE(offload_ctx->sg_tx_data));
1114 clean_acked_data_enable(inet_csk(sk), &tls_icsk_clean_acked);
1115 ctx->push_pending_record = tls_device_push_pending_record;
1117 /* TLS offload is greatly simplified if we don't send
1118 * SKBs where only part of the payload needs to be encrypted.
1119 * So mark the last skb in the write queue as end of record.
1121 skb = tcp_write_queue_tail(sk);
1123 TCP_SKB_CB(skb)->eor = 1;
1125 netdev = get_netdev_for_sock(sk);
1127 pr_err_ratelimited("%s: netdev not found\n", __func__);
1132 if (!(netdev->features & NETIF_F_HW_TLS_TX)) {
1134 goto release_netdev;
1137 /* Avoid offloading if the device is down
1138 * We don't want to offload new flows after
1139 * the NETDEV_DOWN event
1141 * device_offload_lock is taken in tls_devices's NETDEV_DOWN
1142 * handler thus protecting from the device going down before
1143 * ctx was added to tls_device_list.
1145 down_read(&device_offload_lock);
1146 if (!(netdev->flags & IFF_UP)) {
1151 ctx->priv_ctx_tx = offload_ctx;
1152 rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_TX,
1153 &ctx->crypto_send.info,
1154 tcp_sk(sk)->write_seq);
1155 trace_tls_device_offload_set(sk, TLS_OFFLOAD_CTX_DIR_TX,
1156 tcp_sk(sk)->write_seq, rec_seq, rc);
1160 tls_device_attach(ctx, sk, netdev);
1161 up_read(&device_offload_lock);
1163 /* following this assignment tls_is_sk_tx_device_offloaded
1164 * will return true and the context might be accessed
1165 * by the netdev's xmit function.
1167 smp_store_release(&sk->sk_validate_xmit_skb, tls_validate_xmit_skb);
1173 up_read(&device_offload_lock);
1177 clean_acked_data_disable(inet_csk(sk));
1178 crypto_free_aead(offload_ctx->aead_send);
1180 kfree(ctx->tx.rec_seq);
1185 ctx->priv_ctx_tx = NULL;
1187 kfree(start_marker_record);
1191 int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
1193 struct tls12_crypto_info_aes_gcm_128 *info;
1194 struct tls_offload_context_rx *context;
1195 struct net_device *netdev;
1198 if (ctx->crypto_recv.info.version != TLS_1_2_VERSION)
1201 netdev = get_netdev_for_sock(sk);
1203 pr_err_ratelimited("%s: netdev not found\n", __func__);
1207 if (!(netdev->features & NETIF_F_HW_TLS_RX)) {
1209 goto release_netdev;
1212 /* Avoid offloading if the device is down
1213 * We don't want to offload new flows after
1214 * the NETDEV_DOWN event
1216 * device_offload_lock is taken in tls_devices's NETDEV_DOWN
1217 * handler thus protecting from the device going down before
1218 * ctx was added to tls_device_list.
1220 down_read(&device_offload_lock);
1221 if (!(netdev->flags & IFF_UP)) {
1226 context = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_RX, GFP_KERNEL);
1231 context->resync_nh_reset = 1;
1233 ctx->priv_ctx_rx = context;
1234 rc = tls_set_sw_offload(sk, ctx, 0);
1238 rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_RX,
1239 &ctx->crypto_recv.info,
1240 tcp_sk(sk)->copied_seq);
1241 info = (void *)&ctx->crypto_recv.info;
1242 trace_tls_device_offload_set(sk, TLS_OFFLOAD_CTX_DIR_RX,
1243 tcp_sk(sk)->copied_seq, info->rec_seq, rc);
1245 goto free_sw_resources;
1247 tls_device_attach(ctx, sk, netdev);
1248 up_read(&device_offload_lock);
1255 up_read(&device_offload_lock);
1256 tls_sw_free_resources_rx(sk);
1257 down_read(&device_offload_lock);
1259 ctx->priv_ctx_rx = NULL;
1261 up_read(&device_offload_lock);
1267 void tls_device_offload_cleanup_rx(struct sock *sk)
1269 struct tls_context *tls_ctx = tls_get_ctx(sk);
1270 struct net_device *netdev;
1272 down_read(&device_offload_lock);
1273 netdev = tls_ctx->netdev;
1277 netdev->tlsdev_ops->tls_dev_del(netdev, tls_ctx,
1278 TLS_OFFLOAD_CTX_DIR_RX);
1280 if (tls_ctx->tx_conf != TLS_HW) {
1282 tls_ctx->netdev = NULL;
1284 set_bit(TLS_RX_DEV_CLOSED, &tls_ctx->flags);
1287 up_read(&device_offload_lock);
1288 tls_sw_release_resources_rx(sk);
1291 static int tls_device_down(struct net_device *netdev)
1293 struct tls_context *ctx, *tmp;
1294 unsigned long flags;
1297 /* Request a write lock to block new offload attempts */
1298 down_write(&device_offload_lock);
1300 spin_lock_irqsave(&tls_device_lock, flags);
1301 list_for_each_entry_safe(ctx, tmp, &tls_device_list, list) {
1302 if (ctx->netdev != netdev ||
1303 !refcount_inc_not_zero(&ctx->refcount))
1306 list_move(&ctx->list, &list);
1308 spin_unlock_irqrestore(&tls_device_lock, flags);
1310 list_for_each_entry_safe(ctx, tmp, &list, list) {
1311 /* Stop offloaded TX and switch to the fallback.
1312 * tls_is_sk_tx_device_offloaded will return false.
1314 WRITE_ONCE(ctx->sk->sk_validate_xmit_skb, tls_validate_xmit_skb_sw);
1316 /* Stop the RX and TX resync.
1317 * tls_dev_resync must not be called after tls_dev_del.
1319 WRITE_ONCE(ctx->netdev, NULL);
1321 /* Start skipping the RX resync logic completely. */
1322 set_bit(TLS_RX_DEV_DEGRADED, &ctx->flags);
1324 /* Sync with inflight packets. After this point:
1325 * TX: no non-encrypted packets will be passed to the driver.
1326 * RX: resync requests from the driver will be ignored.
1330 /* Release the offload context on the driver side. */
1331 if (ctx->tx_conf == TLS_HW)
1332 netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
1333 TLS_OFFLOAD_CTX_DIR_TX);
1334 if (ctx->rx_conf == TLS_HW &&
1335 !test_bit(TLS_RX_DEV_CLOSED, &ctx->flags))
1336 netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
1337 TLS_OFFLOAD_CTX_DIR_RX);
1341 /* Move the context to a separate list for two reasons:
1342 * 1. When the context is deallocated, list_del is called.
1343 * 2. It's no longer an offloaded context, so we don't want to
1344 * run offload-specific code on this context.
1346 spin_lock_irqsave(&tls_device_lock, flags);
1347 list_move_tail(&ctx->list, &tls_device_down_list);
1348 spin_unlock_irqrestore(&tls_device_lock, flags);
1350 /* Device contexts for RX and TX will be freed in on sk_destruct
1351 * by tls_device_free_ctx. rx_conf and tx_conf stay in TLS_HW.
1352 * Now release the ref taken above.
1354 if (refcount_dec_and_test(&ctx->refcount)) {
1355 /* sk_destruct ran after tls_device_down took a ref, and
1356 * it returned early. Complete the destruction here.
1358 list_del(&ctx->list);
1359 tls_device_free_ctx(ctx);
1363 up_write(&device_offload_lock);
1365 flush_work(&tls_device_gc_work);
1370 static int tls_dev_event(struct notifier_block *this, unsigned long event,
1373 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1375 if (!dev->tlsdev_ops &&
1376 !(dev->features & (NETIF_F_HW_TLS_RX | NETIF_F_HW_TLS_TX)))
1380 case NETDEV_REGISTER:
1381 case NETDEV_FEAT_CHANGE:
1382 if (netif_is_bond_master(dev))
1384 if ((dev->features & NETIF_F_HW_TLS_RX) &&
1385 !dev->tlsdev_ops->tls_dev_resync)
1388 if (dev->tlsdev_ops &&
1389 dev->tlsdev_ops->tls_dev_add &&
1390 dev->tlsdev_ops->tls_dev_del)
1395 return tls_device_down(dev);
1400 static struct notifier_block tls_dev_notifier = {
1401 .notifier_call = tls_dev_event,
1404 int __init tls_device_init(void)
1406 return register_netdevice_notifier(&tls_dev_notifier);
1409 void __exit tls_device_cleanup(void)
1411 unregister_netdevice_notifier(&tls_dev_notifier);
1412 flush_work(&tls_device_gc_work);
1413 clean_acked_data_flush();