]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/strparser/strparser.c
rxrpc: Fix overlapping ACK accounting
[mirror_ubuntu-jammy-kernel.git] / net / strparser / strparser.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
43a0c675
TH
2/*
3 * Stream Parser
4 *
5 * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
43a0c675
TH
6 */
7
8#include <linux/bpf.h>
9#include <linux/errno.h>
10#include <linux/errqueue.h>
11#include <linux/file.h>
12#include <linux/in.h>
13#include <linux/kernel.h>
15253b4a
PG
14#include <linux/export.h>
15#include <linux/init.h>
43a0c675
TH
16#include <linux/net.h>
17#include <linux/netdevice.h>
18#include <linux/poll.h>
19#include <linux/rculist.h>
20#include <linux/skbuff.h>
21#include <linux/socket.h>
22#include <linux/uaccess.h>
23#include <linux/workqueue.h>
24#include <net/strparser.h>
25#include <net/netns/generic.h>
26#include <net/sock.h>
43a0c675
TH
27
28static struct workqueue_struct *strp_wq;
29
bbb03029 30static inline struct _strp_msg *_strp_msg(struct sk_buff *skb)
43a0c675 31{
bbb03029 32 return (struct _strp_msg *)((void *)skb->cb +
b0e43904 33 offsetof(struct sk_skb_cb, strp));
43a0c675
TH
34}
35
36/* Lower lock held */
bbb03029 37static void strp_abort_strp(struct strparser *strp, int err)
43a0c675 38{
43a0c675
TH
39 /* Unrecoverable error in receive */
40
829385f0 41 cancel_delayed_work(&strp->msg_timer_work);
43a0c675 42
bbb03029 43 if (strp->stopped)
43a0c675
TH
44 return;
45
bbb03029
TH
46 strp->stopped = 1;
47
48 if (strp->sk) {
49 struct sock *sk = strp->sk;
43a0c675 50
bbb03029 51 /* Report an error on the lower socket */
cd00edc1 52 sk->sk_err = -err;
e3ae2365 53 sk_error_report(sk);
bbb03029 54 }
43a0c675
TH
55}
56
bbb03029 57static void strp_start_timer(struct strparser *strp, long timeo)
43a0c675 58{
7c5aba21 59 if (timeo && timeo != LONG_MAX)
829385f0 60 mod_delayed_work(strp_wq, &strp->msg_timer_work, timeo);
43a0c675
TH
61}
62
63/* Lower lock held */
64static void strp_parser_err(struct strparser *strp, int err,
65 read_descriptor_t *desc)
66{
67 desc->error = err;
bbb03029
TH
68 kfree_skb(strp->skb_head);
69 strp->skb_head = NULL;
43a0c675
TH
70 strp->cb.abort_parser(strp, err);
71}
72
96a59083
TH
73static inline int strp_peek_len(struct strparser *strp)
74{
bbb03029
TH
75 if (strp->sk) {
76 struct socket *sock = strp->sk->sk_socket;
77
78 return sock->ops->peek_len(sock);
79 }
80
81 /* If we don't have an associated socket there's nothing to peek.
82 * Return int max to avoid stopping the strparser.
83 */
96a59083 84
bbb03029 85 return INT_MAX;
96a59083
TH
86}
87
43a0c675 88/* Lower socket lock held */
bbb03029
TH
89static int __strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
90 unsigned int orig_offset, size_t orig_len,
91 size_t max_msg_size, long timeo)
43a0c675
TH
92{
93 struct strparser *strp = (struct strparser *)desc->arg.data;
bbb03029 94 struct _strp_msg *stm;
43a0c675
TH
95 struct sk_buff *head, *skb;
96 size_t eaten = 0, cand_len;
97 ssize_t extra;
98 int err;
99 bool cloned_orig = false;
100
bbb03029 101 if (strp->paused)
43a0c675
TH
102 return 0;
103
bbb03029 104 head = strp->skb_head;
43a0c675
TH
105 if (head) {
106 /* Message already in progress */
43a0c675
TH
107 if (unlikely(orig_offset)) {
108 /* Getting data with a non-zero offset when a message is
109 * in progress is not expected. If it does happen, we
110 * need to clone and pull since we can't deal with
111 * offsets in the skbs for a message expect in the head.
112 */
113 orig_skb = skb_clone(orig_skb, GFP_ATOMIC);
114 if (!orig_skb) {
bbb03029 115 STRP_STATS_INCR(strp->stats.mem_fail);
43a0c675
TH
116 desc->error = -ENOMEM;
117 return 0;
118 }
119 if (!pskb_pull(orig_skb, orig_offset)) {
bbb03029 120 STRP_STATS_INCR(strp->stats.mem_fail);
43a0c675
TH
121 kfree_skb(orig_skb);
122 desc->error = -ENOMEM;
123 return 0;
124 }
125 cloned_orig = true;
126 orig_offset = 0;
127 }
128
bbb03029 129 if (!strp->skb_nextp) {
43a0c675
TH
130 /* We are going to append to the frags_list of head.
131 * Need to unshare the frag_list.
132 */
4a9c2e37
JK
133 err = skb_unclone(head, GFP_ATOMIC);
134 if (err) {
135 STRP_STATS_INCR(strp->stats.mem_fail);
136 desc->error = err;
137 return 0;
43a0c675
TH
138 }
139
140 if (unlikely(skb_shinfo(head)->frag_list)) {
141 /* We can't append to an sk_buff that already
142 * has a frag_list. We create a new head, point
143 * the frag_list of that to the old head, and
144 * then are able to use the old head->next for
145 * appending to the message.
146 */
147 if (WARN_ON(head->next)) {
148 desc->error = -EINVAL;
149 return 0;
150 }
151
da29e4b4 152 skb = alloc_skb_for_msg(head);
43a0c675 153 if (!skb) {
bbb03029 154 STRP_STATS_INCR(strp->stats.mem_fail);
43a0c675
TH
155 desc->error = -ENOMEM;
156 return 0;
157 }
da29e4b4 158
bbb03029 159 strp->skb_nextp = &head->next;
bbb03029 160 strp->skb_head = skb;
43a0c675
TH
161 head = skb;
162 } else {
bbb03029 163 strp->skb_nextp =
43a0c675
TH
164 &skb_shinfo(head)->frag_list;
165 }
166 }
167 }
168
169 while (eaten < orig_len) {
170 /* Always clone since we will consume something */
171 skb = skb_clone(orig_skb, GFP_ATOMIC);
172 if (!skb) {
bbb03029 173 STRP_STATS_INCR(strp->stats.mem_fail);
43a0c675
TH
174 desc->error = -ENOMEM;
175 break;
176 }
177
178 cand_len = orig_len - eaten;
179
bbb03029 180 head = strp->skb_head;
43a0c675
TH
181 if (!head) {
182 head = skb;
bbb03029
TH
183 strp->skb_head = head;
184 /* Will set skb_nextp on next packet if needed */
185 strp->skb_nextp = NULL;
186 stm = _strp_msg(head);
187 memset(stm, 0, sizeof(*stm));
188 stm->strp.offset = orig_offset + eaten;
43a0c675 189 } else {
4e485d06 190 /* Unclone if we are appending to an skb that we
43a0c675
TH
191 * already share a frag_list with.
192 */
4e485d06
VG
193 if (skb_has_frag_list(skb)) {
194 err = skb_unclone(skb, GFP_ATOMIC);
195 if (err) {
196 STRP_STATS_INCR(strp->stats.mem_fail);
197 desc->error = err;
198 break;
199 }
43a0c675
TH
200 }
201
bbb03029
TH
202 stm = _strp_msg(head);
203 *strp->skb_nextp = skb;
204 strp->skb_nextp = &skb->next;
43a0c675
TH
205 head->data_len += skb->len;
206 head->len += skb->len;
207 head->truesize += skb->truesize;
208 }
209
bbb03029 210 if (!stm->strp.full_len) {
43a0c675
TH
211 ssize_t len;
212
213 len = (*strp->cb.parse_msg)(strp, head);
214
215 if (!len) {
216 /* Need more header to determine length */
bbb03029 217 if (!stm->accum_len) {
43a0c675 218 /* Start RX timer for new message */
bbb03029 219 strp_start_timer(strp, timeo);
43a0c675 220 }
bbb03029 221 stm->accum_len += cand_len;
43a0c675 222 eaten += cand_len;
bbb03029 223 STRP_STATS_INCR(strp->stats.need_more_hdr);
43a0c675
TH
224 WARN_ON(eaten != orig_len);
225 break;
226 } else if (len < 0) {
bbb03029 227 if (len == -ESTRPIPE && stm->accum_len) {
43a0c675 228 len = -ENODATA;
bbb03029 229 strp->unrecov_intr = 1;
43a0c675 230 } else {
bbb03029 231 strp->interrupted = 1;
43a0c675 232 }
6d3a4c40 233 strp_parser_err(strp, len, desc);
43a0c675 234 break;
bbb03029 235 } else if (len > max_msg_size) {
43a0c675 236 /* Message length exceeds maximum allowed */
bbb03029 237 STRP_STATS_INCR(strp->stats.msg_too_big);
43a0c675
TH
238 strp_parser_err(strp, -EMSGSIZE, desc);
239 break;
240 } else if (len <= (ssize_t)head->len -
bbb03029 241 skb->len - stm->strp.offset) {
43a0c675
TH
242 /* Length must be into new skb (and also
243 * greater than zero)
244 */
bbb03029 245 STRP_STATS_INCR(strp->stats.bad_hdr_len);
43a0c675
TH
246 strp_parser_err(strp, -EPROTO, desc);
247 break;
248 }
249
bbb03029 250 stm->strp.full_len = len;
43a0c675
TH
251 }
252
bbb03029
TH
253 extra = (ssize_t)(stm->accum_len + cand_len) -
254 stm->strp.full_len;
43a0c675
TH
255
256 if (extra < 0) {
257 /* Message not complete yet. */
bbb03029 258 if (stm->strp.full_len - stm->accum_len >
96a59083 259 strp_peek_len(strp)) {
bbb03029
TH
260 /* Don't have the whole message in the socket
261 * buffer. Set strp->need_bytes to wait for
43a0c675
TH
262 * the rest of the message. Also, set "early
263 * eaten" since we've already buffered the skb
96a59083 264 * but don't consume yet per strp_read_sock.
43a0c675
TH
265 */
266
bbb03029 267 if (!stm->accum_len) {
43a0c675 268 /* Start RX timer for new message */
bbb03029 269 strp_start_timer(strp, timeo);
43a0c675
TH
270 }
271
9d0c75bf 272 stm->accum_len += cand_len;
977c7114 273 eaten += cand_len;
bbb03029
TH
274 strp->need_bytes = stm->strp.full_len -
275 stm->accum_len;
bbb03029 276 STRP_STATS_ADD(strp->stats.bytes, cand_len);
43a0c675
TH
277 desc->count = 0; /* Stop reading socket */
278 break;
279 }
bbb03029 280 stm->accum_len += cand_len;
43a0c675
TH
281 eaten += cand_len;
282 WARN_ON(eaten != orig_len);
283 break;
284 }
285
93e21254 286 /* Positive extra indicates more bytes than needed for the
43a0c675
TH
287 * message
288 */
289
290 WARN_ON(extra > cand_len);
291
292 eaten += (cand_len - extra);
293
294 /* Hurray, we have a new message! */
829385f0 295 cancel_delayed_work(&strp->msg_timer_work);
bbb03029 296 strp->skb_head = NULL;
9d0c75bf 297 strp->need_bytes = 0;
bbb03029 298 STRP_STATS_INCR(strp->stats.msgs);
43a0c675
TH
299
300 /* Give skb to upper layer */
301 strp->cb.rcv_msg(strp, head);
302
bbb03029 303 if (unlikely(strp->paused)) {
43a0c675
TH
304 /* Upper layer paused strp */
305 break;
306 }
307 }
308
309 if (cloned_orig)
310 kfree_skb(orig_skb);
311
bbb03029 312 STRP_STATS_ADD(strp->stats.bytes, eaten);
43a0c675
TH
313
314 return eaten;
315}
316
bbb03029
TH
317int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
318 unsigned int orig_offset, size_t orig_len,
319 size_t max_msg_size, long timeo)
320{
321 read_descriptor_t desc; /* Dummy arg to strp_recv */
322
323 desc.arg.data = strp;
324
325 return __strp_recv(&desc, orig_skb, orig_offset, orig_len,
326 max_msg_size, timeo);
327}
328EXPORT_SYMBOL_GPL(strp_process);
329
330static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
331 unsigned int orig_offset, size_t orig_len)
332{
333 struct strparser *strp = (struct strparser *)desc->arg.data;
334
335 return __strp_recv(desc, orig_skb, orig_offset, orig_len,
336 strp->sk->sk_rcvbuf, strp->sk->sk_rcvtimeo);
337}
338
43a0c675
TH
339static int default_read_sock_done(struct strparser *strp, int err)
340{
341 return err;
342}
343
344/* Called with lock held on lower socket */
96a59083 345static int strp_read_sock(struct strparser *strp)
43a0c675 346{
96a59083 347 struct socket *sock = strp->sk->sk_socket;
43a0c675
TH
348 read_descriptor_t desc;
349
f26de110
JF
350 if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
351 return -EBUSY;
352
43a0c675
TH
353 desc.arg.data = strp;
354 desc.error = 0;
355 desc.count = 1; /* give more than one skb per call */
356
96a59083
TH
357 /* sk should be locked here, so okay to do read_sock */
358 sock->ops->read_sock(strp->sk, &desc, strp_recv);
43a0c675
TH
359
360 desc.error = strp->cb.read_sock_done(strp, desc.error);
361
362 return desc.error;
363}
364
365/* Lower sock lock held */
96a59083 366void strp_data_ready(struct strparser *strp)
43a0c675 367{
456488cd 368 if (unlikely(strp->stopped) || strp->paused)
43a0c675
TH
369 return;
370
bbb03029
TH
371 /* This check is needed to synchronize with do_strp_work.
372 * do_strp_work acquires a process lock (lock_sock) whereas
43a0c675
TH
373 * the lock held here is bh_lock_sock. The two locks can be
374 * held by different threads at the same time, but bh_lock_sock
375 * allows a thread in BH context to safely check if the process
376 * lock is held. In this case, if the lock is held, queue work.
377 */
d66fa9ec 378 if (sock_owned_by_user_nocheck(strp->sk)) {
bbb03029 379 queue_work(strp_wq, &strp->work);
43a0c675
TH
380 return;
381 }
382
bbb03029 383 if (strp->need_bytes) {
9d0c75bf 384 if (strp_peek_len(strp) < strp->need_bytes)
43a0c675
TH
385 return;
386 }
387
96a59083 388 if (strp_read_sock(strp) == -ENOMEM)
bbb03029 389 queue_work(strp_wq, &strp->work);
43a0c675 390}
96a59083 391EXPORT_SYMBOL_GPL(strp_data_ready);
43a0c675 392
bbb03029 393static void do_strp_work(struct strparser *strp)
43a0c675 394{
96a59083
TH
395 /* We need the read lock to synchronize with strp_data_ready. We
396 * need the socket lock for calling strp_read_sock.
43a0c675 397 */
bbb03029 398 strp->cb.lock(strp);
43a0c675 399
bbb03029 400 if (unlikely(strp->stopped))
43a0c675
TH
401 goto out;
402
bbb03029 403 if (strp->paused)
43a0c675
TH
404 goto out;
405
96a59083 406 if (strp_read_sock(strp) == -ENOMEM)
bbb03029 407 queue_work(strp_wq, &strp->work);
43a0c675
TH
408
409out:
bbb03029 410 strp->cb.unlock(strp);
43a0c675
TH
411}
412
bbb03029 413static void strp_work(struct work_struct *w)
43a0c675 414{
bbb03029 415 do_strp_work(container_of(w, struct strparser, work));
43a0c675
TH
416}
417
829385f0 418static void strp_msg_timeout(struct work_struct *w)
43a0c675 419{
829385f0
TH
420 struct strparser *strp = container_of(w, struct strparser,
421 msg_timer_work.work);
43a0c675
TH
422
423 /* Message assembly timed out */
bbb03029
TH
424 STRP_STATS_INCR(strp->stats.msg_timeouts);
425 strp->cb.lock(strp);
cd00edc1 426 strp->cb.abort_parser(strp, -ETIMEDOUT);
bbb03029
TH
427 strp->cb.unlock(strp);
428}
429
430static void strp_sock_lock(struct strparser *strp)
431{
432 lock_sock(strp->sk);
433}
434
435static void strp_sock_unlock(struct strparser *strp)
436{
43a0c675
TH
437 release_sock(strp->sk);
438}
439
bbb03029 440int strp_init(struct strparser *strp, struct sock *sk,
3fd87127 441 const struct strp_callbacks *cb)
43a0c675 442{
96a59083 443
43a0c675
TH
444 if (!cb || !cb->rcv_msg || !cb->parse_msg)
445 return -EINVAL;
446
bbb03029
TH
447 /* The sk (sock) arg determines the mode of the stream parser.
448 *
449 * If the sock is set then the strparser is in receive callback mode.
450 * The upper layer calls strp_data_ready to kick receive processing
451 * and strparser calls the read_sock function on the socket to
452 * get packets.
453 *
454 * If the sock is not set then the strparser is in general mode.
455 * The upper layer calls strp_process for each skb to be parsed.
456 */
96a59083 457
f26de110 458 if (!sk) {
bbb03029
TH
459 if (!cb->lock || !cb->unlock)
460 return -EINVAL;
461 }
43a0c675 462
bbb03029 463 memset(strp, 0, sizeof(*strp));
43a0c675 464
bbb03029 465 strp->sk = sk;
43a0c675 466
bbb03029
TH
467 strp->cb.lock = cb->lock ? : strp_sock_lock;
468 strp->cb.unlock = cb->unlock ? : strp_sock_unlock;
43a0c675
TH
469 strp->cb.rcv_msg = cb->rcv_msg;
470 strp->cb.parse_msg = cb->parse_msg;
471 strp->cb.read_sock_done = cb->read_sock_done ? : default_read_sock_done;
bbb03029
TH
472 strp->cb.abort_parser = cb->abort_parser ? : strp_abort_strp;
473
829385f0 474 INIT_DELAYED_WORK(&strp->msg_timer_work, strp_msg_timeout);
bbb03029 475 INIT_WORK(&strp->work, strp_work);
43a0c675
TH
476
477 return 0;
478}
479EXPORT_SYMBOL_GPL(strp_init);
480
7170e604
DRK
481/* Sock process lock held (lock_sock) */
482void __strp_unpause(struct strparser *strp)
483{
484 strp->paused = 0;
485
486 if (strp->need_bytes) {
487 if (strp_peek_len(strp) < strp->need_bytes)
488 return;
489 }
490 strp_read_sock(strp);
491}
492EXPORT_SYMBOL_GPL(__strp_unpause);
493
cff6a334
TH
494void strp_unpause(struct strparser *strp)
495{
bbb03029 496 strp->paused = 0;
cff6a334 497
bbb03029 498 /* Sync setting paused with RX work */
cff6a334
TH
499 smp_mb();
500
bbb03029 501 queue_work(strp_wq, &strp->work);
cff6a334
TH
502}
503EXPORT_SYMBOL_GPL(strp_unpause);
504
96a59083 505/* strp must already be stopped so that strp_recv will no longer be called.
43a0c675
TH
506 * Note that strp_done is not called with the lower socket held.
507 */
508void strp_done(struct strparser *strp)
509{
bbb03029 510 WARN_ON(!strp->stopped);
43a0c675 511
829385f0 512 cancel_delayed_work_sync(&strp->msg_timer_work);
bbb03029 513 cancel_work_sync(&strp->work);
43a0c675 514
bbb03029
TH
515 if (strp->skb_head) {
516 kfree_skb(strp->skb_head);
517 strp->skb_head = NULL;
43a0c675
TH
518 }
519}
520EXPORT_SYMBOL_GPL(strp_done);
521
522void strp_stop(struct strparser *strp)
523{
bbb03029 524 strp->stopped = 1;
43a0c675
TH
525}
526EXPORT_SYMBOL_GPL(strp_stop);
527
528void strp_check_rcv(struct strparser *strp)
529{
bbb03029 530 queue_work(strp_wq, &strp->work);
43a0c675
TH
531}
532EXPORT_SYMBOL_GPL(strp_check_rcv);
533
15253b4a 534static int __init strp_dev_init(void)
43a0c675
TH
535{
536 strp_wq = create_singlethread_workqueue("kstrp");
228cd2db
KL
537 if (unlikely(!strp_wq))
538 return -ENOMEM;
43a0c675
TH
539
540 return 0;
541}
15253b4a 542device_initcall(strp_dev_init);