4 * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
11 #include <linux/bpf.h>
12 #include <linux/errno.h>
13 #include <linux/errqueue.h>
14 #include <linux/file.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/net.h>
19 #include <linux/netdevice.h>
20 #include <linux/poll.h>
21 #include <linux/rculist.h>
22 #include <linux/skbuff.h>
23 #include <linux/socket.h>
24 #include <linux/uaccess.h>
25 #include <linux/workqueue.h>
26 #include <net/strparser.h>
27 #include <net/netns/generic.h>
30 static struct workqueue_struct
*strp_wq
;
33 /* Internal cb structure. struct strp_msg must be first for passing
40 static inline struct _strp_msg
*_strp_msg(struct sk_buff
*skb
)
42 return (struct _strp_msg
*)((void *)skb
->cb
+
43 offsetof(struct qdisc_skb_cb
, data
));
47 static void strp_abort_strp(struct strparser
*strp
, int err
)
49 /* Unrecoverable error in receive */
51 cancel_delayed_work(&strp
->msg_timer_work
);
59 struct sock
*sk
= strp
->sk
;
61 /* Report an error on the lower socket */
63 sk
->sk_error_report(sk
);
67 static void strp_start_timer(struct strparser
*strp
, long timeo
)
69 if (timeo
&& timeo
!= LONG_MAX
)
70 mod_delayed_work(strp_wq
, &strp
->msg_timer_work
, timeo
);
74 static void strp_parser_err(struct strparser
*strp
, int err
,
75 read_descriptor_t
*desc
)
78 kfree_skb(strp
->skb_head
);
79 strp
->skb_head
= NULL
;
80 strp
->cb
.abort_parser(strp
, err
);
83 static inline int strp_peek_len(struct strparser
*strp
)
86 struct socket
*sock
= strp
->sk
->sk_socket
;
88 return sock
->ops
->peek_len(sock
);
91 /* If we don't have an associated socket there's nothing to peek.
92 * Return int max to avoid stopping the strparser.
98 /* Lower socket lock held */
99 static int __strp_recv(read_descriptor_t
*desc
, struct sk_buff
*orig_skb
,
100 unsigned int orig_offset
, size_t orig_len
,
101 size_t max_msg_size
, long timeo
)
103 struct strparser
*strp
= (struct strparser
*)desc
->arg
.data
;
104 struct _strp_msg
*stm
;
105 struct sk_buff
*head
, *skb
;
106 size_t eaten
= 0, cand_len
;
109 bool cloned_orig
= false;
114 head
= strp
->skb_head
;
116 /* Message already in progress */
117 if (unlikely(orig_offset
)) {
118 /* Getting data with a non-zero offset when a message is
119 * in progress is not expected. If it does happen, we
120 * need to clone and pull since we can't deal with
121 * offsets in the skbs for a message expect in the head.
123 orig_skb
= skb_clone(orig_skb
, GFP_ATOMIC
);
125 STRP_STATS_INCR(strp
->stats
.mem_fail
);
126 desc
->error
= -ENOMEM
;
129 if (!pskb_pull(orig_skb
, orig_offset
)) {
130 STRP_STATS_INCR(strp
->stats
.mem_fail
);
132 desc
->error
= -ENOMEM
;
139 if (!strp
->skb_nextp
) {
140 /* We are going to append to the frags_list of head.
141 * Need to unshare the frag_list.
143 err
= skb_unclone(head
, GFP_ATOMIC
);
145 STRP_STATS_INCR(strp
->stats
.mem_fail
);
150 if (unlikely(skb_shinfo(head
)->frag_list
)) {
151 /* We can't append to an sk_buff that already
152 * has a frag_list. We create a new head, point
153 * the frag_list of that to the old head, and
154 * then are able to use the old head->next for
155 * appending to the message.
157 if (WARN_ON(head
->next
)) {
158 desc
->error
= -EINVAL
;
162 skb
= alloc_skb(0, GFP_ATOMIC
);
164 STRP_STATS_INCR(strp
->stats
.mem_fail
);
165 desc
->error
= -ENOMEM
;
168 skb
->len
= head
->len
;
169 skb
->data_len
= head
->len
;
170 skb
->truesize
= head
->truesize
;
171 *_strp_msg(skb
) = *_strp_msg(head
);
172 strp
->skb_nextp
= &head
->next
;
173 skb_shinfo(skb
)->frag_list
= head
;
174 strp
->skb_head
= skb
;
178 &skb_shinfo(head
)->frag_list
;
183 while (eaten
< orig_len
) {
184 /* Always clone since we will consume something */
185 skb
= skb_clone(orig_skb
, GFP_ATOMIC
);
187 STRP_STATS_INCR(strp
->stats
.mem_fail
);
188 desc
->error
= -ENOMEM
;
192 cand_len
= orig_len
- eaten
;
194 head
= strp
->skb_head
;
197 strp
->skb_head
= head
;
198 /* Will set skb_nextp on next packet if needed */
199 strp
->skb_nextp
= NULL
;
200 stm
= _strp_msg(head
);
201 memset(stm
, 0, sizeof(*stm
));
202 stm
->strp
.offset
= orig_offset
+ eaten
;
204 /* Unclone if we are appending to an skb that we
205 * already share a frag_list with.
207 if (skb_has_frag_list(skb
)) {
208 err
= skb_unclone(skb
, GFP_ATOMIC
);
210 STRP_STATS_INCR(strp
->stats
.mem_fail
);
216 stm
= _strp_msg(head
);
217 *strp
->skb_nextp
= skb
;
218 strp
->skb_nextp
= &skb
->next
;
219 head
->data_len
+= skb
->len
;
220 head
->len
+= skb
->len
;
221 head
->truesize
+= skb
->truesize
;
224 if (!stm
->strp
.full_len
) {
227 len
= (*strp
->cb
.parse_msg
)(strp
, head
);
230 /* Need more header to determine length */
231 if (!stm
->accum_len
) {
232 /* Start RX timer for new message */
233 strp_start_timer(strp
, timeo
);
235 stm
->accum_len
+= cand_len
;
237 STRP_STATS_INCR(strp
->stats
.need_more_hdr
);
238 WARN_ON(eaten
!= orig_len
);
240 } else if (len
< 0) {
241 if (len
== -ESTRPIPE
&& stm
->accum_len
) {
243 strp
->unrecov_intr
= 1;
245 strp
->interrupted
= 1;
247 strp_parser_err(strp
, len
, desc
);
249 } else if (len
> max_msg_size
) {
250 /* Message length exceeds maximum allowed */
251 STRP_STATS_INCR(strp
->stats
.msg_too_big
);
252 strp_parser_err(strp
, -EMSGSIZE
, desc
);
254 } else if (len
<= (ssize_t
)head
->len
-
255 skb
->len
- stm
->strp
.offset
) {
256 /* Length must be into new skb (and also
259 STRP_STATS_INCR(strp
->stats
.bad_hdr_len
);
260 strp_parser_err(strp
, -EPROTO
, desc
);
264 stm
->strp
.full_len
= len
;
267 extra
= (ssize_t
)(stm
->accum_len
+ cand_len
) -
271 /* Message not complete yet. */
272 if (stm
->strp
.full_len
- stm
->accum_len
>
273 strp_peek_len(strp
)) {
274 /* Don't have the whole message in the socket
275 * buffer. Set strp->need_bytes to wait for
276 * the rest of the message. Also, set "early
277 * eaten" since we've already buffered the skb
278 * but don't consume yet per strp_read_sock.
281 if (!stm
->accum_len
) {
282 /* Start RX timer for new message */
283 strp_start_timer(strp
, timeo
);
286 stm
->accum_len
+= cand_len
;
288 strp
->need_bytes
= stm
->strp
.full_len
-
290 STRP_STATS_ADD(strp
->stats
.bytes
, cand_len
);
291 desc
->count
= 0; /* Stop reading socket */
294 stm
->accum_len
+= cand_len
;
296 WARN_ON(eaten
!= orig_len
);
300 /* Positive extra indicates ore bytes than needed for the
304 WARN_ON(extra
> cand_len
);
306 eaten
+= (cand_len
- extra
);
308 /* Hurray, we have a new message! */
309 cancel_delayed_work(&strp
->msg_timer_work
);
310 strp
->skb_head
= NULL
;
311 strp
->need_bytes
= 0;
312 STRP_STATS_INCR(strp
->stats
.msgs
);
314 /* Give skb to upper layer */
315 strp
->cb
.rcv_msg(strp
, head
);
317 if (unlikely(strp
->paused
)) {
318 /* Upper layer paused strp */
326 STRP_STATS_ADD(strp
->stats
.bytes
, eaten
);
331 int strp_process(struct strparser
*strp
, struct sk_buff
*orig_skb
,
332 unsigned int orig_offset
, size_t orig_len
,
333 size_t max_msg_size
, long timeo
)
335 read_descriptor_t desc
; /* Dummy arg to strp_recv */
337 desc
.arg
.data
= strp
;
339 return __strp_recv(&desc
, orig_skb
, orig_offset
, orig_len
,
340 max_msg_size
, timeo
);
342 EXPORT_SYMBOL_GPL(strp_process
);
344 static int strp_recv(read_descriptor_t
*desc
, struct sk_buff
*orig_skb
,
345 unsigned int orig_offset
, size_t orig_len
)
347 struct strparser
*strp
= (struct strparser
*)desc
->arg
.data
;
349 return __strp_recv(desc
, orig_skb
, orig_offset
, orig_len
,
350 strp
->sk
->sk_rcvbuf
, strp
->sk
->sk_rcvtimeo
);
353 static int default_read_sock_done(struct strparser
*strp
, int err
)
358 /* Called with lock held on lower socket */
359 static int strp_read_sock(struct strparser
*strp
)
361 struct socket
*sock
= strp
->sk
->sk_socket
;
362 read_descriptor_t desc
;
364 if (unlikely(!sock
|| !sock
->ops
|| !sock
->ops
->read_sock
))
367 desc
.arg
.data
= strp
;
369 desc
.count
= 1; /* give more than one skb per call */
371 /* sk should be locked here, so okay to do read_sock */
372 sock
->ops
->read_sock(strp
->sk
, &desc
, strp_recv
);
374 desc
.error
= strp
->cb
.read_sock_done(strp
, desc
.error
);
379 /* Lower sock lock held */
380 void strp_data_ready(struct strparser
*strp
)
382 if (unlikely(strp
->stopped
) || strp
->paused
)
385 /* This check is needed to synchronize with do_strp_work.
386 * do_strp_work acquires a process lock (lock_sock) whereas
387 * the lock held here is bh_lock_sock. The two locks can be
388 * held by different threads at the same time, but bh_lock_sock
389 * allows a thread in BH context to safely check if the process
390 * lock is held. In this case, if the lock is held, queue work.
392 if (sock_owned_by_user_nocheck(strp
->sk
)) {
393 queue_work(strp_wq
, &strp
->work
);
397 if (strp
->need_bytes
) {
398 if (strp_peek_len(strp
) < strp
->need_bytes
)
402 if (strp_read_sock(strp
) == -ENOMEM
)
403 queue_work(strp_wq
, &strp
->work
);
405 EXPORT_SYMBOL_GPL(strp_data_ready
);
407 static void do_strp_work(struct strparser
*strp
)
409 /* We need the read lock to synchronize with strp_data_ready. We
410 * need the socket lock for calling strp_read_sock.
414 if (unlikely(strp
->stopped
))
420 if (strp_read_sock(strp
) == -ENOMEM
)
421 queue_work(strp_wq
, &strp
->work
);
424 strp
->cb
.unlock(strp
);
427 static void strp_work(struct work_struct
*w
)
429 do_strp_work(container_of(w
, struct strparser
, work
));
432 static void strp_msg_timeout(struct work_struct
*w
)
434 struct strparser
*strp
= container_of(w
, struct strparser
,
435 msg_timer_work
.work
);
437 /* Message assembly timed out */
438 STRP_STATS_INCR(strp
->stats
.msg_timeouts
);
440 strp
->cb
.abort_parser(strp
, -ETIMEDOUT
);
441 strp
->cb
.unlock(strp
);
444 static void strp_sock_lock(struct strparser
*strp
)
449 static void strp_sock_unlock(struct strparser
*strp
)
451 release_sock(strp
->sk
);
454 int strp_init(struct strparser
*strp
, struct sock
*sk
,
455 const struct strp_callbacks
*cb
)
458 if (!cb
|| !cb
->rcv_msg
|| !cb
->parse_msg
)
461 /* The sk (sock) arg determines the mode of the stream parser.
463 * If the sock is set then the strparser is in receive callback mode.
464 * The upper layer calls strp_data_ready to kick receive processing
465 * and strparser calls the read_sock function on the socket to
468 * If the sock is not set then the strparser is in general mode.
469 * The upper layer calls strp_process for each skb to be parsed.
473 if (!cb
->lock
|| !cb
->unlock
)
477 memset(strp
, 0, sizeof(*strp
));
481 strp
->cb
.lock
= cb
->lock
? : strp_sock_lock
;
482 strp
->cb
.unlock
= cb
->unlock
? : strp_sock_unlock
;
483 strp
->cb
.rcv_msg
= cb
->rcv_msg
;
484 strp
->cb
.parse_msg
= cb
->parse_msg
;
485 strp
->cb
.read_sock_done
= cb
->read_sock_done
? : default_read_sock_done
;
486 strp
->cb
.abort_parser
= cb
->abort_parser
? : strp_abort_strp
;
488 INIT_DELAYED_WORK(&strp
->msg_timer_work
, strp_msg_timeout
);
489 INIT_WORK(&strp
->work
, strp_work
);
493 EXPORT_SYMBOL_GPL(strp_init
);
495 /* Sock process lock held (lock_sock) */
496 void __strp_unpause(struct strparser
*strp
)
500 if (strp
->need_bytes
) {
501 if (strp_peek_len(strp
) < strp
->need_bytes
)
504 strp_read_sock(strp
);
506 EXPORT_SYMBOL_GPL(__strp_unpause
);
508 void strp_unpause(struct strparser
*strp
)
512 /* Sync setting paused with RX work */
515 queue_work(strp_wq
, &strp
->work
);
517 EXPORT_SYMBOL_GPL(strp_unpause
);
519 /* strp must already be stopped so that strp_recv will no longer be called.
520 * Note that strp_done is not called with the lower socket held.
522 void strp_done(struct strparser
*strp
)
524 WARN_ON(!strp
->stopped
);
526 cancel_delayed_work_sync(&strp
->msg_timer_work
);
527 cancel_work_sync(&strp
->work
);
529 if (strp
->skb_head
) {
530 kfree_skb(strp
->skb_head
);
531 strp
->skb_head
= NULL
;
534 EXPORT_SYMBOL_GPL(strp_done
);
536 void strp_stop(struct strparser
*strp
)
540 EXPORT_SYMBOL_GPL(strp_stop
);
542 void strp_check_rcv(struct strparser
*strp
)
544 queue_work(strp_wq
, &strp
->work
);
546 EXPORT_SYMBOL_GPL(strp_check_rcv
);
548 static int __init
strp_mod_init(void)
550 strp_wq
= create_singlethread_workqueue("kstrp");
555 static void __exit
strp_mod_exit(void)
557 destroy_workqueue(strp_wq
);
559 module_init(strp_mod_init
);
560 module_exit(strp_mod_exit
);
561 MODULE_LICENSE("GPL");