@@ -29,34 +29,50 @@ static void tls_strp_anchor_free(struct tls_strparser *strp)
29
29
struct skb_shared_info * shinfo = skb_shinfo (strp -> anchor );
30
30
31
31
DEBUG_NET_WARN_ON_ONCE (atomic_read (& shinfo -> dataref ) != 1 );
32
- shinfo -> frag_list = NULL ;
32
+ if (!strp -> copy_mode )
33
+ shinfo -> frag_list = NULL ;
33
34
consume_skb (strp -> anchor );
34
35
strp -> anchor = NULL ;
35
36
}
36
37
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 )
38
+ static struct sk_buff *
39
+ tls_strp_skb_copy (struct tls_strparser * strp , struct sk_buff * in_skb ,
40
+ int offset , int len )
39
41
{
40
- struct strp_msg * rxm ;
41
42
struct sk_buff * skb ;
42
- int i , err , offset ;
43
+ int i , err ;
43
44
44
- skb = alloc_skb_with_frags (0 , strp -> stm . full_len , TLS_PAGE_ORDER ,
45
+ skb = alloc_skb_with_frags (0 , len , TLS_PAGE_ORDER ,
45
46
& err , strp -> sk -> sk_allocation );
46
47
if (!skb )
47
48
return NULL ;
48
49
49
- offset = strp -> stm .offset ;
50
50
for (i = 0 ; i < skb_shinfo (skb )-> nr_frags ; i ++ ) {
51
51
skb_frag_t * frag = & skb_shinfo (skb )-> frags [i ];
52
52
53
- WARN_ON_ONCE (skb_copy_bits (strp -> anchor , offset ,
53
+ WARN_ON_ONCE (skb_copy_bits (in_skb , offset ,
54
54
skb_frag_address (frag ),
55
55
skb_frag_size (frag )));
56
56
offset += skb_frag_size (frag );
57
57
}
58
58
59
- skb_copy_header (skb , strp -> anchor );
59
+ skb -> len = len ;
60
+ skb -> data_len = len ;
61
+ skb_copy_header (skb , in_skb );
62
+ return skb ;
63
+ }
64
+
65
+ /* Create a new skb with the contents of input copied to its page frags */
66
+ static struct sk_buff * tls_strp_msg_make_copy (struct tls_strparser * strp )
67
+ {
68
+ struct strp_msg * rxm ;
69
+ struct sk_buff * skb ;
70
+
71
+ skb = tls_strp_skb_copy (strp , strp -> anchor , strp -> stm .offset ,
72
+ strp -> stm .full_len );
73
+ if (!skb )
74
+ return NULL ;
75
+
60
76
rxm = strp_msg (skb );
61
77
rxm -> offset = 0 ;
62
78
return skb ;
@@ -180,22 +196,22 @@ static void tls_strp_flush_anchor_copy(struct tls_strparser *strp)
180
196
for (i = 0 ; i < shinfo -> nr_frags ; i ++ )
181
197
__skb_frag_unref (& shinfo -> frags [i ], false);
182
198
shinfo -> nr_frags = 0 ;
199
+ if (strp -> copy_mode ) {
200
+ kfree_skb_list (shinfo -> frag_list );
201
+ shinfo -> frag_list = NULL ;
202
+ }
183
203
strp -> copy_mode = 0 ;
204
+ strp -> mixed_decrypted = 0 ;
184
205
}
185
206
186
- static int tls_strp_copyin (read_descriptor_t * desc , struct sk_buff * in_skb ,
187
- unsigned int offset , size_t in_len )
207
+ static int tls_strp_copyin_frag (struct tls_strparser * strp , struct sk_buff * skb ,
208
+ struct sk_buff * in_skb , unsigned int offset ,
209
+ size_t in_len )
188
210
{
189
- struct tls_strparser * strp = (struct tls_strparser * )desc -> arg .data ;
190
- struct sk_buff * skb ;
191
- skb_frag_t * frag ;
192
211
size_t len , chunk ;
212
+ skb_frag_t * frag ;
193
213
int sz ;
194
214
195
- if (strp -> msg_ready )
196
- return 0 ;
197
-
198
- skb = strp -> anchor ;
199
215
frag = & skb_shinfo (skb )-> frags [skb -> len / PAGE_SIZE ];
200
216
201
217
len = in_len ;
@@ -208,19 +224,26 @@ static int tls_strp_copyin(read_descriptor_t *desc, struct sk_buff *in_skb,
208
224
skb_frag_size (frag ),
209
225
chunk ));
210
226
211
- sz = tls_rx_msg_size (strp , strp -> anchor );
212
- if (sz < 0 ) {
213
- desc -> error = sz ;
214
- return 0 ;
215
- }
216
-
217
- /* We may have over-read, sz == 0 is guaranteed under-read */
218
- if (sz > 0 )
219
- chunk = min_t (size_t , chunk , sz - skb -> len );
220
-
221
227
skb -> len += chunk ;
222
228
skb -> data_len += chunk ;
223
229
skb_frag_size_add (frag , chunk );
230
+
231
+ sz = tls_rx_msg_size (strp , skb );
232
+ if (sz < 0 )
233
+ return sz ;
234
+
235
+ /* We may have over-read, sz == 0 is guaranteed under-read */
236
+ if (unlikely (sz && sz < skb -> len )) {
237
+ int over = skb -> len - sz ;
238
+
239
+ WARN_ON_ONCE (over > chunk );
240
+ skb -> len -= over ;
241
+ skb -> data_len -= over ;
242
+ skb_frag_size_add (frag , - over );
243
+
244
+ chunk -= over ;
245
+ }
246
+
224
247
frag ++ ;
225
248
len -= chunk ;
226
249
offset += chunk ;
@@ -247,15 +270,99 @@ static int tls_strp_copyin(read_descriptor_t *desc, struct sk_buff *in_skb,
247
270
offset += chunk ;
248
271
}
249
272
250
- if (strp -> stm .full_len == skb -> len ) {
273
+ read_done :
274
+ return in_len - len ;
275
+ }
276
+
277
+ static int tls_strp_copyin_skb (struct tls_strparser * strp , struct sk_buff * skb ,
278
+ struct sk_buff * in_skb , unsigned int offset ,
279
+ size_t in_len )
280
+ {
281
+ struct sk_buff * nskb , * first , * last ;
282
+ struct skb_shared_info * shinfo ;
283
+ size_t chunk ;
284
+ int sz ;
285
+
286
+ if (strp -> stm .full_len )
287
+ chunk = strp -> stm .full_len - skb -> len ;
288
+ else
289
+ chunk = TLS_MAX_PAYLOAD_SIZE + PAGE_SIZE ;
290
+ chunk = min (chunk , in_len );
291
+
292
+ nskb = tls_strp_skb_copy (strp , in_skb , offset , chunk );
293
+ if (!nskb )
294
+ return - ENOMEM ;
295
+
296
+ shinfo = skb_shinfo (skb );
297
+ if (!shinfo -> frag_list ) {
298
+ shinfo -> frag_list = nskb ;
299
+ nskb -> prev = nskb ;
300
+ } else {
301
+ first = shinfo -> frag_list ;
302
+ last = first -> prev ;
303
+ last -> next = nskb ;
304
+ first -> prev = nskb ;
305
+ }
306
+
307
+ skb -> len += chunk ;
308
+ skb -> data_len += chunk ;
309
+
310
+ if (!strp -> stm .full_len ) {
311
+ sz = tls_rx_msg_size (strp , skb );
312
+ if (sz < 0 )
313
+ return sz ;
314
+
315
+ /* We may have over-read, sz == 0 is guaranteed under-read */
316
+ if (unlikely (sz && sz < skb -> len )) {
317
+ int over = skb -> len - sz ;
318
+
319
+ WARN_ON_ONCE (over > chunk );
320
+ skb -> len -= over ;
321
+ skb -> data_len -= over ;
322
+ __pskb_trim (nskb , nskb -> len - over );
323
+
324
+ chunk -= over ;
325
+ }
326
+
327
+ strp -> stm .full_len = sz ;
328
+ }
329
+
330
+ return chunk ;
331
+ }
332
+
333
+ static int tls_strp_copyin (read_descriptor_t * desc , struct sk_buff * in_skb ,
334
+ unsigned int offset , size_t in_len )
335
+ {
336
+ struct tls_strparser * strp = (struct tls_strparser * )desc -> arg .data ;
337
+ struct sk_buff * skb ;
338
+ int ret ;
339
+
340
+ if (strp -> msg_ready )
341
+ return 0 ;
342
+
343
+ skb = strp -> anchor ;
344
+ if (!skb -> len )
345
+ skb_copy_decrypted (skb , in_skb );
346
+ else
347
+ strp -> mixed_decrypted |= !!skb_cmp_decrypted (skb , in_skb );
348
+
349
+ if (IS_ENABLED (CONFIG_TLS_DEVICE ) && strp -> mixed_decrypted )
350
+ ret = tls_strp_copyin_skb (strp , skb , in_skb , offset , in_len );
351
+ else
352
+ ret = tls_strp_copyin_frag (strp , skb , in_skb , offset , in_len );
353
+ if (ret < 0 ) {
354
+ desc -> error = ret ;
355
+ ret = 0 ;
356
+ }
357
+
358
+ if (strp -> stm .full_len && strp -> stm .full_len == skb -> len ) {
251
359
desc -> count = 0 ;
252
360
253
361
strp -> msg_ready = 1 ;
254
362
tls_rx_msg_ready (strp );
255
363
}
256
364
257
- read_done :
258
- return in_len - len ;
365
+ return ret ;
259
366
}
260
367
261
368
static int tls_strp_read_copyin (struct tls_strparser * strp )
@@ -315,22 +422,28 @@ static int tls_strp_read_copy(struct tls_strparser *strp, bool qshort)
315
422
return 0 ;
316
423
}
317
424
318
- static bool tls_strp_check_no_dup (struct tls_strparser * strp )
425
+ static bool tls_strp_check_queue_ok (struct tls_strparser * strp )
319
426
{
320
427
unsigned int len = strp -> stm .offset + strp -> stm .full_len ;
321
- struct sk_buff * skb ;
428
+ struct sk_buff * first , * skb ;
322
429
u32 seq ;
323
430
324
- skb = skb_shinfo (strp -> anchor )-> frag_list ;
325
- seq = TCP_SKB_CB (skb )-> seq ;
431
+ first = skb_shinfo (strp -> anchor )-> frag_list ;
432
+ skb = first ;
433
+ seq = TCP_SKB_CB (first )-> seq ;
326
434
435
+ /* Make sure there's no duplicate data in the queue,
436
+ * and the decrypted status matches.
437
+ */
327
438
while (skb -> len < len ) {
328
439
seq += skb -> len ;
329
440
len -= skb -> len ;
330
441
skb = skb -> next ;
331
442
332
443
if (TCP_SKB_CB (skb )-> seq != seq )
333
444
return false;
445
+ if (skb_cmp_decrypted (first , skb ))
446
+ return false;
334
447
}
335
448
336
449
return true;
@@ -411,7 +524,7 @@ static int tls_strp_read_sock(struct tls_strparser *strp)
411
524
return tls_strp_read_copy (strp , true);
412
525
}
413
526
414
- if (!tls_strp_check_no_dup (strp ))
527
+ if (!tls_strp_check_queue_ok (strp ))
415
528
return tls_strp_read_copy (strp , false);
416
529
417
530
strp -> msg_ready = 1 ;
0 commit comments