xref: /linux-6.15/net/strparser/strparser.c (revision f78ef7cd)
143a0c675STom Herbert /*
243a0c675STom Herbert  * Stream Parser
343a0c675STom Herbert  *
443a0c675STom Herbert  * Copyright (c) 2016 Tom Herbert <[email protected]>
543a0c675STom Herbert  *
643a0c675STom Herbert  * This program is free software; you can redistribute it and/or modify
743a0c675STom Herbert  * it under the terms of the GNU General Public License version 2
843a0c675STom Herbert  * as published by the Free Software Foundation.
943a0c675STom Herbert  */
1043a0c675STom Herbert 
1143a0c675STom Herbert #include <linux/bpf.h>
1243a0c675STom Herbert #include <linux/errno.h>
1343a0c675STom Herbert #include <linux/errqueue.h>
1443a0c675STom Herbert #include <linux/file.h>
1543a0c675STom Herbert #include <linux/in.h>
1643a0c675STom Herbert #include <linux/kernel.h>
1743a0c675STom Herbert #include <linux/module.h>
1843a0c675STom Herbert #include <linux/net.h>
1943a0c675STom Herbert #include <linux/netdevice.h>
2043a0c675STom Herbert #include <linux/poll.h>
2143a0c675STom Herbert #include <linux/rculist.h>
2243a0c675STom Herbert #include <linux/skbuff.h>
2343a0c675STom Herbert #include <linux/socket.h>
2443a0c675STom Herbert #include <linux/uaccess.h>
2543a0c675STom Herbert #include <linux/workqueue.h>
2643a0c675STom Herbert #include <net/strparser.h>
2743a0c675STom Herbert #include <net/netns/generic.h>
2843a0c675STom Herbert #include <net/sock.h>
2943a0c675STom Herbert 
3043a0c675STom Herbert static struct workqueue_struct *strp_wq;
3143a0c675STom Herbert 
3243a0c675STom Herbert struct _strp_rx_msg {
3343a0c675STom Herbert 	/* Internal cb structure. struct strp_rx_msg must be first for passing
3443a0c675STom Herbert 	 * to upper layer.
3543a0c675STom Herbert 	 */
3643a0c675STom Herbert 	struct strp_rx_msg strp;
3743a0c675STom Herbert 	int accum_len;
3843a0c675STom Herbert 	int early_eaten;
3943a0c675STom Herbert };
4043a0c675STom Herbert 
4143a0c675STom Herbert static inline struct _strp_rx_msg *_strp_rx_msg(struct sk_buff *skb)
4243a0c675STom Herbert {
4343a0c675STom Herbert 	return (struct _strp_rx_msg *)((void *)skb->cb +
4443a0c675STom Herbert 		offsetof(struct qdisc_skb_cb, data));
4543a0c675STom Herbert }
4643a0c675STom Herbert 
4743a0c675STom Herbert /* Lower lock held */
4843a0c675STom Herbert static void strp_abort_rx_strp(struct strparser *strp, int err)
4943a0c675STom Herbert {
5043a0c675STom Herbert 	struct sock *csk = strp->sk;
5143a0c675STom Herbert 
5243a0c675STom Herbert 	/* Unrecoverable error in receive */
5343a0c675STom Herbert 
5443a0c675STom Herbert 	del_timer(&strp->rx_msg_timer);
5543a0c675STom Herbert 
5643a0c675STom Herbert 	if (strp->rx_stopped)
5743a0c675STom Herbert 		return;
5843a0c675STom Herbert 
5943a0c675STom Herbert 	strp->rx_stopped = 1;
6043a0c675STom Herbert 
6143a0c675STom Herbert 	/* Report an error on the lower socket */
6243a0c675STom Herbert 	csk->sk_err = err;
6343a0c675STom Herbert 	csk->sk_error_report(csk);
6443a0c675STom Herbert }
6543a0c675STom Herbert 
6643a0c675STom Herbert static void strp_start_rx_timer(struct strparser *strp)
6743a0c675STom Herbert {
6843a0c675STom Herbert 	if (strp->sk->sk_rcvtimeo)
6943a0c675STom Herbert 		mod_timer(&strp->rx_msg_timer, strp->sk->sk_rcvtimeo);
7043a0c675STom Herbert }
7143a0c675STom Herbert 
7243a0c675STom Herbert /* Lower lock held */
7343a0c675STom Herbert static void strp_parser_err(struct strparser *strp, int err,
7443a0c675STom Herbert 			    read_descriptor_t *desc)
7543a0c675STom Herbert {
7643a0c675STom Herbert 	desc->error = err;
7743a0c675STom Herbert 	kfree_skb(strp->rx_skb_head);
7843a0c675STom Herbert 	strp->rx_skb_head = NULL;
7943a0c675STom Herbert 	strp->cb.abort_parser(strp, err);
8043a0c675STom Herbert }
8143a0c675STom Herbert 
8296a59083STom Herbert static inline int strp_peek_len(struct strparser *strp)
8396a59083STom Herbert {
8496a59083STom Herbert 	struct socket *sock = strp->sk->sk_socket;
8596a59083STom Herbert 
8696a59083STom Herbert 	return sock->ops->peek_len(sock);
8796a59083STom Herbert }
8896a59083STom Herbert 
8943a0c675STom Herbert /* Lower socket lock held */
9096a59083STom Herbert static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
9143a0c675STom Herbert 		     unsigned int orig_offset, size_t orig_len)
9243a0c675STom Herbert {
9343a0c675STom Herbert 	struct strparser *strp = (struct strparser *)desc->arg.data;
9443a0c675STom Herbert 	struct _strp_rx_msg *rxm;
9543a0c675STom Herbert 	struct sk_buff *head, *skb;
9643a0c675STom Herbert 	size_t eaten = 0, cand_len;
9743a0c675STom Herbert 	ssize_t extra;
9843a0c675STom Herbert 	int err;
9943a0c675STom Herbert 	bool cloned_orig = false;
10043a0c675STom Herbert 
10143a0c675STom Herbert 	if (strp->rx_paused)
10243a0c675STom Herbert 		return 0;
10343a0c675STom Herbert 
10443a0c675STom Herbert 	head = strp->rx_skb_head;
10543a0c675STom Herbert 	if (head) {
10643a0c675STom Herbert 		/* Message already in progress */
10743a0c675STom Herbert 
10843a0c675STom Herbert 		rxm = _strp_rx_msg(head);
10943a0c675STom Herbert 		if (unlikely(rxm->early_eaten)) {
11043a0c675STom Herbert 			/* Already some number of bytes on the receive sock
11143a0c675STom Herbert 			 * data saved in rx_skb_head, just indicate they
11243a0c675STom Herbert 			 * are consumed.
11343a0c675STom Herbert 			 */
11443a0c675STom Herbert 			eaten = orig_len <= rxm->early_eaten ?
11543a0c675STom Herbert 				orig_len : rxm->early_eaten;
11643a0c675STom Herbert 			rxm->early_eaten -= eaten;
11743a0c675STom Herbert 
11843a0c675STom Herbert 			return eaten;
11943a0c675STom Herbert 		}
12043a0c675STom Herbert 
12143a0c675STom Herbert 		if (unlikely(orig_offset)) {
12243a0c675STom Herbert 			/* Getting data with a non-zero offset when a message is
12343a0c675STom Herbert 			 * in progress is not expected. If it does happen, we
12443a0c675STom Herbert 			 * need to clone and pull since we can't deal with
12543a0c675STom Herbert 			 * offsets in the skbs for a message expect in the head.
12643a0c675STom Herbert 			 */
12743a0c675STom Herbert 			orig_skb = skb_clone(orig_skb, GFP_ATOMIC);
12843a0c675STom Herbert 			if (!orig_skb) {
12943a0c675STom Herbert 				STRP_STATS_INCR(strp->stats.rx_mem_fail);
13043a0c675STom Herbert 				desc->error = -ENOMEM;
13143a0c675STom Herbert 				return 0;
13243a0c675STom Herbert 			}
13343a0c675STom Herbert 			if (!pskb_pull(orig_skb, orig_offset)) {
13443a0c675STom Herbert 				STRP_STATS_INCR(strp->stats.rx_mem_fail);
13543a0c675STom Herbert 				kfree_skb(orig_skb);
13643a0c675STom Herbert 				desc->error = -ENOMEM;
13743a0c675STom Herbert 				return 0;
13843a0c675STom Herbert 			}
13943a0c675STom Herbert 			cloned_orig = true;
14043a0c675STom Herbert 			orig_offset = 0;
14143a0c675STom Herbert 		}
14243a0c675STom Herbert 
14343a0c675STom Herbert 		if (!strp->rx_skb_nextp) {
14443a0c675STom Herbert 			/* We are going to append to the frags_list of head.
14543a0c675STom Herbert 			 * Need to unshare the frag_list.
14643a0c675STom Herbert 			 */
14743a0c675STom Herbert 			err = skb_unclone(head, GFP_ATOMIC);
14843a0c675STom Herbert 			if (err) {
14943a0c675STom Herbert 				STRP_STATS_INCR(strp->stats.rx_mem_fail);
15043a0c675STom Herbert 				desc->error = err;
15143a0c675STom Herbert 				return 0;
15243a0c675STom Herbert 			}
15343a0c675STom Herbert 
15443a0c675STom Herbert 			if (unlikely(skb_shinfo(head)->frag_list)) {
15543a0c675STom Herbert 				/* We can't append to an sk_buff that already
15643a0c675STom Herbert 				 * has a frag_list. We create a new head, point
15743a0c675STom Herbert 				 * the frag_list of that to the old head, and
15843a0c675STom Herbert 				 * then are able to use the old head->next for
15943a0c675STom Herbert 				 * appending to the message.
16043a0c675STom Herbert 				 */
16143a0c675STom Herbert 				if (WARN_ON(head->next)) {
16243a0c675STom Herbert 					desc->error = -EINVAL;
16343a0c675STom Herbert 					return 0;
16443a0c675STom Herbert 				}
16543a0c675STom Herbert 
16643a0c675STom Herbert 				skb = alloc_skb(0, GFP_ATOMIC);
16743a0c675STom Herbert 				if (!skb) {
16843a0c675STom Herbert 					STRP_STATS_INCR(strp->stats.rx_mem_fail);
16943a0c675STom Herbert 					desc->error = -ENOMEM;
17043a0c675STom Herbert 					return 0;
17143a0c675STom Herbert 				}
17243a0c675STom Herbert 				skb->len = head->len;
17343a0c675STom Herbert 				skb->data_len = head->len;
17443a0c675STom Herbert 				skb->truesize = head->truesize;
17543a0c675STom Herbert 				*_strp_rx_msg(skb) = *_strp_rx_msg(head);
17643a0c675STom Herbert 				strp->rx_skb_nextp = &head->next;
17743a0c675STom Herbert 				skb_shinfo(skb)->frag_list = head;
17843a0c675STom Herbert 				strp->rx_skb_head = skb;
17943a0c675STom Herbert 				head = skb;
18043a0c675STom Herbert 			} else {
18143a0c675STom Herbert 				strp->rx_skb_nextp =
18243a0c675STom Herbert 				    &skb_shinfo(head)->frag_list;
18343a0c675STom Herbert 			}
18443a0c675STom Herbert 		}
18543a0c675STom Herbert 	}
18643a0c675STom Herbert 
18743a0c675STom Herbert 	while (eaten < orig_len) {
18843a0c675STom Herbert 		/* Always clone since we will consume something */
18943a0c675STom Herbert 		skb = skb_clone(orig_skb, GFP_ATOMIC);
19043a0c675STom Herbert 		if (!skb) {
19143a0c675STom Herbert 			STRP_STATS_INCR(strp->stats.rx_mem_fail);
19243a0c675STom Herbert 			desc->error = -ENOMEM;
19343a0c675STom Herbert 			break;
19443a0c675STom Herbert 		}
19543a0c675STom Herbert 
19643a0c675STom Herbert 		cand_len = orig_len - eaten;
19743a0c675STom Herbert 
19843a0c675STom Herbert 		head = strp->rx_skb_head;
19943a0c675STom Herbert 		if (!head) {
20043a0c675STom Herbert 			head = skb;
20143a0c675STom Herbert 			strp->rx_skb_head = head;
20243a0c675STom Herbert 			/* Will set rx_skb_nextp on next packet if needed */
20343a0c675STom Herbert 			strp->rx_skb_nextp = NULL;
20443a0c675STom Herbert 			rxm = _strp_rx_msg(head);
20543a0c675STom Herbert 			memset(rxm, 0, sizeof(*rxm));
20643a0c675STom Herbert 			rxm->strp.offset = orig_offset + eaten;
20743a0c675STom Herbert 		} else {
20843a0c675STom Herbert 			/* Unclone since we may be appending to an skb that we
20943a0c675STom Herbert 			 * already share a frag_list with.
21043a0c675STom Herbert 			 */
21143a0c675STom Herbert 			err = skb_unclone(skb, GFP_ATOMIC);
21243a0c675STom Herbert 			if (err) {
21343a0c675STom Herbert 				STRP_STATS_INCR(strp->stats.rx_mem_fail);
21443a0c675STom Herbert 				desc->error = err;
21543a0c675STom Herbert 				break;
21643a0c675STom Herbert 			}
21743a0c675STom Herbert 
21843a0c675STom Herbert 			rxm = _strp_rx_msg(head);
21943a0c675STom Herbert 			*strp->rx_skb_nextp = skb;
22043a0c675STom Herbert 			strp->rx_skb_nextp = &skb->next;
22143a0c675STom Herbert 			head->data_len += skb->len;
22243a0c675STom Herbert 			head->len += skb->len;
22343a0c675STom Herbert 			head->truesize += skb->truesize;
22443a0c675STom Herbert 		}
22543a0c675STom Herbert 
22643a0c675STom Herbert 		if (!rxm->strp.full_len) {
22743a0c675STom Herbert 			ssize_t len;
22843a0c675STom Herbert 
22943a0c675STom Herbert 			len = (*strp->cb.parse_msg)(strp, head);
23043a0c675STom Herbert 
23143a0c675STom Herbert 			if (!len) {
23243a0c675STom Herbert 				/* Need more header to determine length */
23343a0c675STom Herbert 				if (!rxm->accum_len) {
23443a0c675STom Herbert 					/* Start RX timer for new message */
23543a0c675STom Herbert 					strp_start_rx_timer(strp);
23643a0c675STom Herbert 				}
23743a0c675STom Herbert 				rxm->accum_len += cand_len;
23843a0c675STom Herbert 				eaten += cand_len;
23943a0c675STom Herbert 				STRP_STATS_INCR(strp->stats.rx_need_more_hdr);
24043a0c675STom Herbert 				WARN_ON(eaten != orig_len);
24143a0c675STom Herbert 				break;
24243a0c675STom Herbert 			} else if (len < 0) {
24343a0c675STom Herbert 				if (len == -ESTRPIPE && rxm->accum_len) {
24443a0c675STom Herbert 					len = -ENODATA;
24543a0c675STom Herbert 					strp->rx_unrecov_intr = 1;
24643a0c675STom Herbert 				} else {
24743a0c675STom Herbert 					strp->rx_interrupted = 1;
24843a0c675STom Herbert 				}
2496d3a4c40SGeert Uytterhoeven 				strp_parser_err(strp, len, desc);
25043a0c675STom Herbert 				break;
25143a0c675STom Herbert 			} else if (len > strp->sk->sk_rcvbuf) {
25243a0c675STom Herbert 				/* Message length exceeds maximum allowed */
25343a0c675STom Herbert 				STRP_STATS_INCR(strp->stats.rx_msg_too_big);
25443a0c675STom Herbert 				strp_parser_err(strp, -EMSGSIZE, desc);
25543a0c675STom Herbert 				break;
25643a0c675STom Herbert 			} else if (len <= (ssize_t)head->len -
25743a0c675STom Herbert 					  skb->len - rxm->strp.offset) {
25843a0c675STom Herbert 				/* Length must be into new skb (and also
25943a0c675STom Herbert 				 * greater than zero)
26043a0c675STom Herbert 				 */
26143a0c675STom Herbert 				STRP_STATS_INCR(strp->stats.rx_bad_hdr_len);
26243a0c675STom Herbert 				strp_parser_err(strp, -EPROTO, desc);
26343a0c675STom Herbert 				break;
26443a0c675STom Herbert 			}
26543a0c675STom Herbert 
26643a0c675STom Herbert 			rxm->strp.full_len = len;
26743a0c675STom Herbert 		}
26843a0c675STom Herbert 
26943a0c675STom Herbert 		extra = (ssize_t)(rxm->accum_len + cand_len) -
27043a0c675STom Herbert 			rxm->strp.full_len;
27143a0c675STom Herbert 
27243a0c675STom Herbert 		if (extra < 0) {
27343a0c675STom Herbert 			/* Message not complete yet. */
27443a0c675STom Herbert 			if (rxm->strp.full_len - rxm->accum_len >
27596a59083STom Herbert 			    strp_peek_len(strp)) {
27643a0c675STom Herbert 				/* Don't have the whole messages in the socket
27743a0c675STom Herbert 				 * buffer. Set strp->rx_need_bytes to wait for
27843a0c675STom Herbert 				 * the rest of the message. Also, set "early
27943a0c675STom Herbert 				 * eaten" since we've already buffered the skb
28096a59083STom Herbert 				 * but don't consume yet per strp_read_sock.
28143a0c675STom Herbert 				 */
28243a0c675STom Herbert 
28343a0c675STom Herbert 				if (!rxm->accum_len) {
28443a0c675STom Herbert 					/* Start RX timer for new message */
28543a0c675STom Herbert 					strp_start_rx_timer(strp);
28643a0c675STom Herbert 				}
28743a0c675STom Herbert 
28843a0c675STom Herbert 				strp->rx_need_bytes = rxm->strp.full_len -
28943a0c675STom Herbert 						       rxm->accum_len;
29043a0c675STom Herbert 				rxm->accum_len += cand_len;
29143a0c675STom Herbert 				rxm->early_eaten = cand_len;
29243a0c675STom Herbert 				STRP_STATS_ADD(strp->stats.rx_bytes, cand_len);
29343a0c675STom Herbert 				desc->count = 0; /* Stop reading socket */
29443a0c675STom Herbert 				break;
29543a0c675STom Herbert 			}
29643a0c675STom Herbert 			rxm->accum_len += cand_len;
29743a0c675STom Herbert 			eaten += cand_len;
29843a0c675STom Herbert 			WARN_ON(eaten != orig_len);
29943a0c675STom Herbert 			break;
30043a0c675STom Herbert 		}
30143a0c675STom Herbert 
30243a0c675STom Herbert 		/* Positive extra indicates ore bytes than needed for the
30343a0c675STom Herbert 		 * message
30443a0c675STom Herbert 		 */
30543a0c675STom Herbert 
30643a0c675STom Herbert 		WARN_ON(extra > cand_len);
30743a0c675STom Herbert 
30843a0c675STom Herbert 		eaten += (cand_len - extra);
30943a0c675STom Herbert 
31043a0c675STom Herbert 		/* Hurray, we have a new message! */
31143a0c675STom Herbert 		del_timer(&strp->rx_msg_timer);
31243a0c675STom Herbert 		strp->rx_skb_head = NULL;
31343a0c675STom Herbert 		STRP_STATS_INCR(strp->stats.rx_msgs);
31443a0c675STom Herbert 
31543a0c675STom Herbert 		/* Give skb to upper layer */
31643a0c675STom Herbert 		strp->cb.rcv_msg(strp, head);
31743a0c675STom Herbert 
31843a0c675STom Herbert 		if (unlikely(strp->rx_paused)) {
31943a0c675STom Herbert 			/* Upper layer paused strp */
32043a0c675STom Herbert 			break;
32143a0c675STom Herbert 		}
32243a0c675STom Herbert 	}
32343a0c675STom Herbert 
32443a0c675STom Herbert 	if (cloned_orig)
32543a0c675STom Herbert 		kfree_skb(orig_skb);
32643a0c675STom Herbert 
32743a0c675STom Herbert 	STRP_STATS_ADD(strp->stats.rx_bytes, eaten);
32843a0c675STom Herbert 
32943a0c675STom Herbert 	return eaten;
33043a0c675STom Herbert }
33143a0c675STom Herbert 
33243a0c675STom Herbert static int default_read_sock_done(struct strparser *strp, int err)
33343a0c675STom Herbert {
33443a0c675STom Herbert 	return err;
33543a0c675STom Herbert }
33643a0c675STom Herbert 
33743a0c675STom Herbert /* Called with lock held on lower socket */
33896a59083STom Herbert static int strp_read_sock(struct strparser *strp)
33943a0c675STom Herbert {
34096a59083STom Herbert 	struct socket *sock = strp->sk->sk_socket;
34143a0c675STom Herbert 	read_descriptor_t desc;
34243a0c675STom Herbert 
34343a0c675STom Herbert 	desc.arg.data = strp;
34443a0c675STom Herbert 	desc.error = 0;
34543a0c675STom Herbert 	desc.count = 1; /* give more than one skb per call */
34643a0c675STom Herbert 
34796a59083STom Herbert 	/* sk should be locked here, so okay to do read_sock */
34896a59083STom Herbert 	sock->ops->read_sock(strp->sk, &desc, strp_recv);
34943a0c675STom Herbert 
35043a0c675STom Herbert 	desc.error = strp->cb.read_sock_done(strp, desc.error);
35143a0c675STom Herbert 
35243a0c675STom Herbert 	return desc.error;
35343a0c675STom Herbert }
35443a0c675STom Herbert 
35543a0c675STom Herbert /* Lower sock lock held */
35696a59083STom Herbert void strp_data_ready(struct strparser *strp)
35743a0c675STom Herbert {
35843a0c675STom Herbert 	if (unlikely(strp->rx_stopped))
35943a0c675STom Herbert 		return;
36043a0c675STom Herbert 
36143a0c675STom Herbert 	/* This check is needed to synchronize with do_strp_rx_work.
36243a0c675STom Herbert 	 * do_strp_rx_work acquires a process lock (lock_sock) whereas
36343a0c675STom Herbert 	 * the lock held here is bh_lock_sock. The two locks can be
36443a0c675STom Herbert 	 * held by different threads at the same time, but bh_lock_sock
36543a0c675STom Herbert 	 * allows a thread in BH context to safely check if the process
36643a0c675STom Herbert 	 * lock is held. In this case, if the lock is held, queue work.
36743a0c675STom Herbert 	 */
36896a59083STom Herbert 	if (sock_owned_by_user(strp->sk)) {
36943a0c675STom Herbert 		queue_work(strp_wq, &strp->rx_work);
37043a0c675STom Herbert 		return;
37143a0c675STom Herbert 	}
37243a0c675STom Herbert 
37343a0c675STom Herbert 	if (strp->rx_paused)
37443a0c675STom Herbert 		return;
37543a0c675STom Herbert 
37643a0c675STom Herbert 	if (strp->rx_need_bytes) {
37796a59083STom Herbert 		if (strp_peek_len(strp) >= strp->rx_need_bytes)
37843a0c675STom Herbert 			strp->rx_need_bytes = 0;
37943a0c675STom Herbert 		else
38043a0c675STom Herbert 			return;
38143a0c675STom Herbert 	}
38243a0c675STom Herbert 
38396a59083STom Herbert 	if (strp_read_sock(strp) == -ENOMEM)
38443a0c675STom Herbert 		queue_work(strp_wq, &strp->rx_work);
38543a0c675STom Herbert }
38696a59083STom Herbert EXPORT_SYMBOL_GPL(strp_data_ready);
38743a0c675STom Herbert 
38843a0c675STom Herbert static void do_strp_rx_work(struct strparser *strp)
38943a0c675STom Herbert {
39043a0c675STom Herbert 	read_descriptor_t rd_desc;
39143a0c675STom Herbert 	struct sock *csk = strp->sk;
39243a0c675STom Herbert 
39396a59083STom Herbert 	/* We need the read lock to synchronize with strp_data_ready. We
39496a59083STom Herbert 	 * need the socket lock for calling strp_read_sock.
39543a0c675STom Herbert 	 */
39643a0c675STom Herbert 	lock_sock(csk);
39743a0c675STom Herbert 
39843a0c675STom Herbert 	if (unlikely(strp->rx_stopped))
39943a0c675STom Herbert 		goto out;
40043a0c675STom Herbert 
40143a0c675STom Herbert 	if (strp->rx_paused)
40243a0c675STom Herbert 		goto out;
40343a0c675STom Herbert 
40443a0c675STom Herbert 	rd_desc.arg.data = strp;
40543a0c675STom Herbert 
40696a59083STom Herbert 	if (strp_read_sock(strp) == -ENOMEM)
40743a0c675STom Herbert 		queue_work(strp_wq, &strp->rx_work);
40843a0c675STom Herbert 
40943a0c675STom Herbert out:
41043a0c675STom Herbert 	release_sock(csk);
41143a0c675STom Herbert }
41243a0c675STom Herbert 
41343a0c675STom Herbert static void strp_rx_work(struct work_struct *w)
41443a0c675STom Herbert {
41543a0c675STom Herbert 	do_strp_rx_work(container_of(w, struct strparser, rx_work));
41643a0c675STom Herbert }
41743a0c675STom Herbert 
41843a0c675STom Herbert static void strp_rx_msg_timeout(unsigned long arg)
41943a0c675STom Herbert {
42043a0c675STom Herbert 	struct strparser *strp = (struct strparser *)arg;
42143a0c675STom Herbert 
42243a0c675STom Herbert 	/* Message assembly timed out */
42343a0c675STom Herbert 	STRP_STATS_INCR(strp->stats.rx_msg_timeouts);
42443a0c675STom Herbert 	lock_sock(strp->sk);
42543a0c675STom Herbert 	strp->cb.abort_parser(strp, ETIMEDOUT);
42643a0c675STom Herbert 	release_sock(strp->sk);
42743a0c675STom Herbert }
42843a0c675STom Herbert 
42943a0c675STom Herbert int strp_init(struct strparser *strp, struct sock *csk,
43043a0c675STom Herbert 	      struct strp_callbacks *cb)
43143a0c675STom Herbert {
43296a59083STom Herbert 	struct socket *sock = csk->sk_socket;
43396a59083STom Herbert 
43443a0c675STom Herbert 	if (!cb || !cb->rcv_msg || !cb->parse_msg)
43543a0c675STom Herbert 		return -EINVAL;
43643a0c675STom Herbert 
43796a59083STom Herbert 	if (!sock->ops->read_sock || !sock->ops->peek_len)
43896a59083STom Herbert 		return -EAFNOSUPPORT;
43996a59083STom Herbert 
44043a0c675STom Herbert 	memset(strp, 0, sizeof(*strp));
44143a0c675STom Herbert 
44243a0c675STom Herbert 	strp->sk = csk;
44343a0c675STom Herbert 
44443a0c675STom Herbert 	setup_timer(&strp->rx_msg_timer, strp_rx_msg_timeout,
44543a0c675STom Herbert 		    (unsigned long)strp);
44643a0c675STom Herbert 
44743a0c675STom Herbert 	INIT_WORK(&strp->rx_work, strp_rx_work);
44843a0c675STom Herbert 
44943a0c675STom Herbert 	strp->cb.rcv_msg = cb->rcv_msg;
45043a0c675STom Herbert 	strp->cb.parse_msg = cb->parse_msg;
45143a0c675STom Herbert 	strp->cb.read_sock_done = cb->read_sock_done ? : default_read_sock_done;
45243a0c675STom Herbert 	strp->cb.abort_parser = cb->abort_parser ? : strp_abort_rx_strp;
45343a0c675STom Herbert 
45443a0c675STom Herbert 	return 0;
45543a0c675STom Herbert }
45643a0c675STom Herbert EXPORT_SYMBOL_GPL(strp_init);
45743a0c675STom Herbert 
458cff6a334STom Herbert void strp_unpause(struct strparser *strp)
459cff6a334STom Herbert {
460cff6a334STom Herbert 	strp->rx_paused = 0;
461cff6a334STom Herbert 
462cff6a334STom Herbert 	/* Sync setting rx_paused with RX work */
463cff6a334STom Herbert 	smp_mb();
464cff6a334STom Herbert 
465cff6a334STom Herbert 	queue_work(strp_wq, &strp->rx_work);
466cff6a334STom Herbert }
467cff6a334STom Herbert EXPORT_SYMBOL_GPL(strp_unpause);
468cff6a334STom Herbert 
46996a59083STom Herbert /* strp must already be stopped so that strp_recv will no longer be called.
47043a0c675STom Herbert  * Note that strp_done is not called with the lower socket held.
47143a0c675STom Herbert  */
47243a0c675STom Herbert void strp_done(struct strparser *strp)
47343a0c675STom Herbert {
47443a0c675STom Herbert 	WARN_ON(!strp->rx_stopped);
47543a0c675STom Herbert 
47643a0c675STom Herbert 	del_timer_sync(&strp->rx_msg_timer);
47743a0c675STom Herbert 	cancel_work_sync(&strp->rx_work);
47843a0c675STom Herbert 
47943a0c675STom Herbert 	if (strp->rx_skb_head) {
48043a0c675STom Herbert 		kfree_skb(strp->rx_skb_head);
48143a0c675STom Herbert 		strp->rx_skb_head = NULL;
48243a0c675STom Herbert 	}
48343a0c675STom Herbert }
48443a0c675STom Herbert EXPORT_SYMBOL_GPL(strp_done);
48543a0c675STom Herbert 
48643a0c675STom Herbert void strp_stop(struct strparser *strp)
48743a0c675STom Herbert {
48843a0c675STom Herbert 	strp->rx_stopped = 1;
48943a0c675STom Herbert }
49043a0c675STom Herbert EXPORT_SYMBOL_GPL(strp_stop);
49143a0c675STom Herbert 
49243a0c675STom Herbert void strp_check_rcv(struct strparser *strp)
49343a0c675STom Herbert {
49443a0c675STom Herbert 	queue_work(strp_wq, &strp->rx_work);
49543a0c675STom Herbert }
49643a0c675STom Herbert EXPORT_SYMBOL_GPL(strp_check_rcv);
49743a0c675STom Herbert 
49843a0c675STom Herbert static int __init strp_mod_init(void)
49943a0c675STom Herbert {
50043a0c675STom Herbert 	strp_wq = create_singlethread_workqueue("kstrp");
50143a0c675STom Herbert 
50243a0c675STom Herbert 	return 0;
50343a0c675STom Herbert }
50443a0c675STom Herbert 
50543a0c675STom Herbert static void __exit strp_mod_exit(void)
50643a0c675STom Herbert {
507*f78ef7cdSWANG Cong 	destroy_workqueue(strp_wq);
50843a0c675STom Herbert }
50943a0c675STom Herbert module_init(strp_mod_init);
51043a0c675STom Herbert module_exit(strp_mod_exit);
51143a0c675STom Herbert MODULE_LICENSE("GPL");
512