1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Tom Herbert <tom@herbertland.com> */
4 #include <linux/skbuff.h>
5 #include <linux/skbuff_ref.h>
6 #include <linux/workqueue.h>
7 #include <net/strparser.h>
14 static struct workqueue_struct *tls_strp_wq;
16 static void tls_strp_abort_strp(struct tls_strparser *strp, int err)
23 /* Report an error on the lower socket */
24 WRITE_ONCE(strp->sk->sk_err, -err);
25 /* Paired with smp_rmb() in tcp_poll() */
27 sk_error_report(strp->sk);
30 static void tls_strp_anchor_free(struct tls_strparser *strp)
32 struct skb_shared_info *shinfo = skb_shinfo(strp->anchor);
34 DEBUG_NET_WARN_ON_ONCE(atomic_read(&shinfo->dataref) != 1);
36 shinfo->frag_list = NULL;
37 consume_skb(strp->anchor);
41 static struct sk_buff *
42 tls_strp_skb_copy(struct tls_strparser *strp, struct sk_buff *in_skb,
48 skb = alloc_skb_with_frags(0, len, TLS_PAGE_ORDER,
49 &err, strp->sk->sk_allocation);
53 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
54 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
56 WARN_ON_ONCE(skb_copy_bits(in_skb, offset,
57 skb_frag_address(frag),
58 skb_frag_size(frag)));
59 offset += skb_frag_size(frag);
64 skb_copy_header(skb, in_skb);
68 /* Create a new skb with the contents of input copied to its page frags */
69 static struct sk_buff *tls_strp_msg_make_copy(struct tls_strparser *strp)
74 skb = tls_strp_skb_copy(strp, strp->anchor, strp->stm.offset,
84 /* Steal the input skb, input msg is invalid after calling this function */
85 struct sk_buff *tls_strp_msg_detach(struct tls_sw_context_rx *ctx)
87 struct tls_strparser *strp = &ctx->strp;
89 #ifdef CONFIG_TLS_DEVICE
90 DEBUG_NET_WARN_ON_ONCE(!strp->anchor->decrypted);
92 /* This function turns an input into an output,
93 * that can only happen if we have offload.
98 if (strp->copy_mode) {
101 /* Replace anchor with an empty skb, this is a little
102 * dangerous but __tls_cur_msg() warns on empty skbs
103 * so hopefully we'll catch abuses.
105 skb = alloc_skb(0, strp->sk->sk_allocation);
109 swap(strp->anchor, skb);
113 return tls_strp_msg_make_copy(strp);
116 /* Force the input skb to be in copy mode. The data ownership remains
117 * with the input skb itself (meaning unpause will wipe it) but it can
120 int tls_strp_msg_cow(struct tls_sw_context_rx *ctx)
122 struct tls_strparser *strp = &ctx->strp;
128 skb = tls_strp_msg_make_copy(strp);
132 tls_strp_anchor_free(strp);
135 tcp_read_done(strp->sk, strp->stm.full_len);
141 /* Make a clone (in the skb sense) of the input msg to keep a reference
142 * to the underlying data. The reference-holding skbs get placed on
145 int tls_strp_msg_hold(struct tls_strparser *strp, struct sk_buff_head *dst)
147 struct skb_shared_info *shinfo = skb_shinfo(strp->anchor);
149 if (strp->copy_mode) {
152 WARN_ON_ONCE(!shinfo->nr_frags);
154 /* We can't skb_clone() the anchor, it gets wiped by unpause */
155 skb = alloc_skb(0, strp->sk->sk_allocation);
159 __skb_queue_tail(dst, strp->anchor);
162 struct sk_buff *iter, *clone;
163 int chunk, len, offset;
165 offset = strp->stm.offset;
166 len = strp->stm.full_len;
167 iter = shinfo->frag_list;
170 if (iter->len <= offset) {
175 chunk = iter->len - offset;
178 clone = skb_clone(iter, strp->sk->sk_allocation);
181 __skb_queue_tail(dst, clone);
192 static void tls_strp_flush_anchor_copy(struct tls_strparser *strp)
194 struct skb_shared_info *shinfo = skb_shinfo(strp->anchor);
197 DEBUG_NET_WARN_ON_ONCE(atomic_read(&shinfo->dataref) != 1);
199 for (i = 0; i < shinfo->nr_frags; i++)
200 __skb_frag_unref(&shinfo->frags[i], false);
201 shinfo->nr_frags = 0;
202 if (strp->copy_mode) {
203 kfree_skb_list(shinfo->frag_list);
204 shinfo->frag_list = NULL;
207 strp->mixed_decrypted = 0;
210 static int tls_strp_copyin_frag(struct tls_strparser *strp, struct sk_buff *skb,
211 struct sk_buff *in_skb, unsigned int offset,
218 frag = &skb_shinfo(skb)->frags[skb->len / PAGE_SIZE];
221 /* First make sure we got the header */
222 if (!strp->stm.full_len) {
223 /* Assume one page is more than enough for headers */
224 chunk = min_t(size_t, len, PAGE_SIZE - skb_frag_size(frag));
225 WARN_ON_ONCE(skb_copy_bits(in_skb, offset,
226 skb_frag_address(frag) +
231 skb->data_len += chunk;
232 skb_frag_size_add(frag, chunk);
234 sz = tls_rx_msg_size(strp, skb);
238 /* We may have over-read, sz == 0 is guaranteed under-read */
239 if (unlikely(sz && sz < skb->len)) {
240 int over = skb->len - sz;
242 WARN_ON_ONCE(over > chunk);
244 skb->data_len -= over;
245 skb_frag_size_add(frag, -over);
254 strp->stm.full_len = sz;
255 if (!strp->stm.full_len)
259 /* Load up more data */
260 while (len && strp->stm.full_len > skb->len) {
261 chunk = min_t(size_t, len, strp->stm.full_len - skb->len);
262 chunk = min_t(size_t, chunk, PAGE_SIZE - skb_frag_size(frag));
263 WARN_ON_ONCE(skb_copy_bits(in_skb, offset,
264 skb_frag_address(frag) +
269 skb->data_len += chunk;
270 skb_frag_size_add(frag, chunk);
280 static int tls_strp_copyin_skb(struct tls_strparser *strp, struct sk_buff *skb,
281 struct sk_buff *in_skb, unsigned int offset,
284 struct sk_buff *nskb, *first, *last;
285 struct skb_shared_info *shinfo;
289 if (strp->stm.full_len)
290 chunk = strp->stm.full_len - skb->len;
292 chunk = TLS_MAX_PAYLOAD_SIZE + PAGE_SIZE;
293 chunk = min(chunk, in_len);
295 nskb = tls_strp_skb_copy(strp, in_skb, offset, chunk);
299 shinfo = skb_shinfo(skb);
300 if (!shinfo->frag_list) {
301 shinfo->frag_list = nskb;
304 first = shinfo->frag_list;
311 skb->data_len += chunk;
313 if (!strp->stm.full_len) {
314 sz = tls_rx_msg_size(strp, skb);
318 /* We may have over-read, sz == 0 is guaranteed under-read */
319 if (unlikely(sz && sz < skb->len)) {
320 int over = skb->len - sz;
322 WARN_ON_ONCE(over > chunk);
324 skb->data_len -= over;
325 __pskb_trim(nskb, nskb->len - over);
330 strp->stm.full_len = sz;
336 static int tls_strp_copyin(read_descriptor_t *desc, struct sk_buff *in_skb,
337 unsigned int offset, size_t in_len)
339 struct tls_strparser *strp = (struct tls_strparser *)desc->arg.data;
348 skb_copy_decrypted(skb, in_skb);
350 strp->mixed_decrypted |= !!skb_cmp_decrypted(skb, in_skb);
352 if (IS_ENABLED(CONFIG_TLS_DEVICE) && strp->mixed_decrypted)
353 ret = tls_strp_copyin_skb(strp, skb, in_skb, offset, in_len);
355 ret = tls_strp_copyin_frag(strp, skb, in_skb, offset, in_len);
361 if (strp->stm.full_len && strp->stm.full_len == skb->len) {
364 WRITE_ONCE(strp->msg_ready, 1);
365 tls_rx_msg_ready(strp);
371 static int tls_strp_read_copyin(struct tls_strparser *strp)
373 read_descriptor_t desc;
375 desc.arg.data = strp;
377 desc.count = 1; /* give more than one skb per call */
379 /* sk should be locked here, so okay to do read_sock */
380 tcp_read_sock(strp->sk, &desc, tls_strp_copyin);
385 static int tls_strp_read_copy(struct tls_strparser *strp, bool qshort)
387 struct skb_shared_info *shinfo;
391 /* If the rbuf is small or rcv window has collapsed to 0 we need
392 * to read the data out. Otherwise the connection will stall.
393 * Without pressure threshold of INT_MAX will never be ready.
395 if (likely(qshort && !tcp_epollin_ready(strp->sk, INT_MAX)))
398 shinfo = skb_shinfo(strp->anchor);
400 /* If we don't know the length go max plus page for cipher overhead */
401 need_spc = strp->stm.full_len ?: TLS_MAX_PAYLOAD_SIZE + PAGE_SIZE;
403 for (len = need_spc; len > 0; len -= PAGE_SIZE) {
404 page = alloc_page(strp->sk->sk_allocation);
406 tls_strp_flush_anchor_copy(strp);
410 skb_fill_page_desc(strp->anchor, shinfo->nr_frags++,
414 shinfo->frag_list = NULL;
417 strp->stm.offset = 0;
419 strp->anchor->len = 0;
420 strp->anchor->data_len = 0;
421 strp->anchor->truesize = round_up(need_spc, PAGE_SIZE);
423 tls_strp_read_copyin(strp);
428 static bool tls_strp_check_queue_ok(struct tls_strparser *strp)
430 unsigned int len = strp->stm.offset + strp->stm.full_len;
431 struct sk_buff *first, *skb;
434 first = skb_shinfo(strp->anchor)->frag_list;
436 seq = TCP_SKB_CB(first)->seq;
438 /* Make sure there's no duplicate data in the queue,
439 * and the decrypted status matches.
441 while (skb->len < len) {
446 if (TCP_SKB_CB(skb)->seq != seq)
448 if (skb_cmp_decrypted(first, skb))
455 static void tls_strp_load_anchor_with_queue(struct tls_strparser *strp, int len)
457 struct tcp_sock *tp = tcp_sk(strp->sk);
458 struct sk_buff *first;
461 first = tcp_recv_skb(strp->sk, tp->copied_seq, &offset);
462 if (WARN_ON_ONCE(!first))
465 /* Bestow the state onto the anchor */
466 strp->anchor->len = offset + len;
467 strp->anchor->data_len = offset + len;
468 strp->anchor->truesize = offset + len;
470 skb_shinfo(strp->anchor)->frag_list = first;
472 skb_copy_header(strp->anchor, first);
473 strp->anchor->destructor = NULL;
475 strp->stm.offset = offset;
478 void tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh)
480 struct strp_msg *rxm;
483 DEBUG_NET_WARN_ON_ONCE(!strp->msg_ready);
484 DEBUG_NET_WARN_ON_ONCE(!strp->stm.full_len);
486 if (!strp->copy_mode && force_refresh) {
487 if (WARN_ON(tcp_inq(strp->sk) < strp->stm.full_len))
490 tls_strp_load_anchor_with_queue(strp, strp->stm.full_len);
493 rxm = strp_msg(strp->anchor);
494 rxm->full_len = strp->stm.full_len;
495 rxm->offset = strp->stm.offset;
496 tlm = tls_msg(strp->anchor);
497 tlm->control = strp->mark;
500 /* Called with lock held on lower socket */
501 static int tls_strp_read_sock(struct tls_strparser *strp)
505 inq = tcp_inq(strp->sk);
509 if (unlikely(strp->copy_mode))
510 return tls_strp_read_copyin(strp);
512 if (inq < strp->stm.full_len)
513 return tls_strp_read_copy(strp, true);
515 if (!strp->stm.full_len) {
516 tls_strp_load_anchor_with_queue(strp, inq);
518 sz = tls_rx_msg_size(strp, strp->anchor);
520 tls_strp_abort_strp(strp, sz);
524 strp->stm.full_len = sz;
526 if (!strp->stm.full_len || inq < strp->stm.full_len)
527 return tls_strp_read_copy(strp, true);
530 if (!tls_strp_check_queue_ok(strp))
531 return tls_strp_read_copy(strp, false);
533 WRITE_ONCE(strp->msg_ready, 1);
534 tls_rx_msg_ready(strp);
539 void tls_strp_check_rcv(struct tls_strparser *strp)
541 if (unlikely(strp->stopped) || strp->msg_ready)
544 if (tls_strp_read_sock(strp) == -ENOMEM)
545 queue_work(tls_strp_wq, &strp->work);
548 /* Lower sock lock held */
549 void tls_strp_data_ready(struct tls_strparser *strp)
551 /* This check is needed to synchronize with do_tls_strp_work.
552 * do_tls_strp_work acquires a process lock (lock_sock) whereas
553 * the lock held here is bh_lock_sock. The two locks can be
554 * held by different threads at the same time, but bh_lock_sock
555 * allows a thread in BH context to safely check if the process
556 * lock is held. In this case, if the lock is held, queue work.
558 if (sock_owned_by_user_nocheck(strp->sk)) {
559 queue_work(tls_strp_wq, &strp->work);
563 tls_strp_check_rcv(strp);
566 static void tls_strp_work(struct work_struct *w)
568 struct tls_strparser *strp =
569 container_of(w, struct tls_strparser, work);
572 tls_strp_check_rcv(strp);
573 release_sock(strp->sk);
576 void tls_strp_msg_done(struct tls_strparser *strp)
578 WARN_ON(!strp->stm.full_len);
580 if (likely(!strp->copy_mode))
581 tcp_read_done(strp->sk, strp->stm.full_len);
583 tls_strp_flush_anchor_copy(strp);
585 WRITE_ONCE(strp->msg_ready, 0);
586 memset(&strp->stm, 0, sizeof(strp->stm));
588 tls_strp_check_rcv(strp);
591 void tls_strp_stop(struct tls_strparser *strp)
596 int tls_strp_init(struct tls_strparser *strp, struct sock *sk)
598 memset(strp, 0, sizeof(*strp));
602 strp->anchor = alloc_skb(0, GFP_KERNEL);
606 INIT_WORK(&strp->work, tls_strp_work);
611 /* strp must already be stopped so that tls_strp_recv will no longer be called.
612 * Note that tls_strp_done is not called with the lower socket held.
614 void tls_strp_done(struct tls_strparser *strp)
616 WARN_ON(!strp->stopped);
618 cancel_work_sync(&strp->work);
619 tls_strp_anchor_free(strp);
622 int __init tls_strp_dev_init(void)
624 tls_strp_wq = create_workqueue("tls-strp");
625 if (unlikely(!tls_strp_wq))
631 void tls_strp_dev_exit(void)
633 destroy_workqueue(tls_strp_wq);