blob: 0ba363624339c9d21b2bc06418750cd952949d54 [file] [log] [blame]
Tom Herbert43a0c672016-08-15 14:51:01 -07001/*
2 * Stream Parser
3 *
4 * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 */
10
11#include <linux/bpf.h>
12#include <linux/errno.h>
13#include <linux/errqueue.h>
14#include <linux/file.h>
15#include <linux/in.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/net.h>
19#include <linux/netdevice.h>
20#include <linux/poll.h>
21#include <linux/rculist.h>
22#include <linux/skbuff.h>
23#include <linux/socket.h>
24#include <linux/uaccess.h>
25#include <linux/workqueue.h>
26#include <net/strparser.h>
27#include <net/netns/generic.h>
28#include <net/sock.h>
Tom Herbert43a0c672016-08-15 14:51:01 -070029
30static struct workqueue_struct *strp_wq;
31
Tom Herbertbbb03022017-07-28 16:22:43 -070032struct _strp_msg {
33 /* Internal cb structure. struct strp_msg must be first for passing
Tom Herbert43a0c672016-08-15 14:51:01 -070034 * to upper layer.
35 */
Tom Herbertbbb03022017-07-28 16:22:43 -070036 struct strp_msg strp;
Tom Herbert43a0c672016-08-15 14:51:01 -070037 int accum_len;
Tom Herbert43a0c672016-08-15 14:51:01 -070038};
39
Tom Herbertbbb03022017-07-28 16:22:43 -070040static inline struct _strp_msg *_strp_msg(struct sk_buff *skb)
Tom Herbert43a0c672016-08-15 14:51:01 -070041{
Tom Herbertbbb03022017-07-28 16:22:43 -070042 return (struct _strp_msg *)((void *)skb->cb +
Tom Herbert43a0c672016-08-15 14:51:01 -070043 offsetof(struct qdisc_skb_cb, data));
44}
45
46/* Lower lock held */
Tom Herbertbbb03022017-07-28 16:22:43 -070047static void strp_abort_strp(struct strparser *strp, int err)
Tom Herbert43a0c672016-08-15 14:51:01 -070048{
Tom Herbert43a0c672016-08-15 14:51:01 -070049 /* Unrecoverable error in receive */
50
Tom Herbert829385f2017-10-20 16:40:43 -070051 cancel_delayed_work(&strp->msg_timer_work);
Tom Herbert43a0c672016-08-15 14:51:01 -070052
Tom Herbertbbb03022017-07-28 16:22:43 -070053 if (strp->stopped)
Tom Herbert43a0c672016-08-15 14:51:01 -070054 return;
55
Tom Herbertbbb03022017-07-28 16:22:43 -070056 strp->stopped = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -070057
Tom Herbertbbb03022017-07-28 16:22:43 -070058 if (strp->sk) {
59 struct sock *sk = strp->sk;
60
61 /* Report an error on the lower socket */
Dave Watsoncd00edc2018-03-26 12:31:21 -070062 sk->sk_err = -err;
Tom Herbertbbb03022017-07-28 16:22:43 -070063 sk->sk_error_report(sk);
64 }
Tom Herbert43a0c672016-08-15 14:51:01 -070065}
66
Tom Herbertbbb03022017-07-28 16:22:43 -070067static void strp_start_timer(struct strparser *strp, long timeo)
Tom Herbert43a0c672016-08-15 14:51:01 -070068{
Doron Roberts-Kedes7c5aba22018-04-20 12:11:11 -070069 if (timeo && timeo != LONG_MAX)
Tom Herbert829385f2017-10-20 16:40:43 -070070 mod_delayed_work(strp_wq, &strp->msg_timer_work, timeo);
Tom Herbert43a0c672016-08-15 14:51:01 -070071}
72
73/* Lower lock held */
74static void strp_parser_err(struct strparser *strp, int err,
75 read_descriptor_t *desc)
76{
77 desc->error = err;
Tom Herbertbbb03022017-07-28 16:22:43 -070078 kfree_skb(strp->skb_head);
79 strp->skb_head = NULL;
Tom Herbert43a0c672016-08-15 14:51:01 -070080 strp->cb.abort_parser(strp, err);
81}
82
Tom Herbert96a59082016-08-28 14:43:19 -070083static inline int strp_peek_len(struct strparser *strp)
84{
Tom Herbertbbb03022017-07-28 16:22:43 -070085 if (strp->sk) {
86 struct socket *sock = strp->sk->sk_socket;
Tom Herbert96a59082016-08-28 14:43:19 -070087
Tom Herbertbbb03022017-07-28 16:22:43 -070088 return sock->ops->peek_len(sock);
89 }
90
91 /* If we don't have an associated socket there's nothing to peek.
92 * Return int max to avoid stopping the strparser.
93 */
94
95 return INT_MAX;
Tom Herbert96a59082016-08-28 14:43:19 -070096}
97
Tom Herbert43a0c672016-08-15 14:51:01 -070098/* Lower socket lock held */
Tom Herbertbbb03022017-07-28 16:22:43 -070099static int __strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
100 unsigned int orig_offset, size_t orig_len,
101 size_t max_msg_size, long timeo)
Tom Herbert43a0c672016-08-15 14:51:01 -0700102{
103 struct strparser *strp = (struct strparser *)desc->arg.data;
Tom Herbertbbb03022017-07-28 16:22:43 -0700104 struct _strp_msg *stm;
Tom Herbert43a0c672016-08-15 14:51:01 -0700105 struct sk_buff *head, *skb;
106 size_t eaten = 0, cand_len;
107 ssize_t extra;
108 int err;
109 bool cloned_orig = false;
110
Tom Herbertbbb03022017-07-28 16:22:43 -0700111 if (strp->paused)
Tom Herbert43a0c672016-08-15 14:51:01 -0700112 return 0;
113
Tom Herbertbbb03022017-07-28 16:22:43 -0700114 head = strp->skb_head;
Tom Herbert43a0c672016-08-15 14:51:01 -0700115 if (head) {
116 /* Message already in progress */
Tom Herbert43a0c672016-08-15 14:51:01 -0700117 if (unlikely(orig_offset)) {
118 /* Getting data with a non-zero offset when a message is
119 * in progress is not expected. If it does happen, we
120 * need to clone and pull since we can't deal with
121 * offsets in the skbs for a message expect in the head.
122 */
123 orig_skb = skb_clone(orig_skb, GFP_ATOMIC);
124 if (!orig_skb) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700125 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700126 desc->error = -ENOMEM;
127 return 0;
128 }
129 if (!pskb_pull(orig_skb, orig_offset)) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700130 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700131 kfree_skb(orig_skb);
132 desc->error = -ENOMEM;
133 return 0;
134 }
135 cloned_orig = true;
136 orig_offset = 0;
137 }
138
Tom Herbertbbb03022017-07-28 16:22:43 -0700139 if (!strp->skb_nextp) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700140 /* We are going to append to the frags_list of head.
141 * Need to unshare the frag_list.
142 */
Jakub Kicinski4a9c2e32019-04-10 11:04:32 -0700143 err = skb_unclone(head, GFP_ATOMIC);
144 if (err) {
145 STRP_STATS_INCR(strp->stats.mem_fail);
146 desc->error = err;
147 return 0;
Tom Herbert43a0c672016-08-15 14:51:01 -0700148 }
149
150 if (unlikely(skb_shinfo(head)->frag_list)) {
151 /* We can't append to an sk_buff that already
152 * has a frag_list. We create a new head, point
153 * the frag_list of that to the old head, and
154 * then are able to use the old head->next for
155 * appending to the message.
156 */
157 if (WARN_ON(head->next)) {
158 desc->error = -EINVAL;
159 return 0;
160 }
161
162 skb = alloc_skb(0, GFP_ATOMIC);
163 if (!skb) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700164 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700165 desc->error = -ENOMEM;
166 return 0;
167 }
168 skb->len = head->len;
169 skb->data_len = head->len;
170 skb->truesize = head->truesize;
Tom Herbertbbb03022017-07-28 16:22:43 -0700171 *_strp_msg(skb) = *_strp_msg(head);
172 strp->skb_nextp = &head->next;
Tom Herbert43a0c672016-08-15 14:51:01 -0700173 skb_shinfo(skb)->frag_list = head;
Tom Herbertbbb03022017-07-28 16:22:43 -0700174 strp->skb_head = skb;
Tom Herbert43a0c672016-08-15 14:51:01 -0700175 head = skb;
176 } else {
Tom Herbertbbb03022017-07-28 16:22:43 -0700177 strp->skb_nextp =
Tom Herbert43a0c672016-08-15 14:51:01 -0700178 &skb_shinfo(head)->frag_list;
179 }
180 }
181 }
182
183 while (eaten < orig_len) {
184 /* Always clone since we will consume something */
185 skb = skb_clone(orig_skb, GFP_ATOMIC);
186 if (!skb) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700187 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700188 desc->error = -ENOMEM;
189 break;
190 }
191
192 cand_len = orig_len - eaten;
193
Tom Herbertbbb03022017-07-28 16:22:43 -0700194 head = strp->skb_head;
Tom Herbert43a0c672016-08-15 14:51:01 -0700195 if (!head) {
196 head = skb;
Tom Herbertbbb03022017-07-28 16:22:43 -0700197 strp->skb_head = head;
198 /* Will set skb_nextp on next packet if needed */
199 strp->skb_nextp = NULL;
200 stm = _strp_msg(head);
201 memset(stm, 0, sizeof(*stm));
202 stm->strp.offset = orig_offset + eaten;
Tom Herbert43a0c672016-08-15 14:51:01 -0700203 } else {
Vakul Garg4e485d02018-06-30 00:45:55 +0530204 /* Unclone if we are appending to an skb that we
Tom Herbert43a0c672016-08-15 14:51:01 -0700205 * already share a frag_list with.
206 */
Vakul Garg4e485d02018-06-30 00:45:55 +0530207 if (skb_has_frag_list(skb)) {
208 err = skb_unclone(skb, GFP_ATOMIC);
209 if (err) {
210 STRP_STATS_INCR(strp->stats.mem_fail);
211 desc->error = err;
212 break;
213 }
Tom Herbert43a0c672016-08-15 14:51:01 -0700214 }
215
Tom Herbertbbb03022017-07-28 16:22:43 -0700216 stm = _strp_msg(head);
217 *strp->skb_nextp = skb;
218 strp->skb_nextp = &skb->next;
Tom Herbert43a0c672016-08-15 14:51:01 -0700219 head->data_len += skb->len;
220 head->len += skb->len;
221 head->truesize += skb->truesize;
222 }
223
Tom Herbertbbb03022017-07-28 16:22:43 -0700224 if (!stm->strp.full_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700225 ssize_t len;
226
227 len = (*strp->cb.parse_msg)(strp, head);
228
229 if (!len) {
230 /* Need more header to determine length */
Tom Herbertbbb03022017-07-28 16:22:43 -0700231 if (!stm->accum_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700232 /* Start RX timer for new message */
Tom Herbertbbb03022017-07-28 16:22:43 -0700233 strp_start_timer(strp, timeo);
Tom Herbert43a0c672016-08-15 14:51:01 -0700234 }
Tom Herbertbbb03022017-07-28 16:22:43 -0700235 stm->accum_len += cand_len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700236 eaten += cand_len;
Tom Herbertbbb03022017-07-28 16:22:43 -0700237 STRP_STATS_INCR(strp->stats.need_more_hdr);
Tom Herbert43a0c672016-08-15 14:51:01 -0700238 WARN_ON(eaten != orig_len);
239 break;
240 } else if (len < 0) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700241 if (len == -ESTRPIPE && stm->accum_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700242 len = -ENODATA;
Tom Herbertbbb03022017-07-28 16:22:43 -0700243 strp->unrecov_intr = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -0700244 } else {
Tom Herbertbbb03022017-07-28 16:22:43 -0700245 strp->interrupted = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -0700246 }
Geert Uytterhoeven6d3a4c42016-10-06 15:41:49 +0200247 strp_parser_err(strp, len, desc);
Tom Herbert43a0c672016-08-15 14:51:01 -0700248 break;
Tom Herbertbbb03022017-07-28 16:22:43 -0700249 } else if (len > max_msg_size) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700250 /* Message length exceeds maximum allowed */
Tom Herbertbbb03022017-07-28 16:22:43 -0700251 STRP_STATS_INCR(strp->stats.msg_too_big);
Tom Herbert43a0c672016-08-15 14:51:01 -0700252 strp_parser_err(strp, -EMSGSIZE, desc);
253 break;
254 } else if (len <= (ssize_t)head->len -
Tom Herbertbbb03022017-07-28 16:22:43 -0700255 skb->len - stm->strp.offset) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700256 /* Length must be into new skb (and also
257 * greater than zero)
258 */
Tom Herbertbbb03022017-07-28 16:22:43 -0700259 STRP_STATS_INCR(strp->stats.bad_hdr_len);
Tom Herbert43a0c672016-08-15 14:51:01 -0700260 strp_parser_err(strp, -EPROTO, desc);
261 break;
262 }
263
Tom Herbertbbb03022017-07-28 16:22:43 -0700264 stm->strp.full_len = len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700265 }
266
Tom Herbertbbb03022017-07-28 16:22:43 -0700267 extra = (ssize_t)(stm->accum_len + cand_len) -
268 stm->strp.full_len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700269
270 if (extra < 0) {
271 /* Message not complete yet. */
Tom Herbertbbb03022017-07-28 16:22:43 -0700272 if (stm->strp.full_len - stm->accum_len >
Tom Herbert96a59082016-08-28 14:43:19 -0700273 strp_peek_len(strp)) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700274 /* Don't have the whole message in the socket
275 * buffer. Set strp->need_bytes to wait for
Tom Herbert43a0c672016-08-15 14:51:01 -0700276 * the rest of the message. Also, set "early
277 * eaten" since we've already buffered the skb
Tom Herbert96a59082016-08-28 14:43:19 -0700278 * but don't consume yet per strp_read_sock.
Tom Herbert43a0c672016-08-15 14:51:01 -0700279 */
280
Tom Herbertbbb03022017-07-28 16:22:43 -0700281 if (!stm->accum_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700282 /* Start RX timer for new message */
Tom Herbertbbb03022017-07-28 16:22:43 -0700283 strp_start_timer(strp, timeo);
Tom Herbert43a0c672016-08-15 14:51:01 -0700284 }
285
Doron Roberts-Kedes9d0c75b2018-04-11 15:05:16 -0700286 stm->accum_len += cand_len;
Doron Roberts-Kedes977c7112018-06-26 18:33:33 -0700287 eaten += cand_len;
Tom Herbertbbb03022017-07-28 16:22:43 -0700288 strp->need_bytes = stm->strp.full_len -
289 stm->accum_len;
Tom Herbertbbb03022017-07-28 16:22:43 -0700290 STRP_STATS_ADD(strp->stats.bytes, cand_len);
Tom Herbert43a0c672016-08-15 14:51:01 -0700291 desc->count = 0; /* Stop reading socket */
292 break;
293 }
Tom Herbertbbb03022017-07-28 16:22:43 -0700294 stm->accum_len += cand_len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700295 eaten += cand_len;
296 WARN_ON(eaten != orig_len);
297 break;
298 }
299
Jakub Kicinski93e21252019-04-10 13:18:57 -0700300 /* Positive extra indicates more bytes than needed for the
Tom Herbert43a0c672016-08-15 14:51:01 -0700301 * message
302 */
303
304 WARN_ON(extra > cand_len);
305
306 eaten += (cand_len - extra);
307
308 /* Hurray, we have a new message! */
Tom Herbert829385f2017-10-20 16:40:43 -0700309 cancel_delayed_work(&strp->msg_timer_work);
Tom Herbertbbb03022017-07-28 16:22:43 -0700310 strp->skb_head = NULL;
Doron Roberts-Kedes9d0c75b2018-04-11 15:05:16 -0700311 strp->need_bytes = 0;
Tom Herbertbbb03022017-07-28 16:22:43 -0700312 STRP_STATS_INCR(strp->stats.msgs);
Tom Herbert43a0c672016-08-15 14:51:01 -0700313
314 /* Give skb to upper layer */
315 strp->cb.rcv_msg(strp, head);
316
Tom Herbertbbb03022017-07-28 16:22:43 -0700317 if (unlikely(strp->paused)) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700318 /* Upper layer paused strp */
319 break;
320 }
321 }
322
323 if (cloned_orig)
324 kfree_skb(orig_skb);
325
Tom Herbertbbb03022017-07-28 16:22:43 -0700326 STRP_STATS_ADD(strp->stats.bytes, eaten);
Tom Herbert43a0c672016-08-15 14:51:01 -0700327
328 return eaten;
329}
330
Tom Herbertbbb03022017-07-28 16:22:43 -0700331int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
332 unsigned int orig_offset, size_t orig_len,
333 size_t max_msg_size, long timeo)
334{
335 read_descriptor_t desc; /* Dummy arg to strp_recv */
336
337 desc.arg.data = strp;
338
339 return __strp_recv(&desc, orig_skb, orig_offset, orig_len,
340 max_msg_size, timeo);
341}
342EXPORT_SYMBOL_GPL(strp_process);
343
344static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
345 unsigned int orig_offset, size_t orig_len)
346{
347 struct strparser *strp = (struct strparser *)desc->arg.data;
348
349 return __strp_recv(desc, orig_skb, orig_offset, orig_len,
350 strp->sk->sk_rcvbuf, strp->sk->sk_rcvtimeo);
351}
352
Tom Herbert43a0c672016-08-15 14:51:01 -0700353static int default_read_sock_done(struct strparser *strp, int err)
354{
355 return err;
356}
357
358/* Called with lock held on lower socket */
Tom Herbert96a59082016-08-28 14:43:19 -0700359static int strp_read_sock(struct strparser *strp)
Tom Herbert43a0c672016-08-15 14:51:01 -0700360{
Tom Herbert96a59082016-08-28 14:43:19 -0700361 struct socket *sock = strp->sk->sk_socket;
Tom Herbert43a0c672016-08-15 14:51:01 -0700362 read_descriptor_t desc;
363
John Fastabendf26de112017-08-15 22:30:47 -0700364 if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
365 return -EBUSY;
366
Tom Herbert43a0c672016-08-15 14:51:01 -0700367 desc.arg.data = strp;
368 desc.error = 0;
369 desc.count = 1; /* give more than one skb per call */
370
Tom Herbert96a59082016-08-28 14:43:19 -0700371 /* sk should be locked here, so okay to do read_sock */
372 sock->ops->read_sock(strp->sk, &desc, strp_recv);
Tom Herbert43a0c672016-08-15 14:51:01 -0700373
374 desc.error = strp->cb.read_sock_done(strp, desc.error);
375
376 return desc.error;
377}
378
379/* Lower sock lock held */
Tom Herbert96a59082016-08-28 14:43:19 -0700380void strp_data_ready(struct strparser *strp)
Tom Herbert43a0c672016-08-15 14:51:01 -0700381{
Vakul Garg456488c2018-06-21 03:29:49 +0530382 if (unlikely(strp->stopped) || strp->paused)
Tom Herbert43a0c672016-08-15 14:51:01 -0700383 return;
384
Tom Herbertbbb03022017-07-28 16:22:43 -0700385 /* This check is needed to synchronize with do_strp_work.
386 * do_strp_work acquires a process lock (lock_sock) whereas
Tom Herbert43a0c672016-08-15 14:51:01 -0700387 * the lock held here is bh_lock_sock. The two locks can be
388 * held by different threads at the same time, but bh_lock_sock
389 * allows a thread in BH context to safely check if the process
390 * lock is held. In this case, if the lock is held, queue work.
391 */
Tom Herbertd66fa9e2017-12-28 11:00:44 -0800392 if (sock_owned_by_user_nocheck(strp->sk)) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700393 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700394 return;
395 }
396
Tom Herbertbbb03022017-07-28 16:22:43 -0700397 if (strp->need_bytes) {
Doron Roberts-Kedes9d0c75b2018-04-11 15:05:16 -0700398 if (strp_peek_len(strp) < strp->need_bytes)
Tom Herbert43a0c672016-08-15 14:51:01 -0700399 return;
400 }
401
Tom Herbert96a59082016-08-28 14:43:19 -0700402 if (strp_read_sock(strp) == -ENOMEM)
Tom Herbertbbb03022017-07-28 16:22:43 -0700403 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700404}
Tom Herbert96a59082016-08-28 14:43:19 -0700405EXPORT_SYMBOL_GPL(strp_data_ready);
Tom Herbert43a0c672016-08-15 14:51:01 -0700406
Tom Herbertbbb03022017-07-28 16:22:43 -0700407static void do_strp_work(struct strparser *strp)
Tom Herbert43a0c672016-08-15 14:51:01 -0700408{
Tom Herbert96a59082016-08-28 14:43:19 -0700409 /* We need the read lock to synchronize with strp_data_ready. We
410 * need the socket lock for calling strp_read_sock.
Tom Herbert43a0c672016-08-15 14:51:01 -0700411 */
Tom Herbertbbb03022017-07-28 16:22:43 -0700412 strp->cb.lock(strp);
Tom Herbert43a0c672016-08-15 14:51:01 -0700413
Tom Herbertbbb03022017-07-28 16:22:43 -0700414 if (unlikely(strp->stopped))
Tom Herbert43a0c672016-08-15 14:51:01 -0700415 goto out;
416
Tom Herbertbbb03022017-07-28 16:22:43 -0700417 if (strp->paused)
Tom Herbert43a0c672016-08-15 14:51:01 -0700418 goto out;
419
Tom Herbert96a59082016-08-28 14:43:19 -0700420 if (strp_read_sock(strp) == -ENOMEM)
Tom Herbertbbb03022017-07-28 16:22:43 -0700421 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700422
423out:
Tom Herbertbbb03022017-07-28 16:22:43 -0700424 strp->cb.unlock(strp);
Tom Herbert43a0c672016-08-15 14:51:01 -0700425}
426
Tom Herbertbbb03022017-07-28 16:22:43 -0700427static void strp_work(struct work_struct *w)
Tom Herbert43a0c672016-08-15 14:51:01 -0700428{
Tom Herbertbbb03022017-07-28 16:22:43 -0700429 do_strp_work(container_of(w, struct strparser, work));
Tom Herbert43a0c672016-08-15 14:51:01 -0700430}
431
Tom Herbert829385f2017-10-20 16:40:43 -0700432static void strp_msg_timeout(struct work_struct *w)
Tom Herbert43a0c672016-08-15 14:51:01 -0700433{
Tom Herbert829385f2017-10-20 16:40:43 -0700434 struct strparser *strp = container_of(w, struct strparser,
435 msg_timer_work.work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700436
437 /* Message assembly timed out */
Tom Herbertbbb03022017-07-28 16:22:43 -0700438 STRP_STATS_INCR(strp->stats.msg_timeouts);
439 strp->cb.lock(strp);
Dave Watsoncd00edc2018-03-26 12:31:21 -0700440 strp->cb.abort_parser(strp, -ETIMEDOUT);
Tom Herbertbbb03022017-07-28 16:22:43 -0700441 strp->cb.unlock(strp);
442}
443
444static void strp_sock_lock(struct strparser *strp)
445{
446 lock_sock(strp->sk);
447}
448
449static void strp_sock_unlock(struct strparser *strp)
450{
Tom Herbert43a0c672016-08-15 14:51:01 -0700451 release_sock(strp->sk);
452}
453
Tom Herbertbbb03022017-07-28 16:22:43 -0700454int strp_init(struct strparser *strp, struct sock *sk,
Eric Biggers3fd87122017-08-24 14:38:51 -0700455 const struct strp_callbacks *cb)
Tom Herbert43a0c672016-08-15 14:51:01 -0700456{
Tom Herbert96a59082016-08-28 14:43:19 -0700457
Tom Herbert43a0c672016-08-15 14:51:01 -0700458 if (!cb || !cb->rcv_msg || !cb->parse_msg)
459 return -EINVAL;
460
Tom Herbertbbb03022017-07-28 16:22:43 -0700461 /* The sk (sock) arg determines the mode of the stream parser.
462 *
463 * If the sock is set then the strparser is in receive callback mode.
464 * The upper layer calls strp_data_ready to kick receive processing
465 * and strparser calls the read_sock function on the socket to
466 * get packets.
467 *
468 * If the sock is not set then the strparser is in general mode.
469 * The upper layer calls strp_process for each skb to be parsed.
470 */
471
John Fastabendf26de112017-08-15 22:30:47 -0700472 if (!sk) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700473 if (!cb->lock || !cb->unlock)
474 return -EINVAL;
475 }
Tom Herbert96a59082016-08-28 14:43:19 -0700476
Tom Herbert43a0c672016-08-15 14:51:01 -0700477 memset(strp, 0, sizeof(*strp));
478
Tom Herbertbbb03022017-07-28 16:22:43 -0700479 strp->sk = sk;
Tom Herbert43a0c672016-08-15 14:51:01 -0700480
Tom Herbertbbb03022017-07-28 16:22:43 -0700481 strp->cb.lock = cb->lock ? : strp_sock_lock;
482 strp->cb.unlock = cb->unlock ? : strp_sock_unlock;
Tom Herbert43a0c672016-08-15 14:51:01 -0700483 strp->cb.rcv_msg = cb->rcv_msg;
484 strp->cb.parse_msg = cb->parse_msg;
485 strp->cb.read_sock_done = cb->read_sock_done ? : default_read_sock_done;
Tom Herbertbbb03022017-07-28 16:22:43 -0700486 strp->cb.abort_parser = cb->abort_parser ? : strp_abort_strp;
487
Tom Herbert829385f2017-10-20 16:40:43 -0700488 INIT_DELAYED_WORK(&strp->msg_timer_work, strp_msg_timeout);
Tom Herbertbbb03022017-07-28 16:22:43 -0700489 INIT_WORK(&strp->work, strp_work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700490
491 return 0;
492}
493EXPORT_SYMBOL_GPL(strp_init);
494
Doron Roberts-Kedes7170e602018-06-06 09:33:28 -0700495/* Sock process lock held (lock_sock) */
496void __strp_unpause(struct strparser *strp)
497{
498 strp->paused = 0;
499
500 if (strp->need_bytes) {
501 if (strp_peek_len(strp) < strp->need_bytes)
502 return;
503 }
504 strp_read_sock(strp);
505}
506EXPORT_SYMBOL_GPL(__strp_unpause);
507
Tom Herbertcff6a332016-08-23 11:55:30 -0700508void strp_unpause(struct strparser *strp)
509{
Tom Herbertbbb03022017-07-28 16:22:43 -0700510 strp->paused = 0;
Tom Herbertcff6a332016-08-23 11:55:30 -0700511
Tom Herbertbbb03022017-07-28 16:22:43 -0700512 /* Sync setting paused with RX work */
Tom Herbertcff6a332016-08-23 11:55:30 -0700513 smp_mb();
514
Tom Herbertbbb03022017-07-28 16:22:43 -0700515 queue_work(strp_wq, &strp->work);
Tom Herbertcff6a332016-08-23 11:55:30 -0700516}
517EXPORT_SYMBOL_GPL(strp_unpause);
518
Tom Herbert96a59082016-08-28 14:43:19 -0700519/* strp must already be stopped so that strp_recv will no longer be called.
Tom Herbert43a0c672016-08-15 14:51:01 -0700520 * Note that strp_done is not called with the lower socket held.
521 */
522void strp_done(struct strparser *strp)
523{
Tom Herbertbbb03022017-07-28 16:22:43 -0700524 WARN_ON(!strp->stopped);
Tom Herbert43a0c672016-08-15 14:51:01 -0700525
Tom Herbert829385f2017-10-20 16:40:43 -0700526 cancel_delayed_work_sync(&strp->msg_timer_work);
Tom Herbertbbb03022017-07-28 16:22:43 -0700527 cancel_work_sync(&strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700528
Tom Herbertbbb03022017-07-28 16:22:43 -0700529 if (strp->skb_head) {
530 kfree_skb(strp->skb_head);
531 strp->skb_head = NULL;
Tom Herbert43a0c672016-08-15 14:51:01 -0700532 }
533}
534EXPORT_SYMBOL_GPL(strp_done);
535
536void strp_stop(struct strparser *strp)
537{
Tom Herbertbbb03022017-07-28 16:22:43 -0700538 strp->stopped = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -0700539}
540EXPORT_SYMBOL_GPL(strp_stop);
541
542void strp_check_rcv(struct strparser *strp)
543{
Tom Herbertbbb03022017-07-28 16:22:43 -0700544 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700545}
546EXPORT_SYMBOL_GPL(strp_check_rcv);
547
548static int __init strp_mod_init(void)
549{
550 strp_wq = create_singlethread_workqueue("kstrp");
Kangjie Lu228cd2d2019-03-14 23:12:06 -0500551 if (unlikely(!strp_wq))
552 return -ENOMEM;
Tom Herbert43a0c672016-08-15 14:51:01 -0700553
554 return 0;
555}
556
557static void __exit strp_mod_exit(void)
558{
WANG Congf78ef7c2017-03-03 12:21:14 -0800559 destroy_workqueue(strp_wq);
Tom Herbert43a0c672016-08-15 14:51:01 -0700560}
561module_init(strp_mod_init);
562module_exit(strp_mod_exit);
563MODULE_LICENSE("GPL");