1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
9 #include <linux/errno.h>
10 #include <linux/errqueue.h>
11 #include <linux/file.h>
13 #include <linux/kernel.h>
14 #include <linux/export.h>
15 #include <linux/init.h>
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>
28 static struct workqueue_struct
*strp_wq
;
31 /* Internal cb structure. struct strp_msg must be first for passing
38 static inline struct _strp_msg
*_strp_msg(struct sk_buff
*skb
)
40 return (struct _strp_msg
*)((void *)skb
->cb
+
41 offsetof(struct qdisc_skb_cb
, data
));
45 static void strp_abort_strp(struct strparser
*strp
, int err
)
47 /* Unrecoverable error in receive */
49 cancel_delayed_work(&strp
->msg_timer_work
);
57 struct sock
*sk
= strp
->sk
;
59 /* Report an error on the lower socket */
61 sk
->sk_error_report(sk
);
65 static void strp_start_timer(struct strparser
*strp
, long timeo
)
67 if (timeo
&& timeo
!= LONG_MAX
)
68 mod_delayed_work(strp_wq
, &strp
->msg_timer_work
, timeo
);
72 static void strp_parser_err(struct strparser
*strp
, int err
,
73 read_descriptor_t
*desc
)
76 kfree_skb(strp
->skb_head
);
77 strp
->skb_head
= NULL
;
78 strp
->cb
.abort_parser(strp
, err
);
81 static inline int strp_peek_len(struct strparser
*strp
)
84 struct socket
*sock
= strp
->sk
->sk_socket
;
86 return sock
->ops
->peek_len(sock
);
89 /* If we don't have an associated socket there's nothing to peek.
90 * Return int max to avoid stopping the strparser.
96 /* Lower socket lock held */
97 static int __strp_recv(read_descriptor_t
*desc
, struct sk_buff
*orig_skb
,
98 unsigned int orig_offset
, size_t orig_len
,
99 size_t max_msg_size
, long timeo
)
101 struct strparser
*strp
= (struct strparser
*)desc
->arg
.data
;
102 struct _strp_msg
*stm
;
103 struct sk_buff
*head
, *skb
;
104 size_t eaten
= 0, cand_len
;
107 bool cloned_orig
= false;
112 head
= strp
->skb_head
;
114 /* Message already in progress */
115 if (unlikely(orig_offset
)) {
116 /* Getting data with a non-zero offset when a message is
117 * in progress is not expected. If it does happen, we
118 * need to clone and pull since we can't deal with
119 * offsets in the skbs for a message expect in the head.
121 orig_skb
= skb_clone(orig_skb
, GFP_ATOMIC
);
123 STRP_STATS_INCR(strp
->stats
.mem_fail
);
124 desc
->error
= -ENOMEM
;
127 if (!pskb_pull(orig_skb
, orig_offset
)) {
128 STRP_STATS_INCR(strp
->stats
.mem_fail
);
130 desc
->error
= -ENOMEM
;
137 if (!strp
->skb_nextp
) {
138 /* We are going to append to the frags_list of head.
139 * Need to unshare the frag_list.
141 err
= skb_unclone(head
, GFP_ATOMIC
);
143 STRP_STATS_INCR(strp
->stats
.mem_fail
);
148 if (unlikely(skb_shinfo(head
)->frag_list
)) {
149 /* We can't append to an sk_buff that already
150 * has a frag_list. We create a new head, point
151 * the frag_list of that to the old head, and
152 * then are able to use the old head->next for
153 * appending to the message.
155 if (WARN_ON(head
->next
)) {
156 desc
->error
= -EINVAL
;
160 skb
= alloc_skb(0, GFP_ATOMIC
);
162 STRP_STATS_INCR(strp
->stats
.mem_fail
);
163 desc
->error
= -ENOMEM
;
166 skb
->len
= head
->len
;
167 skb
->data_len
= head
->len
;
168 skb
->truesize
= head
->truesize
;
169 *_strp_msg(skb
) = *_strp_msg(head
);
170 strp
->skb_nextp
= &head
->next
;
171 skb_shinfo(skb
)->frag_list
= head
;
172 strp
->skb_head
= skb
;
176 &skb_shinfo(head
)->frag_list
;
181 while (eaten
< orig_len
) {
182 /* Always clone since we will consume something */
183 skb
= skb_clone(orig_skb
, GFP_ATOMIC
);
185 STRP_STATS_INCR(strp
->stats
.mem_fail
);
186 desc
->error
= -ENOMEM
;
190 cand_len
= orig_len
- eaten
;
192 head
= strp
->skb_head
;
195 strp
->skb_head
= head
;
196 /* Will set skb_nextp on next packet if needed */
197 strp
->skb_nextp
= NULL
;
198 stm
= _strp_msg(head
);
199 memset(stm
, 0, sizeof(*stm
));
200 stm
->strp
.offset
= orig_offset
+ eaten
;
202 /* Unclone if we are appending to an skb that we
203 * already share a frag_list with.
205 if (skb_has_frag_list(skb
)) {
206 err
= skb_unclone(skb
, GFP_ATOMIC
);
208 STRP_STATS_INCR(strp
->stats
.mem_fail
);
214 stm
= _strp_msg(head
);
215 *strp
->skb_nextp
= skb
;
216 strp
->skb_nextp
= &skb
->next
;
217 head
->data_len
+= skb
->len
;
218 head
->len
+= skb
->len
;
219 head
->truesize
+= skb
->truesize
;
222 if (!stm
->strp
.full_len
) {
225 len
= (*strp
->cb
.parse_msg
)(strp
, head
);
228 /* Need more header to determine length */
229 if (!stm
->accum_len
) {
230 /* Start RX timer for new message */
231 strp_start_timer(strp
, timeo
);
233 stm
->accum_len
+= cand_len
;
235 STRP_STATS_INCR(strp
->stats
.need_more_hdr
);
236 WARN_ON(eaten
!= orig_len
);
238 } else if (len
< 0) {
239 if (len
== -ESTRPIPE
&& stm
->accum_len
) {
241 strp
->unrecov_intr
= 1;
243 strp
->interrupted
= 1;
245 strp_parser_err(strp
, len
, desc
);
247 } else if (len
> max_msg_size
) {
248 /* Message length exceeds maximum allowed */
249 STRP_STATS_INCR(strp
->stats
.msg_too_big
);
250 strp_parser_err(strp
, -EMSGSIZE
, desc
);
252 } else if (len
<= (ssize_t
)head
->len
-
253 skb
->len
- stm
->strp
.offset
) {
254 /* Length must be into new skb (and also
257 STRP_STATS_INCR(strp
->stats
.bad_hdr_len
);
258 strp_parser_err(strp
, -EPROTO
, desc
);
262 stm
->strp
.full_len
= len
;
265 extra
= (ssize_t
)(stm
->accum_len
+ cand_len
) -
269 /* Message not complete yet. */
270 if (stm
->strp
.full_len
- stm
->accum_len
>
271 strp_peek_len(strp
)) {
272 /* Don't have the whole message in the socket
273 * buffer. Set strp->need_bytes to wait for
274 * the rest of the message. Also, set "early
275 * eaten" since we've already buffered the skb
276 * but don't consume yet per strp_read_sock.
279 if (!stm
->accum_len
) {
280 /* Start RX timer for new message */
281 strp_start_timer(strp
, timeo
);
284 stm
->accum_len
+= cand_len
;
286 strp
->need_bytes
= stm
->strp
.full_len
-
288 STRP_STATS_ADD(strp
->stats
.bytes
, cand_len
);
289 desc
->count
= 0; /* Stop reading socket */
292 stm
->accum_len
+= cand_len
;
294 WARN_ON(eaten
!= orig_len
);
298 /* Positive extra indicates more bytes than needed for the
302 WARN_ON(extra
> cand_len
);
304 eaten
+= (cand_len
- extra
);
306 /* Hurray, we have a new message! */
307 cancel_delayed_work(&strp
->msg_timer_work
);
308 strp
->skb_head
= NULL
;
309 strp
->need_bytes
= 0;
310 STRP_STATS_INCR(strp
->stats
.msgs
);
312 /* Give skb to upper layer */
313 strp
->cb
.rcv_msg(strp
, head
);
315 if (unlikely(strp
->paused
)) {
316 /* Upper layer paused strp */
324 STRP_STATS_ADD(strp
->stats
.bytes
, eaten
);
329 int strp_process(struct strparser
*strp
, struct sk_buff
*orig_skb
,
330 unsigned int orig_offset
, size_t orig_len
,
331 size_t max_msg_size
, long timeo
)
333 read_descriptor_t desc
; /* Dummy arg to strp_recv */
335 desc
.arg
.data
= strp
;
337 return __strp_recv(&desc
, orig_skb
, orig_offset
, orig_len
,
338 max_msg_size
, timeo
);
340 EXPORT_SYMBOL_GPL(strp_process
);
342 static int strp_recv(read_descriptor_t
*desc
, struct sk_buff
*orig_skb
,
343 unsigned int orig_offset
, size_t orig_len
)
345 struct strparser
*strp
= (struct strparser
*)desc
->arg
.data
;
347 return __strp_recv(desc
, orig_skb
, orig_offset
, orig_len
,
348 strp
->sk
->sk_rcvbuf
, strp
->sk
->sk_rcvtimeo
);
351 static int default_read_sock_done(struct strparser
*strp
, int err
)
356 /* Called with lock held on lower socket */
357 static int strp_read_sock(struct strparser
*strp
)
359 struct socket
*sock
= strp
->sk
->sk_socket
;
360 read_descriptor_t desc
;
362 if (unlikely(!sock
|| !sock
->ops
|| !sock
->ops
->read_sock
))
365 desc
.arg
.data
= strp
;
367 desc
.count
= 1; /* give more than one skb per call */
369 /* sk should be locked here, so okay to do read_sock */
370 sock
->ops
->read_sock(strp
->sk
, &desc
, strp_recv
);
372 desc
.error
= strp
->cb
.read_sock_done(strp
, desc
.error
);
377 /* Lower sock lock held */
378 void strp_data_ready(struct strparser
*strp
)
380 if (unlikely(strp
->stopped
) || strp
->paused
)
383 /* This check is needed to synchronize with do_strp_work.
384 * do_strp_work acquires a process lock (lock_sock) whereas
385 * the lock held here is bh_lock_sock. The two locks can be
386 * held by different threads at the same time, but bh_lock_sock
387 * allows a thread in BH context to safely check if the process
388 * lock is held. In this case, if the lock is held, queue work.
390 if (sock_owned_by_user_nocheck(strp
->sk
)) {
391 queue_work(strp_wq
, &strp
->work
);
395 if (strp
->need_bytes
) {
396 if (strp_peek_len(strp
) < strp
->need_bytes
)
400 if (strp_read_sock(strp
) == -ENOMEM
)
401 queue_work(strp_wq
, &strp
->work
);
403 EXPORT_SYMBOL_GPL(strp_data_ready
);
405 static void do_strp_work(struct strparser
*strp
)
407 /* We need the read lock to synchronize with strp_data_ready. We
408 * need the socket lock for calling strp_read_sock.
412 if (unlikely(strp
->stopped
))
418 if (strp_read_sock(strp
) == -ENOMEM
)
419 queue_work(strp_wq
, &strp
->work
);
422 strp
->cb
.unlock(strp
);
425 static void strp_work(struct work_struct
*w
)
427 do_strp_work(container_of(w
, struct strparser
, work
));
430 static void strp_msg_timeout(struct work_struct
*w
)
432 struct strparser
*strp
= container_of(w
, struct strparser
,
433 msg_timer_work
.work
);
435 /* Message assembly timed out */
436 STRP_STATS_INCR(strp
->stats
.msg_timeouts
);
438 strp
->cb
.abort_parser(strp
, -ETIMEDOUT
);
439 strp
->cb
.unlock(strp
);
442 static void strp_sock_lock(struct strparser
*strp
)
447 static void strp_sock_unlock(struct strparser
*strp
)
449 release_sock(strp
->sk
);
452 int strp_init(struct strparser
*strp
, struct sock
*sk
,
453 const struct strp_callbacks
*cb
)
456 if (!cb
|| !cb
->rcv_msg
|| !cb
->parse_msg
)
459 /* The sk (sock) arg determines the mode of the stream parser.
461 * If the sock is set then the strparser is in receive callback mode.
462 * The upper layer calls strp_data_ready to kick receive processing
463 * and strparser calls the read_sock function on the socket to
466 * If the sock is not set then the strparser is in general mode.
467 * The upper layer calls strp_process for each skb to be parsed.
471 if (!cb
->lock
|| !cb
->unlock
)
475 memset(strp
, 0, sizeof(*strp
));
479 strp
->cb
.lock
= cb
->lock
? : strp_sock_lock
;
480 strp
->cb
.unlock
= cb
->unlock
? : strp_sock_unlock
;
481 strp
->cb
.rcv_msg
= cb
->rcv_msg
;
482 strp
->cb
.parse_msg
= cb
->parse_msg
;
483 strp
->cb
.read_sock_done
= cb
->read_sock_done
? : default_read_sock_done
;
484 strp
->cb
.abort_parser
= cb
->abort_parser
? : strp_abort_strp
;
486 INIT_DELAYED_WORK(&strp
->msg_timer_work
, strp_msg_timeout
);
487 INIT_WORK(&strp
->work
, strp_work
);
491 EXPORT_SYMBOL_GPL(strp_init
);
493 /* Sock process lock held (lock_sock) */
494 void __strp_unpause(struct strparser
*strp
)
498 if (strp
->need_bytes
) {
499 if (strp_peek_len(strp
) < strp
->need_bytes
)
502 strp_read_sock(strp
);
504 EXPORT_SYMBOL_GPL(__strp_unpause
);
506 void strp_unpause(struct strparser
*strp
)
510 /* Sync setting paused with RX work */
513 queue_work(strp_wq
, &strp
->work
);
515 EXPORT_SYMBOL_GPL(strp_unpause
);
517 /* strp must already be stopped so that strp_recv will no longer be called.
518 * Note that strp_done is not called with the lower socket held.
520 void strp_done(struct strparser
*strp
)
522 WARN_ON(!strp
->stopped
);
524 cancel_delayed_work_sync(&strp
->msg_timer_work
);
525 cancel_work_sync(&strp
->work
);
527 if (strp
->skb_head
) {
528 kfree_skb(strp
->skb_head
);
529 strp
->skb_head
= NULL
;
532 EXPORT_SYMBOL_GPL(strp_done
);
534 void strp_stop(struct strparser
*strp
)
538 EXPORT_SYMBOL_GPL(strp_stop
);
540 void strp_check_rcv(struct strparser
*strp
)
542 queue_work(strp_wq
, &strp
->work
);
544 EXPORT_SYMBOL_GPL(strp_check_rcv
);
546 static int __init
strp_dev_init(void)
548 strp_wq
= create_singlethread_workqueue("kstrp");
549 if (unlikely(!strp_wq
))
554 device_initcall(strp_dev_init
);