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
41 static inline struct _strp_msg
*_strp_msg(struct sk_buff
*skb
)
43 return (struct _strp_msg
*)((void *)skb
->cb
+
44 offsetof(struct qdisc_skb_cb
, data
));
48 static void strp_abort_strp(struct strparser
*strp
, int err
)
50 /* Unrecoverable error in receive */
52 cancel_delayed_work(&strp
->msg_timer_work
);
60 struct sock
*sk
= strp
->sk
;
62 /* Report an error on the lower socket */
64 sk
->sk_error_report(sk
);
68 static void strp_start_timer(struct strparser
*strp
, long timeo
)
70 if (timeo
&& timeo
!= LONG_MAX
)
71 mod_delayed_work(strp_wq
, &strp
->msg_timer_work
, timeo
);
75 static void strp_parser_err(struct strparser
*strp
, int err
,
76 read_descriptor_t
*desc
)
79 kfree_skb(strp
->skb_head
);
80 strp
->skb_head
= NULL
;
81 strp
->cb
.abort_parser(strp
, err
);
84 static inline int strp_peek_len(struct strparser
*strp
)
87 struct socket
*sock
= strp
->sk
->sk_socket
;
89 return sock
->ops
->peek_len(sock
);
92 /* If we don't have an associated socket there's nothing to peek.
93 * Return int max to avoid stopping the strparser.
99 /* Lower socket lock held */
100 static int __strp_recv(read_descriptor_t
*desc
, struct sk_buff
*orig_skb
,
101 unsigned int orig_offset
, size_t orig_len
,
102 size_t max_msg_size
, long timeo
)
104 struct strparser
*strp
= (struct strparser
*)desc
->arg
.data
;
105 struct _strp_msg
*stm
;
106 struct sk_buff
*head
, *skb
;
107 size_t eaten
= 0, cand_len
;
110 bool cloned_orig
= false;
115 head
= strp
->skb_head
;
117 /* Message already in progress */
119 stm
= _strp_msg(head
);
120 if (unlikely(stm
->early_eaten
)) {
121 /* Already some number of bytes on the receive sock
122 * data saved in skb_head, just indicate they
125 eaten
= orig_len
<= stm
->early_eaten
?
126 orig_len
: stm
->early_eaten
;
127 stm
->early_eaten
-= eaten
;
132 if (unlikely(orig_offset
)) {
133 /* Getting data with a non-zero offset when a message is
134 * in progress is not expected. If it does happen, we
135 * need to clone and pull since we can't deal with
136 * offsets in the skbs for a message expect in the head.
138 orig_skb
= skb_clone(orig_skb
, GFP_ATOMIC
);
140 STRP_STATS_INCR(strp
->stats
.mem_fail
);
141 desc
->error
= -ENOMEM
;
144 if (!pskb_pull(orig_skb
, orig_offset
)) {
145 STRP_STATS_INCR(strp
->stats
.mem_fail
);
147 desc
->error
= -ENOMEM
;
154 if (!strp
->skb_nextp
) {
155 /* We are going to append to the frags_list of head.
156 * Need to unshare the frag_list.
158 err
= skb_unclone(head
, GFP_ATOMIC
);
160 STRP_STATS_INCR(strp
->stats
.mem_fail
);
165 if (unlikely(skb_shinfo(head
)->frag_list
)) {
166 /* We can't append to an sk_buff that already
167 * has a frag_list. We create a new head, point
168 * the frag_list of that to the old head, and
169 * then are able to use the old head->next for
170 * appending to the message.
172 if (WARN_ON(head
->next
)) {
173 desc
->error
= -EINVAL
;
177 skb
= alloc_skb(0, GFP_ATOMIC
);
179 STRP_STATS_INCR(strp
->stats
.mem_fail
);
180 desc
->error
= -ENOMEM
;
183 skb
->len
= head
->len
;
184 skb
->data_len
= head
->len
;
185 skb
->truesize
= head
->truesize
;
186 *_strp_msg(skb
) = *_strp_msg(head
);
187 strp
->skb_nextp
= &head
->next
;
188 skb_shinfo(skb
)->frag_list
= head
;
189 strp
->skb_head
= skb
;
193 &skb_shinfo(head
)->frag_list
;
198 while (eaten
< orig_len
) {
199 /* Always clone since we will consume something */
200 skb
= skb_clone(orig_skb
, GFP_ATOMIC
);
202 STRP_STATS_INCR(strp
->stats
.mem_fail
);
203 desc
->error
= -ENOMEM
;
207 cand_len
= orig_len
- eaten
;
209 head
= strp
->skb_head
;
212 strp
->skb_head
= head
;
213 /* Will set skb_nextp on next packet if needed */
214 strp
->skb_nextp
= NULL
;
215 stm
= _strp_msg(head
);
216 memset(stm
, 0, sizeof(*stm
));
217 stm
->strp
.offset
= orig_offset
+ eaten
;
219 /* Unclone since we may be appending to an skb that we
220 * already share a frag_list with.
222 err
= skb_unclone(skb
, GFP_ATOMIC
);
224 STRP_STATS_INCR(strp
->stats
.mem_fail
);
229 stm
= _strp_msg(head
);
230 *strp
->skb_nextp
= skb
;
231 strp
->skb_nextp
= &skb
->next
;
232 head
->data_len
+= skb
->len
;
233 head
->len
+= skb
->len
;
234 head
->truesize
+= skb
->truesize
;
237 if (!stm
->strp
.full_len
) {
240 len
= (*strp
->cb
.parse_msg
)(strp
, head
);
243 /* Need more header to determine length */
244 if (!stm
->accum_len
) {
245 /* Start RX timer for new message */
246 strp_start_timer(strp
, timeo
);
248 stm
->accum_len
+= cand_len
;
250 STRP_STATS_INCR(strp
->stats
.need_more_hdr
);
251 WARN_ON(eaten
!= orig_len
);
253 } else if (len
< 0) {
254 if (len
== -ESTRPIPE
&& stm
->accum_len
) {
256 strp
->unrecov_intr
= 1;
258 strp
->interrupted
= 1;
260 strp_parser_err(strp
, len
, desc
);
262 } else if (len
> max_msg_size
) {
263 /* Message length exceeds maximum allowed */
264 STRP_STATS_INCR(strp
->stats
.msg_too_big
);
265 strp_parser_err(strp
, -EMSGSIZE
, desc
);
267 } else if (len
<= (ssize_t
)head
->len
-
268 skb
->len
- stm
->strp
.offset
) {
269 /* Length must be into new skb (and also
272 STRP_STATS_INCR(strp
->stats
.bad_hdr_len
);
273 strp_parser_err(strp
, -EPROTO
, desc
);
277 stm
->strp
.full_len
= len
;
280 extra
= (ssize_t
)(stm
->accum_len
+ cand_len
) -
284 /* Message not complete yet. */
285 if (stm
->strp
.full_len
- stm
->accum_len
>
286 strp_peek_len(strp
)) {
287 /* Don't have the whole message in the socket
288 * buffer. Set strp->need_bytes to wait for
289 * the rest of the message. Also, set "early
290 * eaten" since we've already buffered the skb
291 * but don't consume yet per strp_read_sock.
294 if (!stm
->accum_len
) {
295 /* Start RX timer for new message */
296 strp_start_timer(strp
, timeo
);
299 stm
->accum_len
+= cand_len
;
300 strp
->need_bytes
= stm
->strp
.full_len
-
302 stm
->early_eaten
= cand_len
;
303 STRP_STATS_ADD(strp
->stats
.bytes
, cand_len
);
304 desc
->count
= 0; /* Stop reading socket */
307 stm
->accum_len
+= cand_len
;
309 WARN_ON(eaten
!= orig_len
);
313 /* Positive extra indicates ore bytes than needed for the
317 WARN_ON(extra
> cand_len
);
319 eaten
+= (cand_len
- extra
);
321 /* Hurray, we have a new message! */
322 cancel_delayed_work(&strp
->msg_timer_work
);
323 strp
->skb_head
= NULL
;
324 strp
->need_bytes
= 0;
325 STRP_STATS_INCR(strp
->stats
.msgs
);
327 /* Give skb to upper layer */
328 strp
->cb
.rcv_msg(strp
, head
);
330 if (unlikely(strp
->paused
)) {
331 /* Upper layer paused strp */
339 STRP_STATS_ADD(strp
->stats
.bytes
, eaten
);
344 int strp_process(struct strparser
*strp
, struct sk_buff
*orig_skb
,
345 unsigned int orig_offset
, size_t orig_len
,
346 size_t max_msg_size
, long timeo
)
348 read_descriptor_t desc
; /* Dummy arg to strp_recv */
350 desc
.arg
.data
= strp
;
352 return __strp_recv(&desc
, orig_skb
, orig_offset
, orig_len
,
353 max_msg_size
, timeo
);
355 EXPORT_SYMBOL_GPL(strp_process
);
357 static int strp_recv(read_descriptor_t
*desc
, struct sk_buff
*orig_skb
,
358 unsigned int orig_offset
, size_t orig_len
)
360 struct strparser
*strp
= (struct strparser
*)desc
->arg
.data
;
362 return __strp_recv(desc
, orig_skb
, orig_offset
, orig_len
,
363 strp
->sk
->sk_rcvbuf
, strp
->sk
->sk_rcvtimeo
);
366 static int default_read_sock_done(struct strparser
*strp
, int err
)
371 /* Called with lock held on lower socket */
372 static int strp_read_sock(struct strparser
*strp
)
374 struct socket
*sock
= strp
->sk
->sk_socket
;
375 read_descriptor_t desc
;
377 if (unlikely(!sock
|| !sock
->ops
|| !sock
->ops
->read_sock
))
380 desc
.arg
.data
= strp
;
382 desc
.count
= 1; /* give more than one skb per call */
384 /* sk should be locked here, so okay to do read_sock */
385 sock
->ops
->read_sock(strp
->sk
, &desc
, strp_recv
);
387 desc
.error
= strp
->cb
.read_sock_done(strp
, desc
.error
);
392 /* Lower sock lock held */
393 void strp_data_ready(struct strparser
*strp
)
395 if (unlikely(strp
->stopped
))
398 /* This check is needed to synchronize with do_strp_work.
399 * do_strp_work acquires a process lock (lock_sock) whereas
400 * the lock held here is bh_lock_sock. The two locks can be
401 * held by different threads at the same time, but bh_lock_sock
402 * allows a thread in BH context to safely check if the process
403 * lock is held. In this case, if the lock is held, queue work.
405 if (sock_owned_by_user_nocheck(strp
->sk
)) {
406 queue_work(strp_wq
, &strp
->work
);
413 if (strp
->need_bytes
) {
414 if (strp_peek_len(strp
) < strp
->need_bytes
)
418 if (strp_read_sock(strp
) == -ENOMEM
)
419 queue_work(strp_wq
, &strp
->work
);
421 EXPORT_SYMBOL_GPL(strp_data_ready
);
423 static void do_strp_work(struct strparser
*strp
)
425 read_descriptor_t rd_desc
;
427 /* We need the read lock to synchronize with strp_data_ready. We
428 * need the socket lock for calling strp_read_sock.
432 if (unlikely(strp
->stopped
))
438 rd_desc
.arg
.data
= strp
;
440 if (strp_read_sock(strp
) == -ENOMEM
)
441 queue_work(strp_wq
, &strp
->work
);
444 strp
->cb
.unlock(strp
);
447 static void strp_work(struct work_struct
*w
)
449 do_strp_work(container_of(w
, struct strparser
, work
));
452 static void strp_msg_timeout(struct work_struct
*w
)
454 struct strparser
*strp
= container_of(w
, struct strparser
,
455 msg_timer_work
.work
);
457 /* Message assembly timed out */
458 STRP_STATS_INCR(strp
->stats
.msg_timeouts
);
460 strp
->cb
.abort_parser(strp
, -ETIMEDOUT
);
461 strp
->cb
.unlock(strp
);
464 static void strp_sock_lock(struct strparser
*strp
)
469 static void strp_sock_unlock(struct strparser
*strp
)
471 release_sock(strp
->sk
);
474 int strp_init(struct strparser
*strp
, struct sock
*sk
,
475 const struct strp_callbacks
*cb
)
478 if (!cb
|| !cb
->rcv_msg
|| !cb
->parse_msg
)
481 /* The sk (sock) arg determines the mode of the stream parser.
483 * If the sock is set then the strparser is in receive callback mode.
484 * The upper layer calls strp_data_ready to kick receive processing
485 * and strparser calls the read_sock function on the socket to
488 * If the sock is not set then the strparser is in general mode.
489 * The upper layer calls strp_process for each skb to be parsed.
493 if (!cb
->lock
|| !cb
->unlock
)
497 memset(strp
, 0, sizeof(*strp
));
501 strp
->cb
.lock
= cb
->lock
? : strp_sock_lock
;
502 strp
->cb
.unlock
= cb
->unlock
? : strp_sock_unlock
;
503 strp
->cb
.rcv_msg
= cb
->rcv_msg
;
504 strp
->cb
.parse_msg
= cb
->parse_msg
;
505 strp
->cb
.read_sock_done
= cb
->read_sock_done
? : default_read_sock_done
;
506 strp
->cb
.abort_parser
= cb
->abort_parser
? : strp_abort_strp
;
508 INIT_DELAYED_WORK(&strp
->msg_timer_work
, strp_msg_timeout
);
509 INIT_WORK(&strp
->work
, strp_work
);
513 EXPORT_SYMBOL_GPL(strp_init
);
515 void strp_unpause(struct strparser
*strp
)
519 /* Sync setting paused with RX work */
522 queue_work(strp_wq
, &strp
->work
);
524 EXPORT_SYMBOL_GPL(strp_unpause
);
526 /* strp must already be stopped so that strp_recv will no longer be called.
527 * Note that strp_done is not called with the lower socket held.
529 void strp_done(struct strparser
*strp
)
531 WARN_ON(!strp
->stopped
);
533 cancel_delayed_work_sync(&strp
->msg_timer_work
);
534 cancel_work_sync(&strp
->work
);
536 if (strp
->skb_head
) {
537 kfree_skb(strp
->skb_head
);
538 strp
->skb_head
= NULL
;
541 EXPORT_SYMBOL_GPL(strp_done
);
543 void strp_stop(struct strparser
*strp
)
547 EXPORT_SYMBOL_GPL(strp_stop
);
549 void strp_check_rcv(struct strparser
*strp
)
551 queue_work(strp_wq
, &strp
->work
);
553 EXPORT_SYMBOL_GPL(strp_check_rcv
);
555 static int __init
strp_mod_init(void)
557 strp_wq
= create_singlethread_workqueue("kstrp");
562 static void __exit
strp_mod_exit(void)
564 destroy_workqueue(strp_wq
);
566 module_init(strp_mod_init
);
567 module_exit(strp_mod_exit
);
568 MODULE_LICENSE("GPL");