blob: 0d18fbc6f8702453ce544e8cdd27c473d6ea48f7 [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;
38 int early_eaten;
39};
40
Tom Herbertbbb03022017-07-28 16:22:43 -070041static inline struct _strp_msg *_strp_msg(struct sk_buff *skb)
Tom Herbert43a0c672016-08-15 14:51:01 -070042{
Tom Herbertbbb03022017-07-28 16:22:43 -070043 return (struct _strp_msg *)((void *)skb->cb +
Tom Herbert43a0c672016-08-15 14:51:01 -070044 offsetof(struct qdisc_skb_cb, data));
45}
46
47/* Lower lock held */
Tom Herbertbbb03022017-07-28 16:22:43 -070048static void strp_abort_strp(struct strparser *strp, int err)
Tom Herbert43a0c672016-08-15 14:51:01 -070049{
Tom Herbert43a0c672016-08-15 14:51:01 -070050 /* Unrecoverable error in receive */
51
Tom Herbertbbb03022017-07-28 16:22:43 -070052 del_timer(&strp->msg_timer);
Tom Herbert43a0c672016-08-15 14:51:01 -070053
Tom Herbertbbb03022017-07-28 16:22:43 -070054 if (strp->stopped)
Tom Herbert43a0c672016-08-15 14:51:01 -070055 return;
56
Tom Herbertbbb03022017-07-28 16:22:43 -070057 strp->stopped = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -070058
Tom Herbertbbb03022017-07-28 16:22:43 -070059 if (strp->sk) {
60 struct sock *sk = strp->sk;
61
62 /* Report an error on the lower socket */
63 sk->sk_err = err;
64 sk->sk_error_report(sk);
65 }
Tom Herbert43a0c672016-08-15 14:51:01 -070066}
67
Tom Herbertbbb03022017-07-28 16:22:43 -070068static void strp_start_timer(struct strparser *strp, long timeo)
Tom Herbert43a0c672016-08-15 14:51:01 -070069{
Tom Herbertbbb03022017-07-28 16:22:43 -070070 if (timeo)
71 mod_timer(&strp->msg_timer, timeo);
Tom Herbert43a0c672016-08-15 14:51:01 -070072}
73
74/* Lower lock held */
75static void strp_parser_err(struct strparser *strp, int err,
76 read_descriptor_t *desc)
77{
78 desc->error = err;
Tom Herbertbbb03022017-07-28 16:22:43 -070079 kfree_skb(strp->skb_head);
80 strp->skb_head = NULL;
Tom Herbert43a0c672016-08-15 14:51:01 -070081 strp->cb.abort_parser(strp, err);
82}
83
Tom Herbert96a59082016-08-28 14:43:19 -070084static inline int strp_peek_len(struct strparser *strp)
85{
Tom Herbertbbb03022017-07-28 16:22:43 -070086 if (strp->sk) {
87 struct socket *sock = strp->sk->sk_socket;
Tom Herbert96a59082016-08-28 14:43:19 -070088
Tom Herbertbbb03022017-07-28 16:22:43 -070089 return sock->ops->peek_len(sock);
90 }
91
92 /* If we don't have an associated socket there's nothing to peek.
93 * Return int max to avoid stopping the strparser.
94 */
95
96 return INT_MAX;
Tom Herbert96a59082016-08-28 14:43:19 -070097}
98
Tom Herbert43a0c672016-08-15 14:51:01 -070099/* Lower socket lock held */
Tom Herbertbbb03022017-07-28 16:22:43 -0700100static int __strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
101 unsigned int orig_offset, size_t orig_len,
102 size_t max_msg_size, long timeo)
Tom Herbert43a0c672016-08-15 14:51:01 -0700103{
104 struct strparser *strp = (struct strparser *)desc->arg.data;
Tom Herbertbbb03022017-07-28 16:22:43 -0700105 struct _strp_msg *stm;
Tom Herbert43a0c672016-08-15 14:51:01 -0700106 struct sk_buff *head, *skb;
107 size_t eaten = 0, cand_len;
108 ssize_t extra;
109 int err;
110 bool cloned_orig = false;
111
Tom Herbertbbb03022017-07-28 16:22:43 -0700112 if (strp->paused)
Tom Herbert43a0c672016-08-15 14:51:01 -0700113 return 0;
114
Tom Herbertbbb03022017-07-28 16:22:43 -0700115 head = strp->skb_head;
Tom Herbert43a0c672016-08-15 14:51:01 -0700116 if (head) {
117 /* Message already in progress */
118
Tom Herbertbbb03022017-07-28 16:22:43 -0700119 stm = _strp_msg(head);
120 if (unlikely(stm->early_eaten)) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700121 /* Already some number of bytes on the receive sock
Tom Herbertbbb03022017-07-28 16:22:43 -0700122 * data saved in skb_head, just indicate they
Tom Herbert43a0c672016-08-15 14:51:01 -0700123 * are consumed.
124 */
Tom Herbertbbb03022017-07-28 16:22:43 -0700125 eaten = orig_len <= stm->early_eaten ?
126 orig_len : stm->early_eaten;
127 stm->early_eaten -= eaten;
Tom Herbert43a0c672016-08-15 14:51:01 -0700128
129 return eaten;
130 }
131
132 if (unlikely(orig_offset)) {
133 /* Getting data with a non-zero offset when a message is
134 * in progress is not expected. If it does happen, we
135 * need to clone and pull since we can't deal with
136 * offsets in the skbs for a message expect in the head.
137 */
138 orig_skb = skb_clone(orig_skb, GFP_ATOMIC);
139 if (!orig_skb) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700140 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700141 desc->error = -ENOMEM;
142 return 0;
143 }
144 if (!pskb_pull(orig_skb, orig_offset)) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700145 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700146 kfree_skb(orig_skb);
147 desc->error = -ENOMEM;
148 return 0;
149 }
150 cloned_orig = true;
151 orig_offset = 0;
152 }
153
Tom Herbertbbb03022017-07-28 16:22:43 -0700154 if (!strp->skb_nextp) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700155 /* We are going to append to the frags_list of head.
156 * Need to unshare the frag_list.
157 */
158 err = skb_unclone(head, GFP_ATOMIC);
159 if (err) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700160 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700161 desc->error = err;
162 return 0;
163 }
164
165 if (unlikely(skb_shinfo(head)->frag_list)) {
166 /* We can't append to an sk_buff that already
167 * has a frag_list. We create a new head, point
168 * the frag_list of that to the old head, and
169 * then are able to use the old head->next for
170 * appending to the message.
171 */
172 if (WARN_ON(head->next)) {
173 desc->error = -EINVAL;
174 return 0;
175 }
176
177 skb = alloc_skb(0, GFP_ATOMIC);
178 if (!skb) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700179 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700180 desc->error = -ENOMEM;
181 return 0;
182 }
183 skb->len = head->len;
184 skb->data_len = head->len;
185 skb->truesize = head->truesize;
Tom Herbertbbb03022017-07-28 16:22:43 -0700186 *_strp_msg(skb) = *_strp_msg(head);
187 strp->skb_nextp = &head->next;
Tom Herbert43a0c672016-08-15 14:51:01 -0700188 skb_shinfo(skb)->frag_list = head;
Tom Herbertbbb03022017-07-28 16:22:43 -0700189 strp->skb_head = skb;
Tom Herbert43a0c672016-08-15 14:51:01 -0700190 head = skb;
191 } else {
Tom Herbertbbb03022017-07-28 16:22:43 -0700192 strp->skb_nextp =
Tom Herbert43a0c672016-08-15 14:51:01 -0700193 &skb_shinfo(head)->frag_list;
194 }
195 }
196 }
197
198 while (eaten < orig_len) {
199 /* Always clone since we will consume something */
200 skb = skb_clone(orig_skb, GFP_ATOMIC);
201 if (!skb) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700202 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700203 desc->error = -ENOMEM;
204 break;
205 }
206
207 cand_len = orig_len - eaten;
208
Tom Herbertbbb03022017-07-28 16:22:43 -0700209 head = strp->skb_head;
Tom Herbert43a0c672016-08-15 14:51:01 -0700210 if (!head) {
211 head = skb;
Tom Herbertbbb03022017-07-28 16:22:43 -0700212 strp->skb_head = head;
213 /* Will set skb_nextp on next packet if needed */
214 strp->skb_nextp = NULL;
215 stm = _strp_msg(head);
216 memset(stm, 0, sizeof(*stm));
217 stm->strp.offset = orig_offset + eaten;
Tom Herbert43a0c672016-08-15 14:51:01 -0700218 } else {
219 /* Unclone since we may be appending to an skb that we
220 * already share a frag_list with.
221 */
222 err = skb_unclone(skb, GFP_ATOMIC);
223 if (err) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700224 STRP_STATS_INCR(strp->stats.mem_fail);
Tom Herbert43a0c672016-08-15 14:51:01 -0700225 desc->error = err;
226 break;
227 }
228
Tom Herbertbbb03022017-07-28 16:22:43 -0700229 stm = _strp_msg(head);
230 *strp->skb_nextp = skb;
231 strp->skb_nextp = &skb->next;
Tom Herbert43a0c672016-08-15 14:51:01 -0700232 head->data_len += skb->len;
233 head->len += skb->len;
234 head->truesize += skb->truesize;
235 }
236
Tom Herbertbbb03022017-07-28 16:22:43 -0700237 if (!stm->strp.full_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700238 ssize_t len;
239
240 len = (*strp->cb.parse_msg)(strp, head);
241
242 if (!len) {
243 /* Need more header to determine length */
Tom Herbertbbb03022017-07-28 16:22:43 -0700244 if (!stm->accum_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700245 /* Start RX timer for new message */
Tom Herbertbbb03022017-07-28 16:22:43 -0700246 strp_start_timer(strp, timeo);
Tom Herbert43a0c672016-08-15 14:51:01 -0700247 }
Tom Herbertbbb03022017-07-28 16:22:43 -0700248 stm->accum_len += cand_len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700249 eaten += cand_len;
Tom Herbertbbb03022017-07-28 16:22:43 -0700250 STRP_STATS_INCR(strp->stats.need_more_hdr);
Tom Herbert43a0c672016-08-15 14:51:01 -0700251 WARN_ON(eaten != orig_len);
252 break;
253 } else if (len < 0) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700254 if (len == -ESTRPIPE && stm->accum_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700255 len = -ENODATA;
Tom Herbertbbb03022017-07-28 16:22:43 -0700256 strp->unrecov_intr = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -0700257 } else {
Tom Herbertbbb03022017-07-28 16:22:43 -0700258 strp->interrupted = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -0700259 }
Geert Uytterhoeven6d3a4c42016-10-06 15:41:49 +0200260 strp_parser_err(strp, len, desc);
Tom Herbert43a0c672016-08-15 14:51:01 -0700261 break;
Tom Herbertbbb03022017-07-28 16:22:43 -0700262 } else if (len > max_msg_size) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700263 /* Message length exceeds maximum allowed */
Tom Herbertbbb03022017-07-28 16:22:43 -0700264 STRP_STATS_INCR(strp->stats.msg_too_big);
Tom Herbert43a0c672016-08-15 14:51:01 -0700265 strp_parser_err(strp, -EMSGSIZE, desc);
266 break;
267 } else if (len <= (ssize_t)head->len -
Tom Herbertbbb03022017-07-28 16:22:43 -0700268 skb->len - stm->strp.offset) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700269 /* Length must be into new skb (and also
270 * greater than zero)
271 */
Tom Herbertbbb03022017-07-28 16:22:43 -0700272 STRP_STATS_INCR(strp->stats.bad_hdr_len);
Tom Herbert43a0c672016-08-15 14:51:01 -0700273 strp_parser_err(strp, -EPROTO, desc);
274 break;
275 }
276
Tom Herbertbbb03022017-07-28 16:22:43 -0700277 stm->strp.full_len = len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700278 }
279
Tom Herbertbbb03022017-07-28 16:22:43 -0700280 extra = (ssize_t)(stm->accum_len + cand_len) -
281 stm->strp.full_len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700282
283 if (extra < 0) {
284 /* Message not complete yet. */
Tom Herbertbbb03022017-07-28 16:22:43 -0700285 if (stm->strp.full_len - stm->accum_len >
Tom Herbert96a59082016-08-28 14:43:19 -0700286 strp_peek_len(strp)) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700287 /* Don't have the whole message in the socket
288 * buffer. Set strp->need_bytes to wait for
Tom Herbert43a0c672016-08-15 14:51:01 -0700289 * the rest of the message. Also, set "early
290 * eaten" since we've already buffered the skb
Tom Herbert96a59082016-08-28 14:43:19 -0700291 * but don't consume yet per strp_read_sock.
Tom Herbert43a0c672016-08-15 14:51:01 -0700292 */
293
Tom Herbertbbb03022017-07-28 16:22:43 -0700294 if (!stm->accum_len) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700295 /* Start RX timer for new message */
Tom Herbertbbb03022017-07-28 16:22:43 -0700296 strp_start_timer(strp, timeo);
Tom Herbert43a0c672016-08-15 14:51:01 -0700297 }
298
Tom Herbertbbb03022017-07-28 16:22:43 -0700299 strp->need_bytes = stm->strp.full_len -
300 stm->accum_len;
301 stm->accum_len += cand_len;
302 stm->early_eaten = cand_len;
303 STRP_STATS_ADD(strp->stats.bytes, cand_len);
Tom Herbert43a0c672016-08-15 14:51:01 -0700304 desc->count = 0; /* Stop reading socket */
305 break;
306 }
Tom Herbertbbb03022017-07-28 16:22:43 -0700307 stm->accum_len += cand_len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700308 eaten += cand_len;
309 WARN_ON(eaten != orig_len);
310 break;
311 }
312
313 /* Positive extra indicates ore bytes than needed for the
314 * message
315 */
316
317 WARN_ON(extra > cand_len);
318
319 eaten += (cand_len - extra);
320
321 /* Hurray, we have a new message! */
Tom Herbertbbb03022017-07-28 16:22:43 -0700322 del_timer(&strp->msg_timer);
323 strp->skb_head = NULL;
324 STRP_STATS_INCR(strp->stats.msgs);
Tom Herbert43a0c672016-08-15 14:51:01 -0700325
326 /* Give skb to upper layer */
327 strp->cb.rcv_msg(strp, head);
328
Tom Herbertbbb03022017-07-28 16:22:43 -0700329 if (unlikely(strp->paused)) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700330 /* Upper layer paused strp */
331 break;
332 }
333 }
334
335 if (cloned_orig)
336 kfree_skb(orig_skb);
337
Tom Herbertbbb03022017-07-28 16:22:43 -0700338 STRP_STATS_ADD(strp->stats.bytes, eaten);
Tom Herbert43a0c672016-08-15 14:51:01 -0700339
340 return eaten;
341}
342
Tom Herbertbbb03022017-07-28 16:22:43 -0700343int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
344 unsigned int orig_offset, size_t orig_len,
345 size_t max_msg_size, long timeo)
346{
347 read_descriptor_t desc; /* Dummy arg to strp_recv */
348
349 desc.arg.data = strp;
350
351 return __strp_recv(&desc, orig_skb, orig_offset, orig_len,
352 max_msg_size, timeo);
353}
354EXPORT_SYMBOL_GPL(strp_process);
355
356static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
357 unsigned int orig_offset, size_t orig_len)
358{
359 struct strparser *strp = (struct strparser *)desc->arg.data;
360
361 return __strp_recv(desc, orig_skb, orig_offset, orig_len,
362 strp->sk->sk_rcvbuf, strp->sk->sk_rcvtimeo);
363}
364
Tom Herbert43a0c672016-08-15 14:51:01 -0700365static int default_read_sock_done(struct strparser *strp, int err)
366{
367 return err;
368}
369
370/* Called with lock held on lower socket */
Tom Herbert96a59082016-08-28 14:43:19 -0700371static int strp_read_sock(struct strparser *strp)
Tom Herbert43a0c672016-08-15 14:51:01 -0700372{
Tom Herbert96a59082016-08-28 14:43:19 -0700373 struct socket *sock = strp->sk->sk_socket;
Tom Herbert43a0c672016-08-15 14:51:01 -0700374 read_descriptor_t desc;
375
376 desc.arg.data = strp;
377 desc.error = 0;
378 desc.count = 1; /* give more than one skb per call */
379
Tom Herbert96a59082016-08-28 14:43:19 -0700380 /* sk should be locked here, so okay to do read_sock */
381 sock->ops->read_sock(strp->sk, &desc, strp_recv);
Tom Herbert43a0c672016-08-15 14:51:01 -0700382
383 desc.error = strp->cb.read_sock_done(strp, desc.error);
384
385 return desc.error;
386}
387
388/* Lower sock lock held */
Tom Herbert96a59082016-08-28 14:43:19 -0700389void strp_data_ready(struct strparser *strp)
Tom Herbert43a0c672016-08-15 14:51:01 -0700390{
Tom Herbertbbb03022017-07-28 16:22:43 -0700391 if (unlikely(strp->stopped))
Tom Herbert43a0c672016-08-15 14:51:01 -0700392 return;
393
Tom Herbertbbb03022017-07-28 16:22:43 -0700394 /* This check is needed to synchronize with do_strp_work.
395 * do_strp_work acquires a process lock (lock_sock) whereas
Tom Herbert43a0c672016-08-15 14:51:01 -0700396 * the lock held here is bh_lock_sock. The two locks can be
397 * held by different threads at the same time, but bh_lock_sock
398 * allows a thread in BH context to safely check if the process
399 * lock is held. In this case, if the lock is held, queue work.
400 */
Tom Herbert96a59082016-08-28 14:43:19 -0700401 if (sock_owned_by_user(strp->sk)) {
Tom Herbertbbb03022017-07-28 16:22:43 -0700402 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700403 return;
404 }
405
Tom Herbertbbb03022017-07-28 16:22:43 -0700406 if (strp->paused)
Tom Herbert43a0c672016-08-15 14:51:01 -0700407 return;
408
Tom Herbertbbb03022017-07-28 16:22:43 -0700409 if (strp->need_bytes) {
410 if (strp_peek_len(strp) >= strp->need_bytes)
411 strp->need_bytes = 0;
Tom Herbert43a0c672016-08-15 14:51:01 -0700412 else
413 return;
414 }
415
Tom Herbert96a59082016-08-28 14:43:19 -0700416 if (strp_read_sock(strp) == -ENOMEM)
Tom Herbertbbb03022017-07-28 16:22:43 -0700417 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700418}
Tom Herbert96a59082016-08-28 14:43:19 -0700419EXPORT_SYMBOL_GPL(strp_data_ready);
Tom Herbert43a0c672016-08-15 14:51:01 -0700420
Tom Herbertbbb03022017-07-28 16:22:43 -0700421static void do_strp_work(struct strparser *strp)
Tom Herbert43a0c672016-08-15 14:51:01 -0700422{
423 read_descriptor_t rd_desc;
Tom Herbert43a0c672016-08-15 14:51:01 -0700424
Tom Herbert96a59082016-08-28 14:43:19 -0700425 /* We need the read lock to synchronize with strp_data_ready. We
426 * need the socket lock for calling strp_read_sock.
Tom Herbert43a0c672016-08-15 14:51:01 -0700427 */
Tom Herbertbbb03022017-07-28 16:22:43 -0700428 strp->cb.lock(strp);
Tom Herbert43a0c672016-08-15 14:51:01 -0700429
Tom Herbertbbb03022017-07-28 16:22:43 -0700430 if (unlikely(strp->stopped))
Tom Herbert43a0c672016-08-15 14:51:01 -0700431 goto out;
432
Tom Herbertbbb03022017-07-28 16:22:43 -0700433 if (strp->paused)
Tom Herbert43a0c672016-08-15 14:51:01 -0700434 goto out;
435
436 rd_desc.arg.data = strp;
437
Tom Herbert96a59082016-08-28 14:43:19 -0700438 if (strp_read_sock(strp) == -ENOMEM)
Tom Herbertbbb03022017-07-28 16:22:43 -0700439 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700440
441out:
Tom Herbertbbb03022017-07-28 16:22:43 -0700442 strp->cb.unlock(strp);
Tom Herbert43a0c672016-08-15 14:51:01 -0700443}
444
Tom Herbertbbb03022017-07-28 16:22:43 -0700445static void strp_work(struct work_struct *w)
Tom Herbert43a0c672016-08-15 14:51:01 -0700446{
Tom Herbertbbb03022017-07-28 16:22:43 -0700447 do_strp_work(container_of(w, struct strparser, work));
Tom Herbert43a0c672016-08-15 14:51:01 -0700448}
449
Tom Herbertbbb03022017-07-28 16:22:43 -0700450static void strp_msg_timeout(unsigned long arg)
Tom Herbert43a0c672016-08-15 14:51:01 -0700451{
452 struct strparser *strp = (struct strparser *)arg;
453
454 /* Message assembly timed out */
Tom Herbertbbb03022017-07-28 16:22:43 -0700455 STRP_STATS_INCR(strp->stats.msg_timeouts);
456 strp->cb.lock(strp);
Tom Herbert43a0c672016-08-15 14:51:01 -0700457 strp->cb.abort_parser(strp, ETIMEDOUT);
Tom Herbertbbb03022017-07-28 16:22:43 -0700458 strp->cb.unlock(strp);
459}
460
461static void strp_sock_lock(struct strparser *strp)
462{
463 lock_sock(strp->sk);
464}
465
466static void strp_sock_unlock(struct strparser *strp)
467{
Tom Herbert43a0c672016-08-15 14:51:01 -0700468 release_sock(strp->sk);
469}
470
Tom Herbertbbb03022017-07-28 16:22:43 -0700471int strp_init(struct strparser *strp, struct sock *sk,
Tom Herbert43a0c672016-08-15 14:51:01 -0700472 struct strp_callbacks *cb)
473{
Tom Herbert96a59082016-08-28 14:43:19 -0700474
Tom Herbert43a0c672016-08-15 14:51:01 -0700475 if (!cb || !cb->rcv_msg || !cb->parse_msg)
476 return -EINVAL;
477
Tom Herbertbbb03022017-07-28 16:22:43 -0700478 /* The sk (sock) arg determines the mode of the stream parser.
479 *
480 * If the sock is set then the strparser is in receive callback mode.
481 * The upper layer calls strp_data_ready to kick receive processing
482 * and strparser calls the read_sock function on the socket to
483 * get packets.
484 *
485 * If the sock is not set then the strparser is in general mode.
486 * The upper layer calls strp_process for each skb to be parsed.
487 */
488
489 if (sk) {
490 struct socket *sock = sk->sk_socket;
491
492 if (!sock->ops->read_sock || !sock->ops->peek_len)
493 return -EAFNOSUPPORT;
494 } else {
495 if (!cb->lock || !cb->unlock)
496 return -EINVAL;
497 }
Tom Herbert96a59082016-08-28 14:43:19 -0700498
Tom Herbert43a0c672016-08-15 14:51:01 -0700499 memset(strp, 0, sizeof(*strp));
500
Tom Herbertbbb03022017-07-28 16:22:43 -0700501 strp->sk = sk;
Tom Herbert43a0c672016-08-15 14:51:01 -0700502
Tom Herbertbbb03022017-07-28 16:22:43 -0700503 strp->cb.lock = cb->lock ? : strp_sock_lock;
504 strp->cb.unlock = cb->unlock ? : strp_sock_unlock;
Tom Herbert43a0c672016-08-15 14:51:01 -0700505 strp->cb.rcv_msg = cb->rcv_msg;
506 strp->cb.parse_msg = cb->parse_msg;
507 strp->cb.read_sock_done = cb->read_sock_done ? : default_read_sock_done;
Tom Herbertbbb03022017-07-28 16:22:43 -0700508 strp->cb.abort_parser = cb->abort_parser ? : strp_abort_strp;
509
510 setup_timer(&strp->msg_timer, strp_msg_timeout,
511 (unsigned long)strp);
512
513 INIT_WORK(&strp->work, strp_work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700514
515 return 0;
516}
517EXPORT_SYMBOL_GPL(strp_init);
518
Tom Herbertcff6a332016-08-23 11:55:30 -0700519void strp_unpause(struct strparser *strp)
520{
Tom Herbertbbb03022017-07-28 16:22:43 -0700521 strp->paused = 0;
Tom Herbertcff6a332016-08-23 11:55:30 -0700522
Tom Herbertbbb03022017-07-28 16:22:43 -0700523 /* Sync setting paused with RX work */
Tom Herbertcff6a332016-08-23 11:55:30 -0700524 smp_mb();
525
Tom Herbertbbb03022017-07-28 16:22:43 -0700526 queue_work(strp_wq, &strp->work);
Tom Herbertcff6a332016-08-23 11:55:30 -0700527}
528EXPORT_SYMBOL_GPL(strp_unpause);
529
Tom Herbert96a59082016-08-28 14:43:19 -0700530/* strp must already be stopped so that strp_recv will no longer be called.
Tom Herbert43a0c672016-08-15 14:51:01 -0700531 * Note that strp_done is not called with the lower socket held.
532 */
533void strp_done(struct strparser *strp)
534{
Tom Herbertbbb03022017-07-28 16:22:43 -0700535 WARN_ON(!strp->stopped);
Tom Herbert43a0c672016-08-15 14:51:01 -0700536
Tom Herbertbbb03022017-07-28 16:22:43 -0700537 del_timer_sync(&strp->msg_timer);
538 cancel_work_sync(&strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700539
Tom Herbertbbb03022017-07-28 16:22:43 -0700540 if (strp->skb_head) {
541 kfree_skb(strp->skb_head);
542 strp->skb_head = NULL;
Tom Herbert43a0c672016-08-15 14:51:01 -0700543 }
544}
545EXPORT_SYMBOL_GPL(strp_done);
546
547void strp_stop(struct strparser *strp)
548{
Tom Herbertbbb03022017-07-28 16:22:43 -0700549 strp->stopped = 1;
Tom Herbert43a0c672016-08-15 14:51:01 -0700550}
551EXPORT_SYMBOL_GPL(strp_stop);
552
553void strp_check_rcv(struct strparser *strp)
554{
Tom Herbertbbb03022017-07-28 16:22:43 -0700555 queue_work(strp_wq, &strp->work);
Tom Herbert43a0c672016-08-15 14:51:01 -0700556}
557EXPORT_SYMBOL_GPL(strp_check_rcv);
558
559static int __init strp_mod_init(void)
560{
561 strp_wq = create_singlethread_workqueue("kstrp");
562
563 return 0;
564}
565
566static void __exit strp_mod_exit(void)
567{
WANG Congf78ef7c2017-03-03 12:21:14 -0800568 destroy_workqueue(strp_wq);
Tom Herbert43a0c672016-08-15 14:51:01 -0700569}
570module_init(strp_mod_init);
571module_exit(strp_mod_exit);
572MODULE_LICENSE("GPL");