blob: b3815c1e8f2ea874ae7c4a5eef3f3f9282c254a0 [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 -070030struct _strp_msg {
31 /* Internal cb structure. struct strp_msg must be first for passing
Tom Herbert43a0c672016-08-15 14:51:01 -070032 * to upper layer.
33 */
Tom Herbertbbb03022017-07-28 16:22:43 -070034 struct strp_msg strp;
Tom Herbert43a0c672016-08-15 14:51:01 -070035 int accum_len;
Tom Herbert43a0c672016-08-15 14:51:01 -070036};
37
Tom Herbertbbb03022017-07-28 16:22:43 -070038static inline struct _strp_msg *_strp_msg(struct sk_buff *skb)
Tom Herbert43a0c672016-08-15 14:51:01 -070039{
Tom Herbertbbb03022017-07-28 16:22:43 -070040 return (struct _strp_msg *)((void *)skb->cb +
Tom Herbert43a0c672016-08-15 14:51:01 -070041 offsetof(struct qdisc_skb_cb, data));
42}
43
44/* Lower lock held */
Tom Herbertbbb03022017-07-28 16:22:43 -070045static void strp_abort_strp(struct strparser *strp, int err)
Tom Herbert43a0c672016-08-15 14:51:01 -070046{
Tom Herbert43a0c672016-08-15 14:51:01 -070047 /* Unrecoverable error in receive */
48
Tom Herbert829385f2017-10-20 16:40:43 -070049 cancel_delayed_work(&strp->msg_timer_work);
Tom Herbert43a0c672016-08-15 14:51:01 -070050
Tom Herbertbbb03022017-07-28 16:22:43 -070051 if (strp->stopped)
Tom Herbert43a0c672016-08-15 14:51:01 -070052 return;
53
Tom Herbertbbb03022017-07-28 16:22:43 -070054 strp->stopped = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -070055
Tom Herbertbbb03022017-07-28 16:22:43 -070056 if (strp->sk) {
57 struct sock *sk = strp->sk;
58
59 /* Report an error on the lower socket */
Dave Watsoncd00edc2018-03-26 12:31:21 -070060 sk->sk_err = -err;
Tom Herbertbbb03022017-07-28 16:22:43 -070061 sk->sk_error_report(sk);
62 }
Tom Herbert43a0c672016-08-15 14:51:01 -070063}
64
Tom Herbertbbb03022017-07-28 16:22:43 -070065static void strp_start_timer(struct strparser *strp, long timeo)
Tom Herbert43a0c672016-08-15 14:51:01 -070066{
Doron Roberts-Kedes7c5aba22018-04-20 12:11:11 -070067 if (timeo && timeo != LONG_MAX)
Tom Herbert829385f2017-10-20 16:40:43 -070068 mod_delayed_work(strp_wq, &strp->msg_timer_work, timeo);
Tom Herbert43a0c672016-08-15 14:51:01 -070069}
70
71/* Lower lock held */
72static void strp_parser_err(struct strparser *strp, int err,
73 read_descriptor_t *desc)
74{
75 desc->error = err;
Tom Herbertbbb03022017-07-28 16:22:43 -070076 kfree_skb(strp->skb_head);
77 strp->skb_head = NULL;
Tom Herbert43a0c672016-08-15 14:51:01 -070078 strp->cb.abort_parser(strp, err);
79}
80
Tom Herbert96a59082016-08-28 14:43:19 -070081static inline int strp_peek_len(struct strparser *strp)
82{
Tom Herbertbbb03022017-07-28 16:22:43 -070083 if (strp->sk) {
84 struct socket *sock = strp->sk->sk_socket;
Tom Herbert96a59082016-08-28 14:43:19 -070085
Tom Herbertbbb03022017-07-28 16:22:43 -070086 return sock->ops->peek_len(sock);
87 }
88
89 /* If we don't have an associated socket there's nothing to peek.
90 * Return int max to avoid stopping the strparser.
91 */
92
93 return INT_MAX;
Tom Herbert96a59082016-08-28 14:43:19 -070094}
95
Tom Herbert43a0c672016-08-15 14:51:01 -070096/* Lower socket lock held */
Tom Herbertbbb03022017-07-28 16:22:43 -070097static int __strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
98 unsigned int orig_offset, size_t orig_len,
99 size_t max_msg_size, long timeo)
Tom Herbert43a0c672016-08-15 14:51:01 -0700100{
101 struct strparser *strp = (struct strparser *)desc->arg.data;
Tom Herbertbbb03022017-07-28 16:22:43 -0700102 struct _strp_msg *stm;
Tom Herbert43a0c672016-08-15 14:51:01 -0700103 struct sk_buff *head, *skb;
104 size_t eaten = 0, cand_len;
105 ssize_t extra;
106 int err;
107 bool cloned_orig = false;
108
Tom Herbertbbb03022017-07-28 16:22:43 -0700109 if (strp->paused)
Tom Herbert43a0c672016-08-15 14:51:01 -0700110 return 0;
111
Tom Herbertbbb03022017-07-28 16:22:43 -0700112 head = strp->skb_head;
Tom Herbert43a0c672016-08-15 14:51:01 -0700113 if (head) {
114 /* Message already in progress */
Tom Herbert43a0c672016-08-15 14:51:01 -0700115 if (unlikely(orig_offset)) {
116 /* Getting data with a non-zero offset when a message is
117 * in progress is not expected. If it does happen, we
118 * need to clone and pull since we can't deal with
119 * offsets in the skbs for a message expect in the head.
120 */
121 orig_skb = skb_clone(orig_skb, GFP_ATOMIC);
122 if (!orig_skb) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700123 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700124 desc->error = -ENOMEM;
125 return 0;
126 }
127 if (!pskb_pull(orig_skb, orig_offset)) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700128 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700129 kfree_skb(orig_skb);
130 desc->error = -ENOMEM;
131 return 0;
132 }
133 cloned_orig = true;
134 orig_offset = 0;
135 }
136
Tom Herbertbbb03022017-07-28 16:22:43 -0700137 if (!strp->skb_nextp) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700138 /* We are going to append to the frags_list of head.
139 * Need to unshare the frag_list.
140 */
Jakub Kicinski4a9c2e32019-04-10 11:04:32 -0700141 err = skb_unclone(head, GFP_ATOMIC);
142 if (err) {
143 STRP_STATS_INCR(strp->stats.mem_fail);
144 desc->error = err;
145 return 0;
Tom Herbert43a0c672016-08-15 14:51:01 -0700146 }
147
148 if (unlikely(skb_shinfo(head)->frag_list)) {
149 /* We can't append to an sk_buff that already
150 * has a frag_list. We create a new head, point
151 * the frag_list of that to the old head, and
152 * then are able to use the old head->next for
153 * appending to the message.
154 */
155 if (WARN_ON(head->next)) {
156 desc->error = -EINVAL;
157 return 0;
158 }
159
Jakub Kicinskida29e4b2019-06-03 15:16:58 -0700160 skb = alloc_skb_for_msg(head);
Tom Herbert43a0c672016-08-15 14:51:01 -0700161 if (!skb) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700162 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700163 desc->error = -ENOMEM;
164 return 0;
165 }
Jakub Kicinskida29e4b2019-06-03 15:16:58 -0700166
Tom Herbertbbb03022017-07-28 16:22:43 -0700167 strp->skb_nextp = &head->next;
Tom Herbertbbb03022017-07-28 16:22:43 -0700168 strp->skb_head = skb;
Tom Herbert43a0c672016-08-15 14:51:01 -0700169 head = skb;
170 } else {
Tom Herbertbbb03022017-07-28 16:22:43 -0700171 strp->skb_nextp =
Tom Herbert43a0c672016-08-15 14:51:01 -0700172 &skb_shinfo(head)->frag_list;
173 }
174 }
175 }
176
177 while (eaten < orig_len) {
178 /* Always clone since we will consume something */
179 skb = skb_clone(orig_skb, GFP_ATOMIC);
180 if (!skb) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700181 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700182 desc->error = -ENOMEM;
183 break;
184 }
185
186 cand_len = orig_len - eaten;
187
Tom Herbertbbb03022017-07-28 16:22:43 -0700188 head = strp->skb_head;
Tom Herbert43a0c672016-08-15 14:51:01 -0700189 if (!head) {
190 head = skb;
Tom Herbertbbb03022017-07-28 16:22:43 -0700191 strp->skb_head = head;
192 /* Will set skb_nextp on next packet if needed */
193 strp->skb_nextp = NULL;
194 stm = _strp_msg(head);
195 memset(stm, 0, sizeof(*stm));
196 stm->strp.offset = orig_offset + eaten;
Tom Herbert43a0c672016-08-15 14:51:01 -0700197 } else {
Vakul Garg4e485d02018-06-30 00:45:55 +0530198 /* Unclone if we are appending to an skb that we
Tom Herbert43a0c672016-08-15 14:51:01 -0700199 * already share a frag_list with.
200 */
Vakul Garg4e485d02018-06-30 00:45:55 +0530201 if (skb_has_frag_list(skb)) {
202 err = skb_unclone(skb, GFP_ATOMIC);
203 if (err) {
204 STRP_STATS_INCR(strp->stats.mem_fail);
205 desc->error = err;
206 break;
207 }
Tom Herbert43a0c672016-08-15 14:51:01 -0700208 }
209
Tom Herbertbbb03022017-07-28 16:22:43 -0700210 stm = _strp_msg(head);
211 *strp->skb_nextp = skb;
212 strp->skb_nextp = &skb->next;
Tom Herbert43a0c672016-08-15 14:51:01 -0700213 head->data_len += skb->len;
214 head->len += skb->len;
215 head->truesize += skb->truesize;
216 }
217
Tom Herbertbbb03022017-07-28 16:22:43 -0700218 if (!stm->strp.full_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700219 ssize_t len;
220
221 len = (*strp->cb.parse_msg)(strp, head);
222
223 if (!len) {
224 /* Need more header to determine length */
Tom Herbertbbb03022017-07-28 16:22:43 -0700225 if (!stm->accum_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700226 /* Start RX timer for new message */
Tom Herbertbbb03022017-07-28 16:22:43 -0700227 strp_start_timer(strp, timeo);
Tom Herbert43a0c672016-08-15 14:51:01 -0700228 }
Tom Herbertbbb03022017-07-28 16:22:43 -0700229 stm->accum_len += cand_len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700230 eaten += cand_len;
Tom Herbertbbb03022017-07-28 16:22:43 -0700231 STRP_STATS_INCR(strp->stats.need_more_hdr);
Tom Herbert43a0c672016-08-15 14:51:01 -0700232 WARN_ON(eaten != orig_len);
233 break;
234 } else if (len < 0) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700235 if (len == -ESTRPIPE && stm->accum_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700236 len = -ENODATA;
Tom Herbertbbb03022017-07-28 16:22:43 -0700237 strp->unrecov_intr = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -0700238 } else {
Tom Herbertbbb03022017-07-28 16:22:43 -0700239 strp->interrupted = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -0700240 }
Geert Uytterhoeven6d3a4c42016-10-06 15:41:49 +0200241 strp_parser_err(strp, len, desc);
Tom Herbert43a0c672016-08-15 14:51:01 -0700242 break;
Tom Herbertbbb03022017-07-28 16:22:43 -0700243 } else if (len > max_msg_size) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700244 /* Message length exceeds maximum allowed */
Tom Herbertbbb03022017-07-28 16:22:43 -0700245 STRP_STATS_INCR(strp->stats.msg_too_big);
Tom Herbert43a0c672016-08-15 14:51:01 -0700246 strp_parser_err(strp, -EMSGSIZE, desc);
247 break;
248 } else if (len <= (ssize_t)head->len -
Tom Herbertbbb03022017-07-28 16:22:43 -0700249 skb->len - stm->strp.offset) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700250 /* Length must be into new skb (and also
251 * greater than zero)
252 */
Tom Herbertbbb03022017-07-28 16:22:43 -0700253 STRP_STATS_INCR(strp->stats.bad_hdr_len);
Tom Herbert43a0c672016-08-15 14:51:01 -0700254 strp_parser_err(strp, -EPROTO, desc);
255 break;
256 }
257
Tom Herbertbbb03022017-07-28 16:22:43 -0700258 stm->strp.full_len = len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700259 }
260
Tom Herbertbbb03022017-07-28 16:22:43 -0700261 extra = (ssize_t)(stm->accum_len + cand_len) -
262 stm->strp.full_len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700263
264 if (extra < 0) {
265 /* Message not complete yet. */
Tom Herbertbbb03022017-07-28 16:22:43 -0700266 if (stm->strp.full_len - stm->accum_len >
Tom Herbert96a59082016-08-28 14:43:19 -0700267 strp_peek_len(strp)) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700268 /* Don't have the whole message in the socket
269 * buffer. Set strp->need_bytes to wait for
Tom Herbert43a0c672016-08-15 14:51:01 -0700270 * the rest of the message. Also, set "early
271 * eaten" since we've already buffered the skb
Tom Herbert96a59082016-08-28 14:43:19 -0700272 * but don't consume yet per strp_read_sock.
Tom Herbert43a0c672016-08-15 14:51:01 -0700273 */
274
Tom Herbertbbb03022017-07-28 16:22:43 -0700275 if (!stm->accum_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700276 /* Start RX timer for new message */
Tom Herbertbbb03022017-07-28 16:22:43 -0700277 strp_start_timer(strp, timeo);
Tom Herbert43a0c672016-08-15 14:51:01 -0700278 }
279
Doron Roberts-Kedes9d0c75b2018-04-11 15:05:16 -0700280 stm->accum_len += cand_len;
Doron Roberts-Kedes977c7112018-06-26 18:33:33 -0700281 eaten += cand_len;
Tom Herbertbbb03022017-07-28 16:22:43 -0700282 strp->need_bytes = stm->strp.full_len -
283 stm->accum_len;
Tom Herbertbbb03022017-07-28 16:22:43 -0700284 STRP_STATS_ADD(strp->stats.bytes, cand_len);
Tom Herbert43a0c672016-08-15 14:51:01 -0700285 desc->count = 0; /* Stop reading socket */
286 break;
287 }
Tom Herbertbbb03022017-07-28 16:22:43 -0700288 stm->accum_len += cand_len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700289 eaten += cand_len;
290 WARN_ON(eaten != orig_len);
291 break;
292 }
293
Jakub Kicinski93e21252019-04-10 13:18:57 -0700294 /* Positive extra indicates more bytes than needed for the
Tom Herbert43a0c672016-08-15 14:51:01 -0700295 * message
296 */
297
298 WARN_ON(extra > cand_len);
299
300 eaten += (cand_len - extra);
301
302 /* Hurray, we have a new message! */
Tom Herbert829385f2017-10-20 16:40:43 -0700303 cancel_delayed_work(&strp->msg_timer_work);
Tom Herbertbbb03022017-07-28 16:22:43 -0700304 strp->skb_head = NULL;
Doron Roberts-Kedes9d0c75b2018-04-11 15:05:16 -0700305 strp->need_bytes = 0;
Tom Herbertbbb03022017-07-28 16:22:43 -0700306 STRP_STATS_INCR(strp->stats.msgs);
Tom Herbert43a0c672016-08-15 14:51:01 -0700307
308 /* Give skb to upper layer */
309 strp->cb.rcv_msg(strp, head);
310
Tom Herbertbbb03022017-07-28 16:22:43 -0700311 if (unlikely(strp->paused)) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700312 /* Upper layer paused strp */
313 break;
314 }
315 }
316
317 if (cloned_orig)
318 kfree_skb(orig_skb);
319
Tom Herbertbbb03022017-07-28 16:22:43 -0700320 STRP_STATS_ADD(strp->stats.bytes, eaten);
Tom Herbert43a0c672016-08-15 14:51:01 -0700321
322 return eaten;
323}
324
Tom Herbertbbb03022017-07-28 16:22:43 -0700325int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
326 unsigned int orig_offset, size_t orig_len,
327 size_t max_msg_size, long timeo)
328{
329 read_descriptor_t desc; /* Dummy arg to strp_recv */
330
331 desc.arg.data = strp;
332
333 return __strp_recv(&desc, orig_skb, orig_offset, orig_len,
334 max_msg_size, timeo);
335}
336EXPORT_SYMBOL_GPL(strp_process);
337
338static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
339 unsigned int orig_offset, size_t orig_len)
340{
341 struct strparser *strp = (struct strparser *)desc->arg.data;
342
343 return __strp_recv(desc, orig_skb, orig_offset, orig_len,
344 strp->sk->sk_rcvbuf, strp->sk->sk_rcvtimeo);
345}
346
Tom Herbert43a0c672016-08-15 14:51:01 -0700347static int default_read_sock_done(struct strparser *strp, int err)
348{
349 return err;
350}
351
352/* Called with lock held on lower socket */
Tom Herbert96a59082016-08-28 14:43:19 -0700353static int strp_read_sock(struct strparser *strp)
Tom Herbert43a0c672016-08-15 14:51:01 -0700354{
Tom Herbert96a59082016-08-28 14:43:19 -0700355 struct socket *sock = strp->sk->sk_socket;
Tom Herbert43a0c672016-08-15 14:51:01 -0700356 read_descriptor_t desc;
357
John Fastabendf26de112017-08-15 22:30:47 -0700358 if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
359 return -EBUSY;
360
Tom Herbert43a0c672016-08-15 14:51:01 -0700361 desc.arg.data = strp;
362 desc.error = 0;
363 desc.count = 1; /* give more than one skb per call */
364
Tom Herbert96a59082016-08-28 14:43:19 -0700365 /* sk should be locked here, so okay to do read_sock */
366 sock->ops->read_sock(strp->sk, &desc, strp_recv);
Tom Herbert43a0c672016-08-15 14:51:01 -0700367
368 desc.error = strp->cb.read_sock_done(strp, desc.error);
369
370 return desc.error;
371}
372
373/* Lower sock lock held */
Tom Herbert96a59082016-08-28 14:43:19 -0700374void strp_data_ready(struct strparser *strp)
Tom Herbert43a0c672016-08-15 14:51:01 -0700375{
Vakul Garg456488c2018-06-21 03:29:49 +0530376 if (unlikely(strp->stopped) || strp->paused)
Tom Herbert43a0c672016-08-15 14:51:01 -0700377 return;
378
Tom Herbertbbb03022017-07-28 16:22:43 -0700379 /* This check is needed to synchronize with do_strp_work.
380 * do_strp_work acquires a process lock (lock_sock) whereas
Tom Herbert43a0c672016-08-15 14:51:01 -0700381 * the lock held here is bh_lock_sock. The two locks can be
382 * held by different threads at the same time, but bh_lock_sock
383 * allows a thread in BH context to safely check if the process
384 * lock is held. In this case, if the lock is held, queue work.
385 */
Tom Herbertd66fa9e2017-12-28 11:00:44 -0800386 if (sock_owned_by_user_nocheck(strp->sk)) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700387 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700388 return;
389 }
390
Tom Herbertbbb03022017-07-28 16:22:43 -0700391 if (strp->need_bytes) {
Doron Roberts-Kedes9d0c75b2018-04-11 15:05:16 -0700392 if (strp_peek_len(strp) < strp->need_bytes)
Tom Herbert43a0c672016-08-15 14:51:01 -0700393 return;
394 }
395
Tom Herbert96a59082016-08-28 14:43:19 -0700396 if (strp_read_sock(strp) == -ENOMEM)
Tom Herbertbbb03022017-07-28 16:22:43 -0700397 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700398}
Tom Herbert96a59082016-08-28 14:43:19 -0700399EXPORT_SYMBOL_GPL(strp_data_ready);
Tom Herbert43a0c672016-08-15 14:51:01 -0700400
Tom Herbertbbb03022017-07-28 16:22:43 -0700401static void do_strp_work(struct strparser *strp)
Tom Herbert43a0c672016-08-15 14:51:01 -0700402{
Tom Herbert96a59082016-08-28 14:43:19 -0700403 /* We need the read lock to synchronize with strp_data_ready. We
404 * need the socket lock for calling strp_read_sock.
Tom Herbert43a0c672016-08-15 14:51:01 -0700405 */
Tom Herbertbbb03022017-07-28 16:22:43 -0700406 strp->cb.lock(strp);
Tom Herbert43a0c672016-08-15 14:51:01 -0700407
Tom Herbertbbb03022017-07-28 16:22:43 -0700408 if (unlikely(strp->stopped))
Tom Herbert43a0c672016-08-15 14:51:01 -0700409 goto out;
410
Tom Herbertbbb03022017-07-28 16:22:43 -0700411 if (strp->paused)
Tom Herbert43a0c672016-08-15 14:51:01 -0700412 goto out;
413
Tom Herbert96a59082016-08-28 14:43:19 -0700414 if (strp_read_sock(strp) == -ENOMEM)
Tom Herbertbbb03022017-07-28 16:22:43 -0700415 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700416
417out:
Tom Herbertbbb03022017-07-28 16:22:43 -0700418 strp->cb.unlock(strp);
Tom Herbert43a0c672016-08-15 14:51:01 -0700419}
420
Tom Herbertbbb03022017-07-28 16:22:43 -0700421static void strp_work(struct work_struct *w)
Tom Herbert43a0c672016-08-15 14:51:01 -0700422{
Tom Herbertbbb03022017-07-28 16:22:43 -0700423 do_strp_work(container_of(w, struct strparser, work));
Tom Herbert43a0c672016-08-15 14:51:01 -0700424}
425
Tom Herbert829385f2017-10-20 16:40:43 -0700426static void strp_msg_timeout(struct work_struct *w)
Tom Herbert43a0c672016-08-15 14:51:01 -0700427{
Tom Herbert829385f2017-10-20 16:40:43 -0700428 struct strparser *strp = container_of(w, struct strparser,
429 msg_timer_work.work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700430
431 /* Message assembly timed out */
Tom Herbertbbb03022017-07-28 16:22:43 -0700432 STRP_STATS_INCR(strp->stats.msg_timeouts);
433 strp->cb.lock(strp);
Dave Watsoncd00edc2018-03-26 12:31:21 -0700434 strp->cb.abort_parser(strp, -ETIMEDOUT);
Tom Herbertbbb03022017-07-28 16:22:43 -0700435 strp->cb.unlock(strp);
436}
437
438static void strp_sock_lock(struct strparser *strp)
439{
440 lock_sock(strp->sk);
441}
442
443static void strp_sock_unlock(struct strparser *strp)
444{
Tom Herbert43a0c672016-08-15 14:51:01 -0700445 release_sock(strp->sk);
446}
447
Tom Herbertbbb03022017-07-28 16:22:43 -0700448int strp_init(struct strparser *strp, struct sock *sk,
Eric Biggers3fd87122017-08-24 14:38:51 -0700449 const struct strp_callbacks *cb)
Tom Herbert43a0c672016-08-15 14:51:01 -0700450{
Tom Herbert96a59082016-08-28 14:43:19 -0700451
Tom Herbert43a0c672016-08-15 14:51:01 -0700452 if (!cb || !cb->rcv_msg || !cb->parse_msg)
453 return -EINVAL;
454
Tom Herbertbbb03022017-07-28 16:22:43 -0700455 /* The sk (sock) arg determines the mode of the stream parser.
456 *
457 * If the sock is set then the strparser is in receive callback mode.
458 * The upper layer calls strp_data_ready to kick receive processing
459 * and strparser calls the read_sock function on the socket to
460 * get packets.
461 *
462 * If the sock is not set then the strparser is in general mode.
463 * The upper layer calls strp_process for each skb to be parsed.
464 */
465
John Fastabendf26de112017-08-15 22:30:47 -0700466 if (!sk) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700467 if (!cb->lock || !cb->unlock)
468 return -EINVAL;
469 }
Tom Herbert96a59082016-08-28 14:43:19 -0700470
Tom Herbert43a0c672016-08-15 14:51:01 -0700471 memset(strp, 0, sizeof(*strp));
472
Tom Herbertbbb03022017-07-28 16:22:43 -0700473 strp->sk = sk;
Tom Herbert43a0c672016-08-15 14:51:01 -0700474
Tom Herbertbbb03022017-07-28 16:22:43 -0700475 strp->cb.lock = cb->lock ? : strp_sock_lock;
476 strp->cb.unlock = cb->unlock ? : strp_sock_unlock;
Tom Herbert43a0c672016-08-15 14:51:01 -0700477 strp->cb.rcv_msg = cb->rcv_msg;
478 strp->cb.parse_msg = cb->parse_msg;
479 strp->cb.read_sock_done = cb->read_sock_done ? : default_read_sock_done;
Tom Herbertbbb03022017-07-28 16:22:43 -0700480 strp->cb.abort_parser = cb->abort_parser ? : strp_abort_strp;
481
Tom Herbert829385f2017-10-20 16:40:43 -0700482 INIT_DELAYED_WORK(&strp->msg_timer_work, strp_msg_timeout);
Tom Herbertbbb03022017-07-28 16:22:43 -0700483 INIT_WORK(&strp->work, strp_work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700484
485 return 0;
486}
487EXPORT_SYMBOL_GPL(strp_init);
488
Doron Roberts-Kedes7170e602018-06-06 09:33:28 -0700489/* Sock process lock held (lock_sock) */
490void __strp_unpause(struct strparser *strp)
491{
492 strp->paused = 0;
493
494 if (strp->need_bytes) {
495 if (strp_peek_len(strp) < strp->need_bytes)
496 return;
497 }
498 strp_read_sock(strp);
499}
500EXPORT_SYMBOL_GPL(__strp_unpause);
501
Tom Herbertcff6a332016-08-23 11:55:30 -0700502void strp_unpause(struct strparser *strp)
503{
Tom Herbertbbb03022017-07-28 16:22:43 -0700504 strp->paused = 0;
Tom Herbertcff6a332016-08-23 11:55:30 -0700505
Tom Herbertbbb03022017-07-28 16:22:43 -0700506 /* Sync setting paused with RX work */
Tom Herbertcff6a332016-08-23 11:55:30 -0700507 smp_mb();
508
Tom Herbertbbb03022017-07-28 16:22:43 -0700509 queue_work(strp_wq, &strp->work);
Tom Herbertcff6a332016-08-23 11:55:30 -0700510}
511EXPORT_SYMBOL_GPL(strp_unpause);
512
Tom Herbert96a59082016-08-28 14:43:19 -0700513/* strp must already be stopped so that strp_recv will no longer be called.
Tom Herbert43a0c672016-08-15 14:51:01 -0700514 * Note that strp_done is not called with the lower socket held.
515 */
516void strp_done(struct strparser *strp)
517{
Tom Herbertbbb03022017-07-28 16:22:43 -0700518 WARN_ON(!strp->stopped);
Tom Herbert43a0c672016-08-15 14:51:01 -0700519
Tom Herbert829385f2017-10-20 16:40:43 -0700520 cancel_delayed_work_sync(&strp->msg_timer_work);
Tom Herbertbbb03022017-07-28 16:22:43 -0700521 cancel_work_sync(&strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700522
Tom Herbertbbb03022017-07-28 16:22:43 -0700523 if (strp->skb_head) {
524 kfree_skb(strp->skb_head);
525 strp->skb_head = NULL;
Tom Herbert43a0c672016-08-15 14:51:01 -0700526 }
527}
528EXPORT_SYMBOL_GPL(strp_done);
529
530void strp_stop(struct strparser *strp)
531{
Tom Herbertbbb03022017-07-28 16:22:43 -0700532 strp->stopped = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -0700533}
534EXPORT_SYMBOL_GPL(strp_stop);
535
536void strp_check_rcv(struct strparser *strp)
537{
Tom Herbertbbb03022017-07-28 16:22:43 -0700538 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700539}
540EXPORT_SYMBOL_GPL(strp_check_rcv);
541
Paul Gortmaker15253b42019-04-20 23:29:48 -0400542static int __init strp_dev_init(void)
Tom Herbert43a0c672016-08-15 14:51:01 -0700543{
544 strp_wq = create_singlethread_workqueue("kstrp");
Kangjie Lu228cd2d2019-03-14 23:12:06 -0500545 if (unlikely(!strp_wq))
546 return -ENOMEM;
Tom Herbert43a0c672016-08-15 14:51:01 -0700547
548 return 0;
549}
Paul Gortmaker15253b42019-04-20 23:29:48 -0400550device_initcall(strp_dev_init);