tls: rx: strp: set the skb->len of detached / CoW'ed skbs
[linux-2.6-block.git] / net / tls / tls_strp.c
CommitLineData
c618db2a 1// SPDX-License-Identifier: GPL-2.0-only
84c61fe1 2/* Copyright (c) 2016 Tom Herbert <tom@herbertland.com> */
c618db2a
JK
3
4#include <linux/skbuff.h>
84c61fe1
JK
5#include <linux/workqueue.h>
6#include <net/strparser.h>
7#include <net/tcp.h>
8#include <net/sock.h>
9#include <net/tls.h>
c618db2a
JK
10
11#include "tls.h"
12
84c61fe1
JK
13static struct workqueue_struct *tls_strp_wq;
14
15static void tls_strp_abort_strp(struct tls_strparser *strp, int err)
16{
17 if (strp->stopped)
18 return;
19
20 strp->stopped = 1;
21
22 /* Report an error on the lower socket */
23 strp->sk->sk_err = -err;
24 sk_error_report(strp->sk);
25}
26
27static void tls_strp_anchor_free(struct tls_strparser *strp)
d4e5db64 28{
84c61fe1
JK
29 struct skb_shared_info *shinfo = skb_shinfo(strp->anchor);
30
31 DEBUG_NET_WARN_ON_ONCE(atomic_read(&shinfo->dataref) != 1);
32 shinfo->frag_list = NULL;
33 consume_skb(strp->anchor);
34 strp->anchor = NULL;
35}
36
37/* Create a new skb with the contents of input copied to its page frags */
38static struct sk_buff *tls_strp_msg_make_copy(struct tls_strparser *strp)
39{
40 struct strp_msg *rxm;
d4e5db64 41 struct sk_buff *skb;
84c61fe1
JK
42 int i, err, offset;
43
d800a7b3 44 skb = alloc_skb_with_frags(0, strp->stm.full_len, TLS_PAGE_ORDER,
84c61fe1
JK
45 &err, strp->sk->sk_allocation);
46 if (!skb)
47 return NULL;
48
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];
d4e5db64 52
84c61fe1
JK
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);
57 }
58
210620ae
JK
59 skb->len = strp->stm.full_len;
60 skb->data_len = strp->stm.full_len;
84c61fe1
JK
61 skb_copy_header(skb, strp->anchor);
62 rxm = strp_msg(skb);
63 rxm->offset = 0;
d4e5db64
JK
64 return skb;
65}
66
84c61fe1
JK
67/* Steal the input skb, input msg is invalid after calling this function */
68struct sk_buff *tls_strp_msg_detach(struct tls_sw_context_rx *ctx)
69{
70 struct tls_strparser *strp = &ctx->strp;
71
72#ifdef CONFIG_TLS_DEVICE
73 DEBUG_NET_WARN_ON_ONCE(!strp->anchor->decrypted);
74#else
75 /* This function turns an input into an output,
76 * that can only happen if we have offload.
77 */
78 WARN_ON(1);
79#endif
80
81 if (strp->copy_mode) {
82 struct sk_buff *skb;
83
84 /* Replace anchor with an empty skb, this is a little
85 * dangerous but __tls_cur_msg() warns on empty skbs
86 * so hopefully we'll catch abuses.
87 */
88 skb = alloc_skb(0, strp->sk->sk_allocation);
89 if (!skb)
90 return NULL;
91
92 swap(strp->anchor, skb);
93 return skb;
94 }
95
96 return tls_strp_msg_make_copy(strp);
97}
98
99/* Force the input skb to be in copy mode. The data ownership remains
100 * with the input skb itself (meaning unpause will wipe it) but it can
101 * be modified.
102 */
8b3c59a7
JK
103int tls_strp_msg_cow(struct tls_sw_context_rx *ctx)
104{
84c61fe1
JK
105 struct tls_strparser *strp = &ctx->strp;
106 struct sk_buff *skb;
107
108 if (strp->copy_mode)
109 return 0;
110
111 skb = tls_strp_msg_make_copy(strp);
112 if (!skb)
113 return -ENOMEM;
114
115 tls_strp_anchor_free(strp);
116 strp->anchor = skb;
117
118 tcp_read_done(strp->sk, strp->stm.full_len);
119 strp->copy_mode = 1;
120
121 return 0;
122}
123
124/* Make a clone (in the skb sense) of the input msg to keep a reference
125 * to the underlying data. The reference-holding skbs get placed on
126 * @dst.
127 */
128int tls_strp_msg_hold(struct tls_strparser *strp, struct sk_buff_head *dst)
129{
130 struct skb_shared_info *shinfo = skb_shinfo(strp->anchor);
131
132 if (strp->copy_mode) {
133 struct sk_buff *skb;
134
135 WARN_ON_ONCE(!shinfo->nr_frags);
136
137 /* We can't skb_clone() the anchor, it gets wiped by unpause */
138 skb = alloc_skb(0, strp->sk->sk_allocation);
139 if (!skb)
140 return -ENOMEM;
141
142 __skb_queue_tail(dst, strp->anchor);
143 strp->anchor = skb;
144 } else {
145 struct sk_buff *iter, *clone;
146 int chunk, len, offset;
147
148 offset = strp->stm.offset;
149 len = strp->stm.full_len;
150 iter = shinfo->frag_list;
151
152 while (len > 0) {
153 if (iter->len <= offset) {
154 offset -= iter->len;
155 goto next;
156 }
157
158 chunk = iter->len - offset;
159 offset = 0;
160
161 clone = skb_clone(iter, strp->sk->sk_allocation);
162 if (!clone)
163 return -ENOMEM;
164 __skb_queue_tail(dst, clone);
165
166 len -= chunk;
167next:
168 iter = iter->next;
169 }
170 }
171
172 return 0;
173}
174
175static void tls_strp_flush_anchor_copy(struct tls_strparser *strp)
176{
177 struct skb_shared_info *shinfo = skb_shinfo(strp->anchor);
178 int i;
179
180 DEBUG_NET_WARN_ON_ONCE(atomic_read(&shinfo->dataref) != 1);
181
182 for (i = 0; i < shinfo->nr_frags; i++)
183 __skb_frag_unref(&shinfo->frags[i], false);
184 shinfo->nr_frags = 0;
185 strp->copy_mode = 0;
186}
187
188static int tls_strp_copyin(read_descriptor_t *desc, struct sk_buff *in_skb,
189 unsigned int offset, size_t in_len)
190{
191 struct tls_strparser *strp = (struct tls_strparser *)desc->arg.data;
84c61fe1
JK
192 struct sk_buff *skb;
193 skb_frag_t *frag;
8fd1e151
YL
194 size_t len, chunk;
195 int sz;
84c61fe1
JK
196
197 if (strp->msg_ready)
198 return 0;
199
200 skb = strp->anchor;
201 frag = &skb_shinfo(skb)->frags[skb->len / PAGE_SIZE];
202
203 len = in_len;
204 /* First make sure we got the header */
205 if (!strp->stm.full_len) {
206 /* Assume one page is more than enough for headers */
207 chunk = min_t(size_t, len, PAGE_SIZE - skb_frag_size(frag));
208 WARN_ON_ONCE(skb_copy_bits(in_skb, offset,
209 skb_frag_address(frag) +
210 skb_frag_size(frag),
211 chunk));
212
213 sz = tls_rx_msg_size(strp, strp->anchor);
214 if (sz < 0) {
215 desc->error = sz;
216 return 0;
217 }
218
219 /* We may have over-read, sz == 0 is guaranteed under-read */
220 if (sz > 0)
221 chunk = min_t(size_t, chunk, sz - skb->len);
222
223 skb->len += chunk;
224 skb->data_len += chunk;
225 skb_frag_size_add(frag, chunk);
226 frag++;
227 len -= chunk;
228 offset += chunk;
229
230 strp->stm.full_len = sz;
231 if (!strp->stm.full_len)
232 goto read_done;
233 }
234
235 /* Load up more data */
236 while (len && strp->stm.full_len > skb->len) {
237 chunk = min_t(size_t, len, strp->stm.full_len - skb->len);
238 chunk = min_t(size_t, chunk, PAGE_SIZE - skb_frag_size(frag));
239 WARN_ON_ONCE(skb_copy_bits(in_skb, offset,
240 skb_frag_address(frag) +
241 skb_frag_size(frag),
242 chunk));
243
244 skb->len += chunk;
245 skb->data_len += chunk;
246 skb_frag_size_add(frag, chunk);
247 frag++;
248 len -= chunk;
249 offset += chunk;
250 }
251
252 if (strp->stm.full_len == skb->len) {
253 desc->count = 0;
254
255 strp->msg_ready = 1;
256 tls_rx_msg_ready(strp);
257 }
258
259read_done:
260 return in_len - len;
261}
262
263static int tls_strp_read_copyin(struct tls_strparser *strp)
264{
265 struct socket *sock = strp->sk->sk_socket;
266 read_descriptor_t desc;
267
268 desc.arg.data = strp;
269 desc.error = 0;
270 desc.count = 1; /* give more than one skb per call */
271
272 /* sk should be locked here, so okay to do read_sock */
273 sock->ops->read_sock(strp->sk, &desc, tls_strp_copyin);
274
275 return desc.error;
276}
277
0d87bbd3 278static int tls_strp_read_copy(struct tls_strparser *strp, bool qshort)
84c61fe1
JK
279{
280 struct skb_shared_info *shinfo;
281 struct page *page;
282 int need_spc, len;
283
284 /* If the rbuf is small or rcv window has collapsed to 0 we need
285 * to read the data out. Otherwise the connection will stall.
286 * Without pressure threshold of INT_MAX will never be ready.
287 */
0d87bbd3 288 if (likely(qshort && !tcp_epollin_ready(strp->sk, INT_MAX)))
84c61fe1
JK
289 return 0;
290
291 shinfo = skb_shinfo(strp->anchor);
292 shinfo->frag_list = NULL;
293
294 /* If we don't know the length go max plus page for cipher overhead */
295 need_spc = strp->stm.full_len ?: TLS_MAX_PAYLOAD_SIZE + PAGE_SIZE;
296
297 for (len = need_spc; len > 0; len -= PAGE_SIZE) {
298 page = alloc_page(strp->sk->sk_allocation);
299 if (!page) {
300 tls_strp_flush_anchor_copy(strp);
301 return -ENOMEM;
302 }
303
304 skb_fill_page_desc(strp->anchor, shinfo->nr_frags++,
305 page, 0, 0);
306 }
307
308 strp->copy_mode = 1;
309 strp->stm.offset = 0;
310
311 strp->anchor->len = 0;
312 strp->anchor->data_len = 0;
313 strp->anchor->truesize = round_up(need_spc, PAGE_SIZE);
314
315 tls_strp_read_copyin(strp);
316
317 return 0;
318}
319
0d87bbd3
JK
320static bool tls_strp_check_no_dup(struct tls_strparser *strp)
321{
322 unsigned int len = strp->stm.offset + strp->stm.full_len;
323 struct sk_buff *skb;
324 u32 seq;
325
326 skb = skb_shinfo(strp->anchor)->frag_list;
327 seq = TCP_SKB_CB(skb)->seq;
328
329 while (skb->len < len) {
330 seq += skb->len;
331 len -= skb->len;
332 skb = skb->next;
333
334 if (TCP_SKB_CB(skb)->seq != seq)
335 return false;
336 }
337
338 return true;
339}
340
84c61fe1
JK
341static void tls_strp_load_anchor_with_queue(struct tls_strparser *strp, int len)
342{
343 struct tcp_sock *tp = tcp_sk(strp->sk);
344 struct sk_buff *first;
345 u32 offset;
346
347 first = tcp_recv_skb(strp->sk, tp->copied_seq, &offset);
348 if (WARN_ON_ONCE(!first))
349 return;
350
351 /* Bestow the state onto the anchor */
352 strp->anchor->len = offset + len;
353 strp->anchor->data_len = offset + len;
354 strp->anchor->truesize = offset + len;
355
356 skb_shinfo(strp->anchor)->frag_list = first;
357
358 skb_copy_header(strp->anchor, first);
359 strp->anchor->destructor = NULL;
360
361 strp->stm.offset = offset;
362}
363
364void tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh)
365{
366 struct strp_msg *rxm;
367 struct tls_msg *tlm;
368
369 DEBUG_NET_WARN_ON_ONCE(!strp->msg_ready);
370 DEBUG_NET_WARN_ON_ONCE(!strp->stm.full_len);
371
372 if (!strp->copy_mode && force_refresh) {
373 if (WARN_ON(tcp_inq(strp->sk) < strp->stm.full_len))
374 return;
375
376 tls_strp_load_anchor_with_queue(strp, strp->stm.full_len);
377 }
378
379 rxm = strp_msg(strp->anchor);
380 rxm->full_len = strp->stm.full_len;
381 rxm->offset = strp->stm.offset;
382 tlm = tls_msg(strp->anchor);
383 tlm->control = strp->mark;
384}
385
386/* Called with lock held on lower socket */
387static int tls_strp_read_sock(struct tls_strparser *strp)
388{
389 int sz, inq;
390
391 inq = tcp_inq(strp->sk);
392 if (inq < 1)
393 return 0;
394
395 if (unlikely(strp->copy_mode))
396 return tls_strp_read_copyin(strp);
397
398 if (inq < strp->stm.full_len)
0d87bbd3 399 return tls_strp_read_copy(strp, true);
84c61fe1
JK
400
401 if (!strp->stm.full_len) {
402 tls_strp_load_anchor_with_queue(strp, inq);
403
404 sz = tls_rx_msg_size(strp, strp->anchor);
405 if (sz < 0) {
406 tls_strp_abort_strp(strp, sz);
407 return sz;
408 }
409
410 strp->stm.full_len = sz;
411
412 if (!strp->stm.full_len || inq < strp->stm.full_len)
0d87bbd3 413 return tls_strp_read_copy(strp, true);
84c61fe1
JK
414 }
415
0d87bbd3
JK
416 if (!tls_strp_check_no_dup(strp))
417 return tls_strp_read_copy(strp, false);
418
84c61fe1
JK
419 strp->msg_ready = 1;
420 tls_rx_msg_ready(strp);
421
422 return 0;
423}
424
425void tls_strp_check_rcv(struct tls_strparser *strp)
426{
427 if (unlikely(strp->stopped) || strp->msg_ready)
428 return;
429
430 if (tls_strp_read_sock(strp) == -ENOMEM)
431 queue_work(tls_strp_wq, &strp->work);
432}
433
434/* Lower sock lock held */
435void tls_strp_data_ready(struct tls_strparser *strp)
436{
437 /* This check is needed to synchronize with do_tls_strp_work.
438 * do_tls_strp_work acquires a process lock (lock_sock) whereas
439 * the lock held here is bh_lock_sock. The two locks can be
440 * held by different threads at the same time, but bh_lock_sock
441 * allows a thread in BH context to safely check if the process
442 * lock is held. In this case, if the lock is held, queue work.
443 */
444 if (sock_owned_by_user_nocheck(strp->sk)) {
445 queue_work(tls_strp_wq, &strp->work);
446 return;
447 }
448
449 tls_strp_check_rcv(strp);
450}
451
452static void tls_strp_work(struct work_struct *w)
453{
454 struct tls_strparser *strp =
455 container_of(w, struct tls_strparser, work);
456
457 lock_sock(strp->sk);
458 tls_strp_check_rcv(strp);
459 release_sock(strp->sk);
460}
461
462void tls_strp_msg_done(struct tls_strparser *strp)
463{
464 WARN_ON(!strp->stm.full_len);
465
466 if (likely(!strp->copy_mode))
467 tcp_read_done(strp->sk, strp->stm.full_len);
468 else
469 tls_strp_flush_anchor_copy(strp);
470
471 strp->msg_ready = 0;
472 memset(&strp->stm, 0, sizeof(strp->stm));
473
474 tls_strp_check_rcv(strp);
475}
476
477void tls_strp_stop(struct tls_strparser *strp)
478{
479 strp->stopped = 1;
480}
481
482int tls_strp_init(struct tls_strparser *strp, struct sock *sk)
483{
484 memset(strp, 0, sizeof(*strp));
485
486 strp->sk = sk;
487
488 strp->anchor = alloc_skb(0, GFP_KERNEL);
489 if (!strp->anchor)
490 return -ENOMEM;
491
492 INIT_WORK(&strp->work, tls_strp_work);
8b3c59a7 493
8b3c59a7
JK
494 return 0;
495}
496
84c61fe1
JK
497/* strp must already be stopped so that tls_strp_recv will no longer be called.
498 * Note that tls_strp_done is not called with the lower socket held.
499 */
500void tls_strp_done(struct tls_strparser *strp)
c618db2a 501{
84c61fe1 502 WARN_ON(!strp->stopped);
c618db2a 503
84c61fe1
JK
504 cancel_work_sync(&strp->work);
505 tls_strp_anchor_free(strp);
506}
507
508int __init tls_strp_dev_init(void)
509{
d11ef9cc 510 tls_strp_wq = create_workqueue("tls-strp");
84c61fe1 511 if (unlikely(!tls_strp_wq))
c618db2a 512 return -ENOMEM;
84c61fe1 513
c618db2a
JK
514 return 0;
515}
84c61fe1
JK
516
517void tls_strp_dev_exit(void)
518{
519 destroy_workqueue(tls_strp_wq);
520}