blob: 1a72c67afed5e6acedee4eed9111b0a2ba07e5be [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Tom Herbert43a0c672016-08-15 14:51:01 -07002/*
3 * Stream Parser
4 *
5 * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
Tom Herbert43a0c672016-08-15 14:51:01 -07006 */
7
8#include <linux/bpf.h>
9#include <linux/errno.h>
10#include <linux/errqueue.h>
11#include <linux/file.h>
12#include <linux/in.h>
13#include <linux/kernel.h>
Paul Gortmaker15253b42019-04-20 23:29:48 -040014#include <linux/export.h>
15#include <linux/init.h>
Tom Herbert43a0c672016-08-15 14:51:01 -070016#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>
26#include <net/sock.h>
Tom Herbert43a0c672016-08-15 14:51:01 -070027
28static struct workqueue_struct *strp_wq;
29
Tom Herbertbbb03022017-07-28 16:22:43 -070030static inline struct _strp_msg *_strp_msg(struct sk_buff *skb)
Tom Herbert43a0c672016-08-15 14:51:01 -070031{
Tom Herbertbbb03022017-07-28 16:22:43 -070032 return (struct _strp_msg *)((void *)skb->cb +
John Fastabende0dc3b92021-11-03 13:47:35 -070033 offsetof(struct sk_skb_cb, strp));
Tom Herbert43a0c672016-08-15 14:51:01 -070034}
35
36/* Lower lock held */
Tom Herbertbbb03022017-07-28 16:22:43 -070037static void strp_abort_strp(struct strparser *strp, int err)
Tom Herbert43a0c672016-08-15 14:51:01 -070038{
Tom Herbert43a0c672016-08-15 14:51:01 -070039 /* Unrecoverable error in receive */
40
Tom Herbert829385f2017-10-20 16:40:43 -070041 cancel_delayed_work(&strp->msg_timer_work);
Tom Herbert43a0c672016-08-15 14:51:01 -070042
Tom Herbertbbb03022017-07-28 16:22:43 -070043 if (strp->stopped)
Tom Herbert43a0c672016-08-15 14:51:01 -070044 return;
45
Tom Herbertbbb03022017-07-28 16:22:43 -070046 strp->stopped = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -070047
Tom Herbertbbb03022017-07-28 16:22:43 -070048 if (strp->sk) {
49 struct sock *sk = strp->sk;
50
51 /* Report an error on the lower socket */
Dave Watsoncd00edc2018-03-26 12:31:21 -070052 sk->sk_err = -err;
Alexander Aringe3ae2362021-06-27 18:48:21 -040053 sk_error_report(sk);
Tom Herbertbbb03022017-07-28 16:22:43 -070054 }
Tom Herbert43a0c672016-08-15 14:51:01 -070055}
56
Tom Herbertbbb03022017-07-28 16:22:43 -070057static void strp_start_timer(struct strparser *strp, long timeo)
Tom Herbert43a0c672016-08-15 14:51:01 -070058{
Doron Roberts-Kedes7c5aba22018-04-20 12:11:11 -070059 if (timeo && timeo != LONG_MAX)
Tom Herbert829385f2017-10-20 16:40:43 -070060 mod_delayed_work(strp_wq, &strp->msg_timer_work, timeo);
Tom Herbert43a0c672016-08-15 14:51:01 -070061}
62
63/* Lower lock held */
64static void strp_parser_err(struct strparser *strp, int err,
65 read_descriptor_t *desc)
66{
67 desc->error = err;
Tom Herbertbbb03022017-07-28 16:22:43 -070068 kfree_skb(strp->skb_head);
69 strp->skb_head = NULL;
Tom Herbert43a0c672016-08-15 14:51:01 -070070 strp->cb.abort_parser(strp, err);
71}
72
Tom Herbert96a59082016-08-28 14:43:19 -070073static inline int strp_peek_len(struct strparser *strp)
74{
Tom Herbertbbb03022017-07-28 16:22:43 -070075 if (strp->sk) {
76 struct socket *sock = strp->sk->sk_socket;
Tom Herbert96a59082016-08-28 14:43:19 -070077
Tom Herbertbbb03022017-07-28 16:22:43 -070078 return sock->ops->peek_len(sock);
79 }
80
81 /* If we don't have an associated socket there's nothing to peek.
82 * Return int max to avoid stopping the strparser.
83 */
84
85 return INT_MAX;
Tom Herbert96a59082016-08-28 14:43:19 -070086}
87
Tom Herbert43a0c672016-08-15 14:51:01 -070088/* Lower socket lock held */
Tom Herbertbbb03022017-07-28 16:22:43 -070089static int __strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
90 unsigned int orig_offset, size_t orig_len,
91 size_t max_msg_size, long timeo)
Tom Herbert43a0c672016-08-15 14:51:01 -070092{
93 struct strparser *strp = (struct strparser *)desc->arg.data;
Tom Herbertbbb03022017-07-28 16:22:43 -070094 struct _strp_msg *stm;
Tom Herbert43a0c672016-08-15 14:51:01 -070095 struct sk_buff *head, *skb;
96 size_t eaten = 0, cand_len;
97 ssize_t extra;
98 int err;
99 bool cloned_orig = false;
100
Tom Herbertbbb03022017-07-28 16:22:43 -0700101 if (strp->paused)
Tom Herbert43a0c672016-08-15 14:51:01 -0700102 return 0;
103
Tom Herbertbbb03022017-07-28 16:22:43 -0700104 head = strp->skb_head;
Tom Herbert43a0c672016-08-15 14:51:01 -0700105 if (head) {
106 /* Message already in progress */
Tom Herbert43a0c672016-08-15 14:51:01 -0700107 if (unlikely(orig_offset)) {
108 /* Getting data with a non-zero offset when a message is
109 * in progress is not expected. If it does happen, we
110 * need to clone and pull since we can't deal with
111 * offsets in the skbs for a message expect in the head.
112 */
113 orig_skb = skb_clone(orig_skb, GFP_ATOMIC);
114 if (!orig_skb) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700115 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700116 desc->error = -ENOMEM;
117 return 0;
118 }
119 if (!pskb_pull(orig_skb, orig_offset)) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700120 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700121 kfree_skb(orig_skb);
122 desc->error = -ENOMEM;
123 return 0;
124 }
125 cloned_orig = true;
126 orig_offset = 0;
127 }
128
Tom Herbertbbb03022017-07-28 16:22:43 -0700129 if (!strp->skb_nextp) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700130 /* We are going to append to the frags_list of head.
131 * Need to unshare the frag_list.
132 */
Jakub Kicinski4a9c2e32019-04-10 11:04:32 -0700133 err = skb_unclone(head, GFP_ATOMIC);
134 if (err) {
135 STRP_STATS_INCR(strp->stats.mem_fail);
136 desc->error = err;
137 return 0;
Tom Herbert43a0c672016-08-15 14:51:01 -0700138 }
139
140 if (unlikely(skb_shinfo(head)->frag_list)) {
141 /* We can't append to an sk_buff that already
142 * has a frag_list. We create a new head, point
143 * the frag_list of that to the old head, and
144 * then are able to use the old head->next for
145 * appending to the message.
146 */
147 if (WARN_ON(head->next)) {
148 desc->error = -EINVAL;
149 return 0;
150 }
151
Jakub Kicinskida29e4b2019-06-03 15:16:58 -0700152 skb = alloc_skb_for_msg(head);
Tom Herbert43a0c672016-08-15 14:51:01 -0700153 if (!skb) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700154 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700155 desc->error = -ENOMEM;
156 return 0;
157 }
Jakub Kicinskida29e4b2019-06-03 15:16:58 -0700158
Tom Herbertbbb03022017-07-28 16:22:43 -0700159 strp->skb_nextp = &head->next;
Tom Herbertbbb03022017-07-28 16:22:43 -0700160 strp->skb_head = skb;
Tom Herbert43a0c672016-08-15 14:51:01 -0700161 head = skb;
162 } else {
Tom Herbertbbb03022017-07-28 16:22:43 -0700163 strp->skb_nextp =
Tom Herbert43a0c672016-08-15 14:51:01 -0700164 &skb_shinfo(head)->frag_list;
165 }
166 }
167 }
168
169 while (eaten < orig_len) {
170 /* Always clone since we will consume something */
171 skb = skb_clone(orig_skb, GFP_ATOMIC);
172 if (!skb) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700173 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700174 desc->error = -ENOMEM;
175 break;
176 }
177
178 cand_len = orig_len - eaten;
179
Tom Herbertbbb03022017-07-28 16:22:43 -0700180 head = strp->skb_head;
Tom Herbert43a0c672016-08-15 14:51:01 -0700181 if (!head) {
182 head = skb;
Tom Herbertbbb03022017-07-28 16:22:43 -0700183 strp->skb_head = head;
184 /* Will set skb_nextp on next packet if needed */
185 strp->skb_nextp = NULL;
186 stm = _strp_msg(head);
187 memset(stm, 0, sizeof(*stm));
188 stm->strp.offset = orig_offset + eaten;
Tom Herbert43a0c672016-08-15 14:51:01 -0700189 } else {
Vakul Garg4e485d02018-06-30 00:45:55 +0530190 /* Unclone if we are appending to an skb that we
Tom Herbert43a0c672016-08-15 14:51:01 -0700191 * already share a frag_list with.
192 */
Vakul Garg4e485d02018-06-30 00:45:55 +0530193 if (skb_has_frag_list(skb)) {
194 err = skb_unclone(skb, GFP_ATOMIC);
195 if (err) {
196 STRP_STATS_INCR(strp->stats.mem_fail);
197 desc->error = err;
198 break;
199 }
Tom Herbert43a0c672016-08-15 14:51:01 -0700200 }
201
Tom Herbertbbb03022017-07-28 16:22:43 -0700202 stm = _strp_msg(head);
203 *strp->skb_nextp = skb;
204 strp->skb_nextp = &skb->next;
Tom Herbert43a0c672016-08-15 14:51:01 -0700205 head->data_len += skb->len;
206 head->len += skb->len;
207 head->truesize += skb->truesize;
208 }
209
Tom Herbertbbb03022017-07-28 16:22:43 -0700210 if (!stm->strp.full_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700211 ssize_t len;
212
213 len = (*strp->cb.parse_msg)(strp, head);
214
215 if (!len) {
216 /* Need more header to determine length */
Tom Herbertbbb03022017-07-28 16:22:43 -0700217 if (!stm->accum_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700218 /* Start RX timer for new message */
Tom Herbertbbb03022017-07-28 16:22:43 -0700219 strp_start_timer(strp, timeo);
Tom Herbert43a0c672016-08-15 14:51:01 -0700220 }
Tom Herbertbbb03022017-07-28 16:22:43 -0700221 stm->accum_len += cand_len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700222 eaten += cand_len;
Tom Herbertbbb03022017-07-28 16:22:43 -0700223 STRP_STATS_INCR(strp->stats.need_more_hdr);
Tom Herbert43a0c672016-08-15 14:51:01 -0700224 WARN_ON(eaten != orig_len);
225 break;
226 } else if (len < 0) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700227 if (len == -ESTRPIPE && stm->accum_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700228 len = -ENODATA;
Tom Herbertbbb03022017-07-28 16:22:43 -0700229 strp->unrecov_intr = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -0700230 } else {
Tom Herbertbbb03022017-07-28 16:22:43 -0700231 strp->interrupted = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -0700232 }
Geert Uytterhoeven6d3a4c42016-10-06 15:41:49 +0200233 strp_parser_err(strp, len, desc);
Tom Herbert43a0c672016-08-15 14:51:01 -0700234 break;
Tom Herbertbbb03022017-07-28 16:22:43 -0700235 } else if (len > max_msg_size) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700236 /* Message length exceeds maximum allowed */
Tom Herbertbbb03022017-07-28 16:22:43 -0700237 STRP_STATS_INCR(strp->stats.msg_too_big);
Tom Herbert43a0c672016-08-15 14:51:01 -0700238 strp_parser_err(strp, -EMSGSIZE, desc);
239 break;
240 } else if (len <= (ssize_t)head->len -
Tom Herbertbbb03022017-07-28 16:22:43 -0700241 skb->len - stm->strp.offset) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700242 /* Length must be into new skb (and also
243 * greater than zero)
244 */
Tom Herbertbbb03022017-07-28 16:22:43 -0700245 STRP_STATS_INCR(strp->stats.bad_hdr_len);
Tom Herbert43a0c672016-08-15 14:51:01 -0700246 strp_parser_err(strp, -EPROTO, desc);
247 break;
248 }
249
Tom Herbertbbb03022017-07-28 16:22:43 -0700250 stm->strp.full_len = len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700251 }
252
Tom Herbertbbb03022017-07-28 16:22:43 -0700253 extra = (ssize_t)(stm->accum_len + cand_len) -
254 stm->strp.full_len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700255
256 if (extra < 0) {
257 /* Message not complete yet. */
Tom Herbertbbb03022017-07-28 16:22:43 -0700258 if (stm->strp.full_len - stm->accum_len >
Tom Herbert96a59082016-08-28 14:43:19 -0700259 strp_peek_len(strp)) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700260 /* Don't have the whole message in the socket
261 * buffer. Set strp->need_bytes to wait for
Tom Herbert43a0c672016-08-15 14:51:01 -0700262 * the rest of the message. Also, set "early
263 * eaten" since we've already buffered the skb
Tom Herbert96a59082016-08-28 14:43:19 -0700264 * but don't consume yet per strp_read_sock.
Tom Herbert43a0c672016-08-15 14:51:01 -0700265 */
266
Tom Herbertbbb03022017-07-28 16:22:43 -0700267 if (!stm->accum_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700268 /* Start RX timer for new message */
Tom Herbertbbb03022017-07-28 16:22:43 -0700269 strp_start_timer(strp, timeo);
Tom Herbert43a0c672016-08-15 14:51:01 -0700270 }
271
Doron Roberts-Kedes9d0c75b2018-04-11 15:05:16 -0700272 stm->accum_len += cand_len;
Doron Roberts-Kedes977c7112018-06-26 18:33:33 -0700273 eaten += cand_len;
Tom Herbertbbb03022017-07-28 16:22:43 -0700274 strp->need_bytes = stm->strp.full_len -
275 stm->accum_len;
Tom Herbertbbb03022017-07-28 16:22:43 -0700276 STRP_STATS_ADD(strp->stats.bytes, cand_len);
Tom Herbert43a0c672016-08-15 14:51:01 -0700277 desc->count = 0; /* Stop reading socket */
278 break;
279 }
Tom Herbertbbb03022017-07-28 16:22:43 -0700280 stm->accum_len += cand_len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700281 eaten += cand_len;
282 WARN_ON(eaten != orig_len);
283 break;
284 }
285
Jakub Kicinski93e21252019-04-10 13:18:57 -0700286 /* Positive extra indicates more bytes than needed for the
Tom Herbert43a0c672016-08-15 14:51:01 -0700287 * message
288 */
289
290 WARN_ON(extra > cand_len);
291
292 eaten += (cand_len - extra);
293
294 /* Hurray, we have a new message! */
Tom Herbert829385f2017-10-20 16:40:43 -0700295 cancel_delayed_work(&strp->msg_timer_work);
Tom Herbertbbb03022017-07-28 16:22:43 -0700296 strp->skb_head = NULL;
Doron Roberts-Kedes9d0c75b2018-04-11 15:05:16 -0700297 strp->need_bytes = 0;
Tom Herbertbbb03022017-07-28 16:22:43 -0700298 STRP_STATS_INCR(strp->stats.msgs);
Tom Herbert43a0c672016-08-15 14:51:01 -0700299
300 /* Give skb to upper layer */
301 strp->cb.rcv_msg(strp, head);
302
Tom Herbertbbb03022017-07-28 16:22:43 -0700303 if (unlikely(strp->paused)) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700304 /* Upper layer paused strp */
305 break;
306 }
307 }
308
309 if (cloned_orig)
310 kfree_skb(orig_skb);
311
Tom Herbertbbb03022017-07-28 16:22:43 -0700312 STRP_STATS_ADD(strp->stats.bytes, eaten);
Tom Herbert43a0c672016-08-15 14:51:01 -0700313
314 return eaten;
315}
316
Tom Herbertbbb03022017-07-28 16:22:43 -0700317int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
318 unsigned int orig_offset, size_t orig_len,
319 size_t max_msg_size, long timeo)
320{
321 read_descriptor_t desc; /* Dummy arg to strp_recv */
322
323 desc.arg.data = strp;
324
325 return __strp_recv(&desc, orig_skb, orig_offset, orig_len,
326 max_msg_size, timeo);
327}
328EXPORT_SYMBOL_GPL(strp_process);
329
330static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
331 unsigned int orig_offset, size_t orig_len)
332{
333 struct strparser *strp = (struct strparser *)desc->arg.data;
334
335 return __strp_recv(desc, orig_skb, orig_offset, orig_len,
336 strp->sk->sk_rcvbuf, strp->sk->sk_rcvtimeo);
337}
338
Tom Herbert43a0c672016-08-15 14:51:01 -0700339static int default_read_sock_done(struct strparser *strp, int err)
340{
341 return err;
342}
343
344/* Called with lock held on lower socket */
Tom Herbert96a59082016-08-28 14:43:19 -0700345static int strp_read_sock(struct strparser *strp)
Tom Herbert43a0c672016-08-15 14:51:01 -0700346{
Tom Herbert96a59082016-08-28 14:43:19 -0700347 struct socket *sock = strp->sk->sk_socket;
Tom Herbert43a0c672016-08-15 14:51:01 -0700348 read_descriptor_t desc;
349
John Fastabendf26de112017-08-15 22:30:47 -0700350 if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
351 return -EBUSY;
352
Tom Herbert43a0c672016-08-15 14:51:01 -0700353 desc.arg.data = strp;
354 desc.error = 0;
355 desc.count = 1; /* give more than one skb per call */
356
Tom Herbert96a59082016-08-28 14:43:19 -0700357 /* sk should be locked here, so okay to do read_sock */
358 sock->ops->read_sock(strp->sk, &desc, strp_recv);
Tom Herbert43a0c672016-08-15 14:51:01 -0700359
360 desc.error = strp->cb.read_sock_done(strp, desc.error);
361
362 return desc.error;
363}
364
365/* Lower sock lock held */
Tom Herbert96a59082016-08-28 14:43:19 -0700366void strp_data_ready(struct strparser *strp)
Tom Herbert43a0c672016-08-15 14:51:01 -0700367{
Vakul Garg456488c2018-06-21 03:29:49 +0530368 if (unlikely(strp->stopped) || strp->paused)
Tom Herbert43a0c672016-08-15 14:51:01 -0700369 return;
370
Tom Herbertbbb03022017-07-28 16:22:43 -0700371 /* This check is needed to synchronize with do_strp_work.
372 * do_strp_work acquires a process lock (lock_sock) whereas
Tom Herbert43a0c672016-08-15 14:51:01 -0700373 * the lock held here is bh_lock_sock. The two locks can be
374 * held by different threads at the same time, but bh_lock_sock
375 * allows a thread in BH context to safely check if the process
376 * lock is held. In this case, if the lock is held, queue work.
377 */
Tom Herbertd66fa9e2017-12-28 11:00:44 -0800378 if (sock_owned_by_user_nocheck(strp->sk)) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700379 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700380 return;
381 }
382
Tom Herbertbbb03022017-07-28 16:22:43 -0700383 if (strp->need_bytes) {
Doron Roberts-Kedes9d0c75b2018-04-11 15:05:16 -0700384 if (strp_peek_len(strp) < strp->need_bytes)
Tom Herbert43a0c672016-08-15 14:51:01 -0700385 return;
386 }
387
Tom Herbert96a59082016-08-28 14:43:19 -0700388 if (strp_read_sock(strp) == -ENOMEM)
Tom Herbertbbb03022017-07-28 16:22:43 -0700389 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700390}
Tom Herbert96a59082016-08-28 14:43:19 -0700391EXPORT_SYMBOL_GPL(strp_data_ready);
Tom Herbert43a0c672016-08-15 14:51:01 -0700392
Tom Herbertbbb03022017-07-28 16:22:43 -0700393static void do_strp_work(struct strparser *strp)
Tom Herbert43a0c672016-08-15 14:51:01 -0700394{
Tom Herbert96a59082016-08-28 14:43:19 -0700395 /* We need the read lock to synchronize with strp_data_ready. We
396 * need the socket lock for calling strp_read_sock.
Tom Herbert43a0c672016-08-15 14:51:01 -0700397 */
Tom Herbertbbb03022017-07-28 16:22:43 -0700398 strp->cb.lock(strp);
Tom Herbert43a0c672016-08-15 14:51:01 -0700399
Tom Herbertbbb03022017-07-28 16:22:43 -0700400 if (unlikely(strp->stopped))
Tom Herbert43a0c672016-08-15 14:51:01 -0700401 goto out;
402
Tom Herbertbbb03022017-07-28 16:22:43 -0700403 if (strp->paused)
Tom Herbert43a0c672016-08-15 14:51:01 -0700404 goto out;
405
Tom Herbert96a59082016-08-28 14:43:19 -0700406 if (strp_read_sock(strp) == -ENOMEM)
Tom Herbertbbb03022017-07-28 16:22:43 -0700407 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700408
409out:
Tom Herbertbbb03022017-07-28 16:22:43 -0700410 strp->cb.unlock(strp);
Tom Herbert43a0c672016-08-15 14:51:01 -0700411}
412
Tom Herbertbbb03022017-07-28 16:22:43 -0700413static void strp_work(struct work_struct *w)
Tom Herbert43a0c672016-08-15 14:51:01 -0700414{
Tom Herbertbbb03022017-07-28 16:22:43 -0700415 do_strp_work(container_of(w, struct strparser, work));
Tom Herbert43a0c672016-08-15 14:51:01 -0700416}
417
Tom Herbert829385f2017-10-20 16:40:43 -0700418static void strp_msg_timeout(struct work_struct *w)
Tom Herbert43a0c672016-08-15 14:51:01 -0700419{
Tom Herbert829385f2017-10-20 16:40:43 -0700420 struct strparser *strp = container_of(w, struct strparser,
421 msg_timer_work.work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700422
423 /* Message assembly timed out */
Tom Herbertbbb03022017-07-28 16:22:43 -0700424 STRP_STATS_INCR(strp->stats.msg_timeouts);
425 strp->cb.lock(strp);
Dave Watsoncd00edc2018-03-26 12:31:21 -0700426 strp->cb.abort_parser(strp, -ETIMEDOUT);
Tom Herbertbbb03022017-07-28 16:22:43 -0700427 strp->cb.unlock(strp);
428}
429
430static void strp_sock_lock(struct strparser *strp)
431{
432 lock_sock(strp->sk);
433}
434
435static void strp_sock_unlock(struct strparser *strp)
436{
Tom Herbert43a0c672016-08-15 14:51:01 -0700437 release_sock(strp->sk);
438}
439
Tom Herbertbbb03022017-07-28 16:22:43 -0700440int strp_init(struct strparser *strp, struct sock *sk,
Eric Biggers3fd87122017-08-24 14:38:51 -0700441 const struct strp_callbacks *cb)
Tom Herbert43a0c672016-08-15 14:51:01 -0700442{
Tom Herbert96a59082016-08-28 14:43:19 -0700443
Tom Herbert43a0c672016-08-15 14:51:01 -0700444 if (!cb || !cb->rcv_msg || !cb->parse_msg)
445 return -EINVAL;
446
Tom Herbertbbb03022017-07-28 16:22:43 -0700447 /* The sk (sock) arg determines the mode of the stream parser.
448 *
449 * If the sock is set then the strparser is in receive callback mode.
450 * The upper layer calls strp_data_ready to kick receive processing
451 * and strparser calls the read_sock function on the socket to
452 * get packets.
453 *
454 * If the sock is not set then the strparser is in general mode.
455 * The upper layer calls strp_process for each skb to be parsed.
456 */
457
John Fastabendf26de112017-08-15 22:30:47 -0700458 if (!sk) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700459 if (!cb->lock || !cb->unlock)
460 return -EINVAL;
461 }
Tom Herbert96a59082016-08-28 14:43:19 -0700462
Tom Herbert43a0c672016-08-15 14:51:01 -0700463 memset(strp, 0, sizeof(*strp));
464
Tom Herbertbbb03022017-07-28 16:22:43 -0700465 strp->sk = sk;
Tom Herbert43a0c672016-08-15 14:51:01 -0700466
Tom Herbertbbb03022017-07-28 16:22:43 -0700467 strp->cb.lock = cb->lock ? : strp_sock_lock;
468 strp->cb.unlock = cb->unlock ? : strp_sock_unlock;
Tom Herbert43a0c672016-08-15 14:51:01 -0700469 strp->cb.rcv_msg = cb->rcv_msg;
470 strp->cb.parse_msg = cb->parse_msg;
471 strp->cb.read_sock_done = cb->read_sock_done ? : default_read_sock_done;
Tom Herbertbbb03022017-07-28 16:22:43 -0700472 strp->cb.abort_parser = cb->abort_parser ? : strp_abort_strp;
473
Tom Herbert829385f2017-10-20 16:40:43 -0700474 INIT_DELAYED_WORK(&strp->msg_timer_work, strp_msg_timeout);
Tom Herbertbbb03022017-07-28 16:22:43 -0700475 INIT_WORK(&strp->work, strp_work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700476
477 return 0;
478}
479EXPORT_SYMBOL_GPL(strp_init);
480
Doron Roberts-Kedes7170e602018-06-06 09:33:28 -0700481/* Sock process lock held (lock_sock) */
482void __strp_unpause(struct strparser *strp)
483{
484 strp->paused = 0;
485
486 if (strp->need_bytes) {
487 if (strp_peek_len(strp) < strp->need_bytes)
488 return;
489 }
490 strp_read_sock(strp);
491}
492EXPORT_SYMBOL_GPL(__strp_unpause);
493
Tom Herbertcff6a332016-08-23 11:55:30 -0700494void strp_unpause(struct strparser *strp)
495{
Tom Herbertbbb03022017-07-28 16:22:43 -0700496 strp->paused = 0;
Tom Herbertcff6a332016-08-23 11:55:30 -0700497
Tom Herbertbbb03022017-07-28 16:22:43 -0700498 /* Sync setting paused with RX work */
Tom Herbertcff6a332016-08-23 11:55:30 -0700499 smp_mb();
500
Tom Herbertbbb03022017-07-28 16:22:43 -0700501 queue_work(strp_wq, &strp->work);
Tom Herbertcff6a332016-08-23 11:55:30 -0700502}
503EXPORT_SYMBOL_GPL(strp_unpause);
504
Tom Herbert96a59082016-08-28 14:43:19 -0700505/* strp must already be stopped so that strp_recv will no longer be called.
Tom Herbert43a0c672016-08-15 14:51:01 -0700506 * Note that strp_done is not called with the lower socket held.
507 */
508void strp_done(struct strparser *strp)
509{
Tom Herbertbbb03022017-07-28 16:22:43 -0700510 WARN_ON(!strp->stopped);
Tom Herbert43a0c672016-08-15 14:51:01 -0700511
Tom Herbert829385f2017-10-20 16:40:43 -0700512 cancel_delayed_work_sync(&strp->msg_timer_work);
Tom Herbertbbb03022017-07-28 16:22:43 -0700513 cancel_work_sync(&strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700514
Tom Herbertbbb03022017-07-28 16:22:43 -0700515 if (strp->skb_head) {
516 kfree_skb(strp->skb_head);
517 strp->skb_head = NULL;
Tom Herbert43a0c672016-08-15 14:51:01 -0700518 }
519}
520EXPORT_SYMBOL_GPL(strp_done);
521
522void strp_stop(struct strparser *strp)
523{
Tom Herbertbbb03022017-07-28 16:22:43 -0700524 strp->stopped = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -0700525}
526EXPORT_SYMBOL_GPL(strp_stop);
527
528void strp_check_rcv(struct strparser *strp)
529{
Tom Herbertbbb03022017-07-28 16:22:43 -0700530 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700531}
532EXPORT_SYMBOL_GPL(strp_check_rcv);
533
Paul Gortmaker15253b42019-04-20 23:29:48 -0400534static int __init strp_dev_init(void)
Tom Herbert43a0c672016-08-15 14:51:01 -0700535{
536 strp_wq = create_singlethread_workqueue("kstrp");
Kangjie Lu228cd2d2019-03-14 23:12:06 -0500537 if (unlikely(!strp_wq))
538 return -ENOMEM;
Tom Herbert43a0c672016-08-15 14:51:01 -0700539
540 return 0;
541}
Paul Gortmaker15253b42019-04-20 23:29:48 -0400542device_initcall(strp_dev_init);