xref: /linux/net/strparser/strparser.c (revision 2d91ecac)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
243a0c675STom Herbert /*
343a0c675STom Herbert  * Stream Parser
443a0c675STom Herbert  *
543a0c675STom Herbert  * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
643a0c675STom Herbert  */
743a0c675STom Herbert 
843a0c675STom Herbert #include <linux/bpf.h>
943a0c675STom Herbert #include <linux/errno.h>
1043a0c675STom Herbert #include <linux/errqueue.h>
1143a0c675STom Herbert #include <linux/file.h>
1243a0c675STom Herbert #include <linux/in.h>
1343a0c675STom Herbert #include <linux/kernel.h>
1415253b4aSPaul Gortmaker #include <linux/export.h>
1515253b4aSPaul Gortmaker #include <linux/init.h>
1643a0c675STom Herbert #include <linux/net.h>
1743a0c675STom Herbert #include <linux/netdevice.h>
1843a0c675STom Herbert #include <linux/poll.h>
1943a0c675STom Herbert #include <linux/rculist.h>
2043a0c675STom Herbert #include <linux/skbuff.h>
2143a0c675STom Herbert #include <linux/socket.h>
2243a0c675STom Herbert #include <linux/uaccess.h>
2343a0c675STom Herbert #include <linux/workqueue.h>
2443a0c675STom Herbert #include <net/strparser.h>
2543a0c675STom Herbert #include <net/netns/generic.h>
2643a0c675STom Herbert #include <net/sock.h>
2743a0c675STom Herbert 
2843a0c675STom Herbert static struct workqueue_struct *strp_wq;
2943a0c675STom Herbert 
_strp_msg(struct sk_buff * skb)30bbb03029STom Herbert static inline struct _strp_msg *_strp_msg(struct sk_buff *skb)
3143a0c675STom Herbert {
32bbb03029STom Herbert 	return (struct _strp_msg *)((void *)skb->cb +
33e0dc3b93SJohn Fastabend 		offsetof(struct sk_skb_cb, strp));
3443a0c675STom Herbert }
3543a0c675STom Herbert 
3643a0c675STom Herbert /* Lower lock held */
strp_abort_strp(struct strparser * strp,int err)37bbb03029STom Herbert static void strp_abort_strp(struct strparser *strp, int err)
3843a0c675STom Herbert {
3943a0c675STom Herbert 	/* Unrecoverable error in receive */
4043a0c675STom Herbert 
41829385f0STom Herbert 	cancel_delayed_work(&strp->msg_timer_work);
4243a0c675STom Herbert 
43bbb03029STom Herbert 	if (strp->stopped)
4443a0c675STom Herbert 		return;
4543a0c675STom Herbert 
46bbb03029STom Herbert 	strp->stopped = 1;
47bbb03029STom Herbert 
48bbb03029STom Herbert 	if (strp->sk) {
49bbb03029STom Herbert 		struct sock *sk = strp->sk;
5043a0c675STom Herbert 
5143a0c675STom Herbert 		/* Report an error on the lower socket */
52cd00edc1SDave Watson 		sk->sk_err = -err;
53e3ae2365SAlexander Aring 		sk_error_report(sk);
54bbb03029STom Herbert 	}
5543a0c675STom Herbert }
5643a0c675STom Herbert 
strp_start_timer(struct strparser * strp,long timeo)57bbb03029STom Herbert static void strp_start_timer(struct strparser *strp, long timeo)
5843a0c675STom Herbert {
597c5aba21SDoron Roberts-Kedes 	if (timeo && timeo != LONG_MAX)
60829385f0STom Herbert 		mod_delayed_work(strp_wq, &strp->msg_timer_work, timeo);
6143a0c675STom Herbert }
6243a0c675STom Herbert 
6343a0c675STom Herbert /* Lower lock held */
strp_parser_err(struct strparser * strp,int err,read_descriptor_t * desc)6443a0c675STom Herbert static void strp_parser_err(struct strparser *strp, int err,
6543a0c675STom Herbert 			    read_descriptor_t *desc)
6643a0c675STom Herbert {
6743a0c675STom Herbert 	desc->error = err;
68bbb03029STom Herbert 	kfree_skb(strp->skb_head);
69bbb03029STom Herbert 	strp->skb_head = NULL;
7043a0c675STom Herbert 	strp->cb.abort_parser(strp, err);
7143a0c675STom Herbert }
7243a0c675STom Herbert 
strp_peek_len(struct strparser * strp)7396a59083STom Herbert static inline int strp_peek_len(struct strparser *strp)
7496a59083STom Herbert {
75bbb03029STom Herbert 	if (strp->sk) {
7696a59083STom Herbert 		struct socket *sock = strp->sk->sk_socket;
7796a59083STom Herbert 
7896a59083STom Herbert 		return sock->ops->peek_len(sock);
7996a59083STom Herbert 	}
8096a59083STom Herbert 
81bbb03029STom Herbert 	/* If we don't have an associated socket there's nothing to peek.
82bbb03029STom Herbert 	 * Return int max to avoid stopping the strparser.
83bbb03029STom Herbert 	 */
84bbb03029STom Herbert 
85bbb03029STom Herbert 	return INT_MAX;
86bbb03029STom Herbert }
87bbb03029STom Herbert 
8843a0c675STom Herbert /* Lower socket lock held */
__strp_recv(read_descriptor_t * desc,struct sk_buff * orig_skb,unsigned int orig_offset,size_t orig_len,size_t max_msg_size,long timeo)89bbb03029STom Herbert static int __strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
90bbb03029STom Herbert 		       unsigned int orig_offset, size_t orig_len,
91bbb03029STom Herbert 		       size_t max_msg_size, long timeo)
9243a0c675STom Herbert {
9343a0c675STom Herbert 	struct strparser *strp = (struct strparser *)desc->arg.data;
94bbb03029STom Herbert 	struct _strp_msg *stm;
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 
101bbb03029STom Herbert 	if (strp->paused)
10243a0c675STom Herbert 		return 0;
10343a0c675STom Herbert 
104bbb03029STom Herbert 	head = strp->skb_head;
10543a0c675STom Herbert 	if (head) {
10643a0c675STom Herbert 		/* Message already in progress */
10743a0c675STom Herbert 		if (unlikely(orig_offset)) {
10843a0c675STom Herbert 			/* Getting data with a non-zero offset when a message is
10943a0c675STom Herbert 			 * in progress is not expected. If it does happen, we
11043a0c675STom Herbert 			 * need to clone and pull since we can't deal with
11143a0c675STom Herbert 			 * offsets in the skbs for a message expect in the head.
11243a0c675STom Herbert 			 */
11343a0c675STom Herbert 			orig_skb = skb_clone(orig_skb, GFP_ATOMIC);
11443a0c675STom Herbert 			if (!orig_skb) {
115bbb03029STom Herbert 				STRP_STATS_INCR(strp->stats.mem_fail);
11643a0c675STom Herbert 				desc->error = -ENOMEM;
11743a0c675STom Herbert 				return 0;
11843a0c675STom Herbert 			}
11943a0c675STom Herbert 			if (!pskb_pull(orig_skb, orig_offset)) {
120bbb03029STom Herbert 				STRP_STATS_INCR(strp->stats.mem_fail);
12143a0c675STom Herbert 				kfree_skb(orig_skb);
12243a0c675STom Herbert 				desc->error = -ENOMEM;
12343a0c675STom Herbert 				return 0;
12443a0c675STom Herbert 			}
12543a0c675STom Herbert 			cloned_orig = true;
12643a0c675STom Herbert 			orig_offset = 0;
12743a0c675STom Herbert 		}
12843a0c675STom Herbert 
129bbb03029STom Herbert 		if (!strp->skb_nextp) {
13043a0c675STom Herbert 			/* We are going to append to the frags_list of head.
13143a0c675STom Herbert 			 * Need to unshare the frag_list.
13243a0c675STom Herbert 			 */
13343a0c675STom Herbert 			err = skb_unclone(head, GFP_ATOMIC);
13443a0c675STom Herbert 			if (err) {
135bbb03029STom Herbert 				STRP_STATS_INCR(strp->stats.mem_fail);
13643a0c675STom Herbert 				desc->error = err;
13743a0c675STom Herbert 				return 0;
13843a0c675STom Herbert 			}
13943a0c675STom Herbert 
14043a0c675STom Herbert 			if (unlikely(skb_shinfo(head)->frag_list)) {
14143a0c675STom Herbert 				/* We can't append to an sk_buff that already
14243a0c675STom Herbert 				 * has a frag_list. We create a new head, point
14343a0c675STom Herbert 				 * the frag_list of that to the old head, and
14443a0c675STom Herbert 				 * then are able to use the old head->next for
14543a0c675STom Herbert 				 * appending to the message.
14643a0c675STom Herbert 				 */
14743a0c675STom Herbert 				if (WARN_ON(head->next)) {
14843a0c675STom Herbert 					desc->error = -EINVAL;
14943a0c675STom Herbert 					return 0;
15043a0c675STom Herbert 				}
15143a0c675STom Herbert 
152da29e4b4SJakub Kicinski 				skb = alloc_skb_for_msg(head);
15343a0c675STom Herbert 				if (!skb) {
154bbb03029STom Herbert 					STRP_STATS_INCR(strp->stats.mem_fail);
15543a0c675STom Herbert 					desc->error = -ENOMEM;
15643a0c675STom Herbert 					return 0;
15743a0c675STom Herbert 				}
158da29e4b4SJakub Kicinski 
159bbb03029STom Herbert 				strp->skb_nextp = &head->next;
160bbb03029STom Herbert 				strp->skb_head = skb;
16143a0c675STom Herbert 				head = skb;
16243a0c675STom Herbert 			} else {
163bbb03029STom Herbert 				strp->skb_nextp =
16443a0c675STom Herbert 				    &skb_shinfo(head)->frag_list;
16543a0c675STom Herbert 			}
16643a0c675STom Herbert 		}
16743a0c675STom Herbert 	}
16843a0c675STom Herbert 
16943a0c675STom Herbert 	while (eaten < orig_len) {
17043a0c675STom Herbert 		/* Always clone since we will consume something */
17143a0c675STom Herbert 		skb = skb_clone(orig_skb, GFP_ATOMIC);
17243a0c675STom Herbert 		if (!skb) {
173bbb03029STom Herbert 			STRP_STATS_INCR(strp->stats.mem_fail);
17443a0c675STom Herbert 			desc->error = -ENOMEM;
17543a0c675STom Herbert 			break;
17643a0c675STom Herbert 		}
17743a0c675STom Herbert 
17843a0c675STom Herbert 		cand_len = orig_len - eaten;
17943a0c675STom Herbert 
180bbb03029STom Herbert 		head = strp->skb_head;
18143a0c675STom Herbert 		if (!head) {
18243a0c675STom Herbert 			head = skb;
183bbb03029STom Herbert 			strp->skb_head = head;
184bbb03029STom Herbert 			/* Will set skb_nextp on next packet if needed */
185bbb03029STom Herbert 			strp->skb_nextp = NULL;
186bbb03029STom Herbert 			stm = _strp_msg(head);
187bbb03029STom Herbert 			memset(stm, 0, sizeof(*stm));
188bbb03029STom Herbert 			stm->strp.offset = orig_offset + eaten;
18943a0c675STom Herbert 		} else {
1904e485d06SVakul Garg 			/* Unclone if we are appending to an skb that we
19143a0c675STom Herbert 			 * already share a frag_list with.
19243a0c675STom Herbert 			 */
1934e485d06SVakul Garg 			if (skb_has_frag_list(skb)) {
19443a0c675STom Herbert 				err = skb_unclone(skb, GFP_ATOMIC);
19543a0c675STom Herbert 				if (err) {
196bbb03029STom Herbert 					STRP_STATS_INCR(strp->stats.mem_fail);
19743a0c675STom Herbert 					desc->error = err;
19843a0c675STom Herbert 					break;
19943a0c675STom Herbert 				}
2004e485d06SVakul Garg 			}
20143a0c675STom Herbert 
202bbb03029STom Herbert 			stm = _strp_msg(head);
203bbb03029STom Herbert 			*strp->skb_nextp = skb;
204bbb03029STom Herbert 			strp->skb_nextp = &skb->next;
20543a0c675STom Herbert 			head->data_len += skb->len;
20643a0c675STom Herbert 			head->len += skb->len;
20743a0c675STom Herbert 			head->truesize += skb->truesize;
20843a0c675STom Herbert 		}
20943a0c675STom Herbert 
210bbb03029STom Herbert 		if (!stm->strp.full_len) {
21143a0c675STom Herbert 			ssize_t len;
21243a0c675STom Herbert 
21343a0c675STom Herbert 			len = (*strp->cb.parse_msg)(strp, head);
21443a0c675STom Herbert 
21543a0c675STom Herbert 			if (!len) {
21643a0c675STom Herbert 				/* Need more header to determine length */
217bbb03029STom Herbert 				if (!stm->accum_len) {
21843a0c675STom Herbert 					/* Start RX timer for new message */
219bbb03029STom Herbert 					strp_start_timer(strp, timeo);
22043a0c675STom Herbert 				}
221bbb03029STom Herbert 				stm->accum_len += cand_len;
22243a0c675STom Herbert 				eaten += cand_len;
223bbb03029STom Herbert 				STRP_STATS_INCR(strp->stats.need_more_hdr);
22443a0c675STom Herbert 				WARN_ON(eaten != orig_len);
22543a0c675STom Herbert 				break;
22643a0c675STom Herbert 			} else if (len < 0) {
227bbb03029STom Herbert 				if (len == -ESTRPIPE && stm->accum_len) {
22843a0c675STom Herbert 					len = -ENODATA;
229bbb03029STom Herbert 					strp->unrecov_intr = 1;
23043a0c675STom Herbert 				} else {
231bbb03029STom Herbert 					strp->interrupted = 1;
23243a0c675STom Herbert 				}
2336d3a4c40SGeert Uytterhoeven 				strp_parser_err(strp, len, desc);
23443a0c675STom Herbert 				break;
235bbb03029STom Herbert 			} else if (len > max_msg_size) {
23643a0c675STom Herbert 				/* Message length exceeds maximum allowed */
237bbb03029STom Herbert 				STRP_STATS_INCR(strp->stats.msg_too_big);
23843a0c675STom Herbert 				strp_parser_err(strp, -EMSGSIZE, desc);
23943a0c675STom Herbert 				break;
24043a0c675STom Herbert 			} else if (len <= (ssize_t)head->len -
241bbb03029STom Herbert 					  skb->len - stm->strp.offset) {
24243a0c675STom Herbert 				/* Length must be into new skb (and also
24343a0c675STom Herbert 				 * greater than zero)
24443a0c675STom Herbert 				 */
245bbb03029STom Herbert 				STRP_STATS_INCR(strp->stats.bad_hdr_len);
24643a0c675STom Herbert 				strp_parser_err(strp, -EPROTO, desc);
24743a0c675STom Herbert 				break;
24843a0c675STom Herbert 			}
24943a0c675STom Herbert 
250bbb03029STom Herbert 			stm->strp.full_len = len;
25143a0c675STom Herbert 		}
25243a0c675STom Herbert 
253bbb03029STom Herbert 		extra = (ssize_t)(stm->accum_len + cand_len) -
254bbb03029STom Herbert 			stm->strp.full_len;
25543a0c675STom Herbert 
25643a0c675STom Herbert 		if (extra < 0) {
25743a0c675STom Herbert 			/* Message not complete yet. */
258bbb03029STom Herbert 			if (stm->strp.full_len - stm->accum_len >
25996a59083STom Herbert 			    strp_peek_len(strp)) {
260bbb03029STom Herbert 				/* Don't have the whole message in the socket
261bbb03029STom Herbert 				 * buffer. Set strp->need_bytes to wait for
26243a0c675STom Herbert 				 * the rest of the message. Also, set "early
26343a0c675STom Herbert 				 * eaten" since we've already buffered the skb
26496a59083STom Herbert 				 * but don't consume yet per strp_read_sock.
26543a0c675STom Herbert 				 */
26643a0c675STom Herbert 
267bbb03029STom Herbert 				if (!stm->accum_len) {
26843a0c675STom Herbert 					/* Start RX timer for new message */
269bbb03029STom Herbert 					strp_start_timer(strp, timeo);
27043a0c675STom Herbert 				}
27143a0c675STom Herbert 
2729d0c75bfSDoron Roberts-Kedes 				stm->accum_len += cand_len;
273977c7114SDoron Roberts-Kedes 				eaten += cand_len;
274bbb03029STom Herbert 				strp->need_bytes = stm->strp.full_len -
275bbb03029STom Herbert 						       stm->accum_len;
276bbb03029STom Herbert 				STRP_STATS_ADD(strp->stats.bytes, cand_len);
27743a0c675STom Herbert 				desc->count = 0; /* Stop reading socket */
27843a0c675STom Herbert 				break;
27943a0c675STom Herbert 			}
280bbb03029STom Herbert 			stm->accum_len += cand_len;
28143a0c675STom Herbert 			eaten += cand_len;
28243a0c675STom Herbert 			WARN_ON(eaten != orig_len);
28343a0c675STom Herbert 			break;
28443a0c675STom Herbert 		}
28543a0c675STom Herbert 
28693e21254SJakub Kicinski 		/* Positive extra indicates more bytes than needed for the
28743a0c675STom Herbert 		 * message
28843a0c675STom Herbert 		 */
28943a0c675STom Herbert 
29043a0c675STom Herbert 		WARN_ON(extra > cand_len);
29143a0c675STom Herbert 
29243a0c675STom Herbert 		eaten += (cand_len - extra);
29343a0c675STom Herbert 
29443a0c675STom Herbert 		/* Hurray, we have a new message! */
295829385f0STom Herbert 		cancel_delayed_work(&strp->msg_timer_work);
296bbb03029STom Herbert 		strp->skb_head = NULL;
2979d0c75bfSDoron Roberts-Kedes 		strp->need_bytes = 0;
298bbb03029STom Herbert 		STRP_STATS_INCR(strp->stats.msgs);
29943a0c675STom Herbert 
30043a0c675STom Herbert 		/* Give skb to upper layer */
30143a0c675STom Herbert 		strp->cb.rcv_msg(strp, head);
30243a0c675STom Herbert 
303bbb03029STom Herbert 		if (unlikely(strp->paused)) {
30443a0c675STom Herbert 			/* Upper layer paused strp */
30543a0c675STom Herbert 			break;
30643a0c675STom Herbert 		}
30743a0c675STom Herbert 	}
30843a0c675STom Herbert 
30943a0c675STom Herbert 	if (cloned_orig)
31043a0c675STom Herbert 		kfree_skb(orig_skb);
31143a0c675STom Herbert 
312bbb03029STom Herbert 	STRP_STATS_ADD(strp->stats.bytes, eaten);
31343a0c675STom Herbert 
31443a0c675STom Herbert 	return eaten;
31543a0c675STom Herbert }
31643a0c675STom Herbert 
strp_process(struct strparser * strp,struct sk_buff * orig_skb,unsigned int orig_offset,size_t orig_len,size_t max_msg_size,long timeo)317bbb03029STom Herbert int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
318bbb03029STom Herbert 		 unsigned int orig_offset, size_t orig_len,
319bbb03029STom Herbert 		 size_t max_msg_size, long timeo)
320bbb03029STom Herbert {
321bbb03029STom Herbert 	read_descriptor_t desc; /* Dummy arg to strp_recv */
322bbb03029STom Herbert 
323bbb03029STom Herbert 	desc.arg.data = strp;
324bbb03029STom Herbert 
325bbb03029STom Herbert 	return __strp_recv(&desc, orig_skb, orig_offset, orig_len,
326bbb03029STom Herbert 			   max_msg_size, timeo);
327bbb03029STom Herbert }
328bbb03029STom Herbert EXPORT_SYMBOL_GPL(strp_process);
329bbb03029STom Herbert 
strp_recv(read_descriptor_t * desc,struct sk_buff * orig_skb,unsigned int orig_offset,size_t orig_len)330bbb03029STom Herbert static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
331bbb03029STom Herbert 		     unsigned int orig_offset, size_t orig_len)
332bbb03029STom Herbert {
333bbb03029STom Herbert 	struct strparser *strp = (struct strparser *)desc->arg.data;
334bbb03029STom Herbert 
335bbb03029STom Herbert 	return __strp_recv(desc, orig_skb, orig_offset, orig_len,
336bbb03029STom Herbert 			   strp->sk->sk_rcvbuf, strp->sk->sk_rcvtimeo);
337bbb03029STom Herbert }
338bbb03029STom Herbert 
default_read_sock_done(struct strparser * strp,int err)33943a0c675STom Herbert static int default_read_sock_done(struct strparser *strp, int err)
34043a0c675STom Herbert {
34143a0c675STom Herbert 	return err;
34243a0c675STom Herbert }
34343a0c675STom Herbert 
34443a0c675STom Herbert /* Called with lock held on lower socket */
strp_read_sock(struct strparser * strp)34596a59083STom Herbert static int strp_read_sock(struct strparser *strp)
34643a0c675STom Herbert {
34796a59083STom Herbert 	struct socket *sock = strp->sk->sk_socket;
34843a0c675STom Herbert 	read_descriptor_t desc;
34943a0c675STom Herbert 
350f26de110SJohn Fastabend 	if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
351f26de110SJohn Fastabend 		return -EBUSY;
352f26de110SJohn Fastabend 
35343a0c675STom Herbert 	desc.arg.data = strp;
35443a0c675STom Herbert 	desc.error = 0;
35543a0c675STom Herbert 	desc.count = 1; /* give more than one skb per call */
35643a0c675STom Herbert 
35796a59083STom Herbert 	/* sk should be locked here, so okay to do read_sock */
35896a59083STom Herbert 	sock->ops->read_sock(strp->sk, &desc, strp_recv);
35943a0c675STom Herbert 
36043a0c675STom Herbert 	desc.error = strp->cb.read_sock_done(strp, desc.error);
36143a0c675STom Herbert 
36243a0c675STom Herbert 	return desc.error;
36343a0c675STom Herbert }
36443a0c675STom Herbert 
36543a0c675STom Herbert /* Lower sock lock held */
strp_data_ready(struct strparser * strp)36696a59083STom Herbert void strp_data_ready(struct strparser *strp)
36743a0c675STom Herbert {
368456488cdSVakul Garg 	if (unlikely(strp->stopped) || strp->paused)
36943a0c675STom Herbert 		return;
37043a0c675STom Herbert 
371bbb03029STom Herbert 	/* This check is needed to synchronize with do_strp_work.
372bbb03029STom Herbert 	 * do_strp_work acquires a process lock (lock_sock) whereas
37343a0c675STom Herbert 	 * the lock held here is bh_lock_sock. The two locks can be
37443a0c675STom Herbert 	 * held by different threads at the same time, but bh_lock_sock
37543a0c675STom Herbert 	 * allows a thread in BH context to safely check if the process
37643a0c675STom Herbert 	 * lock is held. In this case, if the lock is held, queue work.
37743a0c675STom Herbert 	 */
378d66fa9ecSTom Herbert 	if (sock_owned_by_user_nocheck(strp->sk)) {
379bbb03029STom Herbert 		queue_work(strp_wq, &strp->work);
38043a0c675STom Herbert 		return;
38143a0c675STom Herbert 	}
38243a0c675STom Herbert 
383bbb03029STom Herbert 	if (strp->need_bytes) {
3849d0c75bfSDoron Roberts-Kedes 		if (strp_peek_len(strp) < strp->need_bytes)
38543a0c675STom Herbert 			return;
38643a0c675STom Herbert 	}
38743a0c675STom Herbert 
38896a59083STom Herbert 	if (strp_read_sock(strp) == -ENOMEM)
389bbb03029STom Herbert 		queue_work(strp_wq, &strp->work);
39043a0c675STom Herbert }
39196a59083STom Herbert EXPORT_SYMBOL_GPL(strp_data_ready);
39243a0c675STom Herbert 
do_strp_work(struct strparser * strp)393bbb03029STom Herbert static void do_strp_work(struct strparser *strp)
39443a0c675STom Herbert {
39596a59083STom Herbert 	/* We need the read lock to synchronize with strp_data_ready. We
39696a59083STom Herbert 	 * need the socket lock for calling strp_read_sock.
39743a0c675STom Herbert 	 */
398bbb03029STom Herbert 	strp->cb.lock(strp);
39943a0c675STom Herbert 
400bbb03029STom Herbert 	if (unlikely(strp->stopped))
40143a0c675STom Herbert 		goto out;
40243a0c675STom Herbert 
403bbb03029STom Herbert 	if (strp->paused)
40443a0c675STom Herbert 		goto out;
40543a0c675STom Herbert 
40696a59083STom Herbert 	if (strp_read_sock(strp) == -ENOMEM)
407bbb03029STom Herbert 		queue_work(strp_wq, &strp->work);
40843a0c675STom Herbert 
40943a0c675STom Herbert out:
410bbb03029STom Herbert 	strp->cb.unlock(strp);
41143a0c675STom Herbert }
41243a0c675STom Herbert 
strp_work(struct work_struct * w)413bbb03029STom Herbert static void strp_work(struct work_struct *w)
41443a0c675STom Herbert {
415bbb03029STom Herbert 	do_strp_work(container_of(w, struct strparser, work));
41643a0c675STom Herbert }
41743a0c675STom Herbert 
strp_msg_timeout(struct work_struct * w)418829385f0STom Herbert static void strp_msg_timeout(struct work_struct *w)
41943a0c675STom Herbert {
420829385f0STom Herbert 	struct strparser *strp = container_of(w, struct strparser,
421829385f0STom Herbert 					      msg_timer_work.work);
42243a0c675STom Herbert 
42343a0c675STom Herbert 	/* Message assembly timed out */
424bbb03029STom Herbert 	STRP_STATS_INCR(strp->stats.msg_timeouts);
425bbb03029STom Herbert 	strp->cb.lock(strp);
426cd00edc1SDave Watson 	strp->cb.abort_parser(strp, -ETIMEDOUT);
427bbb03029STom Herbert 	strp->cb.unlock(strp);
428bbb03029STom Herbert }
429bbb03029STom Herbert 
strp_sock_lock(struct strparser * strp)430bbb03029STom Herbert static void strp_sock_lock(struct strparser *strp)
431bbb03029STom Herbert {
432bbb03029STom Herbert 	lock_sock(strp->sk);
433bbb03029STom Herbert }
434bbb03029STom Herbert 
strp_sock_unlock(struct strparser * strp)435bbb03029STom Herbert static void strp_sock_unlock(struct strparser *strp)
436bbb03029STom Herbert {
43743a0c675STom Herbert 	release_sock(strp->sk);
43843a0c675STom Herbert }
43943a0c675STom Herbert 
strp_init(struct strparser * strp,struct sock * sk,const struct strp_callbacks * cb)440bbb03029STom Herbert int strp_init(struct strparser *strp, struct sock *sk,
4413fd87127SEric Biggers 	      const struct strp_callbacks *cb)
44243a0c675STom Herbert {
44396a59083STom Herbert 
44443a0c675STom Herbert 	if (!cb || !cb->rcv_msg || !cb->parse_msg)
44543a0c675STom Herbert 		return -EINVAL;
44643a0c675STom Herbert 
447bbb03029STom Herbert 	/* The sk (sock) arg determines the mode of the stream parser.
448bbb03029STom Herbert 	 *
449bbb03029STom Herbert 	 * If the sock is set then the strparser is in receive callback mode.
450bbb03029STom Herbert 	 * The upper layer calls strp_data_ready to kick receive processing
451bbb03029STom Herbert 	 * and strparser calls the read_sock function on the socket to
452bbb03029STom Herbert 	 * get packets.
453bbb03029STom Herbert 	 *
454bbb03029STom Herbert 	 * If the sock is not set then the strparser is in general mode.
455bbb03029STom Herbert 	 * The upper layer calls strp_process for each skb to be parsed.
456bbb03029STom Herbert 	 */
457bbb03029STom Herbert 
458f26de110SJohn Fastabend 	if (!sk) {
459bbb03029STom Herbert 		if (!cb->lock || !cb->unlock)
460bbb03029STom Herbert 			return -EINVAL;
461bbb03029STom Herbert 	}
46296a59083STom Herbert 
46343a0c675STom Herbert 	memset(strp, 0, sizeof(*strp));
46443a0c675STom Herbert 
465bbb03029STom Herbert 	strp->sk = sk;
46643a0c675STom Herbert 
467bbb03029STom Herbert 	strp->cb.lock = cb->lock ? : strp_sock_lock;
468bbb03029STom Herbert 	strp->cb.unlock = cb->unlock ? : strp_sock_unlock;
46943a0c675STom Herbert 	strp->cb.rcv_msg = cb->rcv_msg;
47043a0c675STom Herbert 	strp->cb.parse_msg = cb->parse_msg;
47143a0c675STom Herbert 	strp->cb.read_sock_done = cb->read_sock_done ? : default_read_sock_done;
472bbb03029STom Herbert 	strp->cb.abort_parser = cb->abort_parser ? : strp_abort_strp;
473bbb03029STom Herbert 
474829385f0STom Herbert 	INIT_DELAYED_WORK(&strp->msg_timer_work, strp_msg_timeout);
475bbb03029STom Herbert 	INIT_WORK(&strp->work, strp_work);
47643a0c675STom Herbert 
47743a0c675STom Herbert 	return 0;
47843a0c675STom Herbert }
47943a0c675STom Herbert EXPORT_SYMBOL_GPL(strp_init);
48043a0c675STom Herbert 
4817170e604SDoron Roberts-Kedes /* Sock process lock held (lock_sock) */
__strp_unpause(struct strparser * strp)4827170e604SDoron Roberts-Kedes void __strp_unpause(struct strparser *strp)
4837170e604SDoron Roberts-Kedes {
4847170e604SDoron Roberts-Kedes 	strp->paused = 0;
4857170e604SDoron Roberts-Kedes 
4867170e604SDoron Roberts-Kedes 	if (strp->need_bytes) {
4877170e604SDoron Roberts-Kedes 		if (strp_peek_len(strp) < strp->need_bytes)
4887170e604SDoron Roberts-Kedes 			return;
4897170e604SDoron Roberts-Kedes 	}
4907170e604SDoron Roberts-Kedes 	strp_read_sock(strp);
4917170e604SDoron Roberts-Kedes }
4927170e604SDoron Roberts-Kedes EXPORT_SYMBOL_GPL(__strp_unpause);
4937170e604SDoron Roberts-Kedes 
strp_unpause(struct strparser * strp)494cff6a334STom Herbert void strp_unpause(struct strparser *strp)
495cff6a334STom Herbert {
496bbb03029STom Herbert 	strp->paused = 0;
497cff6a334STom Herbert 
498bbb03029STom Herbert 	/* Sync setting paused with RX work */
499cff6a334STom Herbert 	smp_mb();
500cff6a334STom Herbert 
501bbb03029STom Herbert 	queue_work(strp_wq, &strp->work);
502cff6a334STom Herbert }
503cff6a334STom Herbert EXPORT_SYMBOL_GPL(strp_unpause);
504cff6a334STom Herbert 
50596a59083STom Herbert /* strp must already be stopped so that strp_recv will no longer be called.
50643a0c675STom Herbert  * Note that strp_done is not called with the lower socket held.
50743a0c675STom Herbert  */
strp_done(struct strparser * strp)50843a0c675STom Herbert void strp_done(struct strparser *strp)
50943a0c675STom Herbert {
510bbb03029STom Herbert 	WARN_ON(!strp->stopped);
51143a0c675STom Herbert 
512829385f0STom Herbert 	cancel_delayed_work_sync(&strp->msg_timer_work);
513bbb03029STom Herbert 	cancel_work_sync(&strp->work);
51443a0c675STom Herbert 
515bbb03029STom Herbert 	if (strp->skb_head) {
516bbb03029STom Herbert 		kfree_skb(strp->skb_head);
517bbb03029STom Herbert 		strp->skb_head = NULL;
51843a0c675STom Herbert 	}
51943a0c675STom Herbert }
52043a0c675STom Herbert EXPORT_SYMBOL_GPL(strp_done);
52143a0c675STom Herbert 
strp_stop(struct strparser * strp)52243a0c675STom Herbert void strp_stop(struct strparser *strp)
52343a0c675STom Herbert {
524bbb03029STom Herbert 	strp->stopped = 1;
52543a0c675STom Herbert }
52643a0c675STom Herbert EXPORT_SYMBOL_GPL(strp_stop);
52743a0c675STom Herbert 
strp_check_rcv(struct strparser * strp)52843a0c675STom Herbert void strp_check_rcv(struct strparser *strp)
52943a0c675STom Herbert {
530bbb03029STom Herbert 	queue_work(strp_wq, &strp->work);
53143a0c675STom Herbert }
53243a0c675STom Herbert EXPORT_SYMBOL_GPL(strp_check_rcv);
53343a0c675STom Herbert 
strp_dev_init(void)53415253b4aSPaul Gortmaker static int __init strp_dev_init(void)
53543a0c675STom Herbert {
536*2d91ecacSJakub Kicinski 	BUILD_BUG_ON(sizeof(struct sk_skb_cb) >
537*2d91ecacSJakub Kicinski 		     sizeof_field(struct sk_buff, cb));
538*2d91ecacSJakub Kicinski 
53943a0c675STom Herbert 	strp_wq = create_singlethread_workqueue("kstrp");
540228cd2dbSKangjie Lu 	if (unlikely(!strp_wq))
541228cd2dbSKangjie Lu 		return -ENOMEM;
54243a0c675STom Herbert 
54343a0c675STom Herbert 	return 0;
54443a0c675STom Herbert }
54515253b4aSPaul Gortmaker device_initcall(strp_dev_init);
546