1 // SPDX-License-Identifier: GPL-2.0-only
4 #include <linux/skbuff.h>
5 #include <linux/workqueue.h>
6 #include <net/strparser.h>
13 static struct workqueue_struct *tls_strp_wq;
15 static void tls_strp_abort_strp(struct tls_strparser *strp, int err)
22 /* Report an error on the lower socket */
23 strp->sk->sk_err = -err;
24 sk_error_report(strp->sk);
27 static void tls_strp_anchor_free(struct tls_strparser *strp)
29 struct skb_shared_info *shinfo = skb_shinfo(strp->anchor);
31 DEBUG_NET_WARN_ON_ONCE(atomic_read(&shinfo->dataref) != 1);
32 shinfo->frag_list = NULL;
33 consume_skb(strp->anchor);
37 /* Create a new skb with the contents of input copied to its page frags */
38 static struct sk_buff *tls_strp_msg_make_copy(struct tls_strparser *strp)
44 skb = alloc_skb_with_frags(0, strp->stm.full_len, TLS_PAGE_ORDER,
45 &err, strp->sk->sk_allocation);
49 offset = strp->stm.offset;
50 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
51 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
53 WARN_ON_ONCE(skb_copy_bits(strp->anchor, offset,
54 skb_frag_address(frag),
55 skb_frag_size(frag)));
56 offset += skb_frag_size(frag);
59 skb_copy_header(skb, strp->anchor);
65 /* Steal the input skb, input msg is invalid after calling this function */
66 struct sk_buff *tls_strp_msg_detach(struct tls_sw_context_rx *ctx)
68 struct tls_strparser *strp = &ctx->strp;
70 #ifdef CONFIG_TLS_DEVICE
71 DEBUG_NET_WARN_ON_ONCE(!strp->anchor->decrypted);
73 /* This function turns an input into an output,
74 * that can only happen if we have offload.
79 if (strp->copy_mode) {
82 /* Replace anchor with an empty skb, this is a little
83 * dangerous but __tls_cur_msg() warns on empty skbs
84 * so hopefully we'll catch abuses.
86 skb = alloc_skb(0, strp->sk->sk_allocation);
90 swap(strp->anchor, skb);
94 return tls_strp_msg_make_copy(strp);
97 /* Force the input skb to be in copy mode. The data ownership remains
98 * with the input skb itself (meaning unpause will wipe it) but it can
101 int tls_strp_msg_cow(struct tls_sw_context_rx *ctx)
103 struct tls_strparser *strp = &ctx->strp;
109 skb = tls_strp_msg_make_copy(strp);
113 tls_strp_anchor_free(strp);
116 tcp_read_done(strp->sk, strp->stm.full_len);
122 /* Make a clone (in the skb sense) of the input msg to keep a reference
123 * to the underlying data. The reference-holding skbs get placed on
126 int tls_strp_msg_hold(struct tls_strparser *strp, struct sk_buff_head *dst)
128 struct skb_shared_info *shinfo = skb_shinfo(strp->anchor);
130 if (strp->copy_mode) {
133 WARN_ON_ONCE(!shinfo->nr_frags);
135 /* We can't skb_clone() the anchor, it gets wiped by unpause */
136 skb = alloc_skb(0, strp->sk->sk_allocation);
140 __skb_queue_tail(dst, strp->anchor);
143 struct sk_buff *iter, *clone;
144 int chunk, len, offset;
146 offset = strp->stm.offset;
147 len = strp->stm.full_len;
148 iter = shinfo->frag_list;
151 if (iter->len <= offset) {
156 chunk = iter->len - offset;
159 clone = skb_clone(iter, strp->sk->sk_allocation);
162 __skb_queue_tail(dst, clone);
173 static void tls_strp_flush_anchor_copy(struct tls_strparser *strp)
175 struct skb_shared_info *shinfo = skb_shinfo(strp->anchor);
178 DEBUG_NET_WARN_ON_ONCE(atomic_read(&shinfo->dataref) != 1);
180 for (i = 0; i < shinfo->nr_frags; i++)
181 __skb_frag_unref(&shinfo->frags[i], false);
182 shinfo->nr_frags = 0;
186 static int tls_strp_copyin(read_descriptor_t *desc, struct sk_buff *in_skb,
187 unsigned int offset, size_t in_len)
189 struct tls_strparser *strp = (struct tls_strparser *)desc->arg.data;
199 frag = &skb_shinfo(skb)->frags[skb->len / PAGE_SIZE];
202 /* First make sure we got the header */
203 if (!strp->stm.full_len) {
204 /* Assume one page is more than enough for headers */
205 chunk = min_t(size_t, len, PAGE_SIZE - skb_frag_size(frag));
206 WARN_ON_ONCE(skb_copy_bits(in_skb, offset,
207 skb_frag_address(frag) +
211 sz = tls_rx_msg_size(strp, strp->anchor);
217 /* We may have over-read, sz == 0 is guaranteed under-read */
219 chunk = min_t(size_t, chunk, sz - skb->len);
222 skb->data_len += chunk;
223 skb_frag_size_add(frag, chunk);
228 strp->stm.full_len = sz;
229 if (!strp->stm.full_len)
233 /* Load up more data */
234 while (len && strp->stm.full_len > skb->len) {
235 chunk = min_t(size_t, len, strp->stm.full_len - skb->len);
236 chunk = min_t(size_t, chunk, PAGE_SIZE - skb_frag_size(frag));
237 WARN_ON_ONCE(skb_copy_bits(in_skb, offset,
238 skb_frag_address(frag) +
243 skb->data_len += chunk;
244 skb_frag_size_add(frag, chunk);
250 if (strp->stm.full_len == skb->len) {
254 tls_rx_msg_ready(strp);
261 static int tls_strp_read_copyin(struct tls_strparser *strp)
263 struct socket *sock = strp->sk->sk_socket;
264 read_descriptor_t desc;
266 desc.arg.data = strp;
268 desc.count = 1; /* give more than one skb per call */
270 /* sk should be locked here, so okay to do read_sock */
271 sock->ops->read_sock(strp->sk, &desc, tls_strp_copyin);
276 static int tls_strp_read_copy(struct tls_strparser *strp, bool qshort)
278 struct skb_shared_info *shinfo;
282 /* If the rbuf is small or rcv window has collapsed to 0 we need
283 * to read the data out. Otherwise the connection will stall.
284 * Without pressure threshold of INT_MAX will never be ready.
286 if (likely(qshort && !tcp_epollin_ready(strp->sk, INT_MAX)))
289 shinfo = skb_shinfo(strp->anchor);
290 shinfo->frag_list = NULL;
292 /* If we don't know the length go max plus page for cipher overhead */
293 need_spc = strp->stm.full_len ?: TLS_MAX_PAYLOAD_SIZE + PAGE_SIZE;
295 for (len = need_spc; len > 0; len -= PAGE_SIZE) {
296 page = alloc_page(strp->sk->sk_allocation);
298 tls_strp_flush_anchor_copy(strp);
302 skb_fill_page_desc(strp->anchor, shinfo->nr_frags++,
307 strp->stm.offset = 0;
309 strp->anchor->len = 0;
310 strp->anchor->data_len = 0;
311 strp->anchor->truesize = round_up(need_spc, PAGE_SIZE);
313 tls_strp_read_copyin(strp);
318 static bool tls_strp_check_no_dup(struct tls_strparser *strp)
320 unsigned int len = strp->stm.offset + strp->stm.full_len;
324 skb = skb_shinfo(strp->anchor)->frag_list;
325 seq = TCP_SKB_CB(skb)->seq;
327 while (skb->len < len) {
332 if (TCP_SKB_CB(skb)->seq != seq)
339 static void tls_strp_load_anchor_with_queue(struct tls_strparser *strp, int len)
341 struct tcp_sock *tp = tcp_sk(strp->sk);
342 struct sk_buff *first;
345 first = tcp_recv_skb(strp->sk, tp->copied_seq, &offset);
346 if (WARN_ON_ONCE(!first))
349 /* Bestow the state onto the anchor */
350 strp->anchor->len = offset + len;
351 strp->anchor->data_len = offset + len;
352 strp->anchor->truesize = offset + len;
354 skb_shinfo(strp->anchor)->frag_list = first;
356 skb_copy_header(strp->anchor, first);
357 strp->anchor->destructor = NULL;
359 strp->stm.offset = offset;
362 void tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh)
364 struct strp_msg *rxm;
367 DEBUG_NET_WARN_ON_ONCE(!strp->msg_ready);
368 DEBUG_NET_WARN_ON_ONCE(!strp->stm.full_len);
370 if (!strp->copy_mode && force_refresh) {
371 if (WARN_ON(tcp_inq(strp->sk) < strp->stm.full_len))
374 tls_strp_load_anchor_with_queue(strp, strp->stm.full_len);
377 rxm = strp_msg(strp->anchor);
378 rxm->full_len = strp->stm.full_len;
379 rxm->offset = strp->stm.offset;
380 tlm = tls_msg(strp->anchor);
381 tlm->control = strp->mark;
384 /* Called with lock held on lower socket */
385 static int tls_strp_read_sock(struct tls_strparser *strp)
389 inq = tcp_inq(strp->sk);
393 if (unlikely(strp->copy_mode))
394 return tls_strp_read_copyin(strp);
396 if (inq < strp->stm.full_len)
397 return tls_strp_read_copy(strp, true);
399 if (!strp->stm.full_len) {
400 tls_strp_load_anchor_with_queue(strp, inq);
402 sz = tls_rx_msg_size(strp, strp->anchor);
404 tls_strp_abort_strp(strp, sz);
408 strp->stm.full_len = sz;
410 if (!strp->stm.full_len || inq < strp->stm.full_len)
411 return tls_strp_read_copy(strp, true);
414 if (!tls_strp_check_no_dup(strp))
415 return tls_strp_read_copy(strp, false);
418 tls_rx_msg_ready(strp);
423 void tls_strp_check_rcv(struct tls_strparser *strp)
425 if (unlikely(strp->stopped) || strp->msg_ready)
428 if (tls_strp_read_sock(strp) == -ENOMEM)
429 queue_work(tls_strp_wq, &strp->work);
432 /* Lower sock lock held */
433 void tls_strp_data_ready(struct tls_strparser *strp)
435 /* This check is needed to synchronize with do_tls_strp_work.
436 * do_tls_strp_work acquires a process lock (lock_sock) whereas
437 * the lock held here is bh_lock_sock. The two locks can be
438 * held by different threads at the same time, but bh_lock_sock
439 * allows a thread in BH context to safely check if the process
440 * lock is held. In this case, if the lock is held, queue work.
442 if (sock_owned_by_user_nocheck(strp->sk)) {
443 queue_work(tls_strp_wq, &strp->work);
447 tls_strp_check_rcv(strp);
450 static void tls_strp_work(struct work_struct *w)
452 struct tls_strparser *strp =
453 container_of(w, struct tls_strparser, work);
456 tls_strp_check_rcv(strp);
457 release_sock(strp->sk);
460 void tls_strp_msg_done(struct tls_strparser *strp)
462 WARN_ON(!strp->stm.full_len);
464 if (likely(!strp->copy_mode))
465 tcp_read_done(strp->sk, strp->stm.full_len);
467 tls_strp_flush_anchor_copy(strp);
470 memset(&strp->stm, 0, sizeof(strp->stm));
472 tls_strp_check_rcv(strp);
475 void tls_strp_stop(struct tls_strparser *strp)
480 int tls_strp_init(struct tls_strparser *strp, struct sock *sk)
482 memset(strp, 0, sizeof(*strp));
486 strp->anchor = alloc_skb(0, GFP_KERNEL);
490 INIT_WORK(&strp->work, tls_strp_work);
495 /* strp must already be stopped so that tls_strp_recv will no longer be called.
496 * Note that tls_strp_done is not called with the lower socket held.
498 void tls_strp_done(struct tls_strparser *strp)
500 WARN_ON(!strp->stopped);
502 cancel_work_sync(&strp->work);
503 tls_strp_anchor_free(strp);
506 int __init tls_strp_dev_init(void)
508 tls_strp_wq = create_workqueue("tls-strp");
509 if (unlikely(!tls_strp_wq))
515 void tls_strp_dev_exit(void)
517 destroy_workqueue(tls_strp_wq);